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

70 Zeilen
2,2 KiB
Text

2020-04-01 07:03:26 +02:00
import { computed, set } from "@ember/object";
2020-04-07 09:54:30 +02:00
import { alias, equal, or, not } from "@ember/object/computed";
2020-04-07 12:28:39 +02:00
import { newPair, connectorContent, inputTypesContent, defaultSelectionType, defaultConnector } from '../lib/wizard-mapper';
2020-04-05 03:37:09 +02:00
import Component from "@ember/component";
2020-04-06 03:54:16 +02:00
import { observes } from "discourse-common/utils/decorators";
2020-04-07 12:28:39 +02:00
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-06 10:36:38 +02:00
classNameBindings: [':mapper-input', 'inputType'],
2020-04-01 07:03:26 +02:00
inputType: alias('input.type'),
isConditional: equal('inputType', 'conditional'),
2020-04-05 03:37:09 +02:00
isAssignment: equal('inputType', 'assignment'),
2020-04-06 10:36:38 +02:00
isAssociation: equal('inputType', 'association'),
isValidation: equal('inputType', 'validation'),
2020-04-05 03:37:09 +02:00
hasOutput: or('isConditional', 'isAssignment'),
2020-04-06 10:36:38 +02:00
hasPairs: or('isConditional', 'isAssociation', 'isValidation'),
2020-04-07 09:54:30 +02:00
canAddPair: not('isAssignment'),
2020-04-01 07:03:26 +02:00
connectors: computed(function() { return connectorContent('output', this.input.type, this.options) }),
inputTypes: computed(function() { return inputTypesContent(this.options) }),
2020-04-07 10:33:51 +02:00
@observes('input.type')
2020-04-07 12:28:39 +02:00
setupType() {
2020-04-07 10:33:51 +02:00
if (this.hasPairs && (!this.input.pairs || this.input.pairs.length < 1)) {
this.send('addPair');
}
2020-04-07 12:28:39 +02:00
if (this.hasOutput) {
this.set('input.output', null);
if (!this.input.outputConnector) {
const options = this.options;
this.set('input.output_type', defaultSelectionType('output', options));
this.set('input.output_connector', defaultConnector('output', this.inputType, options));
}
2020-04-07 12:28:39 +02:00
}
2020-04-07 10:33:51 +02:00
},
2020-04-01 07:03:26 +02:00
actions: {
addPair() {
2020-04-07 12:28:39 +02:00
if (!this.input.pairs) {
this.set('input.pairs', A());
}
2020-04-01 07:03:26 +02:00
const pairs = this.input.pairs;
const pairCount = pairs.length + 1;
2020-04-07 12:28:39 +02:00
2020-04-01 07:03:26 +02:00
pairs.forEach(p => (set(p, 'pairCount', pairCount)));
pairs.pushObject(
newPair(
2020-04-07 12:28:39 +02:00
this.input.type,
2020-04-01 07:03:26 +02:00
Object.assign(
{},
this.options,
2020-04-07 12:28:39 +02:00
{ index: pairs.length, pairCount }
2020-04-01 07:03:26 +02:00
)
)
);
},
removePair(pair) {
const pairs = this.input.pairs;
const pairCount = pairs.length - 1;
pairs.forEach(p => (set(p, 'pairCount', pairCount)));
pairs.removeObject(pair);
}
}
});