sharedialoglinkshareview.js 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962
  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. var $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. var $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. $input.parent().find('input').removeClass('error');
  311. var password = $input.val();
  312. if ($li.find('.linkPassText').attr('placeholder') === PASSWORD_PLACEHOLDER_MESSAGE_OPTIONAL) {
  313. // in IE9 the password might be the placeholder due to bugs in the placeholders polyfill
  314. if(password === PASSWORD_PLACEHOLDER_MESSAGE_OPTIONAL) {
  315. password = '';
  316. }
  317. } else {
  318. // in IE9 the password might be the placeholder due to bugs in the placeholders polyfill
  319. if(password === '' || password === PASSWORD_PLACEHOLDER || password === PASSWORD_PLACEHOLDER_MESSAGE) {
  320. return;
  321. }
  322. }
  323. $loading
  324. .removeClass('hidden')
  325. .addClass('inlineblock');
  326. this.model.saveLinkShare({
  327. password: password,
  328. cid: shareId
  329. }, {
  330. complete: function(model) {
  331. $loading.removeClass('inlineblock').addClass('hidden');
  332. },
  333. error: function(model, msg) {
  334. // Add visual feedback to both the input and the submit button
  335. $input.parent().find('input').addClass('error');
  336. // destroy old tooltips
  337. var $container = $input.parent();
  338. $container.tooltip('destroy');
  339. $input.addClass('error');
  340. $container.attr('title', msg);
  341. $container.tooltip({placement: 'bottom', trigger: 'manual'});
  342. $container.tooltip('show');
  343. }
  344. });
  345. },
  346. onPasswordByTalkChange: function(event) {
  347. var $element = $(event.target);
  348. var $li = $element.closest('li[data-share-id]');
  349. var shareId = $li.data('share-id');
  350. var $checkbox = $li.find('.passwordByTalkCheckbox');
  351. $checkbox.siblings('.icon-loading-small').removeClass('hidden').addClass('inlineblock');
  352. var sendPasswordByTalk = false;
  353. if($checkbox.is(':checked')) {
  354. sendPasswordByTalk = true;
  355. }
  356. this.model.saveLinkShare({
  357. sendPasswordByTalk: sendPasswordByTalk,
  358. cid: shareId
  359. }, {
  360. success: function() {
  361. $checkbox.siblings('.icon-loading-small').addClass('hidden').removeClass('inlineblock');
  362. },
  363. error: function(obj, msg) {
  364. OC.Notification.showTemporary(t('core', 'Unable to toggle this option'));
  365. $checkbox.siblings('.icon-loading-small').addClass('hidden').removeClass('inlineblock');
  366. }
  367. });
  368. },
  369. onAllowPublicEditingChange: function(event) {
  370. var $element = $(event.target);
  371. var $li = $element.closest('li[data-share-id]');
  372. var shareId = $li.data('share-id');
  373. var $checkbox = $li.find('.publicEditingCheckbox');
  374. $checkbox.siblings('.icon-loading-small').removeClass('hidden').addClass('inlineblock');
  375. var permissions = OC.PERMISSION_READ;
  376. if($checkbox.is(':checked')) {
  377. permissions = OC.PERMISSION_UPDATE | OC.PERMISSION_READ;
  378. }
  379. this.model.saveLinkShare({
  380. permissions: permissions,
  381. cid: shareId
  382. }, {
  383. success: function() {
  384. $checkbox.siblings('.icon-loading-small').addClass('hidden').removeClass('inlineblock');
  385. },
  386. error: function(obj, msg) {
  387. OC.Notification.showTemporary(t('core', 'Unable to toggle this option'));
  388. $checkbox.siblings('.icon-loading-small').addClass('hidden').removeClass('inlineblock');
  389. }
  390. });
  391. },
  392. onPublicUploadChange: function(event) {
  393. var $element = $(event.target);
  394. var $li = $element.closest('li[data-share-id]');
  395. var shareId = $li.data('share-id');
  396. var permissions = event.currentTarget.value;
  397. this.model.saveLinkShare({
  398. permissions: permissions,
  399. cid: shareId
  400. });
  401. },
  402. showNoteForm: function(event) {
  403. event.preventDefault();
  404. event.stopPropagation();
  405. var self = this;
  406. var $element = $(event.target);
  407. var $li = $element.closest('li[data-share-id]');
  408. var $menu = $element.closest('li');
  409. var $form = $menu.next('li.share-note-form');
  410. // show elements
  411. $menu.find('.share-note-delete').toggleClass('hidden');
  412. $form.toggleClass('hidden');
  413. $form.find('textarea').focus();
  414. },
  415. deleteNote: function(event) {
  416. event.preventDefault();
  417. event.stopPropagation();
  418. var self = this;
  419. var $element = $(event.target);
  420. var $li = $element.closest('li[data-share-id]');
  421. var shareId = $li.data('share-id');
  422. var $menu = $element.closest('li');
  423. var $form = $menu.next('li.share-note-form');
  424. $form.find('.share-note').val('');
  425. $form.addClass('hidden');
  426. $menu.find('.share-note-delete').addClass('hidden');
  427. self.sendNote('', shareId, $menu);
  428. },
  429. updateNote: function(event) {
  430. event.preventDefault();
  431. event.stopPropagation();
  432. var self = this;
  433. var $element = $(event.target);
  434. var $li = $element.closest('li[data-share-id]');
  435. var shareId = $li.data('share-id');
  436. var $form = $element.closest('li.share-note-form');
  437. var $menu = $form.prev('li');
  438. var message = $form.find('.share-note').val().trim();
  439. if (message.length < 1) {
  440. return;
  441. }
  442. self.sendNote(message, shareId, $menu);
  443. },
  444. sendNote: function(note, shareId, $menu) {
  445. var $form = $menu.next('li.share-note-form');
  446. var $submit = $form.find('input.share-note-submit');
  447. var $error = $form.find('input.share-note-error');
  448. $submit.prop('disabled', true);
  449. $menu.find('.icon-loading-small').removeClass('hidden');
  450. $menu.find('.icon-edit').hide();
  451. var complete = function() {
  452. $submit.prop('disabled', false);
  453. $menu.find('.icon-loading-small').addClass('hidden');
  454. $menu.find('.icon-edit').show();
  455. };
  456. var error = function() {
  457. $error.show();
  458. setTimeout(function() {
  459. $error.hide();
  460. }, 3000);
  461. };
  462. // send data
  463. $.ajax({
  464. method: 'PUT',
  465. url: OC.linkToOCS('apps/files_sharing/api/v1/shares',2) + shareId + '?' + OC.buildQueryString({format: 'json'}),
  466. data: { note: note },
  467. complete : complete,
  468. error: error
  469. });
  470. },
  471. render: function() {
  472. this.$el.find('.has-tooltip').tooltip();
  473. // reset previously set passwords
  474. this.password = '';
  475. var linkShareTemplate = this.template();
  476. var resharingAllowed = this.model.sharePermissionPossible();
  477. if(!resharingAllowed
  478. || !this.showLink
  479. || !this.configModel.isShareWithLinkAllowed())
  480. {
  481. var templateData = {shareAllowed: false};
  482. if (!resharingAllowed) {
  483. // add message
  484. templateData.noSharingPlaceholder = t('core', 'Resharing is not allowed');
  485. }
  486. this.$el.html(linkShareTemplate(templateData));
  487. return this;
  488. }
  489. var publicUpload =
  490. this.model.isFolder()
  491. && this.model.createPermissionPossible()
  492. && this.configModel.isPublicUploadEnabled();
  493. var publicEditingChecked = '';
  494. if(this.model.isPublicEditingAllowed()) {
  495. publicEditingChecked = 'checked="checked"';
  496. }
  497. var isPasswordEnforced = this.configModel.get('enforcePasswordForPublicLink');
  498. var isPasswordEnabledByDefault = this.configModel.get('enableLinkPasswordByDefault') === true;
  499. var passwordPlaceholderInitial = this.configModel.get('enforcePasswordForPublicLink')
  500. ? PASSWORD_PLACEHOLDER_MESSAGE : PASSWORD_PLACEHOLDER_MESSAGE_OPTIONAL;
  501. var publicEditable =
  502. !this.model.isFolder()
  503. && this.model.updatePermissionPossible();
  504. var isExpirationEnforced = this.configModel.get('isDefaultExpireDateEnforced');
  505. // what if there is another date picker on that page?
  506. var minDate = new Date();
  507. // min date should always be the next day
  508. minDate.setDate(minDate.getDate()+1);
  509. $.datepicker.setDefaults({
  510. minDate: minDate
  511. });
  512. this.$el.find('.datepicker').datepicker({dateFormat : 'dd-mm-yy'});
  513. var minPasswordLength = 4
  514. // password policy?
  515. if(OC.getCapabilities().password_policy && OC.getCapabilities().password_policy.minLength) {
  516. minPasswordLength = OC.getCapabilities().password_policy.minLength;
  517. }
  518. var popoverBase = {
  519. urlLabel: t('core', 'Link'),
  520. hideDownloadLabel: t('core', 'Hide download'),
  521. enablePasswordLabel: isPasswordEnforced ? t('core', 'Password protection enforced') : t('core', 'Password protect'),
  522. passwordLabel: t('core', 'Password'),
  523. passwordPlaceholderInitial: passwordPlaceholderInitial,
  524. publicUpload: publicUpload,
  525. publicEditing: publicEditable,
  526. publicEditingChecked: publicEditingChecked,
  527. publicEditingLabel: t('core', 'Allow editing'),
  528. mailPrivatePlaceholder: t('core', 'Email link to person'),
  529. mailButtonText: t('core', 'Send'),
  530. publicUploadRWLabel: t('core', 'Allow upload and editing'),
  531. publicUploadRLabel: t('core', 'Read only'),
  532. publicUploadWLabel: t('core', 'File drop (upload only)'),
  533. publicUploadRWValue: OC.PERMISSION_UPDATE | OC.PERMISSION_CREATE | OC.PERMISSION_READ | OC.PERMISSION_DELETE,
  534. publicUploadRValue: OC.PERMISSION_READ,
  535. publicUploadWValue: OC.PERMISSION_CREATE,
  536. expireDateLabel: isExpirationEnforced ? t('core', 'Expiration date enforced') : t('core', 'Set expiration date'),
  537. expirationLabel: t('core', 'Expiration'),
  538. expirationDatePlaceholder: t('core', 'Expiration date'),
  539. isExpirationEnforced: isExpirationEnforced,
  540. isPasswordEnforced: isPasswordEnforced,
  541. defaultExpireDate: moment().add(1, 'day').format('DD-MM-YYYY'), // Can't expire today
  542. addNoteLabel: t('core', 'Note to recipient'),
  543. unshareLabel: t('core', 'Unshare'),
  544. unshareLinkLabel: t('core', 'Delete share link'),
  545. newShareLabel: t('core', 'Add another link'),
  546. };
  547. var pendingPopover = {
  548. isPasswordEnforced: isPasswordEnforced,
  549. enforcedPasswordLabel: t('core', 'Password protection for links is mandatory'),
  550. passwordPlaceholder: passwordPlaceholderInitial,
  551. minPasswordLength: minPasswordLength,
  552. };
  553. var pendingPopoverMenu = this.pendingPopoverMenuTemplate(_.extend({}, pendingPopover))
  554. var linkShares = this.getShareeList();
  555. if(_.isArray(linkShares)) {
  556. for (var i = 0; i < linkShares.length; i++) {
  557. var social = [];
  558. OC.Share.Social.Collection.each(function (model) {
  559. var url = model.get('url');
  560. url = url.replace('{{reference}}', linkShares[i].shareLinkURL);
  561. social.push({
  562. url: url,
  563. label: t('core', 'Share to {name}', {name: model.get('name')}),
  564. name: model.get('name'),
  565. iconClass: model.get('iconClass'),
  566. newWindow: model.get('newWindow')
  567. });
  568. });
  569. var popover = this.getPopoverObject(linkShares[i])
  570. linkShares[i].popoverMenu = this.popoverMenuTemplate(_.extend({}, popoverBase, popover, {social: social}));
  571. linkShares[i].pendingPopoverMenu = pendingPopoverMenu
  572. }
  573. }
  574. this.$el.html(linkShareTemplate({
  575. linkShares: linkShares,
  576. shareAllowed: true,
  577. nolinkShares: linkShares.length === 0,
  578. newShareLabel: t('core', 'Share link'),
  579. newShareTitle: t('core', 'New share link'),
  580. pendingPopoverMenu: pendingPopoverMenu,
  581. showPending: this.showPending === this.newShareId,
  582. newShareId: this.newShareId,
  583. }));
  584. this.delegateEvents();
  585. // new note autosize
  586. autosize(this.$el.find('.share-note-form .share-note'));
  587. return this;
  588. },
  589. onToggleMenu: function(event) {
  590. event.preventDefault();
  591. event.stopPropagation();
  592. var $element = $(event.target);
  593. var $li = $element.closest('li[data-share-id]');
  594. var $menu = $li.find('.sharingOptionsGroup .popovermenu');
  595. var shareId = $li.data('share-id');
  596. OC.showMenu(null, $menu);
  597. // focus the password if not set and enforced
  598. var isPasswordEnabledByDefault = this.configModel.get('enableLinkPasswordByDefault') === true;
  599. var haspassword = $menu.find('.linkPassText').val() !== '';
  600. if (!haspassword && isPasswordEnabledByDefault) {
  601. $menu.find('.linkPassText').focus();
  602. }
  603. },
  604. /**
  605. * @returns {Function} from Handlebars
  606. * @private
  607. */
  608. template: function () {
  609. return OC.Share.Templates['sharedialoglinkshareview'];
  610. },
  611. /**
  612. * renders the popover template and returns the resulting HTML
  613. *
  614. * @param {Object} data
  615. * @returns {string}
  616. */
  617. popoverMenuTemplate: function(data) {
  618. return OC.Share.Templates['sharedialoglinkshareview_popover_menu'](data);
  619. },
  620. /**
  621. * renders the pending popover template and returns the resulting HTML
  622. *
  623. * @param {Object} data
  624. * @returns {string}
  625. */
  626. pendingPopoverMenuTemplate: function(data) {
  627. return OC.Share.Templates['sharedialoglinkshareview_popover_menu_pending'](data);
  628. },
  629. onPopUpClick: function(event) {
  630. event.preventDefault();
  631. event.stopPropagation();
  632. var url = $(event.currentTarget).data('url');
  633. var newWindow = $(event.currentTarget).data('window');
  634. $(event.currentTarget).tooltip('hide');
  635. if (url) {
  636. if (newWindow === true) {
  637. var width = 600;
  638. var height = 400;
  639. var left = (screen.width / 2) - (width / 2);
  640. var top = (screen.height / 2) - (height / 2);
  641. window.open(url, 'name', 'width=' + width + ', height=' + height + ', top=' + top + ', left=' + left);
  642. } else {
  643. window.location.href = url;
  644. }
  645. }
  646. },
  647. onExpireDateChange: function(event) {
  648. var $element = $(event.target);
  649. var li = $element.closest('li[data-share-id]');
  650. var shareId = li.data('share-id');
  651. var expirationDatePicker = '#expirationDateContainer-' + shareId;
  652. var datePicker = $(expirationDatePicker);
  653. var state = $element.prop('checked');
  654. datePicker.toggleClass('hidden', !state);
  655. if (!state) {
  656. // disabled, let's hide the input and
  657. // set the expireDate to nothing
  658. $element.closest('li').next('li').addClass('hidden');
  659. this.setExpirationDate('', shareId);
  660. } else {
  661. // enabled, show the input and the datepicker
  662. $element.closest('li').next('li').removeClass('hidden');
  663. this.showDatePicker(event);
  664. }
  665. },
  666. showDatePicker: function(event) {
  667. var $element = $(event.target);
  668. var li = $element.closest('li[data-share-id]');
  669. var shareId = li.data('share-id');
  670. var maxDate = $element.data('max-date');
  671. var expirationDatePicker = '#expirationDatePicker-' + shareId;
  672. var self = this;
  673. $(expirationDatePicker).datepicker({
  674. dateFormat : 'dd-mm-yy',
  675. onSelect: function (expireDate) {
  676. self.setExpirationDate(expireDate, shareId);
  677. },
  678. maxDate: maxDate
  679. });
  680. $(expirationDatePicker).datepicker('show');
  681. $(expirationDatePicker).focus();
  682. },
  683. setExpirationDate: function(expireDate, shareId) {
  684. this.model.saveLinkShare({expireDate: expireDate, cid: shareId});
  685. },
  686. onChangeExpirationDate: function(event) {
  687. var $element = $(event.target);
  688. var expireDate = $element.val();
  689. var li = $element.closest('li[data-share-id]');
  690. var shareId = li.data('share-id');
  691. var expirationDatePicker = '#expirationDatePicker-' + shareId;
  692. this.setExpirationDate(expireDate, shareId);
  693. $(expirationDatePicker).datepicker('hide');
  694. },
  695. /**
  696. * get an array of sharees' share properties
  697. *
  698. * @returns {Array}
  699. */
  700. getShareeList: function() {
  701. var shares = this.model.get('linkShares');
  702. if(!this.model.hasLinkShares()) {
  703. return [];
  704. }
  705. var list = [];
  706. for(var index = 0; index < shares.length; index++) {
  707. var share = this.getShareeObject(index);
  708. // first empty {} is necessary, otherwise we get in trouble
  709. // with references
  710. list.push(_.extend({}, share));
  711. }
  712. return list;
  713. },
  714. /**
  715. *
  716. * @param {OC.Share.Types.ShareInfo} shareInfo
  717. * @returns {object}
  718. */
  719. getShareeObject: function(shareIndex) {
  720. var share = this.model.get('linkShares')[shareIndex];
  721. return _.extend({}, share, {
  722. cid: share.id,
  723. shareAllowed: true,
  724. linkShareLabel: share.label ? share.label : t('core', 'Share link'),
  725. popoverMenu: {},
  726. shareLinkURL: share.url,
  727. newShareTitle: t('core', 'New share link'),
  728. copyLabel: t('core', 'Copy link'),
  729. showPending: this.showPending === share.id,
  730. linkShareCreationDate: t('core', 'Created on {time}', { time: moment(share.stime * 1000).format('LLLL') })
  731. })
  732. },
  733. getPopoverObject: function(share) {
  734. var publicUploadRWChecked = '';
  735. var publicUploadRChecked = '';
  736. var publicUploadWChecked = '';
  737. switch (this.model.linkSharePermissions(share.id)) {
  738. case OC.PERMISSION_READ:
  739. publicUploadRChecked = 'checked';
  740. break;
  741. case OC.PERMISSION_CREATE:
  742. publicUploadWChecked = 'checked';
  743. break;
  744. case OC.PERMISSION_UPDATE | OC.PERMISSION_CREATE | OC.PERMISSION_READ | OC.PERMISSION_DELETE:
  745. publicUploadRWChecked = 'checked';
  746. break;
  747. }
  748. var isPasswordSet = !!share.password;
  749. var isPasswordEnabledByDefault = this.configModel.get('enableLinkPasswordByDefault') === true;
  750. var isPasswordEnforced = this.configModel.get('enforcePasswordForPublicLink');
  751. var isExpirationEnforced = this.configModel.get('isDefaultExpireDateEnforced');
  752. var defaultExpireDays = this.configModel.get('defaultExpireDate');
  753. var hasExpireDate = !!share.expiration || isExpirationEnforced;
  754. var expireDate;
  755. if (hasExpireDate) {
  756. expireDate = moment(share.expiration, 'YYYY-MM-DD').format('DD-MM-YYYY');
  757. }
  758. var isTalkEnabled = OC.appswebroots['spreed'] !== undefined;
  759. var sendPasswordByTalk = share.sendPasswordByTalk;
  760. var hideDownload = share.hideDownload;
  761. var maxDate = null;
  762. if(hasExpireDate) {
  763. if(isExpirationEnforced) {
  764. // TODO: hack: backend returns string instead of integer
  765. var shareTime = share.stime;
  766. if (_.isNumber(shareTime)) {
  767. shareTime = new Date(shareTime * 1000);
  768. }
  769. if (!shareTime) {
  770. shareTime = new Date(); // now
  771. }
  772. shareTime = OC.Util.stripTime(shareTime).getTime();
  773. maxDate = new Date(shareTime + defaultExpireDays * 24 * 3600 * 1000);
  774. }
  775. }
  776. return {
  777. cid: share.id,
  778. shareLinkURL: share.url,
  779. passwordPlaceholder: isPasswordSet ? PASSWORD_PLACEHOLDER : PASSWORD_PLACEHOLDER_MESSAGE,
  780. isPasswordSet: isPasswordSet || isPasswordEnabledByDefault || isPasswordEnforced,
  781. showPasswordByTalkCheckBox: isTalkEnabled && isPasswordSet,
  782. passwordByTalkLabel: t('core', 'Password protect by Talk'),
  783. isPasswordByTalkSet: sendPasswordByTalk,
  784. publicUploadRWChecked: publicUploadRWChecked,
  785. publicUploadRChecked: publicUploadRChecked,
  786. publicUploadWChecked: publicUploadWChecked,
  787. hasExpireDate: hasExpireDate,
  788. expireDate: expireDate,
  789. shareNote: share.note,
  790. hasNote: share.note !== '',
  791. maxDate: maxDate,
  792. hideDownload: hideDownload,
  793. isExpirationEnforced: isExpirationEnforced,
  794. }
  795. },
  796. onUnshare: function(event) {
  797. event.preventDefault();
  798. event.stopPropagation();
  799. var self = this;
  800. var $element = $(event.target);
  801. if (!$element.is('a')) {
  802. $element = $element.closest('a');
  803. }
  804. var $loading = $element.find('.icon-loading-small').eq(0);
  805. if(!$loading.hasClass('hidden')) {
  806. // in process
  807. return false;
  808. }
  809. $loading.removeClass('hidden');
  810. var $li = $element.closest('li[data-share-id]');
  811. var shareId = $li.data('share-id');
  812. self.model.removeShare(shareId, {
  813. success: function() {
  814. $li.remove();
  815. self.render()
  816. },
  817. error: function() {
  818. $loading.addClass('hidden');
  819. OC.Notification.showTemporary(t('core', 'Could not unshare'));
  820. }
  821. });
  822. return false;
  823. },
  824. });
  825. OC.Share.ShareDialogLinkShareView = ShareDialogLinkShareView;
  826. })();