-
-
Notifications
You must be signed in to change notification settings - Fork 876
/
Copy pathLoader.ts
262 lines (216 loc) · 8.78 KB
/
Loader.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
import { errorPrefix, generatedAttribute } from "./Utils/Constants";
import { Container } from "./Container";
import type { Engine } from "../engine";
import type { IOptions } from "../Options/Interfaces/IOptions";
import type { LoaderParams } from "./Interfaces/LoaderParams";
import type { RecursivePartial } from "../Types/RecursivePartial";
import type { SingleOrMultiple } from "../Types/SingleOrMultiple";
import { getRandom } from "../Utils/NumberUtils";
import { itemFromSingleOrMultiple } from "../Utils/Utils";
async function getDataFromUrl(
jsonUrl?: SingleOrMultiple<string>,
index?: number
): Promise<SingleOrMultiple<RecursivePartial<IOptions>> | undefined> {
const url = itemFromSingleOrMultiple(jsonUrl, index);
if (!url) {
return;
}
const response = await fetch(url);
if (response.ok) {
return response.json();
}
console.error(`${errorPrefix} ${response.status} while retrieving config file`);
}
/**
* Main class for creating the {@link Container} objects
* @category Core
*/
export class Loader {
/**
* The engine containing this Loader instance
* @private
*/
private readonly _engine;
/**
* Loader constructor, assigns the engine
* @param engine the engine containing this Loader instance
*/
constructor(engine: Engine) {
this._engine = engine;
}
/**
* Loads the provided options to create a {@link Container} object.
* @param tagId the particles container element id
* @param options the options object to initialize the {@link Container}
* @param index if an options array is provided, this will retrieve the exact index of that array
*/
load(
tagId: string | SingleOrMultiple<RecursivePartial<IOptions>>,
options?: SingleOrMultiple<RecursivePartial<IOptions>> | number,
index?: number
): Promise<Container | undefined> {
const params: LoaderParams = { index, remote: false };
if (typeof tagId === "string") {
params.tagId = tagId;
} else {
params.options = tagId;
}
if (typeof options === "number") {
params.index = options;
} else {
params.options = options ?? params.options;
}
return this.loadOptions(params);
}
/**
* Loads the provided json with a GET request. The content will be used to create a {@link Container} object.
* This method is async, so if you need a callback refer to JavaScript function `fetch`
* @param tagId the particles container element id
* @param jsonUrl the json path (or paths array) to use in the GET request
* @param index the index of the paths array, if a single path is passed this value is ignored
* @returns A Promise with the {@link Container} object created
*/
async loadJSON(
tagId: string | SingleOrMultiple<string>,
jsonUrl?: SingleOrMultiple<string> | number,
index?: number
): Promise<Container | undefined> {
let url: SingleOrMultiple<string>, id: string | undefined;
if (typeof jsonUrl === "number" || jsonUrl === undefined) {
url = tagId;
} else {
id = tagId as string;
url = jsonUrl;
}
return this.loadRemoteOptions({ tagId: id, url, index, remote: true });
}
/**
* Starts an animation in a container, starting from the given options
* @param params all the parameters required for loading options in the current animation
*/
async loadOptions(params: LoaderParams): Promise<Container | undefined> {
const tagId = params.tagId ?? `tsparticles${Math.floor(getRandom() * 10000)}`,
{ index, url: jsonUrl, remote } = params,
options = remote ? await getDataFromUrl(jsonUrl, index) : params.options;
/* elements */
let domContainer = params.element ?? document.getElementById(tagId);
if (!domContainer) {
domContainer = document.createElement("div");
domContainer.id = tagId;
document.querySelector("body")?.append(domContainer);
}
const currentOptions = itemFromSingleOrMultiple(options, index),
dom = this._engine.dom(),
oldIndex = dom.findIndex((v) => v.id === tagId);
if (oldIndex >= 0) {
const old = this._engine.domItem(oldIndex);
if (old && !old.destroyed) {
old.destroy();
dom.splice(oldIndex, 1);
}
}
let canvasEl: HTMLCanvasElement;
if (domContainer.tagName.toLowerCase() === "canvas") {
canvasEl = domContainer as HTMLCanvasElement;
canvasEl.dataset[generatedAttribute] = "false";
} else {
const existingCanvases = domContainer.getElementsByTagName("canvas");
/* get existing canvas if present, otherwise a new one will be created */
if (existingCanvases.length) {
canvasEl = existingCanvases[0];
canvasEl.dataset[generatedAttribute] = "false";
} else {
/* create canvas element */
canvasEl = document.createElement("canvas");
canvasEl.dataset[generatedAttribute] = "true";
/* append canvas */
domContainer.appendChild(canvasEl);
}
}
if (!canvasEl.style.width) {
canvasEl.style.width = "100%";
}
if (!canvasEl.style.height) {
canvasEl.style.height = "100%";
}
/* launch tsParticles */
const newItem = new Container(this._engine, tagId, currentOptions);
if (oldIndex >= 0) {
dom.splice(oldIndex, 0, newItem);
} else {
dom.push(newItem);
}
newItem.canvas.loadCanvas(canvasEl);
await newItem.start();
return newItem;
}
/**
* Starts an animation in a container, starting from the given remote options
* @param params all the parameters required for loading a remote url into options in the current animation
*/
async loadRemoteOptions(params: LoaderParams): Promise<Container | undefined> {
return this.loadOptions(params);
}
/**
* Loads the provided options to create a {@link Container} object.
* @param id the particles container element id
* @param domContainer the dom container
* @param options the options object to initialize the {@link Container}
* @param index if an options array is provided, this will retrieve the exact index of that array
*/
async set(
id: string | HTMLElement,
domContainer: HTMLElement | SingleOrMultiple<RecursivePartial<IOptions>>,
options?: SingleOrMultiple<RecursivePartial<IOptions>> | number,
index?: number
): Promise<Container | undefined> {
const params: LoaderParams = { index, remote: false };
if (typeof id === "string") {
params.tagId = id;
} else {
params.element = id;
}
if (domContainer instanceof HTMLElement) {
params.element = domContainer;
} else {
params.options = domContainer;
}
if (typeof options === "number") {
params.index = options;
} else {
params.options = options ?? params.options;
}
return this.loadOptions(params);
}
/**
* Loads the provided json with a GET request. The content will be used to create a {@link Container} object.
* This method is async, so if you need a callback refer to JavaScript function `fetch`
* @param id the particles container element id
* @param domContainer the container used to contains the particles
* @param jsonUrl the json path (or paths array) to use in the GET request
* @param index the index of the paths array, if a single path is passed this value is ignored
* @returns A Promise with the {@link Container} object created
*/
async setJSON(
id: string | HTMLElement,
domContainer: HTMLElement | SingleOrMultiple<string>,
jsonUrl: SingleOrMultiple<string> | (number | undefined),
index?: number
): Promise<Container | undefined> {
let url: SingleOrMultiple<string>,
newId: string | undefined,
newIndex: number | undefined,
element: HTMLElement;
if (id instanceof HTMLElement) {
element = id;
url = domContainer as SingleOrMultiple<string>;
newIndex = jsonUrl as number;
} else {
newId = id;
element = domContainer as HTMLElement;
url = jsonUrl as SingleOrMultiple<string>;
newIndex = index;
}
return this.loadRemoteOptions({ tagId: newId, url, index: newIndex, element, remote: true });
}
}