share.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. _.extend(OC.Files.Client, {
  12. PROPERTY_SHARE_TYPES: '{' + OC.Files.Client.NS_OWNCLOUD + '}share-types',
  13. PROPERTY_OWNER_DISPLAY_NAME: '{' + OC.Files.Client.NS_OWNCLOUD + '}owner-display-name'
  14. });
  15. if (!OCA.Sharing) {
  16. OCA.Sharing = {};
  17. }
  18. /**
  19. * @namespace
  20. */
  21. OCA.Sharing.Util = {
  22. /**
  23. * Initialize the sharing plugin.
  24. *
  25. * Registers the "Share" file action and adds additional
  26. * DOM attributes for the sharing file info.
  27. *
  28. * @param {OCA.Files.FileList} fileList file list to be extended
  29. */
  30. attach: function(fileList) {
  31. // core sharing is disabled/not loaded
  32. if (!OC.Share) {
  33. return;
  34. }
  35. if (fileList.id === 'trashbin' || fileList.id === 'files.public') {
  36. return;
  37. }
  38. var fileActions = fileList.fileActions;
  39. var oldCreateRow = fileList._createRow;
  40. fileList._createRow = function(fileData) {
  41. var tr = oldCreateRow.apply(this, arguments);
  42. var sharePermissions = OCA.Sharing.Util.getSharePermissions(fileData);
  43. tr.attr('data-share-permissions', sharePermissions);
  44. if (fileData.shareOwner) {
  45. tr.attr('data-share-owner', fileData.shareOwner);
  46. // user should always be able to rename a mount point
  47. if (fileData.mountType === 'shared-root') {
  48. tr.attr('data-permissions', fileData.permissions | OC.PERMISSION_UPDATE);
  49. }
  50. }
  51. if (fileData.recipientsDisplayName) {
  52. tr.attr('data-share-recipients', fileData.recipientsDisplayName);
  53. }
  54. if (fileData.shareTypes) {
  55. tr.attr('data-share-types', fileData.shareTypes.join(','));
  56. }
  57. return tr;
  58. };
  59. var oldElementToFile = fileList.elementToFile;
  60. fileList.elementToFile = function($el) {
  61. var fileInfo = oldElementToFile.apply(this, arguments);
  62. fileInfo.sharePermissions = $el.attr('data-share-permissions') || undefined;
  63. fileInfo.shareOwner = $el.attr('data-share-owner') || undefined;
  64. if( $el.attr('data-share-types')){
  65. var shareTypes = $el.attr('data-share-types').split(',');
  66. fileInfo.shareTypes = shareTypes;
  67. }
  68. if( $el.attr('data-expiration')){
  69. var expirationTimestamp = parseInt($el.attr('data-expiration'));
  70. fileInfo.shares = [];
  71. fileInfo.shares.push({expiration: expirationTimestamp});
  72. }
  73. fileInfo.recipientsDisplayName = $el.attr('data-share-recipients') || undefined;
  74. return fileInfo;
  75. };
  76. var oldGetWebdavProperties = fileList._getWebdavProperties;
  77. fileList._getWebdavProperties = function() {
  78. var props = oldGetWebdavProperties.apply(this, arguments);
  79. props.push(OC.Files.Client.PROPERTY_OWNER_DISPLAY_NAME);
  80. props.push(OC.Files.Client.PROPERTY_SHARE_TYPES);
  81. return props;
  82. };
  83. fileList.filesClient.addFileInfoParser(function(response) {
  84. var data = {};
  85. var props = response.propStat[0].properties;
  86. var permissionsProp = props[OC.Files.Client.PROPERTY_PERMISSIONS];
  87. if (permissionsProp && permissionsProp.indexOf('S') >= 0) {
  88. data.shareOwner = props[OC.Files.Client.PROPERTY_OWNER_DISPLAY_NAME];
  89. }
  90. var shareTypesProp = props[OC.Files.Client.PROPERTY_SHARE_TYPES];
  91. if (shareTypesProp) {
  92. data.shareTypes = _.chain(shareTypesProp).filter(function(xmlvalue) {
  93. return (xmlvalue.namespaceURI === OC.Files.Client.NS_OWNCLOUD && xmlvalue.nodeName.split(':')[1] === 'share-type');
  94. }).map(function(xmlvalue) {
  95. return parseInt(xmlvalue.textContent || xmlvalue.text, 10);
  96. }).value();
  97. }
  98. return data;
  99. });
  100. // use delegate to catch the case with multiple file lists
  101. fileList.$el.on('fileActionsReady', function(ev){
  102. var $files = ev.$files;
  103. _.each($files, function(file) {
  104. var $tr = $(file);
  105. var shareTypes = $tr.attr('data-share-types') || '';
  106. var shareOwner = $tr.attr('data-share-owner');
  107. if (shareTypes || shareOwner) {
  108. var hasLink = false;
  109. var hasShares = false;
  110. _.each(shareTypes.split(',') || [], function(shareType) {
  111. shareType = parseInt(shareType, 10);
  112. if (shareType === OC.Share.SHARE_TYPE_LINK) {
  113. hasLink = true;
  114. } else if (shareType === OC.Share.SHARE_TYPE_EMAIL) {
  115. hasLink = true;
  116. } else if (shareType === OC.Share.SHARE_TYPE_USER) {
  117. hasShares = true;
  118. } else if (shareType === OC.Share.SHARE_TYPE_GROUP) {
  119. hasShares = true;
  120. } else if (shareType === OC.Share.SHARE_TYPE_REMOTE) {
  121. hasShares = true;
  122. } else if (shareType === OC.Share.SHARE_TYPE_CIRCLE) {
  123. hasShares = true;
  124. }
  125. });
  126. OCA.Sharing.Util._updateFileActionIcon($tr, hasShares, hasLink);
  127. }
  128. });
  129. });
  130. fileList.$el.on('changeDirectory', function() {
  131. OCA.Sharing.sharesLoaded = false;
  132. });
  133. fileActions.registerAction({
  134. name: 'Share',
  135. displayName: '',
  136. altText: t('core', 'Share'),
  137. mime: 'all',
  138. permissions: OC.PERMISSION_ALL,
  139. iconClass: 'icon-share',
  140. type: OCA.Files.FileActions.TYPE_INLINE,
  141. actionHandler: function(fileName) {
  142. fileList.showDetailsView(fileName, 'shareTabView');
  143. },
  144. render: function(actionSpec, isDefault, context) {
  145. var permissions = parseInt(context.$file.attr('data-permissions'), 10);
  146. // if no share permissions but share owner exists, still show the link
  147. if ((permissions & OC.PERMISSION_SHARE) !== 0 || context.$file.attr('data-share-owner')) {
  148. return fileActions._defaultRenderAction.call(fileActions, actionSpec, isDefault, context);
  149. }
  150. // don't render anything
  151. return null;
  152. }
  153. });
  154. var shareTab = new OCA.Sharing.ShareTabView('shareTabView', {order: -20});
  155. // detect changes and change the matching list entry
  156. shareTab.on('sharesChanged', function(shareModel) {
  157. var fileInfoModel = shareModel.fileInfoModel;
  158. var $tr = fileList.findFileEl(fileInfoModel.get('name'));
  159. // We count email shares as link share
  160. var hasLinkShare = shareModel.hasLinkShare();
  161. shareModel.get('shares').forEach(function (share) {
  162. if (share.share_type === OC.Share.SHARE_TYPE_EMAIL) {
  163. hasLinkShare = true;
  164. }
  165. });
  166. OCA.Sharing.Util._updateFileListDataAttributes(fileList, $tr, shareModel);
  167. if (!OCA.Sharing.Util._updateFileActionIcon($tr, shareModel.hasUserShares(), hasLinkShare)) {
  168. // remove icon, if applicable
  169. OC.Share.markFileAsShared($tr, false, false);
  170. }
  171. var newIcon = $tr.attr('data-icon');
  172. // in case markFileAsShared decided to change the icon,
  173. // we need to modify the model
  174. // (FIXME: yes, this is hacky)
  175. if (fileInfoModel.get('icon') !== newIcon) {
  176. fileInfoModel.set('icon', newIcon);
  177. }
  178. });
  179. fileList.registerTabView(shareTab);
  180. var breadCrumbSharingDetailView = new OCA.Sharing.ShareBreadCrumbView({shareTab: shareTab});
  181. fileList.registerBreadCrumbDetailView(breadCrumbSharingDetailView);
  182. },
  183. /**
  184. * Update file list data attributes
  185. */
  186. _updateFileListDataAttributes: function(fileList, $tr, shareModel) {
  187. // files app current cannot show recipients on load, so we don't update the
  188. // icon when changed for consistency
  189. if (fileList.id === 'files') {
  190. return;
  191. }
  192. var recipients = _.pluck(shareModel.get('shares'), 'share_with_displayname');
  193. // note: we only update the data attribute because updateIcon()
  194. if (recipients.length) {
  195. $tr.attr('data-share-recipients', OCA.Sharing.Util.formatRecipients(recipients));
  196. }
  197. else {
  198. $tr.removeAttr('data-share-recipients');
  199. }
  200. },
  201. /**
  202. * Update the file action share icon for the given file
  203. *
  204. * @param $tr file element of the file to update
  205. * @param {bool} hasUserShares true if a user share exists
  206. * @param {bool} hasLinkShare true if a link share exists
  207. *
  208. * @return {bool} true if the icon was set, false otherwise
  209. */
  210. _updateFileActionIcon: function($tr, hasUserShares, hasLinkShare) {
  211. // if the statuses are loaded already, use them for the icon
  212. // (needed when scrolling to the next page)
  213. if (hasUserShares || hasLinkShare || $tr.attr('data-share-recipients') || $tr.attr('data-share-owner')) {
  214. OC.Share.markFileAsShared($tr, true, hasLinkShare);
  215. return true;
  216. }
  217. return false;
  218. },
  219. /**
  220. * Formats a recipients array to be displayed.
  221. * The first four recipients will be shown and the
  222. * other ones will be shown as "+x" where "x" is the number of
  223. * remaining recipients.
  224. *
  225. * @param {Array.<String>} recipients recipients array
  226. * @param {int} count optional total recipients count (in case the array was shortened)
  227. * @return {String} formatted recipients display text
  228. */
  229. formatRecipients: function(recipients, count) {
  230. var maxRecipients = 4;
  231. var text;
  232. if (!_.isNumber(count)) {
  233. count = recipients.length;
  234. }
  235. // TODO: use natural sort
  236. recipients = _.first(recipients, maxRecipients).sort();
  237. text = recipients.join(', ');
  238. if (count > maxRecipients) {
  239. text += ', +' + (count - maxRecipients);
  240. }
  241. return text;
  242. },
  243. /**
  244. * @param {Array} fileData
  245. * @returns {String}
  246. */
  247. getSharePermissions: function(fileData) {
  248. var sharePermissions = fileData.permissions;
  249. if (fileData.mountType && fileData.mountType === "external-root"){
  250. // for external storages we can't use the permissions of the mountpoint
  251. // instead we show all permissions and only use the share permissions from the mountpoint to handle resharing
  252. sharePermissions = sharePermissions | (OC.PERMISSION_ALL & ~OC.PERMISSION_SHARE);
  253. }
  254. if (fileData.type === 'file') {
  255. // files can't be shared with delete permissions
  256. sharePermissions = sharePermissions & ~OC.PERMISSION_DELETE;
  257. // create permissions don't mean anything for files
  258. sharePermissions = sharePermissions & ~OC.PERMISSION_CREATE;
  259. }
  260. return sharePermissions;
  261. }
  262. };
  263. })();
  264. OC.Plugins.register('OCA.Files.FileList', OCA.Sharing.Util);