-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.mq-sync.js
326 lines (273 loc) · 9.02 KB
/
jquery.mq-sync.js
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
(function (factory) {
if (typeof module === "object" && typeof module.exports === "object") {
module.exports = factory(require("jquery"), window, document);
} else {
factory(jQuery, window, document);
}
}(function($, window, document) {
var $body = $('body'),
$orderElement = $('title'),
$listenElement = $('head'),
mqOrderNamed = !!(mqOrderNamed) ? mqOrderNamed : {},
mqOrderNumbered = !!(mqOrderNumbered) ? mqOrderNumbered : [],
currentMediaQuery,
mqSync = {},
/**
* Set up the media query plugin
*/
init = function() {
// setup public stuff
mqSync = {
listenElement: $listenElement,
responsiveImages: responsiveImages,
isBelow: checkBelow,
isAbove: checkAbove
};
currentMediaQuery = fetchMediaQuery();
$body.data('media-query', currentMediaQuery);
// set the order of the breakpoints
setOrder();
// On window resize, set media query var
$(window).resize(onResize);
$.mqSync = mqSync;
},
/**
* Check if the media query name is a match
* @param which The media query to check against
*/
matches = function(which) {
// See if the current media query matches the requested one
return (fetchMediaQuery() == which);
},
/**
* Check if the media query is greater than the specified
* @param which The media query to check against
*/
checkAbove = function (which) {
var currentMq = mqOrderNamed[fetchMediaQuery()],
whichMq = mqOrderNamed[which];
return (currentMq >= whichMq);
},
/**
* Check if the media query is less than the specified
* @param which The media query to check against
*/
checkBelow = function (which) {
var currentMq = mqOrderNamed[fetchMediaQuery()],
whichMq = mqOrderNamed[which];
return (currentMq < whichMq);
},
/**
* When the browser is resized, update the media query
*/
onResize = function () {
var lastQuery = currentMediaQuery;
// Set the global current media query
currentMediaQuery = fetchMediaQuery();
// The media query does not match the old
if (currentMediaQuery != lastQuery) {
// Fire an event noting that the media query has changed
mqSync.listenElement.trigger('mediaQueryChange', [currentMediaQuery, lastQuery]);
$body.data('media-query', currentMediaQuery);
}
},
/**
* Read in the media query
*/
fetchMediaQuery = function () {
// We read in the media query name from the html element's font family
var mq = mqSync.listenElement.css('font-family');
// Strip out quotes and commas
mq = mq.replace(/['",]/g, '');
return mq;
},
/**
* Set the order of media queries
* @param orderedArray An array of the media queries in order from smallest to largest
*/
setOrder = function () {
var mediaQueries = $orderElement.css('font-family');
mqOrderNumbered = mediaQueries.replace("'", "").split(',');
$.each(mqOrderNumbered, function(index, value) {
mqOrderNamed[value] = index;
});
},
/**
* This module resizes responsive images automatically
* @param $el jQuery object or string element reference
*/
responsiveImages = function($el) {
// set up valid images variable. checks if the param is a string or jquery element
var $images = ( typeof $el === 'string' ) ? $($el) : $el,
// set up the responsive images to manipulate. uses default if undefined
$responsiveImages = $images ? $images : $('.mqsync-responsive'),
// array to hold all known src's attached to images
knownSizes = [],
init = function() {
var $img;
// When the media query changes
mqSync.listenElement.on('mediaQueryChange', onMediaQueryChange);
// Loop through each and store its original source
$responsiveImages.each(function() {
$img = $(this);
storeSizes($img);
});
// Update the current responsive image size
update();
},
// Every time the media query changes, do these things
onMediaQueryChange = function(event, newMediaQuery) {
update(newMediaQuery);
},
/**
* Store the image sources attached to each responsive image, making each check on mq change more effecient
* @param $image Which element to pull sizes from
*/
storeSizes = function($image) {
var $img = $image,
sizes = {},
mqName;
sizes.image = $img;
sizes.loaded = [];
sizes['original-src'] = sizes['active-src'] = getSource($img);
// loop over all the data attr's on the image
$.each($img.data(), function(key, value) {
// jQuery turns data attr's into camelcase strings, so make sure they are dashed instead
mqName = toDashed(key);
// make sure the stores src is an absolute url
sizes[mqName] = value;
});
// add the sizes for this image to the array
knownSizes.push(sizes);
},
/**
* Turns camelcase string into dashed
* @param string The string to manipulate
*/
toDashed = function(string) {
var words = [],
currentChar = '',
currentWord = '',
i = 0;
for (i; string.length >= i; i++) {
currentChar = string.charAt(i);
if ( currentChar === currentChar.toUpperCase() ) {
words.push(currentWord);
currentWord = currentChar.toLowerCase();
} else {
currentWord = currentWord + currentChar;
}
}
words = words.join('-');
return words;
},
/**
* Return the current image source
* @param $image Which element to store the source on
*/
getSource = function ($image) {
var imageSrc;
if ($image.is('img')) {
imageSrc = $image.attr('src');
} else {
imageSrc = $image.css('background-image').replace(/^url\(['"]?/,'').replace(/['"]?\)$/,'');
}
return imageSrc;
},
/**
* Run through each responsive image and see if an image exists at that media query
* @param newMediaQuery [current] The new media query to load
*/
update = function (newMediaQuery) {
var isLoaded = false;
// Default to the current media query - just run an update
if (newMediaQuery == null)
newMediaQuery = fetchMediaQuery();
// Loop over each known size and update the image src if it has one for this breakpoint
$.each(knownSizes, function (i) {
// store the current image object in the loop
var currentImage = knownSizes[i],
// store the new source from the know sizes if there is one
newSource = currentImage[newMediaQuery];
// if a new source isn't set
if (!newSource) {
var ii = mqOrderNamed[newMediaQuery],
mq;
// decrement through the numbered mq's
for (ii; ii > 0; ii--) {
mq = mqOrderNumbered[ii];
// if a matched mq is found on the image
if (currentImage[mq]) {
// set the new source
newSource = currentImage[mq];
break; // break that loop
}
}
// if after all that no source was found then just revert back to the original source
newSource = newSource || currentImage['original-src'];
}
// if the new source is not the active source
if (newSource.indexOf(currentImage['active-src']) === -1) {
// loop over all loaded images and see if the new source has been loaded
$.each(currentImage.loaded, function(i) {
if (currentImage.loaded[i].indexOf(newSource) > 0) {
isLoaded = true;
return;
}
});
// if the new source has been loaded
if (isLoaded) {
// There is an image supplied for this media query
swapImage(currentImage.image, newSource);
} else {
// preload the image to swap
preload(newSource, function(src) {
// There is an image supplied for this media query
swapImage(currentImage.image, src);
// update the list of loaded src's
currentImage.loaded.push(src);
});
}
// update the active src
currentImage['active-src'] = newSource;
}
});
},
/**
* Swap out either an <img>s source or the background image of another element.
* @param $target The jQuery element you want to swap an image on
* @param newImageSrc The source for the new image to use
*/
swapImage = function($target, newImageSrc) {
if ($target.is('img')) {
$target.attr('src', newImageSrc);
} else {
$target.css('background-image', 'url(' + newImageSrc + ')');
}
},
/**
* Preload the new image before inserting it into the DOM
* @param src The source of the image to load
* @param callback The function to call when the image is loaded
*/
preload = function(src, callback) {
var img, imgLoaded = $.Deferred();
(function(url, imgLoaded) {
img = new Image();
img.onload = function() {
imgLoaded.resolve();
};
img.src = url;
})(src, imgLoaded);
// when the image is loaded trigger the callback
$.when($, imgLoaded).then(function() {
callback(img.src);
// clear the memory of that image
img = null;
});
};
init();
};
// set everything up
init();
}));