share.js 12 KB

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