0
0
Fork 1
Spiegel von https://github.com/paviliondev/discourse-custom-wizard.git synchronisiert 2024-11-23 09:50:28 +01:00
discourse-custom-wizard/assets/javascripts/discourse/components/wizard-mapper.js.es6

85 Zeilen
2,3 KiB
Text

2020-04-01 07:03:26 +02:00
import { getOwner } from 'discourse-common/lib/get-owner';
2020-04-05 03:37:09 +02:00
import { newInput, selectionTypes } from '../lib/wizard-mapper';
2020-04-06 10:36:38 +02:00
import { default as discourseComputed, observes, on } from 'discourse-common/utils/decorators';
2020-04-15 07:41:27 +02:00
import { later } from "@ember/runloop";
2020-04-05 03:37:09 +02:00
import Component from "@ember/component";
import { A } from "@ember/array";
2020-04-01 07:03:26 +02:00
2020-04-05 03:37:09 +02:00
export default Component.extend({
2020-04-01 07:03:26 +02:00
classNames: 'wizard-mapper',
2020-04-15 07:41:27 +02:00
didReceiveAttrs() {
if (this.inputs && this.inputs.constructor !== Array) {
later(() => this.set('inputs', null));
}
},
2020-04-10 09:57:49 +02:00
@discourseComputed('inputs.@each.type')
canAdd(inputs) {
2020-04-15 07:41:27 +02:00
return !inputs ||
inputs.constructor !== Array ||
inputs.every(i => {
return ['assignment','association'].indexOf(i.type) === -1;
});
2020-04-01 07:03:26 +02:00
},
2020-04-06 10:36:38 +02:00
@discourseComputed('options.@each.inputType')
2020-04-01 07:03:26 +02:00
inputOptions(options) {
2020-04-05 03:37:09 +02:00
let result = {
2020-04-10 09:57:49 +02:00
inputTypes: options.inputTypes || 'assignment,conditional',
2020-04-06 10:36:38 +02:00
inputConnector: options.inputConnector || 'or',
2020-04-01 07:03:26 +02:00
pairConnector: options.pairConnector || null,
2020-04-06 03:54:16 +02:00
outputConnector: options.outputConnector || null,
context: options.context || null
2020-04-01 07:03:26 +02:00
}
2020-04-06 10:36:38 +02:00
2020-04-05 03:37:09 +02:00
let inputTypes = ['key', 'value', 'output'];
inputTypes.forEach(type => {
2020-04-06 03:54:16 +02:00
result[`${type}Placeholder`] = options[`${type}Placeholder`] || null;
2020-04-05 03:37:09 +02:00
result[`${type}DefaultSelection`] = options[`${type}DefaultSelection`] || null;
});
selectionTypes.forEach(type => {
2020-04-07 16:18:12 +02:00
if (options[`${type}Selection`] !== undefined) {
2020-04-05 03:37:09 +02:00
result[`${type}Selection`] = options[`${type}Selection`]
} else {
result[`${type}Selection`] = type === 'text' ? true : false;
}
});
2020-04-06 03:54:16 +02:00
2020-04-05 03:37:09 +02:00
return result;
},
2020-04-20 11:42:52 +02:00
onUpdate() {
},
2020-04-01 07:03:26 +02:00
actions: {
add() {
2020-04-06 03:54:16 +02:00
if (!this.get('inputs')) {
this.set('inputs', A());
}
2020-04-06 10:36:38 +02:00
this.get('inputs').pushObject(
newInput(this.inputOptions, this.inputs.length)
);
2020-04-20 11:41:13 +02:00
this.onUpdate(this.property, 'input');
2020-04-01 07:03:26 +02:00
},
remove(input) {
2020-04-07 06:56:16 +02:00
const inputs = this.inputs;
inputs.removeObject(input);
if (inputs.length) {
inputs[0].set('connector', null);
}
2020-04-20 11:41:13 +02:00
this.onUpdate(this.property, 'input');
},
inputUpdated(component, type) {
this.onUpdate(this.property, component, type);
2020-04-01 07:03:26 +02:00
}
}
});