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");
}.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 }));
}
}
});

View File

@ -5,18 +5,15 @@
{{errorMessage}}
</div>
{{/if}}
{{#if finished}}
{{{successMessage}}}
{{else}}
<label>{{inviteInstructions}}</label>
{{#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}}
<label>{{{groupInstructions}}}</label>
{{group-selector groupFinder=groupFinder groupNames=groupNames placeholderKey="topic.invite_private.group_name"}}
@ -25,9 +22,8 @@
</div>
<div class="modal-footer">
{{#if finished}}
<button class='btn btn-primary' {{action "closeModal"}}>{{i18n 'close'}}</button>
{{d-button class="btn-primary" action="closeModal" label="close"}}
{{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}}
</div>

View File

@ -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("<i class='fa fa-users'></i>");
},
click: function() {
return this.get('controller').send('showInvite');
click() {
this.get('controller').send('showInvite');
}
});

View File

@ -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');

View File

@ -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());
}
}
});