Debian Codegraph @

js/widgets/widget-lazyload.js

raw symbols SCIP
1 /*! Widget: lazyload (BETA) - 4/1/2016 (v2.25.7) *//*
2 * Requires tablesorter v2.8+ and jQuery 1.7+
3 * by Rob Garrison
4 */
5 /*jshint browser:true, jquery:true, unused:false */
6 ;( function( $, window ) {
7 'use strict';
8 var ts = $.tablesorter;
9
10 ts.lazyload = {
11 init : function( c, wo ) {
12 if ( wo.lazyload_event === 'scrollstop' && !ts.addScrollStopDone ) {
13 ts.addScrollStop();
14 ts.addScrollStopDone = true;
15 $.event.special.scrollstop.latency = wo.lazyload_latency || 250;
16 }
17 ts.lazyload.update( c, wo );
18 var namespace = c.namespace + 'lazyload ',
19 events = [
20 wo.lazyload_update,
21 'pagerUpdate',
22 wo.columnSelector_updated || 'columnUpdate',
23 ''
24 ].join( namespace );
25 c.$table
26 .on( events, function() {
27 ts.lazyload.update( c, c.widgetOptions );
28 })
29 .on( 'filterEnd' + namespace, function() {
30 // give lazyload a nudge after filtering the table. Fixes #1169
31 $(window).scroll();
32 });
33 },
34 update : function( c, wo ) {
35 // add '.' if not already included
36 var sel = ( /(\.|#)/.test( wo.lazyload_imageClass ) ? '' : '.' ) + wo.lazyload_imageClass;
37 c.$table.find( sel ).lazyload({
38 threshold : wo.lazyload_threshold,
39 failure_limit : wo.lazyload_failure_limit,
40 event : wo.lazyload_event,
41 effect : wo.lazyload_effect,
42 container : wo.lazyload_container,
43 data_attribute : wo.lazyload_data_attribute,
44 skip_invisible : wo.lazyload_skip_invisible,
45 appear : wo.lazyload_appear,
46 load : wo.lazyload_load,
47 placeholder : wo.lazyload_placeholder
48 });
49 // give lazyload a nudge after updating the table. Fixes #1169
50 setTimeout(function() {
51 $(window).scroll();
52 }, 1);
53 },
54 remove : function( c ) {
55 c.$table.off( c.namespace + 'lazyload' );
56 }
57 };
58
59 ts.addWidget({
60 id: 'lazyload',
61 options: {
62 // widget options
63 lazyload_imageClass : 'lazy',
64 lazyload_update : 'lazyloadUpdate',
65 // scrollStop option (https://github.com/ssorallen/jquery-scrollstop)
66 lazyload_latency : 250,
67 // lazyload options (see http://www.appelsiini.net/projects/lazyload)
68 lazyload_threshold : 0,
69 lazyload_failure_limit : 0,
70 lazyload_event : 'scrollstop',
71 lazyload_effect : 'show',
72 lazyload_container : window,
73 lazyload_data_attribute : 'original',
74 lazyload_skip_invisible : true,
75 lazyload_appear : null,
76 lazyload_load : null,
77 lazyload_placeholder : 'data:image/gif;base64,R0lGODlhAQABAIABAP///wAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw=='
78 },
79 init: function( table, thisWidget, c, wo ) {
80 ts.lazyload.init( c, wo );
81 },
82 remove : function( table, c, wo ) {
83 ts.lazyload.remove( c, wo );
84 }
85 });
86
87 // jscs:disable
88 ts.addScrollStop = function() {
89 // jQuery Scrollstop Plugin v1.2.0
90 // https://github.com/ssorallen/jquery-scrollstop
91 /*
92 (function (factory) {
93 // UMD[2] wrapper for jQuery plugins to work in AMD or in CommonJS.
94 //
95 // [2] https://github.com/umdjs/umd
96
97 if (typeof define === 'function' && define.amd) {
98 // AMD. Register as an anonymous module.
99 define(['jquery'], factory);
100 } else if (typeof exports === 'object') {
101 // Node/CommonJS
102 module.exports = factory(require('jquery'));
103 } else {
104 // Browser globals
105 factory(jQuery);
106 }
107 }(function ($) { */
108 // $.event.dispatch was undocumented and was deprecated in jQuery 1.7[1]. It
109 // was replaced by $.event.handle in jQuery 1.9.
110 //
111 // Use the first of the available functions to support jQuery <1.8.
112 //
113 // [1] https://github.com/jquery/jquery-migrate/blob/master/src/event.js#L25
114 var dispatch = $.event.dispatch || $.event.handle;
115
116 var special = $.event.special,
117 uid1 = 'D' + (+new Date()),
118 uid2 = 'D' + (+new Date() + 1);
119
120 special.scrollstart = {
121 setup: function(data) {
122 var _data = $.extend({
123 latency: special.scrollstop.latency
124 }, data);
125
126 var timer,
127 handler = function(evt) {
128 var _self = this,
129 _args = arguments;
130 if (timer) {
131 clearTimeout(timer);
132 } else {
133 evt.type = 'scrollstart';
134 dispatch.apply(_self, _args);
135 }
136 timer = setTimeout(function() {
137 timer = null;
138 }, _data.latency);
139 };
140 $(this).bind('scroll', handler).data(uid1, handler);
141 },
142 teardown: function() {
143 $(this).unbind('scroll', $(this).data(uid1));
144 }
145 };
146 special.scrollstop = {
147 latency: 250,
148 setup: function(data) {
149 var _data = $.extend({
150 latency: special.scrollstop.latency
151 }, data);
152 var timer,
153 handler = function(evt) {
154 var _self = this,
155 _args = arguments;
156 if (timer) {
157 clearTimeout(timer);
158 }
159 timer = setTimeout(function() {
160 timer = null;
161 evt.type = 'scrollstop';
162 dispatch.apply(_self, _args);
163 }, _data.latency);
164 };
165 $(this).bind('scroll', handler).data(uid2, handler);
166 },
167 teardown: function() {
168 $(this).unbind('scroll', $(this).data(uid2));
169 }
170 };
171 /*
172 }));
173 */
174
175 };
176 // jscs:enable
177
178 })( jQuery, window );
179
180 // jscs:disable
181 /*!
182 * Lazy Load - jQuery plugin for lazy loading images
183 *
184 * Copyright (c) 2007-2015 Mika Tuupola
185 *
186 * Licensed under the MIT license:
187 * http://www.opensource.org/licenses/mit-license.php
188 *
189 * Project home:
190 * http://www.appelsiini.net/projects/lazyload
191 *
192 * Version: 1.9.7
193 *
194 */
195 (function($, window, document, undefined) {
196 var $window = $(window);
197 $.fn.lazyload = function(options) {
198 var elements = this;
199 var $container;
200 var settings = {
201 threshold : 0,
202 failure_limit : 0,
203 event : 'scroll',
204 effect : 'show',
205 container : window,
206 data_attribute : 'original',
207 skip_invisible : false,
208 appear : null,
209 load : null,
210 placeholder : 'data:image/gif;base64,R0lGODlhAQABAIABAP///wAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw=='
211 };
212 function update() {
213 var counter = 0;
214 elements.each(function() {
215 var $this = $(this);
216 if (settings.skip_invisible && !$this.is(':visible')) {
217 return;
218 }
219 if ($.abovethetop(this, settings) || $.leftofbegin(this, settings)) {
220 /* Nothing. */
221 } else if (!$.belowthefold(this, settings) && !$.rightoffold(this, settings)) {
222 $this.trigger('appear');
223 /* if we found an image we'll load, reset the counter */
224 counter = 0;
225 } else {
226 if (++counter > settings.failure_limit) {
227 return false;
228 }
229 }
230 });
231 }
232 if (options) {
233 /* Maintain BC for a couple of versions. */
234 if (undefined !== options.failurelimit) {
235 options.failure_limit = options.failurelimit;
236 delete options.failurelimit;
237 }
238 if (undefined !== options.effectspeed) {
239 options.effect_speed = options.effectspeed;
240 delete options.effectspeed;
241 }
242 $.extend(settings, options);
243 }
244 /* Cache container as jQuery as object. */
245 $container = (settings.container === undefined ||
246 settings.container === window) ? $window : $(settings.container);
247 /* Fire one scroll event per scroll. Not one scroll event per image. */
248 if (0 === settings.event.indexOf('scroll')) {
249 $container.bind(settings.event, function() {
250 return update();
251 });
252 }
253 this.each(function() {
254 var self = this;
255 var $self = $(self);
256 self.loaded = false;
257 /* If no src attribute given use data:uri. */
258 if ($self.attr('src') === undefined || $self.attr('src') === false) {
259 if ($self.is('img')) {
260 $self.attr('src', settings.placeholder);
261 }
262 }
263 /* When appear is triggered load original image. */
264 $self.one('appear', function() {
265 if (!this.loaded) {
266 if (settings.appear) {
267 var elements_left = elements.length;
268 settings.appear.call(self, elements_left, settings);
269 }
270 $('<img />')
271 .bind('load', function() {
272 var original = $self.attr('data-' + settings.data_attribute);
273 $self.hide();
274 if ($self.is('img')) {
275 $self.attr('src', original);
276 } else {
277 $self.css('background-image', 'url("' + original + '")');
278 }
279 $self[settings.effect](settings.effect_speed);
280 self.loaded = true;
281 /* Remove image from array so it is not looped next time. */
282 var temp = $.grep(elements, function(element) {
283 return !element.loaded;
284 });
285 elements = $(temp);
286 if (settings.load) {
287 var elements_left = elements.length;
288 settings.load.call(self, elements_left, settings);
289 }
290 })
291 .attr('src', $self.attr('data-' + settings.data_attribute));
292 }
293 });
294 /* When wanted event is triggered load original image */
295 /* by triggering appear. */
296 if (0 !== settings.event.indexOf('scroll')) {
297 $self.bind(settings.event, function() {
298 if (!self.loaded) {
299 $self.trigger('appear');
300 }
301 });
302 }
303 });
304 /* Check if something appears when window is resized. */
305 $window.bind('resize', function() {
306 update();
307 });
308 /* With IOS5 force loading images when navigating with back button. */
309 /* Non optimal workaround. */
310 if ((/(?:iphone|ipod|ipad).*os 5/gi).test(navigator.appVersion)) {
311 $window.bind('pageshow', function(event) {
312 if (event.originalEvent && event.originalEvent.persisted) {
313 elements.each(function() {
314 $(this).trigger('appear');
315 });
316 }
317 });
318 }
319 /* Force initial check if images should appear. */
320 $(document).ready(function() {
321 update();
322 });
323 return this;
324 };
325 /* Convenience methods in jQuery namespace. */
326 /* Use as $.belowthefold(element, {threshold : 100, container : window}) */
327 $.belowthefold = function(element, settings) {
328 var fold;
329 if (settings.container === undefined || settings.container === window) {
330 fold = (window.innerHeight ? window.innerHeight : $window.height()) + $window.scrollTop();
331 } else {
332 fold = $(settings.container).offset().top + $(settings.container).height();
333 }
334 return fold <= $(element).offset().top - settings.threshold;
335 };
336 $.rightoffold = function(element, settings) {
337 var fold;
338 if (settings.container === undefined || settings.container === window) {
339 fold = $window.width() + $window.scrollLeft();
340 } else {
341 fold = $(settings.container).offset().left + $(settings.container).width();
342 }
343 return fold <= $(element).offset().left - settings.threshold;
344 };
345 $.abovethetop = function(element, settings) {
346 var fold;
347 if (settings.container === undefined || settings.container === window) {
348 fold = $window.scrollTop();
349 } else {
350 fold = $(settings.container).offset().top;
351 }
352 return fold >= $(element).offset().top + settings.threshold + $(element).height();
353 };
354 $.leftofbegin = function(element, settings) {
355 var fold;
356 if (settings.container === undefined || settings.container === window) {
357 fold = $window.scrollLeft();
358 } else {
359 fold = $(settings.container).offset().left;
360 }
361 return fold >= $(element).offset().left + settings.threshold + $(element).width();
362 };
363 $.inviewport = function(element, settings) {
364 return !$.rightoffold(element, settings) && !$.leftofbegin(element, settings) &&
365 !$.belowthefold(element, settings) && !$.abovethetop(element, settings);
366 };
367 /* Custom selectors for your convenience. */
368 /* Use as $('img:below-the-fold').something() or */
369 /* $('img').filter(':below-the-fold').something() which is faster */
370 $.extend($.expr[':'], {
371 'below-the-fold' : function(a) { return $.belowthefold(a, {threshold : 0}); },
372 'above-the-top' : function(a) { return !$.belowthefold(a, {threshold : 0}); },
373 'right-of-screen': function(a) { return $.rightoffold(a, {threshold : 0}); },
374 'left-of-screen' : function(a) { return !$.rightoffold(a, {threshold : 0}); },
375 'in-viewport' : function(a) { return $.inviewport(a, {threshold : 0}); },
376 /* Maintain BC for couple of versions. */
377 'above-the-fold' : function(a) { return !$.belowthefold(a, {threshold : 0}); },
378 'right-of-fold' : function(a) { return $.rightoffold(a, {threshold : 0}); },
379 'left-of-fold' : function(a) { return !$.rightoffold(a, {threshold : 0}); }
380 });
381 })(jQuery, window, document);