sharedialoglinkshareview.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947
  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. 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 isExpirationEnforced = this.configModel.get('isDefaultExpireDateEnforced');
  502. // what if there is another date picker on that page?
  503. var minDate = new Date();
  504. // min date should always be the next day
  505. minDate.setDate(minDate.getDate()+1);
  506. $.datepicker.setDefaults({
  507. minDate: minDate
  508. });
  509. this.$el.find('.datepicker').datepicker({dateFormat : 'dd-mm-yy'});
  510. var minPasswordLength = 4
  511. // password policy?
  512. if(oc_capabilities.password_policy && oc_capabilities.password_policy.minLength) {
  513. minPasswordLength = oc_capabilities.password_policy.minLength;
  514. }
  515. var popoverBase = {
  516. urlLabel: t('core', 'Link'),
  517. hideDownloadLabel: t('core', 'Hide download'),
  518. enablePasswordLabel: isPasswordEnforced ? t('core', 'Password protection enforced') : t('core', 'Password protect'),
  519. passwordLabel: t('core', 'Password'),
  520. passwordPlaceholderInitial: passwordPlaceholderInitial,
  521. publicUpload: publicUpload,
  522. publicEditing: publicEditable,
  523. publicEditingChecked: publicEditingChecked,
  524. publicEditingLabel: t('core', 'Allow editing'),
  525. mailPrivatePlaceholder: t('core', 'Email link to person'),
  526. mailButtonText: t('core', 'Send'),
  527. publicUploadRWLabel: t('core', 'Allow upload and editing'),
  528. publicUploadRLabel: t('core', 'Read only'),
  529. publicUploadWLabel: t('core', 'File drop (upload only)'),
  530. publicUploadRWValue: OC.PERMISSION_UPDATE | OC.PERMISSION_CREATE | OC.PERMISSION_READ | OC.PERMISSION_DELETE,
  531. publicUploadRValue: OC.PERMISSION_READ,
  532. publicUploadWValue: OC.PERMISSION_CREATE,
  533. expireDateLabel: isExpirationEnforced ? t('core', 'Expiration date enforced') : t('core', 'Set expiration date'),
  534. expirationLabel: t('core', 'Expiration'),
  535. expirationDatePlaceholder: t('core', 'Expiration date'),
  536. isExpirationEnforced: isExpirationEnforced,
  537. isPasswordEnforced: isPasswordEnforced,
  538. defaultExpireDate: moment().add(1, 'day').format('DD-MM-YYYY'), // Can't expire today
  539. addNoteLabel: t('core', 'Note to recipient'),
  540. unshareLabel: t('core', 'Unshare'),
  541. unshareLinkLabel: t('core', 'Delete share link'),
  542. newShareLabel: t('core', 'Add another link'),
  543. };
  544. var pendingPopover = {
  545. isPasswordEnforced: isPasswordEnforced,
  546. enforcedPasswordLabel: t('core', 'Password protection for links is mandatory'),
  547. passwordPlaceholder: passwordPlaceholderInitial,
  548. minPasswordLength: minPasswordLength,
  549. };
  550. var pendingPopoverMenu = this.pendingPopoverMenuTemplate(_.extend({}, pendingPopover))
  551. var linkShares = this.getShareeList();
  552. if(_.isArray(linkShares)) {
  553. for (var i = 0; i < linkShares.length; i++) {
  554. var social = [];
  555. OC.Share.Social.Collection.each(function (model) {
  556. var url = model.get('url');
  557. url = url.replace('{{reference}}', linkShares[i].shareLinkURL);
  558. social.push({
  559. url: url,
  560. label: t('core', 'Share to {name}', {name: model.get('name')}),
  561. name: model.get('name'),
  562. iconClass: model.get('iconClass'),
  563. newWindow: model.get('newWindow')
  564. });
  565. });
  566. var popover = this.getPopoverObject(linkShares[i])
  567. linkShares[i].popoverMenu = this.popoverMenuTemplate(_.extend({}, popoverBase, popover, {social: social}));
  568. linkShares[i].pendingPopoverMenu = pendingPopoverMenu
  569. }
  570. }
  571. this.$el.html(linkShareTemplate({
  572. linkShares: linkShares,
  573. shareAllowed: true,
  574. nolinkShares: linkShares.length === 0,
  575. newShareLabel: t('core', 'Share link'),
  576. newShareTitle: t('core', 'New share link'),
  577. pendingPopoverMenu: pendingPopoverMenu,
  578. showPending: this.showPending === this.newShareId,
  579. newShareId: this.newShareId,
  580. }));
  581. this.delegateEvents();
  582. // new note autosize
  583. autosize(this.$el.find('.share-note-form .share-note'));
  584. return this;
  585. },
  586. onToggleMenu: function(event) {
  587. event.preventDefault();
  588. event.stopPropagation();
  589. var $element = $(event.target);
  590. var $li = $element.closest('li[data-share-id]');
  591. var $menu = $li.find('.sharingOptionsGroup .popovermenu');
  592. var shareId = $li.data('share-id');
  593. OC.showMenu(null, $menu);
  594. // focus the password if not set and enforced
  595. var isPasswordEnabledByDefault = this.configModel.get('enableLinkPasswordByDefault') === true;
  596. var haspassword = $menu.find('.linkPassText').val() !== '';
  597. if (!haspassword && isPasswordEnabledByDefault) {
  598. $menu.find('.linkPassText').focus();
  599. }
  600. },
  601. /**
  602. * @returns {Function} from Handlebars
  603. * @private
  604. */
  605. template: function () {
  606. return OC.Share.Templates['sharedialoglinkshareview'];
  607. },
  608. /**
  609. * renders the popover template and returns the resulting HTML
  610. *
  611. * @param {Object} data
  612. * @returns {string}
  613. */
  614. popoverMenuTemplate: function(data) {
  615. return OC.Share.Templates['sharedialoglinkshareview_popover_menu'](data);
  616. },
  617. /**
  618. * renders the pending popover template and returns the resulting HTML
  619. *
  620. * @param {Object} data
  621. * @returns {string}
  622. */
  623. pendingPopoverMenuTemplate: function(data) {
  624. return OC.Share.Templates['sharedialoglinkshareview_popover_menu_pending'](data);
  625. },
  626. onPopUpClick: function(event) {
  627. event.preventDefault();
  628. event.stopPropagation();
  629. var url = $(event.currentTarget).data('url');
  630. var newWindow = $(event.currentTarget).data('window');
  631. $(event.currentTarget).tooltip('hide');
  632. if (url) {
  633. if (newWindow === true) {
  634. var width = 600;
  635. var height = 400;
  636. var left = (screen.width / 2) - (width / 2);
  637. var top = (screen.height / 2) - (height / 2);
  638. window.open(url, 'name', 'width=' + width + ', height=' + height + ', top=' + top + ', left=' + left);
  639. } else {
  640. window.location.href = url;
  641. }
  642. }
  643. },
  644. onExpireDateChange: function(event) {
  645. var $element = $(event.target);
  646. var li = $element.closest('li[data-share-id]');
  647. var shareId = li.data('share-id');
  648. var expirationDatePicker = '#expirationDateContainer-' + shareId;
  649. var datePicker = $(expirationDatePicker);
  650. var state = $element.prop('checked');
  651. datePicker.toggleClass('hidden', !state);
  652. if (!state) {
  653. // disabled, let's hide the input and
  654. // set the expireDate to nothing
  655. $element.closest('li').next('li').addClass('hidden');
  656. this.setExpirationDate('');
  657. } else {
  658. // enabled, show the input and the datepicker
  659. $element.closest('li').next('li').removeClass('hidden');
  660. this.showDatePicker(event);
  661. }
  662. },
  663. showDatePicker: function(event) {
  664. var $element = $(event.target);
  665. var li = $element.closest('li[data-share-id]');
  666. var shareId = li.data('share-id');
  667. var maxDate = $element.data('max-date');
  668. var expirationDatePicker = '#expirationDatePicker-' + shareId;
  669. var self = this;
  670. $(expirationDatePicker).datepicker({
  671. dateFormat : 'dd-mm-yy',
  672. onSelect: function (expireDate) {
  673. self.setExpirationDate(expireDate, shareId);
  674. },
  675. maxDate: maxDate
  676. });
  677. $(expirationDatePicker).datepicker('show');
  678. $(expirationDatePicker).focus();
  679. },
  680. setExpirationDate: function(expireDate, shareId) {
  681. this.model.saveLinkShare({expireDate: expireDate, cid: shareId});
  682. },
  683. /**
  684. * get an array of sharees' share properties
  685. *
  686. * @returns {Array}
  687. */
  688. getShareeList: function() {
  689. var shares = this.model.get('linkShares');
  690. if(!this.model.hasLinkShares()) {
  691. return [];
  692. }
  693. var list = [];
  694. for(var index = 0; index < shares.length; index++) {
  695. var share = this.getShareeObject(index);
  696. // first empty {} is necessary, otherwise we get in trouble
  697. // with references
  698. list.push(_.extend({}, share));
  699. }
  700. return list;
  701. },
  702. /**
  703. *
  704. * @param {OC.Share.Types.ShareInfo} shareInfo
  705. * @returns {object}
  706. */
  707. getShareeObject: function(shareIndex) {
  708. var share = this.model.get('linkShares')[shareIndex];
  709. return _.extend({}, share, {
  710. cid: share.id,
  711. shareAllowed: true,
  712. linkShareLabel: share.label ? share.label : t('core', 'Share link'),
  713. popoverMenu: {},
  714. shareLinkURL: share.url,
  715. newShareTitle: t('core', 'New share link'),
  716. copyLabel: t('core', 'Copy link'),
  717. showPending: this.showPending === share.id,
  718. linkShareCreationDate: t('core', 'Created on {time}', { time: moment(share.stime * 1000).format('LLLL') })
  719. })
  720. },
  721. getPopoverObject: function(share) {
  722. var publicUploadRWChecked = '';
  723. var publicUploadRChecked = '';
  724. var publicUploadWChecked = '';
  725. switch (this.model.linkSharePermissions(share.id)) {
  726. case OC.PERMISSION_READ:
  727. publicUploadRChecked = 'checked';
  728. break;
  729. case OC.PERMISSION_CREATE:
  730. publicUploadWChecked = 'checked';
  731. break;
  732. case OC.PERMISSION_UPDATE | OC.PERMISSION_CREATE | OC.PERMISSION_READ | OC.PERMISSION_DELETE:
  733. publicUploadRWChecked = 'checked';
  734. break;
  735. }
  736. var isPasswordSet = !!share.password;
  737. var isPasswordEnabledByDefault = this.configModel.get('enableLinkPasswordByDefault') === true;
  738. var isPasswordEnforced = this.configModel.get('enforcePasswordForPublicLink');
  739. var isExpirationEnforced = this.configModel.get('isDefaultExpireDateEnforced');
  740. var defaultExpireDays = this.configModel.get('defaultExpireDate');
  741. var hasExpireDate = !!share.expiration || isExpirationEnforced;
  742. var expireDate;
  743. if (hasExpireDate) {
  744. expireDate = moment(share.expiration, 'YYYY-MM-DD').format('DD-MM-YYYY');
  745. }
  746. var isTalkEnabled = oc_appswebroots['spreed'] !== undefined;
  747. var sendPasswordByTalk = share.sendPasswordByTalk;
  748. var hideDownload = share.hideDownload;
  749. var maxDate = null;
  750. if(hasExpireDate) {
  751. if(isExpirationEnforced) {
  752. // TODO: hack: backend returns string instead of integer
  753. var shareTime = share.stime;
  754. if (_.isNumber(shareTime)) {
  755. shareTime = new Date(shareTime * 1000);
  756. }
  757. if (!shareTime) {
  758. shareTime = new Date(); // now
  759. }
  760. shareTime = OC.Util.stripTime(shareTime).getTime();
  761. maxDate = new Date(shareTime + defaultExpireDays * 24 * 3600 * 1000);
  762. }
  763. }
  764. return {
  765. cid: share.id,
  766. shareLinkURL: share.url,
  767. passwordPlaceholder: isPasswordSet ? PASSWORD_PLACEHOLDER : PASSWORD_PLACEHOLDER_MESSAGE,
  768. isPasswordSet: isPasswordSet || isPasswordEnabledByDefault || isPasswordEnforced,
  769. showPasswordByTalkCheckBox: isTalkEnabled && isPasswordSet,
  770. passwordByTalkLabel: t('core', 'Password protect by Talk'),
  771. isPasswordByTalkSet: sendPasswordByTalk,
  772. publicUploadRWChecked: publicUploadRWChecked,
  773. publicUploadRChecked: publicUploadRChecked,
  774. publicUploadWChecked: publicUploadWChecked,
  775. hasExpireDate: hasExpireDate,
  776. expireDate: expireDate,
  777. shareNote: share.note,
  778. hasNote: share.note !== '',
  779. maxDate: maxDate,
  780. hideDownload: hideDownload,
  781. isExpirationEnforced: isExpirationEnforced,
  782. }
  783. },
  784. onUnshare: function(event) {
  785. event.preventDefault();
  786. event.stopPropagation();
  787. var self = this;
  788. var $element = $(event.target);
  789. if (!$element.is('a')) {
  790. $element = $element.closest('a');
  791. }
  792. var $loading = $element.find('.icon-loading-small').eq(0);
  793. if(!$loading.hasClass('hidden')) {
  794. // in process
  795. return false;
  796. }
  797. $loading.removeClass('hidden');
  798. var $li = $element.closest('li[data-share-id]');
  799. var shareId = $li.data('share-id');
  800. self.model.removeShare(shareId, {
  801. success: function() {
  802. $li.remove();
  803. self.render()
  804. },
  805. error: function() {
  806. $loading.addClass('hidden');
  807. OC.Notification.showTemporary(t('core', 'Could not unshare'));
  808. }
  809. });
  810. return false;
  811. },
  812. });
  813. OC.Share.ShareDialogLinkShareView = ShareDialogLinkShareView;
  814. })();