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

79 Zeilen
2,2 KiB
Text

2020-04-01 07:03:26 +02:00
import { computed, set } from "@ember/object";
2021-04-12 07:10:02 +02:00
import { alias, equal, not, or } from "@ember/object/computed";
import {
connectorContent,
defaultConnector,
2021-04-12 07:10:02 +02:00
defaultSelectionType,
inputTypesContent,
newPair,
} 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({
classNameBindings: [":mapper-input", "inputType"],
inputType: alias("input.type"),
isConditional: equal("inputType", "conditional"),
isAssignment: equal("inputType", "assignment"),
isAssociation: equal("inputType", "association"),
isValidation: equal("inputType", "validation"),
hasOutput: or("isConditional", "isAssignment"),
hasPairs: or("isConditional", "isAssociation", "isValidation"),
canAddPair: not("isAssignment"),
connectors: computed(function () {
return connectorContent("output", this.input.type, this.options);
}),
inputTypes: computed(function () {
return inputTypesContent(this.options);
}),
@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 10:33:51 +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-07 12:28:39 +02:00
}
2020-04-01 07:03:26 +02:00
const pairs = this.input.pairs;
const pairCount = pairs.length + 1;
pairs.forEach((p) => set(p, "pairCount", pairCount));
2020-04-01 07:03:26 +02:00
pairs.pushObject(
newPair(
this.input.type,
Object.assign({}, this.options, { index: pairs.length, pairCount })
2020-04-01 07:03:26 +02:00
)
);
},
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));
2020-04-01 07:03:26 +02:00
pairs.removeObject(pair);
},
},
2020-04-01 07:03:26 +02:00
});