sharedialoglinkshareview.js 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951
  1. /*
  2. * Copyright (c) 2015
  3. *
  4. * This file is licensed under the Affero General Public License version 3
  5. * or later.
  6. *
  7. * See the COPYING-README file.
  8. *
  9. */
  10. /* globals Clipboard, Handlebars */
  11. (function() {
  12. if (!OC.Share) {
  13. OC.Share = {};
  14. }
  15. var PASSWORD_PLACEHOLDER = '**********';
  16. var PASSWORD_PLACEHOLDER_MESSAGE = t('core', 'Choose a password for the public link');
  17. var PASSWORD_PLACEHOLDER_MESSAGE_OPTIONAL = t('core', 'Choose a password for the public link or press the "Enter" key');
  18. /**
  19. * @class OCA.Share.ShareDialogLinkShareView
  20. * @member {OC.Share.ShareItemModel} model
  21. * @member {jQuery} $el
  22. * @memberof OCA.Sharing
  23. * @classdesc
  24. *
  25. * Represents the GUI of the share dialogue
  26. *
  27. */
  28. var ShareDialogLinkShareView = OC.Backbone.View.extend({
  29. /** @type {string} **/
  30. id: 'shareDialogLinkShare',
  31. /** @type {OC.Share.ShareConfigModel} **/
  32. configModel: undefined,
  33. /** @type {boolean} **/
  34. showLink: true,
  35. /** @type {boolean} **/
  36. showPending: false,
  37. /** @type {string} **/
  38. password: '',
  39. /** @type {string} **/
  40. newShareId: 'new-share',
  41. events: {
  42. // open menu
  43. 'click .share-menu .icon-more': 'onToggleMenu',
  44. // hide download
  45. 'change .hideDownloadCheckbox': 'onHideDownloadChange',
  46. // password
  47. 'click input.share-pass-submit': 'onPasswordEntered',
  48. 'keyup input.linkPassText': 'onPasswordKeyUp', // check for the enter key
  49. 'change .showPasswordCheckbox': 'onShowPasswordClick',
  50. 'change .passwordByTalkCheckbox': 'onPasswordByTalkChange',
  51. 'change .publicEditingCheckbox': 'onAllowPublicEditingChange',
  52. // copy link url
  53. 'click .linkText': 'onLinkTextClick',
  54. // social
  55. 'click .pop-up': 'onPopUpClick',
  56. // permission change
  57. 'change .publicUploadRadio': 'onPublicUploadChange',
  58. // expire date
  59. 'click .expireDate' : 'onExpireDateChange',
  60. 'change .datepicker': 'onChangeExpirationDate',
  61. 'click .datepicker' : 'showDatePicker',
  62. // note
  63. 'click .share-add': 'showNoteForm',
  64. 'click .share-note-delete': 'deleteNote',
  65. 'click .share-note-submit': 'updateNote',
  66. // remove
  67. 'click .unshare': 'onUnshare',
  68. // new share
  69. 'click .new-share': 'newShare',
  70. // enforced pass set
  71. 'submit .enforcedPassForm': 'enforcedPasswordSet',
  72. },
  73. initialize: function(options) {
  74. var view = this;
  75. this.model.on('change:permissions', function() {
  76. view.render();
  77. });
  78. this.model.on('change:itemType', function() {
  79. view.render();
  80. });
  81. this.model.on('change:allowPublicUploadStatus', function() {
  82. view.render();
  83. });
  84. this.model.on('change:hideFileListStatus', function() {
  85. view.render();
  86. });
  87. this.model.on('change:linkShares', function(model, linkShares) {
  88. // The "Password protect by Talk" item is shown only when there
  89. // is a password. Unfortunately there is no fine grained
  90. // rendering of items in the link shares, so the whole view
  91. // needs to be rendered again when the password of a share
  92. // changes.
  93. // Note that this event handler is concerned only about password
  94. // changes; other changes in the link shares does not trigger
  95. // a rendering, so the view must be rendered again as needed in
  96. // those cases (for example, when a link share is removed).
  97. var previousLinkShares = model.previous('linkShares');
  98. if (previousLinkShares.length !== linkShares.length) {
  99. return;
  100. }
  101. var i;
  102. for (i = 0; i < linkShares.length; i++) {
  103. if (linkShares[i].id !== previousLinkShares[i].id) {
  104. // A resorting should never happen, but just in case.
  105. return;
  106. }
  107. if (linkShares[i].password !== previousLinkShares[i].password) {
  108. view.render();
  109. return;
  110. }
  111. }
  112. });
  113. if(!_.isUndefined(options.configModel)) {
  114. this.configModel = options.configModel;
  115. } else {
  116. throw 'missing OC.Share.ShareConfigModel';
  117. }
  118. var clipboard = new Clipboard('.clipboard-button');
  119. clipboard.on('success', function(e) {
  120. var $trigger = $(e.trigger);
  121. $trigger.tooltip('hide')
  122. .attr('data-original-title', t('core', 'Copied!'))
  123. .tooltip('fixTitle')
  124. .tooltip({placement: 'bottom', trigger: 'manual'})
  125. .tooltip('show');
  126. _.delay(function() {
  127. $trigger.tooltip('hide')
  128. .attr('data-original-title', t('core', 'Copy link'))
  129. .tooltip('fixTitle')
  130. }, 3000);
  131. });
  132. clipboard.on('error', function (e) {
  133. var $trigger = $(e.trigger);
  134. var $menu = $trigger.next('.share-menu').find('.popovermenu');
  135. var $linkTextMenu = $menu.find('li.linkTextMenu');
  136. var $input = $linkTextMenu.find('.linkText');
  137. var $li = $trigger.closest('li[data-share-id]');
  138. var shareId = $li.data('share-id');
  139. // show menu
  140. OC.showMenu(null, $menu);
  141. var actionMsg = '';
  142. if (/iPhone|iPad/i.test(navigator.userAgent)) {
  143. actionMsg = t('core', 'Not supported!');
  144. } else if (/Mac/i.test(navigator.userAgent)) {
  145. actionMsg = t('core', 'Press ⌘-C to copy.');
  146. } else {
  147. actionMsg = t('core', 'Press Ctrl-C to copy.');
  148. }
  149. $linkTextMenu.removeClass('hidden');
  150. $input.select();
  151. $input.tooltip('hide')
  152. .attr('data-original-title', actionMsg)
  153. .tooltip('fixTitle')
  154. .tooltip({placement: 'bottom', trigger: 'manual'})
  155. .tooltip('show');
  156. _.delay(function () {
  157. $input.tooltip('hide');
  158. $input.attr('data-original-title', t('core', 'Copy'))
  159. .tooltip('fixTitle');
  160. }, 3000);
  161. });
  162. },
  163. newShare: function(event) {
  164. var self = this;
  165. var $target = $(event.target);
  166. var $li = $target.closest('li[data-share-id]');
  167. var shareId = $li.data('share-id');
  168. var $loading = $li.find('.share-menu > .icon-loading-small');
  169. if(!$loading.hasClass('hidden') && this.password === '') {
  170. // in process
  171. return false;
  172. }
  173. // hide all icons and show loading
  174. $li.find('.icon').addClass('hidden');
  175. $loading.removeClass('hidden');
  176. // hide menu
  177. OC.hideMenus();
  178. var shareData = {}
  179. var isPasswordEnforced = this.configModel.get('enforcePasswordForPublicLink');
  180. var isExpirationEnforced = this.configModel.get('isDefaultExpireDateEnforced');
  181. // set default expire date
  182. if (isExpirationEnforced) {
  183. var defaultExpireDays = this.configModel.get('defaultExpireDate');
  184. var expireDate = moment().add(defaultExpireDays, 'day').format('DD-MM-YYYY')
  185. shareData.expireDate = expireDate;
  186. }
  187. // if password is set, add to data
  188. if (isPasswordEnforced && this.password !== '') {
  189. shareData.password = this.password
  190. }
  191. var newShareId = false;
  192. // We need a password before the share creation
  193. if (isPasswordEnforced && !this.showPending && this.password === '') {
  194. this.showPending = shareId;
  195. var self = this.render();
  196. self.$el.find('.pending #enforcedPassText').focus();
  197. } else {
  198. // else, we have a password or it is not enforced
  199. $.when(this.model.saveLinkShare(shareData, {
  200. success: function() {
  201. $loading.addClass('hidden');
  202. $li.find('.icon').removeClass('hidden');
  203. self.render();
  204. // open the menu by default
  205. // we can only do that after the render
  206. if (newShareId) {
  207. var shares = self.$el.find('li[data-share-id]');
  208. var $newShare = self.$el.find('li[data-share-id="'+newShareId+'"]');
  209. // only open the menu by default if this is the first share
  210. if ($newShare && shares.length === 1) {
  211. $menu = $newShare.find('.popovermenu');
  212. OC.showMenu(null, $menu);
  213. }
  214. }
  215. },
  216. error: function() {
  217. // empty function to override the default Dialog warning
  218. }
  219. })).fail(function(response) {
  220. // password failure? Show error
  221. self.password = ''
  222. if (isPasswordEnforced && response && response.responseJSON && response.responseJSON.ocs.meta && response.responseJSON.ocs.meta.message) {
  223. $input = self.$el.find('.pending #enforcedPassText')
  224. $input.tooltip('destroy');
  225. $input.attr('title', response.responseJSON.ocs.meta.message);
  226. $input.tooltip({placement: 'bottom', trigger: 'manual'});
  227. $input.tooltip('show');
  228. } else {
  229. OC.Notification.showTemporary(t('core', 'Unable to create a link share'));
  230. $loading.addClass('hidden');
  231. $li.find('.icon').removeClass('hidden');
  232. }
  233. }).then(function(response) {
  234. // resolve before success
  235. newShareId = response.ocs.data.id
  236. });
  237. }
  238. },
  239. enforcedPasswordSet: function(event) {
  240. event.preventDefault();
  241. var $form = $(event.target);
  242. var $input = $form.find('input.enforcedPassText');
  243. this.password = $input.val();
  244. this.showPending = false;
  245. this.newShare(event);
  246. },
  247. onLinkTextClick: function(event) {
  248. var $element = $(event.target);
  249. var $li = $element.closest('li[data-share-id]');
  250. var $el = $li.find('.linkText');
  251. $el.focus();
  252. $el.select();
  253. },
  254. onHideDownloadChange: function(event) {
  255. var $element = $(event.target);
  256. var $li = $element.closest('li[data-share-id]');
  257. var shareId = $li.data('share-id');
  258. var $checkbox = $li.find('.hideDownloadCheckbox');
  259. $checkbox.siblings('.icon-loading-small').removeClass('hidden').addClass('inlineblock');
  260. var hideDownload = false;
  261. if($checkbox.is(':checked')) {
  262. hideDownload = true;
  263. }
  264. this.model.saveLinkShare({
  265. hideDownload: hideDownload,
  266. cid: shareId
  267. }, {
  268. success: function() {
  269. $checkbox.siblings('.icon-loading-small').addClass('hidden').removeClass('inlineblock');
  270. },
  271. error: function(obj, msg) {
  272. OC.Notification.showTemporary(t('core', 'Unable to toggle this option'));
  273. $checkbox.siblings('.icon-loading-small').addClass('hidden').removeClass('inlineblock');
  274. }
  275. });
  276. },
  277. onShowPasswordClick: function(event) {
  278. var $element = $(event.target);
  279. var $li = $element.closest('li[data-share-id]');
  280. var shareId = $li.data('share-id');
  281. $li.find('.linkPass').slideToggle(OC.menuSpeed);
  282. $li.find('.linkPassMenu').toggleClass('hidden');
  283. if(!$li.find('.showPasswordCheckbox').is(':checked')) {
  284. this.model.saveLinkShare({
  285. password: '',
  286. cid: shareId
  287. });
  288. } else {
  289. if (!OC.Util.isIE()) {
  290. $li.find('.linkPassText').focus();
  291. }
  292. }
  293. },
  294. onPasswordKeyUp: function(event) {
  295. if(event.keyCode === 13) {
  296. this.onPasswordEntered(event);
  297. }
  298. },
  299. onPasswordEntered: function(event) {
  300. var $element = $(event.target);
  301. var $li = $element.closest('li[data-share-id]');
  302. var shareId = $li.data('share-id');
  303. var $loading = $li.find('.linkPassMenu .icon-loading-small');
  304. if (!$loading.hasClass('hidden')) {
  305. // still in process
  306. return;
  307. }
  308. var $input = $li.find('.linkPassText');
  309. $input.removeClass('error');
  310. var password = $input.val();
  311. if ($li.find('.linkPassText').attr('placeholder') === PASSWORD_PLACEHOLDER_MESSAGE_OPTIONAL) {
  312. // in IE9 the password might be the placeholder due to bugs in the placeholders polyfill
  313. if(password === PASSWORD_PLACEHOLDER_MESSAGE_OPTIONAL) {
  314. password = '';
  315. }
  316. } else {
  317. // in IE9 the password might be the placeholder due to bugs in the placeholders polyfill
  318. if(password === '' || password === PASSWORD_PLACEHOLDER || password === PASSWORD_PLACEHOLDER_MESSAGE) {
  319. return;
  320. }
  321. }
  322. $loading
  323. .removeClass('hidden')
  324. .addClass('inlineblock');
  325. this.model.saveLinkShare({
  326. password: password,
  327. cid: shareId
  328. }, {
  329. complete: function(model) {
  330. $loading.removeClass('inlineblock').addClass('hidden');
  331. },
  332. error: function(model, msg) {
  333. // destroy old tooltips
  334. var $container = $input.parent();
  335. $container.tooltip('destroy');
  336. $input.addClass('error');
  337. $container.attr('title', msg);
  338. $container.tooltip({placement: 'bottom', trigger: 'manual'});
  339. $container.tooltip('show');
  340. }
  341. });
  342. },
  343. onPasswordByTalkChange: function(event) {
  344. var $element = $(event.target);
  345. var $li = $element.closest('li[data-share-id]');
  346. var shareId = $li.data('share-id');
  347. var $checkbox = $li.find('.passwordByTalkCheckbox');
  348. $checkbox.siblings('.icon-loading-small').removeClass('hidden').addClass('inlineblock');
  349. var sendPasswordByTalk = false;
  350. if($checkbox.is(':checked')) {
  351. sendPasswordByTalk = true;
  352. }
  353. this.model.saveLinkShare({
  354. sendPasswordByTalk: sendPasswordByTalk,
  355. cid: shareId
  356. }, {
  357. success: function() {
  358. $checkbox.siblings('.icon-loading-small').addClass('hidden').removeClass('inlineblock');
  359. },
  360. error: function(obj, msg) {
  361. OC.Notification.showTemporary(t('core', 'Unable to toggle this option'));
  362. $checkbox.siblings('.icon-loading-small').addClass('hidden').removeClass('inlineblock');
  363. }
  364. });
  365. },
  366. onAllowPublicEditingChange: function(event) {
  367. var $element = $(event.target);
  368. var $li = $element.closest('li[data-share-id]');
  369. var shareId = $li.data('share-id');
  370. var $checkbox = $li.find('.publicEditingCheckbox');
  371. $checkbox.siblings('.icon-loading-small').removeClass('hidden').addClass('inlineblock');
  372. var permissions = OC.PERMISSION_READ;
  373. if($checkbox.is(':checked')) {
  374. permissions = OC.PERMISSION_UPDATE | OC.PERMISSION_READ;
  375. }
  376. this.model.saveLinkShare({
  377. permissions: permissions,
  378. cid: shareId
  379. }, {
  380. success: function() {
  381. $checkbox.siblings('.icon-loading-small').addClass('hidden').removeClass('inlineblock');
  382. },
  383. error: function(obj, msg) {
  384. OC.Notification.showTemporary(t('core', 'Unable to toggle this option'));
  385. $checkbox.siblings('.icon-loading-small').addClass('hidden').removeClass('inlineblock');
  386. }
  387. });
  388. },
  389. onPublicUploadChange: function(event) {
  390. var $element = $(event.target);
  391. var $li = $element.closest('li[data-share-id]');
  392. var shareId = $li.data('share-id');
  393. var permissions = event.currentTarget.value;
  394. this.model.saveLinkShare({
  395. permissions: permissions,
  396. cid: shareId
  397. });
  398. },
  399. showNoteForm: function(event) {
  400. event.preventDefault();
  401. event.stopPropagation();
  402. var self = this;
  403. var $element = $(event.target);
  404. var $li = $element.closest('li[data-share-id]');
  405. var $menu = $element.closest('li');
  406. var $form = $menu.next('li.share-note-form');
  407. // show elements
  408. $menu.find('.share-note-delete').toggleClass('hidden');
  409. $form.toggleClass('hidden');
  410. $form.find('textarea').focus();
  411. },
  412. deleteNote: function(event) {
  413. event.preventDefault();
  414. event.stopPropagation();
  415. var self = this;
  416. var $element = $(event.target);
  417. var $li = $element.closest('li[data-share-id]');
  418. var shareId = $li.data('share-id');
  419. var $menu = $element.closest('li');
  420. var $form = $menu.next('li.share-note-form');
  421. $form.find('.share-note').val('');
  422. $form.addClass('hidden');
  423. $menu.find('.share-note-delete').addClass('hidden');
  424. self.sendNote('', shareId, $menu);
  425. },
  426. updateNote: function(event) {
  427. event.preventDefault();
  428. event.stopPropagation();
  429. var self = this;
  430. var $element = $(event.target);
  431. var $li = $element.closest('li[data-share-id]');
  432. var shareId = $li.data('share-id');
  433. var $form = $element.closest('li.share-note-form');
  434. var $menu = $form.prev('li');
  435. var message = $form.find('.share-note').val().trim();
  436. if (message.length < 1) {
  437. return;
  438. }
  439. self.sendNote(message, shareId, $menu);
  440. },
  441. sendNote: function(note, shareId, $menu) {
  442. var $form = $menu.next('li.share-note-form');
  443. var $submit = $form.find('input.share-note-submit');
  444. var $error = $form.find('input.share-note-error');
  445. $submit.prop('disabled', true);
  446. $menu.find('.icon-loading-small').removeClass('hidden');
  447. $menu.find('.icon-edit').hide();
  448. var complete = function() {
  449. $submit.prop('disabled', false);
  450. $menu.find('.icon-loading-small').addClass('hidden');
  451. $menu.find('.icon-edit').show();
  452. };
  453. var error = function() {
  454. $error.show();
  455. setTimeout(function() {
  456. $error.hide();
  457. }, 3000);
  458. };
  459. // send data
  460. $.ajax({
  461. method: 'PUT',
  462. url: OC.linkToOCS('apps/files_sharing/api/v1/shares',2) + shareId + '?' + OC.buildQueryString({format: 'json'}),
  463. data: { note: note },
  464. complete : complete,
  465. error: error
  466. });
  467. },
  468. render: function() {
  469. this.$el.find('.has-tooltip').tooltip();
  470. // reset previously set passwords
  471. this.password = '';
  472. var linkShareTemplate = this.template();
  473. var resharingAllowed = this.model.sharePermissionPossible();
  474. if(!resharingAllowed
  475. || !this.showLink
  476. || !this.configModel.isShareWithLinkAllowed())
  477. {
  478. var templateData = {shareAllowed: false};
  479. if (!resharingAllowed) {
  480. // add message
  481. templateData.noSharingPlaceholder = t('core', 'Resharing is not allowed');
  482. }
  483. this.$el.html(linkShareTemplate(templateData));
  484. return this;
  485. }
  486. var publicUpload =
  487. this.model.isFolder()
  488. && this.model.createPermissionPossible()
  489. && this.configModel.isPublicUploadEnabled();
  490. var publicEditingChecked = '';
  491. if(this.model.isPublicEditingAllowed()) {
  492. publicEditingChecked = 'checked="checked"';
  493. }
  494. var isPasswordEnforced = this.configModel.get('enforcePasswordForPublicLink');
  495. var isPasswordEnabledByDefault = this.configModel.get('enableLinkPasswordByDefault') === true;
  496. var passwordPlaceholderInitial = this.configModel.get('enforcePasswordForPublicLink')
  497. ? PASSWORD_PLACEHOLDER_MESSAGE : PASSWORD_PLACEHOLDER_MESSAGE_OPTIONAL;
  498. var publicEditable =
  499. !this.model.isFolder()
  500. && this.model.updatePermissionPossible();
  501. var social = [];
  502. OC.Share.Social.Collection.each(function(model) {
  503. var url = model.get('url');
  504. url = url.replace('{{reference}}', link);
  505. social.push({
  506. url: url,
  507. label: t('core', 'Share to {name}', {name: model.get('name')}),
  508. name: model.get('name'),
  509. iconClass: model.get('iconClass'),
  510. newWindow: model.get('newWindow')
  511. });
  512. });
  513. var isExpirationEnforced = this.configModel.get('isDefaultExpireDateEnforced');
  514. // what if there is another date picker on that page?
  515. var minDate = new Date();
  516. // min date should always be the next day
  517. minDate.setDate(minDate.getDate()+1);
  518. $.datepicker.setDefaults({
  519. minDate: minDate
  520. });
  521. this.$el.find('.datepicker').datepicker({dateFormat : 'dd-mm-yy'});
  522. var minPasswordLength = 4
  523. // password policy?
  524. if(oc_capabilities.password_policy && oc_capabilities.password_policy.minLength) {
  525. minPasswordLength = oc_capabilities.password_policy.minLength;
  526. }
  527. var popoverBase = {
  528. social: social,
  529. urlLabel: t('core', 'Link'),
  530. hideDownloadLabel: t('core', 'Hide download'),
  531. enablePasswordLabel: isPasswordEnforced ? t('core', 'Password protection enforced') : t('core', 'Password protect'),
  532. passwordLabel: t('core', 'Password'),
  533. passwordPlaceholderInitial: passwordPlaceholderInitial,
  534. publicUpload: publicUpload,
  535. publicEditing: publicEditable,
  536. publicEditingChecked: publicEditingChecked,
  537. publicEditingLabel: t('core', 'Allow editing'),
  538. mailPrivatePlaceholder: t('core', 'Email link to person'),
  539. mailButtonText: t('core', 'Send'),
  540. publicUploadRWLabel: t('core', 'Allow upload and editing'),
  541. publicUploadRLabel: t('core', 'Read only'),
  542. publicUploadWLabel: t('core', 'File drop (upload only)'),
  543. publicUploadRWValue: OC.PERMISSION_UPDATE | OC.PERMISSION_CREATE | OC.PERMISSION_READ | OC.PERMISSION_DELETE,
  544. publicUploadRValue: OC.PERMISSION_READ,
  545. publicUploadWValue: OC.PERMISSION_CREATE,
  546. expireDateLabel: isExpirationEnforced ? t('core', 'Expiration date enforced') : t('core', 'Set expiration date'),
  547. expirationLabel: t('core', 'Expiration'),
  548. expirationDatePlaceholder: t('core', 'Expiration date'),
  549. isExpirationEnforced: isExpirationEnforced,
  550. isPasswordEnforced: isPasswordEnforced,
  551. defaultExpireDate: moment().add(1, 'day').format('DD-MM-YYYY'), // Can't expire today
  552. addNoteLabel: t('core', 'Note to recipient'),
  553. unshareLabel: t('core', 'Unshare'),
  554. unshareLinkLabel: t('core', 'Delete share link'),
  555. newShareLabel: t('core', 'Add another link'),
  556. };
  557. var pendingPopover = {
  558. isPasswordEnforced: isPasswordEnforced,
  559. enforcedPasswordLabel: t('core', 'Password protection for links is mandatory'),
  560. passwordPlaceholder: passwordPlaceholderInitial,
  561. minPasswordLength: minPasswordLength,
  562. };
  563. var pendingPopoverMenu = this.pendingPopoverMenuTemplate(_.extend({}, pendingPopover))
  564. var linkShares = this.getShareeList();
  565. if(_.isArray(linkShares)) {
  566. for (var i = 0; i < linkShares.length; i++) {
  567. var popover = this.getPopoverObject(linkShares[i])
  568. linkShares[i].popoverMenu = this.popoverMenuTemplate(_.extend({}, popoverBase, popover));
  569. linkShares[i].pendingPopoverMenu = pendingPopoverMenu
  570. }
  571. }
  572. this.$el.html(linkShareTemplate({
  573. linkShares: linkShares,
  574. shareAllowed: true,
  575. nolinkShares: linkShares.length === 0,
  576. newShareLabel: t('core', 'Share link'),
  577. newShareTitle: t('core', 'New share link'),
  578. pendingPopoverMenu: pendingPopoverMenu,
  579. showPending: this.showPending === this.newShareId,
  580. newShareId: this.newShareId,
  581. }));
  582. this.delegateEvents();
  583. // new note autosize
  584. autosize(this.$el.find('.share-note-form .share-note'));
  585. return this;
  586. },
  587. onToggleMenu: function(event) {
  588. event.preventDefault();
  589. event.stopPropagation();
  590. var $element = $(event.target);
  591. var $li = $element.closest('li[data-share-id]');
  592. var $menu = $li.find('.sharingOptionsGroup .popovermenu');
  593. var shareId = $li.data('share-id');
  594. OC.showMenu(null, $menu);
  595. // focus the password if not set and enforced
  596. var isPasswordEnabledByDefault = this.configModel.get('enableLinkPasswordByDefault') === true;
  597. var haspassword = $menu.find('.linkPassText').val() !== '';
  598. if (!haspassword && isPasswordEnabledByDefault) {
  599. $menu.find('.linkPassText').focus();
  600. }
  601. },
  602. /**
  603. * @returns {Function} from Handlebars
  604. * @private
  605. */
  606. template: function () {
  607. return OC.Share.Templates['sharedialoglinkshareview'];
  608. },
  609. /**
  610. * renders the popover template and returns the resulting HTML
  611. *
  612. * @param {Object} data
  613. * @returns {string}
  614. */
  615. popoverMenuTemplate: function(data) {
  616. return OC.Share.Templates['sharedialoglinkshareview_popover_menu'](data);
  617. },
  618. /**
  619. * renders the pending popover template and returns the resulting HTML
  620. *
  621. * @param {Object} data
  622. * @returns {string}
  623. */
  624. pendingPopoverMenuTemplate: function(data) {
  625. return OC.Share.Templates['sharedialoglinkshareview_popover_menu_pending'](data);
  626. },
  627. onPopUpClick: function(event) {
  628. event.preventDefault();
  629. event.stopPropagation();
  630. var url = $(event.currentTarget).data('url');
  631. var newWindow = $(event.currentTarget).data('window');
  632. $(event.currentTarget).tooltip('hide');
  633. if (url) {
  634. if (newWindow === true) {
  635. var width = 600;
  636. var height = 400;
  637. var left = (screen.width / 2) - (width / 2);
  638. var top = (screen.height / 2) - (height / 2);
  639. window.open(url, 'name', 'width=' + width + ', height=' + height + ', top=' + top + ', left=' + left);
  640. } else {
  641. window.location.href = url;
  642. }
  643. }
  644. },
  645. onExpireDateChange: function(event) {
  646. var $element = $(event.target);
  647. var li = $element.closest('li[data-share-id]');
  648. var shareId = li.data('share-id');
  649. var expirationDatePicker = '#expirationDateContainer-' + shareId;
  650. var datePicker = $(expirationDatePicker);
  651. var state = $element.prop('checked');
  652. datePicker.toggleClass('hidden', !state);
  653. if (!state) {
  654. // disabled, let's hide the input and
  655. // set the expireDate to nothing
  656. $element.closest('li').next('li').addClass('hidden');
  657. this.setExpirationDate('');
  658. } else {
  659. // enabled, show the input and the datepicker
  660. $element.closest('li').next('li').removeClass('hidden');
  661. this.showDatePicker(event);
  662. }
  663. },
  664. showDatePicker: function(event) {
  665. var $element = $(event.target);
  666. var li = $element.closest('li[data-share-id]');
  667. var shareId = li.data('share-id');
  668. var maxDate = $element.data('max-date');
  669. var expirationDatePicker = '#expirationDatePicker-' + shareId;
  670. var self = this;
  671. $(expirationDatePicker).datepicker({
  672. dateFormat : 'dd-mm-yy',
  673. onSelect: function (expireDate) {
  674. self.setExpirationDate(expireDate, shareId);
  675. },
  676. maxDate: maxDate
  677. });
  678. $(expirationDatePicker).datepicker('show');
  679. $(expirationDatePicker).focus();
  680. },
  681. setExpirationDate: function(expireDate, shareId) {
  682. this.model.saveLinkShare({expireDate: expireDate, cid: shareId});
  683. },
  684. /**
  685. * get an array of sharees' share properties
  686. *
  687. * @returns {Array}
  688. */
  689. getShareeList: function() {
  690. var shares = this.model.get('linkShares');
  691. if(!this.model.hasLinkShares()) {
  692. return [];
  693. }
  694. var list = [];
  695. for(var index = 0; index < shares.length; index++) {
  696. var share = this.getShareeObject(index);
  697. // first empty {} is necessary, otherwise we get in trouble
  698. // with references
  699. list.push(_.extend({}, share));
  700. }
  701. return list;
  702. },
  703. /**
  704. *
  705. * @param {OC.Share.Types.ShareInfo} shareInfo
  706. * @returns {object}
  707. */
  708. getShareeObject: function(shareIndex) {
  709. var share = this.model.get('linkShares')[shareIndex];
  710. return _.extend({}, share, {
  711. cid: share.id,
  712. shareAllowed: true,
  713. linkShareLabel: share.label ? share.label : t('core', 'Share link'),
  714. popoverMenu: {},
  715. shareLinkURL: share.url,
  716. newShareTitle: t('core', 'New share link'),
  717. copyLabel: t('core', 'Copy link'),
  718. showPending: this.showPending === share.id,
  719. linkShareCreationDate: t('core', 'Created on {time}', { time: moment(share.stime * 1000).format('LLLL') })
  720. })
  721. },
  722. getPopoverObject: function(share) {
  723. var publicUploadRWChecked = '';
  724. var publicUploadRChecked = '';
  725. var publicUploadWChecked = '';
  726. switch (this.model.linkSharePermissions(share.id)) {
  727. case OC.PERMISSION_READ:
  728. publicUploadRChecked = 'checked';
  729. break;
  730. case OC.PERMISSION_CREATE:
  731. publicUploadWChecked = 'checked';
  732. break;
  733. case OC.PERMISSION_UPDATE | OC.PERMISSION_CREATE | OC.PERMISSION_READ | OC.PERMISSION_DELETE:
  734. publicUploadRWChecked = 'checked';
  735. break;
  736. }
  737. var isPasswordSet = !!share.password;
  738. var isPasswordEnabledByDefault = this.configModel.get('enableLinkPasswordByDefault') === true;
  739. var isPasswordEnforced = this.configModel.get('enforcePasswordForPublicLink');
  740. var showPasswordCheckBox = !isPasswordEnforced || !share.password;
  741. var isExpirationEnforced = this.configModel.get('isDefaultExpireDateEnforced');
  742. var defaultExpireDays = this.configModel.get('defaultExpireDate');
  743. var hasExpireDate = !!share.expiration || isExpirationEnforced;
  744. var expireDate;
  745. if (hasExpireDate) {
  746. expireDate = moment(share.expiration, 'YYYY-MM-DD').format('DD-MM-YYYY');
  747. }
  748. var isTalkEnabled = oc_appswebroots['spreed'] !== undefined;
  749. var sendPasswordByTalk = share.sendPasswordByTalk;
  750. var hideDownload = share.hideDownload;
  751. var maxDate = null;
  752. if(hasExpireDate) {
  753. if(isExpirationEnforced) {
  754. // TODO: hack: backend returns string instead of integer
  755. var shareTime = share.stime;
  756. if (_.isNumber(shareTime)) {
  757. shareTime = new Date(shareTime * 1000);
  758. }
  759. if (!shareTime) {
  760. shareTime = new Date(); // now
  761. }
  762. shareTime = OC.Util.stripTime(shareTime).getTime();
  763. maxDate = new Date(shareTime + defaultExpireDays * 24 * 3600 * 1000);
  764. }
  765. }
  766. return {
  767. cid: share.id,
  768. shareLinkURL: share.url,
  769. passwordPlaceholder: isPasswordSet ? PASSWORD_PLACEHOLDER : PASSWORD_PLACEHOLDER_MESSAGE,
  770. isPasswordSet: isPasswordSet || isPasswordEnabledByDefault || isPasswordEnforced,
  771. showPasswordCheckBox: showPasswordCheckBox,
  772. showPasswordByTalkCheckBox: isTalkEnabled && isPasswordSet,
  773. passwordByTalkLabel: t('core', 'Password protect by Talk'),
  774. isPasswordByTalkSet: sendPasswordByTalk,
  775. publicUploadRWChecked: publicUploadRWChecked,
  776. publicUploadRChecked: publicUploadRChecked,
  777. publicUploadWChecked: publicUploadWChecked,
  778. hasExpireDate: hasExpireDate,
  779. expireDate: expireDate,
  780. shareNote: share.note,
  781. hasNote: share.note !== '',
  782. maxDate: maxDate,
  783. hideDownload: hideDownload,
  784. isExpirationEnforced: isExpirationEnforced,
  785. }
  786. },
  787. onUnshare: function(event) {
  788. event.preventDefault();
  789. event.stopPropagation();
  790. var self = this;
  791. var $element = $(event.target);
  792. if (!$element.is('a')) {
  793. $element = $element.closest('a');
  794. }
  795. var $loading = $element.find('.icon-loading-small').eq(0);
  796. if(!$loading.hasClass('hidden')) {
  797. // in process
  798. return false;
  799. }
  800. $loading.removeClass('hidden');
  801. var $li = $element.closest('li[data-share-id]');
  802. var shareId = $li.data('share-id');
  803. self.model.removeShare(shareId, {
  804. success: function() {
  805. $li.remove();
  806. self.render()
  807. },
  808. error: function() {
  809. $loading.addClass('hidden');
  810. OC.Notification.showTemporary(t('core', 'Could not unshare'));
  811. }
  812. });
  813. return false;
  814. },
  815. });
  816. OC.Share.ShareDialogLinkShareView = ShareDialogLinkShareView;
  817. })();