@dpouris/gswap 0.0.1 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- package/README.md +4 -4
- package/dist/index.d.ts +1 -0
- package/dist/index.js +5 -0
- package/dist/src/main.d.ts +11 -0
- package/dist/src/main.js +177 -0
- package/dist/types/GallerySwapTypes.d.ts +14 -0
- package/dist/types/GallerySwapTypes.js +2 -0
- package/package.json +8 -4
package/README.md
CHANGED
@@ -104,10 +104,10 @@ This will create a new instance of gswap and will place the gallery absolutely i
|
|
104
104
|
|
105
105
|
# Methods 🧑💻
|
106
106
|
|
107
|
-
- [**gswap.next()**](
|
108
|
-
- [**gswap.prev()**](
|
109
|
-
- [**gswap.goTo(index)**](
|
110
|
-
- [**gswap.stackImages()**](
|
107
|
+
- [**gswap.next()**](<#gswap.next()>)
|
108
|
+
- [**gswap.prev()**](<#gswap.next()>)
|
109
|
+
- [**gswap.goTo(index)**](<#gswap.goTo(index)>) -> [wip]
|
110
|
+
- [**gswap.stackImages()**](<#gswap.stackImages()>)
|
111
111
|
|
112
112
|
---
|
113
113
|
|
package/dist/index.d.ts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
export { GSwap as GSwap } from "./src/main";
|
package/dist/index.js
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
import type { Options } from "../types/GallerySwapTypes";
|
2
|
+
export declare class GSwap {
|
3
|
+
#private;
|
4
|
+
containerElem: HTMLDivElement;
|
5
|
+
images: string[];
|
6
|
+
options: Options;
|
7
|
+
constructor(containerElem: HTMLDivElement, images: string[], options: Options);
|
8
|
+
stackImages: () => void;
|
9
|
+
next: () => void;
|
10
|
+
prev: () => void;
|
11
|
+
}
|
package/dist/src/main.js
ADDED
@@ -0,0 +1,177 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
3
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
4
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
5
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
6
|
+
};
|
7
|
+
var _GSwap_instances, _GSwap_currentImg, _GSwap_createGSwapElement, _GSwap_createImageElements, _GSwap_createNavigation, _GSwap_appendElementsOnContainer, _GSwap_shiftImagesToTheRight, _GSwap_shiftImagesToTheLeft, _GSwap_findPrevActiveElem, _GSwap_findNextActiveElement;
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
9
|
+
exports.GSwap = void 0;
|
10
|
+
class GSwap {
|
11
|
+
constructor(containerElem, images, options) {
|
12
|
+
_GSwap_instances.add(this);
|
13
|
+
_GSwap_currentImg.set(this, 0);
|
14
|
+
_GSwap_createNavigation.set(this, () => {
|
15
|
+
const nav = document.createElement("nav");
|
16
|
+
nav.classList.add("gallery-swap-nav");
|
17
|
+
const navLeft = document.createElement("button");
|
18
|
+
navLeft.onclick = this.prev;
|
19
|
+
navLeft.innerHTML = "→";
|
20
|
+
navLeft.classList.add("gallery-swap-nav-left");
|
21
|
+
const navRight = document.createElement("button");
|
22
|
+
navRight.onclick = this.next;
|
23
|
+
navRight.innerHTML = "←";
|
24
|
+
navRight.classList.add("gallery-swap-nav-right");
|
25
|
+
if (this.options.navigation === "forwardOnly") {
|
26
|
+
nav.appendChild(navRight);
|
27
|
+
return nav;
|
28
|
+
}
|
29
|
+
if (this.options.navigation === "backOnly") {
|
30
|
+
nav.appendChild(navLeft);
|
31
|
+
return nav;
|
32
|
+
}
|
33
|
+
nav.appendChild(navLeft);
|
34
|
+
nav.appendChild(navRight);
|
35
|
+
return nav;
|
36
|
+
});
|
37
|
+
_GSwap_shiftImagesToTheRight.set(this, () => {
|
38
|
+
const last = this.containerElem.children[0].lastElementChild;
|
39
|
+
this.containerElem.children[0].insertAdjacentHTML("afterbegin", last.outerHTML);
|
40
|
+
last.remove();
|
41
|
+
});
|
42
|
+
_GSwap_shiftImagesToTheLeft.set(this, () => {
|
43
|
+
const first = this.containerElem.children[0]
|
44
|
+
.firstElementChild;
|
45
|
+
this.containerElem.children[0].insertAdjacentHTML("beforeend", first.outerHTML);
|
46
|
+
first.style.opacity = "0";
|
47
|
+
first.remove();
|
48
|
+
// first.remove();
|
49
|
+
});
|
50
|
+
_GSwap_findPrevActiveElem.set(this, () => {
|
51
|
+
this.containerElem.children[0].childNodes.forEach((image) => {
|
52
|
+
const imgElem = image;
|
53
|
+
if (imgElem.classList.contains("active")) {
|
54
|
+
imgElem.classList.remove("active");
|
55
|
+
}
|
56
|
+
});
|
57
|
+
const activeElem = this.containerElem.children[0]
|
58
|
+
.lastElementChild;
|
59
|
+
activeElem.style.opacity = "0";
|
60
|
+
setTimeout(() => {
|
61
|
+
activeElem.style.opacity = "1";
|
62
|
+
}, this.options.animationDuration);
|
63
|
+
activeElem.classList.add("active");
|
64
|
+
});
|
65
|
+
_GSwap_findNextActiveElement.set(this, () => {
|
66
|
+
this.containerElem.children[0].childNodes.forEach((image) => {
|
67
|
+
const imgElem = image;
|
68
|
+
if (imgElem.classList.contains("active")) {
|
69
|
+
imgElem.classList.remove("active");
|
70
|
+
imgElem.style.opacity = "0";
|
71
|
+
setTimeout(() => {
|
72
|
+
imgElem.style.opacity = "1";
|
73
|
+
}, this.options.animationDuration);
|
74
|
+
}
|
75
|
+
});
|
76
|
+
const activeElem = this.containerElem.children[0].lastElementChild;
|
77
|
+
activeElem.classList.add("active");
|
78
|
+
});
|
79
|
+
this.stackImages = () => {
|
80
|
+
let directionLeft;
|
81
|
+
let directionTop;
|
82
|
+
switch (this.options.direction) {
|
83
|
+
case "left":
|
84
|
+
directionLeft = 20;
|
85
|
+
directionTop = 20;
|
86
|
+
break;
|
87
|
+
case "right":
|
88
|
+
directionLeft = -20;
|
89
|
+
directionTop = 20;
|
90
|
+
break;
|
91
|
+
case "top":
|
92
|
+
directionLeft = 0;
|
93
|
+
directionTop = 20;
|
94
|
+
break;
|
95
|
+
case "bottom":
|
96
|
+
directionLeft = 0;
|
97
|
+
directionTop = -20;
|
98
|
+
break;
|
99
|
+
}
|
100
|
+
let counter = 0;
|
101
|
+
this.containerElem.children[0].childNodes.forEach((image) => {
|
102
|
+
image.style.position = "absolute";
|
103
|
+
image.style.opacity = "1";
|
104
|
+
image.style.top =
|
105
|
+
(counter * directionTop).toString() + "px";
|
106
|
+
image.style.left =
|
107
|
+
(counter * directionLeft).toString() + "px";
|
108
|
+
counter++;
|
109
|
+
});
|
110
|
+
};
|
111
|
+
this.next = () => {
|
112
|
+
__classPrivateFieldGet(this, _GSwap_shiftImagesToTheRight, "f").call(this);
|
113
|
+
this.stackImages();
|
114
|
+
__classPrivateFieldGet(this, _GSwap_findNextActiveElement, "f").call(this);
|
115
|
+
};
|
116
|
+
this.prev = () => {
|
117
|
+
__classPrivateFieldGet(this, _GSwap_shiftImagesToTheLeft, "f").call(this);
|
118
|
+
this.stackImages();
|
119
|
+
__classPrivateFieldGet(this, _GSwap_findPrevActiveElem, "f").call(this);
|
120
|
+
};
|
121
|
+
this.containerElem = containerElem;
|
122
|
+
this.images = images;
|
123
|
+
this.options = options;
|
124
|
+
this.options.imgDimensions = this.options
|
125
|
+
.imgDimensions.hasOwnProperty("height")
|
126
|
+
.hasOwnProperty("width")
|
127
|
+
? this.options.imgDimensions
|
128
|
+
: { width: 300, height: 300 };
|
129
|
+
this.options.direction = this.options.direction;
|
130
|
+
this.options.animationDuration =
|
131
|
+
this.options.animationDuration === undefined
|
132
|
+
? 300
|
133
|
+
: this.options.animationDuration;
|
134
|
+
this.options.direction =
|
135
|
+
this.options.direction === undefined ? "left" : this.options.direction;
|
136
|
+
this.options.navigation =
|
137
|
+
this.options.navigation === undefined ? true : this.options.navigation;
|
138
|
+
__classPrivateFieldGet(this, _GSwap_instances, "m", _GSwap_appendElementsOnContainer).call(this);
|
139
|
+
this.stackImages();
|
140
|
+
}
|
141
|
+
}
|
142
|
+
exports.GSwap = GSwap;
|
143
|
+
_GSwap_currentImg = new WeakMap(), _GSwap_createNavigation = new WeakMap(), _GSwap_shiftImagesToTheRight = new WeakMap(), _GSwap_shiftImagesToTheLeft = new WeakMap(), _GSwap_findPrevActiveElem = new WeakMap(), _GSwap_findNextActiveElement = new WeakMap(), _GSwap_instances = new WeakSet(), _GSwap_createGSwapElement = function _GSwap_createGSwapElement() {
|
144
|
+
const GSElement = document.createElement("div");
|
145
|
+
GSElement.classList.add("gallery-swap");
|
146
|
+
// GSElement.style.transition = `all ${this.options.animationDuration} ${this.options.animation}`;
|
147
|
+
GSElement.style.height = this.options.imgDimensions.height * 2 + "px";
|
148
|
+
GSElement.style.width = this.options.imgDimensions.width * 2 + "px";
|
149
|
+
GSElement.style.position = "relative";
|
150
|
+
// GSElement.style.animation = this.options.animation;
|
151
|
+
// GSElement.style.animationDuration = this.options.animationDuration;
|
152
|
+
// const imageContainer = document.createElement("div");
|
153
|
+
// Place images inside div container
|
154
|
+
const images = __classPrivateFieldGet(this, _GSwap_instances, "m", _GSwap_createImageElements).call(this);
|
155
|
+
images.forEach((image) => {
|
156
|
+
GSElement.appendChild(image);
|
157
|
+
});
|
158
|
+
return GSElement;
|
159
|
+
}, _GSwap_createImageElements = function _GSwap_createImageElements() {
|
160
|
+
return this.images.map((image) => {
|
161
|
+
const imgElement = document.createElement("img");
|
162
|
+
if (image === this.images[this.images.length - 1]) {
|
163
|
+
imgElement.classList.add("active");
|
164
|
+
}
|
165
|
+
imgElement.src = image;
|
166
|
+
imgElement.width = this.options.imgDimensions.width;
|
167
|
+
imgElement.height = this.options.imgDimensions.height;
|
168
|
+
imgElement.style.transition = `all ${this.options.animationDuration}ms ease-in-out`;
|
169
|
+
return imgElement;
|
170
|
+
});
|
171
|
+
}, _GSwap_appendElementsOnContainer = function _GSwap_appendElementsOnContainer() {
|
172
|
+
this.containerElem.innerHTML = "";
|
173
|
+
this.containerElem.appendChild(__classPrivateFieldGet(this, _GSwap_instances, "m", _GSwap_createGSwapElement).call(this));
|
174
|
+
if (this.options.navigation === true) {
|
175
|
+
this.containerElem.appendChild(__classPrivateFieldGet(this, _GSwap_createNavigation, "f").call(this));
|
176
|
+
}
|
177
|
+
};
|
@@ -0,0 +1,14 @@
|
|
1
|
+
export interface GallerySwap {
|
2
|
+
container: HTMLDivElement;
|
3
|
+
}
|
4
|
+
export declare type Options = {
|
5
|
+
animation?: string;
|
6
|
+
animationDuration?: number;
|
7
|
+
navigation?: boolean | "forwardOnly" | "backOnly";
|
8
|
+
repeat?: boolean;
|
9
|
+
direction?: "left" | "right" | "top" | "bottom";
|
10
|
+
imgDimensions?: {
|
11
|
+
height: number;
|
12
|
+
width: number;
|
13
|
+
};
|
14
|
+
};
|
package/package.json
CHANGED
@@ -1,20 +1,24 @@
|
|
1
1
|
{
|
2
2
|
"name": "@dpouris/gswap",
|
3
|
-
"version": "0.0.
|
3
|
+
"version": "0.0.4",
|
4
4
|
"description": "A library for swapping between images in a gallery",
|
5
5
|
"main": "./dist/main.js",
|
6
6
|
"types": "./dist/main.d.ts",
|
7
7
|
"files": [
|
8
|
-
"
|
8
|
+
"/dist",
|
9
9
|
"./README.md"
|
10
10
|
],
|
11
11
|
"directories": {
|
12
|
-
"example": "examples"
|
12
|
+
"example": "examples",
|
13
|
+
"gswap": "dist"
|
13
14
|
},
|
14
15
|
"scripts": {
|
15
16
|
"test": "echo \"Error: no test specified\" && exit 1"
|
16
17
|
},
|
17
18
|
"keywords": [],
|
18
19
|
"author": "dpouris",
|
19
|
-
"license": "ISC"
|
20
|
+
"license": "ISC",
|
21
|
+
"dependencies": {
|
22
|
+
"@dpouris/gswap": "^0.0.3"
|
23
|
+
}
|
20
24
|
}
|