sharedialogmailview.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * Copyright (c) 2016
  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 escapeHTML, Handlebars */
  11. (function() {
  12. if (!OC.Share) {
  13. OC.Share = {};
  14. }
  15. var TEMPLATE =
  16. '{{#if shareAllowed}}' +
  17. ' {{#if mailPublicNotificationEnabled}}' +
  18. '<form id="emailPrivateLink" class="emailPrivateLinkForm oneline">' +
  19. ' <input id="email" class="emailField" value="{{email}}" placeholder="{{mailPrivatePlaceholder}}" type="text" />' +
  20. ' <a id="emailButton" class="icon icon-mail hasTooltip" title="Send e-mail"></a>' +
  21. '</form>' +
  22. ' {{/if}}' +
  23. '{{/if}}'
  24. ;
  25. /**
  26. * @class OCA.Share.ShareDialogMailView
  27. * @member {OC.Share.ShareItemModel} model
  28. * @member {jQuery} $el
  29. * @memberof OCA.Sharing
  30. * @classdesc
  31. *
  32. * Represents the GUI of the share dialogue
  33. *
  34. */
  35. var ShareDialogMailView = OC.Backbone.View.extend({
  36. /** @type {string} **/
  37. id: 'shareDialogMailView',
  38. /** @type {OC.Share.ShareConfigModel} **/
  39. configModel: undefined,
  40. /** @type {Function} **/
  41. _template: undefined,
  42. /** @type {boolean} **/
  43. showLink: true,
  44. events: {
  45. 'click #emailButton': '_onEmailPrivateLink'
  46. },
  47. initialize: function(options) {
  48. var view = this;
  49. this.model.on('change:linkShare', function() {
  50. view.render();
  51. });
  52. if(!_.isUndefined(options.configModel)) {
  53. this.configModel = options.configModel;
  54. } else {
  55. throw 'missing OC.Share.ShareConfigModel';
  56. }
  57. _.bindAll(
  58. this,
  59. '_onEmailPrivateLink'
  60. );
  61. },
  62. _onEmailPrivateLink: function(event) {
  63. event.preventDefault();
  64. var $emailField = this.$el.find('.emailField');
  65. var $emailButton = this.$el.find('.emailButton');
  66. var email = $emailField.val();
  67. if (email !== '') {
  68. $emailField.prop('disabled', true);
  69. $emailButton.prop('disabled', true);
  70. $emailField.val(t('core', 'Sending ...'));
  71. this.model.sendEmailPrivateLink(email).done(function() {
  72. $emailField.css('font-weight', 'bold').val(t('core','Email sent'));
  73. setTimeout(function() {
  74. $emailField.val('');
  75. $emailField.css('font-weight', 'normal');
  76. $emailField.prop('disabled', false);
  77. $emailButton.prop('disabled', false);
  78. }, 2000);
  79. }).fail(function() {
  80. $emailField.val(email);
  81. $emailField.css('font-weight', 'normal');
  82. $emailField.prop('disabled', false);
  83. $emailButton.prop('disabled', false);
  84. });
  85. }
  86. return false;
  87. },
  88. render: function() {
  89. var linkShareTemplate = this.template();
  90. var resharingAllowed = this.model.sharePermissionPossible();
  91. var email = this.$el.find('.emailField').val();
  92. if(!resharingAllowed
  93. || !this.showLink
  94. || !this.configModel.isShareWithLinkAllowed())
  95. {
  96. var templateData = {shareAllowed: false};
  97. if (!resharingAllowed) {
  98. // add message
  99. templateData.noSharingPlaceholder = t('core', 'Resharing is not allowed');
  100. }
  101. this.$el.html(linkShareTemplate(templateData));
  102. return this;
  103. }
  104. var isLinkShare = this.model.get('linkShare').isLinkShare;
  105. this.$el.html(linkShareTemplate({
  106. cid: this.cid,
  107. shareAllowed: true,
  108. mailPublicNotificationEnabled: isLinkShare && this.configModel.isMailPublicNotificationEnabled(),
  109. mailPrivatePlaceholder: t('core', 'Email link to person'),
  110. mailButtonText: t('core', 'Send link via email'),
  111. email: email
  112. }));
  113. var $emailField = this.$el.find('.emailField');
  114. if (isLinkShare && $emailField.length !== 0) {
  115. $emailField.autocomplete({
  116. minLength: 1,
  117. source: function (search, response) {
  118. $.get(
  119. OC.generateUrl('core/ajax/share.php'), {
  120. fetch: 'getShareWithEmail',
  121. search: search.term
  122. }, function(result) {
  123. if (result.status === 'success' && result.data.length > 0) {
  124. response(result.data);
  125. }
  126. });
  127. },
  128. select: function( event, item ) {
  129. $emailField.val(item.item.email);
  130. return false;
  131. }
  132. })
  133. .data("ui-autocomplete")._renderItem = function( ul, item ) {
  134. return $('<li>')
  135. .append('<a>' + escapeHTML(item.displayname) + "<br>" + escapeHTML(item.email) + '</a>' )
  136. .appendTo( ul );
  137. };
  138. }
  139. this.delegateEvents();
  140. return this;
  141. },
  142. /**
  143. * @returns {Function} from Handlebars
  144. * @private
  145. */
  146. template: function () {
  147. if (!this._template) {
  148. this._template = Handlebars.compile(TEMPLATE);
  149. }
  150. return this._template;
  151. }
  152. });
  153. OC.Share.ShareDialogMailView = ShareDialogMailView;
  154. })();