From 7ca67456b4f3ac354168eac917173e40fbb3a6ba Mon Sep 17 00:00:00 2001 From: Jimmy Cai Date: Tue, 22 Dec 2020 00:27:36 +0100 Subject: [PATCH] refactor(gallery): add openGallery method --- assets/ts/gallery.ts | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/assets/ts/gallery.ts b/assets/ts/gallery.ts index 1fb9d4e..c1d052b 100644 --- a/assets/ts/gallery.ts +++ b/assets/ts/gallery.ts @@ -115,6 +115,22 @@ class StackGallery { return Promise.all(tasks); } + private openGallery(index: number) { + const pswp = document.querySelector('.pswp') as HTMLDivElement; + const ps = new window.PhotoSwipe(pswp, window.PhotoSwipeUI_Default, this.items, { + index: index, + getThumbBoundsFn: (index) => { + const thumbnail = this.items[index].el.getElementsByTagName('img')[0], + pageYScroll = window.pageYOffset || document.documentElement.scrollTop, + rect = thumbnail.getBoundingClientRect(); + + return { x: rect.left, y: rect.top + pageYScroll, w: rect.width }; + } + }); + + ps.init(); + } + private initPS() { const pswp = document.querySelector('.pswp') as HTMLDivElement; pswp.style.removeProperty('display'); @@ -124,20 +140,7 @@ class StackGallery { a.addEventListener('click', (e) => { e.preventDefault(); - - const pswp = document.querySelector('.pswp') as HTMLDivElement; - const ps = new window.PhotoSwipe(pswp, window.PhotoSwipeUI_Default, this.items, { - index: index, - getThumbBoundsFn: (index) => { - const thumbnail = this.items[index].el.getElementsByTagName('img')[0], - pageYScroll = window.pageYOffset || document.documentElement.scrollTop, - rect = thumbnail.getBoundingClientRect(); - - return { x: rect.left, y: rect.top + pageYScroll, w: rect.width }; - } - }); - - ps.init(); + this.openGallery(index); }) } }