PluginProbe
Tagembed: Social Media Feeds and Customer Reviews Widget / 7.9
Tagembed: Social Media Feeds and Customer Reviews Widget v7.9
7.9 8.0 7.8 7.5 7.6 7.7 7.4 trunk 3.10 3.9 4.0 4.1 4.2 4.3 4.4 4.5 4.6 4.7 4.8 4.9 5.0 5.1 5.2 5.3 5.4 All 45 releases
tagembed-widget / assets / js / lazyload.js
assets/js
account 4 days ago customize 4 days ago display 4 days ago editor 4 days ago feed 4 days ago filter 4 days ago network 4 days ago support 4 days ago theme 4 days ago upgrade 4 days ago vendor 4 days ago widget 4 days ago confirm_dialog.js 4.5 KB 4 days ago dialog.form.js 10.6 KB 4 days ago lazyload.js 4.9 KB 4 days ago loader.js 1.3 KB 4 days ago tagembed.common.js 25.0 KB 4 days ago tagembed.deactive.js 9.2 KB 4 days ago toast.js 3.0 KB 4 days ago

lazyload.js in Tagembed: Social Media Feeds and Customer Reviews Widget 7.9, at assets/js/lazyload.js

139 lines 4.9 KB Raw Download Zip
1 (function (root, factory) {
2 if (typeof exports === "object") {
3 module.exports = factory(root);
4 } else if (typeof define === "function" && define.amd) {
5 define([], factory);
6 } else {
7 root.__tagembed__LazyLoad = factory(root);
8 }
9 })(typeof global !== "undefined" ? global : this.window || this.global, function (root) {
10 "use strict";
11 if (typeof define === "function" && define.amd) {
12 root = window;
13 }
14 const defaults = {
15 src: "data-src",
16 srcset: "data-srcset",
17 selector: ".lazyload",
18 root: null,
19 rootMargin: "0px",
20 threshold: 0
21 };
22 const extend = function () {
23 let extended = {};
24 let deep = false;
25 let i = 0;
26 let length = arguments.length;
27 if (Object.prototype.toString.call(arguments[0]) === "[object Boolean]") {
28 deep = arguments[0];
29 i++;
30 }
31 let merge = function (obj) {
32 for (let prop in obj) {
33 if (Object.prototype.hasOwnProperty.call(obj, prop)) {
34 /* If deep merge and property is an object, merge properties */
35 if (deep && Object.prototype.toString.call(obj[prop]) === "[object Object]") {
36 extended[prop] = extend(true, extended[prop], obj[prop]);
37 } else {
38 extended[prop] = obj[prop];
39 }
40 }
41 }
42 };
43 for (; i < length; i++) {
44 let obj = arguments[i];
45 merge(obj);
46 }
47 return extended;
48 };
49 function __tagembed__LazyLoad(images, options) {
50 this.settings = extend(defaults, options || {});
51 this.images = images || document.querySelectorAll(this.settings.selector);
52 this.observer = null;
53 this.init();
54 }
55 __tagembed__LazyLoad.prototype = {
56 init: function () {
57 if (!root.IntersectionObserver) {
58 this.loadImages();
59 return;
60 }
61 let self = this;
62 let observerConfig = {
63 root: this.settings.root,
64 rootMargin: this.settings.rootMargin,
65 threshold: [this.settings.threshold]
66 };
67 this.observer = new IntersectionObserver(function (entries) {
68 Array.prototype.forEach.call(entries, function (entry) {
69 if (entry.isIntersecting) {
70 self.observer.unobserve(entry.target);
71 let src = entry.target.getAttribute(self.settings.src);
72 let srcset = entry.target.getAttribute(self.settings.srcset);
73 if ("img" === entry.target.tagName.toLowerCase()) {
74 if (src) {
75 entry.target.src = src;
76 }
77 if (srcset) {
78 entry.target.srcset = srcset;
79 }
80 } else {
81 entry.target.style.backgroundImage = "url(" + src + ")";
82 }
83 }
84 });
85 }, observerConfig);
86 Array.prototype.forEach.call(this.images, function (image) {
87 self.observer.observe(image);
88 });
89 },
90 loadAndDestroy: function () {
91 if (!this.settings) {
92 return;
93 }
94 this.loadImages();
95 this.destroy();
96 },
97 loadImages: function () {
98 if (!this.settings) {
99 return;
100 }
101 let self = this;
102 Array.prototype.forEach.call(this.images, function (image) {
103 let src = image.getAttribute(self.settings.src);
104 let srcset = image.getAttribute(self.settings.srcset);
105 if ("img" === image.tagName.toLowerCase()) {
106 if (src) {
107 image.src = src;
108 }
109 if (srcset) {
110 image.srcset = srcset;
111 }
112 } else {
113 image.style.backgroundImage = "url('" + src + "')";
114 }
115 });
116 },
117 destroy: function () {
118 if (!this.settings) {
119 return;
120 }
121 this.observer.disconnect();
122 this.settings = null;
123 }
124 };
125 root.__tagembed__lazyload = function (images, options) {
126 return new __tagembed__LazyLoad(images, options);
127 };
128 if (root.jQuery) {
129 const $ = root.jQuery;
130 $.fn.__tagembed__lazyload = function (options) {
131 options = options || {};
132 options.attribute = options.attribute || "data-src";
133 new __tagembed__LazyLoad($.makeArray(this), options);
134 return this;
135 };
136 }
137 return __tagembed__LazyLoad;
138 });
139