share.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. /* global escapeHTML */
  2. /**
  3. * @namespace
  4. */
  5. OC.Share = _.extend(OC.Share || {}, {
  6. SHARE_TYPE_USER:0,
  7. SHARE_TYPE_GROUP:1,
  8. SHARE_TYPE_LINK:3,
  9. SHARE_TYPE_EMAIL:4,
  10. SHARE_TYPE_REMOTE:6,
  11. SHARE_TYPE_CIRCLE:7,
  12. SHARE_TYPE_GUEST:8,
  13. SHARE_TYPE_REMOTE_GROUP:9,
  14. SHARE_TYPE_ROOM:10,
  15. /**
  16. * Regular expression for splitting parts of remote share owners:
  17. * "user@example.com/path/to/owncloud"
  18. * "user@anotherexample.com@example.com/path/to/owncloud
  19. */
  20. _REMOTE_OWNER_REGEXP: new RegExp("^([^@]*)@(([^@]*)@)?([^/]*)([/](.*)?)?$"),
  21. /**
  22. * @deprecated use OC.Share.currentShares instead
  23. */
  24. itemShares:[],
  25. /**
  26. * Full list of all share statuses
  27. */
  28. statuses:{},
  29. /**
  30. * Shares for the currently selected file.
  31. * (for which the dropdown is open)
  32. *
  33. * Key is item type and value is an array or
  34. * shares of the given item type.
  35. */
  36. currentShares: {},
  37. /**
  38. * Whether the share dropdown is opened.
  39. */
  40. droppedDown:false,
  41. /**
  42. * Loads ALL share statuses from server, stores them in
  43. * OC.Share.statuses then calls OC.Share.updateIcons() to update the
  44. * files "Share" icon to "Shared" according to their share status and
  45. * share type.
  46. *
  47. * If a callback is specified, the update step is skipped.
  48. *
  49. * @param itemType item type
  50. * @param fileList file list instance, defaults to OCA.Files.App.fileList
  51. * @param callback function to call after the shares were loaded
  52. */
  53. loadIcons:function(itemType, fileList, callback) {
  54. var path = fileList.dirInfo.path;
  55. if (path === '/') {
  56. path = '';
  57. }
  58. path += '/' + fileList.dirInfo.name;
  59. // Load all share icons
  60. $.get(
  61. OC.linkToOCS('apps/files_sharing/api/v1', 2) + 'shares',
  62. {
  63. subfiles: 'true',
  64. path: path,
  65. format: 'json'
  66. }, function(result) {
  67. if (result && result.ocs.meta.statuscode === 200) {
  68. OC.Share.statuses = {};
  69. $.each(result.ocs.data, function(it, share) {
  70. if (!(share.item_source in OC.Share.statuses)) {
  71. OC.Share.statuses[share.item_source] = {link: false};
  72. }
  73. if (share.share_type === OC.Share.SHARE_TYPE_LINK) {
  74. OC.Share.statuses[share.item_source] = {link: true};
  75. }
  76. });
  77. if (_.isFunction(callback)) {
  78. callback(OC.Share.statuses);
  79. } else {
  80. OC.Share.updateIcons(itemType, fileList);
  81. }
  82. }
  83. }
  84. );
  85. },
  86. /**
  87. * Updates the files' "Share" icons according to the known
  88. * sharing states stored in OC.Share.statuses.
  89. * (not reloaded from server)
  90. *
  91. * @param itemType item type
  92. * @param fileList file list instance
  93. * defaults to OCA.Files.App.fileList
  94. */
  95. updateIcons:function(itemType, fileList){
  96. var item;
  97. var $fileList;
  98. var currentDir;
  99. if (!fileList && OCA.Files) {
  100. fileList = OCA.Files.App.fileList;
  101. }
  102. // fileList is usually only defined in the files app
  103. if (fileList) {
  104. $fileList = fileList.$fileList;
  105. currentDir = fileList.getCurrentDirectory();
  106. }
  107. // TODO: iterating over the files might be more efficient
  108. for (item in OC.Share.statuses){
  109. var iconClass = 'icon-shared';
  110. var data = OC.Share.statuses[item];
  111. var hasLink = data.link;
  112. // Links override shared in terms of icon display
  113. if (hasLink) {
  114. iconClass = 'icon-public';
  115. }
  116. if (itemType !== 'file' && itemType !== 'folder') {
  117. $('a.share[data-item="'+item+'"] .icon').removeClass('icon-shared icon-public').addClass(iconClass);
  118. } else {
  119. // TODO: ultimately this part should be moved to files_sharing app
  120. var file = $fileList.find('tr[data-id="'+item+'"]');
  121. var shareFolder = OC.imagePath('core', 'filetypes/folder-shared');
  122. var img;
  123. if (file.length > 0) {
  124. this.markFileAsShared(file, true, hasLink);
  125. } else {
  126. var dir = currentDir;
  127. if (dir.length > 1) {
  128. var last = '';
  129. var path = dir;
  130. // Search for possible parent folders that are shared
  131. while (path != last) {
  132. if (path === data.path && !data.link) {
  133. var actions = $fileList.find('.fileactions .action[data-action="Share"]');
  134. var files = $fileList.find('.filename');
  135. var i;
  136. for (i = 0; i < actions.length; i++) {
  137. // TODO: use this.markFileAsShared()
  138. img = $(actions[i]).find('img');
  139. if (img.attr('src') !== OC.imagePath('core', 'actions/public')) {
  140. img.attr('src', image);
  141. $(actions[i]).addClass('permanent');
  142. $(actions[i]).html('<span> '+t('core', 'Shared')+'</span>').prepend(img);
  143. }
  144. }
  145. for(i = 0; i < files.length; i++) {
  146. if ($(files[i]).closest('tr').data('type') === 'dir') {
  147. $(files[i]).find('.thumbnail').css('background-image', 'url('+shareFolder+')');
  148. }
  149. }
  150. }
  151. last = path;
  152. path = OC.Share.dirname(path);
  153. }
  154. }
  155. }
  156. }
  157. }
  158. },
  159. updateIcon:function(itemType, itemSource) {
  160. var shares = false;
  161. var link = false;
  162. var iconClass = '';
  163. $.each(OC.Share.itemShares, function(index) {
  164. if (OC.Share.itemShares[index]) {
  165. if (index == OC.Share.SHARE_TYPE_LINK) {
  166. if (OC.Share.itemShares[index] == true) {
  167. shares = true;
  168. iconClass = 'icon-public';
  169. link = true;
  170. return;
  171. }
  172. } else if (OC.Share.itemShares[index].length > 0) {
  173. shares = true;
  174. iconClass = 'icon-shared';
  175. }
  176. }
  177. });
  178. if (itemType != 'file' && itemType != 'folder') {
  179. $('a.share[data-item="'+itemSource+'"] .icon').removeClass('icon-shared icon-public').addClass(iconClass);
  180. } else {
  181. var $tr = $('tr').filterAttr('data-id', String(itemSource));
  182. if ($tr.length > 0) {
  183. // it might happen that multiple lists exist in the DOM
  184. // with the same id
  185. $tr.each(function() {
  186. OC.Share.markFileAsShared($(this), shares, link);
  187. });
  188. }
  189. }
  190. if (shares) {
  191. OC.Share.statuses[itemSource] = OC.Share.statuses[itemSource] || {};
  192. OC.Share.statuses[itemSource].link = link;
  193. } else {
  194. delete OC.Share.statuses[itemSource];
  195. }
  196. },
  197. /**
  198. * Format a remote address
  199. *
  200. * @param {String} shareWith userid, full remote share, or whatever
  201. * @param {String} shareWithDisplayName
  202. * @param {String} message
  203. * @return {String} HTML code to display
  204. */
  205. _formatRemoteShare: function(shareWith, shareWithDisplayName, message) {
  206. var parts = this._REMOTE_OWNER_REGEXP.exec(shareWith);
  207. if (!parts) {
  208. // display avatar of the user
  209. var avatar = '<span class="avatar" data-username="' + escapeHTML(shareWith) + '" title="' + message + " " + escapeHTML(shareWithDisplayName) + '"></span>';
  210. var hidden = '<span class="hidden-visually">' + message + ' ' + escapeHTML(shareWithDisplayName) + '</span> ';
  211. return avatar + hidden;
  212. }
  213. var userName = parts[1];
  214. var userDomain = parts[3];
  215. var server = parts[4];
  216. var tooltip = message + ' ' + userName;
  217. if (userDomain) {
  218. tooltip += '@' + userDomain;
  219. }
  220. if (server) {
  221. if (!userDomain) {
  222. userDomain = '…';
  223. }
  224. tooltip += '@' + server;
  225. }
  226. var html = '<span class="remoteAddress" title="' + escapeHTML(tooltip) + '">';
  227. html += '<span class="username">' + escapeHTML(userName) + '</span>';
  228. if (userDomain) {
  229. html += '<span class="userDomain">@' + escapeHTML(userDomain) + '</span>';
  230. }
  231. html += '</span> ';
  232. return html;
  233. },
  234. /**
  235. * Loop over all recipients in the list and format them using
  236. * all kind of fancy magic.
  237. *
  238. * @param {Object} recipients array of all the recipients
  239. * @return {String[]} modified list of recipients
  240. */
  241. _formatShareList: function(recipients) {
  242. var _parent = this;
  243. recipients = _.toArray(recipients);
  244. recipients.sort(function(a, b) {
  245. return a.shareWithDisplayName.localeCompare(b.shareWithDisplayName);
  246. });
  247. return $.map(recipients, function(recipient) {
  248. return _parent._formatRemoteShare(recipient.shareWith, recipient.shareWithDisplayName, t('core', 'Shared with'));
  249. });
  250. },
  251. /**
  252. * Marks/unmarks a given file as shared by changing its action icon
  253. * and folder icon.
  254. *
  255. * @param $tr file element to mark as shared
  256. * @param hasShares whether shares are available
  257. * @param hasLink whether link share is available
  258. */
  259. markFileAsShared: function($tr, hasShares, hasLink) {
  260. var action = $tr.find('.fileactions .action[data-action="Share"]');
  261. var type = $tr.data('type');
  262. var icon = action.find('.icon');
  263. var message, recipients, avatars;
  264. var ownerId = $tr.attr('data-share-owner-id');
  265. var owner = $tr.attr('data-share-owner');
  266. var mountType = $tr.attr('data-mounttype');
  267. var shareFolderIcon;
  268. var iconClass = 'icon-shared';
  269. action.removeClass('shared-style');
  270. // update folder icon
  271. if (type === 'dir' && (hasShares || hasLink || ownerId)) {
  272. if (typeof mountType !== 'undefined' && mountType !== 'shared-root' && mountType !== 'shared') {
  273. shareFolderIcon = OC.MimeType.getIconUrl('dir-' + mountType);
  274. } else if (hasLink) {
  275. shareFolderIcon = OC.MimeType.getIconUrl('dir-public');
  276. } else {
  277. shareFolderIcon = OC.MimeType.getIconUrl('dir-shared');
  278. }
  279. $tr.find('.filename .thumbnail').css('background-image', 'url(' + shareFolderIcon + ')');
  280. $tr.attr('data-icon', shareFolderIcon);
  281. } else if (type === 'dir') {
  282. var isEncrypted = $tr.attr('data-e2eencrypted');
  283. // FIXME: duplicate of FileList._createRow logic for external folder,
  284. // need to refactor the icon logic into a single code path eventually
  285. if (isEncrypted === 'true') {
  286. shareFolderIcon = OC.MimeType.getIconUrl('dir-encrypted');
  287. $tr.attr('data-icon', shareFolderIcon);
  288. } else if (mountType && mountType.indexOf('external') === 0) {
  289. shareFolderIcon = OC.MimeType.getIconUrl('dir-external');
  290. $tr.attr('data-icon', shareFolderIcon);
  291. } else {
  292. shareFolderIcon = OC.MimeType.getIconUrl('dir');
  293. // back to default
  294. $tr.removeAttr('data-icon');
  295. }
  296. $tr.find('.filename .thumbnail').css('background-image', 'url(' + shareFolderIcon + ')');
  297. }
  298. // update share action text / icon
  299. if (hasShares || ownerId) {
  300. recipients = $tr.data('share-recipient-data');
  301. action.addClass('shared-style');
  302. avatars = '<span>' + t('core', 'Shared') + '</span>';
  303. // even if reshared, only show "Shared by"
  304. if (ownerId) {
  305. message = t('core', 'Shared by');
  306. avatars = this._formatRemoteShare(ownerId, owner, message);
  307. } else if (recipients) {
  308. avatars = this._formatShareList(recipients);
  309. }
  310. action.html(avatars).prepend(icon);
  311. if (ownerId || recipients) {
  312. var avatarElement = action.find('.avatar');
  313. avatarElement.each(function () {
  314. $(this).avatar($(this).data('username'), 32);
  315. });
  316. action.find('span[title]').tooltip({placement: 'top'});
  317. }
  318. } else {
  319. action.html('<span class="hidden-visually">' + t('core', 'Shared') + '</span>').prepend(icon);
  320. }
  321. if (hasLink) {
  322. iconClass = 'icon-public';
  323. }
  324. icon.removeClass('icon-shared icon-public').addClass(iconClass);
  325. },
  326. showDropDown:function(itemType, itemSource, appendTo, link, possiblePermissions, filename) {
  327. var configModel = new OC.Share.ShareConfigModel();
  328. var attributes = {itemType: itemType, itemSource: itemSource, possiblePermissions: possiblePermissions};
  329. var itemModel = new OC.Share.ShareItemModel(attributes, {configModel: configModel});
  330. var dialogView = new OC.Share.ShareDialogView({
  331. id: 'dropdown',
  332. model: itemModel,
  333. configModel: configModel,
  334. className: 'drop shareDropDown',
  335. attributes: {
  336. 'data-item-source-name': filename,
  337. 'data-item-type': itemType,
  338. 'data-item-source': itemSource
  339. }
  340. });
  341. dialogView.setShowLink(link);
  342. var $dialog = dialogView.render().$el;
  343. $dialog.appendTo(appendTo);
  344. $dialog.slideDown(OC.menuSpeed, function() {
  345. OC.Share.droppedDown = true;
  346. });
  347. itemModel.fetch();
  348. },
  349. hideDropDown:function(callback) {
  350. OC.Share.currentShares = null;
  351. $('#dropdown').slideUp(OC.menuSpeed, function() {
  352. OC.Share.droppedDown = false;
  353. $('#dropdown').remove();
  354. if (typeof FileActions !== 'undefined') {
  355. $('tr').removeClass('mouseOver');
  356. }
  357. if (callback) {
  358. callback.call();
  359. }
  360. });
  361. },
  362. dirname:function(path) {
  363. return path.replace(/\\/g,'/').replace(/\/[^\/]*$/, '');
  364. }
  365. });
  366. $(document).ready(function() {
  367. if(typeof monthNames != 'undefined'){
  368. // min date should always be the next day
  369. var minDate = new Date();
  370. minDate.setDate(minDate.getDate()+1);
  371. $.datepicker.setDefaults({
  372. monthNames: monthNames,
  373. monthNamesShort: monthNamesShort,
  374. dayNames: dayNames,
  375. dayNamesMin: dayNamesMin,
  376. dayNamesShort: dayNamesShort,
  377. firstDay: firstDay,
  378. minDate : minDate
  379. });
  380. }
  381. $(this).click(function(event) {
  382. var target = $(event.target);
  383. var isMatched = !target.is('.drop, .ui-datepicker-next, .ui-datepicker-prev, .ui-icon')
  384. && !target.closest('#ui-datepicker-div').length && !target.closest('.ui-autocomplete').length;
  385. if (OC.Share && OC.Share.droppedDown && isMatched && $('#dropdown').has(event.target).length === 0) {
  386. OC.Share.hideDropDown();
  387. }
  388. });
  389. });