share.js 48 KB

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