share.js 28 KB

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