1/** 2 * [jQuery-lazyload-any]{@link https://github.com/emn178/jquery-lazyload-any} 3 * 4 * @version 0.3.1 5 * @author Yi-Cyuan Chen [[email protected]] 6 * @copyright Yi-Cyuan Chen 2014-2016 7 * @license MIT 8 * 9 * Extended by Michael Dempfle to replace placeholders by Javascript variables 10 * _JS_variable__ will be repaced by the global variable. 11 * 12 * var test="bla" 13 * "_JS_test__" will be replaced by "bla" 14 * 15 */ 16(function ($, window, document) { 17 var KEY = 'jquery-lazyload-any'; 18 var EVENT = 'appear'; 19 var SELECTOR_KEY = KEY + '-' + EVENT; 20 var SELECTOR = ':' + SELECTOR_KEY; 21 var SCROLLER_KEY = KEY + '-scroller'; 22 var DISPLAY_KEY = KEY + '-display'; 23 var WATCH_KEY = KEY + '-watch'; 24 var DIV = $('<div/>'); 25 var screenHeight, screenWidth, init = false, observations = $(); 26 27 $.expr[':'][SELECTOR_KEY] = function (element) { 28 return $(element).data(SELECTOR_KEY) !== undefined; 29 }; 30 31 function test() { 32 var element = $(this); 33 if (element.is(':visible') && visible(element)) { 34 element.trigger(EVENT); 35 } 36 } 37 38 function visible(element) { 39 var rect = element[0].getBoundingClientRect(); 40 var x1 = -element.data(KEY).threshold; 41 var y1 = x1; 42 var y2 = screenHeight - y1; 43 var x2 = screenWidth - x1; 44 return (rect.top >= y1 && rect.top <= y2 || rect.bottom >= y1 && rect.bottom <= y2) && 45 (rect.left >= x1 && rect.left <= x2 || rect.right >= x1 && rect.right <= x2); 46 } 47 48 function resize() { 49 screenHeight = window.innerHeight || document.documentElement.clientHeight; 50 screenWidth = window.innerWidth || document.documentElement.clientWidth; 51 detect(); 52 } 53 54 function detect() { 55 observations = observations.filter(SELECTOR); 56 if (this.nodeType == 1) { 57 $(this).find(SELECTOR).each(test); 58 } else { 59 observations.each(test); 60 } 61 } 62 63 function show() { 64 var element = $(this); 65 var options = element.data(KEY); 66 var content = element.data('lazyload'); 67 if (!content) { 68 var script = element.children().filter('script[type="text/lazyload"]').get(0); 69 content = $(script).html(); 70 // we filter _JS_variable__ from the content 71 var resultArray = content.match(/_JS_(.*)__/g); 72 if (resultArray != null) { 73 var arrayLength = resultArray.length; 74 try { 75 for (var i = 0; i < arrayLength; i++) { 76 alert(resultArray[i]); 77 var variable = resultArray[i].slice(4,-2); 78 alert(variable); 79 content = content.replace(resultArray[i], window[variable]); 80 } 81 } catch(err) { 82 if (console && console.error) { 83 console.error('Advanced iframe: The configured variable cannot be found. Please check your configuration.'); 84 } 85 } 86 } 87 } 88 if (!content) { 89 var comment = element.contents().filter(function () { 90 return this.nodeType === 8; 91 }).get(0); 92 content = comment && $.trim(comment.data); 93 } 94 var newElement = DIV.html(content).contents(); 95 element.replaceWith(newElement); 96 if ($.isFunction(options.load)) { 97 options.load.call(newElement, newElement); 98 } 99 } 100 101 function watch() { 102 var element = $(this); 103 if (!(watchScroller(element) | watchDisplay(element))) { 104 return; 105 } 106 if (element.data(WATCH_KEY)) { 107 return; 108 } 109 element.data(WATCH_KEY, 1); 110 element.bind(EVENT, clearWatch); 111 } 112 113 function watchScroller(element) { 114 if (element.data(SCROLLER_KEY)) { 115 return false; 116 } 117 var overflow = element.css('overflow'); 118 if (overflow != 'scroll' && overflow != 'auto') { 119 return false; 120 } 121 element.data(SCROLLER_KEY, 1); 122 element.bind('scroll', detect); 123 return true; 124 } 125 126 function watchDisplay(element) { 127 if (element.data(DISPLAY_KEY)) { 128 return; 129 } 130 var display = element.css('display'); 131 if (display != 'none') { 132 return; 133 } 134 element.data(DISPLAY_KEY, 1); 135 element._bindShow(detect); 136 return true; 137 } 138 139 function clearWatch() { 140 var element = $(this); 141 if (element.find(SELECTOR).length === 0) { 142 element.removeData(SCROLLER_KEY).removeData(DISPLAY_KEY).removeData(WATCH_KEY); 143 element.unbind('scroll', detect).unbind(EVENT, clearWatch)._unbindShow(detect); 144 } 145 } 146 147 function refresh(selector) { 148 var elements = selector === undefined ? observations : $(selector); 149 elements.each(function () { 150 var element = $(this); 151 if (!element.is(SELECTOR)) { 152 return; 153 } 154 element.parents().each(watch); 155 }); 156 } 157 158 $.fn.lazyload = function (options) { 159 var opts = { 160 threshold: 0, 161 trigger: EVENT 162 }; 163 $.extend(opts, options); 164 var trigger = opts.trigger.split(' '); 165 this.data(SELECTOR_KEY, $.inArray(EVENT, trigger) != -1).data(KEY, opts); 166 this.bind(opts.trigger, show); 167 this.each(test); 168 this.parents().each(watch); 169 this.each(function () { 170 observations = observations.add(this); 171 }); 172 173 if (!init) { 174 init = true; 175 resize(); 176 $(document).ready(function () { 177 $(window).bind('resize', resize).bind('scroll', detect); 178 }); 179 } 180 return this; 181 }; 182 183 $.lazyload = { 184 check: detect, 185 refresh: refresh 186 }; 187 188 // SHOW EVENT 189 (function () { 190 var EVENT = 'show'; 191 var SELECTOR_KEY = KEY + '-' + EVENT; 192 var SELECTOR = ':' + SELECTOR_KEY; 193 var interval = 50, timer, observations = $(); 194 195 $.expr[':'][SELECTOR_KEY] = function (element) { 196 return $(element).data(SELECTOR_KEY) !== undefined; 197 }; 198 199 function test() { 200 var element = $(this); 201 var status = element.css('display') != 'none'; 202 if (element.data(SELECTOR_KEY) != status) { 203 element.data(SELECTOR_KEY, status); 204 if (status) { 205 element.trigger(EVENT); 206 } 207 } 208 } 209 210 function detect() { 211 observations = observations.filter(SELECTOR); 212 observations.each(test); 213 if (observations.length === 0) { 214 timer = clearInterval(timer); 215 } 216 } 217 218 $.fn._bindShow = function (handler) { 219 this.bind(EVENT, handler); 220 this.data(SELECTOR_KEY, this.css('display') != 'none'); 221 observations = observations.add(this); 222 if (interval && !timer) { 223 timer = setInterval(detect, interval); 224 } 225 }; 226 227 $.fn._unbindShow = function (handler) { 228 this.unbind(EVENT, handler); 229 this.removeData(SELECTOR_KEY); 230 }; 231 232 $.lazyload.setInterval = function (value) { 233 if (value == interval || !$.isNumeric(value) || value < 0) { 234 return; 235 } 236 interval = value; 237 timer = clearInterval(timer); 238 if (interval > 0) { 239 timer = setInterval(detect, interval); 240 } 241 }; 242 })(); 243})(jQuery, window, document);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.