@get-set/gs-zoom 0.0.4 → 0.0.6
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.
- package/README.md +129 -1
- package/actions/general.ts +29 -0
- package/actions/getAdjustTransform.ts +46 -0
- package/actions/getCurrentParams.ts +42 -0
- package/actions/getTranslateCoordToPoint.ts +37 -0
- package/actions/init.ts +25 -0
- package/actions/initActionEvents.ts +102 -0
- package/actions/initAdditionals.ts +91 -0
- package/actions/initAdjust.ts +120 -0
- package/actions/initAdjustAdditionalActive.ts +31 -0
- package/actions/initArrows.ts +36 -0
- package/actions/initAutoplay.ts +91 -0
- package/actions/initChange.ts +139 -0
- package/actions/initClose.ts +29 -0
- package/actions/initDraggle.ts +258 -0
- package/actions/initDraw.ts +83 -0
- package/actions/initDrawItem.ts +77 -0
- package/actions/initFullScreen.ts +30 -0
- package/actions/initLightBox.ts +13 -0
- package/actions/initMagnifier.ts +53 -0
- package/actions/initNavigateWithKeys.ts +22 -0
- package/actions/initOpen.ts +29 -0
- package/actions/initWheel.ts +44 -0
- package/actions/setStyles.ts +28 -0
- package/components/GSZoom.tsx +178 -0
- package/{dist/components → components}/styles/GSZoom.scss +252 -254
- package/components/styles/GSZoomCSS.ts +237 -0
- package/constants/defaultParams.ts +20 -0
- package/constants/icons.ts +25 -0
- package/constants/magnifierDefaultProps.ts +9 -0
- package/constants/types.ts +6 -0
- package/dist/GSZoom.d.ts +2 -0
- package/dist/actions/general.d.ts +7 -0
- package/dist/actions/general.d.ts.map +1 -0
- package/dist/actions/general.js +26 -30
- package/dist/actions/general.js.map +1 -0
- package/dist/actions/getAdjustTransform.d.ts +3 -0
- package/dist/actions/getAdjustTransform.d.ts.map +1 -0
- package/dist/actions/getAdjustTransform.js +41 -43
- package/dist/actions/getAdjustTransform.js.map +1 -0
- package/dist/actions/getCurrentParams.d.ts +4 -0
- package/dist/actions/getCurrentParams.d.ts.map +1 -0
- package/dist/actions/getCurrentParams.js +35 -42
- package/dist/actions/getCurrentParams.js.map +1 -0
- package/dist/actions/getTranslateCoordToPoint.d.ts +3 -0
- package/dist/actions/getTranslateCoordToPoint.d.ts.map +1 -0
- package/dist/actions/getTranslateCoordToPoint.js +21 -41
- package/dist/actions/getTranslateCoordToPoint.js.map +1 -0
- package/dist/actions/init.d.ts +4 -0
- package/dist/actions/init.d.ts.map +1 -0
- package/dist/actions/init.js +21 -21
- package/dist/actions/init.js.map +1 -0
- package/dist/actions/initActionEvents.d.ts +4 -0
- package/dist/actions/initActionEvents.d.ts.map +1 -0
- package/dist/actions/initActionEvents.js +80 -127
- package/dist/actions/initActionEvents.js.map +1 -0
- package/dist/actions/initAdditionals.d.ts +4 -0
- package/dist/actions/initAdditionals.d.ts.map +1 -0
- package/dist/actions/initAdditionals.js +78 -117
- package/dist/actions/initAdditionals.js.map +1 -0
- package/dist/actions/initAdjust.d.ts +4 -0
- package/dist/actions/initAdjust.d.ts.map +1 -0
- package/dist/actions/initAdjust.js +96 -107
- package/dist/actions/initAdjust.js.map +1 -0
- package/dist/actions/initAdjustAdditionalActive.d.ts +4 -0
- package/dist/actions/initAdjustAdditionalActive.d.ts.map +1 -0
- package/dist/actions/initAdjustAdditionalActive.js +25 -33
- package/dist/actions/initAdjustAdditionalActive.js.map +1 -0
- package/dist/actions/initArrows.d.ts +4 -0
- package/dist/actions/initArrows.d.ts.map +1 -0
- package/dist/actions/initArrows.js +28 -44
- package/dist/actions/initArrows.js.map +1 -0
- package/dist/actions/initAutoplay.d.ts +6 -0
- package/dist/actions/initAutoplay.d.ts.map +1 -0
- package/dist/actions/initAutoplay.js +85 -81
- package/dist/actions/initAutoplay.js.map +1 -0
- package/dist/actions/initChange.d.ts +4 -0
- package/dist/actions/initChange.d.ts.map +1 -0
- package/dist/actions/initChange.js +116 -137
- package/dist/actions/initChange.js.map +1 -0
- package/dist/actions/initClose.d.ts +3 -0
- package/dist/actions/initClose.d.ts.map +1 -0
- package/dist/actions/initClose.js +24 -28
- package/dist/actions/initClose.js.map +1 -0
- package/dist/actions/initDraggle.d.ts +4 -0
- package/dist/actions/initDraggle.d.ts.map +1 -0
- package/dist/actions/initDraggle.js +227 -303
- package/dist/actions/initDraggle.js.map +1 -0
- package/dist/actions/initDraw.d.ts +4 -0
- package/dist/actions/initDraw.d.ts.map +1 -0
- package/dist/actions/initDraw.js +74 -90
- package/dist/actions/initDraw.js.map +1 -0
- package/dist/actions/initDrawItem.d.ts +4 -0
- package/dist/actions/initDrawItem.d.ts.map +1 -0
- package/dist/actions/initDrawItem.js +63 -79
- package/dist/actions/initDrawItem.js.map +1 -0
- package/dist/actions/initFullScreen.d.ts +4 -0
- package/dist/actions/initFullScreen.d.ts.map +1 -0
- package/dist/actions/initFullScreen.js +32 -32
- package/dist/actions/initFullScreen.js.map +1 -0
- package/dist/actions/initLightBox.d.ts +4 -0
- package/dist/actions/initLightBox.d.ts.map +1 -0
- package/dist/actions/initLightBox.js +11 -12
- package/dist/actions/initLightBox.js.map +1 -0
- package/dist/actions/initMagnifier.d.ts +4 -0
- package/dist/actions/initMagnifier.d.ts.map +1 -0
- package/dist/actions/initMagnifier.js +42 -54
- package/dist/actions/initMagnifier.js.map +1 -0
- package/dist/actions/initNavigateWithKeys.d.ts +4 -0
- package/dist/actions/initNavigateWithKeys.d.ts.map +1 -0
- package/dist/actions/initNavigateWithKeys.js +18 -27
- package/dist/actions/initNavigateWithKeys.js.map +1 -0
- package/dist/actions/initOpen.d.ts +4 -0
- package/dist/actions/initOpen.d.ts.map +1 -0
- package/dist/actions/initOpen.js +27 -28
- package/dist/actions/initOpen.js.map +1 -0
- package/dist/actions/initWheel.d.ts +4 -0
- package/dist/actions/initWheel.d.ts.map +1 -0
- package/dist/actions/initWheel.js +27 -49
- package/dist/actions/initWheel.js.map +1 -0
- package/dist/actions/setStyles.d.ts +4 -0
- package/dist/actions/setStyles.d.ts.map +1 -0
- package/dist/actions/setStyles.js +23 -24
- package/dist/actions/setStyles.js.map +1 -0
- package/dist/components/GSZoom.d.ts +23 -0
- package/dist/components/GSZoom.d.ts.map +1 -0
- package/dist/components/GSZoom.js +122 -177
- package/dist/components/GSZoom.js.map +1 -0
- package/dist/components/styles/GSZoomCSS.d.ts +3 -0
- package/dist/components/styles/GSZoomCSS.d.ts.map +1 -0
- package/dist/components/styles/GSZoomCSS.js +238 -0
- package/dist/components/styles/GSZoomCSS.js.map +1 -0
- package/dist/constants/defaultParams.d.ts +4 -0
- package/dist/constants/defaultParams.d.ts.map +1 -0
- package/dist/constants/defaultParams.js +18 -19
- package/dist/constants/defaultParams.js.map +1 -0
- package/dist/constants/icons.d.ts +13 -0
- package/dist/constants/icons.d.ts.map +1 -0
- package/dist/constants/icons.js +16 -25
- package/dist/constants/icons.js.map +1 -0
- package/dist/constants/magnifierDefaultProps.d.ts +4 -0
- package/dist/constants/magnifierDefaultProps.d.ts.map +1 -0
- package/dist/constants/magnifierDefaultProps.js +7 -7
- package/dist/constants/magnifierDefaultProps.js.map +1 -0
- package/dist/constants/types.d.ts +6 -0
- package/dist/constants/types.d.ts.map +1 -0
- package/dist/constants/types.js +6 -6
- package/dist/constants/types.js.map +1 -0
- package/dist/helpers/uihelpers.d.ts +2 -0
- package/dist/helpers/uihelpers.d.ts.map +1 -0
- package/dist/helpers/uihelpers.js +8 -9
- package/dist/helpers/uihelpers.js.map +1 -0
- package/dist/types/params.d.ts +35 -0
- package/dist/types/params.d.ts.map +1 -0
- package/dist/types/params.js +2 -0
- package/dist/types/params.js.map +1 -0
- package/dist/types/ref.d.ts +23 -0
- package/dist/types/ref.d.ts.map +1 -0
- package/dist/types/ref.js +2 -0
- package/dist/types/ref.js.map +1 -0
- package/dist-js/bundle.js +1689 -0
- package/helpers/uihelpers.ts +7 -0
- package/package.json +74 -43
- package/styles/GSZoom.scss +252 -0
- package/types/global.d.ts +33 -0
- package/types/params.ts +36 -0
- package/types/ref.ts +23 -0
- package/dist/components/styles/GSZoom.css.map +0 -1
- package/index.js +0 -3
- /package/{dist → components/styles}/GSZoom.css +0 -0
- /package/{dist/components/styles → styles}/GSZoom.css +0 -0
package/README.md
CHANGED
|
@@ -1 +1,129 @@
|
|
|
1
|
-
# Get-Set Zoom
|
|
1
|
+
# Get-Set Zoom
|
|
2
|
+
|
|
3
|
+
A lightbox & magnifier zoom plugin with drag, pinch, autoplay, fullscreen and keyboard navigation.
|
|
4
|
+
One codebase — builds both a **vanilla JS bundle** and a **React component**.
|
|
5
|
+
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Build
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
npm install
|
|
12
|
+
npm run build
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
| Command | Output | Use for |
|
|
16
|
+
|----------------------|-------------------------------|-------------------|
|
|
17
|
+
| `npm run build` | both outputs below | everything |
|
|
18
|
+
| `npm run build:js` | `dist-js/bundle.js` | vanilla JS / CDN |
|
|
19
|
+
| `npm run build:react`| `dist/components/GSZoom.js` | React / npm |
|
|
20
|
+
|
|
21
|
+
---
|
|
22
|
+
|
|
23
|
+
## Vanilla JS
|
|
24
|
+
|
|
25
|
+
```html
|
|
26
|
+
<link rel="stylesheet" href="dist-js/GSZoom.css" />
|
|
27
|
+
<script src="dist-js/bundle.js"></script>
|
|
28
|
+
|
|
29
|
+
<!-- Lightbox on a collection -->
|
|
30
|
+
<div class="gallery">
|
|
31
|
+
<img src="photo1.jpg" data-gstitle="Photo 1" />
|
|
32
|
+
<img src="photo2.jpg" />
|
|
33
|
+
</div>
|
|
34
|
+
|
|
35
|
+
<script>
|
|
36
|
+
new GSZoom(document.querySelectorAll('.gallery img'), {
|
|
37
|
+
type: 'lightbox',
|
|
38
|
+
arrows: true,
|
|
39
|
+
navigateWithKeys: true,
|
|
40
|
+
maxZoom: 5,
|
|
41
|
+
autoplaySpeed: 4000,
|
|
42
|
+
responsive: [
|
|
43
|
+
{ windowSize: 768, params: { arrows: false } },
|
|
44
|
+
],
|
|
45
|
+
afterLightBoxOpen: () => console.log('opened'),
|
|
46
|
+
});
|
|
47
|
+
</script>
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
jQuery and `HTMLElement.prototype` shortcuts are also available:
|
|
51
|
+
|
|
52
|
+
```js
|
|
53
|
+
// jQuery
|
|
54
|
+
$('.gallery img').GSZoom({ type: 'lightbox' });
|
|
55
|
+
|
|
56
|
+
// Prototype
|
|
57
|
+
document.querySelector('img').GSZoom({ type: 'magnifier' });
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
---
|
|
61
|
+
|
|
62
|
+
## React
|
|
63
|
+
|
|
64
|
+
The CSS is injected automatically when the component mounts — no manual stylesheet import needed.
|
|
65
|
+
|
|
66
|
+
```jsx
|
|
67
|
+
import GSZoom from '@get-set/gs-zoom';
|
|
68
|
+
|
|
69
|
+
// Lightbox — collection mode (wrap multiple images)
|
|
70
|
+
<GSZoom type="lightbox" arrows navigateWithKeys maxZoom={5}>
|
|
71
|
+
<img src="photo1.jpg" data-gstitle="Photo one" data-gssubtitle="Subtitle" />
|
|
72
|
+
<img src="photo2.jpg" />
|
|
73
|
+
</GSZoom>
|
|
74
|
+
|
|
75
|
+
// Lightbox — single image mode
|
|
76
|
+
<GSZoom type="lightbox" src="photo1.jpg" data-gstitle="Photo one" />
|
|
77
|
+
|
|
78
|
+
// Magnifier
|
|
79
|
+
<GSZoom
|
|
80
|
+
type="magnifier"
|
|
81
|
+
src="product.jpg"
|
|
82
|
+
magnifier={{ zoom: 4, form: 'circle', size: 180 }}
|
|
83
|
+
/>
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
---
|
|
87
|
+
|
|
88
|
+
## Parameters
|
|
89
|
+
|
|
90
|
+
| Param | Type | Default | Description |
|
|
91
|
+
|---------------------------------|-------------------------|---------------|--------------------------------------------------|
|
|
92
|
+
| `reference` | `string` | auto | Unique key. Auto-generated if omitted. |
|
|
93
|
+
| `type` | `'lightbox'\|'magnifier'` | `'lightbox'` | Plugin mode. |
|
|
94
|
+
| `arrows` | `boolean` | `true` | Show prev/next arrow buttons. |
|
|
95
|
+
| `navigateWithKeys` | `boolean` | `true` | Enable ← → keyboard navigation. |
|
|
96
|
+
| `showAdditionals` | `boolean` | `true` | Show thumbnail strip in lightbox. |
|
|
97
|
+
| `zoomOnWheel` | `boolean` | `true` | Zoom with mouse wheel. |
|
|
98
|
+
| `maxZoom` | `number` | `5` | Maximum zoom multiplier. |
|
|
99
|
+
| `disableFullScreen` | `boolean` | `false` | Prevent fullscreen API call on open. |
|
|
100
|
+
| `imgLoading` | `string` | `''` | HTML string shown while image loads. |
|
|
101
|
+
| `autoplaySpeed` | `number` | `5000` | Autoplay interval in ms. |
|
|
102
|
+
| `mainImageQueryParameters` | `string` | `''` | Query string appended to main image src. |
|
|
103
|
+
| `additionalImageQueryParameters`| `string` | `''` | Query string appended to thumbnail src. |
|
|
104
|
+
| `magnifier` | `MagnifierParams` | see below | Magnifier mode settings. |
|
|
105
|
+
| `responsive` | `ResponsiveOption[]` | `[]` | Breakpoint overrides (sorted automatically). |
|
|
106
|
+
| `beforeInit` | `() => void` | — | Callback before plugin initialises. |
|
|
107
|
+
| `afterInit` | `() => void` | — | Callback after plugin initialises. |
|
|
108
|
+
| `beforeLightBoxOpen` | `() => void` | — | Callback before lightbox opens. |
|
|
109
|
+
| `afterLightBoxOpen` | `() => void` | — | Callback after lightbox opens. |
|
|
110
|
+
| `beforeLightBoxClose` | `() => void` | — | Callback before lightbox closes. |
|
|
111
|
+
| `afterLightBoxClose` | `() => void` | — | Callback after lightbox closes. |
|
|
112
|
+
| `afterChange` | `() => void` | — | Callback after slide changes. |
|
|
113
|
+
|
|
114
|
+
### MagnifierParams
|
|
115
|
+
|
|
116
|
+
| Prop | Type | Default | Description |
|
|
117
|
+
|--------|--------------------|------------|-------------------------------|
|
|
118
|
+
| `zoom` | `number` | `3` | Zoom multiplier inside lens. |
|
|
119
|
+
| `form` | `'circle'\|'square'` | `'circle'` | Shape of the magnifier lens. |
|
|
120
|
+
| `size` | `number` | `150` | Lens diameter/side in px. |
|
|
121
|
+
|
|
122
|
+
### Image data attributes
|
|
123
|
+
|
|
124
|
+
| Attribute | Description |
|
|
125
|
+
|--------------------|----------------------------------------------|
|
|
126
|
+
| `data-gstitle` | Title text shown below the image in lightbox.|
|
|
127
|
+
| `data-gssubtitle` | Subtitle text. |
|
|
128
|
+
| `data-gsdescription` | Description text. |
|
|
129
|
+
| `data-gszoomsrc` | Alternative high-res src for the magnifier. |
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import initClose from './initClose';
|
|
2
|
+
|
|
3
|
+
export const getScale = (element: HTMLElement): number => {
|
|
4
|
+
const style = window.getComputedStyle(element);
|
|
5
|
+
const transform = style.transform;
|
|
6
|
+
|
|
7
|
+
if (transform !== 'none') {
|
|
8
|
+
const values = transform.match(/matrix.*\((.+)\)/)?.[1].split(', ');
|
|
9
|
+
if (values) {
|
|
10
|
+
return parseFloat(values[0]);
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
return 1;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export const getTranslateCoord = (element: HTMLElement): { x: number; y: number } => {
|
|
17
|
+
const style = window.getComputedStyle(element);
|
|
18
|
+
const matrix = new DOMMatrix(style.transform);
|
|
19
|
+
return {
|
|
20
|
+
x: matrix.e,
|
|
21
|
+
y: matrix.f,
|
|
22
|
+
};
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export const fullscreenchange = (): void => {
|
|
26
|
+
if (!document.fullscreenElement) {
|
|
27
|
+
initClose();
|
|
28
|
+
}
|
|
29
|
+
};
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
const getAdjustTransform = (
|
|
2
|
+
scale: number,
|
|
3
|
+
translatex: number,
|
|
4
|
+
translatey: number,
|
|
5
|
+
$img: HTMLElement,
|
|
6
|
+
$container: HTMLElement,
|
|
7
|
+
): [number, number] => {
|
|
8
|
+
const actualWidth = $img.clientWidth * scale;
|
|
9
|
+
const actualHeight = $img.clientHeight * scale;
|
|
10
|
+
const containerWidth = $container.clientWidth;
|
|
11
|
+
const containerHeight = $container.clientHeight;
|
|
12
|
+
const maxTranslateXValue = (actualWidth - containerWidth) / 2;
|
|
13
|
+
const maxTranslateYValue = (actualHeight - containerHeight) / 2;
|
|
14
|
+
const minTranslateXValue = (containerWidth - actualWidth) / 2;
|
|
15
|
+
const minTranslateYValue = (containerHeight - actualHeight) / 2;
|
|
16
|
+
let newTranslateXValue = translatex / scale;
|
|
17
|
+
let newTranslateYValue = translatey / scale;
|
|
18
|
+
|
|
19
|
+
if (actualWidth <= containerWidth) {
|
|
20
|
+
if (translatex !== 0) {
|
|
21
|
+
newTranslateXValue = 0;
|
|
22
|
+
}
|
|
23
|
+
} else {
|
|
24
|
+
if (translatex > maxTranslateXValue) {
|
|
25
|
+
newTranslateXValue = maxTranslateXValue / scale;
|
|
26
|
+
} else if (translatex < minTranslateXValue) {
|
|
27
|
+
newTranslateXValue = minTranslateXValue / scale;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
if (actualHeight <= containerHeight) {
|
|
32
|
+
if (translatey !== 0) {
|
|
33
|
+
newTranslateYValue = 0;
|
|
34
|
+
}
|
|
35
|
+
} else {
|
|
36
|
+
if (translatey > maxTranslateYValue) {
|
|
37
|
+
newTranslateYValue = maxTranslateYValue / scale;
|
|
38
|
+
} else if (translatey < minTranslateYValue) {
|
|
39
|
+
newTranslateYValue = minTranslateYValue / scale;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return [newTranslateXValue, newTranslateYValue];
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
export default getAdjustTransform;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import _defaultParams from '../constants/defaultParams';
|
|
2
|
+
import magnifierDefaultProps from '../constants/magnifierDefaultProps';
|
|
3
|
+
import { Params } from '../types/params';
|
|
4
|
+
|
|
5
|
+
const getCurrentParams = (params: Params): Params => {
|
|
6
|
+
const defaultParams: Params = { ..._defaultParams };
|
|
7
|
+
let finalParams: Params = {
|
|
8
|
+
...defaultParams,
|
|
9
|
+
...params,
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
if (finalParams.responsive && finalParams.responsive.length > 0) {
|
|
13
|
+
finalParams.responsive.sort((a, b) => b.windowSize - a.windowSize);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
if (finalParams.responsive && finalParams.responsive.length > 0) {
|
|
17
|
+
const windowSize = window.innerWidth;
|
|
18
|
+
const availableResponsives = finalParams.responsive.filter(
|
|
19
|
+
(resp) => resp.windowSize >= windowSize,
|
|
20
|
+
);
|
|
21
|
+
|
|
22
|
+
availableResponsives.forEach((resp) => {
|
|
23
|
+
finalParams = {
|
|
24
|
+
...finalParams,
|
|
25
|
+
...resp.params,
|
|
26
|
+
magnifier: {
|
|
27
|
+
...magnifierDefaultProps,
|
|
28
|
+
...resp.magnifier,
|
|
29
|
+
},
|
|
30
|
+
};
|
|
31
|
+
});
|
|
32
|
+
} else {
|
|
33
|
+
finalParams.magnifier = {
|
|
34
|
+
...magnifierDefaultProps,
|
|
35
|
+
...finalParams.magnifier,
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
return finalParams;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
export default getCurrentParams;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { getTranslateCoord } from './general';
|
|
2
|
+
|
|
3
|
+
const getTranslateCoordToPoint = (
|
|
4
|
+
$img: HTMLElement,
|
|
5
|
+
$container: HTMLElement,
|
|
6
|
+
scale: number,
|
|
7
|
+
newScale: number,
|
|
8
|
+
clientX: number,
|
|
9
|
+
clientY: number,
|
|
10
|
+
): [number, number] => {
|
|
11
|
+
const coords = getTranslateCoord($img);
|
|
12
|
+
const translatex = coords.x;
|
|
13
|
+
const translatey = coords.y;
|
|
14
|
+
const actualWidth = $img.clientWidth * scale;
|
|
15
|
+
const actualHeight = $img.clientHeight * scale;
|
|
16
|
+
const containerWidth = $container.clientWidth;
|
|
17
|
+
const containerHeight = $container.clientHeight;
|
|
18
|
+
|
|
19
|
+
const mouseDirectionY =
|
|
20
|
+
(clientY - (containerHeight - actualHeight) / 2 - translatey) / actualHeight;
|
|
21
|
+
const mouseDirectionX =
|
|
22
|
+
(clientX - (containerWidth - actualWidth) / 2 - translatex) / actualWidth;
|
|
23
|
+
|
|
24
|
+
const newActualWidth = (actualWidth * newScale) / scale;
|
|
25
|
+
const newActualHeight = (actualHeight * newScale) / scale;
|
|
26
|
+
|
|
27
|
+
const newTranslatex =
|
|
28
|
+
(newActualWidth - containerWidth) / 2 -
|
|
29
|
+
(newActualWidth * mouseDirectionX - clientX);
|
|
30
|
+
const newTranslatey =
|
|
31
|
+
(newActualHeight - containerHeight) / 2 -
|
|
32
|
+
(newActualHeight * mouseDirectionY - clientY);
|
|
33
|
+
|
|
34
|
+
return [newTranslatex, newTranslatey];
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
export default getTranslateCoordToPoint;
|
package/actions/init.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import types from '../constants/types';
|
|
2
|
+
import initLightBox from './initLightBox';
|
|
3
|
+
import initMagnifier from './initMagnifier';
|
|
4
|
+
import { Ref } from '../types/ref';
|
|
5
|
+
|
|
6
|
+
const init = (ref: Ref): void => {
|
|
7
|
+
if (typeof ref.currentParams.beforeInit === 'function') {
|
|
8
|
+
ref.currentParams.beforeInit();
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
switch (ref.currentParams.type) {
|
|
12
|
+
case types.lightbox:
|
|
13
|
+
initLightBox(ref);
|
|
14
|
+
break;
|
|
15
|
+
case types.magnifier:
|
|
16
|
+
initMagnifier(ref);
|
|
17
|
+
break;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
if (typeof ref.currentParams.afterInit === 'function') {
|
|
21
|
+
ref.currentParams.afterInit();
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export default init;
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import icons from '../constants/icons';
|
|
2
|
+
import getAdjustTransform from './getAdjustTransform';
|
|
3
|
+
import getTranslateCoordToPoint from './getTranslateCoordToPoint';
|
|
4
|
+
import { initAutoplay, stopAutoplay } from './initAutoplay';
|
|
5
|
+
import { exitFullScreen } from './initFullScreen';
|
|
6
|
+
import setStyles from './setStyles';
|
|
7
|
+
import { Ref } from '../types/ref';
|
|
8
|
+
|
|
9
|
+
const initActionEvents = (ref: Ref): void => {
|
|
10
|
+
const params = ref.currentParams;
|
|
11
|
+
const $container = ref.$container!;
|
|
12
|
+
const $btnAdditionals = ref.$btnAdditionals;
|
|
13
|
+
const $btnAutoplay = ref.$btnAutoplay;
|
|
14
|
+
const $btnClose = ref.$btnClose;
|
|
15
|
+
const $btnZoomIn = ref.$btnZoomIn;
|
|
16
|
+
const $btnZoomOut = ref.$btnZoomOut;
|
|
17
|
+
|
|
18
|
+
if ($btnAdditionals != null) {
|
|
19
|
+
$btnAdditionals.addEventListener('click', () => {
|
|
20
|
+
$container
|
|
21
|
+
.querySelector('.gs-zoom-additionals')
|
|
22
|
+
?.classList.toggle('gs-zoom-hidden');
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
if ($btnAutoplay != null) {
|
|
27
|
+
$btnAutoplay.addEventListener('click', () => {
|
|
28
|
+
if (ref.autoplay === undefined) {
|
|
29
|
+
initAutoplay(ref);
|
|
30
|
+
$btnAutoplay.innerHTML = icons.autoplayPause;
|
|
31
|
+
} else {
|
|
32
|
+
stopAutoplay(ref);
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
if ($btnClose != null) {
|
|
38
|
+
$btnClose.addEventListener('click', () => {
|
|
39
|
+
exitFullScreen();
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
if ($btnZoomIn != null) {
|
|
44
|
+
$btnZoomIn.addEventListener('click', () => {
|
|
45
|
+
const $img = ref.$img!;
|
|
46
|
+
const scale =
|
|
47
|
+
$img.dataset.scale !== undefined ? parseFloat($img.dataset.scale) : 1;
|
|
48
|
+
|
|
49
|
+
if (scale < params.maxZoom!) {
|
|
50
|
+
clearTimeout(ref.zoomButtonTimeout);
|
|
51
|
+
const newScale = Math.min(scale + 0.4, params.maxZoom!);
|
|
52
|
+
$img.dataset.scale = String(newScale);
|
|
53
|
+
let [newTranslatex, newTranslatey] = getTranslateCoordToPoint(
|
|
54
|
+
$img, $container, scale, newScale,
|
|
55
|
+
$container.clientWidth / 2,
|
|
56
|
+
$container.clientHeight / 2,
|
|
57
|
+
);
|
|
58
|
+
[newTranslatex, newTranslatey] = getAdjustTransform(
|
|
59
|
+
newScale, newTranslatex, newTranslatey, $img, $container,
|
|
60
|
+
);
|
|
61
|
+
setStyles(ref, $img, {
|
|
62
|
+
transition: '.3s ease-in-out',
|
|
63
|
+
transform: `scale(${newScale}) translate(${newTranslatex}px, ${newTranslatey}px)`,
|
|
64
|
+
});
|
|
65
|
+
ref.zoomButtonTimeout = setTimeout(() => {
|
|
66
|
+
setStyles(ref, $img, { transition: 'unset' });
|
|
67
|
+
}, 300);
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
if ($btnZoomOut != null) {
|
|
73
|
+
$btnZoomOut.addEventListener('click', () => {
|
|
74
|
+
const $img = ref.$img!;
|
|
75
|
+
const scale =
|
|
76
|
+
$img.dataset.scale !== undefined ? parseFloat($img.dataset.scale) : 1;
|
|
77
|
+
|
|
78
|
+
if (scale > 1) {
|
|
79
|
+
clearTimeout(ref.zoomButtonTimeout);
|
|
80
|
+
const newScale = Math.max(1, scale - 0.4);
|
|
81
|
+
$img.dataset.scale = String(newScale);
|
|
82
|
+
let [newTranslatex, newTranslatey] = getTranslateCoordToPoint(
|
|
83
|
+
$img, $container, scale, newScale,
|
|
84
|
+
$container.clientWidth / 2,
|
|
85
|
+
$container.clientHeight / 2,
|
|
86
|
+
);
|
|
87
|
+
[newTranslatex, newTranslatey] = getAdjustTransform(
|
|
88
|
+
newScale, newTranslatex, newTranslatey, $img, $container,
|
|
89
|
+
);
|
|
90
|
+
setStyles(ref, $img, {
|
|
91
|
+
transition: '.3s ease-in-out',
|
|
92
|
+
transform: `scale(${newScale}) translate(${newTranslatex}px, ${newTranslatey}px)`,
|
|
93
|
+
});
|
|
94
|
+
ref.zoomButtonTimeout = setTimeout(() => {
|
|
95
|
+
setStyles(ref, $img, { transition: 'unset' });
|
|
96
|
+
}, 300);
|
|
97
|
+
}
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
export default initActionEvents;
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { getTranslateCoord } from './general';
|
|
2
|
+
import initChange from './initChange';
|
|
3
|
+
import { Ref } from '../types/ref';
|
|
4
|
+
|
|
5
|
+
const initAdditionals = (ref: Ref): void => {
|
|
6
|
+
const params = ref.currentParams;
|
|
7
|
+
|
|
8
|
+
if (params.showAdditionals && ref.list.length > 1) {
|
|
9
|
+
const $container = ref.$container!;
|
|
10
|
+
const $addContainer = document.createElement('div');
|
|
11
|
+
const $addList = document.createElement('div');
|
|
12
|
+
const $addListContainer = document.createElement('div');
|
|
13
|
+
|
|
14
|
+
$addContainer.classList.add('gs-zoom-additionals', 'gs-zoom-hide-inzoomed', 'gs-zoom-hidden');
|
|
15
|
+
$addListContainer.classList.add('gs-add-list-container');
|
|
16
|
+
$addList.classList.add('gs-add-list');
|
|
17
|
+
$addList.style.transform = 'translateX(0px)';
|
|
18
|
+
|
|
19
|
+
ref.list.forEach((el, index) => {
|
|
20
|
+
const $item = document.createElement('div');
|
|
21
|
+
$item.dataset.index = String(index);
|
|
22
|
+
if (index === ref.currentIndex) {
|
|
23
|
+
$item.classList.add('active');
|
|
24
|
+
}
|
|
25
|
+
const $itemImg = document.createElement('img');
|
|
26
|
+
$itemImg.src = el.src.split('?')[0] + params.additionalImageQueryParameters;
|
|
27
|
+
$itemImg.onload = () => {
|
|
28
|
+
$item.classList.remove('gz-zoom-is-loading');
|
|
29
|
+
};
|
|
30
|
+
$item.classList.add('gs-add-item', 'gz-zoom-is-loading');
|
|
31
|
+
$item.append($itemImg);
|
|
32
|
+
$addList.append($item);
|
|
33
|
+
$item.addEventListener('click', () => {
|
|
34
|
+
initChange(ref, index);
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
$addListContainer.append($addList);
|
|
39
|
+
$addContainer.append($addListContainer);
|
|
40
|
+
$container.append($addContainer);
|
|
41
|
+
|
|
42
|
+
const clampTranslate = () => {
|
|
43
|
+
const translate = getTranslateCoord($addList).x;
|
|
44
|
+
if (translate > 0) {
|
|
45
|
+
$addList.style.transform = 'translateX(0px)';
|
|
46
|
+
} else if (translate < $container.clientWidth - $addList.clientWidth) {
|
|
47
|
+
$addList.style.transform = `translateX(${$container.clientWidth - $addList.clientWidth}px)`;
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
$addList.ontouchstart = $addList.onmousedown = (e: MouseEvent | TouchEvent) => {
|
|
52
|
+
if ($addList.clientWidth > $container.clientWidth) {
|
|
53
|
+
const te = e as TouchEvent;
|
|
54
|
+
window.initX =
|
|
55
|
+
te.targetTouches?.length > 0 ? te.targetTouches[0].clientX : (e as MouseEvent).clientX;
|
|
56
|
+
window.initListTranslateX = getTranslateCoord($addList).x;
|
|
57
|
+
$container.classList.add('gs-additional-isdragging');
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
$container.addEventListener('touchmove', (e: TouchEvent) => {
|
|
62
|
+
if ($container.classList.contains('gs-additional-isdragging')) {
|
|
63
|
+
window.clientX = e.targetTouches[0].clientX;
|
|
64
|
+
$addList.style.transform = `translateX(${window.initListTranslateX + (window.clientX - window.initX)}px)`;
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
$container.addEventListener('mousemove', (e: MouseEvent) => {
|
|
69
|
+
if (
|
|
70
|
+
$container.classList.contains('gs-additional-isdragging') &&
|
|
71
|
+
$addList.clientWidth > $container.clientWidth
|
|
72
|
+
) {
|
|
73
|
+
window.clientX = e.clientX;
|
|
74
|
+
$addList.style.transform = `translateX(${window.initListTranslateX + (window.clientX - window.initX)}px)`;
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
const onDragEnd = () => {
|
|
79
|
+
if ($container.classList.contains('gs-additional-isdragging')) {
|
|
80
|
+
$container.classList.remove('gs-additional-isdragging');
|
|
81
|
+
clampTranslate();
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
$container.addEventListener('mouseleave', onDragEnd);
|
|
86
|
+
$container.addEventListener('mouseup', onDragEnd);
|
|
87
|
+
$container.addEventListener('touchend', onDragEnd);
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
export default initAdditionals;
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { getTranslateCoord } from './general';
|
|
2
|
+
import getAdjustTransform from './getAdjustTransform';
|
|
3
|
+
import getTranslateCoordToPoint from './getTranslateCoordToPoint';
|
|
4
|
+
import setStyles from './setStyles';
|
|
5
|
+
import { Ref } from '../types/ref';
|
|
6
|
+
|
|
7
|
+
const initAdjust = (
|
|
8
|
+
ref: Ref,
|
|
9
|
+
$container: HTMLElement,
|
|
10
|
+
$img: HTMLElement,
|
|
11
|
+
withAnimation: boolean = true,
|
|
12
|
+
): void => {
|
|
13
|
+
if ($img.classList.contains('gs-zoom-isdragging') || !withAnimation) {
|
|
14
|
+
const params = ref.currentParams;
|
|
15
|
+
let needAdjust = false;
|
|
16
|
+
|
|
17
|
+
if (withAnimation) {
|
|
18
|
+
$img.classList.remove('gs-zoom-isdragging');
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
let scale = $img.dataset.scale != null ? parseFloat($img.dataset.scale) : 1;
|
|
22
|
+
let isOverzoomed = false;
|
|
23
|
+
const oldScale = scale;
|
|
24
|
+
|
|
25
|
+
if (scale < 1) {
|
|
26
|
+
scale = 1;
|
|
27
|
+
needAdjust = true;
|
|
28
|
+
} else if (scale > params.maxZoom!) {
|
|
29
|
+
isOverzoomed = true;
|
|
30
|
+
scale = params.maxZoom!;
|
|
31
|
+
needAdjust = true;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
$img.dataset.scale = String(scale);
|
|
35
|
+
|
|
36
|
+
const actualWidth = $img.clientWidth * scale;
|
|
37
|
+
const actualHeight = $img.clientHeight * scale;
|
|
38
|
+
const containerWidth = $container.clientWidth;
|
|
39
|
+
const containerHeight = $container.clientHeight;
|
|
40
|
+
const coords = getTranslateCoord($img);
|
|
41
|
+
const translatex = coords.x;
|
|
42
|
+
const translatey = coords.y;
|
|
43
|
+
const maxTranslateXValue = (actualWidth - containerWidth) / 2;
|
|
44
|
+
const maxTranslateYValue = (actualHeight - containerHeight) / 2;
|
|
45
|
+
const minTranslateXValue = (containerWidth - actualWidth) / 2;
|
|
46
|
+
const minTranslateYValue = (containerHeight - actualHeight) / 2;
|
|
47
|
+
let newTranslateXValue = translatex / scale;
|
|
48
|
+
let newTranslateYValue = translatey / scale;
|
|
49
|
+
|
|
50
|
+
if (actualWidth <= containerWidth) {
|
|
51
|
+
if (translatex !== 0) {
|
|
52
|
+
needAdjust = true;
|
|
53
|
+
newTranslateXValue = 0;
|
|
54
|
+
}
|
|
55
|
+
} else {
|
|
56
|
+
if (translatex > maxTranslateXValue) {
|
|
57
|
+
needAdjust = true;
|
|
58
|
+
newTranslateXValue = maxTranslateXValue / scale;
|
|
59
|
+
} else if (translatex < minTranslateXValue) {
|
|
60
|
+
needAdjust = true;
|
|
61
|
+
newTranslateXValue = minTranslateXValue / scale;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
if (actualHeight <= containerHeight) {
|
|
66
|
+
if (translatey !== 0) {
|
|
67
|
+
needAdjust = true;
|
|
68
|
+
newTranslateYValue = 0;
|
|
69
|
+
}
|
|
70
|
+
} else {
|
|
71
|
+
if (translatey > maxTranslateYValue) {
|
|
72
|
+
needAdjust = true;
|
|
73
|
+
newTranslateYValue = maxTranslateYValue / scale;
|
|
74
|
+
} else if (translatey < minTranslateYValue) {
|
|
75
|
+
needAdjust = true;
|
|
76
|
+
newTranslateYValue = minTranslateYValue / scale;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
if (isOverzoomed) {
|
|
81
|
+
const clientCenterX = (window.clientX + window.clientX2) / 2;
|
|
82
|
+
const clientCenterY = (window.clientY + window.clientY2) / 2;
|
|
83
|
+
let [newTranslatex, newTranslatey] = getTranslateCoordToPoint(
|
|
84
|
+
$img,
|
|
85
|
+
$container,
|
|
86
|
+
oldScale,
|
|
87
|
+
scale,
|
|
88
|
+
clientCenterX,
|
|
89
|
+
clientCenterY,
|
|
90
|
+
);
|
|
91
|
+
[newTranslatex, newTranslatey] = getAdjustTransform(
|
|
92
|
+
scale,
|
|
93
|
+
newTranslatex,
|
|
94
|
+
newTranslatey,
|
|
95
|
+
$img,
|
|
96
|
+
$container,
|
|
97
|
+
);
|
|
98
|
+
newTranslateXValue = newTranslatex;
|
|
99
|
+
newTranslateYValue = newTranslatey;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
if (needAdjust) {
|
|
103
|
+
if (withAnimation) {
|
|
104
|
+
$img.classList.add('gs-zoom-is-adjusting');
|
|
105
|
+
}
|
|
106
|
+
setStyles(ref, $img, {
|
|
107
|
+
transform: `scale(${scale}) translate(${newTranslateXValue}px, ${newTranslateYValue}px)`,
|
|
108
|
+
});
|
|
109
|
+
$img.dataset.translatex = String(newTranslateXValue * scale);
|
|
110
|
+
$img.dataset.translatey = String(newTranslateYValue * scale);
|
|
111
|
+
if (withAnimation) {
|
|
112
|
+
setTimeout(() => {
|
|
113
|
+
$img.classList.remove('gs-zoom-is-adjusting');
|
|
114
|
+
}, 400);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
export default initAdjust;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { getTranslateCoord } from './general';
|
|
2
|
+
import { Ref } from '../types/ref';
|
|
3
|
+
|
|
4
|
+
const initAdjustAdditionalActive = (ref: Ref): void => {
|
|
5
|
+
if (ref.currentParams.showAdditionals && ref.list.length > 1) {
|
|
6
|
+
const $container = ref.$container!;
|
|
7
|
+
const $active = $container.querySelector('.gs-add-list .gs-add-item.active') as HTMLElement;
|
|
8
|
+
|
|
9
|
+
if ($active != null) {
|
|
10
|
+
const $list = $container.querySelector('.gs-add-list') as HTMLElement;
|
|
11
|
+
const tranlateLeft = getTranslateCoord($list).x;
|
|
12
|
+
const offsetLeft = $active.offsetLeft;
|
|
13
|
+
const containerWidth = $container.clientWidth;
|
|
14
|
+
const itemWidth = $active.clientWidth;
|
|
15
|
+
const listWidth = $list.clientWidth;
|
|
16
|
+
const offsetContainer = offsetLeft + tranlateLeft;
|
|
17
|
+
const gap = parseFloat(getComputedStyle($list).columnGap);
|
|
18
|
+
|
|
19
|
+
if (offsetContainer < itemWidth + gap) {
|
|
20
|
+
$list.style.transform = `translateX(${Math.min(itemWidth + gap - offsetLeft, 0)}px)`;
|
|
21
|
+
} else if (offsetContainer > containerWidth - (2 * itemWidth + gap)) {
|
|
22
|
+
$list.style.transform = `translateX(${Math.max(
|
|
23
|
+
containerWidth - offsetLeft - 2 * itemWidth - gap,
|
|
24
|
+
containerWidth - listWidth,
|
|
25
|
+
)}px)`;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
export default initAdjustAdditionalActive;
|