forked from lesjames/breakpoint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.breakpoint.js
281 lines (205 loc) · 8.24 KB
/
jquery.breakpoint.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
/*!
* jQuery Breakpoint plugin v4.6.4
* http://github.com/lesjames/breakpoint
*
* MIT License
*/
(function (factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['jquery', 'imagesloaded', 'eventEmitter/EventEmitter'], factory);
} else {
// Browser globals
factory(window.jQuery, window.imagesLoaded, window.EventEmitter);
}
}(function ($, imagesLoaded, EventEmitter) {
// breakpoint functions
// ==========================================================================
// some craxy regex to deal with how browsers pass the JSON through CSS
function removeQuotes(string) {
if (typeof string === 'string' || string instanceof String) {
string = string.replace(/^['"]+|\s+|\\|(;\s?})+|['"]$/g, '');
}
return string;
}
// get the breakpoint labels from the body's css generated content
function getBreakpoint() {
var style = null;
// modern browser can read the label
if (window.getComputedStyle && window.getComputedStyle(document.body, '::before')) {
// grab json from css generated content
style = window.getComputedStyle(document.body, '::before');
style = style.content;
} else {
// older browsers need some help
var getComputedFallback = function(el) {
this.el = el;
this.getPropertyValue = function(prop) {
var re = /(\-([a-z]){1})/g;
if (re.test(prop)) {
prop = prop.replace(re, function () {
return arguments[2].toUpperCase();
});
}
return el.currentStyle[prop] ? el.currentStyle[prop] : null;
};
return this;
};
// fallback label is added as a font-family to the head, thanks Jeremy Keith
style = getComputedFallback(document.getElementsByTagName('head')[0]);
style = style.getPropertyValue('font-family');
}
// parse that sucka and return it
return JSON.parse(removeQuotes(style));
}
// finds the source of an image by matching breakpoint label to image data attr
function findSource($image, set) {
// try the current breakpoint first
var src = $image.attr('data-' + set.options.prefix + set.breakpoint.current),
i = set.breakpoint.position - 1;
// if no match is found walk backwards through the
// labels array until a matching data attr is found
if (src === undefined) {
for (i; i >= 0; i = i - 1) {
src = $image.attr('data-' + set.options.prefix + set.breakpoint.all[i]);
if (src !== undefined) {
break;
}
}
}
return src;
}
function setSource($image, set) {
var src = findSource($image, set);
if (src) {
// cached images don't fire load sometimes, so we reset src
$image.attr('src', 'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==');
$image.attr('src', src);
}
return $image;
}
// callback and deferred execution
function breakpointComplete(set) {
// this requires imagesLoaded and that requires querySelector
if (imagesLoaded && document.querySelectorAll && set.images.length > 0) {
var load = imagesLoaded(set.images);
load.on('done', function ( instance ) {
set.dfd.resolve( set.breakpoint, instance );
if ( $.isFunction( set.callback ) ) {
set.callback.call( null, set.breakpoint, instance );
}
});
load.on('fail', function ( instance ) {
set.dfd.reject( set.breakpoint, instance );
});
load.on('progress', function( instance, image ) {
set.dfd.notify( set.breakpoint, instance, image );
});
} else {
// no images here or imagesLoaded isn't loaded,
// just send back the breakpoint info
set.dfd.resolve( set.breakpoint );
if ( $.isFunction( set.callback ) ) {
set.callback.call( null, set.breakpoint );
}
}
}
// Breakpoint Constructor
// ==========================================================================
function BreakpointImages( images ) {
// keep a reference to this
var _this = this;
// store the selected images
this.images = images;
// create callback and deferred
this.callback = null;
this.dfd = $.Deferred();
// breakpoint default options
this.options = {
prefix: '',
debug: false
};
// check to see if the breakpoint has changed
this.checkBreakpoint = function () {
// grab the latest breakpoint so we can see it if updated
var latestBreakpoint = getBreakpoint();
// if the breakpoint is set and it matches the previous value bail out
if (_this.breakpoint && _this.breakpoint.current === latestBreakpoint.current) {
return;
}
// breakpoint is new so save it
_this.breakpoint = latestBreakpoint;
_this.processImages();
};
// process the images with the current breakpoint
this.processImages = function () {
if (_this.images.length > 0) {
_this.images.each(function () {
setSource( $(this), _this );
});
}
breakpointComplete(_this);
};
// init event listener
this.on('breakpoint.init', function () {
_this.checkBreakpoint();
// return true removes this listener
return true;
});
// window resize listener
this.on('breakpoint.update', _this.checkBreakpoint);
// for removing the resize listener
this.removeEvent = function () {
_this.off('breakpoint.update', _this.checkBreakpoint);
};
}
// add event methods
BreakpointImages.prototype = new EventEmitter();
// define the plugin
// ==========================================================================
$.fn.breakpoint = function (options, callback) {
var $images = [];
if (this[0] !== document) {
// reduce selection to only images
$images = this.find('img').add( this.filter('img') );
}
// create an instance and pass it selected images
var breakpointImages = new BreakpointImages( $images );
// if callback passed as only argument
if ( $.isFunction( options ) ) {
callback = options;
options = {};
}
// store options and callback on instance
breakpointImages.options = $.extend({}, breakpointImages.options, options);
breakpointImages.callback = callback;
// event bindings
// ==========================================================================
// get things started
$(document).ready(function () {
breakpointImages.trigger('breakpoint.init');
});
// reevaluate breakpoint when window resizes
$(window).on('resize.breakpoint', function () {
breakpointImages.trigger('breakpoint.update');
});
// remove listeners on unload
$(window).on('unload.breakpoint', function () {
$(window).off('.breakpoint');
breakpointImages.removeEvent();
});
if (breakpointImages.options.debug) {
// if debug return functions for testing
return {
breakpointImages: breakpointImages,
removeQuotes: removeQuotes,
getBreakpoint: getBreakpoint,
findSource: findSource,
setSource: setSource
};
} else {
// return the deferred and attach it to the instance
return breakpointImages.dfd.promise( breakpointImages );
}
};
}));