Source: js/PluginManager.js

js/PluginManager.js

const PLUGIN_ORDER = [
    'drag',
    'pinch',
    'wheel',
    'follow',
    'mouse-edges',
    'decelerate',
    'animate',
    'bounce',
    'snap-zoom',
    'clamp-zoom',
    'snap',
    'clamp',
];
/**
 * Use this to access current plugins or add user-defined plugins
 *
 * @public
 */
export class PluginManager {
    /** This is called by {@link Viewport} to initialize the {@link Viewport.plugins plugins}. */
    constructor(viewport) {
        this.viewport = viewport;
        this.list = [];
        this.plugins = {};
    }
    /**
     * Inserts a named plugin or a user plugin into the viewport
     * default plugin order: 'drag', 'pinch', 'wheel', 'follow', 'mouse-edges', 'decelerate', 'bounce',
     * 'snap-zoom', 'clamp-zoom', 'snap', 'clamp'
     *
     * @param {string} name of plugin
     * @param {Plugin} plugin - instantiated Plugin class
     * @param {number} index to insert userPlugin (otherwise inserts it at the end)
     */
    add(name, plugin, index = PLUGIN_ORDER.length) {
        const oldPlugin = this.plugins[name];
        if (oldPlugin) {
            oldPlugin.destroy();
        }
        this.plugins[name] = plugin;
        const current = PLUGIN_ORDER.indexOf(name);
        if (current !== -1) {
            PLUGIN_ORDER.splice(current, 1);
        }
        PLUGIN_ORDER.splice(index, 0, name);
        this.sort();
    }
    /**
     * Get plugin
     *
     * @param {string} name of plugin
     * @param {boolean} [ignorePaused] return null if plugin is paused
     */
    get(name, ignorePaused) {
        var _a;
        if (ignorePaused) {
            if ((_a = this.plugins[name]) === null || _a === void 0 ? void 0 : _a.paused) {
                return null;
            }
        }
        return this.plugins[name];
    }
    /**
     * Update all active plugins
     *
     * @internal
     * @ignore
     * @param {number} elapsed type in milliseconds since last update
     */
    update(elapsed) {
        for (const plugin of this.list) {
            plugin.update(elapsed);
        }
    }
    /**
     * Resize all active plugins
     *
     * @internal
     * @ignore
     */
    resize() {
        for (const plugin of this.list) {
            plugin.resize();
        }
    }
    /** Clamps and resets bounce and decelerate (as needed) after manually moving viewport */
    reset() {
        for (const plugin of this.list) {
            plugin.reset();
        }
    }
    /** removes all installed plugins */
    removeAll() {
        this.list.forEach((plugin) => {
            plugin.destroy();
        });
        this.plugins = {};
        this.sort();
    }
    /**
     * Removes installed plugin
     *
     * @param {string} name of plugin (e.g., 'drag', 'pinch')
     */
    remove(name) {
        var _a;
        if (this.plugins[name]) {
            (_a = this.plugins[name]) === null || _a === void 0 ? void 0 : _a.destroy();
            delete this.plugins[name];
            this.viewport.emit(`${name}-remove`);
            this.sort();
        }
    }
    /**
     * Pause plugin
     *
     * @param {string} name of plugin (e.g., 'drag', 'pinch')
     */
    pause(name) {
        var _a;
        (_a = this.plugins[name]) === null || _a === void 0 ? void 0 : _a.pause();
    }
    /**
     * Resume plugin
     *
     * @param {string} name of plugin (e.g., 'drag', 'pinch')
     */
    resume(name) {
        var _a;
        (_a = this.plugins[name]) === null || _a === void 0 ? void 0 : _a.resume();
    }
    /**
     * Sort plugins according to PLUGIN_ORDER
     *
     * @internal
     * @ignore
     */
    sort() {
        this.list = [];
        for (const plugin of PLUGIN_ORDER) {
            if (this.plugins[plugin]) {
                this.list.push(this.plugins[plugin]);
            }
        }
    }
    /**
     * Handle down for all plugins
     *
     * @internal
     * @ignore
     */
    down(event) {
        let stop = false;
        for (const plugin of this.list) {
            if (plugin.down(event)) {
                stop = true;
            }
        }
        return stop;
    }
    /**
     * Handle move for all plugins
     *
     * @internal
     * @ignore
     */
    move(event) {
        let stop = false;
        for (const plugin of this.viewport.plugins.list) {
            if (plugin.move(event)) {
                stop = true;
            }
        }
        return stop;
    }
    /**
     * Handle up for all plugins
     *
     * @internal
     * @ignore
     */
    up(event) {
        let stop = false;
        for (const plugin of this.list) {
            if (plugin.up(event)) {
                stop = true;
            }
        }
        return stop;
    }
    /**
     * Handle wheel event for all plugins
     *
     * @internal
     * @ignore
     */
    wheel(e) {
        let result = false;
        for (const plugin of this.list) {
            if (plugin.wheel(e)) {
                result = true;
            }
        }
        return result;
    }
}