summaryrefslogtreecommitdiffstats
path: root/core/src/OC/plugins.js
blob: 8212fc0b4eeac4bd343e1905e5de2f8cb27f3629 (plain)
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
/**
 * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */

export default {

	/**
	 * @type {Array.<OC.Plugin>}
	 */
	_plugins: {},

	/**
	 * Register plugin
	 *
	 * @param {string} targetName app name / class name to hook into
	 * @param {OC.Plugin} plugin plugin
	 */
	register(targetName, plugin) {
		let plugins = this._plugins[targetName]
		if (!plugins) {
			plugins = this._plugins[targetName] = []
		}
		plugins.push(plugin)
	},

	/**
	 * Returns all plugin registered to the given target
	 * name / app name / class name.
	 *
	 * @param {string} targetName app name / class name to hook into
	 * @return {Array.<OC.Plugin>} array of plugins
	 */
	getPlugins(targetName) {
		return this._plugins[targetName] || []
	},

	/**
	 * Call attach() on all plugins registered to the given target name.
	 *
	 * @param {string} targetName app name / class name
	 * @param {object} targetObject to be extended
	 * @param {object} [options] options
	 */
	attach(targetName, targetObject, options) {
		const plugins = this.getPlugins(targetName)
		for (let i = 0; i < plugins.length; i++) {
			if (plugins[i].attach) {
				plugins[i].attach(targetObject, options)
			}
		}
	},

	/**
	 * Call detach() on all plugins registered to the given target name.
	 *
	 * @param {string} targetName app name / class name
	 * @param {object} targetObject to be extended
	 * @param {object} [options] options
	 */
	detach(targetName, targetObject, options) {
		const plugins = this.getPlugins(targetName)
		for (let i = 0; i < plugins.length; i++) {
			if (plugins[i].detach) {
				plugins[i].detach(targetObject, options)
			}
		}
	},

}