3 * Provides bLazy loader.
6 (function (Drupal, drupalSettings, _db, window, document) {
11 * Blazy public methods.
15 Drupal.blazy = Drupal.blazy || {
19 globals: function () {
21 var settings = drupalSettings.blazy || {};
27 return _db.extend(settings, commons);
30 clearing: function (el) {
31 // The .b-lazy element can be attached to IMG, or DIV as CSS background.
32 el.className = el.className.replace(/(\S+)loading/, '');
34 // The .is-loading can be .grid, .slide__content, .box__content, etc.
36 _db.closest(el, '.is-loading'),
37 _db.closest(el, '[class*="loading"]')
40 // Also cleans up closest containers containing loading class.
41 _db.forEach(loaders, function (wrapEl) {
42 if (wrapEl !== null) {
43 wrapEl.className = wrapEl.className.replace(/(\S+)loading/, '');
50 * Blazy utility functions.
52 * @param {HTMLElement} elm
53 * The Blazy HTML element.
55 function doBlazy(elm) {
56 var me = Drupal.blazy;
57 var dataAttr = elm.getAttribute('data-blazy');
58 var empty = dataAttr === '' || dataAttr === '[]';
59 var data = empty ? false : _db.parse(dataAttr);
60 var opts = !data ? me.globals() : _db.extend({}, me.globals(), data);
61 var ratios = elm.querySelectorAll('[data-dimensions]');
62 var loopRatio = ratios.length > 0;
65 * Updates the dynamic multi-breakpoint aspect ratio.
67 * This only applies to multi-serving images with aspect ratio fluid if
68 * each element contains [data-dimensions] attribute.
69 * Static single aspect ratio, e.g. `media--ratio--169`, will be ignored,
70 * and will use CSS instead.
72 * @param {HTMLElement} el
73 * The .media--ratio HTML element.
75 function updateRatio(el) {
76 var dimensions = !el.getAttribute('data-dimensions') ? false : _db.parse(el.getAttribute('data-dimensions'));
82 var keys = Object.keys(dimensions);
84 var xl = keys[keys.length - 1];
85 var mw = function (w) {
86 return w >= me.windowWidth;
88 var pad = keys.filter(mw).map(function (v) {
92 if (pad === 'undefined') {
93 pad = dimensions[me.windowWidth >= xl ? xl : xs];
96 if (pad !== 'undefined') {
97 el.style.paddingBottom = pad + '%';
101 // Initializes Blazy instance.
102 me.init = new Blazy(opts);
104 // Reacts on resizing.
106 _db.resize(function () {
107 me.windowWidth = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
110 _db.forEach(ratios, updateRatio, elm);
113 // Dispatch resizing event.
114 _db.trigger(elm, 'resizing', {windowWidth: me.windowWidth});
120 elm.className += ' blazy--on';
124 * Attaches blazy behavior to HTML element identified by [data-blazy].
126 * @type {Drupal~behavior}
128 Drupal.behaviors.blazy = {
129 attach: function (context) {
130 var me = Drupal.blazy;
131 var el = context.querySelector('[data-blazy]');
133 // Runs basic Blazy if no [data-blazy] found, probably a single image.
134 // Cannot use .contains(), as IE11 doesn't support method 'contains'.
136 me.init = new Blazy(me.globals());
140 // Runs Blazy with multi-serving images, and aspect ratio supports.
141 var blazies = context.querySelectorAll('.blazy:not(.blazy--on)');
142 _db.once(_db.forEach(blazies, doBlazy));
146 }(Drupal, drupalSettings, dBlazy, this, this.document));