aboutsummaryrefslogtreecommitdiffstats
path: root/core
diff options
context:
space:
mode:
authorVincent Petry <pvince81@owncloud.com>2014-12-01 16:17:28 +0100
committerVincent Petry <pvince81@owncloud.com>2014-12-01 16:20:44 +0100
commitc02ef695217f42dff501e481a50f4669d1cb1f29 (patch)
tree98ef997af355d9fd050645a7ba5c827056c87392 /core
parent8db4dd7585aa9daebb32a3f3305f4061b17c316d (diff)
downloadnextcloud-server-c02ef695217f42dff501e481a50f4669d1cb1f29.tar.gz
nextcloud-server-c02ef695217f42dff501e481a50f4669d1cb1f29.zip
Simple Plugin system for Javascript
Diffstat (limited to 'core')
-rw-r--r--core/js/js.js81
-rw-r--r--core/js/tests/specHelper.js3
-rw-r--r--core/js/tests/specs/coreSpec.js31
3 files changed, 115 insertions, 0 deletions
diff --git a/core/js/js.js b/core/js/js.js
index eb2f10b51f0..cc3a548de28 100644
--- a/core/js/js.js
+++ b/core/js/js.js
@@ -499,6 +499,87 @@ var OC={
};
/**
+ * @namespace OC.Plugins
+ */
+OC.Plugins = {
+ /**
+ * @type Array.<OC.Plugin>
+ */
+ _plugins: {},
+
+ /**
+ * Register plugin
+ *
+ * @param {String} targetName app name / class name to hook into
+ * @param {OC.Plugin} plugin
+ */
+ register: function(targetName, plugin) {
+ var 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: function(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} object to be extended
+ * @param {Object} [options] options
+ */
+ attach: function(targetName, targetObject, options) {
+ var plugins = this.getPlugins(targetName);
+ for (var 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} object to be extended
+ * @param {Object} [options] options
+ */
+ detach: function(targetName, targetObject, options) {
+ var plugins = this.getPlugins(targetName);
+ for (var i = 0; i < plugins.length; i++) {
+ if (plugins[i].detach) {
+ plugins[i].detach(targetObject, options);
+ }
+ }
+ },
+
+ /**
+ * Plugin
+ *
+ * @todo make this a real class in the future
+ * @typedef {Object} OC.Plugin
+ *
+ * @property {String} name plugin name
+ * @property {Function} attach function that will be called when the
+ * plugin is attached
+ * @property {Function} [detach] function that will be called when the
+ * plugin is detached
+ */
+
+};
+
+/**
* @namespace OC.search
*/
OC.search.customResults={};
diff --git a/core/js/tests/specHelper.js b/core/js/tests/specHelper.js
index 4111b6763d9..59c2a99645f 100644
--- a/core/js/tests/specHelper.js
+++ b/core/js/tests/specHelper.js
@@ -120,6 +120,9 @@ window.isPhantom = /phantom/i.test(navigator.userAgent);
if (!OC.TestUtil) {
OC.TestUtil = TestUtil;
}
+
+ // reset plugins
+ OC.Plugins._plugins = [];
});
afterEach(function() {
diff --git a/core/js/tests/specs/coreSpec.js b/core/js/tests/specs/coreSpec.js
index 2c5c22905b0..08395f4d4c2 100644
--- a/core/js/tests/specs/coreSpec.js
+++ b/core/js/tests/specs/coreSpec.js
@@ -655,5 +655,36 @@ describe('Core base tests', function() {
]);
});
});
+ describe('Plugins', function() {
+ var plugin;
+
+ beforeEach(function() {
+ plugin = {
+ name: 'Some name',
+ attach: function(obj) {
+ obj.attached = true;
+ },
+
+ detach: function(obj) {
+ obj.attached = false;
+ }
+ };
+ OC.Plugins.register('OC.Test.SomeName', plugin);
+ });
+ it('attach plugin to object', function() {
+ var obj = {something: true};
+ OC.Plugins.attach('OC.Test.SomeName', obj);
+ expect(obj.attached).toEqual(true);
+ OC.Plugins.detach('OC.Test.SomeName', obj);
+ expect(obj.attached).toEqual(false);
+ });
+ it('only call handler for target name', function() {
+ var obj = {something: true};
+ OC.Plugins.attach('OC.Test.SomeOtherName', obj);
+ expect(obj.attached).not.toBeDefined();
+ OC.Plugins.detach('OC.Test.SomeOtherName', obj);
+ expect(obj.attached).not.toBeDefined();
+ });
+ });
});