Move rule editing into a modal dialog

This commit is contained in:
David Taylor 2017-07-31 17:09:21 +01:00
parent 4b2e8af711
commit d437634f61
14 changed files with 197 additions and 83 deletions

View File

@ -61,21 +61,14 @@ class DiscourseChat::Rule < DiscourseChat::PluginModel
end
end
# Don't want this to end up as anything other than an integer
def category_id=(val)
if val.nil? or val.blank?
super(nil)
else
super(val.to_i)
end
end
# Don't want this to end up as anything other than an integer
def channel_id=(val)
if val.nil? or val.blank?
super(nil)
else
super(val.to_i)
# These are only allowed to be integers
%w(channel_id category_id group_id).each do |name|
define_method "#{name}=" do |val|
if val.nil? or val.blank?
super(nil)
else
super(val.to_i)
end
end
end

View File

@ -0,0 +1,3 @@
export default Ember.Component.extend({
classNames: ['channel-info'],
});

View File

@ -26,8 +26,11 @@ export default Ember.Component.extend({
},
createRule(channel){
var newRule = this.get('store').createRecord('rule',{channel_id: channel.id});
channel.rules.pushObject(newRule)
this.sendAction('createRule', channel)
},
editRule(rule){
this.sendAction('editRule', rule, this.get('channel'))
},
showError(error_key){

View File

@ -2,37 +2,21 @@ import { popupAjaxError } from 'discourse/lib/ajax-error';
export default Ember.Component.extend({
tagName: 'tr',
editing: false,
autoEdit: function(){
if(!this.get('rule').id){
this.set('editing', true);
}
}.on('init'),
actions: {
edit: function(){
this.set('editing', true);
this.sendAction('edit', this.get('rule'))
},
cancel: function(){
this.send('refresh');
},
save: function(){
this.get('rule').save().then(result => {
this.send('refresh');
}).catch(popupAjaxError);
},
delete(rule){
rule.destroyRecord().then(() => {
this.send('refresh');
}).catch(popupAjaxError)
},
refresh: function(){
this.sendAction('refresh');
}
}
});

View File

@ -35,6 +35,17 @@ export default Ember.Controller.extend({
showModal('admin-plugins-chat-test', { model: model, admin: true });
},
createRule(channel){
this.set('modalShowing', true);
var model = {rule: this.store.createRecord('rule',{channel_id: channel.id}), channel:channel, provider:this.get('model.provider')};
showModal('admin-plugins-chat-edit-rule', { model: model, admin: true });
},
editRule(rule, channel){
this.set('modalShowing', true);
var model = {rule: rule, channel:channel, provider:this.get('model.provider')};
showModal('admin-plugins-chat-edit-rule', { model: model, admin: true });
},
}
});

View File

@ -0,0 +1,42 @@
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, {
setupKeydown: function() {
Ember.run.schedule('afterRender', () => {
$('#chat_integration_edit_channel_modal').keydown(e => {
if (e.keyCode === 13) {
this.send('save');
}
});
});
}.on('init'),
saveDisabled: function(){
return false;
}.property(),
actions: {
cancel: function(){
this.send('closeModal');
},
save: function(){
if(this.get('saveDisabled')){return};
const self = this;
this.get('model.rule').save().then(function(result) {
self.send('closeModal');
}).catch(function(error) {
self.flash(extractError(error), 'error');
});
}
}
});

View File

@ -30,12 +30,12 @@ export default RestModel.extend({
},
updateProperties() {
var prop_names = ['category_id','tags','filter'];
var prop_names = ['category_id','group_id','tags','filter'];
return this.getProperties(prop_names);
},
createProperties() {
var prop_names = ['channel_id', 'category_id','tags','filter'];
var prop_names = ['channel_id', 'category_id','group_id','tags','filter'];
return this.getProperties(prop_names);
}

View File

@ -0,0 +1,81 @@
{{#d-modal-body id="chat_integration_edit_rule_modal" title="chat_integration.edit_rule_modal.title"}}
<div>
<form {{action "save" on="submit"}}>
<table>
<tr class="input">
<td class="label"><label for='provider'>{{i18n "chat_integration.edit_rule_modal.provider"}}</label></td>
<td>
{{i18n (concat 'chat_integration.provider.' model.channel.provider '.title')}}
</td>
</tr>
<tr class="instructions">
<td></td>
<td></td>
</tr>
<tr class="input">
<td class="label"><label for='channel'>{{i18n "chat_integration.edit_rule_modal.channel"}}</label></td>
<td>
{{channel-data provider=model.provider channel=model.channel}}
</td>
</tr>
<tr class="instructions">
<td></td>
<td></td>
</tr>
<tr class="input">
<td class="label"><label for='filter'>{{i18n "chat_integration.edit_rule_modal.filter"}}</label></td>
<td>
{{combo-box name="filter" content=model.rule.available_filters value=model.rule.filter}}
</td>
</tr>
<tr class="instructions">
<td></td>
<td><label>{{i18n 'chat_integration.edit_rule_modal.instructions.filter'}}</label></td>
</tr>
<tr class="input">
<td class="label"><label for='category'>{{i18n "chat_integration.edit_rule_modal.category"}}</label></td>
<td>
{{category-chooser
name="category"
value=model.rule.category_id
rootNoneLabel="chat_integration.all_categories"
rootNone=true
overrideWidths=false
}}
</td>
</tr>
<tr class="instructions">
<td></td>
<td><label>{{i18n 'chat_integration.edit_rule_modal.instructions.category'}}</label></td>
</tr>
{{#if siteSettings.tagging_enabled}}
<tr class="input">
<td class="label"><label for='tags'>{{i18n "chat_integration.edit_rule_modal.tags"}}</label></td>
<td>
{{tag-chooser placeholderKey="chat_integration.all_tags" name="tags" tags=model.rule.tags}}
</td>
</tr>
<tr class="instructions">
<td></td>
<td><label>{{i18n 'chat_integration.edit_rule_modal.instructions.tags'}}</label></td>
</tr>
{{/if}}
</table>
</form>
</div>
{{/d-modal-body}}
<div class="modal-footer">
{{d-button id="save_channel" class='btn-primary btn-large' action="save" title="chat_integration.edit_rule_modal.save" label="chat_integration.edit_rule_modal.save" disabled=saveDisabled}}
{{d-button class="btn-large" action="cancel" title="chat_integration.edit_rule_modal.cancel" label="chat_integration.edit_rule_modal.cancel"}}
</div>

View File

@ -14,6 +14,8 @@
refresh='refresh'
edit='editChannel'
test='testChannel'
createRule='createRule'
editRule='editRule'
}}
{{/each}}

View File

@ -0,0 +1,7 @@
{{# each provider.channel_parameters as |param|}}
{{#if param.hidden}}{{else}}
<span class='field-name'>{{i18n (concat 'chat_integration.provider.' channel.provider '.param.' param.key '.title')}}:</span>
<span class='field-value'>{{get channel.data param.key}}</span>
<br/>
{{/if}}
{{/each}}

View File

@ -12,14 +12,7 @@
{{/if}}
{{# each provider.channel_parameters as |param|}}
{{#if param.hidden}}{{else}}
<span class='field-name'>{{i18n (concat 'chat_integration.provider.' channel.provider '.param.' param.key '.title')}}:</span>
<span class='field-value'>{{get channel.data param.key}}</span>
<br/>
{{/if}}
{{/each}}
{{channel-data provider=provider channel=channel}}
</span>
@ -43,7 +36,7 @@
{{#each channel.rules as |rule|}}
{{rule-row rule=rule refresh=refresh}}
{{rule-row rule=rule edit='editRule' refresh='refresh'}}
{{/each}}

View File

@ -1,53 +1,31 @@
<td>
{{#if editing}}
{{combo-box name="filter" content=rule.available_filters value=rule.filter}}
{{else}}
{{rule.filterName}}
{{/if}}
{{rule.filterName}}
</td>
<td>
{{#if editing}}
{{category-chooser
name="category"
value=rule.category_id
rootNoneLabel="chat_integration.all_categories"
rootNone=true
overrideWidths=false
}}
{{#if rule.category}}
{{category-link rule.category allowUncategorized="true" link="false"}}
{{else if rule.group_id}}
{{i18n "chat_integration.group_prefix"}} {{rule.group_name}}
{{else}}
{{#if rule.category}}
{{category-link rule.category allowUncategorized="true" link="false"}}
{{else}}
{{i18n "chat_integration.all_categories"}}
{{/if}}
{{i18n "chat_integration.all_categories"}}
{{/if}}
</td>
{{#if siteSettings.tagging_enabled}}
<td>
{{#if editing}}
{{tag-chooser placeholderKey="chat_integration.all_tags" name="tags" tags=rule.tags}}
{{#if rule.tags}}
{{rule.tags}}
{{else}}
{{#if rule.tags}}
{{rule.tags}}
{{else}}
{{i18n "chat_integration.all_tags"}}
{{/if}}
{{i18n "chat_integration.all_tags"}}
{{/if}}
</td>
{{/if}}
<td>
{{#if editing}}
{{d-button action="save" actionParam=rule icon="check" class="ok" title="chat_integration.rule_table.save_rule"}}
{{d-button action="cancel" actionParam=rule icon="times" class="cancel" title="chat_integration.rule_table.cancel_edit"}}
{{else}}
{{d-button action="edit" actionParam=rule icon="pencil" class="edit" title="chat_integration.rule_table.edit_rule"}}
{{d-button action="delete" actionParam=rule icon="trash-o" class="delete" title="chat_integration.rule_table.delete_rule"}}
{{/if}}
{{d-button action="edit" actionParam=rule icon="pencil" class="edit" title="chat_integration.rule_table.edit_rule"}}
{{d-button action="delete" actionParam=rule icon="trash-o" class="delete" title="chat_integration.rule_table.delete_rule"}}
</td>

View File

@ -53,7 +53,7 @@
}
#chat_integration_edit_channel_modal, #chat_integration_test_modal{
#chat_integration_edit_channel_modal, #chat_integration_test_modal, #chat_integration_edit_rule_modal{
table{
width:100%;
@ -90,5 +90,8 @@
margin-top: 0px;
}
.field-name{
font-weight: bold;
}
}

View File

@ -6,6 +6,7 @@ en:
no_providers: "You need to enable some providers in the plugin settings"
channels_with_errors: "Some channels for this provider failed last time messages were sent. Click the error icon(s) to learn more."
channel_exception: "An unknown error occured when a message was last sent to this channel. Check the site logs for more information."
group_prefix: "Group:"
all_categories: "(all categories)"
all_tags: "(all tags)"
create_rule: "Create Rule"
@ -39,6 +40,19 @@ en:
channel_validation:
ok: "Valid"
fail: "Invalid format"
edit_rule_modal:
title: Edit Rule
save: Save Rule
cancel: Cancel
provider: Provider
channel: Channel
filter: Filter
category: Category
tags: Tags
instructions:
filter: "Notification level. Mute overrides other matching rules."
category: "This rule will only apply to topics in the specified category."
tags: "If specified, this rule will only apply to topics which have at least one of these tags."
provider: