share.js 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854
  1. OC.Share={
  2. SHARE_TYPE_USER:0,
  3. SHARE_TYPE_GROUP:1,
  4. SHARE_TYPE_LINK:3,
  5. SHARE_TYPE_EMAIL:4,
  6. itemShares:[],
  7. statuses:{},
  8. droppedDown:false,
  9. /**
  10. * Loads ALL share statuses from server, stores them in OC.Share.statuses then
  11. * calls OC.Share.updateIcons() to update the files "Share" icon to "Shared"
  12. * according to their share status and share type.
  13. */
  14. loadIcons:function(itemType) {
  15. // Load all share icons
  16. $.get(OC.filePath('core', 'ajax', 'share.php'), { fetch: 'getItemsSharedStatuses', itemType: itemType }, function(result) {
  17. if (result && result.status === 'success') {
  18. OC.Share.statuses = {};
  19. $.each(result.data, function(item, data) {
  20. OC.Share.statuses[item] = data;
  21. });
  22. OC.Share.updateIcons(itemType);
  23. }
  24. });
  25. },
  26. /**
  27. * Updates the files' "Share" icons according to the known
  28. * sharing states stored in OC.Share.statuses.
  29. * (not reloaded from server)
  30. */
  31. updateIcons:function(itemType){
  32. var item;
  33. for (item in OC.Share.statuses){
  34. var data = OC.Share.statuses[item];
  35. var hasLink = data['link'];
  36. // Links override shared in terms of icon display
  37. if (hasLink) {
  38. var image = OC.imagePath('core', 'actions/public');
  39. } else {
  40. var image = OC.imagePath('core', 'actions/shared');
  41. }
  42. if (itemType != 'file' && itemType != 'folder') {
  43. $('a.share[data-item="'+item+'"]').css('background', 'url('+image+') no-repeat center');
  44. } else {
  45. var file = $('tr[data-id="'+item+'"]');
  46. if (file.length > 0) {
  47. var action = $(file).find('.fileactions .action[data-action="Share"]');
  48. var img = action.find('img').attr('src', image);
  49. action.addClass('permanent');
  50. action.html(' <span>'+t('core', 'Shared')+'</span>').prepend(img);
  51. } else {
  52. var dir = $('#dir').val();
  53. if (dir.length > 1) {
  54. var last = '';
  55. var path = dir;
  56. // Search for possible parent folders that are shared
  57. while (path != last) {
  58. if (path == data['path'] && !data['link']) {
  59. var actions = $('.fileactions .action[data-action="Share"]');
  60. $.each(actions, function(index, action) {
  61. var img = $(action).find('img');
  62. if (img.attr('src') != OC.imagePath('core', 'actions/public')) {
  63. img.attr('src', image);
  64. $(action).addClass('permanent');
  65. $(action).html(' <span>'+t('core', 'Shared')+'</span>').prepend(img);
  66. }
  67. });
  68. }
  69. last = path;
  70. path = OC.Share.dirname(path);
  71. }
  72. }
  73. }
  74. }
  75. }
  76. },
  77. updateIcon:function(itemType, itemSource) {
  78. var shares = false;
  79. var link = false;
  80. var image = OC.imagePath('core', 'actions/share');
  81. $.each(OC.Share.itemShares, function(index) {
  82. if (OC.Share.itemShares[index]) {
  83. if (index == OC.Share.SHARE_TYPE_LINK) {
  84. if (OC.Share.itemShares[index] == true) {
  85. shares = true;
  86. image = OC.imagePath('core', 'actions/public');
  87. link = true;
  88. return;
  89. }
  90. } else if (OC.Share.itemShares[index].length > 0) {
  91. shares = true;
  92. image = OC.imagePath('core', 'actions/shared');
  93. }
  94. }
  95. });
  96. if (itemType != 'file' && itemType != 'folder') {
  97. $('a.share[data-item="'+itemSource+'"]').css('background', 'url('+image+') no-repeat center');
  98. } else {
  99. var file = $('tr').filterAttr('data-id', String(itemSource));
  100. if (file.length > 0) {
  101. var action = $(file).find('.fileactions .action').filterAttr('data-action', 'Share');
  102. var img = action.find('img').attr('src', image);
  103. if (shares) {
  104. action.addClass('permanent');
  105. action.html(' <span>'+ escapeHTML(t('core', 'Shared'))+'</span>').prepend(img);
  106. } else {
  107. action.removeClass('permanent');
  108. action.html(' <span>'+ escapeHTML(t('core', 'Share'))+'</span>').prepend(img);
  109. }
  110. }
  111. }
  112. if (shares) {
  113. OC.Share.statuses[itemSource] = OC.Share.statuses[itemSource] || {};
  114. OC.Share.statuses[itemSource]['link'] = link;
  115. } else {
  116. delete OC.Share.statuses[itemSource];
  117. }
  118. },
  119. loadItem:function(itemType, itemSource) {
  120. var data = '';
  121. var checkReshare = true;
  122. if (typeof OC.Share.statuses[itemSource] === 'undefined') {
  123. // NOTE: Check does not always work and misses some shares, fix later
  124. var checkShares = true;
  125. } else {
  126. var checkShares = true;
  127. }
  128. $.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) {
  129. if (result && result.status === 'success') {
  130. data = result.data;
  131. } else {
  132. data = false;
  133. }
  134. }});
  135. return data;
  136. },
  137. share:function(itemType, itemSource, shareType, shareWith, permissions, itemSourceName, callback) {
  138. $.post(OC.filePath('core', 'ajax', 'share.php'),
  139. {
  140. action: 'share',
  141. itemType: itemType,
  142. itemSource: itemSource,
  143. shareType: shareType,
  144. shareWith: shareWith,
  145. permissions: permissions,
  146. itemSourceName: itemSourceName
  147. }, function (result) {
  148. if (result && result.status === 'success') {
  149. if (callback) {
  150. callback(result.data);
  151. }
  152. } else {
  153. if (result.data && result.data.message) {
  154. var msg = result.data.message;
  155. } else {
  156. var msg = t('core', 'Error');
  157. }
  158. OC.dialogs.alert(msg, t('core', 'Error while sharing'));
  159. }
  160. });
  161. },
  162. unshare:function(itemType, itemSource, shareType, shareWith, callback) {
  163. $.post(OC.filePath('core', 'ajax', 'share.php'), { action: 'unshare', itemType: itemType, itemSource: itemSource, shareType: shareType, shareWith: shareWith }, function(result) {
  164. if (result && result.status === 'success') {
  165. if (callback) {
  166. callback();
  167. }
  168. } else {
  169. OC.dialogs.alert(t('core', 'Error while unsharing'), t('core', 'Error'));
  170. }
  171. });
  172. },
  173. setPermissions:function(itemType, itemSource, shareType, shareWith, permissions) {
  174. $.post(OC.filePath('core', 'ajax', 'share.php'), { action: 'setPermissions', itemType: itemType, itemSource: itemSource, shareType: shareType, shareWith: shareWith, permissions: permissions }, function(result) {
  175. if (!result || result.status !== 'success') {
  176. OC.dialogs.alert(t('core', 'Error while changing permissions'), t('core', 'Error'));
  177. }
  178. });
  179. },
  180. showDropDown:function(itemType, itemSource, appendTo, link, possiblePermissions, filename) {
  181. var data = OC.Share.loadItem(itemType, itemSource);
  182. var dropDownEl;
  183. var html = '<div id="dropdown" class="drop" data-item-type="'+itemType+'" data-item-source="'+itemSource+'">';
  184. if (data !== false && data.reshare !== false && data.reshare.uid_owner !== undefined) {
  185. if (data.reshare.share_type == OC.Share.SHARE_TYPE_GROUP) {
  186. html += '<span class="reshare">'+t('core', 'Shared with you and the group {group} by {owner}', {group: escapeHTML(data.reshare.share_with), owner: escapeHTML(data.reshare.displayname_owner)})+'</span>';
  187. } else {
  188. html += '<span class="reshare">'+t('core', 'Shared with you by {owner}', {owner: escapeHTML(data.reshare.displayname_owner)})+'</span>';
  189. }
  190. html += '<br />';
  191. }
  192. if (possiblePermissions & OC.PERMISSION_SHARE) {
  193. // Determine the Allow Public Upload status.
  194. // Used later on to determine if the
  195. // respective checkbox should be checked or
  196. // not.
  197. var publicUploadEnabled = $('#filestable').data('allow-public-upload');
  198. if (typeof publicUploadEnabled == 'undefined') {
  199. publicUploadEnabled = 'no';
  200. }
  201. var allowPublicUploadStatus = false;
  202. $.each(data.shares, function(key, value) {
  203. if (value.share_type === OC.Share.SHARE_TYPE_LINK) {
  204. allowPublicUploadStatus = (value.permissions & OC.PERMISSION_CREATE) ? true : false;
  205. return true;
  206. }
  207. });
  208. html += '<input id="shareWith" type="text" placeholder="'+t('core', 'Share with user or group …')+'" />';
  209. html += '<ul id="shareWithList">';
  210. html += '</ul>';
  211. var linksAllowed = $('#allowShareWithLink').val() === 'yes';
  212. if (link && linksAllowed) {
  213. html += '<div id="link">';
  214. html += '<input type="checkbox" name="linkCheckbox" id="linkCheckbox" value="1" /><label for="linkCheckbox">'+t('core', 'Share link')+'</label>';
  215. html += '<br />';
  216. var defaultExpireMessage = '';
  217. if ((itemType === 'folder' || itemType === 'file') && oc_appconfig.core.defaultExpireDateEnabled === 'yes') {
  218. if (oc_appconfig.core.defaultExpireDateEnforced === 'yes') {
  219. defaultExpireMessage = t('core', 'The public link will expire no later than {days} days after it is created', {'days': oc_appconfig.core.defaultExpireDate}) + '<br/>';
  220. } else {
  221. defaultExpireMessage = t('core', 'By default the public link will expire after {days} days', {'days': oc_appconfig.core.defaultExpireDate}) + '<br/>';
  222. }
  223. }
  224. html += '<input id="linkText" type="text" readonly="readonly" />';
  225. html += '<input type="checkbox" name="showPassword" id="showPassword" value="1" style="display:none;" /><label for="showPassword" style="display:none;">'+t('core', 'Password protect')+'</label>';
  226. html += '<div id="linkPass">';
  227. html += '<input id="linkPassText" type="password" placeholder="'+t('core', 'Choose a password for the public link')+'" />';
  228. html += '</div>';
  229. if (itemType === 'folder' && (possiblePermissions & OC.PERMISSION_CREATE) && publicUploadEnabled === 'yes') {
  230. html += '<div id="allowPublicUploadWrapper" style="display:none;">';
  231. html += '<input type="checkbox" value="1" name="allowPublicUpload" id="sharingDialogAllowPublicUpload"' + ((allowPublicUploadStatus) ? 'checked="checked"' : '') + ' />';
  232. html += '<label for="sharingDialogAllowPublicUpload">' + t('core', 'Allow Public Upload') + '</label>';
  233. html += '</div>';
  234. }
  235. html += '</div><form id="emailPrivateLink" >';
  236. html += '<input id="email" style="display:none; width:62%;" value="" placeholder="'+t('core', 'Email link to person')+'" type="text" />';
  237. html += '<input id="emailButton" style="display:none;" type="submit" value="'+t('core', 'Send')+'" />';
  238. html += '</form>';
  239. }
  240. html += '<div id="expiration">';
  241. html += '<input type="checkbox" name="expirationCheckbox" id="expirationCheckbox" value="1" /><label for="expirationCheckbox">'+t('core', 'Set expiration date')+'</label>';
  242. html += '<input id="expirationDate" type="text" placeholder="'+t('core', 'Expiration date')+'" style="display:none; width:90%;" />';
  243. html += '<div id="defaultExpireMessage">'+defaultExpireMessage+'</div>';
  244. html += '</div>';
  245. dropDownEl = $(html);
  246. dropDownEl = dropDownEl.appendTo(appendTo);
  247. // Reset item shares
  248. OC.Share.itemShares = [];
  249. if (data.shares) {
  250. $.each(data.shares, function(index, share) {
  251. if (share.share_type == OC.Share.SHARE_TYPE_LINK) {
  252. if ( !('file_target' in share) ) {
  253. OC.Share.showLink(share.token, share.share_with, itemSource);
  254. }
  255. } else {
  256. if (share.collection) {
  257. OC.Share.addShareWith(share.share_type, share.share_with, share.share_with_displayname, share.permissions, possiblePermissions, share.mail_send, share.collection);
  258. } else {
  259. OC.Share.addShareWith(share.share_type, share.share_with, share.share_with_displayname, share.permissions, possiblePermissions, share.mail_send, false);
  260. }
  261. }
  262. if (share.expiration != null) {
  263. OC.Share.showExpirationDate(share.expiration);
  264. }
  265. });
  266. }
  267. $('#shareWith').autocomplete({minLength: 1, source: function(search, response) {
  268. // if (cache[search.term]) {
  269. // response(cache[search.term]);
  270. // } else {
  271. $.get(OC.filePath('core', 'ajax', 'share.php'), { fetch: 'getShareWith', search: search.term, itemShares: OC.Share.itemShares }, function(result) {
  272. if (result.status == 'success' && result.data.length > 0) {
  273. $( "#shareWith" ).autocomplete( "option", "autoFocus", true );
  274. response(result.data);
  275. } else {
  276. // Suggest sharing via email if valid email address
  277. // var pattern = new RegExp(/^[+a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/i);
  278. // if (pattern.test(search.term)) {
  279. // response([{label: t('core', 'Share via email:')+' '+search.term, value: {shareType: OC.Share.SHARE_TYPE_EMAIL, shareWith: search.term}}]);
  280. // } else {
  281. $( "#shareWith" ).autocomplete( "option", "autoFocus", false );
  282. response([t('core', 'No people found')]);
  283. // }
  284. }
  285. });
  286. // }
  287. },
  288. focus: function(event, focused) {
  289. event.preventDefault();
  290. },
  291. select: function(event, selected) {
  292. event.stopPropagation();
  293. var itemType = $('#dropdown').data('item-type');
  294. var itemSource = $('#dropdown').data('item-source');
  295. var itemSourceName = $('#dropdown').data('item-source-name');
  296. var shareType = selected.item.value.shareType;
  297. var shareWith = selected.item.value.shareWith;
  298. $(this).val(shareWith);
  299. // Default permissions are Edit (CRUD) and Share
  300. // Check if these permissions are possible
  301. var permissions = OC.PERMISSION_READ;
  302. if (possiblePermissions & OC.PERMISSION_UPDATE) {
  303. permissions = permissions | OC.PERMISSION_UPDATE;
  304. }
  305. if (possiblePermissions & OC.PERMISSION_CREATE) {
  306. permissions = permissions | OC.PERMISSION_CREATE;
  307. }
  308. if (possiblePermissions & OC.PERMISSION_DELETE) {
  309. permissions = permissions | OC.PERMISSION_DELETE;
  310. }
  311. if (possiblePermissions & OC.PERMISSION_SHARE) {
  312. permissions = permissions | OC.PERMISSION_SHARE;
  313. }
  314. OC.Share.share(itemType, itemSource, shareType, shareWith, permissions, itemSourceName, function() {
  315. OC.Share.addShareWith(shareType, shareWith, selected.item.label, permissions, possiblePermissions);
  316. $('#shareWith').val('');
  317. OC.Share.updateIcon(itemType, itemSource);
  318. });
  319. return false;
  320. }
  321. })
  322. // customize internal _renderItem function to display groups and users differently
  323. .data("ui-autocomplete")._renderItem = function( ul, item ) {
  324. var insert = $( "<a>" );
  325. var text = (item.value.shareType == 1)? item.label + ' ('+t('core', 'group')+')' : item.label;
  326. insert.text( text );
  327. if(item.value.shareType == 1) {
  328. insert = insert.wrapInner('<strong></strong>');
  329. }
  330. return $( "<li>" )
  331. .addClass((item.value.shareType == 1)?'group':'user')
  332. .append( insert )
  333. .appendTo( ul );
  334. };
  335. if (link) {
  336. $('#email').autocomplete({
  337. minLength: 1,
  338. source: function (search, response) {
  339. $.get(OC.filePath('core', 'ajax', 'share.php'), { fetch: 'getShareWithEmail', search: search.term }, function(result) {
  340. if (result.status == 'success' && result.data.length > 0) {
  341. response(result.data);
  342. }
  343. });
  344. },
  345. select: function( event, item ) {
  346. $('#email').val(item.item.email);
  347. return false;
  348. }
  349. })
  350. .data("ui-autocomplete")._renderItem = function( ul, item ) {
  351. return $('<li>')
  352. .append('<a>' + escapeHTML(item.displayname) + "<br>" + escapeHTML(item.email) + '</a>' )
  353. .appendTo( ul );
  354. };
  355. }
  356. } else {
  357. html += '<input id="shareWith" type="text" placeholder="'+t('core', 'Resharing is not allowed')+'" style="width:90%;" disabled="disabled"/>';
  358. html += '</div>';
  359. dropDownEl = $(html);
  360. dropDownEl.appendTo(appendTo);
  361. }
  362. dropDownEl.attr('data-item-source-name', filename);
  363. $('#dropdown').show('blind', function() {
  364. OC.Share.droppedDown = true;
  365. });
  366. if ($('html').hasClass('lte9')){
  367. $('#dropdown input[placeholder]').placeholder();
  368. }
  369. $('#shareWith').focus();
  370. },
  371. hideDropDown:function(callback) {
  372. $('#dropdown').hide('blind', function() {
  373. OC.Share.droppedDown = false;
  374. $('#dropdown').remove();
  375. if (typeof FileActions !== 'undefined') {
  376. $('tr').removeClass('mouseOver');
  377. }
  378. if (callback) {
  379. callback.call();
  380. }
  381. });
  382. },
  383. addShareWith:function(shareType, shareWith, shareWithDisplayName, permissions, possiblePermissions, mailSend, collection) {
  384. if (shareType === 1) {
  385. shareWithDisplayName = shareWithDisplayName + " (" + t('core', 'group') + ')';
  386. }
  387. if (!OC.Share.itemShares[shareType]) {
  388. OC.Share.itemShares[shareType] = [];
  389. }
  390. OC.Share.itemShares[shareType].push(shareWith);
  391. if (collection) {
  392. if (collection.item_type == 'file' || collection.item_type == 'folder') {
  393. var item = collection.path;
  394. } else {
  395. var item = collection.item_source;
  396. }
  397. var collectionList = $('#shareWithList li').filterAttr('data-collection', item);
  398. if (collectionList.length > 0) {
  399. $(collectionList).append(', '+shareWithDisplayName);
  400. } else {
  401. var html = '<li style="clear: both;" data-collection="'+item+'">'+t('core', 'Shared in {item} with {user}', {'item': item, user: shareWithDisplayName})+'</li>';
  402. $('#shareWithList').prepend(html);
  403. }
  404. } else {
  405. var editChecked = createChecked = updateChecked = deleteChecked = shareChecked = '';
  406. if (permissions & OC.PERMISSION_CREATE) {
  407. createChecked = 'checked="checked"';
  408. editChecked = 'checked="checked"';
  409. }
  410. if (permissions & OC.PERMISSION_UPDATE) {
  411. updateChecked = 'checked="checked"';
  412. editChecked = 'checked="checked"';
  413. }
  414. if (permissions & OC.PERMISSION_DELETE) {
  415. deleteChecked = 'checked="checked"';
  416. editChecked = 'checked="checked"';
  417. }
  418. if (permissions & OC.PERMISSION_SHARE) {
  419. shareChecked = 'checked="checked"';
  420. }
  421. var html = '<li style="clear: both;" data-share-type="'+escapeHTML(shareType)+'" data-share-with="'+escapeHTML(shareWith)+'" title="' + escapeHTML(shareWith) + '">';
  422. var showCrudsButton;
  423. html += '<a href="#" class="unshare"><img class="svg" alt="'+t('core', 'Unshare')+'" src="'+OC.imagePath('core', 'actions/delete')+'"/></a>';
  424. html += '<span class="username">' + escapeHTML(shareWithDisplayName) + '</span>';
  425. var mailNotificationEnabled = $('input:hidden[name=mailNotificationEnabled]').val();
  426. if (mailNotificationEnabled === 'yes') {
  427. var checked = '';
  428. if (mailSend === '1') {
  429. checked = 'checked';
  430. }
  431. html += '<label><input type="checkbox" name="mailNotification" class="mailNotification" ' + checked + ' />'+t('core', 'notify by email')+'</label> ';
  432. }
  433. if (possiblePermissions & OC.PERMISSION_CREATE || possiblePermissions & OC.PERMISSION_UPDATE || possiblePermissions & OC.PERMISSION_DELETE) {
  434. html += '<label><input type="checkbox" name="edit" class="permissions" '+editChecked+' />'+t('core', 'can edit')+'</label> ';
  435. }
  436. showCrudsButton = '<a href="#" class="showCruds"><img class="svg" alt="'+t('core', 'access control')+'" src="'+OC.imagePath('core', 'actions/triangle-s')+'"/></a>';
  437. html += '<div class="cruds" style="display:none;">';
  438. if (possiblePermissions & OC.PERMISSION_CREATE) {
  439. html += '<label><input type="checkbox" name="create" class="permissions" '+createChecked+' data-permissions="'+OC.PERMISSION_CREATE+'" />'+t('core', 'create')+'</label>';
  440. }
  441. if (possiblePermissions & OC.PERMISSION_UPDATE) {
  442. html += '<label><input type="checkbox" name="update" class="permissions" '+updateChecked+' data-permissions="'+OC.PERMISSION_UPDATE+'" />'+t('core', 'update')+'</label>';
  443. }
  444. if (possiblePermissions & OC.PERMISSION_DELETE) {
  445. html += '<label><input type="checkbox" name="delete" class="permissions" '+deleteChecked+' data-permissions="'+OC.PERMISSION_DELETE+'" />'+t('core', 'delete')+'</label>';
  446. }
  447. if (possiblePermissions & OC.PERMISSION_SHARE) {
  448. html += '<label><input type="checkbox" name="share" class="permissions" '+shareChecked+' data-permissions="'+OC.PERMISSION_SHARE+'" />'+t('core', 'share')+'</label>';
  449. }
  450. html += '</div>';
  451. html += '</li>';
  452. html = $(html).appendTo('#shareWithList');
  453. // insert cruds button into last label element
  454. var lastLabel = html.find('>label:last');
  455. if (lastLabel.exists()){
  456. lastLabel.append(showCrudsButton);
  457. }
  458. else{
  459. html.find('.cruds').before(showCrudsButton);
  460. }
  461. $('#expiration').show();
  462. }
  463. },
  464. showLink:function(token, password, itemSource) {
  465. OC.Share.itemShares[OC.Share.SHARE_TYPE_LINK] = true;
  466. $('#linkCheckbox').attr('checked', true);
  467. if (! token) {
  468. //fallback to pre token link
  469. var filename = $('tr').filterAttr('data-id', String(itemSource)).data('file');
  470. var type = $('tr').filterAttr('data-id', String(itemSource)).data('type');
  471. if ($('#dir').val() == '/') {
  472. var file = $('#dir').val() + filename;
  473. } else {
  474. var file = $('#dir').val() + '/' + filename;
  475. }
  476. file = '/'+OC.currentUser+'/files'+file;
  477. var link = parent.location.protocol+'//'+location.host+OC.linkTo('', 'public.php')+'?service=files&'+type+'='+encodeURIComponent(file);
  478. } else {
  479. //TODO add path param when showing a link to file in a subfolder of a public link share
  480. var link = parent.location.protocol+'//'+location.host+OC.linkTo('', 'public.php')+'?service=files&t='+token;
  481. }
  482. $('#linkText').val(link);
  483. $('#linkText').show('blind');
  484. $('#linkText').css('display','block');
  485. if (oc_appconfig.core.enforcePasswordForPublicLink === false || password === null) {
  486. $('#showPassword').show();
  487. $('#showPassword+label').show();
  488. }
  489. if (password != null) {
  490. $('#linkPass').show('blind');
  491. $('#showPassword').attr('checked', true);
  492. $('#linkPassText').attr('placeholder', '**********');
  493. }
  494. $('#expiration').show();
  495. $('#defaultExpireMessage').show();
  496. $('#emailPrivateLink #email').show();
  497. $('#emailPrivateLink #emailButton').show();
  498. $('#allowPublicUploadWrapper').show();
  499. },
  500. hideLink:function() {
  501. $('#linkText').hide('blind');
  502. $('#defaultExpireMessage').hide();
  503. $('#showPassword').hide();
  504. $('#showPassword+label').hide();
  505. $('#linkPass').hide('blind');
  506. $('#emailPrivateLink #email').hide();
  507. $('#emailPrivateLink #emailButton').hide();
  508. $('#allowPublicUploadWrapper').hide();
  509. },
  510. dirname:function(path) {
  511. return path.replace(/\\/g,'/').replace(/\/[^\/]*$/, '');
  512. },
  513. showExpirationDate:function(date) {
  514. $('#expirationCheckbox').attr('checked', true);
  515. $('#expirationDate').val(date);
  516. $('#expirationDate').show('blind');
  517. $('#expirationDate').css('display','block');
  518. $('#expirationDate').datepicker({
  519. dateFormat : 'dd-mm-yy'
  520. });
  521. }
  522. };
  523. $(document).ready(function() {
  524. if(typeof monthNames != 'undefined'){
  525. $.datepicker.setDefaults({
  526. monthNames: monthNames,
  527. monthNamesShort: $.map(monthNames, function(v) { return v.slice(0,3)+'.'; }),
  528. dayNames: dayNames,
  529. dayNamesMin: $.map(dayNames, function(v) { return v.slice(0,2); }),
  530. dayNamesShort: $.map(dayNames, function(v) { return v.slice(0,3)+'.'; }),
  531. firstDay: firstDay
  532. });
  533. }
  534. $(document).on('click', 'a.share', function(event) {
  535. event.stopPropagation();
  536. if ($(this).data('item-type') !== undefined && $(this).data('item') !== undefined) {
  537. var itemType = $(this).data('item-type');
  538. var itemSource = $(this).data('item');
  539. var appendTo = $(this).parent().parent();
  540. var link = false;
  541. var possiblePermissions = $(this).data('possible-permissions');
  542. if ($(this).data('link') !== undefined && $(this).data('link') == true) {
  543. link = true;
  544. }
  545. if (OC.Share.droppedDown) {
  546. if (itemSource != $('#dropdown').data('item')) {
  547. OC.Share.hideDropDown(function () {
  548. OC.Share.showDropDown(itemType, itemSource, appendTo, link, possiblePermissions);
  549. });
  550. } else {
  551. OC.Share.hideDropDown();
  552. }
  553. } else {
  554. OC.Share.showDropDown(itemType, itemSource, appendTo, link, possiblePermissions);
  555. }
  556. }
  557. });
  558. $(this).click(function(event) {
  559. var target = $(event.target);
  560. var isMatched = !target.is('.drop, .ui-datepicker-next, .ui-datepicker-prev, .ui-icon')
  561. && !target.closest('#ui-datepicker-div').length && !target.closest('.ui-autocomplete').length;
  562. if (OC.Share.droppedDown && isMatched && $('#dropdown').has(event.target).length === 0) {
  563. OC.Share.hideDropDown();
  564. }
  565. });
  566. $(document).on('click', '#dropdown .showCruds', function() {
  567. $(this).closest('li').find('.cruds').toggle();
  568. return false;
  569. });
  570. $(document).on('click', '#dropdown .unshare', function() {
  571. var $li = $(this).closest('li');
  572. var itemType = $('#dropdown').data('item-type');
  573. var itemSource = $('#dropdown').data('item-source');
  574. var shareType = $li.data('share-type');
  575. var shareWith = $li.attr('data-share-with');
  576. OC.Share.unshare(itemType, itemSource, shareType, shareWith, function() {
  577. $li.remove();
  578. var index = OC.Share.itemShares[shareType].indexOf(shareWith);
  579. OC.Share.itemShares[shareType].splice(index, 1);
  580. OC.Share.updateIcon(itemType, itemSource);
  581. if (typeof OC.Share.statuses[itemSource] === 'undefined') {
  582. $('#expiration').hide('blind');
  583. }
  584. });
  585. return false;
  586. });
  587. $(document).on('change', '#dropdown .permissions', function() {
  588. var li = $(this).closest('li');
  589. if ($(this).attr('name') == 'edit') {
  590. var checkboxes = $('.permissions', li);
  591. var checked = $(this).is(':checked');
  592. // Check/uncheck Create, Update, and Delete checkboxes if Edit is checked/unck
  593. $(checkboxes).filter('input[name="create"]').attr('checked', checked);
  594. $(checkboxes).filter('input[name="update"]').attr('checked', checked);
  595. $(checkboxes).filter('input[name="delete"]').attr('checked', checked);
  596. } else {
  597. var checkboxes = $('.permissions', li);
  598. // Uncheck Edit if Create, Update, and Delete are not checked
  599. if (!$(this).is(':checked')
  600. && !$(checkboxes).filter('input[name="create"]').is(':checked')
  601. && !$(checkboxes).filter('input[name="update"]').is(':checked')
  602. && !$(checkboxes).filter('input[name="delete"]').is(':checked'))
  603. {
  604. $(checkboxes).filter('input[name="edit"]').attr('checked', false);
  605. // Check Edit if Create, Update, or Delete is checked
  606. } else if (($(this).attr('name') == 'create'
  607. || $(this).attr('name') == 'update'
  608. || $(this).attr('name') == 'delete'))
  609. {
  610. $(checkboxes).filter('input[name="edit"]').attr('checked', true);
  611. }
  612. }
  613. var permissions = OC.PERMISSION_READ;
  614. $(checkboxes).filter(':not(input[name="edit"])').filter(':checked').each(function(index, checkbox) {
  615. permissions |= $(checkbox).data('permissions');
  616. });
  617. OC.Share.setPermissions($('#dropdown').data('item-type'),
  618. $('#dropdown').data('item-source'),
  619. li.data('share-type'),
  620. li.attr('data-share-with'),
  621. permissions);
  622. });
  623. $(document).on('change', '#dropdown #linkCheckbox', function() {
  624. var itemType = $('#dropdown').data('item-type');
  625. var itemSource = $('#dropdown').data('item-source');
  626. var itemSourceName = $('#dropdown').data('item-source-name');
  627. if (this.checked) {
  628. // Create a link
  629. if (oc_appconfig.core.enforcePasswordForPublicLink === false) {
  630. OC.Share.share(itemType, itemSource, OC.Share.SHARE_TYPE_LINK, '', OC.PERMISSION_READ, itemSourceName, function(data) {
  631. OC.Share.showLink(data.token, null, itemSource);
  632. OC.Share.updateIcon(itemType, itemSource);
  633. });
  634. } else {
  635. $('#linkPass').toggle('blind');
  636. $('#linkPassText').focus();
  637. }
  638. } else {
  639. // Delete private link
  640. OC.Share.hideLink();
  641. if ($('#linkText').val() !== '') {
  642. OC.Share.unshare(itemType, itemSource, OC.Share.SHARE_TYPE_LINK, '', function() {
  643. OC.Share.itemShares[OC.Share.SHARE_TYPE_LINK] = false;
  644. OC.Share.updateIcon(itemType, itemSource);
  645. if (typeof OC.Share.statuses[itemSource] === 'undefined') {
  646. $('#expiration').hide('blind');
  647. }
  648. });
  649. }
  650. }
  651. });
  652. $(document).on('click', '#dropdown #linkText', function() {
  653. $(this).focus();
  654. $(this).select();
  655. });
  656. // Handle the Allow Public Upload Checkbox
  657. $(document).on('click', '#sharingDialogAllowPublicUpload', function() {
  658. // Gather data
  659. var allowPublicUpload = $(this).is(':checked');
  660. var itemType = $('#dropdown').data('item-type');
  661. var itemSource = $('#dropdown').data('item-source');
  662. var itemSourceName = $('#dropdown').data('item-source-name');
  663. var permissions = 0;
  664. // Calculate permissions
  665. if (allowPublicUpload) {
  666. permissions = OC.PERMISSION_UPDATE + OC.PERMISSION_CREATE + OC.PERMISSION_READ;
  667. } else {
  668. permissions = OC.PERMISSION_READ;
  669. }
  670. // Update the share information
  671. OC.Share.share(itemType, itemSource, OC.Share.SHARE_TYPE_LINK, '', permissions, itemSourceName, function(data) {
  672. });
  673. });
  674. $(document).on('click', '#dropdown #showPassword', function() {
  675. $('#linkPass').toggle('blind');
  676. if (!$('#showPassword').is(':checked') ) {
  677. var itemType = $('#dropdown').data('item-type');
  678. var itemSource = $('#dropdown').data('item-source');
  679. var itemSourceName = $('#dropdown').data('item-source-name');
  680. var allowPublicUpload = $('#sharingDialogAllowPublicUpload').is(':checked');
  681. var permissions = 0;
  682. // Calculate permissions
  683. if (allowPublicUpload) {
  684. permissions = OC.PERMISSION_UPDATE + OC.PERMISSION_CREATE + OC.PERMISSION_READ;
  685. } else {
  686. permissions = OC.PERMISSION_READ;
  687. }
  688. OC.Share.share(itemType, itemSource, OC.Share.SHARE_TYPE_LINK, '', permissions, itemSourceName);
  689. } else {
  690. $('#linkPassText').focus();
  691. }
  692. });
  693. $(document).on('focusout keyup', '#dropdown #linkPassText', function(event) {
  694. var linkPassText = $('#linkPassText');
  695. if ( linkPassText.val() != '' && (event.type == 'focusout' || event.keyCode == 13) ) {
  696. var allowPublicUpload = $('#sharingDialogAllowPublicUpload').is(':checked');
  697. var dropDown = $('#dropdown');
  698. var itemType = dropDown.data('item-type');
  699. var itemSource = dropDown.data('item-source');
  700. var itemSourceName = $('#dropdown').data('item-source-name');
  701. var permissions = 0;
  702. // Calculate permissions
  703. if (allowPublicUpload) {
  704. permissions = OC.PERMISSION_UPDATE + OC.PERMISSION_CREATE + OC.PERMISSION_READ;
  705. } else {
  706. permissions = OC.PERMISSION_READ;
  707. }
  708. OC.Share.share(itemType, itemSource, OC.Share.SHARE_TYPE_LINK, $('#linkPassText').val(), permissions, itemSourceName, function(data) {
  709. linkPassText.val('');
  710. linkPassText.attr('placeholder', t('core', 'Password protected'));
  711. if (oc_appconfig.core.enforcePasswordForPublicLink) {
  712. OC.Share.showLink(data.token, "password set", itemSource);
  713. OC.Share.updateIcon(itemType, itemSource);
  714. }
  715. });
  716. }
  717. });
  718. $(document).on('click', '#dropdown #expirationCheckbox', function() {
  719. if (this.checked) {
  720. OC.Share.showExpirationDate('');
  721. } else {
  722. var itemType = $('#dropdown').data('item-type');
  723. var itemSource = $('#dropdown').data('item-source');
  724. $.post(OC.filePath('core', 'ajax', 'share.php'), { action: 'setExpirationDate', itemType: itemType, itemSource: itemSource, date: '' }, function(result) {
  725. if (!result || result.status !== 'success') {
  726. OC.dialogs.alert(t('core', 'Error unsetting expiration date'), t('core', 'Error'));
  727. }
  728. $('#expirationDate').hide('blind');
  729. if (oc_appconfig.core.defaultExpireDateEnforced === 'no') {
  730. $('#defaultExpireMessage'). show('blind');
  731. }
  732. });
  733. }
  734. });
  735. $(document).on('change', '#dropdown #expirationDate', function() {
  736. var itemType = $('#dropdown').data('item-type');
  737. var itemSource = $('#dropdown').data('item-source');
  738. $(this).tipsy('hide');
  739. $(this).removeClass('error');
  740. $.post(OC.filePath('core', 'ajax', 'share.php'), { action: 'setExpirationDate', itemType: itemType, itemSource: itemSource, date: $(this).val() }, function(result) {
  741. if (!result || result.status !== 'success') {
  742. var expirationDateField = $('#dropdown #expirationDate');
  743. if (!result.data.message) {
  744. expirationDateField.attr('original-title', t('core', 'Error setting expiration date'));
  745. } else {
  746. expirationDateField.attr('original-title', result.data.message);
  747. }
  748. expirationDateField.tipsy({gravity: 'n', fade: true});
  749. expirationDateField.tipsy('show');
  750. expirationDateField.addClass('error');
  751. } else {
  752. if (oc_appconfig.core.defaultExpireDateEnforced === 'no') {
  753. $('#defaultExpireMessage'). hide('blind');
  754. }
  755. }
  756. });
  757. });
  758. $(document).on('submit', '#dropdown #emailPrivateLink', function(event) {
  759. event.preventDefault();
  760. var link = $('#linkText').val();
  761. var itemType = $('#dropdown').data('item-type');
  762. var itemSource = $('#dropdown').data('item-source');
  763. var file = $('tr').filterAttr('data-id', String(itemSource)).data('file');
  764. var email = $('#email').val();
  765. var expirationDate = '';
  766. if ( $('#expirationCheckbox').is(':checked') === true ) {
  767. expirationDate = $( "#expirationDate" ).val();
  768. }
  769. if (email != '') {
  770. $('#email').prop('disabled', true);
  771. $('#email').val(t('core', 'Sending ...'));
  772. $('#emailButton').prop('disabled', true);
  773. $.post(OC.filePath('core', 'ajax', 'share.php'), { action: 'email', toaddress: email, link: link, itemType: itemType, itemSource: itemSource, file: file, expiration: expirationDate},
  774. function(result) {
  775. $('#email').prop('disabled', false);
  776. $('#emailButton').prop('disabled', false);
  777. if (result && result.status == 'success') {
  778. $('#email').css('font-weight', 'bold');
  779. $('#email').animate({ fontWeight: 'normal' }, 2000, function() {
  780. $(this).val('');
  781. }).val(t('core','Email sent'));
  782. } else {
  783. OC.dialogs.alert(result.data.message, t('core', 'Error while sharing'));
  784. }
  785. });
  786. }
  787. });
  788. $(document).on('click', '#dropdown input[name=mailNotification]', function() {
  789. var $li = $(this).closest('li');
  790. var itemType = $('#dropdown').data('item-type');
  791. var itemSource = $('#dropdown').data('item-source');
  792. var action = '';
  793. if (this.checked) {
  794. action = 'informRecipients';
  795. } else {
  796. action = 'informRecipientsDisabled';
  797. }
  798. var shareType = $li.data('share-type');
  799. var shareWith = $li.attr('data-share-with');
  800. $.post(OC.filePath('core', 'ajax', 'share.php'), {action: action, recipient: shareWith, shareType: shareType, itemSource: itemSource, itemType: itemType}, function(result) {
  801. if (result.status !== 'success') {
  802. OC.dialogs.alert(t('core', result.data.message), t('core', 'Warning'));
  803. }
  804. });
  805. });
  806. });