UNPKG

4.31 kBJavaScriptView Raw
1(function (factory) {
2 if (typeof module === "object" && typeof module.exports === "object") {
3 var v = factory(require, exports);
4 if (v !== undefined) module.exports = v;
5 }
6 else if (typeof define === "function" && define.amd) {
7 define(["require", "exports", "../../Utils/Utils"], factory);
8 }
9})(function (require, exports) {
10 "use strict";
11 Object.defineProperty(exports, "__esModule", { value: true });
12 exports.Plugins = void 0;
13 const Utils_1 = require("../../Utils/Utils");
14 function getItemsFromInitializer(container, map, initializers, force = false) {
15 let res = map.get(container);
16 if (!res || force) {
17 res = [...initializers.values()].map((t) => t(container));
18 map.set(container, res);
19 }
20 return res;
21 }
22 class Plugins {
23 constructor(engine) {
24 this._engine = engine;
25 this.plugins = [];
26 this._initializers = {
27 interactors: new Map(),
28 movers: new Map(),
29 updaters: new Map(),
30 };
31 this.interactors = new Map();
32 this.movers = new Map();
33 this.updaters = new Map();
34 this.presets = new Map();
35 this.drawers = new Map();
36 this.pathGenerators = new Map();
37 }
38 addInteractor(name, initInteractor) {
39 this._initializers.interactors.set(name, initInteractor);
40 }
41 addParticleMover(name, initMover) {
42 this._initializers.movers.set(name, initMover);
43 }
44 addParticleUpdater(name, initUpdater) {
45 this._initializers.updaters.set(name, initUpdater);
46 }
47 addPathGenerator(type, pathGenerator) {
48 !this.getPathGenerator(type) && this.pathGenerators.set(type, pathGenerator);
49 }
50 addPlugin(plugin) {
51 !this.getPlugin(plugin.id) && this.plugins.push(plugin);
52 }
53 addPreset(presetKey, options, override = false) {
54 (override || !this.getPreset(presetKey)) && this.presets.set(presetKey, options);
55 }
56 addShapeDrawer(types, drawer) {
57 (0, Utils_1.executeOnSingleOrMultiple)(types, (type) => {
58 !this.getShapeDrawer(type) && this.drawers.set(type, drawer);
59 });
60 }
61 destroy(container) {
62 this.updaters.delete(container);
63 this.movers.delete(container);
64 this.interactors.delete(container);
65 }
66 getAvailablePlugins(container) {
67 const res = new Map();
68 for (const plugin of this.plugins) {
69 plugin.needsPlugin(container.actualOptions) && res.set(plugin.id, plugin.getPlugin(container));
70 }
71 return res;
72 }
73 getInteractors(container, force = false) {
74 return getItemsFromInitializer(container, this.interactors, this._initializers.interactors, force);
75 }
76 getMovers(container, force = false) {
77 return getItemsFromInitializer(container, this.movers, this._initializers.movers, force);
78 }
79 getPathGenerator(type) {
80 return this.pathGenerators.get(type);
81 }
82 getPlugin(plugin) {
83 return this.plugins.find((t) => t.id === plugin);
84 }
85 getPreset(preset) {
86 return this.presets.get(preset);
87 }
88 getShapeDrawer(type) {
89 return this.drawers.get(type);
90 }
91 getSupportedShapes() {
92 return this.drawers.keys();
93 }
94 getUpdaters(container, force = false) {
95 return getItemsFromInitializer(container, this.updaters, this._initializers.updaters, force);
96 }
97 loadOptions(options, sourceOptions) {
98 for (const plugin of this.plugins) {
99 plugin.loadOptions(options, sourceOptions);
100 }
101 }
102 loadParticlesOptions(container, options, ...sourceOptions) {
103 const updaters = this.updaters.get(container);
104 if (!updaters) {
105 return;
106 }
107 for (const updater of updaters) {
108 updater.loadOptions && updater.loadOptions(options, ...sourceOptions);
109 }
110 }
111 }
112 exports.Plugins = Plugins;
113});