2013-10-28 19:22:06 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2014
|
|
|
|
*
|
|
|
|
* This file is licensed under the Affero General Public License version 3
|
|
|
|
* or later.
|
|
|
|
*
|
|
|
|
* See the COPYING-README file.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2014-05-28 16:39:29 +00:00
|
|
|
(function() {
|
|
|
|
if (!OCA.Sharing) {
|
|
|
|
OCA.Sharing = {};
|
|
|
|
}
|
2014-06-23 21:56:10 +00:00
|
|
|
/**
|
|
|
|
* @namespace
|
|
|
|
*/
|
2014-05-28 16:39:29 +00:00
|
|
|
OCA.Sharing.Util = {
|
2014-06-23 21:56:10 +00:00
|
|
|
/**
|
2014-12-01 15:17:28 +00:00
|
|
|
* Initialize the sharing plugin.
|
2014-06-23 21:56:10 +00:00
|
|
|
*
|
|
|
|
* Registers the "Share" file action and adds additional
|
|
|
|
* DOM attributes for the sharing file info.
|
|
|
|
*
|
2014-12-01 15:17:28 +00:00
|
|
|
* @param {OCA.Files.FileList} fileList file list to be extended
|
2014-06-23 21:56:10 +00:00
|
|
|
*/
|
2014-12-01 15:17:28 +00:00
|
|
|
attach: function(fileList) {
|
2015-07-02 12:56:33 +00:00
|
|
|
// core sharing is disabled/not loaded
|
|
|
|
if (!OC.Share) {
|
|
|
|
return;
|
|
|
|
}
|
2015-01-26 16:06:07 +00:00
|
|
|
if (fileList.id === 'trashbin' || fileList.id === 'files.public') {
|
2014-12-01 15:17:28 +00:00
|
|
|
return;
|
2014-06-02 15:23:52 +00:00
|
|
|
}
|
2015-09-14 15:20:51 +00:00
|
|
|
var fileActions = fileList.fileActions;
|
2014-12-01 15:17:28 +00:00
|
|
|
var oldCreateRow = fileList._createRow;
|
|
|
|
fileList._createRow = function(fileData) {
|
|
|
|
var tr = oldCreateRow.apply(this, arguments);
|
|
|
|
var sharePermissions = fileData.permissions;
|
|
|
|
if (fileData.mountType && fileData.mountType === "external-root"){
|
|
|
|
// for external storages we cant use the permissions of the mountpoint
|
|
|
|
// instead we show all permissions and only use the share permissions from the mountpoint to handle resharing
|
|
|
|
sharePermissions = sharePermissions | (OC.PERMISSION_ALL & ~OC.PERMISSION_SHARE);
|
|
|
|
}
|
|
|
|
if (fileData.type === 'file') {
|
|
|
|
// files can't be shared with delete permissions
|
|
|
|
sharePermissions = sharePermissions & ~OC.PERMISSION_DELETE;
|
|
|
|
}
|
|
|
|
tr.attr('data-share-permissions', sharePermissions);
|
|
|
|
if (fileData.shareOwner) {
|
|
|
|
tr.attr('data-share-owner', fileData.shareOwner);
|
|
|
|
// user should always be able to rename a mount point
|
|
|
|
if (fileData.isShareMountPoint) {
|
|
|
|
tr.attr('data-permissions', fileData.permissions | OC.PERMISSION_UPDATE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (fileData.recipientsDisplayName) {
|
|
|
|
tr.attr('data-share-recipients', fileData.recipientsDisplayName);
|
|
|
|
}
|
|
|
|
return tr;
|
|
|
|
};
|
2013-10-28 19:22:06 +00:00
|
|
|
|
2015-08-14 13:54:27 +00:00
|
|
|
var oldElementToFile = fileList.elementToFile;
|
|
|
|
fileList.elementToFile = function($el) {
|
|
|
|
var fileInfo = oldElementToFile.apply(this, arguments);
|
|
|
|
fileInfo.sharePermissions = $el.attr('data-share-permissions') || undefined;
|
|
|
|
fileInfo.shareOwner = $el.attr('data-share-owner') || undefined;
|
|
|
|
return fileInfo;
|
|
|
|
};
|
|
|
|
|
2014-06-02 15:23:52 +00:00
|
|
|
// use delegate to catch the case with multiple file lists
|
2014-12-01 15:17:28 +00:00
|
|
|
fileList.$el.on('fileActionsReady', function(ev){
|
2014-06-02 15:23:52 +00:00
|
|
|
var fileList = ev.fileList;
|
2014-07-01 19:32:04 +00:00
|
|
|
var $files = ev.$files;
|
|
|
|
|
|
|
|
function updateIcons($files) {
|
|
|
|
if (!$files) {
|
|
|
|
// if none specified, update all
|
|
|
|
$files = fileList.$fileList.find('tr');
|
|
|
|
}
|
|
|
|
_.each($files, function(file) {
|
2015-09-14 15:20:51 +00:00
|
|
|
var $tr = $(file);
|
|
|
|
var shareStatus = OC.Share.statuses[$tr.data('id')];
|
|
|
|
OCA.Sharing.Util._updateFileActionIcon($tr, !!shareStatus, shareStatus && shareStatus.link);
|
2014-07-01 19:32:04 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-06-02 15:23:52 +00:00
|
|
|
if (!OCA.Sharing.sharesLoaded){
|
2014-07-01 19:32:04 +00:00
|
|
|
OC.Share.loadIcons('file', fileList, function() {
|
|
|
|
// since we don't know which files are affected, just refresh them all
|
|
|
|
updateIcons();
|
|
|
|
});
|
2014-06-02 15:23:52 +00:00
|
|
|
// assume that we got all shares, so switching directories
|
|
|
|
// will not invalidate that list
|
|
|
|
OCA.Sharing.sharesLoaded = true;
|
|
|
|
}
|
|
|
|
else{
|
2014-07-01 19:32:04 +00:00
|
|
|
updateIcons($files);
|
2014-06-02 15:23:52 +00:00
|
|
|
}
|
|
|
|
});
|
2014-05-19 13:20:44 +00:00
|
|
|
|
2015-09-14 15:20:51 +00:00
|
|
|
fileActions.registerAction({
|
|
|
|
name: 'Share',
|
|
|
|
displayName: '',
|
|
|
|
mime: 'all',
|
2015-09-28 14:23:33 +00:00
|
|
|
permissions: OC.PERMISSION_ALL,
|
2015-09-14 15:20:51 +00:00
|
|
|
icon: OC.imagePath('core', 'actions/share'),
|
|
|
|
type: OCA.Files.FileActions.TYPE_INLINE,
|
|
|
|
actionHandler: function(fileName) {
|
|
|
|
fileList.showDetailsView(fileName, 'shareTabView');
|
2015-09-28 14:23:33 +00:00
|
|
|
},
|
|
|
|
render: function(actionSpec, isDefault, context) {
|
|
|
|
var permissions = parseInt(context.$file.attr('data-permissions'), 10);
|
|
|
|
// if no share permissions but share owner exists, still show the link
|
|
|
|
if ((permissions & OC.PERMISSION_SHARE) !== 0 || context.$file.attr('data-share-owner')) {
|
|
|
|
return fileActions._defaultRenderAction.call(fileActions, actionSpec, isDefault, context);
|
|
|
|
}
|
|
|
|
// don't render anything
|
|
|
|
return null;
|
2015-09-14 15:20:51 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-09-28 10:30:12 +00:00
|
|
|
var shareTab = new OCA.Sharing.ShareTabView('shareTabView', {order: -20});
|
2015-09-14 15:20:51 +00:00
|
|
|
// detect changes and change the matching list entry
|
|
|
|
shareTab.on('sharesChanged', function(shareModel) {
|
|
|
|
var fileInfoModel = shareModel.fileInfoModel;
|
|
|
|
var $tr = fileList.findFileEl(fileInfoModel.get('name'));
|
|
|
|
OCA.Sharing.Util._updateFileListDataAttributes(fileList, $tr, shareModel);
|
|
|
|
if (!OCA.Sharing.Util._updateFileActionIcon($tr, shareModel.hasUserShares(), shareModel.hasLinkShare())) {
|
|
|
|
// remove icon, if applicable
|
|
|
|
OC.Share.markFileAsShared($tr, false, false);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
fileList.registerTabView(shareTab);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update file list data attributes
|
|
|
|
*/
|
|
|
|
_updateFileListDataAttributes: function(fileList, $tr, shareModel) {
|
|
|
|
// files app current cannot show recipients on load, so we don't update the
|
|
|
|
// icon when changed for consistency
|
|
|
|
if (fileList.id === 'files') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var recipients = _.pluck(shareModel.get('shares'), 'share_with_displayname');
|
|
|
|
// note: we only update the data attribute because updateIcon()
|
|
|
|
if (recipients.length) {
|
|
|
|
$tr.attr('data-share-recipients', OCA.Sharing.Util.formatRecipients(recipients));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$tr.removeAttr('data-share-recipients');
|
|
|
|
}
|
2014-05-28 16:39:29 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2014-07-01 19:32:04 +00:00
|
|
|
* Update the file action share icon for the given file
|
|
|
|
*
|
|
|
|
* @param $tr file element of the file to update
|
2015-09-14 15:20:51 +00:00
|
|
|
* @param {bool} hasUserShares true if a user share exists
|
|
|
|
* @param {bool} hasLinkShare true if a link share exists
|
|
|
|
*
|
|
|
|
* @return {bool} true if the icon was set, false otherwise
|
2014-07-01 19:32:04 +00:00
|
|
|
*/
|
2015-09-14 15:20:51 +00:00
|
|
|
_updateFileActionIcon: function($tr, hasUserShares, hasLinkShare) {
|
2014-07-01 19:32:04 +00:00
|
|
|
// if the statuses are loaded already, use them for the icon
|
|
|
|
// (needed when scrolling to the next page)
|
2015-09-14 15:20:51 +00:00
|
|
|
if (hasUserShares || hasLinkShare || $tr.attr('data-share-recipients') || $tr.attr('data-share-owner')) {
|
|
|
|
OC.Share.markFileAsShared($tr, true, hasLinkShare);
|
|
|
|
return true;
|
2014-07-01 19:32:04 +00:00
|
|
|
}
|
2015-09-14 15:20:51 +00:00
|
|
|
return false;
|
2014-07-01 19:32:04 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2014-06-02 13:59:06 +00:00
|
|
|
* Formats a recipients array to be displayed.
|
2014-05-28 16:39:29 +00:00
|
|
|
* The first four recipients will be shown and the
|
|
|
|
* other ones will be shown as "+x" where "x" is the number of
|
|
|
|
* remaining recipients.
|
|
|
|
*
|
2014-06-23 21:56:10 +00:00
|
|
|
* @param {Array.<String>} recipients recipients array
|
|
|
|
* @param {int} count optional total recipients count (in case the array was shortened)
|
|
|
|
* @return {String} formatted recipients display text
|
2014-05-28 16:39:29 +00:00
|
|
|
*/
|
|
|
|
formatRecipients: function(recipients, count) {
|
|
|
|
var maxRecipients = 4;
|
|
|
|
var text;
|
|
|
|
if (!_.isNumber(count)) {
|
|
|
|
count = recipients.length;
|
|
|
|
}
|
2014-06-02 13:59:06 +00:00
|
|
|
// TODO: use natural sort
|
|
|
|
recipients = _.first(recipients, maxRecipients).sort();
|
|
|
|
text = recipients.join(', ');
|
2014-05-28 16:39:29 +00:00
|
|
|
if (count > maxRecipients) {
|
|
|
|
text += ', +' + (count - maxRecipients);
|
|
|
|
}
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
})();
|
|
|
|
|
2014-12-01 15:17:28 +00:00
|
|
|
OC.Plugins.register('OCA.Files.FileList', OCA.Sharing.Util);
|
2014-05-28 16:39:29 +00:00
|
|
|
|