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.

sharedialoglinkshareview.js 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. (function() {
  11. if (!OC.Share) {
  12. OC.Share = {};
  13. }
  14. var PASSWORD_PLACEHOLDER = '**********';
  15. var PASSWORD_PLACEHOLDER_MESSAGE = t('core', 'Choose a password for the public link');
  16. var TEMPLATE =
  17. '{{#if shareAllowed}}' +
  18. '<span class="icon-loading-small hidden"></span>' +
  19. '<input type="checkbox" name="linkCheckbox" id="linkCheckbox-{{cid}}" class="checkbox linkCheckbox" value="1" {{#if isLinkShare}}checked="checked"{{/if}} />' +
  20. '<label for="linkCheckbox-{{cid}}">{{linkShareLabel}}</label>' +
  21. '<br />' +
  22. '<label for="linkText-{{cid}}" class="hidden-visually">{{urlLabel}}</label>' +
  23. '<input id="linkText-{{cid}}" class="linkText {{#unless isLinkShare}}hidden{{/unless}}" type="text" readonly="readonly" value="{{shareLinkURL}}" />' +
  24. ' {{#if publicUpload}}' +
  25. '<div id="allowPublicUploadWrapper">' +
  26. ' <span class="icon-loading-small hidden"></span>' +
  27. ' <input type="checkbox" value="1" name="allowPublicUpload" id="sharingDialogAllowPublicUpload-{{cid}}" class="checkbox publicUploadCheckbox" {{{publicUploadChecked}}} />' +
  28. '<label for="sharingDialogAllowPublicUpload-{{cid}}">{{publicUploadLabel}}</label>' +
  29. '</div>' +
  30. ' {{/if}}' +
  31. ' {{#if showPasswordCheckBox}}' +
  32. '<input type="checkbox" name="showPassword" id="showPassword-{{cid}}" class="checkbox showPasswordCheckbox" {{#if isPasswordSet}}checked="checked"{{/if}} value="1" />' +
  33. '<label for="showPassword-{{cid}}">{{enablePasswordLabel}}</label>' +
  34. ' {{/if}}' +
  35. '<div id="linkPass" class="linkPass {{#unless isPasswordSet}}hidden{{/unless}}">' +
  36. ' <label for="linkPassText-{{cid}}" class="hidden-visually">{{passwordLabel}}</label>' +
  37. ' <input id="linkPassText-{{cid}}" class="linkPassText" type="password" placeholder="{{passwordPlaceholder}}" />' +
  38. ' <span class="icon-loading-small hidden"></span>' +
  39. '</div>' +
  40. '{{else}}' +
  41. // FIXME: this doesn't belong in this view
  42. '{{#if noSharingPlaceholder}}<input id="shareWith-{{cid}}" class="shareWithField" type="text" placeholder="{{noSharingPlaceholder}}" disabled="disabled"/>{{/if}}' +
  43. '{{/if}}'
  44. ;
  45. /**
  46. * @class OCA.Share.ShareDialogLinkShareView
  47. * @member {OC.Share.ShareItemModel} model
  48. * @member {jQuery} $el
  49. * @memberof OCA.Sharing
  50. * @classdesc
  51. *
  52. * Represents the GUI of the share dialogue
  53. *
  54. */
  55. var ShareDialogLinkShareView = OC.Backbone.View.extend({
  56. /** @type {string} **/
  57. id: 'shareDialogLinkShare',
  58. /** @type {OC.Share.ShareConfigModel} **/
  59. configModel: undefined,
  60. /** @type {Function} **/
  61. _template: undefined,
  62. /** @type {boolean} **/
  63. showLink: true,
  64. events: {
  65. 'focusout input.linkPassText': 'onPasswordEntered',
  66. 'keyup input.linkPassText': 'onPasswordKeyUp',
  67. 'click .linkCheckbox': 'onLinkCheckBoxChange',
  68. 'click .linkText': 'onLinkTextClick',
  69. 'change .publicUploadCheckbox': 'onAllowPublicUploadChange',
  70. 'click .showPasswordCheckbox': 'onShowPasswordClick'
  71. },
  72. initialize: function(options) {
  73. var view = this;
  74. this.model.on('change:permissions', function() {
  75. view.render();
  76. });
  77. this.model.on('change:itemType', function() {
  78. view.render();
  79. });
  80. this.model.on('change:allowPublicUploadStatus', function() {
  81. view.render();
  82. });
  83. this.model.on('change:linkShare', function() {
  84. view.render();
  85. });
  86. if(!_.isUndefined(options.configModel)) {
  87. this.configModel = options.configModel;
  88. } else {
  89. throw 'missing OC.Share.ShareConfigModel';
  90. }
  91. _.bindAll(
  92. this,
  93. 'onLinkCheckBoxChange',
  94. 'onPasswordEntered',
  95. 'onPasswordKeyUp',
  96. 'onLinkTextClick',
  97. 'onShowPasswordClick',
  98. 'onAllowPublicUploadChange'
  99. );
  100. },
  101. onLinkCheckBoxChange: function() {
  102. var $checkBox = this.$el.find('.linkCheckbox');
  103. var $loading = $checkBox.siblings('.icon-loading-small');
  104. if(!$loading.hasClass('hidden')) {
  105. return false;
  106. }
  107. if($checkBox.is(':checked')) {
  108. if(this.configModel.get('enforcePasswordForPublicLink') === false) {
  109. $loading.removeClass('hidden');
  110. // this will create it
  111. this.model.saveLinkShare();
  112. } else {
  113. this.$el.find('.linkPass').slideToggle(OC.menuSpeed);
  114. this.$el.find('.linkPassText').focus();
  115. }
  116. } else {
  117. if (this.model.get('linkShare').isLinkShare) {
  118. $loading.removeClass('hidden');
  119. this.model.removeLinkShare();
  120. } else {
  121. this.$el.find('.linkPass').slideToggle(OC.menuSpeed);
  122. }
  123. }
  124. },
  125. onLinkTextClick: function() {
  126. var $el = this.$el.find('.linkText');
  127. $el.focus();
  128. $el.select();
  129. },
  130. onShowPasswordClick: function() {
  131. this.$el.find('.linkPass').slideToggle(OC.menuSpeed);
  132. if(!this.$el.find('.showPasswordCheckbox').is(':checked')) {
  133. this.model.saveLinkShare({
  134. password: ''
  135. });
  136. } else {
  137. this.$el.find('.linkPassText').focus();
  138. }
  139. },
  140. onPasswordKeyUp: function(event) {
  141. if(event.keyCode == 13) {
  142. this.onPasswordEntered();
  143. }
  144. },
  145. onPasswordEntered: function() {
  146. var $loading = this.$el.find('.linkPass .icon-loading-small');
  147. if (!$loading.hasClass('hidden')) {
  148. // still in process
  149. return;
  150. }
  151. var $input = this.$el.find('.linkPassText');
  152. $input.removeClass('error');
  153. var password = $input.val();
  154. // in IE9 the password might be the placeholder due to bugs in the placeholders polyfill
  155. if(password === '' || password === PASSWORD_PLACEHOLDER || password === PASSWORD_PLACEHOLDER_MESSAGE) {
  156. return;
  157. }
  158. $loading
  159. .removeClass('hidden')
  160. .addClass('inlineblock');
  161. this.model.saveLinkShare({
  162. password: password
  163. }, {
  164. error: function(model, msg) {
  165. // destroy old tooltips
  166. $input.tooltip('destroy');
  167. $loading.removeClass('inlineblock').addClass('hidden');
  168. $input.addClass('error');
  169. $input.attr('title', msg);
  170. $input.tooltip({placement: 'bottom', trigger: 'manual'});
  171. $input.tooltip('show');
  172. }
  173. });
  174. },
  175. onAllowPublicUploadChange: function() {
  176. var $checkbox = this.$('.publicUploadCheckbox');
  177. $checkbox.siblings('.icon-loading-small').removeClass('hidden').addClass('inlineblock');
  178. var permissions = OC.PERMISSION_READ;
  179. if($checkbox.is(':checked')) {
  180. permissions = OC.PERMISSION_UPDATE | OC.PERMISSION_CREATE | OC.PERMISSION_READ;
  181. }
  182. this.model.saveLinkShare({
  183. permissions: permissions
  184. });
  185. },
  186. render: function() {
  187. var linkShareTemplate = this.template();
  188. var resharingAllowed = this.model.sharePermissionPossible();
  189. if(!resharingAllowed
  190. || !this.showLink
  191. || !this.configModel.isShareWithLinkAllowed())
  192. {
  193. var templateData = {shareAllowed: false};
  194. if (!resharingAllowed) {
  195. // add message
  196. templateData.noSharingPlaceholder = t('core', 'Resharing is not allowed');
  197. }
  198. this.$el.html(linkShareTemplate(templateData));
  199. return this;
  200. }
  201. var publicUpload =
  202. this.model.isFolder()
  203. && this.model.createPermissionPossible()
  204. && this.configModel.isPublicUploadEnabled();
  205. var publicUploadChecked = '';
  206. if(this.model.isPublicUploadAllowed()) {
  207. publicUploadChecked = 'checked="checked"';
  208. }
  209. var isLinkShare = this.model.get('linkShare').isLinkShare;
  210. var isPasswordSet = !!this.model.get('linkShare').password;
  211. var showPasswordCheckBox = isLinkShare
  212. && ( !this.configModel.get('enforcePasswordForPublicLink')
  213. || !this.model.get('linkShare').password);
  214. this.$el.html(linkShareTemplate({
  215. cid: this.cid,
  216. shareAllowed: true,
  217. isLinkShare: isLinkShare,
  218. shareLinkURL: this.model.get('linkShare').link,
  219. linkShareLabel: t('core', 'Share link'),
  220. urlLabel: t('core', 'Link'),
  221. enablePasswordLabel: t('core', 'Password protect'),
  222. passwordLabel: t('core', 'Password'),
  223. passwordPlaceholder: isPasswordSet ? PASSWORD_PLACEHOLDER : PASSWORD_PLACEHOLDER_MESSAGE,
  224. isPasswordSet: isPasswordSet,
  225. showPasswordCheckBox: showPasswordCheckBox,
  226. publicUpload: publicUpload && isLinkShare,
  227. publicUploadChecked: publicUploadChecked,
  228. publicUploadLabel: t('core', 'Allow editing'),
  229. mailPublicNotificationEnabled: isLinkShare && this.configModel.isMailPublicNotificationEnabled(),
  230. mailPrivatePlaceholder: t('core', 'Email link to person'),
  231. mailButtonText: t('core', 'Send')
  232. }));
  233. this.delegateEvents();
  234. return this;
  235. },
  236. /**
  237. * @returns {Function} from Handlebars
  238. * @private
  239. */
  240. template: function () {
  241. if (!this._template) {
  242. this._template = Handlebars.compile(TEMPLATE);
  243. }
  244. return this._template;
  245. }
  246. });
  247. OC.Share.ShareDialogLinkShareView = ShareDialogLinkShareView;
  248. })();