-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathpushme.js
More file actions
127 lines (112 loc) · 3.08 KB
/
pushme.js
File metadata and controls
127 lines (112 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/**
* pushme.js - jQuery Plugin
* Push elements into specific containers depended on certain media queries
*
* @dependencies jQuery v1.5.0 http://jquery.com
* @author Cornel Boppart <cornel@bopp-art.com>
* @copyright Author
* @version 2.1.1 (07/01/2016)
*/
;(function ($) {
var pushme = {
labels: {
log: {
pushed: ' was pushed to ',
mediaQueriesNotSupported: 'Your browser does not support media queries.'
},
error: {
notFound: ' could not get found.'
}
},
/**
* Default settings
*
*/
settings: {
element: '#example',
pushAction: 'appendTo',
mq: 'screen and (min-width:768px)',
log: false
},
/**
* Initializes the plugin
*
* @param {object} options The custom plugin options (Not yet merged with default settings)
* @return {object} this The current element itself
*/
init: function (options) {
var settings = $.extend(pushme.settings, options);
return this.each(function () {
var $this = $(this);
if (pushme.checkPrerequisites($this, settings) === true) {
pushme.pushTo($this, settings);
}
});
},
/**
* Pushes the element to somewhere
*
* @param {object} $object The element that should be pushed
* @param {object} settings All custom plugin settings
* @return {void}
*/
pushTo: function ($object, settings) {
if (pushme.matchMedia()(settings.mq).matches) {
$object[settings.pushAction]($(settings.element));
if (settings.log === true) {
console.info(
$object.selector + pushme.labels.log.pushed + settings.element +
' (action: ' + settings.pushAction + ', mq: ' + settings.mq + ')'
);
}
}
},
/**
* Returns the supported match media
*
* @return {mixed} The supported media query or undefined if the browser doesn't support match media
*/
matchMedia: function () {
return window.matchMedia || window.msMatchMedia;
},
/**
* Checks if all prerequisites are given
*
* @param {object} $object The object to check if it exists
* @param {object} settings All custom plugin settings (Merged with default settings)
* @return {boolean} True if all checks were passed, else false
*/
checkPrerequisites: function ($object, settings) {
if (typeof pushme.matchMedia() !== 'function') {
if (settings.log === true) {
console.info(pushme.labels.log.mediaQueriesNotSupported);
}
return false;
}
if ($(settings.element).length < 1) {
if (settings.log === true) {
console.error(settings.element + pushme.labels.error.notFound);
}
return false;
}
if ($object.length < 1) {
if (settings.log === true) {
console.error($object.selector + pushme.labels.error.notFound);
}
return false;
}
return true;
}
};
$.fn.pushme = function (method) {
if (pushme[method]) {
return pushme[method].apply(this, Array.prototype.slice.call(arguments, 1));
}
else if (typeof method === 'object' || !method) {
return pushme.init.apply(this, arguments);
}
else {
return $.error('Method ' + method + ' does not exist on jQuery.pushme');
}
};
})(jQuery);