/
ElementFactory.js
111 lines (100 loc) · 2.05 KB
/
ElementFactory.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import {
create
} from '../model';
import { assign } from 'min-dash';
/**
* @typedef {import('../model/Types').Element} Element
* @typedef {import('../model/Types').Connection} Connection
* @typedef {import('../model/Types').Label} Label
* @typedef {import('../model/Types').Root} Root
* @typedef {import('../model/Types').Shape} Shape
*/
/**
* A factory for model elements.
*
* @template {Connection} [T=Connection]
* @template {Label} [U=Label]
* @template {Root} [V=Root]
* @template {Shape} [W=Shape]
*/
export default function ElementFactory() {
this._uid = 12;
}
/**
* Create a root element.
*
* @param {Partial<Root>} [attrs]
*
* @return {V} The created root element.
*/
ElementFactory.prototype.createRoot = function(attrs) {
return this.create('root', attrs);
};
/**
* Create a label.
*
* @param {Partial<Label>} [attrs]
*
* @return {U} The created label.
*/
ElementFactory.prototype.createLabel = function(attrs) {
return this.create('label', attrs);
};
/**
* Create a shape.
*
* @param {Partial<Shape>} [attrs]
*
* @return {W} The created shape.
*/
ElementFactory.prototype.createShape = function(attrs) {
return this.create('shape', attrs);
};
/**
* Create a connection.
*
* @param {Partial<Connection>} [attrs]
*
* @return {T} The created connection.
*/
ElementFactory.prototype.createConnection = function(attrs) {
return this.create('connection', attrs);
};
/**
* Create a root element.
*
* @overlord
* @param {'root'} type
* @param {Partial<Root>} [attrs]
* @return {V}
*/
/**
* Create a shape.
*
* @overlord
* @param {'shape'} type
* @param {Partial<Shape>} [attrs]
* @return {W}
*/
/**
* Create a connection.
*
* @overlord
* @param {'connection'} type
* @param {Partial<Connection>} [attrs]
* @return {T}
*/
/**
* Create a label.
*
* @param {'label'} type
* @param {Partial<Label>} [attrs]
* @return {U}
*/
ElementFactory.prototype.create = function(type, attrs) {
attrs = assign({}, attrs || {});
if (!attrs.id) {
attrs.id = type + '_' + (this._uid++);
}
return create(type, attrs);
};