sharedialogexpirationview.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. }
  86. },
  87. _onChangeExpirationDate: function(event) {
  88. var $target = $(event.target);
  89. $target.tooltip('hide');
  90. $target.removeClass('error');
  91. this.model.saveLinkShare({
  92. expiration: moment($target.val(), 'DD-MM-YYYY').format('YYYY-MM-DD')
  93. }, {
  94. error: function(model, message) {
  95. if (!message) {
  96. $target.attr('title', t('core', 'Error setting expiration date'));
  97. } else {
  98. $target.attr('title', message);
  99. }
  100. $target.tooltip({gravity: 'n'});
  101. $target.tooltip('show');
  102. $target.addClass('error');
  103. }
  104. });
  105. },
  106. render: function() {
  107. var defaultExpireMessage = '';
  108. var defaultExpireDays = this.configModel.get('defaultExpireDate');
  109. var isExpirationEnforced = this.configModel.get('isDefaultExpireDateEnforced');
  110. if( (this.model.isFolder() || this.model.isFile())
  111. && isExpirationEnforced) {
  112. defaultExpireMessage = t(
  113. 'core',
  114. 'The public link will expire no later than {days} days after it is created',
  115. {'days': defaultExpireDays }
  116. );
  117. }
  118. var isExpirationSet = !!this.model.get('linkShare').expiration || isExpirationEnforced;
  119. var expiration;
  120. if (isExpirationSet) {
  121. expiration = moment(this.model.get('linkShare').expiration, 'YYYY-MM-DD').format('DD-MM-YYYY');
  122. }
  123. this.$el.html(this.template({
  124. cid: this.cid,
  125. setExpirationLabel: t('core', 'Set expiration date'),
  126. expirationLabel: t('core', 'Expiration'),
  127. expirationDatePlaceholder: t('core', 'Expiration date'),
  128. defaultExpireMessage: defaultExpireMessage,
  129. isLinkShare: this.model.get('linkShare').isLinkShare,
  130. isExpirationSet: isExpirationSet,
  131. isExpirationEnforced: isExpirationEnforced,
  132. disableCheckbox: isExpirationEnforced && isExpirationSet,
  133. expirationValue: expiration
  134. }));
  135. // what if there is another date picker on that page?
  136. var minDate = new Date();
  137. var maxDate = null;
  138. // min date should always be the next day
  139. minDate.setDate(minDate.getDate()+1);
  140. if(isExpirationSet) {
  141. if(isExpirationEnforced) {
  142. // TODO: hack: backend returns string instead of integer
  143. var shareTime = this.model.get('linkShare').stime;
  144. if (_.isNumber(shareTime)) {
  145. shareTime = new Date(shareTime * 1000);
  146. }
  147. if (!shareTime) {
  148. shareTime = new Date(); // now
  149. }
  150. shareTime = OC.Util.stripTime(shareTime).getTime();
  151. maxDate = new Date(shareTime + defaultExpireDays * 24 * 3600 * 1000);
  152. }
  153. }
  154. $.datepicker.setDefaults({
  155. minDate: minDate,
  156. maxDate: maxDate
  157. });
  158. this.$el.find('.datepicker').datepicker({dateFormat : 'dd-mm-yy'});
  159. this.delegateEvents();
  160. return this;
  161. },
  162. /**
  163. * @returns {Function} from Handlebars
  164. * @private
  165. */
  166. template: function (data) {
  167. if (!this._template) {
  168. this._template = Handlebars.compile(TEMPLATE);
  169. }
  170. return this._template(data);
  171. }
  172. });
  173. OC.Share.ShareDialogExpirationView = ShareDialogExpirationView;
  174. })();