share.js 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229
  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. if (data.reshare.share_type == OC.Share.SHARE_TYPE_GROUP) {
  353. html += '<span class="reshare">'+t('core', 'Shared with you and the group {group} by {owner}', {group: data.reshare.share_with, owner: data.reshare.displayname_owner});
  354. if (oc_config.enable_avatars === true) {
  355. html += ' <div id="avatar-share-owner" style="display: inline-block"></div>';
  356. }
  357. html += '</span>';
  358. } else {
  359. html += '<span class="reshare">'+t('core', 'Shared with you by {owner}', {owner: data.reshare.displayname_owner});
  360. if (oc_config.enable_avatars === true) {
  361. html += ' <div id="avatar-share-owner" style="display: inline-block"></div>';
  362. }
  363. html += '</span>';
  364. }
  365. html += '<br />';
  366. // reduce possible permissions to what the original share allowed
  367. possiblePermissions = possiblePermissions & data.reshare.permissions;
  368. }
  369. if (possiblePermissions & OC.PERMISSION_SHARE) {
  370. // Determine the Allow Public Upload status.
  371. // Used later on to determine if the
  372. // respective checkbox should be checked or
  373. // not.
  374. var publicUploadEnabled = $('#filestable').data('allow-public-upload');
  375. if (typeof publicUploadEnabled == 'undefined') {
  376. publicUploadEnabled = 'no';
  377. }
  378. var allowPublicUploadStatus = false;
  379. $.each(data.shares, function(key, value) {
  380. if (value.share_type === OC.Share.SHARE_TYPE_LINK) {
  381. allowPublicUploadStatus = (value.permissions & OC.PERMISSION_CREATE) ? true : false;
  382. return true;
  383. }
  384. });
  385. html += '<label for="shareWith" class="hidden-visually">'+t('core', 'Share')+'</label>';
  386. html += '<input id="shareWith" type="text" placeholder="'+t('core', 'Share with user or group …')+'" />';
  387. html += '<span class="shareWithLoading icon-loading-small hidden"></span>';
  388. html += '<ul id="shareWithList">';
  389. html += '</ul>';
  390. var linksAllowed = $('#allowShareWithLink').val() === 'yes';
  391. if (link && linksAllowed) {
  392. html += '<div id="link" class="linkShare">';
  393. html += '<span class="icon-loading-small hidden"></span>';
  394. html += '<input type="checkbox" name="linkCheckbox" id="linkCheckbox" value="1" /><label for="linkCheckbox">'+t('core', 'Share link')+'</label>';
  395. html += '<br />';
  396. var defaultExpireMessage = '';
  397. if ((itemType === 'folder' || itemType === 'file') && oc_appconfig.core.defaultExpireDateEnforced) {
  398. defaultExpireMessage = t('core', 'The public link will expire no later than {days} days after it is created', {'days': oc_appconfig.core.defaultExpireDate}) + '<br/>';
  399. }
  400. html += '<label for="linkText" class="hidden-visually">'+t('core', 'Link')+'</label>';
  401. html += '<input id="linkText" type="text" readonly="readonly" />';
  402. html += '<input type="checkbox" name="showPassword" id="showPassword" value="1" style="display:none;" /><label for="showPassword" style="display:none;">'+t('core', 'Password protect')+'</label>';
  403. html += '<div id="linkPass">';
  404. html += '<label for="linkPassText" class="hidden-visually">'+t('core', 'Password')+'</label>';
  405. html += '<input id="linkPassText" type="password" placeholder="'+t('core', 'Choose a password for the public link')+'" />';
  406. html += '<span class="icon-loading-small hidden"></span>';
  407. html += '</div>';
  408. if (itemType === 'folder' && (possiblePermissions & OC.PERMISSION_CREATE) && publicUploadEnabled === 'yes') {
  409. html += '<div id="allowPublicUploadWrapper" style="display:none;">';
  410. html += '<span class="icon-loading-small hidden"></span>';
  411. html += '<input type="checkbox" value="1" name="allowPublicUpload" id="sharingDialogAllowPublicUpload"' + ((allowPublicUploadStatus) ? 'checked="checked"' : '') + ' />';
  412. html += '<label for="sharingDialogAllowPublicUpload">' + t('core', 'Allow editing') + '</label>';
  413. html += '</div>';
  414. }
  415. html += '</div>';
  416. var mailPublicNotificationEnabled = $('input:hidden[name=mailPublicNotificationEnabled]').val();
  417. if (mailPublicNotificationEnabled === 'yes') {
  418. html += '<form id="emailPrivateLink">';
  419. html += '<input id="email" style="display:none; width:62%;" value="" placeholder="'+t('core', 'Email link to person')+'" type="text" />';
  420. html += '<input id="emailButton" style="display:none;" type="submit" value="'+t('core', 'Send')+'" />';
  421. html += '</form>';
  422. }
  423. }
  424. html += '<div id="expiration">';
  425. html += '<input type="checkbox" name="expirationCheckbox" id="expirationCheckbox" value="1" /><label for="expirationCheckbox">'+t('core', 'Set expiration date')+'</label>';
  426. html += '<label for="expirationDate" class="hidden-visually">'+t('core', 'Expiration')+'</label>';
  427. html += '<input id="expirationDate" type="text" placeholder="'+t('core', 'Expiration date')+'" style="display:none; width:90%;" />';
  428. html += '<em id="defaultExpireMessage">'+defaultExpireMessage+'</em>';
  429. html += '</div>';
  430. dropDownEl = $(html);
  431. dropDownEl = dropDownEl.appendTo(appendTo);
  432. //Get owner avatars
  433. if (oc_config.enable_avatars === true && data !== false && data.reshare !== false && data.reshare.uid_owner !== undefined) {
  434. $('#avatar-share-owner').avatar(data.reshare.uid_owner, 32);
  435. }
  436. // Reset item shares
  437. OC.Share.itemShares = [];
  438. OC.Share.currentShares = {};
  439. if (data.shares) {
  440. $.each(data.shares, function(index, share) {
  441. if (share.share_type == OC.Share.SHARE_TYPE_LINK) {
  442. if (itemSource === share.file_source || itemSource === share.item_source) {
  443. OC.Share.showLink(share.token, share.share_with, itemSource);
  444. }
  445. } else {
  446. if (share.collection) {
  447. OC.Share.addShareWith(share.share_type, share.share_with, share.share_with_displayname, share.permissions, possiblePermissions, share.mail_send, share.collection);
  448. } else {
  449. if (share.share_type === OC.Share.SHARE_TYPE_REMOTE) {
  450. 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);
  451. } else {
  452. OC.Share.addShareWith(share.share_type, share.share_with, share.share_with_displayname, share.permissions, possiblePermissions, share.mail_send, false);
  453. }
  454. }
  455. }
  456. if (share.expiration != null) {
  457. OC.Share.showExpirationDate(share.expiration, share.stime);
  458. }
  459. });
  460. }
  461. $('#shareWith').autocomplete({minLength: 2, delay: 750, source: function(search, response) {
  462. var $loading = $('#dropdown .shareWithLoading');
  463. $loading.removeClass('hidden');
  464. $.get(OC.filePath('core', 'ajax', 'share.php'), { fetch: 'getShareWith', search: search.term.trim(), itemShares: OC.Share.itemShares, itemType: itemType }, function(result) {
  465. $loading.addClass('hidden');
  466. if (result.status == 'success' && result.data.length > 0) {
  467. $( "#shareWith" ).autocomplete( "option", "autoFocus", true );
  468. response(result.data);
  469. } else {
  470. response();
  471. }
  472. });
  473. },
  474. focus: function(event, focused) {
  475. event.preventDefault();
  476. },
  477. select: function(event, selected) {
  478. event.stopPropagation();
  479. var $dropDown = $('#dropdown');
  480. var itemType = $dropDown.data('item-type');
  481. var itemSource = $dropDown.data('item-source');
  482. var itemSourceName = $dropDown.data('item-source-name');
  483. var expirationDate = '';
  484. if ( $('#expirationCheckbox').is(':checked') === true ) {
  485. expirationDate = $( "#expirationDate" ).val();
  486. }
  487. var shareType = selected.item.value.shareType;
  488. var shareWith = selected.item.value.shareWith;
  489. $(this).val(shareWith);
  490. // Default permissions are Edit (CRUD) and Share
  491. // Check if these permissions are possible
  492. var permissions = OC.PERMISSION_READ;
  493. if (shareType === OC.Share.SHARE_TYPE_REMOTE) {
  494. permissions = OC.PERMISSION_CREATE | OC.PERMISSION_UPDATE | OC.PERMISSION_READ;
  495. } else {
  496. if (possiblePermissions & OC.PERMISSION_UPDATE) {
  497. permissions = permissions | OC.PERMISSION_UPDATE;
  498. }
  499. if (possiblePermissions & OC.PERMISSION_CREATE) {
  500. permissions = permissions | OC.PERMISSION_CREATE;
  501. }
  502. if (possiblePermissions & OC.PERMISSION_DELETE) {
  503. permissions = permissions | OC.PERMISSION_DELETE;
  504. }
  505. if (oc_appconfig.core.resharingAllowed && (possiblePermissions & OC.PERMISSION_SHARE)) {
  506. permissions = permissions | OC.PERMISSION_SHARE;
  507. }
  508. }
  509. var $input = $(this);
  510. var $loading = $dropDown.find('.shareWithLoading');
  511. $loading.removeClass('hidden');
  512. $input.val(t('core', 'Adding user...'));
  513. $input.prop('disabled', true);
  514. OC.Share.share(itemType, itemSource, shareType, shareWith, permissions, itemSourceName, expirationDate, function() {
  515. $input.prop('disabled', false);
  516. $loading.addClass('hidden');
  517. var posPermissions = possiblePermissions;
  518. if (shareType === OC.Share.SHARE_TYPE_REMOTE) {
  519. posPermissions = permissions;
  520. }
  521. OC.Share.addShareWith(shareType, shareWith, selected.item.label, permissions, posPermissions);
  522. $('#shareWith').val('');
  523. $('#dropdown').trigger(new $.Event('sharesChanged', {shares: OC.Share.currentShares}));
  524. OC.Share.updateIcon(itemType, itemSource);
  525. });
  526. return false;
  527. }
  528. })
  529. // customize internal _renderItem function to display groups and users differently
  530. .data("ui-autocomplete")._renderItem = function( ul, item ) {
  531. var insert = $( "<a>" );
  532. var text = item.label;
  533. if (item.value.shareType === OC.Share.SHARE_TYPE_GROUP) {
  534. text = text + ' ('+t('core', 'group')+')';
  535. } else if (item.value.shareType === OC.Share.SHARE_TYPE_REMOTE) {
  536. text = text + ' ('+t('core', 'remote')+')';
  537. }
  538. insert.text( text );
  539. if(item.value.shareType === OC.Share.SHARE_TYPE_GROUP) {
  540. insert = insert.wrapInner('<strong></strong>');
  541. }
  542. return $( "<li>" )
  543. .addClass((item.value.shareType === OC.Share.SHARE_TYPE_GROUP)?'group':'user')
  544. .append( insert )
  545. .appendTo( ul );
  546. };
  547. if (link && linksAllowed && $('#email').length != 0) {
  548. $('#email').autocomplete({
  549. minLength: 1,
  550. source: function (search, response) {
  551. $.get(OC.filePath('core', 'ajax', 'share.php'), { fetch: 'getShareWithEmail', search: search.term }, function(result) {
  552. if (result.status == 'success' && result.data.length > 0) {
  553. response(result.data);
  554. }
  555. });
  556. },
  557. select: function( event, item ) {
  558. $('#email').val(item.item.email);
  559. return false;
  560. }
  561. })
  562. .data("ui-autocomplete")._renderItem = function( ul, item ) {
  563. return $('<li>')
  564. .append('<a>' + escapeHTML(item.displayname) + "<br>" + escapeHTML(item.email) + '</a>' )
  565. .appendTo( ul );
  566. };
  567. }
  568. } else {
  569. html += '<input id="shareWith" type="text" placeholder="'+t('core', 'Resharing is not allowed')+'" style="width:90%;" disabled="disabled"/>';
  570. html += '</div>';
  571. dropDownEl = $(html);
  572. dropDownEl.appendTo(appendTo);
  573. }
  574. dropDownEl.attr('data-item-source-name', filename);
  575. $('#dropdown').show('blind', function() {
  576. OC.Share.droppedDown = true;
  577. });
  578. if ($('html').hasClass('lte9')){
  579. $('#dropdown input[placeholder]').placeholder();
  580. }
  581. $('#shareWith').focus();
  582. },
  583. hideDropDown:function(callback) {
  584. OC.Share.currentShares = null;
  585. $('#dropdown').hide('blind', function() {
  586. OC.Share.droppedDown = false;
  587. $('#dropdown').remove();
  588. if (typeof FileActions !== 'undefined') {
  589. $('tr').removeClass('mouseOver');
  590. }
  591. if (callback) {
  592. callback.call();
  593. }
  594. });
  595. },
  596. addShareWith:function(shareType, shareWith, shareWithDisplayName, permissions, possiblePermissions, mailSend, collection) {
  597. var shareItem = {
  598. share_type: shareType,
  599. share_with: shareWith,
  600. share_with_displayname: shareWithDisplayName,
  601. permissions: permissions
  602. };
  603. if (shareType === OC.Share.SHARE_TYPE_GROUP) {
  604. shareWithDisplayName = shareWithDisplayName + " (" + t('core', 'group') + ')';
  605. }
  606. if (shareType === OC.Share.SHARE_TYPE_REMOTE) {
  607. shareWithDisplayName = shareWithDisplayName + " (" + t('core', 'remote') + ')';
  608. }
  609. if (!OC.Share.itemShares[shareType]) {
  610. OC.Share.itemShares[shareType] = [];
  611. }
  612. OC.Share.itemShares[shareType].push(shareWith);
  613. if (collection) {
  614. if (collection.item_type == 'file' || collection.item_type == 'folder') {
  615. var item = collection.path;
  616. } else {
  617. var item = collection.item_source;
  618. }
  619. var collectionList = $('#shareWithList li').filterAttr('data-collection', item);
  620. if (collectionList.length > 0) {
  621. $(collectionList).append(', '+shareWithDisplayName);
  622. } else {
  623. var html = '<li style="clear: both;" data-collection="'+item+'">'+t('core', 'Shared in {item} with {user}', {'item': item, user: shareWithDisplayName})+'</li>';
  624. $('#shareWithList').prepend(html);
  625. }
  626. } else {
  627. var editChecked = createChecked = updateChecked = deleteChecked = shareChecked = '';
  628. if (permissions & OC.PERMISSION_CREATE) {
  629. createChecked = 'checked="checked"';
  630. editChecked = 'checked="checked"';
  631. }
  632. if (permissions & OC.PERMISSION_UPDATE) {
  633. updateChecked = 'checked="checked"';
  634. editChecked = 'checked="checked"';
  635. }
  636. if (permissions & OC.PERMISSION_DELETE) {
  637. deleteChecked = 'checked="checked"';
  638. editChecked = 'checked="checked"';
  639. }
  640. if (permissions & OC.PERMISSION_SHARE) {
  641. shareChecked = 'checked="checked"';
  642. }
  643. var html = '<li style="clear: both;" data-share-type="'+escapeHTML(shareType)+'" data-share-with="'+escapeHTML(shareWith)+'" title="' + escapeHTML(shareWith) + '">';
  644. var showCrudsButton;
  645. html += '<a href="#" class="unshare"><img class="svg" alt="'+t('core', 'Unshare')+'" title="'+t('core', 'Unshare')+'" src="'+OC.imagePath('core', 'actions/delete')+'"/></a>';
  646. if (oc_config.enable_avatars === true) {
  647. if (shareType === OC.Share.SHARE_TYPE_USER) {
  648. html += '<div id="avatar-' + escapeHTML(shareWith) + '" class="avatar"></div>';
  649. } else {
  650. html += '<div class="avatar" style="padding-right: 32px"></div>';
  651. }
  652. }
  653. html += '<span class="username">' + escapeHTML(shareWithDisplayName) + '</span>';
  654. var mailNotificationEnabled = $('input:hidden[name=mailNotificationEnabled]').val();
  655. if (mailNotificationEnabled === 'yes' && shareType !== OC.Share.SHARE_TYPE_REMOTE) {
  656. var checked = '';
  657. if (mailSend === '1') {
  658. checked = 'checked';
  659. }
  660. html += '<label><input type="checkbox" name="mailNotification" class="mailNotification" ' + checked + ' />'+t('core', 'notify by email')+'</label> ';
  661. }
  662. if (oc_appconfig.core.resharingAllowed && (possiblePermissions & OC.PERMISSION_SHARE)) {
  663. html += '<input id="canShare-'+escapeHTML(shareWith)+'" type="checkbox" name="share" class="permissions" '+shareChecked+' data-permissions="'+OC.PERMISSION_SHARE+'" /><label for="canShare-'+escapeHTML(shareWith)+'">'+t('core', 'can share')+'</label>';
  664. }
  665. if (possiblePermissions & OC.PERMISSION_CREATE || possiblePermissions & OC.PERMISSION_UPDATE || possiblePermissions & OC.PERMISSION_DELETE) {
  666. html += '<input id="canEdit-'+escapeHTML(shareWith)+'" type="checkbox" name="edit" class="permissions" '+editChecked+' /><label for="canEdit-'+escapeHTML(shareWith)+'">'+t('core', 'can edit')+'</label>';
  667. }
  668. if (shareType !== OC.Share.SHARE_TYPE_REMOTE) {
  669. showCrudsButton = '<a href="#" class="showCruds"><img class="svg" alt="'+t('core', 'access control')+'" src="'+OC.imagePath('core', 'actions/triangle-s')+'"/></a>';
  670. }
  671. html += '<div class="cruds" style="display:none;">';
  672. if (possiblePermissions & OC.PERMISSION_CREATE) {
  673. html += '<input id="canCreate-' + escapeHTML(shareWith) + '" type="checkbox" name="create" class="permissions" ' + createChecked + ' data-permissions="' + OC.PERMISSION_CREATE + '"/><label for="canCreate-' + escapeHTML(shareWith) + '">' + t('core', 'create') + '</label>';
  674. }
  675. if (possiblePermissions & OC.PERMISSION_UPDATE) {
  676. html += '<input id="canUpdate-' + escapeHTML(shareWith) + '" type="checkbox" name="update" class="permissions" ' + updateChecked + ' data-permissions="' + OC.PERMISSION_UPDATE + '"/><label for="canUpdate-' + escapeHTML(shareWith) + '">' + t('core', 'change') + '</label>';
  677. }
  678. if (possiblePermissions & OC.PERMISSION_DELETE) {
  679. html += '<input id="canDelete-' + escapeHTML(shareWith) + '" type="checkbox" name="delete" class="permissions" ' + deleteChecked + ' data-permissions="' + OC.PERMISSION_DELETE + '"/><label for="canDelete-' + escapeHTML(shareWith) + '">' + t('core', 'delete') + '</label>';
  680. }
  681. html += '</div>';
  682. html += '</li>';
  683. html = $(html).appendTo('#shareWithList');
  684. if (oc_config.enable_avatars === true && shareType === OC.Share.SHARE_TYPE_USER) {
  685. $('#avatar-' + escapeHTML(shareWith)).avatar(escapeHTML(shareWith), 32);
  686. }
  687. // insert cruds button into last label element
  688. var lastLabel = html.find('>label:last');
  689. if (lastLabel.exists()){
  690. lastLabel.append(showCrudsButton);
  691. }
  692. else{
  693. html.find('.cruds').before(showCrudsButton);
  694. }
  695. if (!OC.Share.currentShares[shareType]) {
  696. OC.Share.currentShares[shareType] = [];
  697. }
  698. OC.Share.currentShares[shareType].push(shareItem);
  699. }
  700. },
  701. showLink:function(token, password, itemSource) {
  702. OC.Share.itemShares[OC.Share.SHARE_TYPE_LINK] = true;
  703. $('#linkCheckbox').attr('checked', true);
  704. //check itemType
  705. var linkSharetype=$('#dropdown').data('item-type');
  706. if (! token) {
  707. //fallback to pre token link
  708. var filename = $('tr').filterAttr('data-id', String(itemSource)).data('file');
  709. var type = $('tr').filterAttr('data-id', String(itemSource)).data('type');
  710. if ($('#dir').val() == '/') {
  711. var file = $('#dir').val() + filename;
  712. } else {
  713. var file = $('#dir').val() + '/' + filename;
  714. }
  715. file = '/'+OC.currentUser+'/files'+file;
  716. // TODO: use oc webroot ?
  717. var link = parent.location.protocol+'//'+location.host+OC.linkTo('', 'public.php')+'?service=files&'+type+'='+encodeURIComponent(file);
  718. } else {
  719. //TODO add path param when showing a link to file in a subfolder of a public link share
  720. var service='';
  721. if(linkSharetype === 'folder' || linkSharetype === 'file'){
  722. service='files';
  723. }else{
  724. service=linkSharetype;
  725. }
  726. // TODO: use oc webroot ?
  727. if (service !== 'files') {
  728. var link = parent.location.protocol+'//'+location.host+OC.linkTo('', 'public.php')+'?service='+service+'&t='+token;
  729. } else {
  730. var link = parent.location.protocol+'//'+location.host+OC.generateUrl('/s/')+token;
  731. }
  732. }
  733. $('#linkText').val(link);
  734. $('#linkText').show('blind');
  735. $('#linkText').css('display','block');
  736. if (oc_appconfig.core.enforcePasswordForPublicLink === false || password === null) {
  737. $('#showPassword').show();
  738. $('#showPassword+label').show();
  739. }
  740. if (password != null) {
  741. $('#linkPass').show('blind');
  742. $('#showPassword').attr('checked', true);
  743. $('#linkPassText').attr('placeholder', '**********');
  744. }
  745. $('#expiration').show();
  746. $('#emailPrivateLink #email').show();
  747. $('#emailPrivateLink #emailButton').show();
  748. $('#allowPublicUploadWrapper').show();
  749. },
  750. hideLink:function() {
  751. $('#linkText').hide('blind');
  752. $('#defaultExpireMessage').hide();
  753. $('#showPassword').hide();
  754. $('#showPassword+label').hide();
  755. $('#linkPass').hide('blind');
  756. $('#emailPrivateLink #email').hide();
  757. $('#emailPrivateLink #emailButton').hide();
  758. $('#allowPublicUploadWrapper').hide();
  759. },
  760. dirname:function(path) {
  761. return path.replace(/\\/g,'/').replace(/\/[^\/]*$/, '');
  762. },
  763. /**
  764. * Displays the expiration date field
  765. *
  766. * @param {Date} date current expiration date
  767. * @param {int} [shareTime] share timestamp in seconds, defaults to now
  768. */
  769. showExpirationDate:function(date, shareTime) {
  770. var now = new Date();
  771. // min date should always be the next day
  772. var minDate = new Date();
  773. minDate.setDate(minDate.getDate()+1);
  774. var datePickerOptions = {
  775. minDate: minDate,
  776. maxDate: null
  777. };
  778. if (_.isNumber(shareTime)) {
  779. shareTime = new Date(shareTime * 1000);
  780. }
  781. if (!shareTime) {
  782. shareTime = now;
  783. }
  784. $('#expirationCheckbox').attr('checked', true);
  785. $('#expirationDate').val(date);
  786. $('#expirationDate').show('blind');
  787. $('#expirationDate').css('display','block');
  788. $('#expirationDate').datepicker({
  789. dateFormat : 'dd-mm-yy'
  790. });
  791. if (oc_appconfig.core.defaultExpireDateEnforced) {
  792. $('#expirationCheckbox').attr('disabled', true);
  793. shareTime = OC.Util.stripTime(shareTime).getTime();
  794. // max date is share date + X days
  795. datePickerOptions.maxDate = new Date(shareTime + oc_appconfig.core.defaultExpireDate * 24 * 3600 * 1000);
  796. }
  797. if(oc_appconfig.core.defaultExpireDateEnabled) {
  798. $('#defaultExpireMessage').show('blind');
  799. }
  800. $.datepicker.setDefaults(datePickerOptions);
  801. }
  802. };
  803. $(document).ready(function() {
  804. if(typeof monthNames != 'undefined'){
  805. // min date should always be the next day
  806. var minDate = new Date();
  807. minDate.setDate(minDate.getDate()+1);
  808. $.datepicker.setDefaults({
  809. monthNames: monthNames,
  810. monthNamesShort: $.map(monthNames, function(v) { return v.slice(0,3)+'.'; }),
  811. dayNames: dayNames,
  812. dayNamesMin: $.map(dayNames, function(v) { return v.slice(0,2); }),
  813. dayNamesShort: $.map(dayNames, function(v) { return v.slice(0,3)+'.'; }),
  814. firstDay: firstDay,
  815. minDate : minDate
  816. });
  817. }
  818. $(document).on('click', 'a.share', function(event) {
  819. event.stopPropagation();
  820. if ($(this).data('item-type') !== undefined && $(this).data('item') !== undefined) {
  821. var itemType = $(this).data('item-type');
  822. var itemSource = $(this).data('item');
  823. var appendTo = $(this).parent().parent();
  824. var link = false;
  825. var possiblePermissions = $(this).data('possible-permissions');
  826. if ($(this).data('link') !== undefined && $(this).data('link') == true) {
  827. link = true;
  828. }
  829. if (OC.Share.droppedDown) {
  830. if (itemSource != $('#dropdown').data('item')) {
  831. OC.Share.hideDropDown(function () {
  832. OC.Share.showDropDown(itemType, itemSource, appendTo, link, possiblePermissions);
  833. });
  834. } else {
  835. OC.Share.hideDropDown();
  836. }
  837. } else {
  838. OC.Share.showDropDown(itemType, itemSource, appendTo, link, possiblePermissions);
  839. }
  840. }
  841. });
  842. $(this).click(function(event) {
  843. var target = $(event.target);
  844. var isMatched = !target.is('.drop, .ui-datepicker-next, .ui-datepicker-prev, .ui-icon')
  845. && !target.closest('#ui-datepicker-div').length && !target.closest('.ui-autocomplete').length;
  846. if (OC.Share.droppedDown && isMatched && $('#dropdown').has(event.target).length === 0) {
  847. OC.Share.hideDropDown();
  848. }
  849. });
  850. $(document).on('click', '#dropdown .showCruds', function() {
  851. $(this).closest('li').find('.cruds').toggle();
  852. return false;
  853. });
  854. $(document).on('click', '#dropdown .unshare', function() {
  855. var $li = $(this).closest('li');
  856. var itemType = $('#dropdown').data('item-type');
  857. var itemSource = $('#dropdown').data('item-source');
  858. var shareType = $li.data('share-type');
  859. var shareWith = $li.attr('data-share-with');
  860. var $button = $(this);
  861. if (!$button.is('a')) {
  862. $button = $button.closest('a');
  863. }
  864. if ($button.hasClass('icon-loading-small')) {
  865. // deletion in progress
  866. return false;
  867. }
  868. $button.empty().addClass('icon-loading-small');
  869. OC.Share.unshare(itemType, itemSource, shareType, shareWith, function() {
  870. $li.remove();
  871. var index = OC.Share.itemShares[shareType].indexOf(shareWith);
  872. OC.Share.itemShares[shareType].splice(index, 1);
  873. // updated list of shares
  874. OC.Share.currentShares[shareType].splice(index, 1);
  875. $('#dropdown').trigger(new $.Event('sharesChanged', {shares: OC.Share.currentShares}));
  876. OC.Share.updateIcon(itemType, itemSource);
  877. if (typeof OC.Share.statuses[itemSource] === 'undefined') {
  878. $('#expiration').hide('blind');
  879. }
  880. });
  881. return false;
  882. });
  883. $(document).on('change', '#dropdown .permissions', function() {
  884. var li = $(this).closest('li');
  885. if ($(this).attr('name') == 'edit') {
  886. var checkboxes = $('.permissions', li);
  887. var checked = $(this).is(':checked');
  888. // Check/uncheck Create, Update, and Delete checkboxes if Edit is checked/unck
  889. $(checkboxes).filter('input[name="create"]').attr('checked', checked);
  890. $(checkboxes).filter('input[name="update"]').attr('checked', checked);
  891. $(checkboxes).filter('input[name="delete"]').attr('checked', checked);
  892. } else {
  893. var checkboxes = $('.permissions', li);
  894. // Uncheck Edit if Create, Update, and Delete are not checked
  895. if (!$(this).is(':checked')
  896. && !$(checkboxes).filter('input[name="create"]').is(':checked')
  897. && !$(checkboxes).filter('input[name="update"]').is(':checked')
  898. && !$(checkboxes).filter('input[name="delete"]').is(':checked'))
  899. {
  900. $(checkboxes).filter('input[name="edit"]').attr('checked', false);
  901. // Check Edit if Create, Update, or Delete is checked
  902. } else if (($(this).attr('name') == 'create'
  903. || $(this).attr('name') == 'update'
  904. || $(this).attr('name') == 'delete'))
  905. {
  906. $(checkboxes).filter('input[name="edit"]').attr('checked', true);
  907. }
  908. }
  909. var permissions = OC.PERMISSION_READ;
  910. $(checkboxes).filter(':not(input[name="edit"])').filter(':checked').each(function(index, checkbox) {
  911. permissions |= $(checkbox).data('permissions');
  912. });
  913. OC.Share.setPermissions($('#dropdown').data('item-type'),
  914. $('#dropdown').data('item-source'),
  915. li.data('share-type'),
  916. li.attr('data-share-with'),
  917. permissions);
  918. });
  919. $(document).on('change', '#dropdown #linkCheckbox', function() {
  920. var $dropDown = $('#dropdown');
  921. var itemType = $dropDown.data('item-type');
  922. var itemSource = $dropDown.data('item-source');
  923. var itemSourceName = $dropDown.data('item-source-name');
  924. var $loading = $dropDown.find('#link .icon-loading-small');
  925. var $button = $(this);
  926. if (!$loading.hasClass('hidden')) {
  927. // already in progress
  928. return false;
  929. }
  930. if (this.checked) {
  931. var expireDateString = '';
  932. if (oc_appconfig.core.defaultExpireDateEnabled) {
  933. var date = new Date().getTime();
  934. var expireAfterMs = oc_appconfig.core.defaultExpireDate * 24 * 60 * 60 * 1000;
  935. var expireDate = new Date(date + expireAfterMs);
  936. var month = expireDate.getMonth() + 1;
  937. var year = expireDate.getFullYear();
  938. var day = expireDate.getDate();
  939. expireDateString = year + "-" + month + '-' + day + ' 00:00:00';
  940. }
  941. // Create a link
  942. if (oc_appconfig.core.enforcePasswordForPublicLink === false) {
  943. $loading.removeClass('hidden');
  944. $button.addClass('hidden');
  945. $button.prop('disabled', true);
  946. OC.Share.share(itemType, itemSource, OC.Share.SHARE_TYPE_LINK, '', OC.PERMISSION_READ, itemSourceName, expireDateString, function(data) {
  947. $loading.addClass('hidden');
  948. $button.removeClass('hidden');
  949. $button.prop('disabled', false);
  950. OC.Share.showLink(data.token, null, itemSource);
  951. $('#dropdown').trigger(new $.Event('sharesChanged', {shares: OC.Share.currentShares}));
  952. OC.Share.updateIcon(itemType, itemSource);
  953. });
  954. } else {
  955. $('#linkPass').toggle('blind');
  956. $('#linkPassText').focus();
  957. }
  958. if (expireDateString !== '') {
  959. OC.Share.showExpirationDate(expireDateString);
  960. }
  961. } else {
  962. // Delete private link
  963. OC.Share.hideLink();
  964. $('#expiration').hide('blind');
  965. if ($('#linkText').val() !== '') {
  966. $loading.removeClass('hidden');
  967. $button.addClass('hidden');
  968. $button.prop('disabled', true);
  969. OC.Share.unshare(itemType, itemSource, OC.Share.SHARE_TYPE_LINK, '', function() {
  970. $loading.addClass('hidden');
  971. $button.removeClass('hidden');
  972. $button.prop('disabled', false);
  973. OC.Share.itemShares[OC.Share.SHARE_TYPE_LINK] = false;
  974. $('#dropdown').trigger(new $.Event('sharesChanged', {shares: OC.Share.currentShares}));
  975. OC.Share.updateIcon(itemType, itemSource);
  976. if (typeof OC.Share.statuses[itemSource] === 'undefined') {
  977. $('#expiration').hide('blind');
  978. }
  979. });
  980. }
  981. }
  982. });
  983. $(document).on('click', '#dropdown #linkText', function() {
  984. $(this).focus();
  985. $(this).select();
  986. });
  987. // Handle the Allow Public Upload Checkbox
  988. $(document).on('click', '#sharingDialogAllowPublicUpload', function() {
  989. // Gather data
  990. var $dropDown = $('#dropdown');
  991. var allowPublicUpload = $(this).is(':checked');
  992. var itemType = $dropDown.data('item-type');
  993. var itemSource = $dropDown.data('item-source');
  994. var itemSourceName = $dropDown.data('item-source-name');
  995. var expirationDate = '';
  996. if ($('#expirationCheckbox').is(':checked') === true) {
  997. expirationDate = $( "#expirationDate" ).val();
  998. }
  999. var permissions = 0;
  1000. var $button = $(this);
  1001. var $loading = $dropDown.find('#allowPublicUploadWrapper .icon-loading-small');
  1002. if (!$loading.hasClass('hidden')) {
  1003. // already in progress
  1004. return false;
  1005. }
  1006. // Calculate permissions
  1007. if (allowPublicUpload) {
  1008. permissions = OC.PERMISSION_UPDATE + OC.PERMISSION_CREATE + OC.PERMISSION_READ;
  1009. } else {
  1010. permissions = OC.PERMISSION_READ;
  1011. }
  1012. // Update the share information
  1013. $button.addClass('hidden');
  1014. $button.prop('disabled', true);
  1015. $loading.removeClass('hidden');
  1016. OC.Share.share(itemType, itemSource, OC.Share.SHARE_TYPE_LINK, '', permissions, itemSourceName, expirationDate, function(data) {
  1017. $loading.addClass('hidden');
  1018. $button.removeClass('hidden');
  1019. $button.prop('disabled', false);
  1020. });
  1021. });
  1022. $(document).on('click', '#dropdown #showPassword', function() {
  1023. $('#linkPass').toggle('blind');
  1024. if (!$('#showPassword').is(':checked') ) {
  1025. var itemType = $('#dropdown').data('item-type');
  1026. var itemSource = $('#dropdown').data('item-source');
  1027. var itemSourceName = $('#dropdown').data('item-source-name');
  1028. var allowPublicUpload = $('#sharingDialogAllowPublicUpload').is(':checked');
  1029. var permissions = 0;
  1030. var $loading = $('#showPassword .icon-loading-small');
  1031. // Calculate permissions
  1032. if (allowPublicUpload) {
  1033. permissions = OC.PERMISSION_UPDATE + OC.PERMISSION_CREATE + OC.PERMISSION_READ;
  1034. } else {
  1035. permissions = OC.PERMISSION_READ;
  1036. }
  1037. $loading.removeClass('hidden');
  1038. OC.Share.share(itemType, itemSource, OC.Share.SHARE_TYPE_LINK, '', permissions, itemSourceName).then(function() {
  1039. $loading.addClass('hidden');
  1040. $('#linkPassText').attr('placeholder', t('core', 'Choose a password for the public link'));
  1041. });
  1042. } else {
  1043. $('#linkPassText').focus();
  1044. }
  1045. });
  1046. $(document).on('focusout keyup', '#dropdown #linkPassText', function(event) {
  1047. var linkPassText = $('#linkPassText');
  1048. if ( linkPassText.val() != '' && (event.type == 'focusout' || event.keyCode == 13) ) {
  1049. var allowPublicUpload = $('#sharingDialogAllowPublicUpload').is(':checked');
  1050. var dropDown = $('#dropdown');
  1051. var itemType = dropDown.data('item-type');
  1052. var itemSource = dropDown.data('item-source');
  1053. var itemSourceName = $('#dropdown').data('item-source-name');
  1054. var permissions = 0;
  1055. var $loading = dropDown.find('#linkPass .icon-loading-small');
  1056. // Calculate permissions
  1057. if (allowPublicUpload) {
  1058. permissions = OC.PERMISSION_UPDATE + OC.PERMISSION_CREATE + OC.PERMISSION_READ;
  1059. } else {
  1060. permissions = OC.PERMISSION_READ;
  1061. }
  1062. $loading.removeClass('hidden');
  1063. OC.Share.share(itemType, itemSource, OC.Share.SHARE_TYPE_LINK, $('#linkPassText').val(), permissions, itemSourceName, function(data) {
  1064. $loading.addClass('hidden');
  1065. linkPassText.val('');
  1066. linkPassText.attr('placeholder', t('core', 'Password protected'));
  1067. if (oc_appconfig.core.enforcePasswordForPublicLink) {
  1068. OC.Share.showLink(data.token, "password set", itemSource);
  1069. OC.Share.updateIcon(itemType, itemSource);
  1070. }
  1071. });
  1072. }
  1073. });
  1074. $(document).on('click', '#dropdown #expirationCheckbox', function() {
  1075. if (this.checked) {
  1076. OC.Share.showExpirationDate('');
  1077. } else {
  1078. var itemType = $('#dropdown').data('item-type');
  1079. var itemSource = $('#dropdown').data('item-source');
  1080. $.post(OC.filePath('core', 'ajax', 'share.php'), { action: 'setExpirationDate', itemType: itemType, itemSource: itemSource, date: '' }, function(result) {
  1081. if (!result || result.status !== 'success') {
  1082. OC.dialogs.alert(t('core', 'Error unsetting expiration date'), t('core', 'Error'));
  1083. }
  1084. $('#expirationDate').hide('blind');
  1085. if (oc_appconfig.core.defaultExpireDateEnforced === false) {
  1086. $('#defaultExpireMessage').show('blind');
  1087. }
  1088. });
  1089. }
  1090. });
  1091. $(document).on('change', '#dropdown #expirationDate', function() {
  1092. var itemType = $('#dropdown').data('item-type');
  1093. var itemSource = $('#dropdown').data('item-source');
  1094. $(this).tipsy('hide');
  1095. $(this).removeClass('error');
  1096. $.post(OC.filePath('core', 'ajax', 'share.php'), { action: 'setExpirationDate', itemType: itemType, itemSource: itemSource, date: $(this).val() }, function(result) {
  1097. if (!result || result.status !== 'success') {
  1098. var expirationDateField = $('#dropdown #expirationDate');
  1099. if (!result.data.message) {
  1100. expirationDateField.attr('original-title', t('core', 'Error setting expiration date'));
  1101. } else {
  1102. expirationDateField.attr('original-title', result.data.message);
  1103. }
  1104. expirationDateField.tipsy({gravity: 'n', fade: true});
  1105. expirationDateField.tipsy('show');
  1106. expirationDateField.addClass('error');
  1107. } else {
  1108. if (oc_appconfig.core.defaultExpireDateEnforced === 'no') {
  1109. $('#defaultExpireMessage'). hide('blind');
  1110. }
  1111. }
  1112. });
  1113. });
  1114. $(document).on('submit', '#dropdown #emailPrivateLink', function(event) {
  1115. event.preventDefault();
  1116. var link = $('#linkText').val();
  1117. var itemType = $('#dropdown').data('item-type');
  1118. var itemSource = $('#dropdown').data('item-source');
  1119. var file = $('tr').filterAttr('data-id', String(itemSource)).data('file');
  1120. var email = $('#email').val();
  1121. var expirationDate = '';
  1122. if ( $('#expirationCheckbox').is(':checked') === true ) {
  1123. expirationDate = $( "#expirationDate" ).val();
  1124. }
  1125. if (email != '') {
  1126. $('#email').prop('disabled', true);
  1127. $('#email').val(t('core', 'Sending ...'));
  1128. $('#emailButton').prop('disabled', true);
  1129. $.post(OC.filePath('core', 'ajax', 'share.php'), { action: 'email', toaddress: email, link: link, itemType: itemType, itemSource: itemSource, file: file, expiration: expirationDate},
  1130. function(result) {
  1131. $('#email').prop('disabled', false);
  1132. $('#emailButton').prop('disabled', false);
  1133. if (result && result.status == 'success') {
  1134. $('#email').css('font-weight', 'bold');
  1135. $('#email').animate({ fontWeight: 'normal' }, 2000, function() {
  1136. $(this).val('');
  1137. }).val(t('core','Email sent'));
  1138. } else {
  1139. OC.dialogs.alert(result.data.message, t('core', 'Error while sharing'));
  1140. }
  1141. });
  1142. }
  1143. });
  1144. $(document).on('click', '#dropdown input[name=mailNotification]', function() {
  1145. var $li = $(this).closest('li');
  1146. var itemType = $('#dropdown').data('item-type');
  1147. var itemSource = $('#dropdown').data('item-source');
  1148. var action = '';
  1149. if (this.checked) {
  1150. action = 'informRecipients';
  1151. } else {
  1152. action = 'informRecipientsDisabled';
  1153. }
  1154. var shareType = $li.data('share-type');
  1155. var shareWith = $li.attr('data-share-with');
  1156. $.post(OC.filePath('core', 'ajax', 'share.php'), {action: action, recipient: shareWith, shareType: shareType, itemSource: itemSource, itemType: itemType}, function(result) {
  1157. if (result.status !== 'success') {
  1158. OC.dialogs.alert(t('core', result.data.message), t('core', 'Warning'));
  1159. }
  1160. });
  1161. });
  1162. });