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.

298 lines
9.1 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  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 TEMPLATE =
  15. '{{#if shareAllowed}}' +
  16. '<span class="icon-loading-small hidden"></span>' +
  17. '<input type="checkbox" name="linkCheckbox" id="linkCheckbox" value="1" {{#if isLinkShare}}checked="checked"{{/if}} /><label for="linkCheckbox">{{linkShareLabel}}</label>' +
  18. '<br />' +
  19. '<label for="linkText" class="hidden-visually">{{urlLabel}}</label>' +
  20. '<input id="linkText" {{#unless isLinkShare}}class="hidden"{{/unless}} type="text" readonly="readonly" value="{{shareLinkURL}}" />' +
  21. ' {{#if showPasswordCheckBox}}' +
  22. '<input type="checkbox" name="showPassword" id="showPassword" {{#if isPasswordSet}}checked="checked"{{/if}} value="1" /><label for="showPassword">{{enablePasswordLabel}}</label>' +
  23. ' {{/if}}' +
  24. '<div id="linkPass" {{#unless isPasswordSet}}class="hidden"{{/unless}}>' +
  25. ' <label for="linkPassText" class="hidden-visually">{{passwordLabel}}</label>' +
  26. ' <input id="linkPassText" type="password" placeholder="{{passwordPlaceholder}}" />' +
  27. ' <span class="icon-loading-small hidden"></span>' +
  28. '</div>' +
  29. ' {{#if publicUpload}}' +
  30. '<div id="allowPublicUploadWrapper">' +
  31. ' <span class="icon-loading-small hidden"></span>' +
  32. ' <input type="checkbox" value="1" name="allowPublicUpload" id="sharingDialogAllowPublicUpload" {{{publicUploadChecked}}} />' +
  33. '<label for="sharingDialogAllowPublicUpload">{{publicUploadLabel}}</label>' +
  34. '</div>' +
  35. ' {{/if}}' +
  36. ' {{#if mailPublicNotificationEnabled}}' +
  37. '<form id="emailPrivateLink" class="emailPrivateLinkForm">' +
  38. ' <input id="email" value="" placeholder="{{mailPrivatePlaceholder}}" type="text" />' +
  39. ' <input id="emailButton" type="submit" value="{{mailButtonText}}" />' +
  40. '</form>' +
  41. ' {{/if}}' +
  42. '{{else}}' +
  43. '<input id="shareWith" type="text" placeholder="{{noSharingPlaceholder}}" disabled="disabled"/>' +
  44. '{{/if}}'
  45. ;
  46. /**
  47. * @class OCA.Share.ShareDialogLinkShareView
  48. * @member {OC.Share.ShareItemModel} model
  49. * @member {jQuery} $el
  50. * @memberof OCA.Sharing
  51. * @classdesc
  52. *
  53. * Represents the GUI of the share dialogue
  54. *
  55. */
  56. var ShareDialogLinkShareView = OC.Backbone.View.extend({
  57. /** @type {string} **/
  58. id: 'shareDialogLinkShare',
  59. /** @type {OC.Share.ShareConfigModel} **/
  60. configModel: undefined,
  61. /** @type {Function} **/
  62. _template: undefined,
  63. /** @type {boolean} **/
  64. showLink: true,
  65. events: {
  66. 'submit .emailPrivateLinkForm': '_onEmailPrivateLink'
  67. },
  68. initialize: function(options) {
  69. var view = this;
  70. this.model.on('change:permissions', function() {
  71. view.render();
  72. });
  73. this.model.on('change:itemType', function() {
  74. view.render();
  75. });
  76. this.model.on('change:allowPublicUploadStatus', function() {
  77. view.render();
  78. });
  79. this.model.on('change:linkShare', function() {
  80. view.render();
  81. });
  82. if(!_.isUndefined(options.configModel)) {
  83. this.configModel = options.configModel;
  84. } else {
  85. throw 'missing OC.Share.ShareConfigModel';
  86. }
  87. _.bindAll(this, 'onLinkCheckBoxChange', 'onPasswordEntered',
  88. 'onShowPasswordClick', 'onAllowPublicUploadChange');
  89. },
  90. onLinkCheckBoxChange: function() {
  91. var $checkBox = this.$el.find('#linkCheckbox');
  92. var $loading = $checkBox.siblings('.icon-loading-small');
  93. if(!$loading.hasClass('hidden')) {
  94. return false;
  95. }
  96. if($checkBox.is(':checked')) {
  97. if(this.configModel.get('enforcePasswordForPublicLink') === false) {
  98. $loading.removeClass('hidden');
  99. // this will create it
  100. this.model.saveLinkShare();
  101. } else {
  102. this.$el.find('#linkPass').slideToggle(OC.menuSpeed);
  103. // TODO drop with IE8 drop
  104. if($('html').hasClass('ie8')) {
  105. this.$el.find('#linkPassText').attr('placeholder', null);
  106. this.$el.find('#linkPassText').val('');
  107. }
  108. this.$el.find('#linkPassText').focus();
  109. }
  110. } else {
  111. this.model.removeLinkShare();
  112. }
  113. },
  114. onLinkTextClick: function() {
  115. this.focus();
  116. this.select();
  117. },
  118. onShowPasswordClick: function() {
  119. this.$el.find('#linkPass').slideToggle(OC.menuSpeed);
  120. if(!this.$el.find('#showPassword').is(':checked')) {
  121. this.model.setPassword('');
  122. this.model.saveLinkShare();
  123. } else {
  124. this.$el.find('#linkPassText').focus();
  125. }
  126. },
  127. onPasswordEntered: function() {
  128. var password = this.$el.find('#linkPassText').val();
  129. if(password === '') {
  130. return;
  131. }
  132. this.$el.find('#linkPass .icon-loading-small')
  133. .removeClass('hidden')
  134. .addClass('inlineblock');
  135. this.model.setPassword(password);
  136. this.model.saveLinkShare();
  137. },
  138. onAllowPublicUploadChange: function() {
  139. this.$el.find('#sharingDialogAllowPublicUpload')
  140. .siblings('.icon-loading-small').removeClass('hidden').addClass('inlineblock');
  141. this.model.setPublicUpload(this.$el.find('#sharingDialogAllowPublicUpload').is(':checked'));
  142. this.model.saveLinkShare();
  143. },
  144. _onEmailPrivateLink: function(event) {
  145. event.preventDefault();
  146. var $emailField = this.$el.find('#email');
  147. var $emailButton = this.$el.find('#emailButton');
  148. var email = this.$el.find('#email').val();
  149. if (email !== '') {
  150. $emailField.prop('disabled', true);
  151. $emailButton.prop('disabled', true);
  152. $emailField.val(t('core', 'Sending ...'));
  153. this.model.sendEmailPrivateLink(email).then(function() {
  154. $emailField.css('font-weight', 'bold').val(t('core','Email sent'));
  155. setTimeout(function() {
  156. $emailField.css('font-weight', 'normal').val('');
  157. $emailField.prop('disabled', false);
  158. $emailButton.prop('disabled', false);
  159. }, 2000);
  160. });
  161. }
  162. return false;
  163. },
  164. render: function() {
  165. var linkShareTemplate = this.template();
  166. if( !this.model.sharePermissionPossible()
  167. || !this.showLink
  168. || !this.configModel.isShareWithLinkAllowed())
  169. {
  170. this.$el.html(linkShareTemplate({
  171. shareAllowed: false,
  172. noSharingPlaceholder: t('core', 'Resharing is not allowed')
  173. }));
  174. return this;
  175. }
  176. var publicUpload =
  177. this.model.isFolder()
  178. && this.model.createPermissionPossible()
  179. && this.configModel.isPublicUploadEnabled();
  180. var publicUploadChecked = '';
  181. if(this.model.isPublicUploadAllowed()) {
  182. publicUploadChecked = 'checked="checked"';
  183. }
  184. var isLinkShare = this.model.get('linkShare').isLinkShare;
  185. var isPasswordSet = !!this.model.get('linkShare').password;
  186. var showPasswordCheckBox = isLinkShare
  187. && ( !this.configModel.get('enforcePasswordForPublicLink')
  188. || !this.model.get('linkShare').password);
  189. this.$el.html(linkShareTemplate({
  190. shareAllowed: true,
  191. isLinkShare: isLinkShare,
  192. shareLinkURL: this.model.get('linkShare').link,
  193. linkShareLabel: t('core', 'Share link'),
  194. urlLabel: t('core', 'Link'),
  195. enablePasswordLabel: t('core', 'Password protect'),
  196. passwordLabel: t('core', 'Password'),
  197. passwordPlaceholder: isPasswordSet ? '**********' : t('core', 'Choose a password for the public link'),
  198. isPasswordSet: isPasswordSet,
  199. showPasswordCheckBox: showPasswordCheckBox,
  200. publicUpload: publicUpload && isLinkShare,
  201. publicUploadChecked: publicUploadChecked,
  202. publicUploadLabel: t('core', 'Allow editing'),
  203. mailPublicNotificationEnabled: isLinkShare && this.configModel.isMailPublicNotificationEnabled(),
  204. mailPrivatePlaceholder: t('core', 'Email link to person'),
  205. mailButtonText: t('core', 'Send')
  206. }));
  207. // TODO: move this to delegate events instead
  208. this.$el.find('#linkCheckbox').click(this.onLinkCheckBoxChange);
  209. this.$el.find('#sharingDialogAllowPublicUpload').change(this.onAllowPublicUploadChange);
  210. this.$el.find('#linkText').click(this.onLinkTextClick);
  211. this.$el.find('#showPassword').click(this.onShowPasswordClick);
  212. this.$el.find('#linkPassText').focusout(this.onPasswordEntered);
  213. var view = this;
  214. this.$el.find('#linkPassText').keyup(function(event) {
  215. if(event.keyCode == 13) {
  216. view.onPasswordEntered();
  217. }
  218. });
  219. var $emailField = this.$el.find('#email');
  220. if (isLinkShare && $emailField.length !== 0) {
  221. $emailField.autocomplete({
  222. minLength: 1,
  223. source: function (search, response) {
  224. $.get(
  225. OC.generateUrl('core/ajax/share.php'), {
  226. fetch: 'getShareWithEmail',
  227. search: search.term
  228. }, function(result) {
  229. if (result.status == 'success' && result.data.length > 0) {
  230. response(result.data);
  231. }
  232. });
  233. },
  234. select: function( event, item ) {
  235. $emailField.val(item.item.email);
  236. return false;
  237. }
  238. })
  239. .data("ui-autocomplete")._renderItem = function( ul, item ) {
  240. return $('<li>')
  241. .append('<a>' + escapeHTML(item.displayname) + "<br>" + escapeHTML(item.email) + '</a>' )
  242. .appendTo( ul );
  243. };
  244. }
  245. this.delegateEvents();
  246. return this;
  247. },
  248. /**
  249. * @returns {Function} from Handlebars
  250. * @private
  251. */
  252. template: function () {
  253. if (!this._template) {
  254. this._template = Handlebars.compile(TEMPLATE);
  255. }
  256. return this._template;
  257. }
  258. });
  259. OC.Share.ShareDialogLinkShareView = ShareDialogLinkShareView;
  260. })();