diff --git a/app/controllers/admin.rb b/app/controllers/admin.rb index f953d017..aacacacc 100644 --- a/app/controllers/admin.rb +++ b/app/controllers/admin.rb @@ -44,7 +44,7 @@ class CustomWizard::AdminController < ::ApplicationController def find_wizard params.require(:wizard_id) - wizard = PluginStore.get('custom_wizard', params[:wizard_id]) + wizard = PluginStore.get('custom_wizard', params[:wizard_id].underscore) render json: success_json.merge(wizard: wizard) end @@ -60,7 +60,7 @@ class CustomWizard::AdminController < ::ApplicationController def find_submissions params.require(:wizard_id) - wizard = PluginStore.get('custom_wizard_submissions', params[:wizard_id]) + wizard = PluginStore.get('custom_wizard_submissions', params[:wizard_id].underscore) render json: success_json.merge(submissions: submissions) end diff --git a/app/controllers/steps.rb b/app/controllers/steps.rb index 3a1fa06f..e795abf6 100644 --- a/app/controllers/steps.rb +++ b/app/controllers/steps.rb @@ -2,7 +2,7 @@ class CustomWizard::StepsController < ApplicationController before_action :ensure_logged_in def update - wizard = CustomWizard::Builder.new(current_user, params[:wizard_id]).build + wizard = CustomWizard::Builder.new(current_user, params[:wizard_id].underscore).build updater = wizard.create_updater(params[:step_id], params[:fields]) updater.update diff --git a/app/controllers/wizard.rb b/app/controllers/wizard.rb index a66b5de8..a37b54d1 100644 --- a/app/controllers/wizard.rb +++ b/app/controllers/wizard.rb @@ -6,7 +6,7 @@ class CustomWizard::WizardController < ::ApplicationController def index respond_to do |format| format.json do - wizard = CustomWizard::Builder.new(current_user, params[:wizard_id]).build + wizard = CustomWizard::Builder.new(current_user, params[:wizard_id].underscore).build render_serialized(wizard, WizardSerializer) end format.html {} diff --git a/assets/javascripts/discourse/components/wizard-custom-field.js.es6 b/assets/javascripts/discourse/components/wizard-custom-field.js.es6 index fc3698b1..e308b804 100644 --- a/assets/javascripts/discourse/components/wizard-custom-field.js.es6 +++ b/assets/javascripts/discourse/components/wizard-custom-field.js.es6 @@ -8,7 +8,6 @@ export default Ember.Component.extend({ @observes('field.id') init() { this._super(...arguments); - if (!this.get('field.choices')) { this.set('field.choices', Ember.A()); } diff --git a/assets/javascripts/discourse/components/wizard-custom-step.js.es6 b/assets/javascripts/discourse/components/wizard-custom-step.js.es6 index 7127575c..76a86bd9 100644 --- a/assets/javascripts/discourse/components/wizard-custom-step.js.es6 +++ b/assets/javascripts/discourse/components/wizard-custom-step.js.es6 @@ -6,23 +6,25 @@ export default Ember.Component.extend({ currentAction: null, @on('init') - @observes('step.id') + @observes('step') setup() { this._super(...arguments); - const fields = this.get('step.fields'); - const actions = this.get('step.actions'); + const fields = this.get('step.fields') || []; + const actions = this.get('step.actions') || []; this.set('currentField', fields[0]); this.set('currentAction', actions[0]); }, @computed('step.fields.[]', 'currentField') fieldLinks(fields, current) { + if (!fields) return; + return fields.map((f) => { if (f) { - let link = { - id: f.get('id'), - label: f.get('label') - }; + const id = f.get('id'); + const label = f.get('label'); + + let link = { id, label: label || id }; let classes = 'btn'; if (current && f.get('id') === current.get('id')) { @@ -38,12 +40,14 @@ export default Ember.Component.extend({ @computed('step.actions.[]', 'currentAction') actionLinks(actions, current) { + if (!actions) return; + return actions.map((a) => { if (a) { - let link = { - id: a.get('id'), - label: a.get('label') - }; + const id = a.get('id'); + const label = a.get('label'); + + let link = { id, label: label || id }; let classes = 'btn'; if (current && a.get('id') === current.get('id')) { @@ -62,7 +66,7 @@ export default Ember.Component.extend({ const fields = this.get('step.fields'); const newNum = fields.length + 1; const field = Ember.Object.create({ - id: `field-${newNum}`, label: `Field ${newNum}` + id: `field-${newNum}` }); fields.pushObject(field); this.set('currentField', field); @@ -72,7 +76,7 @@ export default Ember.Component.extend({ const actions = this.get('step.actions'); const newNum = actions.length + 1; const action = Ember.Object.create({ - id: `action-${newNum}`, label: `Action ${newNum}` + id: `action-${newNum}` }); actions.pushObject(action); this.set('currentAction', action); diff --git a/assets/javascripts/discourse/controllers/admin-wizard.js.es6 b/assets/javascripts/discourse/controllers/admin-wizard.js.es6 index 7c343bde..1130d6c8 100644 --- a/assets/javascripts/discourse/controllers/admin-wizard.js.es6 +++ b/assets/javascripts/discourse/controllers/admin-wizard.js.es6 @@ -6,13 +6,13 @@ export default Ember.Controller.extend({ stepLinks(steps, currentStep) { return steps.map((s) => { if (s) { - let link = { - id: s.get('id'), - title: s.get('title') - }; + const id = s.get('id'); + const title = s.get('title'); + + let link = { id, title: title || id }; let classes = 'btn'; - if (currentStep && s.get('id') === currentStep.get('id')) { + if (currentStep && id === currentStep.get('id')) { classes += ' btn-primary'; }; @@ -23,21 +23,25 @@ export default Ember.Controller.extend({ }); }, - @computed('model.id') + @computed('model.id', 'model.name') wizardUrl(wizardId) { - return window.location.origin + '/wizard/custom/' + wizardId; + return window.location.origin + '/wizard/custom/' + Ember.String.dasherize(wizardId); }, actions: { save() { this.get('model').save().then(() => { - this.send("refreshRoute"); + if (this.get('newWizard')) { + this.send("refreshAllWizards"); + } else { + this.send("refreshWizard"); + } }); }, remove() { this.get('model').remove().then(() => { - this.transitionToRoute('adminWizardsCustom'); + this.send("refreshAllWizards"); }); }, @@ -47,10 +51,8 @@ export default Ember.Controller.extend({ const step = Ember.Object.create({ fields: Ember.A(), actions: Ember.A(), - title: `Step ${newNum}`, id: `step-${newNum}` }); - steps.pushObject(step); this.set('currentStep', step); }, @@ -58,6 +60,7 @@ export default Ember.Controller.extend({ removeStep(stepId) { const steps = this.get('model.steps'); steps.removeObject(steps.findBy('id', stepId)); + this.set('currentStep', steps[steps.length - 1]); }, changeStep(stepId) { diff --git a/assets/javascripts/discourse/helpers/custom-wizard.js.es6 b/assets/javascripts/discourse/helpers/custom-wizard.js.es6 new file mode 100644 index 00000000..c761d9a0 --- /dev/null +++ b/assets/javascripts/discourse/helpers/custom-wizard.js.es6 @@ -0,0 +1,5 @@ +import { registerUnbound } from 'discourse-common/lib/helpers'; + +registerUnbound('dasherize', function(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 fad4b470..91ac2c5a 100644 --- a/assets/javascripts/discourse/models/custom-wizard.js.es6 +++ b/assets/javascripts/discourse/models/custom-wizard.js.es6 @@ -1,37 +1,57 @@ -import { default as computed } from 'ember-addons/ember-computed-decorators'; +import { observes, on } from 'ember-addons/ember-computed-decorators'; import { ajax } from 'discourse/lib/ajax'; const CustomWizard = Discourse.Model.extend({ - init() { + @on('init') + setup() { const id = this.get('id'); if (id) this.set('existingId', id); }, - @computed('name') - id: (name) => name ? Ember.String.dasherize(name) : null, + @observes('name') + updateId() { + const name = this.get('name'); + this.set('id', name.underscore()); + }, save() { const stepsObj = this.get('steps'); let steps = []; stepsObj.forEach((s) => { + + if (!s.title && !s.translation_key) return; + let step = { - id: Ember.String.dasherize(s.title), - title: s.title, - banner: s.banner, - description: s.description, + id: (s.title || s.translation_key.split('.').pop()).underscore(), fields: [], actions: [] }; + if (s.title) step['title'] = s.title; + if (s.translation_key) step['translation_key'] = s.translation_key; + if (s.banner) step['banner'] = s.banner; + if (s.description) step['description'] = s.description; + const fields = s.get('fields'); fields.forEach((f) => { - f.set('id', Ember.String.dasherize(f.get('label'))); + const fl = f.get('label'); + const fkey = f.get('translation_key'); + + if (!fl && !fkey) return; + + f.set('id', (fl || fkey.split('.').pop()).underscore()); if (f.get('type') === 'dropdown') { const choices = f.get('choices'); + choices.forEach((c) => { - c.set('id', c.get('label')); + const cl = c.get('label'); + const ckey = c.get('translation_key'); + + if (!cl && !ckey) return; + + c.set('id', (cl || ckey.split('.').pop()).underscore()); }); } @@ -39,7 +59,9 @@ const CustomWizard = Discourse.Model.extend({ }); s.actions.forEach((a) => { - a.set('id', Ember.String.dasherize(a.get('label'))); + const al = a.get('label'); + if (!al) return; + a.set('id', al.underscore()); step['actions'].push(a); }); @@ -48,8 +70,9 @@ const CustomWizard = Discourse.Model.extend({ const id = this.get('id'); const name = this.get('name'); + const background = this.get('background'); const save_submissions = this.get('save_submissions'); - let wizard = { id, name, save_submissions, steps }; + let wizard = { id, name, background, save_submissions, steps }; const existingId = this.get('existingId'); if (existingId && existingId !== id) { @@ -93,13 +116,13 @@ CustomWizard.reopenClass({ create(w) { const wizard = this._super.apply(this); - let steps = Ember.A(); let props = { steps }; if (w) { props['id'] = w.id; props['name'] = w.name; + props['background'] = w.background; if (w.steps) { w.steps.forEach((s) => { @@ -125,6 +148,7 @@ CustomWizard.reopenClass({ steps.pushObject(Ember.Object.create({ id: s.id, + translation_key: s.translation_key, title: s.title, description: s.description, banner: s.banner, @@ -134,7 +158,11 @@ CustomWizard.reopenClass({ }); }; } else { + props['id'] = ''; + props['name'] = ''; + props['background'] = ''; props['save_submissions'] = true; + props['steps'] = Ember.A(); }; wizard.setProperties(props); diff --git a/assets/javascripts/discourse/routes/admin-wizard.js.es6 b/assets/javascripts/discourse/routes/admin-wizard.js.es6 index d260f253..2c8f373b 100644 --- a/assets/javascripts/discourse/routes/admin-wizard.js.es6 +++ b/assets/javascripts/discourse/routes/admin-wizard.js.es6 @@ -4,12 +4,12 @@ import { ajax } from 'discourse/lib/ajax'; export default Discourse.Route.extend({ model(params) { if (params.wizard_id === 'new') { - this.set('new', true); + this.set('newWizard', true); return CustomWizard.create(); } - this.set('new', false); + this.set('newWizard', false); - const wizard = this.modelFor('admin-wizards-custom').findBy('id', params.wizard_id); + const wizard = this.modelFor('admin-wizards-custom').findBy('id', params.wizard_id.underscore()); if (!wizard) return this.transitionTo('adminWizardsCustom.index'); return wizard; @@ -21,14 +21,17 @@ export default Discourse.Route.extend({ }, setupController(controller, model) { - let props = { new: this.get('new'), model }; - const steps = model.get('steps'); - if (steps[0]) props['currentStep'] = steps[0]; - controller.setProperties(props); + const newWizard = this.get('newWizard'); + const steps = model.get('steps') || []; + controller.setProperties({ + newWizard, + model, + currentStep: steps[0] + }); }, actions: { - refreshRoute() { + refreshWizard() { this.refresh(); } } diff --git a/assets/javascripts/discourse/routes/admin-wizards-custom.js.es6 b/assets/javascripts/discourse/routes/admin-wizards-custom.js.es6 index 10fa40b9..53e813ef 100644 --- a/assets/javascripts/discourse/routes/admin-wizards-custom.js.es6 +++ b/assets/javascripts/discourse/routes/admin-wizards-custom.js.es6 @@ -5,10 +5,11 @@ export default Discourse.Route.extend({ return CustomWizard.findAll(); }, - afterModel(model, transition) { - if (transition.intent.name !== 'adminWizard' && model.length > 0) { - this.transitionTo('adminWizard', model[0].id); - } + afterModel(model) { + const transitionToWizard = this.get('transitionToWizard'); + if (transitionToWizard === 'last' && model.length) { + this.transitionTo('adminWizard', model[model.length - 1].id); + }; }, setupController(controller, model){ @@ -16,10 +17,9 @@ export default Discourse.Route.extend({ }, actions: { - willTransition(transition) { - if (transition.intent.name === 'adminWizardsCustom') { - this.refresh(); - } + refreshAllWizards() { + this.set('transitionToWizard', 'last'); + this.refresh(); } } }); diff --git a/assets/javascripts/discourse/routes/admin-wizards.js.es6 b/assets/javascripts/discourse/routes/admin-wizards.js.es6 index 149e51be..f6eac996 100644 --- a/assets/javascripts/discourse/routes/admin-wizards.js.es6 +++ b/assets/javascripts/discourse/routes/admin-wizards.js.es6 @@ -1,5 +1 @@ -export default Discourse.Route.extend({ - redirect() { - this.transitionTo('adminWizardsCustom'); - } -}); +export default Discourse.Route.extend(); diff --git a/assets/javascripts/discourse/templates/admin-wizard.hbs b/assets/javascripts/discourse/templates/admin-wizard.hbs index 7a8516fc..93f24627 100644 --- a/assets/javascripts/discourse/templates/admin-wizard.hbs +++ b/assets/javascripts/discourse/templates/admin-wizard.hbs @@ -4,12 +4,21 @@ {{i18n 'admin.wizard.header'}} +