share.js 14 KB

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