2017-07-03 12:18:30 +01:00
|
|
|
import Rule from 'discourse/plugins/discourse-chat-integration/admin/models/rule'
|
2017-06-29 17:50:54 +01:00
|
|
|
import ModalFunctionality from 'discourse/mixins/modal-functionality';
|
2017-06-28 18:04:21 +01:00
|
|
|
import { ajax } from 'discourse/lib/ajax';
|
2017-06-29 17:50:54 +01:00
|
|
|
import { extractError } from 'discourse/lib/ajax-error';
|
2017-07-04 00:14:01 +01:00
|
|
|
import InputValidation from 'discourse/models/input-validation';
|
2017-06-28 18:04:21 +01:00
|
|
|
|
2017-06-29 17:50:54 +01:00
|
|
|
export default Ember.Controller.extend(ModalFunctionality, {
|
2017-06-28 18:04:21 +01:00
|
|
|
|
2017-06-28 22:32:02 +01:00
|
|
|
model: Rule.create({}),
|
2017-06-29 12:56:48 +01:00
|
|
|
|
2017-07-04 00:14:01 +01:00
|
|
|
channelValidation: function(){
|
|
|
|
|
|
|
|
var regString = this.get('model.provider.channel_regex');
|
|
|
|
var regex = new RegExp(regString);
|
|
|
|
var val = this.get('model.rule.channel');
|
|
|
|
|
|
|
|
if(val == ""){ // Fail silently if field blank
|
|
|
|
return InputValidation.create({
|
|
|
|
failed: true,
|
|
|
|
});
|
|
|
|
}else if(!regString){ // Pass silently if no regex available for provider
|
|
|
|
return InputValidation.create({
|
|
|
|
ok: true,
|
|
|
|
});
|
|
|
|
}else if(regex.test(val)){ // Test against regex
|
|
|
|
return InputValidation.create({
|
|
|
|
ok: true,
|
|
|
|
reason: I18n.t('chat_integration.edit_rule_modal.channel_validation.ok')
|
|
|
|
});
|
|
|
|
}else{ // Failed regex
|
|
|
|
return InputValidation.create({
|
|
|
|
failed: true,
|
|
|
|
reason: I18n.t('chat_integration.edit_rule_modal.channel_validation.fail')
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}.property('model.rule.channel'),
|
|
|
|
|
|
|
|
saveDisabled: function(){
|
|
|
|
if(this.get('channelValidation.failed')){ return true }
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}.property('channelValidation.failed'),
|
|
|
|
|
2017-06-28 18:04:21 +01:00
|
|
|
actions: {
|
|
|
|
cancel: function(){
|
|
|
|
this.send('closeModal');
|
|
|
|
},
|
2017-06-29 17:50:54 +01:00
|
|
|
|
|
|
|
save: function(){
|
|
|
|
|
|
|
|
const self = this;
|
|
|
|
|
2017-07-04 00:14:01 +01:00
|
|
|
this.get('model.rule').update().then(function(result) {
|
2017-06-29 17:50:54 +01:00
|
|
|
self.send('closeModal');
|
|
|
|
}).catch(function(error) {
|
|
|
|
self.flash(extractError(error), 'error');
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
2017-06-28 18:04:21 +01:00
|
|
|
}
|
|
|
|
});
|