1
0

share.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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 shareFolderIcon;
  267. var iconClass = 'icon-shared';
  268. action.removeClass('shared-style');
  269. // update folder icon
  270. if (type === 'dir' && (hasShares || hasLink || ownerId)) {
  271. if (hasLink) {
  272. shareFolderIcon = OC.MimeType.getIconUrl('dir-public');
  273. }
  274. else {
  275. shareFolderIcon = OC.MimeType.getIconUrl('dir-shared');
  276. }
  277. $tr.find('.filename .thumbnail').css('background-image', 'url(' + shareFolderIcon + ')');
  278. $tr.attr('data-icon', shareFolderIcon);
  279. } else if (type === 'dir') {
  280. var isEncrypted = $tr.attr('data-e2eencrypted');
  281. var mountType = $tr.attr('data-mounttype');
  282. // FIXME: duplicate of FileList._createRow logic for external folder,
  283. // need to refactor the icon logic into a single code path eventually
  284. if (isEncrypted === 'true') {
  285. shareFolderIcon = OC.MimeType.getIconUrl('dir-encrypted');
  286. $tr.attr('data-icon', shareFolderIcon);
  287. } else if (mountType && mountType.indexOf('external') === 0) {
  288. shareFolderIcon = OC.MimeType.getIconUrl('dir-external');
  289. $tr.attr('data-icon', shareFolderIcon);
  290. } else {
  291. shareFolderIcon = OC.MimeType.getIconUrl('dir');
  292. // back to default
  293. $tr.removeAttr('data-icon');
  294. }
  295. $tr.find('.filename .thumbnail').css('background-image', 'url(' + shareFolderIcon + ')');
  296. }
  297. // update share action text / icon
  298. if (hasShares || ownerId) {
  299. recipients = $tr.data('share-recipient-data');
  300. action.addClass('shared-style');
  301. avatars = '<span>' + t('core', 'Shared') + '</span>';
  302. // even if reshared, only show "Shared by"
  303. if (ownerId) {
  304. message = t('core', 'Shared by');
  305. avatars = this._formatRemoteShare(ownerId, owner, message);
  306. } else if (recipients) {
  307. avatars = this._formatShareList(recipients);
  308. }
  309. action.html(avatars).prepend(icon);
  310. if (ownerId || recipients) {
  311. var avatarElement = action.find('.avatar');
  312. avatarElement.each(function () {
  313. $(this).avatar($(this).data('username'), 32);
  314. });
  315. action.find('span[title]').tooltip({placement: 'top'});
  316. }
  317. } else {
  318. action.html('<span class="hidden-visually">' + t('core', 'Shared') + '</span>').prepend(icon);
  319. }
  320. if (hasLink) {
  321. iconClass = 'icon-public';
  322. }
  323. icon.removeClass('icon-shared icon-public').addClass(iconClass);
  324. },
  325. showDropDown:function(itemType, itemSource, appendTo, link, possiblePermissions, filename) {
  326. var configModel = new OC.Share.ShareConfigModel();
  327. var attributes = {itemType: itemType, itemSource: itemSource, possiblePermissions: possiblePermissions};
  328. var itemModel = new OC.Share.ShareItemModel(attributes, {configModel: configModel});
  329. var dialogView = new OC.Share.ShareDialogView({
  330. id: 'dropdown',
  331. model: itemModel,
  332. configModel: configModel,
  333. className: 'drop shareDropDown',
  334. attributes: {
  335. 'data-item-source-name': filename,
  336. 'data-item-type': itemType,
  337. 'data-item-source': itemSource
  338. }
  339. });
  340. dialogView.setShowLink(link);
  341. var $dialog = dialogView.render().$el;
  342. $dialog.appendTo(appendTo);
  343. $dialog.slideDown(OC.menuSpeed, function() {
  344. OC.Share.droppedDown = true;
  345. });
  346. itemModel.fetch();
  347. },
  348. hideDropDown:function(callback) {
  349. OC.Share.currentShares = null;
  350. $('#dropdown').slideUp(OC.menuSpeed, function() {
  351. OC.Share.droppedDown = false;
  352. $('#dropdown').remove();
  353. if (typeof FileActions !== 'undefined') {
  354. $('tr').removeClass('mouseOver');
  355. }
  356. if (callback) {
  357. callback.call();
  358. }
  359. });
  360. },
  361. dirname:function(path) {
  362. return path.replace(/\\/g,'/').replace(/\/[^\/]*$/, '');
  363. }
  364. });
  365. $(document).ready(function() {
  366. if(typeof monthNames != 'undefined'){
  367. // min date should always be the next day
  368. var minDate = new Date();
  369. minDate.setDate(minDate.getDate()+1);
  370. $.datepicker.setDefaults({
  371. monthNames: monthNames,
  372. monthNamesShort: monthNamesShort,
  373. dayNames: dayNames,
  374. dayNamesMin: dayNamesMin,
  375. dayNamesShort: dayNamesShort,
  376. firstDay: firstDay,
  377. minDate : minDate
  378. });
  379. }
  380. $(this).click(function(event) {
  381. var target = $(event.target);
  382. var isMatched = !target.is('.drop, .ui-datepicker-next, .ui-datepicker-prev, .ui-icon')
  383. && !target.closest('#ui-datepicker-div').length && !target.closest('.ui-autocomplete').length;
  384. if (OC.Share && OC.Share.droppedDown && isMatched && $('#dropdown').has(event.target).length === 0) {
  385. OC.Share.hideDropDown();
  386. }
  387. });
  388. });