xref: /plugin/combo/resources/snippet/js/lozad-svg-injection.js (revision a6bf47aa01c4ea7d24944b0e48eb1943151e3c25)
1window.addEventListener("load", function (event) {
2    // lazy loads elements with default selector as '.lozad'
3    const svgObserver = lozad('.lazy-svg-injection-combo', {
4        load: function (el) {
5            // SVGInjector takes over and load the svg element
6            // in place of lozad
7            SVGInjector(el, {
8                    each: function (svg) {
9                        // Style copy (max width)
10                        // If any error, svg is a string with the error
11                        // Example: `Unable to load SVG file: http://doku/_media/ui/preserveaspectratio.svg`
12                        if (typeof svg === 'object') {
13                            if (el.hasOwnProperty("style")) {
14                                svg.style.cssText = el.style.cssText;
15                            }
16                            if (el.hasOwnProperty("dataset")) {
17                                let dataSet = el.dataset;
18                                if (dataSet.hasOwnProperty("class")) {
19                                    dataSet.class.split(" ").forEach(e => svg.classList.add(e));
20                                }
21                            }
22                        }
23                    },
24                }
25            )
26        },
27        loaded: function (el) {
28            // Custom implementation on a loaded element
29            el.classList.add('loaded-combo');
30        }
31    });
32    svgObserver.observe();
33});
34