From 4e6a2a1fa4642b9f7ec8bfcbe7c8a9e648ca53ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Hanol?= Date: Fri, 3 Apr 2015 18:42:56 +0200 Subject: [PATCH] some refactoring (cc @techAPJ) --- .../discourse/controllers/invite.js.es6 | 143 +++++------------- .../discourse/templates/modal/invite.hbs | 8 +- .../views/invite-reply-button.js.es6 | 6 +- .../discourse/views/modal-body.js.es6 | 2 +- .../javascripts/discourse/views/modal.js.es6 | 7 - 5 files changed, 40 insertions(+), 126 deletions(-) diff --git a/app/assets/javascripts/discourse/controllers/invite.js.es6 b/app/assets/javascripts/discourse/controllers/invite.js.es6 index 6b396130c36..7c7c7c17754 100644 --- a/app/assets/javascripts/discourse/controllers/invite.js.es6 +++ b/app/assets/javascripts/discourse/controllers/invite.js.es6 @@ -12,77 +12,41 @@ export default ObjectController.extend(ModalFunctionality, { return Discourse.User.currentProp("admin"); }.property(), - /** - Can we submit the form? - - @property disabled - **/ disabled: function() { if (this.get('saving')) return true; if (this.blank('emailOrUsername')) return true; - if ( !this.get('invitingToTopic') && !Discourse.Utilities.emailValid(this.get('emailOrUsername')) ) return true; + if (!this.get('invitingToTopic') && !Discourse.Utilities.emailValid(this.get('emailOrUsername'))) return true; if (this.get('model.details.can_invite_to')) return false; if (this.get('isPrivateTopic') && this.blank('groupNames')) return true; return false; }.property('emailOrUsername', 'invitingToTopic', 'isPrivateTopic', 'groupNames', 'saving'), - /** - The current text for the invite button - - @property buttonTitle - **/ buttonTitle: function() { - if (this.get('saving')) return I18n.t('topic.inviting'); - return I18n.t('topic.invite_reply.action'); + return this.get('saving') ? I18n.t('topic.inviting') : I18n.t('topic.invite_reply.action'); }.property('saving'), - /** - We are inviting to a topic if the model isn't the current user. The current user would - mean we are inviting to the forum in general. - - @property invitingToTopic - **/ + // We are inviting to a topic if the model isn't the current user. + // The current user would mean we are inviting to the forum in general. invitingToTopic: function() { return this.get('model') !== Discourse.User.current(); }.property('model'), - /** - Is Private Topic? (i.e. visible only to specific group members) - - @property isPrivateTopic - **/ + // Is Private Topic? (i.e. visible only to specific group members) isPrivateTopic: Em.computed.and('invitingToTopic', 'model.category.read_restricted'), - /** - Is Message? - - @property isMessage - **/ isMessage: Em.computed.equal('model.archetype', 'private_message'), - /** - Allow Existing Members? (username autocomplete) - - @property allowExistingMembers - **/ + // Allow Existing Members? (username autocomplete) allowExistingMembers: function() { return this.get('invitingToTopic') && !this.get('isPrivateTopic'); }.property('invitingToTopic', 'isPrivateTopic'), - /** - Show Groups? (add invited user to private group) - - @property showGroups - **/ + // Show Groups? (add invited user to private group) showGroups: function() { return this.get('isAdmin') && (Discourse.Utilities.emailValid(this.get('emailOrUsername')) || this.get('isPrivateTopic') || !this.get('invitingToTopic')); }.property('isAdmin', 'emailOrUsername', 'isPrivateTopic', 'invitingToTopic'), - /** - Instructional text for the modal. - - @property inviteInstructions - **/ + // Instructional text for the modal. inviteInstructions: function() { if (this.get('isMessage')) { return I18n.t('topic.invite_private.email_or_username'); @@ -100,58 +64,29 @@ export default ObjectController.extend(ModalFunctionality, { } }.property('isMessage', 'invitingToTopic', 'emailOrUsername'), - /** - Instructional text for the group selection. - - @property groupInstructions - **/ + // Instructional text for the group selection. groupInstructions: function() { - if (this.get('isPrivateTopic')) { - return I18n.t('topic.automatically_add_to_groups_required'); - } else { - return I18n.t('topic.automatically_add_to_groups_optional'); - } + return this.get('isPrivateTopic') ? + I18n.t('topic.automatically_add_to_groups_required') : + I18n.t('topic.automatically_add_to_groups_optional'); }.property('isPrivateTopic'), - /** - Function to find groups. - **/ - groupFinder: function(term) { + groupFinder(term) { return Discourse.Group.findAll({search: term, ignore_automatic: true}); }, - /** - The "success" text for when the invite was created. - - @property successMessage - **/ successMessage: function() { - if (this.get('isMessage')) { - return I18n.t('topic.invite_private.success'); - } else { - return I18n.t('topic.invite_reply.success', { emailOrUsername: this.get('emailOrUsername') }); - } + return this.get('isMessage') ? + I18n.t('topic.invite_private.success') : + I18n.t('topic.invite_reply.success', { emailOrUsername: this.get('emailOrUsername') }); }.property('isMessage', 'emailOrUsername'), - /** - The "error" text for when the invite fails. - - @property errorMessage - **/ errorMessage: function() { - if (this.get('isMessage')) { - return I18n.t('topic.invite_private.error'); - } else { - return I18n.t('topic.invite_reply.error'); - } + return this.get('isMessage') ? I18n.t('topic.invite_private.error') : I18n.t('topic.invite_reply.error'); }.property('isMessage'), - /** - Reset the modal to allow a new user to be invited. - - @method reset - **/ - reset: function() { + // Reset the modal to allow a new user to be invited. + reset() { this.setProperties({ emailOrUsername: null, groupNames: null, @@ -163,36 +98,26 @@ export default ObjectController.extend(ModalFunctionality, { actions: { - /** - Create the invite and update the modal accordingly. - - @method createInvite - **/ - createInvite: function() { - + createInvite() { if (this.get('disabled')) { return; } - var self = this; - var groupNames = this.get('groupNames'); - var userInvitedController = this.get('controllers.user-invited'); + const groupNames = this.get('groupNames'), + userInvitedController = this.get('controllers.user-invited'); this.setProperties({ saving: true, error: false }); - this.get('model').createInvite(this.get('emailOrUsername'), groupNames).then(function(result) { - self.setProperties({ saving: false, finished: true }); - if (!self.get('invitingToTopic')) { - Discourse.Invite.findInvitedBy(Discourse.User.current()).then(function (invite_model) { - userInvitedController.set('model', invite_model); - userInvitedController.set('totalInvites', invite_model.invites.length); - }); - } else if (self.get('isMessage') && result && result.user) { - self.get('model.details.allowed_users').pushObject(result.user); - } - }).catch(function() { - self.setProperties({ saving: false, error: true }); - }); - return false; + + return this.get('model').createInvite(this.get('emailOrUsername'), groupNames).then(result => { + this.setProperties({ saving: false, finished: true }); + if (!this.get('invitingToTopic')) { + Discourse.Invite.findInvitedBy(Discourse.User.current()).then(invite_model => { + userInvitedController.set('model', invite_model); + userInvitedController.set('totalInvites', invite_model.invites.length); + }); + } else if (this.get('isMessage') && result && result.user) { + this.get('model.details.allowed_users').pushObject(result.user); + } + }).catch(() => this.setProperties({ saving: false, error: true })); } } - }); diff --git a/app/assets/javascripts/discourse/templates/modal/invite.hbs b/app/assets/javascripts/discourse/templates/modal/invite.hbs index 52a8767298b..16c80290d34 100644 --- a/app/assets/javascripts/discourse/templates/modal/invite.hbs +++ b/app/assets/javascripts/discourse/templates/modal/invite.hbs @@ -5,18 +5,15 @@ {{errorMessage}} {{/if}} - {{#if finished}} {{{successMessage}}} {{else}} - {{#if allowExistingMembers}} {{user-selector single="true" allowAny=true usernames=emailOrUsername includeGroups="true" placeholderKey="topic.invite_private.email_or_username_placeholder"}} {{else}} {{text-field value=emailOrUsername placeholderKey="topic.invite_reply.email_placeholder"}} {{/if}} - {{#if showGroups}} {{group-selector groupFinder=groupFinder groupNames=groupNames placeholderKey="topic.invite_private.group_name"}} @@ -25,9 +22,8 @@ diff --git a/app/assets/javascripts/discourse/views/invite-reply-button.js.es6 b/app/assets/javascripts/discourse/views/invite-reply-button.js.es6 index fe30c68c96b..bea163892a6 100644 --- a/app/assets/javascripts/discourse/views/invite-reply-button.js.es6 +++ b/app/assets/javascripts/discourse/views/invite-reply-button.js.es6 @@ -6,11 +6,11 @@ export default ButtonView.extend({ attributeBindings: ['disabled'], disabled: Em.computed.or('controller.archived', 'controller.closed', 'controller.deleted'), - renderIcon: function(buffer) { + renderIcon(buffer) { buffer.push(""); }, - click: function() { - return this.get('controller').send('showInvite'); + click() { + this.get('controller').send('showInvite'); } }); diff --git a/app/assets/javascripts/discourse/views/modal-body.js.es6 b/app/assets/javascripts/discourse/views/modal-body.js.es6 index 720e9551642..ee621d5e31d 100644 --- a/app/assets/javascripts/discourse/views/modal-body.js.es6 +++ b/app/assets/javascripts/discourse/views/modal-body.js.es6 @@ -7,7 +7,7 @@ export default Discourse.View.extend({ // Focus on first element if (!Discourse.Mobile.mobileView && this.get('focusInput')) { - Em.run.schedule('afterRender', () => this.$('input:first').focus() ); + Em.run.schedule('afterRender', () => this.$('input:first').focus()); } const title = this.get('title'); diff --git a/app/assets/javascripts/discourse/views/modal.js.es6 b/app/assets/javascripts/discourse/views/modal.js.es6 index f1af3bb33d3..4283466f7e3 100644 --- a/app/assets/javascripts/discourse/views/modal.js.es6 +++ b/app/assets/javascripts/discourse/views/modal.js.es6 @@ -12,12 +12,5 @@ export default Ember.View.extend({ // the backdrop and makes it unclickable. $('.modal-header a.close').click(); } - }, - - keyDown(e) { - // Delegate click to modal close when pressing ESC - if (e.keyCode === 27) { - Em.run.next(() => $('.modal-header a.close').click()); - } } });