UNPKG

4.89 kBJavaScriptView Raw
1import { Container } from "./Container";
2import { generatedAttribute } from "./Utils/Constants";
3import { getRandom } from "../Utils/NumberUtils";
4import { itemFromSingleOrMultiple } from "../Utils/Utils";
5async function getDataFromUrl(jsonUrl, index) {
6 const url = itemFromSingleOrMultiple(jsonUrl, index);
7 if (!url) {
8 return;
9 }
10 const response = await fetch(url);
11 if (response.ok) {
12 return response.json();
13 }
14 console.error(`tsParticles - Error ${response.status} while retrieving config file`);
15}
16export class Loader {
17 constructor(engine) {
18 this._engine = engine;
19 }
20 load(tagId, options, index) {
21 const params = { index, remote: false };
22 if (typeof tagId === "string") {
23 params.tagId = tagId;
24 }
25 else {
26 params.options = tagId;
27 }
28 if (typeof options === "number") {
29 params.index = options;
30 }
31 else {
32 params.options = options !== null && options !== void 0 ? options : params.options;
33 }
34 return this.loadOptions(params);
35 }
36 async loadJSON(tagId, jsonUrl, index) {
37 let url, id;
38 if (typeof jsonUrl === "number" || jsonUrl === undefined) {
39 url = tagId;
40 }
41 else {
42 id = tagId;
43 url = jsonUrl;
44 }
45 return this.loadRemoteOptions({ tagId: id, url, index, remote: true });
46 }
47 async loadOptions(params) {
48 var _a, _b, _c;
49 const tagId = (_a = params.tagId) !== null && _a !== void 0 ? _a : `tsparticles${Math.floor(getRandom() * 10000)}`, { index, url: jsonUrl, remote } = params, options = remote ? await getDataFromUrl(jsonUrl, index) : params.options;
50 let domContainer = (_b = params.element) !== null && _b !== void 0 ? _b : document.getElementById(tagId);
51 if (!domContainer) {
52 domContainer = document.createElement("div");
53 domContainer.id = tagId;
54 (_c = document.querySelector("body")) === null || _c === void 0 ? void 0 : _c.append(domContainer);
55 }
56 const currentOptions = itemFromSingleOrMultiple(options, index), dom = this._engine.dom(), oldIndex = dom.findIndex((v) => v.id === tagId);
57 if (oldIndex >= 0) {
58 const old = this._engine.domItem(oldIndex);
59 if (old && !old.destroyed) {
60 old.destroy();
61 dom.splice(oldIndex, 1);
62 }
63 }
64 let canvasEl;
65 if (domContainer.tagName.toLowerCase() === "canvas") {
66 canvasEl = domContainer;
67 canvasEl.dataset[generatedAttribute] = "false";
68 }
69 else {
70 const existingCanvases = domContainer.getElementsByTagName("canvas");
71 if (existingCanvases.length) {
72 canvasEl = existingCanvases[0];
73 canvasEl.dataset[generatedAttribute] = "false";
74 }
75 else {
76 canvasEl = document.createElement("canvas");
77 canvasEl.dataset[generatedAttribute] = "true";
78 domContainer.appendChild(canvasEl);
79 }
80 }
81 if (!canvasEl.style.width) {
82 canvasEl.style.width = "100%";
83 }
84 if (!canvasEl.style.height) {
85 canvasEl.style.height = "100%";
86 }
87 const newItem = new Container(this._engine, tagId, currentOptions);
88 if (oldIndex >= 0) {
89 dom.splice(oldIndex, 0, newItem);
90 }
91 else {
92 dom.push(newItem);
93 }
94 newItem.canvas.loadCanvas(canvasEl);
95 await newItem.start();
96 return newItem;
97 }
98 async loadRemoteOptions(params) {
99 return this.loadOptions(params);
100 }
101 async set(id, domContainer, options, index) {
102 const params = { index, remote: false };
103 if (typeof id === "string") {
104 params.tagId = id;
105 }
106 else {
107 params.element = id;
108 }
109 if (domContainer instanceof HTMLElement) {
110 params.element = domContainer;
111 }
112 else {
113 params.options = domContainer;
114 }
115 if (typeof options === "number") {
116 params.index = options;
117 }
118 else {
119 params.options = options !== null && options !== void 0 ? options : params.options;
120 }
121 return this.loadOptions(params);
122 }
123 async setJSON(id, domContainer, jsonUrl, index) {
124 let url, newId, newIndex, element;
125 if (id instanceof HTMLElement) {
126 element = id;
127 url = domContainer;
128 newIndex = jsonUrl;
129 }
130 else {
131 newId = id;
132 element = domContainer;
133 url = jsonUrl;
134 newIndex = index;
135 }
136 return this.loadRemoteOptions({ tagId: newId, url, index: newIndex, element, remote: true });
137 }
138}