From d437634f6184a7d5edb970bc7099a5f5691d7ef3 Mon Sep 17 00:00:00 2001 From: David Taylor Date: Mon, 31 Jul 2017 17:09:21 +0100 Subject: [PATCH] Move rule editing into a modal dialog --- app/models/rule.rb | 23 ++---- .../admin/components/channel-data.js.es6 | 3 + .../admin/components/channel-details.js.es6 | 7 +- .../admin/components/rule-row.js.es6 | 24 +----- .../admin-plugins-chat-provider.js.es6 | 11 +++ .../admin-plugins-chat-edit-rule.js.es6 | 42 ++++++++++ assets/javascripts/admin/models/rule.js.es6 | 4 +- .../modal/admin-plugins-chat-edit-rule.hbs | 81 +++++++++++++++++++ .../templates/admin/plugins-chat-provider.hbs | 2 + .../templates/components/channel-data.hbs | 7 ++ .../templates/components/channel-details.hbs | 11 +-- .../templates/components/rule-row.hbs | 46 +++-------- .../stylesheets/chat-integration-admin.scss | 5 +- config/locales/client.en.yml | 14 ++++ 14 files changed, 197 insertions(+), 83 deletions(-) create mode 100644 assets/javascripts/admin/components/channel-data.js.es6 create mode 100644 assets/javascripts/admin/controllers/modals/admin-plugins-chat-edit-rule.js.es6 create mode 100644 assets/javascripts/admin/templates/modal/admin-plugins-chat-edit-rule.hbs create mode 100644 assets/javascripts/discourse/templates/components/channel-data.hbs diff --git a/app/models/rule.rb b/app/models/rule.rb index e485deb..44c0125 100644 --- a/app/models/rule.rb +++ b/app/models/rule.rb @@ -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 diff --git a/assets/javascripts/admin/components/channel-data.js.es6 b/assets/javascripts/admin/components/channel-data.js.es6 new file mode 100644 index 0000000..259fb6c --- /dev/null +++ b/assets/javascripts/admin/components/channel-data.js.es6 @@ -0,0 +1,3 @@ +export default Ember.Component.extend({ + classNames: ['channel-info'], +}); \ No newline at end of file diff --git a/assets/javascripts/admin/components/channel-details.js.es6 b/assets/javascripts/admin/components/channel-details.js.es6 index 08fcd1b..3b9195c 100644 --- a/assets/javascripts/admin/components/channel-details.js.es6 +++ b/assets/javascripts/admin/components/channel-details.js.es6 @@ -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){ diff --git a/assets/javascripts/admin/components/rule-row.js.es6 b/assets/javascripts/admin/components/rule-row.js.es6 index 7622fa1..9782507 100644 --- a/assets/javascripts/admin/components/rule-row.js.es6 +++ b/assets/javascripts/admin/components/rule-row.js.es6 @@ -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'); } + + + } }); \ No newline at end of file diff --git a/assets/javascripts/admin/controllers/admin-plugins-chat-provider.js.es6 b/assets/javascripts/admin/controllers/admin-plugins-chat-provider.js.es6 index f52965c..c3f90e6 100644 --- a/assets/javascripts/admin/controllers/admin-plugins-chat-provider.js.es6 +++ b/assets/javascripts/admin/controllers/admin-plugins-chat-provider.js.es6 @@ -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 }); + }, + } }); \ No newline at end of file diff --git a/assets/javascripts/admin/controllers/modals/admin-plugins-chat-edit-rule.js.es6 b/assets/javascripts/admin/controllers/modals/admin-plugins-chat-edit-rule.js.es6 new file mode 100644 index 0000000..220d525 --- /dev/null +++ b/assets/javascripts/admin/controllers/modals/admin-plugins-chat-edit-rule.js.es6 @@ -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'); + }); + + } + } +}); \ No newline at end of file diff --git a/assets/javascripts/admin/models/rule.js.es6 b/assets/javascripts/admin/models/rule.js.es6 index 44fe2c8..f225d45 100644 --- a/assets/javascripts/admin/models/rule.js.es6 +++ b/assets/javascripts/admin/models/rule.js.es6 @@ -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); } diff --git a/assets/javascripts/admin/templates/modal/admin-plugins-chat-edit-rule.hbs b/assets/javascripts/admin/templates/modal/admin-plugins-chat-edit-rule.hbs new file mode 100644 index 0000000..d7e4c68 --- /dev/null +++ b/assets/javascripts/admin/templates/modal/admin-plugins-chat-edit-rule.hbs @@ -0,0 +1,81 @@ +{{#d-modal-body id="chat_integration_edit_rule_modal" title="chat_integration.edit_rule_modal.title"}} +
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + {{#if siteSettings.tagging_enabled}} + + + + + + + + + {{/if}} + +
+ {{i18n (concat 'chat_integration.provider.' model.channel.provider '.title')}} +
+ {{channel-data provider=model.provider channel=model.channel}} +
+ {{combo-box name="filter" content=model.rule.available_filters value=model.rule.filter}} +
+ {{category-chooser + name="category" + value=model.rule.category_id + rootNoneLabel="chat_integration.all_categories" + rootNone=true + overrideWidths=false + }} +
+ {{tag-chooser placeholderKey="chat_integration.all_tags" name="tags" tags=model.rule.tags}} +
+ +
+
+{{/d-modal-body}} + + \ No newline at end of file diff --git a/assets/javascripts/discourse/templates/admin/plugins-chat-provider.hbs b/assets/javascripts/discourse/templates/admin/plugins-chat-provider.hbs index e4b2946..ee1d6bc 100644 --- a/assets/javascripts/discourse/templates/admin/plugins-chat-provider.hbs +++ b/assets/javascripts/discourse/templates/admin/plugins-chat-provider.hbs @@ -14,6 +14,8 @@ refresh='refresh' edit='editChannel' test='testChannel' + createRule='createRule' + editRule='editRule' }} {{/each}} diff --git a/assets/javascripts/discourse/templates/components/channel-data.hbs b/assets/javascripts/discourse/templates/components/channel-data.hbs new file mode 100644 index 0000000..bdcdfbc --- /dev/null +++ b/assets/javascripts/discourse/templates/components/channel-data.hbs @@ -0,0 +1,7 @@ +{{# each provider.channel_parameters as |param|}} + {{#if param.hidden}}{{else}} + {{i18n (concat 'chat_integration.provider.' channel.provider '.param.' param.key '.title')}}: + {{get channel.data param.key}} +
+ {{/if}} +{{/each}} \ No newline at end of file diff --git a/assets/javascripts/discourse/templates/components/channel-details.hbs b/assets/javascripts/discourse/templates/components/channel-details.hbs index 83412ff..b50b5a9 100644 --- a/assets/javascripts/discourse/templates/components/channel-details.hbs +++ b/assets/javascripts/discourse/templates/components/channel-details.hbs @@ -12,14 +12,7 @@ {{/if}} - {{# each provider.channel_parameters as |param|}} - - {{#if param.hidden}}{{else}} - {{i18n (concat 'chat_integration.provider.' channel.provider '.param.' param.key '.title')}}: - {{get channel.data param.key}} -
- {{/if}} - {{/each}} + {{channel-data provider=provider channel=channel}} @@ -43,7 +36,7 @@ {{#each channel.rules as |rule|}} - {{rule-row rule=rule refresh=refresh}} + {{rule-row rule=rule edit='editRule' refresh='refresh'}} {{/each}} diff --git a/assets/javascripts/discourse/templates/components/rule-row.hbs b/assets/javascripts/discourse/templates/components/rule-row.hbs index 9af5d24..59efe10 100644 --- a/assets/javascripts/discourse/templates/components/rule-row.hbs +++ b/assets/javascripts/discourse/templates/components/rule-row.hbs @@ -1,53 +1,31 @@ - {{#if editing}} - {{combo-box name="filter" content=rule.available_filters value=rule.filter}} - {{else}} - {{rule.filterName}} - {{/if}} + {{rule.filterName}} - - {{#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}} {{#if siteSettings.tagging_enabled}} - {{#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}} {{/if}} - - {{#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"}} + diff --git a/assets/stylesheets/chat-integration-admin.scss b/assets/stylesheets/chat-integration-admin.scss index 7407cde..fda4780 100644 --- a/assets/stylesheets/chat-integration-admin.scss +++ b/assets/stylesheets/chat-integration-admin.scss @@ -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; + } } diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index 0094d9a..fdda3a9 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -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: