New admin interface for improved channel definitions

This commit is contained in:
David Taylor
2017-07-18 16:17:03 +01:00
parent 1ef9073027
commit bdb81191d7
24 changed files with 514 additions and 308 deletions
@@ -7,42 +7,38 @@ import { popupAjaxError } from 'discourse/lib/ajax-error';
export default Ember.Controller.extend({
modalShowing: false,
anyErrors: function(){
var anyErrors = false;
this.get('model.rules').forEach(function(rule){
if(rule.error_key){
this.get('model.channels').forEach(function(channel){
if(channel.error_key){
anyErrors = true;
}
});
return anyErrors;
}.property('model.rules'),
}.property('model.channels'),
actions:{
create(){
createChannel(){
this.set('modalShowing', true);
var model = {rule: this.store.createRecord('rule',{provider: this.get('model.provider').id}), provider:this.get('model.provider')};
showModal('admin-plugins-chat-edit-rule', { model: model, admin: true });
var model = {channel: this.store.createRecord('channel',{provider: this.get('model.provider').id, data:{}},), provider:this.get('model.provider')};
showModal('admin-plugins-chat-edit-channel', { model: model, admin: true });
},
edit(rule){
editChannel(channel){
this.set('modalShowing', true);
var model = {rule: rule, provider:this.get('model.provider')};
showModal('admin-plugins-chat-edit-rule', { model: model, admin: true });
},
delete(rule){
const self = this;
rule.destroyRecord().then(function() {
self.send('refresh');
}).catch(popupAjaxError)
var model = {channel: channel, provider: this.get('model.provider')};
showModal('admin-plugins-chat-edit-channel', { model: model, admin: true });
},
testChannel(channel){
this.set('modalShowing', true);
var model = {channel:channel}
showModal('admin-plugins-chat-test', { model: model, admin: true });
},
showError(error_key){
bootbox.alert(I18n.t(error_key));
},
test(){
this.set('modalShowing', true);
var model = {provider:this.get('model.provider'), channel:''}
showModal('admin-plugins-chat-test', { model: model, admin: true });
}
}
@@ -0,0 +1,94 @@
import Rule from 'discourse/plugins/discourse-chat-integration/admin/models/rule'
import ModalFunctionality from 'discourse/mixins/modal-functionality';
import { ajax } from 'discourse/lib/ajax';
import { extractError } from 'discourse/lib/ajax-error';
import InputValidation from 'discourse/models/input-validation';
export default Ember.Controller.extend(ModalFunctionality, {
initThing: function(){
console.log("Initialising controller");
console.log(this.get('model.data'));
}.on('init'),
// The validation property must be defined at runtime since the possible parameters vary by provider
setupValidations: function(){
if(this.get('model.provider')){
var theKeys = this.get('model.provider.channel_parameters').map( ( param ) => param['key'] );
Ember.defineProperty(this,'paramValidation',Ember.computed('model.channel.data.{' + theKeys.join(',') + '}',this._paramValidation));
}
}.observes('model'),
validate(parameter){
var regString = parameter.regex;
var regex = new RegExp(regString);
var val = this.get('model.channel.data.'+parameter.key);
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_channel_modal.channel_validation.ok')
});
}else{ // Failed regex
return InputValidation.create({
failed: true,
reason: I18n.t('chat_integration.edit_channel_modal.channel_validation.fail')
});
}
},
_paramValidation: function(){
var response = {}
var parameters = this.get('model.provider.channel_parameters');
parameters.forEach(parameter => {
response[parameter.key] = this.validate(parameter);
});
return response;
},
saveDisabled: function(){
var validations = this.get('paramValidation');
if(!validations){ return true }
var invalid = false;
Object.keys(validations).forEach(key =>{
if(!validations[key]){
invalid = true;
}
if(!validations[key]['ok']){
invalid = true;
}
});
return invalid;
}.property('paramValidation'),
actions: {
cancel: function(){
this.send('closeModal');
},
save: function(){
const self = this;
this.get('model.channel').save().then(function(result) {
self.send('closeModal');
}).catch(function(error) {
self.flash(extractError(error), 'error');
});
}
}
});
@@ -1,61 +0,0 @@
import Rule from 'discourse/plugins/discourse-chat-integration/admin/models/rule'
import ModalFunctionality from 'discourse/mixins/modal-functionality';
import { ajax } from 'discourse/lib/ajax';
import { extractError } from 'discourse/lib/ajax-error';
import InputValidation from 'discourse/models/input-validation';
export default Ember.Controller.extend(ModalFunctionality, {
model: Rule.create({}),
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'),
actions: {
cancel: function(){
this.send('closeModal');
},
save: function(){
const self = this;
this.get('model.rule').update().then(function(result) {
self.send('closeModal');
}).catch(function(error) {
self.flash(extractError(error), 'error');
});
}
}
});
@@ -3,11 +3,11 @@ import { ajax } from 'discourse/lib/ajax';
export default Ember.Controller.extend(ModalFunctionality, {
sendDisabled: function(){
if(this.get('model').topic_id && this.get('model').channel){
if(this.get('model').topic_id){
return false
}
return true
}.property('model.topic_id', 'model.channel'),
}.property('model.topic_id'),
actions: {
@@ -15,8 +15,7 @@ export default Ember.Controller.extend(ModalFunctionality, {
self = this;
this.set('loading', true);
ajax("/admin/plugins/chat/test", {
data: { provider: this.get('model.provider.name'),
channel: this.get('model.channel'),
data: { channel_id: this.get('model.channel.id'),
topic_id: this.get('model.topic_id')
},
type: 'POST'