1
0

share.js 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231
  1. /* global escapeHTML */
  2. /**
  3. * @namespace
  4. */
  5. 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. // Load all share icons
  51. $.get(
  52. OC.filePath('core', 'ajax', 'share.php'),
  53. {
  54. fetch: 'getItemsSharedStatuses',
  55. itemType: itemType
  56. }, function(result) {
  57. if (result && result.status === 'success') {
  58. OC.Share.statuses = {};
  59. $.each(result.data, function(item, data) {
  60. OC.Share.statuses[item] = data;
  61. });
  62. if (_.isFunction(callback)) {
  63. callback(OC.Share.statuses);
  64. } else {
  65. OC.Share.updateIcons(itemType, fileList);
  66. }
  67. }
  68. }
  69. );
  70. },
  71. /**
  72. * Updates the files' "Share" icons according to the known
  73. * sharing states stored in OC.Share.statuses.
  74. * (not reloaded from server)
  75. *
  76. * @param itemType item type
  77. * @param fileList file list instance
  78. * defaults to OCA.Files.App.fileList
  79. */
  80. updateIcons:function(itemType, fileList){
  81. var item;
  82. var $fileList;
  83. var currentDir;
  84. if (!fileList && OCA.Files) {
  85. fileList = OCA.Files.App.fileList;
  86. }
  87. // fileList is usually only defined in the files app
  88. if (fileList) {
  89. $fileList = fileList.$fileList;
  90. currentDir = fileList.getCurrentDirectory();
  91. }
  92. // TODO: iterating over the files might be more efficient
  93. for (item in OC.Share.statuses){
  94. var image = OC.imagePath('core', 'actions/share');
  95. var data = OC.Share.statuses[item];
  96. var hasLink = data.link;
  97. // Links override shared in terms of icon display
  98. if (hasLink) {
  99. image = OC.imagePath('core', 'actions/public');
  100. }
  101. if (itemType !== 'file' && itemType !== 'folder') {
  102. $('a.share[data-item="'+item+'"]').css('background', 'url('+image+') no-repeat center');
  103. } else {
  104. // TODO: ultimately this part should be moved to files_sharing app
  105. var file = $fileList.find('tr[data-id="'+item+'"]');
  106. var shareFolder = OC.imagePath('core', 'filetypes/folder-shared');
  107. var img;
  108. if (file.length > 0) {
  109. this.markFileAsShared(file, true, hasLink);
  110. } else {
  111. var dir = currentDir;
  112. if (dir.length > 1) {
  113. var last = '';
  114. var path = dir;
  115. // Search for possible parent folders that are shared
  116. while (path != last) {
  117. if (path === data.path && !data.link) {
  118. var actions = $fileList.find('.fileactions .action[data-action="Share"]');
  119. var files = $fileList.find('.filename');
  120. var i;
  121. for (i = 0; i < actions.length; i++) {
  122. // TODO: use this.markFileAsShared()
  123. img = $(actions[i]).find('img');
  124. if (img.attr('src') !== OC.imagePath('core', 'actions/public')) {
  125. img.attr('src', image);
  126. $(actions[i]).addClass('permanent');
  127. $(actions[i]).html(' <span>'+t('core', 'Shared')+'</span>').prepend(img);
  128. }
  129. }
  130. for(i = 0; i < files.length; i++) {
  131. if ($(files[i]).closest('tr').data('type') === 'dir') {
  132. $(files[i]).find('.thumbnail').css('background-image', 'url('+shareFolder+')');
  133. }
  134. }
  135. }
  136. last = path;
  137. path = OC.Share.dirname(path);
  138. }
  139. }
  140. }
  141. }
  142. }
  143. },
  144. updateIcon:function(itemType, itemSource) {
  145. var shares = false;
  146. var link = false;
  147. var image = OC.imagePath('core', 'actions/share');
  148. $.each(OC.Share.itemShares, function(index) {
  149. if (OC.Share.itemShares[index]) {
  150. if (index == OC.Share.SHARE_TYPE_LINK) {
  151. if (OC.Share.itemShares[index] == true) {
  152. shares = true;
  153. image = OC.imagePath('core', 'actions/public');
  154. link = true;
  155. return;
  156. }
  157. } else if (OC.Share.itemShares[index].length > 0) {
  158. shares = true;
  159. image = OC.imagePath('core', 'actions/share');
  160. }
  161. }
  162. });
  163. if (itemType != 'file' && itemType != 'folder') {
  164. $('a.share[data-item="'+itemSource+'"]').css('background', 'url('+image+') no-repeat center');
  165. } else {
  166. var $tr = $('tr').filterAttr('data-id', String(itemSource));
  167. if ($tr.length > 0) {
  168. // it might happen that multiple lists exist in the DOM
  169. // with the same id
  170. $tr.each(function() {
  171. OC.Share.markFileAsShared($(this), shares, link);
  172. });
  173. }
  174. }
  175. if (shares) {
  176. OC.Share.statuses[itemSource] = OC.Share.statuses[itemSource] || {};
  177. OC.Share.statuses[itemSource]['link'] = link;
  178. } else {
  179. delete OC.Share.statuses[itemSource];
  180. }
  181. },
  182. /**
  183. * Format remote share owner to make it more readable
  184. *
  185. * @param {String} owner full remote share owner name
  186. * @return {String} HTML code for the owner display
  187. */
  188. _formatSharedByOwner: function(owner) {
  189. var parts = this._REMOTE_OWNER_REGEXP.exec(owner);
  190. if (!parts) {
  191. // display as is, most likely to be a simple owner name
  192. return escapeHTML(owner);
  193. }
  194. var userName = parts[1];
  195. var userDomain = parts[3];
  196. var server = parts[4];
  197. var tooltip = userName;
  198. if (userDomain) {
  199. tooltip += '@' + userDomain;
  200. }
  201. if (server) {
  202. if (!userDomain) {
  203. userDomain = '…';
  204. }
  205. tooltip += '@' + server;
  206. }
  207. var html = '<span class="remoteOwner" title="' + escapeHTML(tooltip) + '">';
  208. html += '<span class="username">' + escapeHTML(userName) + '</span>';
  209. if (userDomain) {
  210. html += '<span class="userDomain">@' + escapeHTML(userDomain) + '</span>';
  211. }
  212. html += '</span>';
  213. return html;
  214. },
  215. /**
  216. * Marks/unmarks a given file as shared by changing its action icon
  217. * and folder icon.
  218. *
  219. * @param $tr file element to mark as shared
  220. * @param hasShares whether shares are available
  221. * @param hasLink whether link share is available
  222. */
  223. markFileAsShared: function($tr, hasShares, hasLink) {
  224. var action = $tr.find('.fileactions .action[data-action="Share"]');
  225. var type = $tr.data('type');
  226. var img = action.find('img');
  227. var message;
  228. var recipients;
  229. var owner = $tr.attr('data-share-owner');
  230. var shareFolderIcon;
  231. var image = OC.imagePath('core', 'actions/share');
  232. // update folder icon
  233. if (type === 'dir' && (hasShares || hasLink || owner)) {
  234. if (hasLink) {
  235. shareFolderIcon = OC.imagePath('core', 'filetypes/folder-public');
  236. }
  237. else {
  238. shareFolderIcon = OC.imagePath('core', 'filetypes/folder-shared');
  239. }
  240. $tr.find('.filename .thumbnail').css('background-image', 'url(' + shareFolderIcon + ')');
  241. } else if (type === 'dir') {
  242. shareFolderIcon = OC.imagePath('core', 'filetypes/folder');
  243. $tr.find('.filename .thumbnail').css('background-image', 'url(' + shareFolderIcon + ')');
  244. }
  245. // update share action text / icon
  246. if (hasShares || owner) {
  247. recipients = $tr.attr('data-share-recipients');
  248. action.addClass('permanent');
  249. message = t('core', 'Shared');
  250. // even if reshared, only show "Shared by"
  251. if (owner) {
  252. message = this._formatSharedByOwner(owner);
  253. }
  254. else if (recipients) {
  255. message = t('core', 'Shared with {recipients}', {recipients: recipients});
  256. }
  257. action.html(' <span>' + message + '</span>').prepend(img);
  258. if (owner) {
  259. action.find('.remoteOwner').tipsy({gravity: 's'});
  260. }
  261. }
  262. else {
  263. action.removeClass('permanent');
  264. action.html(' <span>'+ escapeHTML(t('core', 'Share'))+'</span>').prepend(img);
  265. }
  266. if (hasLink) {
  267. image = OC.imagePath('core', 'actions/public');
  268. }
  269. img.attr('src', image);
  270. },
  271. loadItem:function(itemType, itemSource) {
  272. var data = '';
  273. var checkReshare = true;
  274. if (typeof OC.Share.statuses[itemSource] === 'undefined') {
  275. // NOTE: Check does not always work and misses some shares, fix later
  276. var checkShares = true;
  277. } else {
  278. var checkShares = true;
  279. }
  280. $.ajax({type: 'GET', url: OC.filePath('core', 'ajax', 'share.php'), data: { fetch: 'getItem', itemType: itemType, itemSource: itemSource, checkReshare: checkReshare, checkShares: checkShares }, async: false, success: function(result) {
  281. if (result && result.status === 'success') {
  282. data = result.data;
  283. } else {
  284. data = false;
  285. }
  286. }});
  287. return data;
  288. },
  289. share:function(itemType, itemSource, shareType, shareWith, permissions, itemSourceName, expirationDate, callback) {
  290. // Add a fallback for old share() calls without expirationDate.
  291. // We should remove this in a later version,
  292. // after the Apps have been updated.
  293. if (typeof callback === 'undefined' &&
  294. typeof expirationDate === 'function') {
  295. callback = expirationDate;
  296. expirationDate = '';
  297. console.warn(
  298. "Call to 'OC.Share.share()' with too few arguments. " +
  299. "'expirationDate' was assumed to be 'callback'. " +
  300. "Please revisit the call and fix the list of arguments."
  301. );
  302. }
  303. return $.post(OC.filePath('core', 'ajax', 'share.php'),
  304. {
  305. action: 'share',
  306. itemType: itemType,
  307. itemSource: itemSource,
  308. shareType: shareType,
  309. shareWith: shareWith,
  310. permissions: permissions,
  311. itemSourceName: itemSourceName,
  312. expirationDate: expirationDate
  313. }, function (result) {
  314. if (result && result.status === 'success') {
  315. if (callback) {
  316. callback(result.data);
  317. }
  318. } else {
  319. if (result.data && result.data.message) {
  320. var msg = result.data.message;
  321. } else {
  322. var msg = t('core', 'Error');
  323. }
  324. OC.dialogs.alert(msg, t('core', 'Error while sharing'));
  325. }
  326. }
  327. );
  328. },
  329. unshare:function(itemType, itemSource, shareType, shareWith, callback) {
  330. $.post(OC.filePath('core', 'ajax', 'share.php'), { action: 'unshare', itemType: itemType, itemSource: itemSource, shareType: shareType, shareWith: shareWith }, function(result) {
  331. if (result && result.status === 'success') {
  332. if (callback) {
  333. callback();
  334. }
  335. } else {
  336. OC.dialogs.alert(t('core', 'Error while unsharing'), t('core', 'Error'));
  337. }
  338. });
  339. },
  340. setPermissions:function(itemType, itemSource, shareType, shareWith, permissions) {
  341. $.post(OC.filePath('core', 'ajax', 'share.php'), { action: 'setPermissions', itemType: itemType, itemSource: itemSource, shareType: shareType, shareWith: shareWith, permissions: permissions }, function(result) {
  342. if (!result || result.status !== 'success') {
  343. OC.dialogs.alert(t('core', 'Error while changing permissions'), t('core', 'Error'));
  344. }
  345. });
  346. },
  347. showDropDown:function(itemType, itemSource, appendTo, link, possiblePermissions, filename) {
  348. var data = OC.Share.loadItem(itemType, itemSource);
  349. var dropDownEl;
  350. var html = '<div id="dropdown" class="drop shareDropDown" data-item-type="'+itemType+'" data-item-source="'+itemSource+'">';
  351. if (data !== false && data.reshare !== false && data.reshare.uid_owner !== undefined) {
  352. html += '<span class="reshare">';
  353. if (oc_config.enable_avatars === true) {
  354. html += '<div class="avatar"></div> ';
  355. }
  356. if (data.reshare.share_type == OC.Share.SHARE_TYPE_GROUP) {
  357. html += t('core', 'Shared with you and the group {group} by {owner}', {group: data.reshare.share_with, owner: data.reshare.displayname_owner});
  358. } else {
  359. html += t('core', 'Shared with you by {owner}', {owner: data.reshare.displayname_owner});
  360. }
  361. html += '</span><br />';
  362. // reduce possible permissions to what the original share allowed
  363. possiblePermissions = possiblePermissions & data.reshare.permissions;
  364. }
  365. if (possiblePermissions & OC.PERMISSION_SHARE) {
  366. // Determine the Allow Public Upload status.
  367. // Used later on to determine if the
  368. // respective checkbox should be checked or
  369. // not.
  370. var publicUploadEnabled = $('#filestable').data('allow-public-upload');
  371. if (typeof publicUploadEnabled == 'undefined') {
  372. publicUploadEnabled = 'no';
  373. }
  374. var allowPublicUploadStatus = false;
  375. $.each(data.shares, function(key, value) {
  376. if (value.share_type === OC.Share.SHARE_TYPE_LINK) {
  377. allowPublicUploadStatus = (value.permissions & OC.PERMISSION_CREATE) ? true : false;
  378. return true;
  379. }
  380. });
  381. html += '<label for="shareWith" class="hidden-visually">'+t('core', 'Share')+'</label>';
  382. html += '<input id="shareWith" type="text" placeholder="'+t('core', 'Share with user or group …')+'" />';
  383. html += '<span class="shareWithLoading icon-loading-small hidden"></span>';
  384. html += '<ul id="shareWithList">';
  385. html += '</ul>';
  386. var linksAllowed = $('#allowShareWithLink').val() === 'yes';
  387. if (link && linksAllowed) {
  388. html += '<div id="link" class="linkShare">';
  389. html += '<span class="icon-loading-small hidden"></span>';
  390. html += '<input type="checkbox" name="linkCheckbox" id="linkCheckbox" value="1" /><label for="linkCheckbox">'+t('core', 'Share link')+'</label>';
  391. html += '<br />';
  392. var defaultExpireMessage = '';
  393. if ((itemType === 'folder' || itemType === 'file') && oc_appconfig.core.defaultExpireDateEnforced) {
  394. defaultExpireMessage = t('core', 'The public link will expire no later than {days} days after it is created', {'days': oc_appconfig.core.defaultExpireDate}) + '<br/>';
  395. }
  396. html += '<label for="linkText" class="hidden-visually">'+t('core', 'Link')+'</label>';
  397. html += '<input id="linkText" type="text" readonly="readonly" />';
  398. html += '<input type="checkbox" name="showPassword" id="showPassword" value="1" style="display:none;" /><label for="showPassword" style="display:none;">'+t('core', 'Password protect')+'</label>';
  399. html += '<div id="linkPass">';
  400. html += '<label for="linkPassText" class="hidden-visually">'+t('core', 'Password')+'</label>';
  401. html += '<input id="linkPassText" type="password" placeholder="'+t('core', 'Choose a password for the public link')+'" />';
  402. html += '<span class="icon-loading-small hidden"></span>';
  403. html += '</div>';
  404. if (itemType === 'folder' && (possiblePermissions & OC.PERMISSION_CREATE) && publicUploadEnabled === 'yes') {
  405. html += '<div id="allowPublicUploadWrapper" style="display:none;">';
  406. html += '<span class="icon-loading-small hidden"></span>';
  407. html += '<input type="checkbox" value="1" name="allowPublicUpload" id="sharingDialogAllowPublicUpload"' + ((allowPublicUploadStatus) ? 'checked="checked"' : '') + ' />';
  408. html += '<label for="sharingDialogAllowPublicUpload">' + t('core', 'Allow editing') + '</label>';
  409. html += '</div>';
  410. }
  411. html += '</div>';
  412. var mailPublicNotificationEnabled = $('input:hidden[name=mailPublicNotificationEnabled]').val();
  413. if (mailPublicNotificationEnabled === 'yes') {
  414. html += '<form id="emailPrivateLink">';
  415. html += '<input id="email" style="display:none; width:62%;" value="" placeholder="'+t('core', 'Email link to person')+'" type="text" />';
  416. html += '<input id="emailButton" style="display:none;" type="submit" value="'+t('core', 'Send')+'" />';
  417. html += '</form>';
  418. }
  419. }
  420. html += '<div id="expiration">';
  421. html += '<input type="checkbox" name="expirationCheckbox" id="expirationCheckbox" value="1" /><label for="expirationCheckbox">'+t('core', 'Set expiration date')+'</label>';
  422. html += '<label for="expirationDate" class="hidden-visually">'+t('core', 'Expiration')+'</label>';
  423. html += '<input id="expirationDate" type="text" placeholder="'+t('core', 'Expiration date')+'" style="display:none; width:90%;" />';
  424. html += '<em id="defaultExpireMessage">'+defaultExpireMessage+'</em>';
  425. html += '</div>';
  426. dropDownEl = $(html);
  427. dropDownEl = dropDownEl.appendTo(appendTo);
  428. //Get owner avatars
  429. if (oc_config.enable_avatars === true && data !== false && data.reshare !== false && data.reshare.uid_owner !== undefined) {
  430. dropDownEl.find(".avatar").avatar(data.reshare.uid_owner, 32);
  431. }
  432. // Reset item shares
  433. OC.Share.itemShares = [];
  434. OC.Share.currentShares = {};
  435. if (data.shares) {
  436. $.each(data.shares, function(index, share) {
  437. if (share.share_type == OC.Share.SHARE_TYPE_LINK) {
  438. if (itemSource === share.file_source || itemSource === share.item_source) {
  439. OC.Share.showLink(share.token, share.share_with, itemSource);
  440. }
  441. } else {
  442. if (share.collection) {
  443. OC.Share.addShareWith(share.share_type, share.share_with, share.share_with_displayname, share.permissions, possiblePermissions, share.mail_send, share.collection);
  444. } else {
  445. if (share.share_type === OC.Share.SHARE_TYPE_REMOTE) {
  446. OC.Share.addShareWith(share.share_type, share.share_with, share.share_with_displayname, share.permissions, OC.PERMISSION_READ | OC.PERMISSION_UPDATE | OC.PERMISSION_CREATE, share.mail_send, false);
  447. } else {
  448. OC.Share.addShareWith(share.share_type, share.share_with, share.share_with_displayname, share.permissions, possiblePermissions, share.mail_send, false);
  449. }
  450. }
  451. }
  452. if (share.expiration != null) {
  453. OC.Share.showExpirationDate(share.expiration, share.stime);
  454. }
  455. });
  456. }
  457. $('#shareWith').autocomplete({minLength: 2, delay: 750, source: function(search, response) {
  458. var $loading = $('#dropdown .shareWithLoading');
  459. $loading.removeClass('hidden');
  460. $.get(OC.filePath('core', 'ajax', 'share.php'), { fetch: 'getShareWith', search: search.term.trim(), itemShares: OC.Share.itemShares, itemType: itemType }, function(result) {
  461. $loading.addClass('hidden');
  462. if (result.status == 'success' && result.data.length > 0) {
  463. $( "#shareWith" ).autocomplete( "option", "autoFocus", true );
  464. response(result.data);
  465. } else {
  466. response();
  467. }
  468. });
  469. },
  470. focus: function(event, focused) {
  471. event.preventDefault();
  472. },
  473. select: function(event, selected) {
  474. event.stopPropagation();
  475. var $dropDown = $('#dropdown');
  476. var itemType = $dropDown.data('item-type');
  477. var itemSource = $dropDown.data('item-source');
  478. var itemSourceName = $dropDown.data('item-source-name');
  479. var expirationDate = '';
  480. if ( $('#expirationCheckbox').is(':checked') === true ) {
  481. expirationDate = $( "#expirationDate" ).val();
  482. }
  483. var shareType = selected.item.value.shareType;
  484. var shareWith = selected.item.value.shareWith;
  485. $(this).val(shareWith);
  486. // Default permissions are Edit (CRUD) and Share
  487. // Check if these permissions are possible
  488. var permissions = OC.PERMISSION_READ;
  489. if (shareType === OC.Share.SHARE_TYPE_REMOTE) {
  490. permissions = OC.PERMISSION_CREATE | OC.PERMISSION_UPDATE | OC.PERMISSION_READ;
  491. } else {
  492. if (possiblePermissions & OC.PERMISSION_UPDATE) {
  493. permissions = permissions | OC.PERMISSION_UPDATE;
  494. }
  495. if (possiblePermissions & OC.PERMISSION_CREATE) {
  496. permissions = permissions | OC.PERMISSION_CREATE;
  497. }
  498. if (possiblePermissions & OC.PERMISSION_DELETE) {
  499. permissions = permissions | OC.PERMISSION_DELETE;
  500. }
  501. if (oc_appconfig.core.resharingAllowed && (possiblePermissions & OC.PERMISSION_SHARE)) {
  502. permissions = permissions | OC.PERMISSION_SHARE;
  503. }
  504. }
  505. var $input = $(this);
  506. var $loading = $dropDown.find('.shareWithLoading');
  507. $loading.removeClass('hidden');
  508. $input.val(t('core', 'Adding user...'));
  509. $input.prop('disabled', true);
  510. OC.Share.share(itemType, itemSource, shareType, shareWith, permissions, itemSourceName, expirationDate, function() {
  511. $input.prop('disabled', false);
  512. $loading.addClass('hidden');
  513. var posPermissions = possiblePermissions;
  514. if (shareType === OC.Share.SHARE_TYPE_REMOTE) {
  515. posPermissions = permissions;
  516. }
  517. OC.Share.addShareWith(shareType, shareWith, selected.item.label, permissions, posPermissions);
  518. $('#shareWith').val('');
  519. $('#dropdown').trigger(new $.Event('sharesChanged', {shares: OC.Share.currentShares}));
  520. OC.Share.updateIcon(itemType, itemSource);
  521. });
  522. return false;
  523. }
  524. })
  525. // customize internal _renderItem function to display groups and users differently
  526. .data("ui-autocomplete")._renderItem = function( ul, item ) {
  527. var insert = $( "<a>" );
  528. var text = item.label;
  529. if (item.value.shareType === OC.Share.SHARE_TYPE_GROUP) {
  530. text = text + ' ('+t('core', 'group')+')';
  531. } else if (item.value.shareType === OC.Share.SHARE_TYPE_REMOTE) {
  532. text = text + ' ('+t('core', 'remote')+')';
  533. }
  534. insert.text( text );
  535. if(item.value.shareType === OC.Share.SHARE_TYPE_GROUP) {
  536. insert = insert.wrapInner('<strong></strong>');
  537. }
  538. return $( "<li>" )
  539. .addClass((item.value.shareType === OC.Share.SHARE_TYPE_GROUP)?'group':'user')
  540. .append( insert )
  541. .appendTo( ul );
  542. };
  543. if (link && linksAllowed && $('#email').length != 0) {
  544. $('#email').autocomplete({
  545. minLength: 1,
  546. source: function (search, response) {
  547. $.get(OC.filePath('core', 'ajax', 'share.php'), { fetch: 'getShareWithEmail', search: search.term }, function(result) {
  548. if (result.status == 'success' && result.data.length > 0) {
  549. response(result.data);
  550. }
  551. });
  552. },
  553. select: function( event, item ) {
  554. $('#email').val(item.item.email);
  555. return false;
  556. }
  557. })
  558. .data("ui-autocomplete")._renderItem = function( ul, item ) {
  559. return $('<li>')
  560. .append('<a>' + escapeHTML(item.displayname) + "<br>" + escapeHTML(item.email) + '</a>' )
  561. .appendTo( ul );
  562. };
  563. }
  564. } else {
  565. html += '<input id="shareWith" type="text" placeholder="'+t('core', 'Resharing is not allowed')+'" style="width:90%;" disabled="disabled"/>';
  566. html += '</div>';
  567. dropDownEl = $(html);
  568. dropDownEl.appendTo(appendTo);
  569. }
  570. dropDownEl.attr('data-item-source-name', filename);
  571. $('#dropdown').slideDown(OC.menuSpeed, function() {
  572. OC.Share.droppedDown = true;
  573. });
  574. if ($('html').hasClass('lte9')){
  575. $('#dropdown input[placeholder]').placeholder();
  576. }
  577. $('#shareWith').focus();
  578. },
  579. hideDropDown:function(callback) {
  580. OC.Share.currentShares = null;
  581. $('#dropdown').slideUp(OC.menuSpeed, function() {
  582. OC.Share.droppedDown = false;
  583. $('#dropdown').remove();
  584. if (typeof FileActions !== 'undefined') {
  585. $('tr').removeClass('mouseOver');
  586. }
  587. if (callback) {
  588. callback.call();
  589. }
  590. });
  591. },
  592. addShareWith:function(shareType, shareWith, shareWithDisplayName, permissions, possiblePermissions, mailSend, collection) {
  593. var shareItem = {
  594. share_type: shareType,
  595. share_with: shareWith,
  596. share_with_displayname: shareWithDisplayName,
  597. permissions: permissions
  598. };
  599. if (shareType === OC.Share.SHARE_TYPE_GROUP) {
  600. shareWithDisplayName = shareWithDisplayName + " (" + t('core', 'group') + ')';
  601. }
  602. if (shareType === OC.Share.SHARE_TYPE_REMOTE) {
  603. shareWithDisplayName = shareWithDisplayName + " (" + t('core', 'remote') + ')';
  604. }
  605. if (!OC.Share.itemShares[shareType]) {
  606. OC.Share.itemShares[shareType] = [];
  607. }
  608. OC.Share.itemShares[shareType].push(shareWith);
  609. if (collection) {
  610. if (collection.item_type == 'file' || collection.item_type == 'folder') {
  611. var item = collection.path;
  612. } else {
  613. var item = collection.item_source;
  614. }
  615. var collectionList = $('#shareWithList li').filterAttr('data-collection', item);
  616. if (collectionList.length > 0) {
  617. $(collectionList).append(', '+shareWithDisplayName);
  618. } else {
  619. var html = '<li style="clear: both;" data-collection="'+item+'">'+t('core', 'Shared in {item} with {user}', {'item': item, user: shareWithDisplayName})+'</li>';
  620. $('#shareWithList').prepend(html);
  621. }
  622. } else {
  623. var editChecked = createChecked = updateChecked = deleteChecked = shareChecked = '';
  624. if (permissions & OC.PERMISSION_CREATE) {
  625. createChecked = 'checked="checked"';
  626. editChecked = 'checked="checked"';
  627. }
  628. if (permissions & OC.PERMISSION_UPDATE) {
  629. updateChecked = 'checked="checked"';
  630. editChecked = 'checked="checked"';
  631. }
  632. if (permissions & OC.PERMISSION_DELETE) {
  633. deleteChecked = 'checked="checked"';
  634. editChecked = 'checked="checked"';
  635. }
  636. if (permissions & OC.PERMISSION_SHARE) {
  637. shareChecked = 'checked="checked"';
  638. }
  639. var html = '<li style="clear: both;" data-share-type="'+escapeHTML(shareType)+'" data-share-with="'+escapeHTML(shareWith)+'" title="' + escapeHTML(shareWith) + '">';
  640. var showCrudsButton;
  641. html += '<a href="#" class="unshare"><img class="svg" alt="'+t('core', 'Unshare')+'" title="'+t('core', 'Unshare')+'" src="'+OC.imagePath('core', 'actions/delete')+'"/></a>';
  642. if (oc_config.enable_avatars === true) {
  643. html += '<div class="avatar"></div>';
  644. }
  645. html += '<span class="username">' + escapeHTML(shareWithDisplayName) + '</span>';
  646. var mailNotificationEnabled = $('input:hidden[name=mailNotificationEnabled]').val();
  647. if (mailNotificationEnabled === 'yes' && shareType !== OC.Share.SHARE_TYPE_REMOTE) {
  648. var checked = '';
  649. if (mailSend === '1') {
  650. checked = 'checked';
  651. }
  652. html += '<label><input type="checkbox" name="mailNotification" class="mailNotification" ' + checked + ' />'+t('core', 'notify by email')+'</label> ';
  653. }
  654. if (oc_appconfig.core.resharingAllowed && (possiblePermissions & OC.PERMISSION_SHARE)) {
  655. html += '<label><input id="canShare-'+escapeHTML(shareWith)+'" type="checkbox" name="share" class="permissions" '+shareChecked+' data-permissions="'+OC.PERMISSION_SHARE+'" />'+t('core', 'can share')+'</label>';
  656. }
  657. if (possiblePermissions & OC.PERMISSION_CREATE || possiblePermissions & OC.PERMISSION_UPDATE || possiblePermissions & OC.PERMISSION_DELETE) {
  658. html += '<label><input id="canEdit-'+escapeHTML(shareWith)+'" type="checkbox" name="edit" class="permissions" '+editChecked+' />'+t('core', 'can edit')+'</label>';
  659. }
  660. if (shareType !== OC.Share.SHARE_TYPE_REMOTE) {
  661. showCrudsButton = '<a href="#" class="showCruds"><img class="svg" alt="'+t('core', 'access control')+'" src="'+OC.imagePath('core', 'actions/triangle-s')+'"/></a>';
  662. }
  663. html += '<div class="cruds" style="display:none;">';
  664. if (possiblePermissions & OC.PERMISSION_CREATE) {
  665. html += '<label><input id="canCreate-' + escapeHTML(shareWith) + '" type="checkbox" name="create" class="permissions" ' + createChecked + ' data-permissions="' + OC.PERMISSION_CREATE + '"/>' + t('core', 'create') + '</label>';
  666. }
  667. if (possiblePermissions & OC.PERMISSION_UPDATE) {
  668. html += '<label><input id="canUpdate-' + escapeHTML(shareWith) + '" type="checkbox" name="update" class="permissions" ' + updateChecked + ' data-permissions="' + OC.PERMISSION_UPDATE + '"/>' + t('core', 'change') + '</label>';
  669. }
  670. if (possiblePermissions & OC.PERMISSION_DELETE) {
  671. html += '<label><input id="canDelete-' + escapeHTML(shareWith) + '" type="checkbox" name="delete" class="permissions" ' + deleteChecked + ' data-permissions="' + OC.PERMISSION_DELETE + '"/>' + t('core', 'delete') + '</label>';
  672. }
  673. html += '</div>';
  674. html += '</li>';
  675. html = $(html).appendTo('#shareWithList');
  676. if (oc_config.enable_avatars === true) {
  677. if (shareType === OC.Share.SHARE_TYPE_USER) {
  678. html.find('.avatar').avatar(escapeHTML(shareWith), 32);
  679. } else {
  680. //Add sharetype to generate different seed if there is a group and use with the same name
  681. html.find('.avatar').imageplaceholder(escapeHTML(shareWith) + ' ' + shareType);
  682. }
  683. }
  684. // insert cruds button into last label element
  685. var lastLabel = html.find('>label:last');
  686. if (lastLabel.exists()){
  687. lastLabel.append(showCrudsButton);
  688. }
  689. else{
  690. html.find('.cruds').before(showCrudsButton);
  691. }
  692. if (!OC.Share.currentShares[shareType]) {
  693. OC.Share.currentShares[shareType] = [];
  694. }
  695. OC.Share.currentShares[shareType].push(shareItem);
  696. }
  697. },
  698. showLink:function(token, password, itemSource) {
  699. OC.Share.itemShares[OC.Share.SHARE_TYPE_LINK] = true;
  700. $('#linkCheckbox').attr('checked', true);
  701. //check itemType
  702. var linkSharetype=$('#dropdown').data('item-type');
  703. if (! token) {
  704. //fallback to pre token link
  705. var filename = $('tr').filterAttr('data-id', String(itemSource)).data('file');
  706. var type = $('tr').filterAttr('data-id', String(itemSource)).data('type');
  707. if ($('#dir').val() == '/') {
  708. var file = $('#dir').val() + filename;
  709. } else {
  710. var file = $('#dir').val() + '/' + filename;
  711. }
  712. file = '/'+OC.currentUser+'/files'+file;
  713. // TODO: use oc webroot ?
  714. var link = parent.location.protocol+'//'+location.host+OC.linkTo('', 'public.php')+'?service=files&'+type+'='+encodeURIComponent(file);
  715. } else {
  716. //TODO add path param when showing a link to file in a subfolder of a public link share
  717. var service='';
  718. if(linkSharetype === 'folder' || linkSharetype === 'file'){
  719. service='files';
  720. }else{
  721. service=linkSharetype;
  722. }
  723. // TODO: use oc webroot ?
  724. if (service !== 'files') {
  725. var link = parent.location.protocol+'//'+location.host+OC.linkTo('', 'public.php')+'?service='+service+'&t='+token;
  726. } else {
  727. var link = parent.location.protocol+'//'+location.host+OC.generateUrl('/s/')+token;
  728. }
  729. }
  730. $('#linkText').val(link);
  731. $('#linkText').slideDown(OC.menuSpeed);
  732. $('#linkText').css('display','block');
  733. if (oc_appconfig.core.enforcePasswordForPublicLink === false || password === null) {
  734. $('#showPassword').show();
  735. $('#showPassword+label').show();
  736. }
  737. if (password != null) {
  738. $('#linkPass').slideDown(OC.menuSpeed);
  739. $('#showPassword').attr('checked', true);
  740. $('#linkPassText').attr('placeholder', '**********');
  741. }
  742. $('#expiration').show();
  743. $('#emailPrivateLink #email').show();
  744. $('#emailPrivateLink #emailButton').show();
  745. $('#allowPublicUploadWrapper').show();
  746. },
  747. hideLink:function() {
  748. $('#linkText').slideUp(OC.menuSpeed);
  749. $('#defaultExpireMessage').hide();
  750. $('#showPassword').hide();
  751. $('#showPassword+label').hide();
  752. $('#linkPass').slideUp(OC.menuSpeed);
  753. $('#emailPrivateLink #email').hide();
  754. $('#emailPrivateLink #emailButton').hide();
  755. $('#allowPublicUploadWrapper').hide();
  756. },
  757. dirname:function(path) {
  758. return path.replace(/\\/g,'/').replace(/\/[^\/]*$/, '');
  759. },
  760. /**
  761. * Displays the expiration date field
  762. *
  763. * @param {Date} date current expiration date
  764. * @param {int} [shareTime] share timestamp in seconds, defaults to now
  765. */
  766. showExpirationDate:function(date, shareTime) {
  767. var now = new Date();
  768. // min date should always be the next day
  769. var minDate = new Date();
  770. minDate.setDate(minDate.getDate()+1);
  771. var datePickerOptions = {
  772. minDate: minDate,
  773. maxDate: null
  774. };
  775. if (_.isNumber(shareTime)) {
  776. shareTime = new Date(shareTime * 1000);
  777. }
  778. if (!shareTime) {
  779. shareTime = now;
  780. }
  781. $('#expirationCheckbox').attr('checked', true);
  782. $('#expirationDate').val(date);
  783. $('#expirationDate').slideDown(OC.menuSpeed);
  784. $('#expirationDate').css('display','block');
  785. $('#expirationDate').datepicker({
  786. dateFormat : 'dd-mm-yy'
  787. });
  788. if (oc_appconfig.core.defaultExpireDateEnforced) {
  789. $('#expirationCheckbox').attr('disabled', true);
  790. shareTime = OC.Util.stripTime(shareTime).getTime();
  791. // max date is share date + X days
  792. datePickerOptions.maxDate = new Date(shareTime + oc_appconfig.core.defaultExpireDate * 24 * 3600 * 1000);
  793. }
  794. if(oc_appconfig.core.defaultExpireDateEnabled) {
  795. $('#defaultExpireMessage').slideDown(OC.menuSpeed);
  796. }
  797. $.datepicker.setDefaults(datePickerOptions);
  798. }
  799. };
  800. $(document).ready(function() {
  801. if(typeof monthNames != 'undefined'){
  802. // min date should always be the next day
  803. var minDate = new Date();
  804. minDate.setDate(minDate.getDate()+1);
  805. $.datepicker.setDefaults({
  806. monthNames: monthNames,
  807. monthNamesShort: $.map(monthNames, function(v) { return v.slice(0,3)+'.'; }),
  808. dayNames: dayNames,
  809. dayNamesMin: $.map(dayNames, function(v) { return v.slice(0,2); }),
  810. dayNamesShort: $.map(dayNames, function(v) { return v.slice(0,3)+'.'; }),
  811. firstDay: firstDay,
  812. minDate : minDate
  813. });
  814. }
  815. $(document).on('click', 'a.share', function(event) {
  816. event.stopPropagation();
  817. if ($(this).data('item-type') !== undefined && $(this).data('item') !== undefined) {
  818. var itemType = $(this).data('item-type');
  819. var itemSource = $(this).data('item');
  820. var appendTo = $(this).parent().parent();
  821. var link = false;
  822. var possiblePermissions = $(this).data('possible-permissions');
  823. if ($(this).data('link') !== undefined && $(this).data('link') == true) {
  824. link = true;
  825. }
  826. if (OC.Share.droppedDown) {
  827. if (itemSource != $('#dropdown').data('item')) {
  828. OC.Share.hideDropDown(function () {
  829. OC.Share.showDropDown(itemType, itemSource, appendTo, link, possiblePermissions);
  830. });
  831. } else {
  832. OC.Share.hideDropDown();
  833. }
  834. } else {
  835. OC.Share.showDropDown(itemType, itemSource, appendTo, link, possiblePermissions);
  836. }
  837. }
  838. });
  839. $(this).click(function(event) {
  840. var target = $(event.target);
  841. var isMatched = !target.is('.drop, .ui-datepicker-next, .ui-datepicker-prev, .ui-icon')
  842. && !target.closest('#ui-datepicker-div').length && !target.closest('.ui-autocomplete').length;
  843. if (OC.Share.droppedDown && isMatched && $('#dropdown').has(event.target).length === 0) {
  844. OC.Share.hideDropDown();
  845. }
  846. });
  847. $(document).on('click', '#dropdown .showCruds', function() {
  848. $(this).closest('li').find('.cruds').toggle();
  849. return false;
  850. });
  851. $(document).on('click', '#dropdown .unshare', function() {
  852. var $li = $(this).closest('li');
  853. var itemType = $('#dropdown').data('item-type');
  854. var itemSource = $('#dropdown').data('item-source');
  855. var shareType = $li.data('share-type');
  856. var shareWith = $li.attr('data-share-with');
  857. var $button = $(this);
  858. if (!$button.is('a')) {
  859. $button = $button.closest('a');
  860. }
  861. if ($button.hasClass('icon-loading-small')) {
  862. // deletion in progress
  863. return false;
  864. }
  865. $button.empty().addClass('icon-loading-small');
  866. OC.Share.unshare(itemType, itemSource, shareType, shareWith, function() {
  867. $li.remove();
  868. var index = OC.Share.itemShares[shareType].indexOf(shareWith);
  869. OC.Share.itemShares[shareType].splice(index, 1);
  870. // updated list of shares
  871. OC.Share.currentShares[shareType].splice(index, 1);
  872. $('#dropdown').trigger(new $.Event('sharesChanged', {shares: OC.Share.currentShares}));
  873. OC.Share.updateIcon(itemType, itemSource);
  874. if (typeof OC.Share.statuses[itemSource] === 'undefined') {
  875. $('#expiration').slideUp(OC.menuSpeed);
  876. }
  877. });
  878. return false;
  879. });
  880. $(document).on('change', '#dropdown .permissions', function() {
  881. var li = $(this).closest('li');
  882. if ($(this).attr('name') == 'edit') {
  883. var checkboxes = $('.permissions', li);
  884. var checked = $(this).is(':checked');
  885. // Check/uncheck Create, Update, and Delete checkboxes if Edit is checked/unck
  886. $(checkboxes).filter('input[name="create"]').attr('checked', checked);
  887. $(checkboxes).filter('input[name="update"]').attr('checked', checked);
  888. $(checkboxes).filter('input[name="delete"]').attr('checked', checked);
  889. } else {
  890. var checkboxes = $('.permissions', li);
  891. // Uncheck Edit if Create, Update, and Delete are not checked
  892. if (!$(this).is(':checked')
  893. && !$(checkboxes).filter('input[name="create"]').is(':checked')
  894. && !$(checkboxes).filter('input[name="update"]').is(':checked')
  895. && !$(checkboxes).filter('input[name="delete"]').is(':checked'))
  896. {
  897. $(checkboxes).filter('input[name="edit"]').attr('checked', false);
  898. // Check Edit if Create, Update, or Delete is checked
  899. } else if (($(this).attr('name') == 'create'
  900. || $(this).attr('name') == 'update'
  901. || $(this).attr('name') == 'delete'))
  902. {
  903. $(checkboxes).filter('input[name="edit"]').attr('checked', true);
  904. }
  905. }
  906. var permissions = OC.PERMISSION_READ;
  907. $(checkboxes).filter(':not(input[name="edit"])').filter(':checked').each(function(index, checkbox) {
  908. permissions |= $(checkbox).data('permissions');
  909. });
  910. OC.Share.setPermissions($('#dropdown').data('item-type'),
  911. $('#dropdown').data('item-source'),
  912. li.data('share-type'),
  913. li.attr('data-share-with'),
  914. permissions);
  915. });
  916. $(document).on('change', '#dropdown #linkCheckbox', function() {
  917. var $dropDown = $('#dropdown');
  918. var itemType = $dropDown.data('item-type');
  919. var itemSource = $dropDown.data('item-source');
  920. var itemSourceName = $dropDown.data('item-source-name');
  921. var $loading = $dropDown.find('#link .icon-loading-small');
  922. var $button = $(this);
  923. if (!$loading.hasClass('hidden')) {
  924. // already in progress
  925. return false;
  926. }
  927. if (this.checked) {
  928. var expireDateString = '';
  929. if (oc_appconfig.core.defaultExpireDateEnabled) {
  930. var date = new Date().getTime();
  931. var expireAfterMs = oc_appconfig.core.defaultExpireDate * 24 * 60 * 60 * 1000;
  932. var expireDate = new Date(date + expireAfterMs);
  933. var month = expireDate.getMonth() + 1;
  934. var year = expireDate.getFullYear();
  935. var day = expireDate.getDate();
  936. expireDateString = year + "-" + month + '-' + day + ' 00:00:00';
  937. }
  938. // Create a link
  939. if (oc_appconfig.core.enforcePasswordForPublicLink === false) {
  940. $loading.removeClass('hidden');
  941. $button.addClass('hidden');
  942. $button.prop('disabled', true);
  943. OC.Share.share(itemType, itemSource, OC.Share.SHARE_TYPE_LINK, '', OC.PERMISSION_READ, itemSourceName, expireDateString, function(data) {
  944. $loading.addClass('hidden');
  945. $button.removeClass('hidden');
  946. $button.prop('disabled', false);
  947. OC.Share.showLink(data.token, null, itemSource);
  948. $('#dropdown').trigger(new $.Event('sharesChanged', {shares: OC.Share.currentShares}));
  949. OC.Share.updateIcon(itemType, itemSource);
  950. });
  951. } else {
  952. $('#linkPass').slideToggle(OC.menuSpeed);
  953. // TODO drop with IE8 drop
  954. if(html.hasClass('ie8')) {
  955. $('#linkPassText').attr('placeholder', null);
  956. $('#linkPassText').val('');
  957. }
  958. $('#linkPassText').focus();
  959. }
  960. if (expireDateString !== '') {
  961. OC.Share.showExpirationDate(expireDateString);
  962. }
  963. } else {
  964. // Delete private link
  965. OC.Share.hideLink();
  966. $('#expiration').slideUp(OC.menuSpeed);
  967. if ($('#linkText').val() !== '') {
  968. $loading.removeClass('hidden');
  969. $button.addClass('hidden');
  970. $button.prop('disabled', true);
  971. OC.Share.unshare(itemType, itemSource, OC.Share.SHARE_TYPE_LINK, '', function() {
  972. $loading.addClass('hidden');
  973. $button.removeClass('hidden');
  974. $button.prop('disabled', false);
  975. OC.Share.itemShares[OC.Share.SHARE_TYPE_LINK] = false;
  976. $('#dropdown').trigger(new $.Event('sharesChanged', {shares: OC.Share.currentShares}));
  977. OC.Share.updateIcon(itemType, itemSource);
  978. if (typeof OC.Share.statuses[itemSource] === 'undefined') {
  979. $('#expiration').slideUp(OC.menuSpeed);
  980. }
  981. });
  982. }
  983. }
  984. });
  985. $(document).on('click', '#dropdown #linkText', function() {
  986. $(this).focus();
  987. $(this).select();
  988. });
  989. // Handle the Allow Public Upload Checkbox
  990. $(document).on('click', '#sharingDialogAllowPublicUpload', function() {
  991. // Gather data
  992. var $dropDown = $('#dropdown');
  993. var allowPublicUpload = $(this).is(':checked');
  994. var itemType = $dropDown.data('item-type');
  995. var itemSource = $dropDown.data('item-source');
  996. var itemSourceName = $dropDown.data('item-source-name');
  997. var expirationDate = '';
  998. if ($('#expirationCheckbox').is(':checked') === true) {
  999. expirationDate = $( "#expirationDate" ).val();
  1000. }
  1001. var permissions = 0;
  1002. var $button = $(this);
  1003. var $loading = $dropDown.find('#allowPublicUploadWrapper .icon-loading-small');
  1004. if (!$loading.hasClass('hidden')) {
  1005. // already in progress
  1006. return false;
  1007. }
  1008. // Calculate permissions
  1009. if (allowPublicUpload) {
  1010. permissions = OC.PERMISSION_UPDATE + OC.PERMISSION_CREATE + OC.PERMISSION_READ;
  1011. } else {
  1012. permissions = OC.PERMISSION_READ;
  1013. }
  1014. // Update the share information
  1015. $button.addClass('hidden');
  1016. $button.prop('disabled', true);
  1017. $loading.removeClass('hidden');
  1018. OC.Share.share(itemType, itemSource, OC.Share.SHARE_TYPE_LINK, '', permissions, itemSourceName, expirationDate, function(data) {
  1019. $loading.addClass('hidden');
  1020. $button.removeClass('hidden');
  1021. $button.prop('disabled', false);
  1022. });
  1023. });
  1024. $(document).on('click', '#dropdown #showPassword', function() {
  1025. $('#linkPass').slideToggle(OC.menuSpeed);
  1026. if (!$('#showPassword').is(':checked') ) {
  1027. var itemType = $('#dropdown').data('item-type');
  1028. var itemSource = $('#dropdown').data('item-source');
  1029. var itemSourceName = $('#dropdown').data('item-source-name');
  1030. var allowPublicUpload = $('#sharingDialogAllowPublicUpload').is(':checked');
  1031. var permissions = 0;
  1032. var $loading = $('#showPassword .icon-loading-small');
  1033. // Calculate permissions
  1034. if (allowPublicUpload) {
  1035. permissions = OC.PERMISSION_UPDATE + OC.PERMISSION_CREATE + OC.PERMISSION_READ;
  1036. } else {
  1037. permissions = OC.PERMISSION_READ;
  1038. }
  1039. $loading.removeClass('hidden');
  1040. OC.Share.share(itemType, itemSource, OC.Share.SHARE_TYPE_LINK, '', permissions, itemSourceName).then(function() {
  1041. $loading.addClass('hidden');
  1042. $('#linkPassText').attr('placeholder', t('core', 'Choose a password for the public link'));
  1043. });
  1044. } else {
  1045. $('#linkPassText').focus();
  1046. }
  1047. });
  1048. $(document).on('focusout keyup', '#dropdown #linkPassText', function(event) {
  1049. var linkPassText = $('#linkPassText');
  1050. if ( linkPassText.val() != '' && (event.type == 'focusout' || event.keyCode == 13) ) {
  1051. var allowPublicUpload = $('#sharingDialogAllowPublicUpload').is(':checked');
  1052. var dropDown = $('#dropdown');
  1053. var itemType = dropDown.data('item-type');
  1054. var itemSource = dropDown.data('item-source');
  1055. var itemSourceName = $('#dropdown').data('item-source-name');
  1056. var permissions = 0;
  1057. var $loading = dropDown.find('#linkPass .icon-loading-small');
  1058. // Calculate permissions
  1059. if (allowPublicUpload) {
  1060. permissions = OC.PERMISSION_UPDATE + OC.PERMISSION_CREATE + OC.PERMISSION_READ;
  1061. } else {
  1062. permissions = OC.PERMISSION_READ;
  1063. }
  1064. $loading.removeClass('hidden');
  1065. OC.Share.share(itemType, itemSource, OC.Share.SHARE_TYPE_LINK, $('#linkPassText').val(), permissions, itemSourceName, function(data) {
  1066. $loading.addClass('hidden');
  1067. linkPassText.val('');
  1068. linkPassText.attr('placeholder', t('core', 'Password protected'));
  1069. if (oc_appconfig.core.enforcePasswordForPublicLink) {
  1070. OC.Share.showLink(data.token, "password set", itemSource);
  1071. OC.Share.updateIcon(itemType, itemSource);
  1072. }
  1073. });
  1074. }
  1075. });
  1076. $(document).on('click', '#dropdown #expirationCheckbox', function() {
  1077. if (this.checked) {
  1078. OC.Share.showExpirationDate('');
  1079. } else {
  1080. var itemType = $('#dropdown').data('item-type');
  1081. var itemSource = $('#dropdown').data('item-source');
  1082. $.post(OC.filePath('core', 'ajax', 'share.php'), { action: 'setExpirationDate', itemType: itemType, itemSource: itemSource, date: '' }, function(result) {
  1083. if (!result || result.status !== 'success') {
  1084. OC.dialogs.alert(t('core', 'Error unsetting expiration date'), t('core', 'Error'));
  1085. }
  1086. $('#expirationDate').slideUp(OC.menuSpeed);
  1087. if (oc_appconfig.core.defaultExpireDateEnforced === false) {
  1088. $('#defaultExpireMessage').slideDown(OC.menuSpeed);
  1089. }
  1090. });
  1091. }
  1092. });
  1093. $(document).on('change', '#dropdown #expirationDate', function() {
  1094. var itemType = $('#dropdown').data('item-type');
  1095. var itemSource = $('#dropdown').data('item-source');
  1096. $(this).tipsy('hide');
  1097. $(this).removeClass('error');
  1098. $.post(OC.filePath('core', 'ajax', 'share.php'), { action: 'setExpirationDate', itemType: itemType, itemSource: itemSource, date: $(this).val() }, function(result) {
  1099. if (!result || result.status !== 'success') {
  1100. var expirationDateField = $('#dropdown #expirationDate');
  1101. if (!result.data.message) {
  1102. expirationDateField.attr('original-title', t('core', 'Error setting expiration date'));
  1103. } else {
  1104. expirationDateField.attr('original-title', result.data.message);
  1105. }
  1106. expirationDateField.tipsy({gravity: 'n', fade: true});
  1107. expirationDateField.tipsy('show');
  1108. expirationDateField.addClass('error');
  1109. } else {
  1110. if (oc_appconfig.core.defaultExpireDateEnforced === 'no') {
  1111. $('#defaultExpireMessage').slideUp(OC.menuSpeed);
  1112. }
  1113. }
  1114. });
  1115. });
  1116. $(document).on('submit', '#dropdown #emailPrivateLink', function(event) {
  1117. event.preventDefault();
  1118. var link = $('#linkText').val();
  1119. var itemType = $('#dropdown').data('item-type');
  1120. var itemSource = $('#dropdown').data('item-source');
  1121. var file = $('tr').filterAttr('data-id', String(itemSource)).data('file');
  1122. var email = $('#email').val();
  1123. var expirationDate = '';
  1124. if ( $('#expirationCheckbox').is(':checked') === true ) {
  1125. expirationDate = $( "#expirationDate" ).val();
  1126. }
  1127. if (email != '') {
  1128. $('#email').prop('disabled', true);
  1129. $('#email').val(t('core', 'Sending ...'));
  1130. $('#emailButton').prop('disabled', true);
  1131. $.post(OC.filePath('core', 'ajax', 'share.php'), { action: 'email', toaddress: email, link: link, itemType: itemType, itemSource: itemSource, file: file, expiration: expirationDate},
  1132. function(result) {
  1133. $('#email').prop('disabled', false);
  1134. $('#emailButton').prop('disabled', false);
  1135. if (result && result.status == 'success') {
  1136. $('#email').css('font-weight', 'bold').val(t('core','Email sent'));
  1137. setTimeout(function() {
  1138. $('#email').css('font-weight', 'normal').val('');
  1139. }, 2000);
  1140. } else {
  1141. OC.dialogs.alert(result.data.message, t('core', 'Error while sharing'));
  1142. }
  1143. });
  1144. }
  1145. });
  1146. $(document).on('click', '#dropdown input[name=mailNotification]', function() {
  1147. var $li = $(this).closest('li');
  1148. var itemType = $('#dropdown').data('item-type');
  1149. var itemSource = $('#dropdown').data('item-source');
  1150. var action = '';
  1151. if (this.checked) {
  1152. action = 'informRecipients';
  1153. } else {
  1154. action = 'informRecipientsDisabled';
  1155. }
  1156. var shareType = $li.data('share-type');
  1157. var shareWith = $li.attr('data-share-with');
  1158. $.post(OC.filePath('core', 'ajax', 'share.php'), {action: action, recipient: shareWith, shareType: shareType, itemSource: itemSource, itemType: itemType}, function(result) {
  1159. if (result.status !== 'success') {
  1160. OC.dialogs.alert(t('core', result.data.message), t('core', 'Warning'));
  1161. }
  1162. });
  1163. });
  1164. });