From a8f1902b02d02cc2b7d624b3d7b288f8aea84fdc Mon Sep 17 00:00:00 2001 From: Daniel Calviño Sánchez Date: Wed, 27 Sep 2017 01:50:24 +0200 Subject: Add support to FileActionsMenu for icon class functions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Icon class function properties make possible to render a different icon class depending on the context of the file action. Inline file actions had support for them already and called them passing the file name and context of the file action as parameters. Due to this the FileActionsMenu passes those parameters too to icon class functions instead of just the context like done for display name functions. Signed-off-by: Daniel Calviño Sánchez --- apps/files/js/fileactions.js | 13 ++++++++++++- apps/files/js/fileactionsmenu.js | 5 +++++ apps/files/tests/js/fileactionsmenuSpec.js | 28 ++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 1 deletion(-) (limited to 'apps') diff --git a/apps/files/js/fileactions.js b/apps/files/js/fileactions.js index 3da9b06b0d3..0f320c8b3c7 100644 --- a/apps/files/js/fileactions.js +++ b/apps/files/js/fileactions.js @@ -706,7 +706,7 @@ * @property {String} mime mime type * @property {int} permissions permissions * @property {(Function|String)} icon icon path to the icon or function that returns it (deprecated, use iconClass instead) - * @property {(Function|String)} iconClass class name of the icon (recommended for theming) + * @property {(String|OCA.Files.FileActions~iconClassFunction)} iconClass class name of the icon (recommended for theming) * @property {OCA.Files.FileActions~renderActionFunction} [render] optional rendering function * @property {OCA.Files.FileActions~actionHandler} actionHandler action handler function */ @@ -745,6 +745,17 @@ * @return {String} display name */ + /** + * Icon class function for actions. + * The function returns the icon class of the action using + * the given context information. + * + * @callback OCA.Files.FileActions~iconClassFunction + * @param {String} fileName name of the file on which the action must be performed + * @param {OCA.Files.FileActionContext} context action context + * @return {String} icon class + */ + /** * Action handler function for file actions * diff --git a/apps/files/js/fileactionsmenu.js b/apps/files/js/fileactionsmenu.js index 45d2bd83049..b8022f13734 100644 --- a/apps/files/js/fileactionsmenu.js +++ b/apps/files/js/fileactionsmenu.js @@ -115,6 +115,11 @@ item = _.extend({}, item); item.displayName = item.displayName(self._context); } + if (_.isFunction(item.iconClass)) { + var fileName = self._context.$file.attr('data-file'); + item = _.extend({}, item); + item.iconClass = item.iconClass(fileName, self._context); + } return item; }); items = items.sort(function(actionA, actionB) { diff --git a/apps/files/tests/js/fileactionsmenuSpec.js b/apps/files/tests/js/fileactionsmenuSpec.js index 3028db2b3ac..926516b3043 100644 --- a/apps/files/tests/js/fileactionsmenuSpec.js +++ b/apps/files/tests/js/fileactionsmenuSpec.js @@ -205,6 +205,34 @@ describe('OCA.Files.FileActionsMenu tests', function() { expect(displayNameStub.calledWith(menuContext)).toEqual(true); expect(menu.$el.find('a[data-action=Something]').text()).toEqual('Test'); }); + it('uses plain iconClass', function() { + fileActions.registerAction({ + name: 'Something', + mime: 'text/plain', + permissions: OC.PERMISSION_ALL, + iconClass: 'test' + }); + + menu.render(); + + expect(menu.$el.find('a[data-action=Something]').children('span.icon').hasClass('test')).toEqual(true); + }); + it('calls iconClass function', function() { + var iconClassStub = sinon.stub().returns('test'); + + fileActions.registerAction({ + name: 'Something', + mime: 'text/plain', + permissions: OC.PERMISSION_ALL, + iconClass: iconClassStub + }); + + menu.render(); + + expect(iconClassStub.calledOnce).toEqual(true); + expect(iconClassStub.calledWith(menuContext.$file.attr('data-file'), menuContext)).toEqual(true); + expect(menu.$el.find('a[data-action=Something]').children('span.icon').hasClass('test')).toEqual(true); + }); }); describe('action handler', function() { -- cgit v1.2.3