You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

sharedialogexpirationview.js 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. /* global moment */
  11. (function() {
  12. if (!OC.Share) {
  13. OC.Share = {};
  14. }
  15. var TEMPLATE =
  16. // currently expiration is only effective for link share.
  17. // this is about to change in future. Therefore this is not included
  18. // in the LinkShareView to ease reusing it in future. Then,
  19. // modifications (getting rid of IDs) are still necessary.
  20. '{{#if isLinkShare}}' +
  21. '<input type="checkbox" name="expirationCheckbox" class="expirationCheckbox checkbox" id="expirationCheckbox-{{cid}}" value="1" ' +
  22. '{{#if isExpirationSet}}checked="checked"{{/if}} {{#if disableCheckbox}}disabled="disabled"{{/if}} />' +
  23. '<label for="expirationCheckbox-{{cid}}">{{setExpirationLabel}}</label>' +
  24. '<div class="expirationDateContainer {{#unless isExpirationSet}}hidden{{/unless}}">' +
  25. ' <label for="expirationDate" class="hidden-visually" value="{{expirationDate}}">{{expirationLabel}}</label>' +
  26. ' <input id="expirationDate" class="datepicker" type="text" placeholder="{{expirationDatePlaceholder}}" value="{{expirationValue}}" />' +
  27. '</div>' +
  28. ' {{#if isExpirationEnforced}}' +
  29. // originally the expire message was shown when a default date was set, however it never had text
  30. '<em id="defaultExpireMessage">{{defaultExpireMessage}}</em>' +
  31. ' {{/if}}' +
  32. '{{/if}}'
  33. ;
  34. /**
  35. * @class OCA.Share.ShareDialogExpirationView
  36. * @member {OC.Share.ShareItemModel} model
  37. * @member {jQuery} $el
  38. * @memberof OCA.Sharing
  39. * @classdesc
  40. *
  41. * Represents the expiration part in the GUI of the share dialogue
  42. *
  43. */
  44. var ShareDialogExpirationView = OC.Backbone.View.extend({
  45. /** @type {string} **/
  46. id: 'shareDialogLinkShare',
  47. /** @type {OC.Share.ShareConfigModel} **/
  48. configModel: undefined,
  49. /** @type {Function} **/
  50. _template: undefined,
  51. /** @type {boolean} **/
  52. showLink: true,
  53. className: 'hidden',
  54. events: {
  55. 'change .expirationCheckbox': '_onToggleExpiration',
  56. 'change .datepicker': '_onChangeExpirationDate'
  57. },
  58. initialize: function(options) {
  59. if(!_.isUndefined(options.configModel)) {
  60. this.configModel = options.configModel;
  61. } else {
  62. throw 'missing OC.Share.ShareConfigModel';
  63. }
  64. var view = this;
  65. this.configModel.on('change:isDefaultExpireDateEnforced', function() {
  66. view.render();
  67. });
  68. this.model.on('change:itemType', function() {
  69. view.render();
  70. });
  71. this.model.on('change:linkShare', function() {
  72. view.render();
  73. });
  74. },
  75. _onToggleExpiration: function(event) {
  76. var $checkbox = $(event.target);
  77. var state = $checkbox.prop('checked');
  78. // TODO: slide animation
  79. this.$el.find('.expirationDateContainer').toggleClass('hidden', !state);
  80. if (!state) {
  81. // discard expiration date
  82. this.model.saveLinkShare({
  83. expireDate: ''
  84. });
  85. } else {
  86. this.$el.find('#expirationDate').focus();
  87. }
  88. },
  89. _onChangeExpirationDate: function(event) {
  90. var $target = $(event.target);
  91. $target.tooltip('hide');
  92. $target.removeClass('error');
  93. this.model.saveLinkShare({
  94. expiration: moment($target.val(), 'DD-MM-YYYY').format('YYYY-MM-DD')
  95. }, {
  96. error: function(model, message) {
  97. if (!message) {
  98. $target.attr('title', t('core', 'Error setting expiration date'));
  99. } else {
  100. $target.attr('title', message);
  101. }
  102. $target.tooltip({gravity: 'n'});
  103. $target.tooltip('show');
  104. $target.addClass('error');
  105. }
  106. });
  107. },
  108. render: function() {
  109. var defaultExpireMessage = '';
  110. var defaultExpireDays = this.configModel.get('defaultExpireDate');
  111. var isExpirationEnforced = this.configModel.get('isDefaultExpireDateEnforced');
  112. if( (this.model.isFolder() || this.model.isFile())
  113. && isExpirationEnforced) {
  114. defaultExpireMessage = t(
  115. 'core',
  116. 'The public link will expire no later than {days} days after it is created',
  117. {'days': defaultExpireDays }
  118. );
  119. }
  120. var isExpirationSet = !!this.model.get('linkShare').expiration || isExpirationEnforced;
  121. var expiration;
  122. if (isExpirationSet) {
  123. expiration = moment(this.model.get('linkShare').expiration, 'YYYY-MM-DD').format('DD-MM-YYYY');
  124. }
  125. this.$el.html(this.template({
  126. cid: this.cid,
  127. setExpirationLabel: t('core', 'Set expiration date'),
  128. expirationLabel: t('core', 'Expiration'),
  129. expirationDatePlaceholder: t('core', 'Expiration date'),
  130. defaultExpireMessage: defaultExpireMessage,
  131. isLinkShare: this.model.get('linkShare').isLinkShare,
  132. isExpirationSet: isExpirationSet,
  133. isExpirationEnforced: isExpirationEnforced,
  134. disableCheckbox: isExpirationEnforced && isExpirationSet,
  135. expirationValue: expiration
  136. }));
  137. // what if there is another date picker on that page?
  138. var minDate = new Date();
  139. var maxDate = null;
  140. // min date should always be the next day
  141. minDate.setDate(minDate.getDate()+1);
  142. if(isExpirationSet) {
  143. if(isExpirationEnforced) {
  144. // TODO: hack: backend returns string instead of integer
  145. var shareTime = this.model.get('linkShare').stime;
  146. if (_.isNumber(shareTime)) {
  147. shareTime = new Date(shareTime * 1000);
  148. }
  149. if (!shareTime) {
  150. shareTime = new Date(); // now
  151. }
  152. shareTime = OC.Util.stripTime(shareTime).getTime();
  153. maxDate = new Date(shareTime + defaultExpireDays * 24 * 3600 * 1000);
  154. }
  155. }
  156. $.datepicker.setDefaults({
  157. minDate: minDate,
  158. maxDate: maxDate
  159. });
  160. this.$el.find('.datepicker').datepicker({dateFormat : 'dd-mm-yy'});
  161. this.delegateEvents();
  162. return this;
  163. },
  164. /**
  165. * @returns {Function} from Handlebars
  166. * @private
  167. */
  168. template: function (data) {
  169. if (!this._template) {
  170. this._template = Handlebars.compile(TEMPLATE);
  171. }
  172. return this._template(data);
  173. }
  174. });
  175. OC.Share.ShareDialogExpirationView = ShareDialogExpirationView;
  176. })();