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 var ie = _db.hasClass(el, 'b-responsive') && el.hasAttribute('data-pfsrc');
33 // The .b-lazy element can be attached to IMG, or DIV as CSS background.
34 el.className = el.className.replace(/(\S+)loading/, '');
36 // The .is-loading can be .grid, .slide__content, .box__content, etc.
38 _db.closest(el, '.is-loading'),
39 _db.closest(el, '[class*="loading"]')
42 // Also cleans up closest containers containing loading class.
43 _db.forEach(loaders, function (wrapEl) {
44 if (wrapEl !== null) {
45 wrapEl.className = wrapEl.className.replace(/(\S+)loading/, '');
49 // @todo: Remove when Blazy library fixes this.
50 // @see http://scottjehl.github.io/picturefill/
51 if (window.picturefill && ie) {
61 * Blazy utility functions.
63 * @param {HTMLElement} elm
64 * The Blazy HTML element.
66 function doBlazy(elm) {
67 var me = Drupal.blazy;
68 var dataAttr = elm.getAttribute('data-blazy');
69 var empty = dataAttr === '' || dataAttr === '[]';
70 var data = empty ? false : _db.parse(dataAttr);
71 var opts = !data ? me.globals() : _db.extend({}, me.globals(), data);
72 var ratios = elm.querySelectorAll('[data-dimensions]');
73 var loopRatio = ratios.length > 0;
76 * Updates the dynamic multi-breakpoint aspect ratio.
78 * This only applies to multi-serving images with aspect ratio fluid if
79 * each element contains [data-dimensions] attribute.
80 * Static single aspect ratio, e.g. `media--ratio--169`, will be ignored,
81 * and will use CSS instead.
83 * @param {HTMLElement} el
84 * The .media--ratio HTML element.
86 function updateRatio(el) {
87 var dimensions = !el.getAttribute('data-dimensions') ? false : _db.parse(el.getAttribute('data-dimensions'));
93 var keys = Object.keys(dimensions);
95 var xl = keys[keys.length - 1];
96 var mw = function (w) {
97 return w >= me.windowWidth;
99 var pad = keys.filter(mw).map(function (v) {
100 return dimensions[v];
103 if (pad === 'undefined') {
104 pad = dimensions[me.windowWidth >= xl ? xl : xs];
107 if (pad !== 'undefined') {
108 el.style.paddingBottom = pad + '%';
112 // Initializes Blazy instance.
113 me.init = new Blazy(opts);
115 // Reacts on resizing.
117 me.init.revalidate();
119 _db.resize(function () {
120 me.windowWidth = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
123 _db.forEach(ratios, updateRatio, elm);
126 // Dispatch resizing event.
127 _db.trigger(elm, 'resizing', {windowWidth: me.windowWidth});
133 elm.className += ' blazy--on';
137 * Attaches blazy behavior to HTML element identified by [data-blazy].
139 * @type {Drupal~behavior}
141 Drupal.behaviors.blazy = {
142 attach: function (context) {
143 var me = Drupal.blazy;
144 var el = context.querySelector('[data-blazy]');
146 // Runs basic Blazy if no [data-blazy] found, probably a single image.
147 // Cannot use .contains(), as IE11 doesn't support method 'contains'.
149 me.init = new Blazy(me.globals());
153 // Runs Blazy with multi-serving images, and aspect ratio supports.
154 var blazies = context.querySelectorAll('.blazy:not(.blazy--on)');
155 _db.once(_db.forEach(blazies, doBlazy));
159 }(Drupal, drupalSettings, dBlazy, this, this.document));