From 81d3139c0553cbd44b0c34284a2e18ce068eb9b4 Mon Sep 17 00:00:00 2001 From: Valexr Date: Tue, 30 Aug 2022 13:47:17 +0300 Subject: [PATCH] @slidy/element - improve init, optimise & cleanup --- packages/element/public/api.js | 15 +++-- packages/element/public/env.js | 12 ++-- packages/element/public/index.html | 70 +++++++++++++++--------- packages/element/public/index.js | 2 +- packages/element/src/component.ts | 88 ------------------------------ packages/element/src/index.ts | 3 +- packages/element/src/options.ts | 29 ---------- packages/element/src/slidy.ts | 71 ++++++++++++++++-------- packages/element/src/types.ts | 87 +---------------------------- packages/element/src/utils.ts | 16 +++++- packages/element/tsconfig.json | 2 +- 11 files changed, 126 insertions(+), 269 deletions(-) delete mode 100644 packages/element/src/component.ts delete mode 100644 packages/element/src/options.ts diff --git a/packages/element/public/api.js b/packages/element/public/api.js index 1da6d6de..f7040323 100644 --- a/packages/element/public/api.js +++ b/packages/element/public/api.js @@ -1,18 +1,21 @@ -export async function getPhotos(node, page, limit) { +export async function getPhotos(page = 9, limit = 9) { node.innerHTML = `Loading... 🚀`; + if (node._slidy && thumbs._slidy) { + // node.setAttribute('length', node.children.length); + // thumbs.setAttribute('length', thumbs.children.length); + } //page: 38 - END, 61 - START, 28 try { - const res = await fetch(`https://picsum.photos/v2/list?limit=${limit}&page=${page}`, { - mode: 'cors', - }); + const res = await fetch(`https://picsum.photos/v2/list?limit=${limit}&page=${page}`); const photos = await res.json(); + if (photos.length === limit && node.isConnected) { node.innerHTML = createSlides(node, photos); thumbs.innerHTML = createSlides(thumbs, photos); dots.innerHTML = createSlides(dots, photos); - if (node.children.length === limit) { - const mounted = Array.from(node.children).every( + if (limit === node.children.length && limit === thumbs.children.length) { + const mounted = Array.from(thumbs.children).every( (child) => child && child.isConnected ); if (mounted) { diff --git a/packages/element/public/env.js b/packages/element/public/env.js index 8589f96d..bdfda945 100644 --- a/packages/element/public/env.js +++ b/packages/element/public/env.js @@ -10,7 +10,7 @@ export function setEvents() { if (options[button.id]) { button.classList.add('active'); } else if (!isNaN(button.id)) { - button.onclick = (e) => updateAttribute('index', () => e.target.id); + button.onclick = (e) => node.setAttribute('index', e.target.id); } } @@ -92,10 +92,10 @@ export function setEvents() { options.animation = animations[animation.value]; break; - case 'resize': - // slidyT.to(options.index); - // console.log(e.detail); - break; + // case 'resize': + // // slidyT.to(options.index); + // // console.log(e.detail); + // break; case 'move': utils.moving(e.detail); @@ -129,7 +129,7 @@ export function setEvents() { } } break; - case 'keys': + // case 'keys': // console.log(e); default: // console.log(e); diff --git a/packages/element/public/index.html b/packages/element/public/index.html index f8041d3f..2fb62fa8 100644 --- a/packages/element/public/index.html +++ b/packages/element/public/index.html @@ -52,7 +52,7 @@ - + @@ -72,7 +72,7 @@ index: 4, clamp: 0, indent: 1, - length: 9, + // length: 9, sensity: 5, gravity: 1.2, duration: 375, @@ -83,25 +83,26 @@ easing: easings.lenear, }; window.position = 0; + window.page = () => utils.randomQ(1, 99); window.getPhotos = getPhotos; - window.updateAttribute = (attribute, cb) => { + window.setEvents = setEvents; + window.updateAttribute = (node, attribute, cb) => { const value = node.getAttribute(attribute); node.setAttribute(attribute, cb(JSON.parse(value))); }; - // node.update({ animation: animations.stairs }); - // updateAttribute('animation', () => animations.stairs); + // node.options = { animation: animations.stairs }; console.log('module window:', window); - fetch('https://unpkg.com/@slidy/element/package.json', { - mode: 'cors', - }) - .then(async (res) => await res.json()) - .then(({ version }) => { - title.innerText = `Slidy ${version} - Element`; - header.innerHTML = `Slidy ${version}Element`; - getPhotos(node, utils.randomQ(1, 99), options.length) - .then((length) => length && setEvents()); - }); + // fetch('https://unpkg.com/@slidy/element/package.json', { + // mode: 'cors', + // }) + // .then(async (res) => await res.json()) + // .then(({ version }) => { + // title.innerText = `Slidy ${version} - Element`; + // header.innerHTML = `Slidy ${version}Element`; + // getPhotos(page(), options.length) + // .then((length) => length && setEvents()); + // }); @@ -113,35 +114,50 @@
- - + - + + +