UNPKG

5.92 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.Loader = void 0;
4const Container_1 = require("./Container");
5const Utils_1 = require("../Utils");
6const tsParticlesDom = [];
7function fetchError(statusCode) {
8 console.error(`Error tsParticles - fetch status: ${statusCode}`);
9 console.error("Error tsParticles - File config not found");
10}
11class Loader {
12 static dom() {
13 return tsParticlesDom;
14 }
15 static domItem(index) {
16 const dom = Loader.dom();
17 const item = dom[index];
18 if (item && !item.destroyed) {
19 return item;
20 }
21 dom.splice(index, 1);
22 }
23 static async loadOptions(params) {
24 var _a, _b, _c;
25 const tagId = (_a = params.tagId) !== null && _a !== void 0 ? _a : `tsparticles${Math.floor(Math.random() * 10000)}`;
26 const { options, index } = params;
27 let domContainer = (_b = params.element) !== null && _b !== void 0 ? _b : document.getElementById(tagId);
28 if (!domContainer) {
29 domContainer = document.createElement("div");
30 domContainer.id = tagId;
31 (_c = document.querySelector("body")) === null || _c === void 0 ? void 0 : _c.append(domContainer);
32 }
33 const currentOptions = options instanceof Array ? (0, Utils_1.itemFromArray)(options, index) : options;
34 const dom = Loader.dom();
35 const oldIndex = dom.findIndex((v) => v.id === tagId);
36 if (oldIndex >= 0) {
37 const old = Loader.domItem(oldIndex);
38 if (old && !old.destroyed) {
39 old.destroy();
40 dom.splice(oldIndex, 1);
41 }
42 }
43 let canvasEl;
44 let generatedCanvas;
45 if (domContainer.tagName.toLowerCase() === "canvas") {
46 canvasEl = domContainer;
47 generatedCanvas = false;
48 }
49 else {
50 const existingCanvases = domContainer.getElementsByTagName("canvas");
51 if (existingCanvases.length) {
52 canvasEl = existingCanvases[0];
53 if (!canvasEl.className) {
54 canvasEl.className = Utils_1.Constants.canvasClass;
55 }
56 generatedCanvas = false;
57 }
58 else {
59 generatedCanvas = true;
60 canvasEl = document.createElement("canvas");
61 canvasEl.className = Utils_1.Constants.canvasClass;
62 canvasEl.style.width = "100%";
63 canvasEl.style.height = "100%";
64 domContainer.appendChild(canvasEl);
65 }
66 }
67 const newItem = new Container_1.Container(tagId, currentOptions);
68 if (oldIndex >= 0) {
69 dom.splice(oldIndex, 0, newItem);
70 }
71 else {
72 dom.push(newItem);
73 }
74 newItem.canvas.loadCanvas(canvasEl, generatedCanvas);
75 await newItem.start();
76 return newItem;
77 }
78 static async loadRemoteOptions(params) {
79 const { url: jsonUrl, index } = params;
80 const url = jsonUrl instanceof Array ? (0, Utils_1.itemFromArray)(jsonUrl, index) : jsonUrl;
81 if (!url) {
82 return;
83 }
84 const response = await fetch(url);
85 if (!response.ok) {
86 fetchError(response.status);
87 return;
88 }
89 const data = await response.json();
90 return await Loader.loadOptions({
91 tagId: params.tagId,
92 element: params.element,
93 index,
94 options: data,
95 });
96 }
97 static load(tagId, options, index) {
98 const params = { index };
99 if (typeof tagId === "string") {
100 params.tagId = tagId;
101 }
102 else {
103 params.options = tagId;
104 }
105 if (typeof options === "number") {
106 params.index = options !== null && options !== void 0 ? options : params.index;
107 }
108 else {
109 params.options = options !== null && options !== void 0 ? options : params.options;
110 }
111 return this.loadOptions(params);
112 }
113 static async set(id, domContainer, options, index) {
114 const params = { index };
115 if (typeof id === "string") {
116 params.tagId = id;
117 }
118 else {
119 params.element = id;
120 }
121 if (domContainer instanceof HTMLElement) {
122 params.element = domContainer;
123 }
124 else {
125 params.options = domContainer;
126 }
127 if (typeof options === "number") {
128 params.index = options;
129 }
130 else {
131 params.options = options !== null && options !== void 0 ? options : params.options;
132 }
133 return this.loadOptions(params);
134 }
135 static async loadJSON(tagId, jsonUrl, index) {
136 let url, id;
137 if (typeof jsonUrl === "number" || jsonUrl === undefined) {
138 url = tagId;
139 }
140 else {
141 id = tagId;
142 url = jsonUrl;
143 }
144 return await Loader.loadRemoteOptions({ tagId: id, url, index });
145 }
146 static async setJSON(id, domContainer, jsonUrl, index) {
147 let url, newId, newIndex, element;
148 if (id instanceof HTMLElement) {
149 element = id;
150 url = domContainer;
151 newIndex = jsonUrl;
152 }
153 else {
154 newId = id;
155 element = domContainer;
156 url = jsonUrl;
157 newIndex = index;
158 }
159 return await Loader.loadRemoteOptions({ tagId: newId, url, index: newIndex, element });
160 }
161 static setOnClickHandler(callback) {
162 const dom = Loader.dom();
163 if (dom.length === 0) {
164 throw new Error("Can only set click handlers after calling tsParticles.load() or tsParticles.loadJSON()");
165 }
166 for (const domItem of dom) {
167 domItem.addClickHandler(callback);
168 }
169 }
170}
171exports.Loader = Loader;