some refactoring (cc @techAPJ)

This commit is contained in:
Régis Hanol 2015-04-03 18:42:56 +02:00
parent ea31edbf74
commit 4e6a2a1fa4
5 changed files with 40 additions and 126 deletions

View File

@ -12,77 +12,41 @@ export default ObjectController.extend(ModalFunctionality, {
return Discourse.User.currentProp("admin"); return Discourse.User.currentProp("admin");
}.property(), }.property(),
/**
Can we submit the form?
@property disabled
**/
disabled: function() { disabled: function() {
if (this.get('saving')) return true; if (this.get('saving')) return true;
if (this.blank('emailOrUsername')) 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('model.details.can_invite_to')) return false;
if (this.get('isPrivateTopic') && this.blank('groupNames')) return true; if (this.get('isPrivateTopic') && this.blank('groupNames')) return true;
return false; return false;
}.property('emailOrUsername', 'invitingToTopic', 'isPrivateTopic', 'groupNames', 'saving'), }.property('emailOrUsername', 'invitingToTopic', 'isPrivateTopic', 'groupNames', 'saving'),
/**
The current text for the invite button
@property buttonTitle
**/
buttonTitle: function() { buttonTitle: function() {
if (this.get('saving')) return I18n.t('topic.inviting'); return this.get('saving') ? I18n.t('topic.inviting') : I18n.t('topic.invite_reply.action');
return I18n.t('topic.invite_reply.action');
}.property('saving'), }.property('saving'),
/** // We are inviting to a topic if the model isn't the current user.
We are inviting to a topic if the model isn't the current user. The current user would // The current user would mean we are inviting to the forum in general.
mean we are inviting to the forum in general.
@property invitingToTopic
**/
invitingToTopic: function() { invitingToTopic: function() {
return this.get('model') !== Discourse.User.current(); return this.get('model') !== Discourse.User.current();
}.property('model'), }.property('model'),
/** // Is Private Topic? (i.e. visible only to specific group members)
Is Private Topic? (i.e. visible only to specific group members)
@property isPrivateTopic
**/
isPrivateTopic: Em.computed.and('invitingToTopic', 'model.category.read_restricted'), isPrivateTopic: Em.computed.and('invitingToTopic', 'model.category.read_restricted'),
/**
Is Message?
@property isMessage
**/
isMessage: Em.computed.equal('model.archetype', 'private_message'), isMessage: Em.computed.equal('model.archetype', 'private_message'),
/** // Allow Existing Members? (username autocomplete)
Allow Existing Members? (username autocomplete)
@property allowExistingMembers
**/
allowExistingMembers: function() { allowExistingMembers: function() {
return this.get('invitingToTopic') && !this.get('isPrivateTopic'); return this.get('invitingToTopic') && !this.get('isPrivateTopic');
}.property('invitingToTopic', 'isPrivateTopic'), }.property('invitingToTopic', 'isPrivateTopic'),
/** // Show Groups? (add invited user to private group)
Show Groups? (add invited user to private group)
@property showGroups
**/
showGroups: function() { showGroups: function() {
return this.get('isAdmin') && (Discourse.Utilities.emailValid(this.get('emailOrUsername')) || this.get('isPrivateTopic') || !this.get('invitingToTopic')); return this.get('isAdmin') && (Discourse.Utilities.emailValid(this.get('emailOrUsername')) || this.get('isPrivateTopic') || !this.get('invitingToTopic'));
}.property('isAdmin', 'emailOrUsername', 'isPrivateTopic', 'invitingToTopic'), }.property('isAdmin', 'emailOrUsername', 'isPrivateTopic', 'invitingToTopic'),
/** // Instructional text for the modal.
Instructional text for the modal.
@property inviteInstructions
**/
inviteInstructions: function() { inviteInstructions: function() {
if (this.get('isMessage')) { if (this.get('isMessage')) {
return I18n.t('topic.invite_private.email_or_username'); return I18n.t('topic.invite_private.email_or_username');
@ -100,58 +64,29 @@ export default ObjectController.extend(ModalFunctionality, {
} }
}.property('isMessage', 'invitingToTopic', 'emailOrUsername'), }.property('isMessage', 'invitingToTopic', 'emailOrUsername'),
/** // Instructional text for the group selection.
Instructional text for the group selection.
@property groupInstructions
**/
groupInstructions: function() { groupInstructions: function() {
if (this.get('isPrivateTopic')) { return this.get('isPrivateTopic') ?
return I18n.t('topic.automatically_add_to_groups_required'); I18n.t('topic.automatically_add_to_groups_required') :
} else { I18n.t('topic.automatically_add_to_groups_optional');
return I18n.t('topic.automatically_add_to_groups_optional');
}
}.property('isPrivateTopic'), }.property('isPrivateTopic'),
/** groupFinder(term) {
Function to find groups.
**/
groupFinder: function(term) {
return Discourse.Group.findAll({search: term, ignore_automatic: true}); return Discourse.Group.findAll({search: term, ignore_automatic: true});
}, },
/**
The "success" text for when the invite was created.
@property successMessage
**/
successMessage: function() { successMessage: function() {
if (this.get('isMessage')) { return this.get('isMessage') ?
return I18n.t('topic.invite_private.success'); I18n.t('topic.invite_private.success') :
} else { I18n.t('topic.invite_reply.success', { emailOrUsername: this.get('emailOrUsername') });
return I18n.t('topic.invite_reply.success', { emailOrUsername: this.get('emailOrUsername') });
}
}.property('isMessage', 'emailOrUsername'), }.property('isMessage', 'emailOrUsername'),
/**
The "error" text for when the invite fails.
@property errorMessage
**/
errorMessage: function() { errorMessage: function() {
if (this.get('isMessage')) { return this.get('isMessage') ? I18n.t('topic.invite_private.error') : I18n.t('topic.invite_reply.error');
return I18n.t('topic.invite_private.error');
} else {
return I18n.t('topic.invite_reply.error');
}
}.property('isMessage'), }.property('isMessage'),
/** // Reset the modal to allow a new user to be invited.
Reset the modal to allow a new user to be invited. reset() {
@method reset
**/
reset: function() {
this.setProperties({ this.setProperties({
emailOrUsername: null, emailOrUsername: null,
groupNames: null, groupNames: null,
@ -163,36 +98,26 @@ export default ObjectController.extend(ModalFunctionality, {
actions: { actions: {
/** createInvite() {
Create the invite and update the modal accordingly.
@method createInvite
**/
createInvite: function() {
if (this.get('disabled')) { return; } if (this.get('disabled')) { return; }
var self = this; const groupNames = this.get('groupNames'),
var groupNames = this.get('groupNames'); userInvitedController = this.get('controllers.user-invited');
var userInvitedController = this.get('controllers.user-invited');
this.setProperties({ saving: true, error: false }); this.setProperties({ saving: true, error: false });
this.get('model').createInvite(this.get('emailOrUsername'), groupNames).then(function(result) {
self.setProperties({ saving: false, finished: true }); return this.get('model').createInvite(this.get('emailOrUsername'), groupNames).then(result => {
if (!self.get('invitingToTopic')) { this.setProperties({ saving: false, finished: true });
Discourse.Invite.findInvitedBy(Discourse.User.current()).then(function (invite_model) { if (!this.get('invitingToTopic')) {
userInvitedController.set('model', invite_model); Discourse.Invite.findInvitedBy(Discourse.User.current()).then(invite_model => {
userInvitedController.set('totalInvites', invite_model.invites.length); 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); } else if (this.get('isMessage') && result && result.user) {
} this.get('model.details.allowed_users').pushObject(result.user);
}).catch(function() { }
self.setProperties({ saving: false, error: true }); }).catch(() => this.setProperties({ saving: false, error: true }));
});
return false;
} }
} }
}); });

View File

@ -5,18 +5,15 @@
{{errorMessage}} {{errorMessage}}
</div> </div>
{{/if}} {{/if}}
{{#if finished}} {{#if finished}}
{{{successMessage}}} {{{successMessage}}}
{{else}} {{else}}
<label>{{inviteInstructions}}</label> <label>{{inviteInstructions}}</label>
{{#if allowExistingMembers}} {{#if allowExistingMembers}}
{{user-selector single="true" allowAny=true usernames=emailOrUsername includeGroups="true" placeholderKey="topic.invite_private.email_or_username_placeholder"}} {{user-selector single="true" allowAny=true usernames=emailOrUsername includeGroups="true" placeholderKey="topic.invite_private.email_or_username_placeholder"}}
{{else}} {{else}}
{{text-field value=emailOrUsername placeholderKey="topic.invite_reply.email_placeholder"}} {{text-field value=emailOrUsername placeholderKey="topic.invite_reply.email_placeholder"}}
{{/if}} {{/if}}
{{#if showGroups}} {{#if showGroups}}
<label>{{{groupInstructions}}}</label> <label>{{{groupInstructions}}}</label>
{{group-selector groupFinder=groupFinder groupNames=groupNames placeholderKey="topic.invite_private.group_name"}} {{group-selector groupFinder=groupFinder groupNames=groupNames placeholderKey="topic.invite_private.group_name"}}
@ -25,9 +22,8 @@
</div> </div>
<div class="modal-footer"> <div class="modal-footer">
{{#if finished}} {{#if finished}}
<button class='btn btn-primary' {{action "closeModal"}}>{{i18n 'close'}}</button> {{d-button class="btn-primary" action="closeModal" label="close"}}
{{else}} {{else}}
<button class='btn btn-primary' {{bind-attr disabled="disabled"}} {{action "createInvite"}}><i class="fa fa-user-plus"></i>{{buttonTitle}}</button> <button class='btn btn-primary' {{bind-attr disabled="disabled"}} {{action "createInvite"}}>{{fa-icon "user-plus"}}{{buttonTitle}}</button>
{{/if}} {{/if}}
</div> </div>

View File

@ -6,11 +6,11 @@ export default ButtonView.extend({
attributeBindings: ['disabled'], attributeBindings: ['disabled'],
disabled: Em.computed.or('controller.archived', 'controller.closed', 'controller.deleted'), disabled: Em.computed.or('controller.archived', 'controller.closed', 'controller.deleted'),
renderIcon: function(buffer) { renderIcon(buffer) {
buffer.push("<i class='fa fa-users'></i>"); buffer.push("<i class='fa fa-users'></i>");
}, },
click: function() { click() {
return this.get('controller').send('showInvite'); this.get('controller').send('showInvite');
} }
}); });

View File

@ -7,7 +7,7 @@ export default Discourse.View.extend({
// Focus on first element // Focus on first element
if (!Discourse.Mobile.mobileView && this.get('focusInput')) { 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'); const title = this.get('title');

View File

@ -12,12 +12,5 @@ export default Ember.View.extend({
// the backdrop and makes it unclickable. // the backdrop and makes it unclickable.
$('.modal-header a.close').click(); $('.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());
}
} }
}); });