|
| 1 | +/* global document, sequentialWorkflowDesigner */ |
| 2 | + |
| 3 | +const uid = sequentialWorkflowDesigner.Uid.next; |
| 4 | + |
| 5 | +function createTaskStep(name) { |
| 6 | + return { |
| 7 | + id: uid(), |
| 8 | + name, |
| 9 | + componentType: 'task', |
| 10 | + type: 'task', |
| 11 | + properties: {} |
| 12 | + }; |
| 13 | +} |
| 14 | + |
| 15 | +function createIfStep(name, t, f) { |
| 16 | + return { |
| 17 | + id: uid(), |
| 18 | + name, |
| 19 | + componentType: 'switch', |
| 20 | + type: 'switch', |
| 21 | + properties: {}, |
| 22 | + branches: { |
| 23 | + True: t, |
| 24 | + False: f |
| 25 | + } |
| 26 | + }; |
| 27 | +} |
| 28 | + |
| 29 | +const configuration = { |
| 30 | + theme: 'soft', |
| 31 | + undoStackSize: 10, |
| 32 | + steps: { |
| 33 | + iconUrlProvider: () => './assets/icon-trigger.svg' |
| 34 | + }, |
| 35 | + toolbox: { |
| 36 | + groups: [ |
| 37 | + { |
| 38 | + name: 'Steps', |
| 39 | + steps: [createIfStep('If', [], []), createTaskStep('Pear'), createTaskStep('Cucumber'), createTaskStep('Tomato')] |
| 40 | + } |
| 41 | + ] |
| 42 | + }, |
| 43 | + editors: false, |
| 44 | + controlBar: true |
| 45 | +}; |
| 46 | + |
| 47 | +class ChangeDetector { |
| 48 | + constructor(definition, onChange) { |
| 49 | + this.walker = new sequentialWorkflowDesigner.DefinitionWalker(); |
| 50 | + this.lastMap = this.extractMap(definition); |
| 51 | + this.onChange = onChange; |
| 52 | + } |
| 53 | + |
| 54 | + extractMap(definition) { |
| 55 | + const map = new Map(); |
| 56 | + this.walker.forEach(definition, step => map.set(step.id, step.name)); |
| 57 | + return map; |
| 58 | + } |
| 59 | + |
| 60 | + detect(newDefinition) { |
| 61 | + const newMap = this.extractMap(newDefinition); |
| 62 | + const added = []; |
| 63 | + const removed = []; |
| 64 | + for (const [id, name] of newMap) { |
| 65 | + if (!this.lastMap.has(id)) { |
| 66 | + added.push([id, name]); |
| 67 | + } |
| 68 | + } |
| 69 | + for (const [id, name] of this.lastMap) { |
| 70 | + if (!newMap.has(id)) { |
| 71 | + removed.push([id, name]); |
| 72 | + } |
| 73 | + } |
| 74 | + this.lastMap = newMap; |
| 75 | + this.onChange({ added, removed }); |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +const startDefinition = { |
| 80 | + properties: {}, |
| 81 | + sequence: [createTaskStep('Apple'), createIfStep('If', [createTaskStep('Banana')], [createTaskStep('Lemon')])] |
| 82 | +}; |
| 83 | + |
| 84 | +const placeholder = document.getElementById('designer'); |
| 85 | +const recentChanges = document.getElementById('recentChanges'); |
| 86 | +const designer = sequentialWorkflowDesigner.Designer.create(placeholder, startDefinition, configuration); |
| 87 | + |
| 88 | +const detector = new ChangeDetector(startDefinition, changes => { |
| 89 | + let diff = ''; |
| 90 | + if (changes.added.length > 0) { |
| 91 | + diff += `📗 Added: ${changes.added.map(r => r[1]).join(', ')}`; |
| 92 | + } |
| 93 | + if (changes.removed.length > 0) { |
| 94 | + diff += `❌ Removed: ${changes.removed.map(r => r[1]).join(', ')}`; |
| 95 | + } |
| 96 | + if (!diff) { |
| 97 | + diff = 'No changes'; |
| 98 | + } |
| 99 | + recentChanges.textContent = diff; |
| 100 | +}); |
| 101 | +designer.onDefinitionChanged.subscribe(event => detector.detect(event.definition)); |
0 commit comments