diff options
author | Vincent Petry <pvince81@owncloud.com> | 2014-07-09 12:26:33 +0200 |
---|---|---|
committer | Vincent Petry <pvince81@owncloud.com> | 2014-07-09 12:26:33 +0200 |
commit | 22653e21a239afa8f29cf2c93afe28d21246e7ba (patch) | |
tree | cacee44aabacafe6ce35ce82a4e62afa7cdaab1e /apps/files | |
parent | 63fdaacbfcc8dbccca6777d623b6baadbd0ed56b (diff) | |
download | nextcloud-server-22653e21a239afa8f29cf2c93afe28d21246e7ba.tar.gz nextcloud-server-22653e21a239afa8f29cf2c93afe28d21246e7ba.zip |
Propagate file action changes to the file lists
Whenever an app needs to register an event late, it does that on the
original file actions object.
Since the file actions that the file list work on is a merged list, not
the original one, the registration event needs to be propagated there as
well.
Diffstat (limited to 'apps/files')
-rw-r--r-- | apps/files/js/app.js | 34 | ||||
-rw-r--r-- | apps/files/js/fileactions.js | 83 | ||||
-rw-r--r-- | apps/files/js/filelist.js | 6 | ||||
-rw-r--r-- | apps/files/tests/js/appSpec.js | 4 | ||||
-rw-r--r-- | apps/files/tests/js/fileactionsSpec.js | 6 |
5 files changed, 96 insertions, 37 deletions
diff --git a/apps/files/js/app.js b/apps/files/js/app.js index 45b6b6a0e16..6f5206fcdb6 100644 --- a/apps/files/js/app.js +++ b/apps/files/js/app.js @@ -32,9 +32,11 @@ // regular actions fileActions.merge(OCA.Files.fileActions); - // in case apps would decide to register file actions later, - // replace the global object with this one - OCA.Files.fileActions = fileActions; + this._onActionsUpdated = _.bind(this._onActionsUpdated, this); + OCA.Files.fileActions.on('setDefault.app-files', this._onActionsUpdated); + OCA.Files.fileActions.on('registerAction.app-files', this._onActionsUpdated); + window.FileActions.on('setDefault.app-files', this._onActionsUpdated); + window.FileActions.on('registerAction.app-files', this._onActionsUpdated); this.files = OCA.Files.Files; @@ -60,6 +62,32 @@ }, /** + * Destroy the app + */ + destroy: function() { + this.navigation = null; + this.fileList.destroy(); + this.fileList = null; + this.files = null; + OCA.Files.fileActions.off('setDefault.app-files', this._onActionsUpdated); + OCA.Files.fileActions.off('registerAction.app-files', this._onActionsUpdated); + window.FileActions.off('setDefault.app-files', this._onActionsUpdated); + window.FileActions.off('registerAction.app-files', this._onActionsUpdated); + }, + + _onActionsUpdated: function(ev, newAction) { + // forward new action to the file list + if (ev.action) { + this.fileList.fileActions.registerAction(ev.action); + } else if (ev.defaultAction) { + this.fileList.fileActions.setDefault( + ev.defaultAction.mime, + ev.defaultAction.name + ); + } + }, + + /** * Returns the container of the currently visible app. * * @return app container diff --git a/apps/files/js/fileactions.js b/apps/files/js/fileactions.js index cbfd047e98f..fd038765ea5 100644 --- a/apps/files/js/fileactions.js +++ b/apps/files/js/fileactions.js @@ -24,47 +24,51 @@ currentFile: null, /** + * Dummy jquery element, for events + */ + $el: null, + + /** * List of handlers to be notified whenever a register() or * setDefault() was called. */ - _updateListeners: [], + _updateListeners: {}, initialize: function() { this.clear(); + // abusing jquery for events until we get a real event lib + this.$el = $('<div class="dummy-fileactions hidden"></div>'); + $('body').append(this.$el); }, /** - * Adds an update listener to be notified whenever register() - * or setDefault() has been called. + * Adds an event handler * + * @param {String} eventName event name * @param Function callback */ - addUpdateListener: function(callback) { - if (!_.isFunction(callback)) { - throw 'Argument passed to FileActions.addUpdateListener must be a function'; - } - this._updateListeners.push(callback); + on: function(eventName, callback) { + this.$el.on(eventName, callback); }, /** - * Removes an update listener. + * Removes an event handler * + * @param {String} eventName event name * @param Function callback */ - removeUpdateListener: function(callback) { - if (!_.isFunction(callback)) { - throw 'Argument passed to FileActions.removeUpdateListener must be a function'; - } - this._updateListeners = _.without(this._updateListeners, callback); + off: function(eventName, callback) { + this.$el.off(eventName, callback); }, /** - * Notifies the registered update listeners + * Notifies the event handlers + * + * @param {String} eventName event name + * @param {Object} data data */ - _notifyUpdateListeners: function() { - for (var i = 0; i < this._updateListeners.length; i++) { - this._updateListeners[i](this); - } + _notifyUpdateListeners: function(eventName, data) { + this.$el.trigger(new $.Event(eventName, data)); }, /** @@ -87,17 +91,44 @@ this.defaults = _.extend(this.defaults, fileActions.defaults); this.icons = _.extend(this.icons, fileActions.icons); }, - register: function (mime, name, permissions, icon, action, displayName) { + /** + * @deprecated use #registerAction() instead + */ + register: function(mime, name, permissions, icon, action, displayName) { + return this.registerAction({ + name: name, + mime: mime, + permissions: permissions, + icon: icon, + actionHandler: action, + displayName: displayName + }); + }, + /** + * Register action + * + * @param {Object} action action object + * @param {String} action.name identifier of the action + * @param {String} action.displayName display name of the action, defaults + * to the name given in action.name + * @param {String} action.mime mime type + * @param {int} action.permissions permissions + * @param {(Function|String)} action.icon icon + * @param {Function} action.actionHandler function that performs the action + */ + registerAction: function (action) { + var mime = action.mime; + var name = action.name; if (!this.actions[mime]) { this.actions[mime] = {}; } this.actions[mime][name] = { - action: action, - permissions: permissions, - displayName: displayName || t('files', name) + action: action.actionHandler, + permissions: action.permissions, + displayName: action.displayName || t('files', name) }; - this.icons[name] = icon; - this._notifyUpdateListeners(); + this.icons[name] = action.icon; + this._notifyUpdateListeners('registerAction', {action: action}); }, clear: function() { this.actions = {}; @@ -108,7 +139,7 @@ }, setDefault: function (mime, name) { this.defaults[mime] = name; - this._notifyUpdateListeners(); + this._notifyUpdateListeners('setDefault', {defaultAction: {mime: mime, name: name}}); }, get: function (mime, type, permissions) { var actions = this.getActions(mime, type, permissions); diff --git a/apps/files/js/filelist.js b/apps/files/js/filelist.js index 3e311655c91..96436d33a2c 100644 --- a/apps/files/js/filelist.js +++ b/apps/files/js/filelist.js @@ -172,7 +172,8 @@ */ destroy: function() { // TODO: also unregister other event handlers - this.fileActions.removeUpdateListener(this._onFileActionsUpdated); + this.fileActions.off('registerAction', this._onFileActionsUpdated); + this.fileActions.off('setDefault', this._onFileActionsUpdated); }, _initFileActions: function(fileActions) { @@ -182,7 +183,8 @@ this.fileActions.registerDefaultActions(); } this._onFileActionsUpdated = _.debounce(_.bind(this._onFileActionsUpdated, this), 100); - this.fileActions.addUpdateListener(this._onFileActionsUpdated); + this.fileActions.on('registerAction', this._onFileActionsUpdated); + this.fileActions.on('setDefault', this._onFileActionsUpdated); }, /** diff --git a/apps/files/tests/js/appSpec.js b/apps/files/tests/js/appSpec.js index 99a73d0b01d..77d36d59fa9 100644 --- a/apps/files/tests/js/appSpec.js +++ b/apps/files/tests/js/appSpec.js @@ -52,9 +52,7 @@ describe('OCA.Files.App tests', function() { App.initialize(); }); afterEach(function() { - App.navigation = null; - App.fileList = null; - App.files = null; + App.destroy(); pushStateStub.restore(); parseUrlQueryStub.restore(); diff --git a/apps/files/tests/js/fileactionsSpec.js b/apps/files/tests/js/fileactionsSpec.js index f087239de9d..f5f18a45a75 100644 --- a/apps/files/tests/js/fileactionsSpec.js +++ b/apps/files/tests/js/fileactionsSpec.js @@ -354,7 +354,7 @@ describe('OCA.Files.FileActions tests', function() { it('notifies update event handlers once after multiple changes', function() { var actionStub = sinon.stub(); var handler = sinon.stub(); - FileActions.addUpdateListener(handler); + FileActions.on('registerAction', handler); FileActions.register( 'all', 'Test', @@ -374,8 +374,8 @@ describe('OCA.Files.FileActions tests', function() { it('does not notifies update event handlers after unregistering', function() { var actionStub = sinon.stub(); var handler = sinon.stub(); - FileActions.addUpdateListener(handler); - FileActions.removeUpdateListener(handler); + FileActions.on('registerAction', handler); + FileActions.off('registerAction', handler); FileActions.register( 'all', 'Test', |