client_hints-8.x-1.0-alpha3/js/client_hints.lazy.js
js/client_hints.lazy.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 | /** * @file client_hints.lazy.js * * Utility functions for recurring scheduling widget. */ ( function ($, Drupal) { 'use strict' ; /** * Registers behaviours related to entity reference field widget. */ Drupal.behaviors.clientHintsLazy = { attach: function (context) { $(context).imagesLoaded( function () { $( this .images).each( function () { if ( typeof this .img.dataset.clientHintsSrc !== 'undefined' ) { // Cache element var img = $( this .img); // Determine DPR var dpr = window.devicePixelRatio || 1; // Go with image width by default & only compute target width if max-width is set var width = this .img.width; if (img.css( 'max-height' ) !== 'none' ) { // Calculate target width by taking into account source dimensions width = Math.ceil(img.attr( 'width' ) / img.attr( 'height' ) * this .img.height); } var parent = img.parent(); parent.imagesLoaded( function () { $( this .images).each( function () { parent.removeClass( 'loading' ); }); }); // Replace placeholder with appropriately sized actual image this .img.src = '/image?file=' + this .img.dataset.clientHintsSrc + '&dpr=' + dpr + '&width=' + width; } }); }); } }; }(jQuery, Drupal)); |