1/** 2 * @file 3 * The Lazy-load behavior. 4 */ 5 6((Drupal) => { 7 Drupal.behaviors.lazy = { 8 attach(context, settings) { 9 const utils = { 10 extend(obj, src) { 11 Object.keys(src).forEach((key) => { 12 obj[key] = src[key]; 13 }); 14 return obj; 15 }, 16 once(selector) { 17 return (context || document).querySelector(selector); 18 }, 19 loadScript(url) { 20 if (document.querySelectorAll(`script[src="${url}"]`).length === 0) { 21 const script = document.createElement('script'); 22 const scripts = document.getElementsByTagName('script')[0]; 23 script.src = url; 24 script.async = true; 25 scripts.parentNode.insertBefore(script, scripts); 26 } 27 }, 28 }; 29 30 if (utils.once('body', context)) { 31 const lazysizes = settings.lazy.lazysizes || {}; 32 33 if (!settings.lazy.preferNative) { 34 // 1. Lazysizes configuration. 35 window.lazySizesConfig = window.lazySizesConfig || {}; 36 window.lazySizesConfig = utils.extend( 37 window.lazySizesConfig, 38 lazysizes, 39 ); 40 // 2. Load all selected lazysizes plugins. 41 if (!Object.entries) { 42 Object.entries = (obj) => { 43 const ownProps = Object.keys(obj); 44 let i = ownProps.length; 45 const resArray = new Array(i); 46 while (i--) { 47 resArray[i] = [ownProps[i], obj[ownProps[i]]]; 48 } 49 return resArray; 50 }; 51 } 52 const min = settings.lazy.minified ? '.min' : ''; 53 Object.entries(lazysizes.plugins).forEach((path) => { 54 utils.loadScript( 55 `${settings.lazy.libraryPath}/plugins/${path[1]}${min}.js`, 56 ); 57 }); 58 // 3. Load the lazysizes library. 59 utils.loadScript(`${settings.lazy.libraryPath}/lazysizes${min}.js`); 60 } 61 } 62 }, 63 }; 64})(Drupal);
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.