You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

share.js 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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-shared',
  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. // FIXME: this is too convoluted. We need to get rid of the above updates
  172. // and only ever update the model and let the events take care of rerendering
  173. fileInfoModel.set({
  174. shareTypes: shareModel.getShareTypes(),
  175. // in case markFileAsShared decided to change the icon,
  176. // we need to modify the model
  177. // (FIXME: yes, this is hacky)
  178. icon: $tr.attr('data-icon')
  179. });
  180. });
  181. fileList.registerTabView(shareTab);
  182. var breadCrumbSharingDetailView = new OCA.Sharing.ShareBreadCrumbView({shareTab: shareTab});
  183. fileList.registerBreadCrumbDetailView(breadCrumbSharingDetailView);
  184. },
  185. /**
  186. * Update file list data attributes
  187. */
  188. _updateFileListDataAttributes: function(fileList, $tr, shareModel) {
  189. // files app current cannot show recipients on load, so we don't update the
  190. // icon when changed for consistency
  191. if (fileList.id === 'files') {
  192. return;
  193. }
  194. var recipients = _.pluck(shareModel.get('shares'), 'share_with_displayname');
  195. // note: we only update the data attribute because updateIcon()
  196. if (recipients.length) {
  197. $tr.attr('data-share-recipients', OCA.Sharing.Util.formatRecipients(recipients));
  198. }
  199. else {
  200. $tr.removeAttr('data-share-recipients');
  201. }
  202. },
  203. /**
  204. * Update the file action share icon for the given file
  205. *
  206. * @param $tr file element of the file to update
  207. * @param {bool} hasUserShares true if a user share exists
  208. * @param {bool} hasLinkShare true if a link share exists
  209. *
  210. * @return {bool} true if the icon was set, false otherwise
  211. */
  212. _updateFileActionIcon: function($tr, hasUserShares, hasLinkShare) {
  213. // if the statuses are loaded already, use them for the icon
  214. // (needed when scrolling to the next page)
  215. if (hasUserShares || hasLinkShare || $tr.attr('data-share-recipients') || $tr.attr('data-share-owner')) {
  216. OC.Share.markFileAsShared($tr, true, hasLinkShare);
  217. return true;
  218. }
  219. return false;
  220. },
  221. /**
  222. * Formats a recipients array to be displayed.
  223. * The first four recipients will be shown and the
  224. * other ones will be shown as "+x" where "x" is the number of
  225. * remaining recipients.
  226. *
  227. * @param {Array.<String>} recipients recipients array
  228. * @param {int} count optional total recipients count (in case the array was shortened)
  229. * @return {String} formatted recipients display text
  230. */
  231. formatRecipients: function(recipients, count) {
  232. var maxRecipients = 4;
  233. var text;
  234. if (!_.isNumber(count)) {
  235. count = recipients.length;
  236. }
  237. // TODO: use natural sort
  238. recipients = _.first(recipients, maxRecipients).sort();
  239. text = recipients.join(', ');
  240. if (count > maxRecipients) {
  241. text += ', +' + (count - maxRecipients);
  242. }
  243. return text;
  244. },
  245. /**
  246. * @param {Array} fileData
  247. * @returns {String}
  248. */
  249. getSharePermissions: function(fileData) {
  250. var sharePermissions = fileData.permissions;
  251. if (fileData.mountType && fileData.mountType === "external-root"){
  252. // for external storages we can't use the permissions of the mountpoint
  253. // instead we show all permissions and only use the share permissions from the mountpoint to handle resharing
  254. sharePermissions = sharePermissions | (OC.PERMISSION_ALL & ~OC.PERMISSION_SHARE);
  255. }
  256. if (fileData.type === 'file') {
  257. // files can't be shared with delete permissions
  258. sharePermissions = sharePermissions & ~OC.PERMISSION_DELETE;
  259. // create permissions don't mean anything for files
  260. sharePermissions = sharePermissions & ~OC.PERMISSION_CREATE;
  261. }
  262. return sharePermissions;
  263. }
  264. };
  265. })();
  266. OC.Plugins.register('OCA.Files.FileList', OCA.Sharing.Util);