2013-10-28 23:22:06 +04: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 20:39:29 +04:00
|
|
|
(function() {
|
|
|
|
if (!OCA.Sharing) {
|
|
|
|
OCA.Sharing = {};
|
|
|
|
}
|
2014-06-24 01:56:10 +04:00
|
|
|
/**
|
|
|
|
* @namespace
|
|
|
|
*/
|
2014-05-28 20:39:29 +04:00
|
|
|
OCA.Sharing.Util = {
|
2014-06-24 01:56:10 +04:00
|
|
|
/**
|
2014-12-01 18:17:28 +03:00
|
|
|
* Initialize the sharing plugin.
|
2014-06-24 01:56:10 +04:00
|
|
|
*
|
|
|
|
* Registers the "Share" file action and adds additional
|
|
|
|
* DOM attributes for the sharing file info.
|
|
|
|
*
|
2014-12-01 18:17:28 +03:00
|
|
|
* @param {OCA.Files.FileList} fileList file list to be extended
|
2014-06-24 01:56:10 +04:00
|
|
|
*/
|
2014-12-01 18:17:28 +03:00
|
|
|
attach: function(fileList) {
|
2015-07-02 15:56:33 +03:00
|
|
|
// core sharing is disabled/not loaded
|
|
|
|
if (!OC.Share) {
|
|
|
|
return;
|
|
|
|
}
|
2015-01-26 19:06:07 +03:00
|
|
|
if (fileList.id === 'trashbin' || fileList.id === 'files.public') {
|
2014-12-01 18:17:28 +03:00
|
|
|
return;
|
2014-06-02 19:23:52 +04:00
|
|
|
}
|
2014-12-01 18:17:28 +03:00
|
|
|
var fileActions = fileList.fileActions;
|
|
|
|
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 23:22:06 +04:00
|
|
|
|
2015-08-14 16:54:27 +03: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 19:23:52 +04:00
|
|
|
// use delegate to catch the case with multiple file lists
|
2014-12-01 18:17:28 +03:00
|
|
|
fileList.$el.on('fileActionsReady', function(ev){
|
2014-06-02 19:23:52 +04:00
|
|
|
var fileList = ev.fileList;
|
2014-07-01 23:32:04 +04:00
|
|
|
var $files = ev.$files;
|
|
|
|
|
|
|
|
function updateIcons($files) {
|
|
|
|
if (!$files) {
|
|
|
|
// if none specified, update all
|
|
|
|
$files = fileList.$fileList.find('tr');
|
|
|
|
}
|
|
|
|
_.each($files, function(file) {
|
|
|
|
OCA.Sharing.Util.updateFileActionIcon($(file));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-06-02 19:23:52 +04:00
|
|
|
if (!OCA.Sharing.sharesLoaded){
|
2014-07-01 23:32:04 +04:00
|
|
|
OC.Share.loadIcons('file', fileList, function() {
|
|
|
|
// since we don't know which files are affected, just refresh them all
|
|
|
|
updateIcons();
|
|
|
|
});
|
2014-06-02 19:23:52 +04:00
|
|
|
// assume that we got all shares, so switching directories
|
|
|
|
// will not invalidate that list
|
|
|
|
OCA.Sharing.sharesLoaded = true;
|
|
|
|
}
|
|
|
|
else{
|
2014-07-01 23:32:04 +04:00
|
|
|
updateIcons($files);
|
2014-06-02 19:23:52 +04:00
|
|
|
}
|
|
|
|
});
|
2014-05-19 17:20:44 +04:00
|
|
|
|
2015-07-16 16:28:45 +03:00
|
|
|
fileActions.registerAction({
|
|
|
|
name: 'Share',
|
2015-08-10 15:13:43 +03:00
|
|
|
displayName: '',
|
2015-07-16 16:28:45 +03:00
|
|
|
mime: 'all',
|
|
|
|
permissions: OC.PERMISSION_SHARE,
|
|
|
|
icon: OC.imagePath('core', 'actions/share'),
|
|
|
|
type: OCA.Files.FileActions.TYPE_INLINE,
|
|
|
|
actionHandler: function(filename, context) {
|
|
|
|
var $tr = context.$file;
|
|
|
|
var itemType = 'file';
|
|
|
|
if ($tr.data('type') === 'dir') {
|
|
|
|
itemType = 'folder';
|
|
|
|
}
|
|
|
|
var possiblePermissions = $tr.data('share-permissions');
|
|
|
|
if (_.isUndefined(possiblePermissions)) {
|
|
|
|
possiblePermissions = $tr.data('permissions');
|
|
|
|
}
|
2014-06-02 19:23:52 +04:00
|
|
|
|
2015-07-16 16:28:45 +03:00
|
|
|
var appendTo = $tr.find('td.filename');
|
|
|
|
// Check if drop down is already visible for a different file
|
|
|
|
if (OC.Share.droppedDown) {
|
|
|
|
if ($tr.attr('data-id') !== $('#dropdown').attr('data-item-source')) {
|
|
|
|
OC.Share.hideDropDown(function () {
|
|
|
|
$tr.addClass('mouseOver');
|
|
|
|
OC.Share.showDropDown(itemType, $tr.data('id'), appendTo, true, possiblePermissions, filename);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
OC.Share.hideDropDown();
|
|
|
|
}
|
2014-05-28 20:39:29 +04:00
|
|
|
} else {
|
2015-07-16 16:28:45 +03:00
|
|
|
$tr.addClass('mouseOver');
|
|
|
|
OC.Share.showDropDown(itemType, $tr.data('id'), appendTo, true, possiblePermissions, filename);
|
2014-06-02 19:23:52 +04:00
|
|
|
}
|
2015-07-16 16:28:45 +03:00
|
|
|
$('#dropdown').on('sharesChanged', function(ev) {
|
|
|
|
// files app current cannot show recipients on load, so we don't update the
|
|
|
|
// icon when changed for consistency
|
|
|
|
if (context.fileList.$el.closest('#app-content-files').length) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var recipients = _.pluck(ev.shares[OC.Share.SHARE_TYPE_USER], 'share_with_displayname');
|
|
|
|
var groupRecipients = _.pluck(ev.shares[OC.Share.SHARE_TYPE_GROUP], 'share_with_displayname');
|
|
|
|
recipients = recipients.concat(groupRecipients);
|
|
|
|
// note: we only update the data attribute because updateIcon()
|
|
|
|
// is called automatically after this event
|
|
|
|
if (recipients.length) {
|
|
|
|
$tr.attr('data-share-recipients', OCA.Sharing.Util.formatRecipients(recipients));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$tr.removeAttr('data-share-recipients');
|
|
|
|
}
|
|
|
|
});
|
2014-06-02 19:23:52 +04:00
|
|
|
}
|
2015-07-16 16:28:45 +03:00
|
|
|
});
|
2015-07-15 17:09:00 +03:00
|
|
|
|
|
|
|
OC.addScript('files_sharing', 'sharetabview').done(function() {
|
|
|
|
fileList.registerTabView(new OCA.Sharing.ShareTabView('shareTabView'));
|
|
|
|
});
|
2014-05-28 20:39:29 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2014-07-01 23:32:04 +04:00
|
|
|
* Update the file action share icon for the given file
|
|
|
|
*
|
|
|
|
* @param $tr file element of the file to update
|
|
|
|
*/
|
|
|
|
updateFileActionIcon: function($tr) {
|
|
|
|
// if the statuses are loaded already, use them for the icon
|
|
|
|
// (needed when scrolling to the next page)
|
|
|
|
var shareStatus = OC.Share.statuses[$tr.data('id')];
|
|
|
|
if (shareStatus || $tr.attr('data-share-recipients') || $tr.attr('data-share-owner')) {
|
|
|
|
var permissions = $tr.data('permissions');
|
|
|
|
var hasLink = !!(shareStatus && shareStatus.link);
|
|
|
|
OC.Share.markFileAsShared($tr, true, hasLink);
|
2015-07-02 17:38:20 +03:00
|
|
|
if ((permissions & OC.PERMISSION_SHARE) === 0 && $tr.attr('data-share-owner')) {
|
2014-07-01 23:32:04 +04:00
|
|
|
// if no share action exists because the admin disabled sharing for this user
|
|
|
|
// we create a share notification action to inform the user about files
|
|
|
|
// shared with him otherwise we just update the existing share action.
|
|
|
|
// TODO: make this work like/with OC.Share.markFileAsShared()
|
|
|
|
$tr.find('.fileactions .action-share-notification').remove();
|
|
|
|
var shareNotification = '<a class="action action-share-notification permanent"' +
|
|
|
|
' data-action="Share-Notification" href="#" original-title="">' +
|
|
|
|
' <img class="svg" src="' + OC.imagePath('core', 'actions/share') + '"></img>';
|
|
|
|
$tr.find('.fileactions').append(function() {
|
2014-07-11 17:16:26 +04:00
|
|
|
var shareBy = escapeHTML($tr.attr('data-share-owner'));
|
2014-07-01 23:32:04 +04:00
|
|
|
var $result = $(shareNotification + '<span> ' + shareBy + '</span></span>');
|
|
|
|
$result.on('click', function() {
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
return $result;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2014-06-02 17:59:06 +04:00
|
|
|
* Formats a recipients array to be displayed.
|
2014-05-28 20:39:29 +04: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-24 01:56:10 +04: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 20:39:29 +04:00
|
|
|
*/
|
|
|
|
formatRecipients: function(recipients, count) {
|
|
|
|
var maxRecipients = 4;
|
|
|
|
var text;
|
|
|
|
if (!_.isNumber(count)) {
|
|
|
|
count = recipients.length;
|
|
|
|
}
|
2014-06-02 17:59:06 +04:00
|
|
|
// TODO: use natural sort
|
|
|
|
recipients = _.first(recipients, maxRecipients).sort();
|
|
|
|
text = recipients.join(', ');
|
2014-05-28 20:39:29 +04:00
|
|
|
if (count > maxRecipients) {
|
|
|
|
text += ', +' + (count - maxRecipients);
|
|
|
|
}
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
})();
|
|
|
|
|
2014-12-01 18:17:28 +03:00
|
|
|
OC.Plugins.register('OCA.Files.FileList', OCA.Sharing.Util);
|
2014-05-28 20:39:29 +04:00
|
|
|
|