Skip to content
This repository was archived by the owner on Jan 10, 2021. It is now read-only.

Commit e0c36a4

Browse files
Merge pull request #22 from dutchenkoOleg/master
0.14.0 - add TypesScript support
2 parents 6134c7e + 9e89457 commit e0c36a4

File tree

8 files changed

+82
-10
lines changed

8 files changed

+82
-10
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# zz-load
22

3+
![types](https://img.shields.io/badge/types-TypeScript-blue)
34
[![license](https://img.shields.io/badge/License-MIT-blue.svg)](https://github.com/WezomAgency/zz-load/blob/master/LICENSE)
45
[![npm](https://img.shields.io/badge/npm-install-orange.svg)](https://www.npmjs.com/package/zz-load)
56
[![WezomAgency](https://img.shields.io/badge/wezom-agency-red.svg)](https://github.com/WezomAgency)

dist/zz-load.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

index.d.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
declare module 'zz-load' {
2+
function zzLoad(
3+
elements: string | NodeList | Element,
4+
options: {
5+
rootMargin?: string;
6+
threshold?: number;
7+
onLoad?(): void;
8+
onError?(): void;
9+
}
10+
): {
11+
observe(): void;
12+
triggerLoad<TElement = HTMLElement>(element: TElement): void;
13+
};
14+
15+
export = zzLoad;
16+
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "zz-load",
3-
"version": "0.13.0",
3+
"version": "0.14.0",
44
"description": "",
55
"main": "./src/zz-load.es.js",
66
"scripts": {

src/attrs.d.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
declare module 'zz-load/src/attrs' {
2+
interface ZZLoadAttrs {
3+
isObserved: string;
4+
isProcessed: string;
5+
isLoaded: string;
6+
isFailed: string;
7+
isInView: string;
8+
sourceImg: string;
9+
sourceSrcSet: string;
10+
sourceBgImg: string;
11+
sourceImage: string;
12+
sourceIframe: string;
13+
sourceContainer: string;
14+
sourceInview: string;
15+
}
16+
17+
export const attrs: ZZLoadAttrs;
18+
}

src/data-set.d.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
declare module 'zz-load/src/data-set' {
2+
interface ZZLoadDataSet {
3+
isObserved: string;
4+
isProcessed: string;
5+
isLoaded: string;
6+
isFailed: string;
7+
isInView: string;
8+
sourceImg: string;
9+
sourceSrcSet: string;
10+
sourceBgImg: string;
11+
sourceImage: string;
12+
sourceIframe: string;
13+
sourceContainer: string;
14+
sourceInview: string;
15+
}
16+
17+
export const dataSet: ZZLoadDataSet;
18+
}

src/events.d.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
declare module 'zz-load/src/events' {
2+
interface ZZLoadEvents {
3+
observed: string;
4+
processed: string;
5+
loaded: string;
6+
failed: string;
7+
inView: string;
8+
outOfView: string;
9+
}
10+
11+
export const events: ZZLoadEvents;
12+
}

src/zz-load.es.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ const _defaultOptions = {
4040
const _extend = (userOptions = {}) => {
4141
let options = {};
4242
for (let key in _defaultOptions) {
43-
options[key] = userOptions.hasOwnProperty(key) ? userOptions[key] : _defaultOptions[key];
43+
options[key] = userOptions.hasOwnProperty(key)
44+
? userOptions[key]
45+
: _defaultOptions[key];
4446
}
4547
return options;
4648
};
@@ -49,7 +51,7 @@ const _extend = (userOptions = {}) => {
4951
* @param {HTMLElement} element
5052
* @private
5153
*/
52-
const _sanitaze = element => {
54+
const _sanitaze = (element) => {
5355
element.removeAttribute(attrs.sourceImg);
5456
element.removeAttribute(attrs.sourceSrcSet);
5557
element.removeAttribute(attrs.sourceBgImg);
@@ -178,23 +180,28 @@ const _load = (element, options, asPromise) => {
178180
if (element.nodeName.toLowerCase() === 'picture') {
179181
const pitureImg = element.getElementsByTagName('img')[0];
180182
if (pitureImg instanceof window.HTMLImageElement) {
181-
const clear = str => str.replace(/^\/\//i, '').replace(window.location.origin, '');
183+
const clear = (str) =>
184+
str.replace(/^\/\//i, '').replace(window.location.origin, '');
182185
const currentSrc = clear(pitureImg.currentSrc);
183186
let src = null;
184187
let srcset = null;
185188
for (let i = 0; i < element.children.length; i++) {
186189
const child = element.children[i];
187190
const isSource = child.nodeName.toLowerCase() === 'source';
188191
const isImg = child.nodeName.toLowerCase() === 'img';
189-
const childSrc = clear(isSource ? child.srcset : isImg ? child.src : '');
192+
const childSrc = clear(
193+
isSource ? child.srcset : isImg ? child.src : ''
194+
);
190195
if (currentSrc === childSrc) {
191196
src = child.getAttribute(attrs.sourceImg) || null;
192197
srcset = child.getAttribute(attrs.sourceSrcSet);
193198
}
194199
}
195200

196201
if (src === null) {
197-
console.warn('You must provide all the `data-zzload-source-*` attributes for all children.');
202+
console.warn(
203+
'You must provide all the `data-zzload-source-*` attributes for all children.'
204+
);
198205
console.warn(element);
199206
return null;
200207
}
@@ -207,7 +214,7 @@ const _load = (element, options, asPromise) => {
207214

208215
if (child.nodeName.toLowerCase() === 'source') {
209216
if (srcset) {
210-
src += (', ' + srcset);
217+
src += ', ' + srcset;
211218
}
212219
child.srcset = src;
213220
} else if (child.nodeName.toLowerCase() === 'img') {
@@ -321,8 +328,8 @@ const _checkIs = {
321328
* @return {Function}
322329
* @private
323330
*/
324-
const _onIntersection = options => (entries, observer) => {
325-
entries.forEach(entry => {
331+
const _onIntersection = (options) => (entries, observer) => {
332+
entries.forEach((entry) => {
326333
/** @type {Element} */
327334
let element = entry.target;
328335
let inViewType = element.hasAttribute(attrs.sourceInview);

0 commit comments

Comments
 (0)