share.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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 image = OC.imagePath('core', 'actions/share');
  161. var iconClass = '';
  162. $.each(OC.Share.itemShares, function(index) {
  163. if (OC.Share.itemShares[index]) {
  164. if (index == OC.Share.SHARE_TYPE_LINK) {
  165. if (OC.Share.itemShares[index] == true) {
  166. shares = true;
  167. iconClass = 'icon-public';
  168. link = true;
  169. return;
  170. }
  171. } else if (OC.Share.itemShares[index].length > 0) {
  172. shares = true;
  173. iconClass = 'icon-shared';
  174. }
  175. }
  176. });
  177. if (itemType != 'file' && itemType != 'folder') {
  178. $('a.share[data-item="'+itemSource+'"] .icon').removeClass('icon-shared icon-public').addClass(iconClass);
  179. } else {
  180. var $tr = $('tr').filterAttr('data-id', String(itemSource));
  181. if ($tr.length > 0) {
  182. // it might happen that multiple lists exist in the DOM
  183. // with the same id
  184. $tr.each(function() {
  185. OC.Share.markFileAsShared($(this), shares, link);
  186. });
  187. }
  188. }
  189. if (shares) {
  190. OC.Share.statuses[itemSource] = OC.Share.statuses[itemSource] || {};
  191. OC.Share.statuses[itemSource].link = link;
  192. } else {
  193. delete OC.Share.statuses[itemSource];
  194. }
  195. },
  196. /**
  197. * Format a remote address
  198. *
  199. * @param {String} remoteAddress full remote share
  200. * @return {String} HTML code to display
  201. */
  202. _formatRemoteShare: function(remoteAddress) {
  203. var parts = this._REMOTE_OWNER_REGEXP.exec(remoteAddress);
  204. if (!parts) {
  205. // display as is, most likely to be a simple owner name
  206. return escapeHTML(remoteAddress);
  207. }
  208. var userName = parts[1];
  209. var userDomain = parts[3];
  210. var server = parts[4];
  211. var dir = parts[6];
  212. var tooltip = userName;
  213. if (userDomain) {
  214. tooltip += '@' + userDomain;
  215. }
  216. if (server) {
  217. if (!userDomain) {
  218. userDomain = '…';
  219. }
  220. tooltip += '@' + server;
  221. }
  222. var html = '<span class="remoteAddress" title="' + escapeHTML(tooltip) + '">';
  223. html += '<span class="username">' + escapeHTML(userName) + '</span>';
  224. if (userDomain) {
  225. html += '<span class="userDomain">@' + escapeHTML(userDomain) + '</span>';
  226. }
  227. html += '</span>';
  228. return html;
  229. },
  230. /**
  231. * Loop over all recipients in the list and format them using
  232. * all kind of fancy magic.
  233. *
  234. * @param {String[]} recipients array of all the recipients
  235. * @return {String[]} modified list of recipients
  236. */
  237. _formatShareList: function(recipients) {
  238. var _parent = this;
  239. return $.map(recipients, function(recipient) {
  240. recipient = _parent._formatRemoteShare(recipient);
  241. return recipient;
  242. });
  243. },
  244. /**
  245. * Marks/unmarks a given file as shared by changing its action icon
  246. * and folder icon.
  247. *
  248. * @param $tr file element to mark as shared
  249. * @param hasShares whether shares are available
  250. * @param hasLink whether link share is available
  251. */
  252. markFileAsShared: function($tr, hasShares, hasLink) {
  253. var action = $tr.find('.fileactions .action[data-action="Share"]');
  254. var type = $tr.data('type');
  255. var icon = action.find('.icon');
  256. var message;
  257. var recipients;
  258. var owner = $tr.attr('data-share-owner');
  259. var shareFolderIcon;
  260. var iconClass = 'icon-shared';
  261. action.removeClass('shared-style');
  262. // update folder icon
  263. if (type === 'dir' && (hasShares || hasLink || owner)) {
  264. if (hasLink) {
  265. shareFolderIcon = OC.MimeType.getIconUrl('dir-public');
  266. }
  267. else {
  268. shareFolderIcon = OC.MimeType.getIconUrl('dir-shared');
  269. }
  270. $tr.find('.filename .thumbnail').css('background-image', 'url(' + shareFolderIcon + ')');
  271. $tr.attr('data-icon', shareFolderIcon);
  272. } else if (type === 'dir') {
  273. var mountType = $tr.attr('data-mounttype');
  274. // FIXME: duplicate of FileList._createRow logic for external folder,
  275. // need to refactor the icon logic into a single code path eventually
  276. if (mountType && mountType.indexOf('external') === 0) {
  277. shareFolderIcon = OC.MimeType.getIconUrl('dir-external');
  278. $tr.attr('data-icon', shareFolderIcon);
  279. } else {
  280. shareFolderIcon = OC.MimeType.getIconUrl('dir');
  281. // back to default
  282. $tr.removeAttr('data-icon');
  283. }
  284. $tr.find('.filename .thumbnail').css('background-image', 'url(' + shareFolderIcon + ')');
  285. }
  286. // update share action text / icon
  287. if (hasShares || owner) {
  288. recipients = $tr.attr('data-share-recipients');
  289. action.addClass('shared-style');
  290. message = t('core', 'Shared');
  291. // even if reshared, only show "Shared by"
  292. if (owner) {
  293. message = this._formatRemoteShare(owner);
  294. }
  295. else if (recipients) {
  296. message = t('core', 'Shared with {recipients}', {recipients: this._formatShareList(recipients.split(", ")).join(", ")}, 0, {escape: false});
  297. }
  298. action.html('<span> ' + message + '</span>').prepend(icon);
  299. if (owner || recipients) {
  300. action.find('.remoteAddress').tooltip({placement: 'top'});
  301. }
  302. }
  303. else {
  304. action.html('<span class="hidden-visually">' + t('core', 'Shared') + '</span>').prepend(icon);
  305. }
  306. if (hasLink) {
  307. iconClass = 'icon-public';
  308. }
  309. icon.removeClass('icon-shared icon-public').addClass(iconClass);
  310. },
  311. showDropDown:function(itemType, itemSource, appendTo, link, possiblePermissions, filename) {
  312. var configModel = new OC.Share.ShareConfigModel();
  313. var attributes = {itemType: itemType, itemSource: itemSource, possiblePermissions: possiblePermissions};
  314. var itemModel = new OC.Share.ShareItemModel(attributes, {configModel: configModel});
  315. var dialogView = new OC.Share.ShareDialogView({
  316. id: 'dropdown',
  317. model: itemModel,
  318. configModel: configModel,
  319. className: 'drop shareDropDown',
  320. attributes: {
  321. 'data-item-source-name': filename,
  322. 'data-item-type': itemType,
  323. 'data-item-source': itemSource
  324. }
  325. });
  326. dialogView.setShowLink(link);
  327. var $dialog = dialogView.render().$el;
  328. $dialog.appendTo(appendTo);
  329. $dialog.slideDown(OC.menuSpeed, function() {
  330. OC.Share.droppedDown = true;
  331. });
  332. itemModel.fetch();
  333. },
  334. hideDropDown:function(callback) {
  335. OC.Share.currentShares = null;
  336. $('#dropdown').slideUp(OC.menuSpeed, function() {
  337. OC.Share.droppedDown = false;
  338. $('#dropdown').remove();
  339. if (typeof FileActions !== 'undefined') {
  340. $('tr').removeClass('mouseOver');
  341. }
  342. if (callback) {
  343. callback.call();
  344. }
  345. });
  346. },
  347. dirname:function(path) {
  348. return path.replace(/\\/g,'/').replace(/\/[^\/]*$/, '');
  349. }
  350. });
  351. $(document).ready(function() {
  352. if(typeof monthNames != 'undefined'){
  353. // min date should always be the next day
  354. var minDate = new Date();
  355. minDate.setDate(minDate.getDate()+1);
  356. $.datepicker.setDefaults({
  357. monthNames: monthNames,
  358. monthNamesShort: monthNamesShort,
  359. dayNames: dayNames,
  360. dayNamesMin: dayNamesMin,
  361. dayNamesShort: dayNamesShort,
  362. firstDay: firstDay,
  363. minDate : minDate
  364. });
  365. }
  366. $(this).click(function(event) {
  367. var target = $(event.target);
  368. var isMatched = !target.is('.drop, .ui-datepicker-next, .ui-datepicker-prev, .ui-icon')
  369. && !target.closest('#ui-datepicker-div').length && !target.closest('.ui-autocomplete').length;
  370. if (OC.Share && OC.Share.droppedDown && isMatched && $('#dropdown').has(event.target).length === 0) {
  371. OC.Share.hideDropDown();
  372. }
  373. });
  374. });