@duckafire/lazy-loading-js 3.0.0 → 3.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1 @@
1
+ "use strict";const __LL_SrcGroup__=Object.freeze({INIT:0,HIGH:1,LAZY:2});class __LL_TV__{constructor(e){this.exceptionMessage="A type error occur.",this.fallbackWarning=null,this.value=e}expect(e){var t,s;this.isExpAlreadyDefined(),this.isUndef(e);const r=typeof e;if("string"===r){const t=typeof this.value;return this.isValidValue="object"===e?null!==this.value&&t===e:"array"===e?Array.isArray(this.value):t===e,this.exceptionMessage=`Expecting type "${e}", instead "${t}".`,this}if("function"!==r)throw new TypeError(`Expecting constructor, instead "${r}".`);return this.isValidValue=this.value instanceof HTMLImageElement,this.exceptionMessage=`Expecting instance or heir of \`${e.constructor.name}\`, instead \`${null===(s=null===(t=this.value)||void 0===t?void 0:t.constructor)||void 0===s?void 0:s.name}\`.`,this}fallback(e,t=!1){if(void 0!==this.fallbackValue)throw new SyntaxError("Fallback already defined.");return this.isUndef(e),t&&(this.fallbackWarning=new Error("Invalid value type. Using fallback.")),this.fallbackValue=e,this}val(){if(void 0===this.isValidValue)throw new SyntaxError("Expected value not defined.");if(this.isValidValue)return this.value;if(void 0!==this.fallbackValue)return null!==this.fallbackWarning&&console.warn(this.fallbackWarning),this.fallbackValue;throw new TypeError(this.exceptionMessage)}isUndef(e){if(void 0===e)throw new TypeError("`undefined` is invalid.")}isExpAlreadyDefined(){if(void 0!==this.isValidValue)throw new SyntaxError("Expected value already defined.")}}class APIUnavailableError extends Error{constructor(){super("Intersection Observer API is unavailable. High quality images are in use.")}}class __LL_Element__{constructor(e,t){this.curGroup=__LL_SrcGroup__.INIT,this.elem=new __LL_TV__(e).expect(HTMLImageElement).val(),this.setAttribute(),this.imgSrc={high:this.catchSrc("high",!t),lazy:this.catchSrc("lazy",t)},this.styleClasses={high:this.catchStyles("high"),lazy:this.catchStyles("lazy")},this.clearBootAttr()}useSrc(e,t){if(t!==__LL_SrcGroup__.HIGH){if(t!==__LL_SrcGroup__.LAZY)throw new SyntaxError(`Invalid source group: "${t}".`);this.toggleSrc(__LL_SrcGroup__.LAZY,this.imgSrc.lazy)&&this.toggleStyles(__LL_SrcGroup__.LAZY,e)}else this.toggleSrc(__LL_SrcGroup__.HIGH,this.imgSrc.high)&&this.toggleStyles(__LL_SrcGroup__.HIGH,e)}setGroupIndex(e){this.elem.dataset.lliId=e.toString()}getAsHTMLImg(){return this.elem}apiUnavailable(){this.useSrc({high:null,lazy:null},__LL_SrcGroup__.HIGH)}setAttribute(){null===this.elem.getAttribute("loading")&&this.elem.setAttribute("loading","lazy")}catchSrc(e,t){if(void 0!==this.elem.dataset[e])return this.elem.dataset[e];if(t)return this.elem.src;throw new Error(`Element attribute not found: \`data-${e}\`.`)}catchStyles(e){var t;return(null===(t=this.elem.getAttribute("data-style-"+e))||void 0===t?void 0:t.split(",").map(e=>e.trim()))||null}clearBootAttr(){for(const e of["high","lazy","styleHigh","styleLazy"])this.elem.removeAttribute("data-"+e)}toggleSrc(e,t){return this.curGroup!==e&&(this.curGroup=e,this.elem.src=t,!0)}toggleStyles(e,t){this.toggleIndieStyles(e,t),this.toggleIndieStyles(e,this.styleClasses)}toggleIndieStyles(e,t){let s=t.lazy,r=t.high;e===__LL_SrcGroup__.LAZY&&(s=t.high,r=t.lazy),null!==s&&this.elem.classList.remove(...s),null!==r&&this.elem.classList.add(...r)}}class __LL_ElementsGroup__{constructor(e,t,s){this.elements=[],this.styleClasses={high:this.valStyle(s.high),lazy:this.valStyle(s.lazy)},this.catchElements(new __LL_TV__(e).expect("string").val(),t)}useSrc(e,t){this.elements[e].useSrc(this.styleClasses,t)}startToObserve(e){this.elements.forEach((t,s)=>{t.setGroupIndex(s),e.observe(t.getAsHTMLImg())})}apiUnavailable(){for(let e=0;e<this.elements.length;e++)this.apiUnavailable()}valStyle(e){if(!e)return null;for(const t of new __LL_TV__(e).expect("array").val())new __LL_TV__(t).expect("string").val();return e}catchElements(e,t){document.querySelectorAll(e).forEach(e=>{this.elements.push(new __LL_Element__(e,t))})}}class __LL_Observer__{constructor(e,t){this.isAvailable();const s={root:new __LL_TV__(e.root).expect(HTMLElement).fallback(null).val(),rootMargin:new __LL_TV__(e.rootMargin).expect("string").fallback("0px 0px 0px 0px").val(),scrollMargin:new __LL_TV__(e.scrollMargin).expect("string").fallback("0px 0px 0px 0px").val(),threshold:this.valThreshold(e.threshold)};this.api=this.startAPI(s),this.elementsGroup=t}useSrc(e,t){this.elementsGroup.useSrc(parseInt(e.target.dataset.lliId),t)}startToObserve(){this.elementsGroup.startToObserve(this.api)}valThreshold(e){if(!Array.isArray(e))return new __LL_TV__(e).expect("number").fallback(0).val();for(const t of e)new __LL_TV__(t).expect("number").val();return e}isAvailable(){if(void 0===window.IntersectionObserver)throw this.elementsGroup.apiUnavailable(),new APIUnavailableError}startAPI(e){return new IntersectionObserver(e=>{e.forEach(e=>{this.useSrc(e,e.isIntersecting?__LL_SrcGroup__.HIGH:__LL_SrcGroup__.LAZY)})},e)}}class LazyLoadingImages{constructor(e,t){this.isApiAvail=!0,this.started=!1;const s=new __LL_TV__(t).expect("object").fallback({},!0).val();try{this.observer=new __LL_Observer__(s.observerOptions||{},new __LL_ElementsGroup__(e,s.useSrcAsFallbackToLazySrc||!0,s.styleClasses||{}))}catch(e){throw e instanceof APIUnavailableError&&(this.isApiAvail=!1),e}s.waitToStart||this.startToObserve()}isApiAvailable(){return this.isApiAvail}isStarted(){return this.started}startToObserve(){this.started?console.warn(new Error("API already started.")):(this.started=!0,this.observer.startToObserve())}}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@duckafire/lazy-loading-js",
3
3
  "author": "duckafire",
4
- "version": "3.0.0",
4
+ "version": "3.0.1",
5
5
  "license": "Zlib",
6
6
  "description": "Simple, no-dependeces, lazy loading implementation to FrontEnd WEB",
7
7
  "keywords": [
@@ -28,7 +28,8 @@
28
28
  "clear": "./scripts.js clear",
29
29
  "compile": "./scripts.js compile",
30
30
  "help": "./scripts.js help",
31
- "minify": "./scripts.js minify"
31
+ "minify": "./scripts.js minify",
32
+ "publish": "./scripts.js clear && ./scripts.js build && npm publish"
32
33
  },
33
34
  "files": [
34
35
  "LICENSE",
@@ -1 +0,0 @@
1
- "use strict";const __LL_ElemStatus__=Object.freeze({NULL:0,HIGH:1,LAZY:2}),__LL_SrcGroup__=Object.freeze({HIGH:"high",LAZY:"lazy"});class __LL_UseSrc__{}class __LL_TV__{constructor(e){this.exceptionMessage="A type error occur.",this.fallbackWarning=null,this.value=e}expect(e){var t,s;this.isExpAlreadyDefined(),this.isUndef(e);const r=typeof e;if("string"===r){const t=typeof this.value;return this.isValidValue="object"===e?null!==this.value&&t===e:"array"===e?Array.isArray(this.value):t===e,this.exceptionMessage=`Expecting type "${e}", instead "${t}".`,this}if("function"!==r)throw new TypeError(`Expecting constructor, instead "${r}".`);return this.isValidValue=this.value instanceof HTMLImageElement,this.exceptionMessage=`Expecting instance or heir of \`${e.constructor.name}\`, instead \`${null===(s=null===(t=this.value)||void 0===t?void 0:t.constructor)||void 0===s?void 0:s.name}\`.`,this}fallback(e,t=!1){if(void 0!==this.fallbackValue)throw new SyntaxError("Fallback already defined.");return this.isUndef(e),t&&(this.fallbackWarning=new Error("Invalid value type. Using fallback.")),this.fallbackValue=e,this}val(){if(void 0===this.isValidValue)throw new SyntaxError("Expected value not defined.");if(this.isValidValue)return this.value;if(void 0!==this.fallbackValue)return null!==this.fallbackWarning&&console.warn(this.fallbackWarning),this.fallbackValue;throw new TypeError(this.exceptionMessage)}isUndef(e){if(void 0===e)throw new TypeError("`undefined` is invalid.")}isExpAlreadyDefined(){if(void 0!==this.isValidValue)throw new SyntaxError("Expected value already defined.")}}class APIUnavailableError extends Error{constructor(){super("Intersection Observer API is unavailable. High quality images are in use.")}}class __LL_Element__ extends __LL_UseSrc__{constructor(e,t=!1){super(),this.status=__LL_ElemStatus__.NULL,this.elem=new __LL_TV__(e).expect(HTMLImageElement).val(),this.setAttribute(),this.imgSrc={high:this.catchSrc(__LL_SrcGroup__.HIGH,!t),lazy:this.catchSrc(__LL_SrcGroup__.LAZY,t)},this.styleClasses={high:this.catchStyles(__LL_SrcGroup__.HIGH),lazy:this.catchStyles(__LL_SrcGroup__.LAZY)},this.clearBootAttr()}useSrc(e,t){if(t!==__LL_SrcGroup__.HIGH){if(t!==__LL_SrcGroup__.LAZY)throw new SyntaxError(`Invalid source group: "${t}".`);this.toggleSrc(__LL_ElemStatus__.LAZY,this.imgSrc.lazy)&&this.toggleStyles(__LL_ElemStatus__.LAZY,e)}else this.toggleSrc(__LL_ElemStatus__.HIGH,this.imgSrc.high)&&this.toggleStyles(__LL_ElemStatus__.HIGH,e)}setAttribute(){void 0===this.elem.getAttribute("loading")&&this.elem.setAttribute("loading","lazy")}setGroupIndex(e){this.elem.dataset.lliId=e.toString()}getAsHTMLImg(){return this.elem}apiUnavailable(){this.useSrc({high:null,lazy:null},__LL_SrcGroup__.HIGH)}catchSrc(e,t){if(void 0!==this.elem.dataset[e])return this.elem.dataset[e];if(t)return this.elem.src;throw new Error(`Element attribute not found: \`data-${e}\`.`)}catchStyles(e){var t;return(null===(t=this.elem.getAttribute("data-style-"+e))||void 0===t?void 0:t.split(","))||null}clearBootAttr(){delete this.elem.dataset.high,this.elem.dataset.lazy,this.elem.dataset.styleHigh,this.elem.dataset.styleLazy}toggleSrc(e,t){return this.status!==e&&(this.status=e,this.elem.src=t,!0)}toggleStyles(e,t){this.toggleIndieStyles(e,t),this.toggleIndieStyles(e,this.styleClasses)}toggleIndieStyles(e,t){let s=t.lazy,r=t.high;e===__LL_ElemStatus__.LAZY&&(s=t.high,r=t.lazy),null!==s&&this.elem.classList.remove(...s),null!==r&&this.elem.classList.add(...r)}}class __LL_ElementsGroup__ extends __LL_UseSrc__{constructor(e,t,s){super(),this.elements=[],this.styleClasses={high:this.valStyle(s.high),lazy:this.valStyle(s.lazy)},this.catchElements(new __LL_TV__(e).expect("string").val(),t)}useSrc(e,t){this.elements[e].useSrc(this.styleClasses,t)}startToObserve(e){this.elements.forEach((t,s)=>{t.setGroupIndex(s),e.observe(t.getAsHTMLImg())})}apiUnavailable(){for(let e=0;e<this.elements.length;e++)this.apiUnavailable()}valStyle(e){if(!e)return null;for(const t of new __LL_TV__(e).expect("array").val())new __LL_TV__(t).expect("string").val();return e}catchElements(e,t){document.querySelectorAll(e).forEach(e=>{this.elements.push(new __LL_Element__(e,t))})}}class __LL_Observer__ extends __LL_UseSrc__{constructor(e,t){super();const s={root:new __LL_TV__(e.root).expect(HTMLElement).fallback(null).val(),rootMargin:new __LL_TV__(e.rootMargin).expect("string").fallback("0px 0px 0px 0px").val(),scrollMargin:new __LL_TV__(e.scrollMargin).expect("string").fallback("0px 0px 0px 0px").val(),threshold:this.valThreshold(e.threshold)};this.isAvailable(),this.api=this.startAPI(s),this.elementsGroup=t}useSrc(e,t){this.elementsGroup.useSrc(parseInt(e.target.dataset.lliId),t)}startToObserve(){this.elementsGroup.startToObserve(this.api)}valThreshold(e){if(!Array.isArray(e))return new __LL_TV__(e).expect("number").fallback(0).val();for(const t of e)new __LL_TV__(t).expect("number").val();return e}isAvailable(){if(void 0===window.IntersectionObserver)throw this.elementsGroup.apiUnavailable(),new APIUnavailableError}startAPI(e){return new IntersectionObserver(e=>{e.forEach(e=>{this.useSrc(e,e.isIntersecting?__LL_SrcGroup__.HIGH:__LL_SrcGroup__.LAZY)})},e)}}class LazyLoadingImage{constructor(e,t){this.isApiAvail=!0,this.started=!1;const s=new __LL_TV__(t).expect("object").fallback({},!0).val();try{this.observer=new __LL_Observer__(s.observerOptions||{},new __LL_ElementsGroup__(e,s.useSrcAsFallbackToLazySrc||!0,s.styleClasses||{}))}catch(e){throw e instanceof APIUnavailableError&&(this.isApiAvail=!1),e}s.waitToStart||this.startToObserve()}isApiAvailable(){return this.isApiAvail}isStarted(){return this.started}startToObserve(){this.started?console.warn(new Error("API already started.")):(this.started=!0,this.observer.startToObserve())}}