Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame^] | 1 | /*- |
| 2 | * ============LICENSE_START======================================================= |
| 3 | * SDC |
| 4 | * ================================================================================ |
| 5 | * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. |
| 6 | * ================================================================================ |
| 7 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | * you may not use this file except in compliance with the License. |
| 9 | * You may obtain a copy of the License at |
| 10 | * |
| 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | * |
| 13 | * Unless required by applicable law or agreed to in writing, software |
| 14 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | * See the License for the specific language governing permissions and |
| 17 | * limitations under the License. |
| 18 | * ============LICENSE_END========================================================= |
| 19 | */ |
| 20 | |
| 21 | import emptyModel from './emptyModel.json'; |
| 22 | |
| 23 | function mergeLifelines(oldLifelines, newLifelines) { |
| 24 | let oldLifelinesMap = new Map(oldLifelines.map(lifeline => [lifeline.id, lifeline])); |
| 25 | let newLifelinesMap = new Map(newLifelines.map(lifeline => [lifeline.id, lifeline])); |
| 26 | |
| 27 | let updatedLifelines = oldLifelines.map(lifeline => { |
| 28 | let newLifeline = newLifelinesMap.get(lifeline.id); |
| 29 | return { |
| 30 | ...lifeline, |
| 31 | name: newLifeline ? newLifeline.name : lifeline.name |
| 32 | }; |
| 33 | }); |
| 34 | |
| 35 | let addedLifelines = newLifelines.filter(lifeline => !oldLifelinesMap.has(lifeline.id)); |
| 36 | |
| 37 | return [ |
| 38 | ...updatedLifelines, |
| 39 | ...addedLifelines |
| 40 | ]; |
| 41 | } |
| 42 | |
| 43 | |
| 44 | const SequenceDiagramModelHelper = Object.freeze({ |
| 45 | |
| 46 | createModel(options) { |
| 47 | return SequenceDiagramModelHelper.updateModel(emptyModel, options); |
| 48 | }, |
| 49 | |
| 50 | updateModel(model, options) { |
| 51 | const diagram = model.diagram; |
| 52 | const metadata = diagram.metadata || model.metadata; |
| 53 | const id = options.id || metadata.id; |
| 54 | const name = options.name || metadata.name; |
| 55 | const lifelines = options.lifelines ? mergeLifelines(diagram.lifelines, options.lifelines) : diagram.lifelines; |
| 56 | |
| 57 | return { |
| 58 | diagram: { |
| 59 | ...diagram, |
| 60 | metadata: { |
| 61 | ...metadata, |
| 62 | id, |
| 63 | name |
| 64 | }, |
| 65 | lifelines |
| 66 | } |
| 67 | }; |
| 68 | } |
| 69 | }); |
| 70 | |
| 71 | export default SequenceDiagramModelHelper; |