2015-07-16 13:28:45 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2014
|
|
|
|
*
|
|
|
|
* This file is licensed under the Affero General Public License version 3
|
|
|
|
* or later.
|
|
|
|
*
|
|
|
|
* See the COPYING-README file.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
(function() {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Construct a new FileActionsMenu instance
|
|
|
|
* @constructs FileActionsMenu
|
|
|
|
* @memberof OCA.Files
|
|
|
|
*/
|
2015-08-11 09:35:46 +00:00
|
|
|
var FileActionsMenu = OC.Backbone.View.extend({
|
|
|
|
tagName: 'div',
|
2015-08-27 11:22:58 +00:00
|
|
|
className: 'fileActionsMenu popovermenu bubble hidden open menu',
|
2015-07-16 13:28:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Current context
|
|
|
|
*
|
|
|
|
* @type OCA.Files.FileActionContext
|
|
|
|
*/
|
|
|
|
_context: null,
|
|
|
|
|
2015-08-11 09:35:46 +00:00
|
|
|
events: {
|
|
|
|
'click a.action': '_onClickAction'
|
2015-07-16 13:28:45 +00:00
|
|
|
},
|
|
|
|
|
2015-08-11 09:35:46 +00:00
|
|
|
template: function(data) {
|
2018-10-15 13:51:40 +00:00
|
|
|
return OCA.Files.Templates['fileactionsmenu'](data);
|
2015-08-05 10:48:42 +00:00
|
|
|
},
|
|
|
|
|
2015-07-16 13:28:45 +00:00
|
|
|
/**
|
|
|
|
* Event handler whenever an action has been clicked within the menu
|
|
|
|
*
|
|
|
|
* @param {Object} event event object
|
|
|
|
*/
|
|
|
|
_onClickAction: function(event) {
|
|
|
|
var $target = $(event.target);
|
|
|
|
if (!$target.is('a')) {
|
|
|
|
$target = $target.closest('a');
|
|
|
|
}
|
|
|
|
var fileActions = this._context.fileActions;
|
|
|
|
var actionName = $target.attr('data-action');
|
|
|
|
var actions = fileActions.getActions(
|
|
|
|
fileActions.getCurrentMimeType(),
|
|
|
|
fileActions.getCurrentType(),
|
|
|
|
fileActions.getCurrentPermissions()
|
|
|
|
);
|
|
|
|
var actionSpec = actions[actionName];
|
|
|
|
var fileName = this._context.$file.attr('data-file');
|
|
|
|
|
|
|
|
event.stopPropagation();
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
OC.hideMenus();
|
|
|
|
|
|
|
|
actionSpec.action(
|
|
|
|
fileName,
|
|
|
|
this._context
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders the menu with the currently set items
|
|
|
|
*/
|
|
|
|
render: function() {
|
2015-12-11 14:14:30 +00:00
|
|
|
var self = this;
|
2015-07-16 13:28:45 +00:00
|
|
|
var fileActions = this._context.fileActions;
|
|
|
|
var actions = fileActions.getActions(
|
|
|
|
fileActions.getCurrentMimeType(),
|
|
|
|
fileActions.getCurrentType(),
|
|
|
|
fileActions.getCurrentPermissions()
|
|
|
|
);
|
|
|
|
|
|
|
|
var defaultAction = fileActions.getDefaultFileAction(
|
|
|
|
fileActions.getCurrentMimeType(),
|
|
|
|
fileActions.getCurrentType(),
|
|
|
|
fileActions.getCurrentPermissions()
|
|
|
|
);
|
|
|
|
|
|
|
|
var items = _.filter(actions, function(actionSpec) {
|
2019-03-14 17:01:44 +00:00
|
|
|
return !defaultAction || actionSpec.name !== defaultAction.name;
|
2015-07-16 13:28:45 +00:00
|
|
|
});
|
2015-12-11 14:14:30 +00:00
|
|
|
items = _.map(items, function(item) {
|
|
|
|
if (_.isFunction(item.displayName)) {
|
|
|
|
item = _.extend({}, item);
|
|
|
|
item.displayName = item.displayName(self._context);
|
|
|
|
}
|
2017-09-26 23:50:24 +00:00
|
|
|
if (_.isFunction(item.iconClass)) {
|
|
|
|
var fileName = self._context.$file.attr('data-file');
|
|
|
|
item = _.extend({}, item);
|
|
|
|
item.iconClass = item.iconClass(fileName, self._context);
|
|
|
|
}
|
2019-03-14 17:01:44 +00:00
|
|
|
if (_.isFunction(item.icon)) {
|
|
|
|
var fileName = self._context.$file.attr('data-file');
|
|
|
|
item = _.extend({}, item);
|
|
|
|
item.icon = item.icon(fileName, self._context);
|
|
|
|
}
|
|
|
|
item.inline = item.type === OCA.Files.FileActions.TYPE_INLINE
|
2015-12-11 14:14:30 +00:00
|
|
|
return item;
|
|
|
|
});
|
2015-09-28 09:19:49 +00:00
|
|
|
items = items.sort(function(actionA, actionB) {
|
|
|
|
var orderA = actionA.order || 0;
|
|
|
|
var orderB = actionB.order || 0;
|
|
|
|
if (orderB === orderA) {
|
|
|
|
return OC.Util.naturalSortCompare(actionA.displayName, actionB.displayName);
|
|
|
|
}
|
|
|
|
return orderA - orderB;
|
|
|
|
});
|
2019-03-14 17:01:44 +00:00
|
|
|
|
2015-07-16 13:28:45 +00:00
|
|
|
items = _.map(items, function(item) {
|
|
|
|
item.nameLowerCase = item.name.toLowerCase();
|
|
|
|
return item;
|
|
|
|
});
|
|
|
|
|
2015-08-11 09:35:46 +00:00
|
|
|
this.$el.html(this.template({
|
2015-07-16 13:28:45 +00:00
|
|
|
items: items
|
|
|
|
}));
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Displays the menu under the given element
|
|
|
|
*
|
|
|
|
* @param {OCA.Files.FileActionContext} context context
|
2015-08-11 09:35:46 +00:00
|
|
|
* @param {Object} $trigger trigger element
|
2015-07-16 13:28:45 +00:00
|
|
|
*/
|
2015-08-11 09:35:46 +00:00
|
|
|
show: function(context) {
|
2015-07-16 13:28:45 +00:00
|
|
|
this._context = context;
|
|
|
|
|
|
|
|
this.render();
|
|
|
|
this.$el.removeClass('hidden');
|
|
|
|
|
|
|
|
OC.showMenu(null, this.$el);
|
|
|
|
}
|
2015-08-11 09:35:46 +00:00
|
|
|
});
|
2015-07-16 13:28:45 +00:00
|
|
|
|
|
|
|
OCA.Files.FileActionsMenu = FileActionsMenu;
|
|
|
|
|
|
|
|
})();
|
|
|
|
|