share.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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 can't 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. // create permissions don't mean anything for files
  48. sharePermissions = sharePermissions & ~OC.PERMISSION_CREATE;
  49. }
  50. tr.attr('data-share-permissions', sharePermissions);
  51. if (fileData.shareOwner) {
  52. tr.attr('data-share-owner', fileData.shareOwner);
  53. // user should always be able to rename a mount point
  54. if (fileData.mountType === 'shared-root') {
  55. tr.attr('data-permissions', fileData.permissions | OC.PERMISSION_UPDATE);
  56. }
  57. }
  58. if (fileData.recipientsDisplayName) {
  59. tr.attr('data-share-recipients', fileData.recipientsDisplayName);
  60. }
  61. if (fileData.shareTypes) {
  62. tr.attr('data-share-types', fileData.shareTypes.join(','));
  63. }
  64. return tr;
  65. };
  66. var oldElementToFile = fileList.elementToFile;
  67. fileList.elementToFile = function($el) {
  68. var fileInfo = oldElementToFile.apply(this, arguments);
  69. fileInfo.sharePermissions = $el.attr('data-share-permissions') || undefined;
  70. fileInfo.shareOwner = $el.attr('data-share-owner') || undefined;
  71. return fileInfo;
  72. };
  73. var NS_OC = 'http://owncloud.org/ns';
  74. var oldGetWebdavProperties = fileList._getWebdavProperties;
  75. fileList._getWebdavProperties = function() {
  76. var props = oldGetWebdavProperties.apply(this, arguments);
  77. props.push('{' + NS_OC + '}owner-display-name');
  78. props.push('{' + NS_OC + '}share-types');
  79. return props;
  80. };
  81. fileList.filesClient.addFileInfoParser(function(response) {
  82. var data = {};
  83. var props = response.propStat[0].properties;
  84. var permissionsProp = props['{' + NS_OC + '}permissions'];
  85. if (permissionsProp && permissionsProp.indexOf('S') >= 0) {
  86. data.shareOwner = props['{' + NS_OC + '}owner-display-name'];
  87. }
  88. var shareTypesProp = props['{' + NS_OC + '}share-types'];
  89. if (shareTypesProp) {
  90. data.shareTypes = _.chain(shareTypesProp).filter(function(xmlvalue) {
  91. return (xmlvalue.namespaceURI === NS_OC && xmlvalue.nodeName.split(':')[1] === 'share-type');
  92. }).map(function(xmlvalue) {
  93. return parseInt(xmlvalue.textContent || xmlvalue.text, 10);
  94. }).value();
  95. }
  96. return data;
  97. });
  98. // use delegate to catch the case with multiple file lists
  99. fileList.$el.on('fileActionsReady', function(ev){
  100. var $files = ev.$files;
  101. _.each($files, function(file) {
  102. var $tr = $(file);
  103. var shareTypes = $tr.attr('data-share-types') || '';
  104. var shareOwner = $tr.attr('data-share-owner');
  105. if (shareTypes || shareOwner) {
  106. var hasLink = false;
  107. var hasShares = false;
  108. _.each(shareTypes.split(',') || [], function(shareType) {
  109. shareType = parseInt(shareType, 10);
  110. if (shareType === OC.Share.SHARE_TYPE_LINK) {
  111. hasLink = true;
  112. } else if (shareType === OC.Share.SHARE_TYPE_USER) {
  113. hasShares = true;
  114. } else if (shareType === OC.Share.SHARE_TYPE_GROUP) {
  115. hasShares = true;
  116. } else if (shareType === OC.Share.SHARE_TYPE_REMOTE) {
  117. hasShares = true;
  118. }
  119. });
  120. OCA.Sharing.Util._updateFileActionIcon($tr, hasShares, hasLink);
  121. }
  122. });
  123. });
  124. fileList.$el.on('changeDirectory', function() {
  125. OCA.Sharing.sharesLoaded = false;
  126. });
  127. fileActions.registerAction({
  128. name: 'Share',
  129. displayName: '',
  130. mime: 'all',
  131. permissions: OC.PERMISSION_ALL,
  132. iconClass: 'icon-share',
  133. type: OCA.Files.FileActions.TYPE_INLINE,
  134. actionHandler: function(fileName) {
  135. fileList.showDetailsView(fileName, 'shareTabView');
  136. },
  137. render: function(actionSpec, isDefault, context) {
  138. var permissions = parseInt(context.$file.attr('data-permissions'), 10);
  139. // if no share permissions but share owner exists, still show the link
  140. if ((permissions & OC.PERMISSION_SHARE) !== 0 || context.$file.attr('data-share-owner')) {
  141. return fileActions._defaultRenderAction.call(fileActions, actionSpec, isDefault, context);
  142. }
  143. // don't render anything
  144. return null;
  145. }
  146. });
  147. var shareTab = new OCA.Sharing.ShareTabView('shareTabView', {order: -20});
  148. // detect changes and change the matching list entry
  149. shareTab.on('sharesChanged', function(shareModel) {
  150. var fileInfoModel = shareModel.fileInfoModel;
  151. var $tr = fileList.findFileEl(fileInfoModel.get('name'));
  152. OCA.Sharing.Util._updateFileListDataAttributes(fileList, $tr, shareModel);
  153. if (!OCA.Sharing.Util._updateFileActionIcon($tr, shareModel.hasUserShares(), shareModel.hasLinkShare())) {
  154. // remove icon, if applicable
  155. OC.Share.markFileAsShared($tr, false, false);
  156. }
  157. var newIcon = $tr.attr('data-icon');
  158. // in case markFileAsShared decided to change the icon,
  159. // we need to modify the model
  160. // (FIXME: yes, this is hacky)
  161. if (fileInfoModel.get('icon') !== newIcon) {
  162. fileInfoModel.set('icon', newIcon);
  163. }
  164. });
  165. fileList.registerTabView(shareTab);
  166. },
  167. /**
  168. * Update file list data attributes
  169. */
  170. _updateFileListDataAttributes: function(fileList, $tr, shareModel) {
  171. // files app current cannot show recipients on load, so we don't update the
  172. // icon when changed for consistency
  173. if (fileList.id === 'files') {
  174. return;
  175. }
  176. var recipients = _.pluck(shareModel.get('shares'), 'share_with_displayname');
  177. // note: we only update the data attribute because updateIcon()
  178. if (recipients.length) {
  179. $tr.attr('data-share-recipients', OCA.Sharing.Util.formatRecipients(recipients));
  180. }
  181. else {
  182. $tr.removeAttr('data-share-recipients');
  183. }
  184. },
  185. /**
  186. * Update the file action share icon for the given file
  187. *
  188. * @param $tr file element of the file to update
  189. * @param {bool} hasUserShares true if a user share exists
  190. * @param {bool} hasLinkShare true if a link share exists
  191. *
  192. * @return {bool} true if the icon was set, false otherwise
  193. */
  194. _updateFileActionIcon: function($tr, hasUserShares, hasLinkShare) {
  195. // if the statuses are loaded already, use them for the icon
  196. // (needed when scrolling to the next page)
  197. if (hasUserShares || hasLinkShare || $tr.attr('data-share-recipients') || $tr.attr('data-share-owner')) {
  198. OC.Share.markFileAsShared($tr, true, hasLinkShare);
  199. return true;
  200. }
  201. return false;
  202. },
  203. /**
  204. * Formats a recipients array to be displayed.
  205. * The first four recipients will be shown and the
  206. * other ones will be shown as "+x" where "x" is the number of
  207. * remaining recipients.
  208. *
  209. * @param {Array.<String>} recipients recipients array
  210. * @param {int} count optional total recipients count (in case the array was shortened)
  211. * @return {String} formatted recipients display text
  212. */
  213. formatRecipients: function(recipients, count) {
  214. var maxRecipients = 4;
  215. var text;
  216. if (!_.isNumber(count)) {
  217. count = recipients.length;
  218. }
  219. // TODO: use natural sort
  220. recipients = _.first(recipients, maxRecipients).sort();
  221. text = recipients.join(', ');
  222. if (count > maxRecipients) {
  223. text += ', +' + (count - maxRecipients);
  224. }
  225. return text;
  226. }
  227. };
  228. })();
  229. OC.Plugins.register('OCA.Files.FileList', OCA.Sharing.Util);