0
0
Fork 1
Spiegel von https://github.com/paviliondev/discourse-custom-wizard.git synchronisiert 2024-11-22 09:20:29 +01:00
Dieser Commit ist enthalten in:
Angus McLeod 2020-04-01 23:16:26 +11:00
Ursprung 2046648862
Commit bbdf11c84f
6 geänderte Dateien mit 118 neuen und 85 gelöschten Zeilen

Datei anzeigen

@ -2,6 +2,7 @@ import { default as discourseComputed, observes } from 'discourse-common/utils/d
import { notEmpty } from "@ember/object/computed";
import showModal from 'discourse/lib/show-modal';
import { generateId } from '../lib/custom-wizard';
import { buildProperties } from '../lib/json';
import { dasherize } from "@ember/string";
export default Ember.Controller.extend({
@ -58,10 +59,16 @@ export default Ember.Controller.extend({
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);
if (this.get('newWizard')) {
this.send("refreshAllWizards");
} else {

Datei anzeigen

@ -1,9 +1,17 @@
import { properties } from '../lib/custom-wizard';
import { mappedProperties } from '../lib/mapper';
import { EmberObject } from '@ember/object';
import EmberObject from '@ember/object';
function present(val) {
return val && val.length;
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;
} else {
return false;
}
}
function mapped(property, type) {
@ -16,14 +24,18 @@ function buildJson(object, type) {
properties[type].forEach((p) => {
let value = object.get(p);
if (mapped(p, type)) {
value = buildMappedJson(value);
}
if (value) {
result[p] = mapped(p, type) ? buildMappedJson(value) : value;
result[p] = value;
}
});
return result;
},
}
function buildMappedJson(inputs) {
if (!inputs || !inputs.length) return false;
@ -76,7 +88,7 @@ function buildMappedJson(inputs) {
return result;
}
buildStepJson(object) {
function buildStepJson(object) {
let steps = [];
let error = null;
@ -143,26 +155,35 @@ function buildObject(json, type) {
if (mapped(prop, type)) {
let inputs = [];
json[prop].forEach(inputJson => {
let input = {}
Object.keys(inputJson).forEach(inputKey => {
if (inputKey === 'pairs') {
let pairs = [];
let pairCount = inputJson.pairs.length;
inputJson.pairs.forEach(pairJson => {
let pair = pairJson;
pair.pairCount = pairCount;
pairs.push(EmberObject.create(pair));
});
} else {
input[inputKey] = inputJson[inputKey];
}
if (present(json[prop])) {
json[prop].forEach(inputJson => {
let input = {}
Object.keys(inputJson).forEach(inputKey => {
if (inputKey === 'pairs') {
let pairs = [];
let pairCount = inputJson.pairs.length;
inputJson.pairs.forEach(pairJson => {
let pair = pairJson;
pair.pairCount = pairCount;
pairs.push(
EmberObject.create(pair)
);
});
input.pairs = pairs;
} else {
input[inputKey] = inputJson[inputKey];
}
});
inputs.push(
EmberObject.create(input)
);
});
inputs.push(EmberObject.create(input));
});
}
params[prop] = Ember.A(inputs);
} else {
@ -170,16 +191,15 @@ function buildObject(json, type) {
}
});
EmberObject.create();
return EmberObject.create(params);
}
function buildWizardProperties(json) {
function buildProperties(json) {
let steps = Ember.A();
let props = {
steps
};
if (present(json)) {
props.id = json.id;
props.existingId = true;
@ -189,53 +209,60 @@ function buildWizardProperties(json) {
});
if (present(json.steps)) {
json.steps.forEach((s) => {
let fields = Ember.A();
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,
json.steps.forEach((stepJson) => {
let stepParams = {
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 {
props['id'] = '';
props['name'] = '';
props['background'] = '';
props['save_submissions'] = true;
props['multiple_submissions'] = false;
props['after_signup'] = false;
props['after_time'] = false;
props['required'] = false;
props['prompt_completion'] = false;
props['restart_on_revisit'] = false;
props['permitted'] = null;
props['steps'] = Ember.A();
props.id = '';
props.name = '';
props.background = '';
props.save_submissions = true;
props.multiple_submissions = false;
props.after_signup = false;
props.after_time = false;
props.required = false;
props.prompt_completion = false;
props.restart_on_revisit = false;
props.permitted = null;
props.steps = Ember.A();
}
return props;
}
export {
buildStepJson,
buildJson,
buildProperties
}

Datei anzeigen

@ -1,11 +1,10 @@
import { ajax } from 'discourse/lib/ajax';
import EmberObject from "@ember/object";
import { buildStepJson, buildJson, buildWizardProperties } from '../lib/json';
import { buildStepJson, buildJson, buildProperties } from '../lib/json';
const CustomWizard = EmberObject.extend({
save() {
return new Ember.RSVP.Promise((resolve, reject) => {
let wizardJson = buildJson(this, 'wizard');
if (wizardJson.after_time && !wizardJson.after_time_scheduled) {
@ -35,6 +34,7 @@ const CustomWizard = EmberObject.extend({
wizard: JSON.stringify(wizardJson)
}
}).then((result) => {
console.log('result: ', result);
if (result.error) {
reject(result);
} else {
@ -59,7 +59,9 @@ CustomWizard.reopenClass({
return ajax("/admin/wizards/custom/all", {
type: 'GET'
}).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 = {}) {
const wizard = this._super.apply(this);
wizard.setProperties(buildWizardProperties(wizardJson));
wizard.setProperties(buildProperties(wizardJson));
return wizard;
}
});

Datei anzeigen

@ -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 { makeArray } from "discourse-common/lib/helpers";

Datei anzeigen

@ -104,7 +104,7 @@ class CustomWizard::AdminController < ::ApplicationController
PluginStore.set('custom_wizard', wizard["id"], wizard)
render json: success_json
render json: success_json.merge(wizard: wizard)
end
def remove

Datei anzeigen

@ -4,7 +4,7 @@ class CustomWizard::Mapper
USER_FIELDS = ['name', 'username', 'email', 'date_of_birth', 'title', 'locale', 'trust_level']
PROFILE_FIELDS = ['location', 'website', 'bio_raw']
OPERATORS = {
equal: '=',
equal: '==',
greater: '>',
less: '<',
greater_or_equal: '>=',
@ -53,11 +53,11 @@ class CustomWizard::Mapper
pairs.each do |pair|
key = map_field(pair['key'], pair['key_type'])
value = map_field(pair['value'], pair['value_type'])
begin
failed = true unless key.public_send(operator(pair['connector']), value)
rescue => e
byebug
rescue NoMethodError
#
end
end