2024-11-22 17:11:51 +01:00
|
|
|
import { A } from "@ember/array";
|
|
|
|
import Component from "@ember/component";
|
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";
|
2024-11-22 17:11:51 +01:00
|
|
|
import { observes } from "discourse-common/utils/decorators";
|
2021-03-28 11:06:49 +02:00
|
|
|
import {
|
|
|
|
connectorContent,
|
|
|
|
defaultConnector,
|
2021-04-12 07:10:02 +02:00
|
|
|
defaultSelectionType,
|
|
|
|
inputTypesContent,
|
|
|
|
newPair,
|
2021-03-28 11:06:49 +02:00
|
|
|
} from "../lib/wizard-mapper";
|
2020-04-01 07:03:26 +02:00
|
|
|
|
2020-04-05 03:37:09 +02:00
|
|
|
export default Component.extend({
|
2021-03-28 11:06:49 +02:00
|
|
|
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)) {
|
2021-03-28 11:06:49 +02:00
|
|
|
this.send("addPair");
|
2020-04-07 10:33:51 +02:00
|
|
|
}
|
2021-03-28 11:06:49 +02:00
|
|
|
|
2020-04-29 03:42:39 +02:00
|
|
|
if (this.hasOutput) {
|
2021-03-28 11:06:49 +02:00
|
|
|
this.set("input.output", null);
|
|
|
|
|
2020-04-29 03:42:39 +02:00
|
|
|
if (!this.input.outputConnector) {
|
|
|
|
const options = this.options;
|
2021-03-28 11:06:49 +02:00
|
|
|
this.set("input.output_type", defaultSelectionType("output", options));
|
|
|
|
this.set(
|
|
|
|
"input.output_connector",
|
|
|
|
defaultConnector("output", this.inputType, options)
|
|
|
|
);
|
2020-04-29 03:42:39 +02:00
|
|
|
}
|
2020-04-07 12:28:39 +02:00
|
|
|
}
|
2020-04-07 10:33:51 +02:00
|
|
|
},
|
2021-03-28 11:06:49 +02:00
|
|
|
|
2020-04-01 07:03:26 +02:00
|
|
|
actions: {
|
|
|
|
addPair() {
|
2020-04-07 12:28:39 +02:00
|
|
|
if (!this.input.pairs) {
|
2021-03-28 11:06:49 +02:00
|
|
|
this.set("input.pairs", A());
|
2020-04-07 12:28:39 +02:00
|
|
|
}
|
2021-03-28 11:06:49 +02:00
|
|
|
|
2020-04-01 07:03:26 +02:00
|
|
|
const pairs = this.input.pairs;
|
|
|
|
const pairCount = pairs.length + 1;
|
2021-03-28 11:06:49 +02:00
|
|
|
|
|
|
|
pairs.forEach((p) => set(p, "pairCount", pairCount));
|
|
|
|
|
2020-04-01 07:03:26 +02:00
|
|
|
pairs.pushObject(
|
|
|
|
newPair(
|
2021-03-28 11:06:49 +02:00
|
|
|
this.input.type,
|
|
|
|
Object.assign({}, this.options, { index: pairs.length, pairCount })
|
2020-04-01 07:03:26 +02:00
|
|
|
)
|
|
|
|
);
|
|
|
|
},
|
2021-03-28 11:06:49 +02:00
|
|
|
|
2020-04-01 07:03:26 +02:00
|
|
|
removePair(pair) {
|
|
|
|
const pairs = this.input.pairs;
|
|
|
|
const pairCount = pairs.length - 1;
|
2021-03-28 11:06:49 +02:00
|
|
|
|
|
|
|
pairs.forEach((p) => set(p, "pairCount", pairCount));
|
2020-04-01 07:03:26 +02:00
|
|
|
pairs.removeObject(pair);
|
2021-03-28 11:06:49 +02:00
|
|
|
},
|
|
|
|
},
|
2020-04-01 07:03:26 +02:00
|
|
|
});
|