Spiegel von
https://github.com/paviliondev/discourse-custom-wizard.git
synchronisiert 2024-11-26 11:00:28 +01:00
wip
Dieser Commit ist enthalten in:
Ursprung
2046648862
Commit
bbdf11c84f
6 geänderte Dateien mit 118 neuen und 85 gelöschten Zeilen
|
@ -2,6 +2,7 @@ import { default as discourseComputed, observes } from 'discourse-common/utils/d
|
||||||
import { notEmpty } from "@ember/object/computed";
|
import { notEmpty } from "@ember/object/computed";
|
||||||
import showModal from 'discourse/lib/show-modal';
|
import showModal from 'discourse/lib/show-modal';
|
||||||
import { generateId } from '../lib/custom-wizard';
|
import { generateId } from '../lib/custom-wizard';
|
||||||
|
import { buildProperties } from '../lib/json';
|
||||||
import { dasherize } from "@ember/string";
|
import { dasherize } from "@ember/string";
|
||||||
|
|
||||||
export default Ember.Controller.extend({
|
export default Ember.Controller.extend({
|
||||||
|
@ -58,10 +59,16 @@ export default Ember.Controller.extend({
|
||||||
error: null
|
error: null
|
||||||
});
|
});
|
||||||
|
|
||||||
const wizard = this.get('model');
|
const wizard = this.model;
|
||||||
|
|
||||||
|
wizard.save().then((result) => {
|
||||||
|
|
||||||
|
this.model.setProperties(
|
||||||
|
buildProperties(result.wizard)
|
||||||
|
);
|
||||||
|
|
||||||
wizard.save().then(() => {
|
|
||||||
this.set('saving', false);
|
this.set('saving', false);
|
||||||
|
|
||||||
if (this.get('newWizard')) {
|
if (this.get('newWizard')) {
|
||||||
this.send("refreshAllWizards");
|
this.send("refreshAllWizards");
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -1,9 +1,17 @@
|
||||||
import { properties } from '../lib/custom-wizard';
|
import { properties } from '../lib/custom-wizard';
|
||||||
import { mappedProperties } from '../lib/mapper';
|
import { mappedProperties } from '../lib/mapper';
|
||||||
import { EmberObject } from '@ember/object';
|
import EmberObject from '@ember/object';
|
||||||
|
|
||||||
function present(val) {
|
function present(val) {
|
||||||
|
if (val === null || val === undefined) {
|
||||||
|
return false;
|
||||||
|
} else if (typeof val === 'object') {
|
||||||
|
return Object.keys(val).length !== 0;
|
||||||
|
} else if (typeof val === 'string' || val.constructor === Array) {
|
||||||
return val && val.length;
|
return val && val.length;
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function mapped(property, type) {
|
function mapped(property, type) {
|
||||||
|
@ -17,13 +25,17 @@ function buildJson(object, type) {
|
||||||
properties[type].forEach((p) => {
|
properties[type].forEach((p) => {
|
||||||
let value = object.get(p);
|
let value = object.get(p);
|
||||||
|
|
||||||
|
if (mapped(p, type)) {
|
||||||
|
value = buildMappedJson(value);
|
||||||
|
}
|
||||||
|
|
||||||
if (value) {
|
if (value) {
|
||||||
result[p] = mapped(p, type) ? buildMappedJson(value) : value;
|
result[p] = value;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
},
|
}
|
||||||
|
|
||||||
function buildMappedJson(inputs) {
|
function buildMappedJson(inputs) {
|
||||||
if (!inputs || !inputs.length) return false;
|
if (!inputs || !inputs.length) return false;
|
||||||
|
@ -76,7 +88,7 @@ function buildMappedJson(inputs) {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
buildStepJson(object) {
|
function buildStepJson(object) {
|
||||||
let steps = [];
|
let steps = [];
|
||||||
let error = null;
|
let error = null;
|
||||||
|
|
||||||
|
@ -143,6 +155,7 @@ function buildObject(json, type) {
|
||||||
if (mapped(prop, type)) {
|
if (mapped(prop, type)) {
|
||||||
let inputs = [];
|
let inputs = [];
|
||||||
|
|
||||||
|
if (present(json[prop])) {
|
||||||
json[prop].forEach(inputJson => {
|
json[prop].forEach(inputJson => {
|
||||||
let input = {}
|
let input = {}
|
||||||
|
|
||||||
|
@ -154,15 +167,23 @@ function buildObject(json, type) {
|
||||||
inputJson.pairs.forEach(pairJson => {
|
inputJson.pairs.forEach(pairJson => {
|
||||||
let pair = pairJson;
|
let pair = pairJson;
|
||||||
pair.pairCount = pairCount;
|
pair.pairCount = pairCount;
|
||||||
pairs.push(EmberObject.create(pair));
|
|
||||||
|
pairs.push(
|
||||||
|
EmberObject.create(pair)
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
input.pairs = pairs;
|
||||||
} else {
|
} else {
|
||||||
input[inputKey] = inputJson[inputKey];
|
input[inputKey] = inputJson[inputKey];
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
inputs.push(EmberObject.create(input));
|
inputs.push(
|
||||||
|
EmberObject.create(input)
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
params[prop] = Ember.A(inputs);
|
params[prop] = Ember.A(inputs);
|
||||||
} else {
|
} else {
|
||||||
|
@ -170,11 +191,10 @@ function buildObject(json, type) {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
return EmberObject.create(params);
|
||||||
EmberObject.create();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function buildWizardProperties(json) {
|
function buildProperties(json) {
|
||||||
let steps = Ember.A();
|
let steps = Ember.A();
|
||||||
let props = {
|
let props = {
|
||||||
steps
|
steps
|
||||||
|
@ -189,53 +209,60 @@ function buildWizardProperties(json) {
|
||||||
});
|
});
|
||||||
|
|
||||||
if (present(json.steps)) {
|
if (present(json.steps)) {
|
||||||
json.steps.forEach((s) => {
|
json.steps.forEach((stepJson) => {
|
||||||
let fields = Ember.A();
|
let stepParams = {
|
||||||
|
|
||||||
if (present(s.fields)) {
|
|
||||||
s.fields.forEach((f) => {
|
|
||||||
fields.pushObject(buildObject(f, 'field'));
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
let actions = Ember.A();
|
|
||||||
if (s.actions && s.actions.length) {
|
|
||||||
s.actions.forEach((a) => {
|
|
||||||
const actionParams = { isNew: false };
|
|
||||||
const action = EmberObject.create($.extend(a, actionParams));
|
|
||||||
actions.pushObject(action);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
steps.pushObject(EmberObject.create({
|
|
||||||
id: s.id,
|
|
||||||
key: s.key,
|
|
||||||
title: s.title,
|
|
||||||
raw_description: s.raw_description,
|
|
||||||
banner: s.banner,
|
|
||||||
required_data: s.required_data,
|
|
||||||
required_data_message: s.required_data_message,
|
|
||||||
permitted_params: s.permitted_params,
|
|
||||||
fields,
|
|
||||||
actions,
|
|
||||||
isNew: false
|
isNew: false
|
||||||
}));
|
};
|
||||||
|
|
||||||
|
properties.step.forEach((p) => {
|
||||||
|
stepParams[p] = stepJson[p];
|
||||||
|
});
|
||||||
|
|
||||||
|
stepParams.fields = Ember.A();
|
||||||
|
|
||||||
|
if (present(stepJson.fields)) {
|
||||||
|
stepJson.fields.forEach((f) => {
|
||||||
|
stepParams.fields.pushObject(
|
||||||
|
buildObject(f, 'field')
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
stepParams.actions = Ember.A();
|
||||||
|
|
||||||
|
if (present(stepJson.actions)) {
|
||||||
|
stepJson.actions.forEach((a) => {
|
||||||
|
stepParams.actions.pushObject(
|
||||||
|
buildObject(a, 'action')
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
steps.pushObject(
|
||||||
|
EmberObject.create(stepParams)
|
||||||
|
);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
} else {
|
} else {
|
||||||
props['id'] = '';
|
props.id = '';
|
||||||
props['name'] = '';
|
props.name = '';
|
||||||
props['background'] = '';
|
props.background = '';
|
||||||
props['save_submissions'] = true;
|
props.save_submissions = true;
|
||||||
props['multiple_submissions'] = false;
|
props.multiple_submissions = false;
|
||||||
props['after_signup'] = false;
|
props.after_signup = false;
|
||||||
props['after_time'] = false;
|
props.after_time = false;
|
||||||
props['required'] = false;
|
props.required = false;
|
||||||
props['prompt_completion'] = false;
|
props.prompt_completion = false;
|
||||||
props['restart_on_revisit'] = false;
|
props.restart_on_revisit = false;
|
||||||
props['permitted'] = null;
|
props.permitted = null;
|
||||||
props['steps'] = Ember.A();
|
props.steps = Ember.A();
|
||||||
}
|
}
|
||||||
|
|
||||||
return props;
|
return props;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export {
|
||||||
|
buildStepJson,
|
||||||
|
buildJson,
|
||||||
|
buildProperties
|
||||||
|
}
|
|
@ -1,11 +1,10 @@
|
||||||
import { ajax } from 'discourse/lib/ajax';
|
import { ajax } from 'discourse/lib/ajax';
|
||||||
import EmberObject from "@ember/object";
|
import EmberObject from "@ember/object";
|
||||||
import { buildStepJson, buildJson, buildWizardProperties } from '../lib/json';
|
import { buildStepJson, buildJson, buildProperties } from '../lib/json';
|
||||||
|
|
||||||
const CustomWizard = EmberObject.extend({
|
const CustomWizard = EmberObject.extend({
|
||||||
save() {
|
save() {
|
||||||
return new Ember.RSVP.Promise((resolve, reject) => {
|
return new Ember.RSVP.Promise((resolve, reject) => {
|
||||||
|
|
||||||
let wizardJson = buildJson(this, 'wizard');
|
let wizardJson = buildJson(this, 'wizard');
|
||||||
|
|
||||||
if (wizardJson.after_time && !wizardJson.after_time_scheduled) {
|
if (wizardJson.after_time && !wizardJson.after_time_scheduled) {
|
||||||
|
@ -35,6 +34,7 @@ const CustomWizard = EmberObject.extend({
|
||||||
wizard: JSON.stringify(wizardJson)
|
wizard: JSON.stringify(wizardJson)
|
||||||
}
|
}
|
||||||
}).then((result) => {
|
}).then((result) => {
|
||||||
|
console.log('result: ', result);
|
||||||
if (result.error) {
|
if (result.error) {
|
||||||
reject(result);
|
reject(result);
|
||||||
} else {
|
} else {
|
||||||
|
@ -59,7 +59,9 @@ CustomWizard.reopenClass({
|
||||||
return ajax("/admin/wizards/custom/all", {
|
return ajax("/admin/wizards/custom/all", {
|
||||||
type: 'GET'
|
type: 'GET'
|
||||||
}).then(result => {
|
}).then(result => {
|
||||||
return result.wizards.map(w => CustomWizard.create(w));
|
return result.wizards.map(wizard => {
|
||||||
|
return CustomWizard.create(wizard);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -73,10 +75,7 @@ CustomWizard.reopenClass({
|
||||||
|
|
||||||
create(wizardJson = {}) {
|
create(wizardJson = {}) {
|
||||||
const wizard = this._super.apply(this);
|
const wizard = this._super.apply(this);
|
||||||
|
wizard.setProperties(buildProperties(wizardJson));
|
||||||
|
|
||||||
wizard.setProperties(buildWizardProperties(wizardJson));
|
|
||||||
|
|
||||||
return wizard;
|
return wizard;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import CategorySelector from 'discourse/components/category-selector';
|
import CategorySelector from 'select-kit/components/category-selector';
|
||||||
import { computed } from "@ember/object";
|
import { computed } from "@ember/object";
|
||||||
import { makeArray } from "discourse-common/lib/helpers";
|
import { makeArray } from "discourse-common/lib/helpers";
|
||||||
|
|
||||||
|
|
|
@ -104,7 +104,7 @@ class CustomWizard::AdminController < ::ApplicationController
|
||||||
|
|
||||||
PluginStore.set('custom_wizard', wizard["id"], wizard)
|
PluginStore.set('custom_wizard', wizard["id"], wizard)
|
||||||
|
|
||||||
render json: success_json
|
render json: success_json.merge(wizard: wizard)
|
||||||
end
|
end
|
||||||
|
|
||||||
def remove
|
def remove
|
||||||
|
|
|
@ -4,7 +4,7 @@ class CustomWizard::Mapper
|
||||||
USER_FIELDS = ['name', 'username', 'email', 'date_of_birth', 'title', 'locale', 'trust_level']
|
USER_FIELDS = ['name', 'username', 'email', 'date_of_birth', 'title', 'locale', 'trust_level']
|
||||||
PROFILE_FIELDS = ['location', 'website', 'bio_raw']
|
PROFILE_FIELDS = ['location', 'website', 'bio_raw']
|
||||||
OPERATORS = {
|
OPERATORS = {
|
||||||
equal: '=',
|
equal: '==',
|
||||||
greater: '>',
|
greater: '>',
|
||||||
less: '<',
|
less: '<',
|
||||||
greater_or_equal: '>=',
|
greater_or_equal: '>=',
|
||||||
|
@ -56,8 +56,8 @@ class CustomWizard::Mapper
|
||||||
|
|
||||||
begin
|
begin
|
||||||
failed = true unless key.public_send(operator(pair['connector']), value)
|
failed = true unless key.public_send(operator(pair['connector']), value)
|
||||||
rescue => e
|
rescue NoMethodError
|
||||||
byebug
|
#
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Laden …
In neuem Issue referenzieren