0
0
Fork 1
Spiegel von https://github.com/paviliondev/discourse-custom-wizard.git synchronisiert 2024-11-22 01:10:28 +01:00

Working wizard creation

Dieser Commit ist enthalten in:
Angus McLeod 2017-09-24 11:01:18 +08:00
Ursprung ebd026887c
Commit a3f9135698
14 geänderte Dateien mit 64 neuen und 55 gelöschten Zeilen

Datei anzeigen

@ -1,11 +1,4 @@
import { default as computed } from 'ember-addons/ember-computed-decorators';
export default Ember.Component.extend({ export default Ember.Component.extend({
targets: ['topic', 'profile', 'email', 'badge', 'save'], targets: ['topic', 'profile', 'email', 'badge', 'save'],
isTopic: Ember.computed.equal('targets', 'topic'), isTopic: Ember.computed.equal('targets', 'topic')
init() {
this._super(...arguments);
console.log(this)
},
}); });

Datei anzeigen

@ -4,18 +4,24 @@ export default Ember.Component.extend({
classNames: 'wizard-custom-field', classNames: 'wizard-custom-field',
fieldTypes: ['dropdown', 'image', 'radio', 'text', 'textarea'], fieldTypes: ['dropdown', 'image', 'radio', 'text', 'textarea'],
isDropdown: Ember.computed.equal('field.type', 'dropdown'), isDropdown: Ember.computed.equal('field.type', 'dropdown'),
choices: Ember.A(),
init() {
this._super(...arguments);
if (!this.get('field.choices')) {
this.set('field.choices', Ember.A());
}
},
@observes('field.label') @observes('field.label')
setFieldId() { setFieldId() {
const label = this.get('field.label'); const label = this.get('field.label');
console.log('setting id')
this.set('field.id', Ember.String.underscore(label)); this.set('field.id', Ember.String.underscore(label));
}, },
actions: { actions: {
addChoice() { addChoice() {
this.get('field.choices').pushObject(Ember.Object.create());
} }
} }
}); });

Datei anzeigen

@ -5,13 +5,11 @@ export default Ember.Component.extend({
@computed('step.fields.@each.id') @computed('step.fields.@each.id')
allowAddAction(stepFields) { allowAddAction(stepFields) {
console.log(stepFields)
return stepFields.get('firstObject.id'); return stepFields.get('firstObject.id');
}, },
actions: { actions: {
addField() { addField() {
console.log('adding field')
this.get('step.fields').pushObject(Ember.Object.create()); this.get('step.fields').pushObject(Ember.Object.create());
}, },
@ -19,8 +17,8 @@ export default Ember.Component.extend({
this.get('step.actions').pushObject(Ember.Object.create()); this.get('step.actions').pushObject(Ember.Object.create());
}, },
removeStep() { removeField(field) {
this.sendAction('removeStep', this.get('step.name')); this.get('step.fields').removeObject(field);
} }
} }
}); });

Datei anzeigen

@ -7,7 +7,7 @@ export default Ember.Controller.extend({
}, },
remove() { remove() {
this.get('model').destroy().then(() => { this.get('model').remove().then(() => {
this.transitionToRoute('adminWizardsCustom'); this.transitionToRoute('adminWizardsCustom');
}); });
}, },
@ -19,8 +19,8 @@ export default Ember.Controller.extend({
}); });
}, },
removeStep(name) { removeStep(step) {
this.get('model.steps').findBy('name', name); this.get('model.steps').removeObject(step);
} }
} }
}); });

Datei anzeigen

@ -1,20 +1,36 @@
import { ajax } from 'discourse/lib/ajax'; import { ajax } from 'discourse/lib/ajax';
import { default as computed } from 'ember-addons/ember-computed-decorators';
const CustomWizard = Discourse.Model.extend({ const CustomWizard = Discourse.Model.extend({
steps: Ember.A(), steps: Ember.A(),
@computed('name')
dasherizedName(name) {
return Ember.String.dasherize(name);
},
save() { save() {
const steps = JSON.stringify(this.get('steps').toArray()); const wizard = {
return ajax(`/admin/wizards/custom/${this.get('name')}`, { id: this.get('id'),
steps: this.get('steps').toArray(),
name: this.get('name')
};
return ajax(`/admin/wizards/custom/save`, {
type: 'PUT', type: 'PUT',
data: { steps } data: {
wizard: JSON.stringify(wizard)
}
}); });
}, },
destroy() { remove() {
return ajax(`/admin/wizards/custom/${this.get('name')}`, { return ajax(`/admin/wizards/custom/remove`, {
type: 'DELETE' type: 'DELETE',
}); data: {
id: this.get('id')
}
}).then(() => this.destroy());
} }
}); });
@ -31,6 +47,7 @@ CustomWizard.reopenClass({
steps.forEach((s) => { steps.forEach((s) => {
s.fields = Ember.A(s.fields); s.fields = Ember.A(s.fields);
s.fields.forEach((f) => f.choices = Ember.A(f.choices));
s.actions = Ember.A(s.actions); s.actions = Ember.A(s.actions);
}); });

Datei anzeigen

@ -4,14 +4,14 @@ export default Discourse.Route.extend({
model(params) { model(params) {
if (params.name === 'new') { if (params.name === 'new') {
this.set('new', true); this.set('new', true);
return CustomWizard.create(); return CustomWizard.create({ name: '', steps: []});
} }
this.set('new', false); this.set('new', false);
const wizard = this.modelFor('admin-wizards-custom').findBy('name', params.name ); const wizard = this.modelFor('admin-wizards-custom').findBy('dasherizedName', params.name );
if (!wizard) { return this.transitionTo('adminWizardsCustom.index'); } if (!wizard) return this.transitionTo('adminWizardsCustom.index');
return wizard; return wizard;
}, },

Datei anzeigen

@ -8,10 +8,11 @@
{{#if model.steps}} {{#if model.steps}}
{{#each model.steps as |s|}} {{#each model.steps as |s|}}
{{wizard-custom-step step=s}} {{wizard-custom-step step=s}}
{{d-button action='removeStep' actionParam=s label='admin.wizard.step.remove'}}
{{/each}} {{/each}}
{{/if}} {{/if}}
{{d-button action='addStep' label='admin.wizard.add_step'}} {{d-button action='addStep' label='admin.wizard.step.add'}}
<div class='buttons'> <div class='buttons'>
<button {{action "save"}} disabled={{disableSave}} class='btn btn-primary'>{{i18n 'admin.wizard.save'}}</button> <button {{action "save"}} disabled={{disableSave}} class='btn btn-primary'>{{i18n 'admin.wizard.save'}}</button>

Datei anzeigen

@ -3,7 +3,7 @@
<ul> <ul>
{{#each model as |w|}} {{#each model as |w|}}
<li> <li>
{{#link-to "adminWizard" w.name}}{{w.name}}{{/link-to}} {{#link-to "adminWizard" w.dasherizedName}}{{w.name}}{{/link-to}}
</li> </li>
{{/each}} {{/each}}
</ul> </ul>

Datei anzeigen

@ -14,7 +14,7 @@
{{#if isDropdown}} {{#if isDropdown}}
<span>{{i18n 'admin.wizard.field.choices_label'}}</span> <span>{{i18n 'admin.wizard.field.choices_label'}}</span>
{{#each field.choices as |c|}} {{#each field.choices as |c|}}
{{input type='text' value=c}} {{input type='text' value=c.label}}
{{/each}} {{/each}}
{{d-button action='addChoice' label='admin.wizard.field.add_choice'}} {{d-button action='addChoice' label='admin.wizard.field.add_choice'}}
{{/if}} {{/if}}

Datei anzeigen

@ -15,6 +15,7 @@
{{#each step.fields as |f|}} {{#each step.fields as |f|}}
{{wizard-custom-field field=f}} {{wizard-custom-field field=f}}
{{d-button action='removeField' actionParam=f label="admin.wizard.field.remove"}}
{{/each}} {{/each}}
{{d-button action='addField' label='admin.wizard.step.add_field'}} {{d-button action='addField' label='admin.wizard.step.add_field'}}
@ -26,5 +27,3 @@
{{#if allowAddAction}} {{#if allowAddAction}}
{{d-button action='addAction' label='admin.wizard.step.add_action'}} {{d-button action='addAction' label='admin.wizard.step.add_action'}}
{{/if}} {{/if}}
{{d-button action='removeStep' label='admin.wizard.remove_step'}}

Datei anzeigen

@ -7,8 +7,6 @@ en:
custom_label: "Custom" custom_label: "Custom"
name: "Name" name: "Name"
name_placeholder: "name of the wizard" name_placeholder: "name of the wizard"
add_step: "Add Step"
remove_step: "Remove Step"
save: "Save Wizard" save: "Save Wizard"
remove: "Delete Wizard" remove: "Delete Wizard"
step: step:
@ -18,6 +16,8 @@ en:
banner_placeholder: "This image will appear under the title" banner_placeholder: "This image will appear under the title"
description: "Step Description" description: "Step Description"
description_placeholder: "This will appear underneath the title and / or title" description_placeholder: "This will appear underneath the title and / or title"
add: "Add Step"
remove: "Remove Step"
add_field: "Add Field" add_field: "Add Field"
add_action: "Add Action" add_action: "Add Action"
field: field:
@ -27,5 +27,6 @@ en:
choices_label: "Dropdown Choices" choices_label: "Dropdown Choices"
add_choice: "Add Choice" add_choice: "Add Choice"
required: "Field Required" required: "Field Required"
remove: "Remove Field"
action: action:
email: "Email" email: "Email"

Datei anzeigen

@ -7,42 +7,35 @@ class CustomWizard::AdminController < ::ApplicationController
end end
def save def save
params.require(:name) params.require(:wizard)
params.permit(:steps)
wizard = { name: params[:name] } wizard = ::JSON.parse(params[:wizard])
wizard['steps'] = params[:steps] if params[:steps] wizard["id"] = SecureRandom.hex(8) if !wizard["id"]
key = params[:name].downcase PluginStore.set('custom_wizards', wizard["id"], wizard)
PluginStore.set('custom_wizards', key, wizard)
render json: success_json render json: success_json
end end
def remove def remove
params.require(:name) params.require(:id)
key = params[:name].downcase PluginStore.remove('custom_wizards', params[:id])
PluginStore.remove('custom_wizards', key)
render json: success_json render json: success_json
end end
def find def find
params.require(:name) params.require(:id)
key = params[:name].downcase wizard = PluginStore.get('custom_wizards', params[:id])
wizard = PluginStore.get('custom_wizards', key)
render json: success_json.merge(wizard: wizard) render json: success_json.merge(wizard: wizard)
end end
def all def all
rows = PluginStoreRow.where(plugin_name: 'custom_wizards') rows = PluginStoreRow.where(plugin_name: 'custom_wizards').order(:id)
wizards = rows ? [*rows].map do |r| wizards = rows ? [*rows].map do |r|
CustomWizard::Wizard.new(r.value) CustomWizard::Wizard.new(r.value)

Datei anzeigen

@ -4,7 +4,8 @@ class CustomWizard::Wizard
def initialize(data) def initialize(data)
parsed = ::JSON.parse(data) parsed = ::JSON.parse(data)
@id = parsed['id']
@name = parsed['name'] @name = parsed['name']
@steps = JSON.parse(parsed['steps']) @steps = parsed['steps']
end end
end end

Datei anzeigen

@ -18,9 +18,9 @@ after_initialize do
get 'custom' => 'admin#index' get 'custom' => 'admin#index'
get 'custom/new' => 'admin#index' get 'custom/new' => 'admin#index'
get 'custom/all' => "admin#all" get 'custom/all' => "admin#all"
get 'custom/:name' => "admin#find" get 'custom/:id' => "admin#find"
put 'custom/:name' => "admin#save" put 'custom/save' => "admin#save"
delete 'custom/:name' => "admin#remove" delete 'custom/remove' => "admin#remove"
end end
require_dependency 'admin_constraint' require_dependency 'admin_constraint'