share.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*
  2. * Copyright (c) 2014
  3. *
  4. * This file is licensed under the Affero General Public License version 3
  5. * or later.
  6. *
  7. * See the COPYING-README file.
  8. *
  9. */
  10. (function() {
  11. if (!OCA.Sharing) {
  12. OCA.Sharing = {};
  13. }
  14. /**
  15. * @namespace
  16. */
  17. OCA.Sharing.Util = {
  18. /**
  19. * Initialize the sharing plugin.
  20. *
  21. * Registers the "Share" file action and adds additional
  22. * DOM attributes for the sharing file info.
  23. *
  24. * @param {OCA.Files.FileList} fileList file list to be extended
  25. */
  26. attach: function(fileList) {
  27. // core sharing is disabled/not loaded
  28. if (!OC.Share) {
  29. return;
  30. }
  31. if (fileList.id === 'trashbin' || fileList.id === 'files.public') {
  32. return;
  33. }
  34. var fileActions = fileList.fileActions;
  35. var oldCreateRow = fileList._createRow;
  36. fileList._createRow = function(fileData) {
  37. var tr = oldCreateRow.apply(this, arguments);
  38. var sharePermissions = fileData.permissions;
  39. if (fileData.mountType && fileData.mountType === "external-root"){
  40. // for external storages we cant use the permissions of the mountpoint
  41. // instead we show all permissions and only use the share permissions from the mountpoint to handle resharing
  42. sharePermissions = sharePermissions | (OC.PERMISSION_ALL & ~OC.PERMISSION_SHARE);
  43. }
  44. if (fileData.type === 'file') {
  45. // files can't be shared with delete permissions
  46. sharePermissions = sharePermissions & ~OC.PERMISSION_DELETE;
  47. }
  48. tr.attr('data-share-permissions', sharePermissions);
  49. if (fileData.shareOwner) {
  50. tr.attr('data-share-owner', fileData.shareOwner);
  51. // user should always be able to rename a mount point
  52. if (fileData.isShareMountPoint) {
  53. tr.attr('data-permissions', fileData.permissions | OC.PERMISSION_UPDATE);
  54. }
  55. }
  56. if (fileData.recipientsDisplayName) {
  57. tr.attr('data-share-recipients', fileData.recipientsDisplayName);
  58. }
  59. return tr;
  60. };
  61. var oldElementToFile = fileList.elementToFile;
  62. fileList.elementToFile = function($el) {
  63. var fileInfo = oldElementToFile.apply(this, arguments);
  64. fileInfo.sharePermissions = $el.attr('data-share-permissions') || undefined;
  65. fileInfo.shareOwner = $el.attr('data-share-owner') || undefined;
  66. return fileInfo;
  67. };
  68. // use delegate to catch the case with multiple file lists
  69. fileList.$el.on('fileActionsReady', function(ev){
  70. var fileList = ev.fileList;
  71. var $files = ev.$files;
  72. function updateIcons($files) {
  73. if (!$files) {
  74. // if none specified, update all
  75. $files = fileList.$fileList.find('tr');
  76. }
  77. _.each($files, function(file) {
  78. OCA.Sharing.Util.updateFileActionIcon($(file));
  79. });
  80. }
  81. if (!OCA.Sharing.sharesLoaded){
  82. OC.Share.loadIcons('file', fileList, function() {
  83. // since we don't know which files are affected, just refresh them all
  84. updateIcons();
  85. });
  86. // assume that we got all shares, so switching directories
  87. // will not invalidate that list
  88. OCA.Sharing.sharesLoaded = true;
  89. }
  90. else{
  91. updateIcons($files);
  92. }
  93. });
  94. fileActions.registerAction({
  95. name: 'Share',
  96. displayName: '',
  97. mime: 'all',
  98. permissions: OC.PERMISSION_SHARE,
  99. icon: OC.imagePath('core', 'actions/share'),
  100. type: OCA.Files.FileActions.TYPE_INLINE,
  101. actionHandler: function(filename, context) {
  102. var $tr = context.$file;
  103. var itemType = 'file';
  104. if ($tr.data('type') === 'dir') {
  105. itemType = 'folder';
  106. }
  107. var possiblePermissions = $tr.data('share-permissions');
  108. if (_.isUndefined(possiblePermissions)) {
  109. possiblePermissions = $tr.data('permissions');
  110. }
  111. var appendTo = $tr.find('td.filename');
  112. // Check if drop down is already visible for a different file
  113. if (OC.Share.droppedDown) {
  114. if ($tr.attr('data-id') !== $('#dropdown').attr('data-item-source')) {
  115. OC.Share.hideDropDown(function () {
  116. $tr.addClass('mouseOver');
  117. OC.Share.showDropDown(itemType, $tr.data('id'), appendTo, true, possiblePermissions, filename);
  118. });
  119. } else {
  120. OC.Share.hideDropDown();
  121. }
  122. } else {
  123. $tr.addClass('mouseOver');
  124. OC.Share.showDropDown(itemType, $tr.data('id'), appendTo, true, possiblePermissions, filename);
  125. }
  126. $('#dropdown').on('sharesChanged', function(ev) {
  127. // files app current cannot show recipients on load, so we don't update the
  128. // icon when changed for consistency
  129. if (context.fileList.$el.closest('#app-content-files').length) {
  130. return;
  131. }
  132. var recipients = _.pluck(ev.shares[OC.Share.SHARE_TYPE_USER], 'share_with_displayname');
  133. var groupRecipients = _.pluck(ev.shares[OC.Share.SHARE_TYPE_GROUP], 'share_with_displayname');
  134. recipients = recipients.concat(groupRecipients);
  135. // note: we only update the data attribute because updateIcon()
  136. // is called automatically after this event
  137. if (recipients.length) {
  138. $tr.attr('data-share-recipients', OCA.Sharing.Util.formatRecipients(recipients));
  139. }
  140. else {
  141. $tr.removeAttr('data-share-recipients');
  142. }
  143. });
  144. }
  145. });
  146. OC.addScript('files_sharing', 'sharetabview').done(function() {
  147. fileList.registerTabView(new OCA.Sharing.ShareTabView('shareTabView'));
  148. });
  149. },
  150. /**
  151. * Update the file action share icon for the given file
  152. *
  153. * @param $tr file element of the file to update
  154. */
  155. updateFileActionIcon: function($tr) {
  156. // if the statuses are loaded already, use them for the icon
  157. // (needed when scrolling to the next page)
  158. var shareStatus = OC.Share.statuses[$tr.data('id')];
  159. if (shareStatus || $tr.attr('data-share-recipients') || $tr.attr('data-share-owner')) {
  160. var permissions = $tr.data('permissions');
  161. var hasLink = !!(shareStatus && shareStatus.link);
  162. OC.Share.markFileAsShared($tr, true, hasLink);
  163. if ((permissions & OC.PERMISSION_SHARE) === 0 && $tr.attr('data-share-owner')) {
  164. // if no share action exists because the admin disabled sharing for this user
  165. // we create a share notification action to inform the user about files
  166. // shared with him otherwise we just update the existing share action.
  167. // TODO: make this work like/with OC.Share.markFileAsShared()
  168. $tr.find('.fileactions .action-share-notification').remove();
  169. var shareNotification = '<a class="action action-share-notification permanent"' +
  170. ' data-action="Share-Notification" href="#" original-title="">' +
  171. ' <img class="svg" src="' + OC.imagePath('core', 'actions/share') + '"></img>';
  172. $tr.find('.fileactions').append(function() {
  173. var shareBy = escapeHTML($tr.attr('data-share-owner'));
  174. var $result = $(shareNotification + '<span> ' + shareBy + '</span></span>');
  175. $result.on('click', function() {
  176. return false;
  177. });
  178. return $result;
  179. });
  180. }
  181. }
  182. },
  183. /**
  184. * Formats a recipients array to be displayed.
  185. * The first four recipients will be shown and the
  186. * other ones will be shown as "+x" where "x" is the number of
  187. * remaining recipients.
  188. *
  189. * @param {Array.<String>} recipients recipients array
  190. * @param {int} count optional total recipients count (in case the array was shortened)
  191. * @return {String} formatted recipients display text
  192. */
  193. formatRecipients: function(recipients, count) {
  194. var maxRecipients = 4;
  195. var text;
  196. if (!_.isNumber(count)) {
  197. count = recipients.length;
  198. }
  199. // TODO: use natural sort
  200. recipients = _.first(recipients, maxRecipients).sort();
  201. text = recipients.join(', ');
  202. if (count > maxRecipients) {
  203. text += ', +' + (count - maxRecipients);
  204. }
  205. return text;
  206. }
  207. };
  208. })();
  209. OC.Plugins.register('OCA.Files.FileList', OCA.Sharing.Util);