UNPKG

stitch-ui

Version:

106 lines (92 loc) 2.91 kB
import { List, Record } from "immutable"; import PipelineStage from "./PipelineStage"; export const PIPELINE_OUTPUT_SINGLE_DOC = "singleDoc"; export const PIPELINE_OUTPUT_BOOLEAN = "boolean"; export const PIPELINE_OUTPUT_ARRAY = "array"; const defaultNewStageArgs = { items: [{ x: 1, y: "foo" }] }; export default class Pipeline extends Record({ stages: new List(), stageEditStates: new List(), dirty: false, output: PIPELINE_OUTPUT_ARRAY }) { toRaw() { return { pipeline: this.stages.map(p => p.toRaw()).toArray(), output: this.output }; } /** * Adds a stage to this pipeline. Newly added stages default to a built-in, * literal action stage. The stageEditStates list is a parallel list of stage * data that stores the edit state for the corresponding stage in the stages * list. When a new stage is added, there are no edits; this is represented by * adding a null to the stageEditStates list. */ addStage() { const newStage = new PipelineStage({ action: "literal", args: defaultNewStageArgs }); return this.set("dirty", true) .set("stages", this.stages.push(newStage)) .set("stageEditStates", this.stageEditStates.push(null)); } removeStage(index) { return this.set("dirty", true) .removeIn(["stages", index]) .removeIn(["stageEditStates", index]); } moveStage(fromIndex, toIndex) { return this.set("dirty", true) .update("stages", s => s.remove(fromIndex).insert(toIndex, s.get(fromIndex)) ) .update("stageEditStates", s => s.remove(fromIndex).insert(toIndex, s.get(fromIndex)) ); } setOutputType(outputType) { return this.set("dirty", true).set("outputType", outputType); } setStage(index, stage) { return this.set("dirty", true) .setIn(["stages", index], stage) .setIn(["stageEditStates", index], stage.toEditState()); } applyEditChange(index, mutator) { return this.setIn( ["stageEditStates", index], mutator( this.stageEditStates.get(index) || this.stages.get(index).toEditState() ).set("dirty", true) ); } startEditing(index) { return this.setIn( ["stageEditStates", index], this.stages.get(index).toEditState() ); } endEditing(index) { return this.setIn(["stageEditStates", index], null); } get error() { return this.stageEditStates.reduce( (acc, editState) => acc || (editState && editState.error), null ); } save() { const pipeline = this; return this.stageEditStates.reduce((acc, editState, i) => { if (editState) { const parsedEditState = editState.parseInput(); return parsedEditState.error ? acc.setIn(["stageEditStates", i], parsedEditState) : acc.setIn(["stages", i], parsedEditState.toStage()); } return acc; }, pipeline); } }