From a09376e64597811b423218ba4f97f7d5a37b3fa6 Mon Sep 17 00:00:00 2001 From: Angus McLeod Date: Mon, 30 Oct 2017 14:24:51 +0800 Subject: [PATCH] various --- .../components/wizard-custom-action.js.es6 | 33 +++++---- .../components/wizard-custom-field.js.es6 | 6 +- .../components/wizard-custom-input.js.es6 | 4 +- .../discourse/controllers/admin-wizard.js.es6 | 5 +- .../discourse/helpers/custom-wizard.js.es6 | 1 + .../discourse/models/custom-wizard.js.es6 | 68 +++++++++++++++---- .../routes/admin-wizard-submissions.js.es6 | 20 +++++- .../templates/admin-wizard-submissions.hbs | 12 ++-- .../discourse/templates/admin-wizard.hbs | 2 +- .../components/wizard-custom-action.hbs | 25 ++++--- .../components/wizard-custom-field.hbs | 27 +++++--- .../components/wizard-custom-input.hbs | 14 ++-- .../components/wizard-custom-step.hbs | 6 +- .../templates/components/wizard-links.hbs | 2 +- assets/stylesheets/wizard_custom_admin.scss | 38 +++++++++-- config/locales/client.en.yml | 19 +++--- controllers/admin.rb | 50 +++++++++++++- lib/builder.rb | 20 +++--- lib/field.rb | 2 +- 19 files changed, 259 insertions(+), 95 deletions(-) diff --git a/assets/javascripts/discourse/components/wizard-custom-action.js.es6 b/assets/javascripts/discourse/components/wizard-custom-action.js.es6 index 146a24d4..ce21fdaa 100644 --- a/assets/javascripts/discourse/components/wizard-custom-action.js.es6 +++ b/assets/javascripts/discourse/components/wizard-custom-action.js.es6 @@ -1,9 +1,9 @@ import { default as computed } from 'ember-addons/ember-computed-decorators'; const ACTION_TYPES = [ - { id: 'create_topic', name: 'create_topic *' }, - { id: 'update_profile', name: 'update_profile *' }, - { id: 'send_message', name: 'send_message *' } + { id: 'create_topic', name: 'Create Topic' }, + { id: 'update_profile', name: 'Update Profile' }, + { id: 'send_message', name: 'Send Message' } ]; const PROFILE_FIELDS = [ @@ -18,7 +18,6 @@ const PROFILE_FIELDS = [ 'bio_raw', 'location', 'website', - 'dismissed_banner_key', 'profile_background', 'card_background' ]; @@ -32,18 +31,28 @@ export default Ember.Component.extend({ sendMessage: Ember.computed.equal('action.type', 'send_message'), disableId: Ember.computed.not('action.isNew'), - @computed('steps') - wizardFields(steps) { + @computed('currentStepId', 'wizard.save_submissions') + availableFields(currentStepId, saveSubmissions) { + const allSteps = this.get('wizard.steps'); + let steps = allSteps; let fields = []; + + if (!saveSubmissions) { + steps = [allSteps.findBy('id', currentStepId)]; + } + steps.forEach((s) => { - let stepFields = s.fields.map((f) => { - return Ember.Object.create({ - id: f.id, - label: `${f.id} (${s.id})` + if (s.fields && s.fields.length > 0) { + let stepFields = s.fields.map((f) => { + return Ember.Object.create({ + id: f.id, + label: `${f.id} (${s.id})` + }); }); - }); - fields.push(...stepFields); + fields.push(...stepFields); + } }); + return fields; } }); diff --git a/assets/javascripts/discourse/components/wizard-custom-field.js.es6 b/assets/javascripts/discourse/components/wizard-custom-field.js.es6 index 8b73ce4b..74d5b848 100644 --- a/assets/javascripts/discourse/components/wizard-custom-field.js.es6 +++ b/assets/javascripts/discourse/components/wizard-custom-field.js.es6 @@ -4,6 +4,10 @@ export default Ember.Component.extend({ classNames: 'wizard-custom-field', isDropdown: Ember.computed.equal('field.type', 'dropdown'), disableId: Ember.computed.not('field.isNew'), + choicesTypes: ['translation', 'preset', 'custom'], + choicesTranslation: Ember.computed.equal('choicesType', 'translation'), + choicesPreset: Ember.computed.equal('choicesType', 'preset'), + choicesCustom: Ember.computed.equal('choicesType', 'custom'), @computed('field.type') isInput: (type) => type === 'text' || type === 'textarea', @@ -13,5 +17,5 @@ export default Ember.Component.extend({ return [ { id: 'categories', name: I18n.t('admin.wizard.field.choices_preset.categories') } ]; - } + }, }); diff --git a/assets/javascripts/discourse/components/wizard-custom-input.js.es6 b/assets/javascripts/discourse/components/wizard-custom-input.js.es6 index c4f89e92..43828ef7 100644 --- a/assets/javascripts/discourse/components/wizard-custom-input.js.es6 +++ b/assets/javascripts/discourse/components/wizard-custom-input.js.es6 @@ -1,6 +1,8 @@ export default Ember.Component.extend({ - noneKey: 'admin.wizard.none', + noneKey: 'admin.wizard.select_field', noneValue: 'admin.wizard.none', + inputKey: 'admin.wizard.key', + inputValue: 'admin.wizard.value', actions: { add() { diff --git a/assets/javascripts/discourse/controllers/admin-wizard.js.es6 b/assets/javascripts/discourse/controllers/admin-wizard.js.es6 index a626f82e..fd1bc147 100644 --- a/assets/javascripts/discourse/controllers/admin-wizard.js.es6 +++ b/assets/javascripts/discourse/controllers/admin-wizard.js.es6 @@ -20,9 +20,10 @@ export default Ember.Controller.extend({ } else { this.send("refreshWizard"); } - }).catch((error) => { + }).catch((result) => { + console.log(result) this.set('saving', false); - this.set('error', I18n.t(`admin.wizard.error.${error}`)); + this.set('error', I18n.t(`admin.wizard.error.${result.error}`)); Ember.run.later(() => this.set('error', null), 10000); }); }, diff --git a/assets/javascripts/discourse/helpers/custom-wizard.js.es6 b/assets/javascripts/discourse/helpers/custom-wizard.js.es6 index c761d9a0..7bc4100e 100644 --- a/assets/javascripts/discourse/helpers/custom-wizard.js.es6 +++ b/assets/javascripts/discourse/helpers/custom-wizard.js.es6 @@ -1,5 +1,6 @@ import { registerUnbound } from 'discourse-common/lib/helpers'; registerUnbound('dasherize', function(string) { + console.log(string) return Ember.String.dasherize(string); }); diff --git a/assets/javascripts/discourse/models/custom-wizard.js.es6 b/assets/javascripts/discourse/models/custom-wizard.js.es6 index 77971cd6..f0561df2 100644 --- a/assets/javascripts/discourse/models/custom-wizard.js.es6 +++ b/assets/javascripts/discourse/models/custom-wizard.js.es6 @@ -4,12 +4,24 @@ const CustomWizard = Discourse.Model.extend({ save() { return new Ember.RSVP.Promise((resolve, reject) => { const id = this.get('id'); - if (!id || !id.underscore()) reject('id_required'); + if (!id || !id.underscore()) return reject({ error: 'id_required' }); let wizard = { id: id.underscore() }; const steps = this.get('steps'); - if (steps.length) wizard['steps'] = this.buildSteps(steps, reject); + if (steps.length > 0) { + const stepsResult = this.buildSteps(steps); + console.log(stepsResult) + if (stepsResult.error) { + reject({ error: stepsResult.error }) + } else { + wizard['steps'] = stepsResult; + } + } + + if (steps.length < 1 || !wizard['steps'] || wizard['steps'].length < 1) { + return reject({ error: 'steps_required' }); + } const name = this.get('name'); if (name) wizard['name'] = name; @@ -28,15 +40,26 @@ const CustomWizard = Discourse.Model.extend({ data: { wizard: JSON.stringify(wizard) } - }).then((result) => resolve(result)); + }).then((result) => { + console.log(result) + if (result.error) { + reject(result); + } else { + resolve(result); + } + }); }); }, - buildSteps(stepsObj, reject) { + buildSteps(stepsObj) { let steps = []; + let error = null; stepsObj.some((s) => { - if (!s.id || !s.id.underscore()) reject('id_required'); + if (!s.id || !s.id.underscore()) { + error = 'id_required'; + return; + }; let step = { id: s.id.underscore() }; @@ -50,22 +73,31 @@ const CustomWizard = Discourse.Model.extend({ step['fields'] = []; fields.some((f) => { - let id = f.get('id'); + let id = f.id; - if (!id || !id.underscore()) reject('id_required'); + if (!id || !id.underscore()) { + error = 'id_required'; + return; + } f.set('id', id.underscore()); - if (f.get('type') === 'dropdown') { - const choices = f.get('choices'); - if (choices && choices.length < 1 && !f.get('choices_key') && !f.get('choices_categories')) { - reject('field.need_choices'); - } + if (f.label === '') delete f.label; + if (f.description === '') delete f.description; + + if (f.type === 'dropdown') { + const choices = f.choices; + //if ((!choices || choices.length < 1) && !f.choices_key && !f.choices_categories) { + //error = 'field.need_choices'; + //return; + //} } delete f.isNew; step['fields'].push(f); }); + + if (error) return; } const actions = s.actions; @@ -74,7 +106,10 @@ const CustomWizard = Discourse.Model.extend({ actions.some((a) => { let id = a.get('id'); - if (!id || !id.underscore()) reject('id_required'); + if (!id || !id.underscore()) { + error = 'id_required'; + return; + } a.set('id', id.underscore()); @@ -83,12 +118,17 @@ const CustomWizard = Discourse.Model.extend({ step['actions'].push(a); }); + if (error) return; } steps.push(step); }); - return steps; + if (error) { + return { error }; + } else { + return { steps }; + }; }, remove() { diff --git a/assets/javascripts/discourse/routes/admin-wizard-submissions.js.es6 b/assets/javascripts/discourse/routes/admin-wizard-submissions.js.es6 index 824606ec..3650bc1c 100644 --- a/assets/javascripts/discourse/routes/admin-wizard-submissions.js.es6 +++ b/assets/javascripts/discourse/routes/admin-wizard-submissions.js.es6 @@ -2,10 +2,26 @@ import CustomWizard from '../models/custom-wizard'; export default Discourse.Route.extend({ model(params) { - return CustomWizard.submissions(params.wizard_id); + return Ember.RSVP.hash({ + submissions: CustomWizard.submissions(params.wizard_id), + wizard: this.modelFor('admin-wizards-submissions').findBy('id', params.wizard_id) + }); }, setupController(controller, model) { - controller.set("model", model); + let fields = ['user_id', 'completed']; + + model.wizard.steps.forEach((s) => { + if (s.fields) { + s.fields.forEach((f) => { + fields.push(f.id); + }); + }; + }); + + controller.setProperties({ + submissions: model.submissions, + fields + }); } }); diff --git a/assets/javascripts/discourse/templates/admin-wizard-submissions.hbs b/assets/javascripts/discourse/templates/admin-wizard-submissions.hbs index 1effd84f..ec8a55ff 100644 --- a/assets/javascripts/discourse/templates/admin-wizard-submissions.hbs +++ b/assets/javascripts/discourse/templates/admin-wizard-submissions.hbs @@ -1,11 +1,11 @@
- {{#each model.submissions as |s|}} - - {{#each-in s as |k v|}} - - {{/each-in}} - + + {{#each fields as |f|}} + + {{/each}} + + {{#each submissions as |s|}} {{#each-in s as |k v|}} diff --git a/assets/javascripts/discourse/templates/admin-wizard.hbs b/assets/javascripts/discourse/templates/admin-wizard.hbs index 71ea7641..555a32a7 100644 --- a/assets/javascripts/discourse/templates/admin-wizard.hbs +++ b/assets/javascripts/discourse/templates/admin-wizard.hbs @@ -60,7 +60,7 @@ {{wizard-links type="step" current=currentStep items=model.steps}} {{#if currentStep}} - {{wizard-custom-step step=currentStep steps=model.steps fieldTypes=model.fieldTypes}} + {{wizard-custom-step step=currentStep wizard=model}} {{/if}}
diff --git a/assets/javascripts/discourse/templates/components/wizard-custom-action.hbs b/assets/javascripts/discourse/templates/components/wizard-custom-action.hbs index d8d2140c..29e0b2f5 100644 --- a/assets/javascripts/discourse/templates/components/wizard-custom-action.hbs +++ b/assets/javascripts/discourse/templates/components/wizard-custom-action.hbs @@ -13,7 +13,6 @@
{{combo-box value=action.type content=types}} -
@@ -23,7 +22,7 @@

{{i18n "admin.wizard.action.create_topic.category"}}

- {{category-select-box value=action.category_id}} + {{category-chooser value=action.category_id}}
@@ -32,7 +31,7 @@

{{i18n "admin.wizard.action.title"}}

- {{combo-box value=action.title content=wizardFields nameProperty="label" none='admin.wizard.action.none'}} + {{combo-box value=action.title content=availableFields nameProperty="label" none='admin.wizard.select_field'}}
@@ -41,13 +40,16 @@

{{i18n "admin.wizard.action.post"}}

- {{combo-box value=action.post content=wizardFields nameProperty="label" none='admin.wizard.action.none'}} + {{combo-box value=action.post content=availableFields nameProperty="label" none='admin.wizard.select_field'}}
- {{wizard-custom-input inputs=action.add_fields valueContent=wizardFields noneValue='admin.wizard.action.none'}} + {{wizard-custom-input inputs=action.add_fields + valueContent=availableFields + inputKey='admin.wizard.action.topic_attr' + noneValue='admin.wizard.select_field'}}
{{/if}} @@ -57,7 +59,7 @@

{{i18n "admin.wizard.action.title"}}

- {{combo-box value=action.title content=wizardFields nameProperty='label' none='admin.wizard.action.none'}} + {{combo-box value=action.title content=availableFields nameProperty='label' none='admin.wizard.select_field'}}
@@ -66,7 +68,7 @@

{{i18n "admin.wizard.action.post"}}

- {{combo-box value=action.post content=wizardFields nameProperty='label' none='admin.wizard.action.none'}} + {{combo-box value=action.post content=availableFields nameProperty='label' none='admin.wizard.select_field'}}
@@ -84,7 +86,9 @@
- {{wizard-custom-input inputs=action.add_fields valueContent=wizardFields noneValue='admin.wizard.action.none'}} + {{wizard-custom-input inputs=action.add_fields + keyContent=availableFields + inputValue='admin.wizard.action.topic_attr'}}
{{/if}} @@ -93,8 +97,7 @@ {{wizard-custom-input inputs=action.profile_updates valueContent=profileFields - keyContent=wizardFields - noneKey='admin.wizard.action.none' - noneValue='admin.wizard.action.none'}} + keyContent=availableFields + noneValue='admin.wizard.action.update_profile.profile_field'}} {{/if}} diff --git a/assets/javascripts/discourse/templates/components/wizard-custom-field.hbs b/assets/javascripts/discourse/templates/components/wizard-custom-field.hbs index a3079644..fa59262c 100644 --- a/assets/javascripts/discourse/templates/components/wizard-custom-field.hbs +++ b/assets/javascripts/discourse/templates/components/wizard-custom-field.hbs @@ -66,30 +66,35 @@ {{#if isDropdown}}
-
+
{{i18n 'admin.wizard.field.choices_label'}}
-
+ + {{combo-box value=choicesType content=choicesTypes none="admin.wizard.field.choices_type"}} + + {{#if choicesTranslation}}
{{i18n 'admin.wizard.field.choices_translation'}}
-
- {{input name="key" value=field.choices_key placeholderKey="admin.wizard.key_placeholder"}} -
-
-
+ {{input name="key" value=field.choices_key placeholderKey="admin.wizard.key_placeholder"}} + {{/if}} + + {{#if choicesPreset}}
{{i18n 'admin.wizard.field.choices_preset.label'}}
{{combo-box value=field.choices_preset content=presetChoices none='admin.wizard.none'}} - +
+ {{i18n 'admin.wizard.field.choices_preset.filter'}} +
{{wizard-custom-input inputs=field.choices_filters}} -
-
+ {{/if}} + + {{#if choicesCustom}}
{{i18n 'admin.wizard.field.choices_custom'}}
{{wizard-custom-input inputs=field.choices}} -
+ {{/if}}
{{/if}} diff --git a/assets/javascripts/discourse/templates/components/wizard-custom-input.hbs b/assets/javascripts/discourse/templates/components/wizard-custom-input.hbs index 5cce6e95..add8f9eb 100644 --- a/assets/javascripts/discourse/templates/components/wizard-custom-input.hbs +++ b/assets/javascripts/discourse/templates/components/wizard-custom-input.hbs @@ -1,16 +1,18 @@ {{#each inputs as |in|}} - +
{{#if keyContent}} {{combo-box value=in.key content=keyContent nameProperty="label" none=noneKey}} {{else}} - {{input type="text" value=in.key placeholder=(i18n 'admin.wizard.key')}} + {{input type="text" value=in.key placeholder=(i18n inputKey)}} {{/if}} {{#if valueContent}} {{combo-box value=in.value content=valueContent nameProperty="label" none=noneValue}} {{else}} - {{input type="text" value=in.value placeholder=(i18n 'admin.wizard.value')}} + {{input type="text" value=in.value placeholder=(i18n inputValue)}} {{/if}} - - {{d-button action='remove' actionParam=in icon='times'}} + {{d-button action='remove' actionParam=in icon='times'}} +
{{/each}} -
{{d-button action='add' label='admin.wizard.add' icon='plus'}}
+
+ {{d-button action='add' label='admin.wizard.add' icon='plus'}} +
diff --git a/assets/javascripts/discourse/templates/components/wizard-custom-step.hbs b/assets/javascripts/discourse/templates/components/wizard-custom-step.hbs index d9041685..43c0391e 100644 --- a/assets/javascripts/discourse/templates/components/wizard-custom-step.hbs +++ b/assets/javascripts/discourse/templates/components/wizard-custom-step.hbs @@ -45,10 +45,12 @@ {{wizard-links type="field" current=currentField items=step.fields}} {{#if currentField}} - {{wizard-custom-field field=currentField types=fieldTypes removeField="removeField"}} + {{wizard-custom-field field=currentField types=wizard.fieldTypes removeField="removeField"}} {{/if}} {{wizard-links type="action" current=currentAction items=step.actions}} {{#if currentAction}} - {{wizard-custom-action action=currentAction steps=steps removeAction="removeAction"}} + {{wizard-custom-action action=currentAction wizard=wizard removeAction="removeAction" currentStepId=step.id}} {{/if}} + + diff --git a/assets/javascripts/discourse/templates/components/wizard-links.hbs b/assets/javascripts/discourse/templates/components/wizard-links.hbs index f5f373d7..f81f1605 100644 --- a/assets/javascripts/discourse/templates/components/wizard-links.hbs +++ b/assets/javascripts/discourse/templates/components/wizard-links.hbs @@ -1,5 +1,5 @@
{{k}}
{{f}}
{{v}}