-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathapp.js
326 lines (294 loc) · 9.04 KB
/
app.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
var app = angular.module('app', ['$jsPlumb']);
app.directive('start', function (jsPlumbFactory) {
return jsPlumbFactory.node({
templateUrl: "start_template.tpl",
link: function (scope, element) {
element.addClass("flowchart-object flowchart-start");
}
});
});
app.directive('question', function (jsPlumbFactory) {
return jsPlumbFactory.node({
inherit: ["removeNode", "editNode"],
templateUrl: "question_template.tpl",
link: function (scope, element) {
element.addClass("flowchart-object flowchart-question");
}
});
});
app.directive('action', function (jsPlumbFactory) {
return jsPlumbFactory.node({
inherit: ["removeNode", "editNode"],
templateUrl: "action_template.tpl",
link: function (scope, element) {
element.addClass("flowchart-object flowchart-action");
}
});
});
app.directive('output', function (jsPlumbFactory) {
return jsPlumbFactory.node({
inherit: ["removeNode", "editNode"],
templateUrl: "output_template.tpl",
link: function (scope, element) {
element.addClass("flowchart-object flowchart-output");
}
});
});
app.controller("DemoController", function ($log, $scope, jsPlumbService) {
var ctrl = this;
// toolkit id
var toolkitId = "flowchartToolkit";
var toolkit;
window.jsps = jsPlumbService;
window.ctrl = this;
// ---------------------------- operations on nodes, edges ---------------------------------------------------------
var _editLabel = function (edge) {
jsPlumbToolkit.Dialogs.show({
id: "dlgText",
data: {
text: edge.data.label || ""
},
onOK: function (data) {
toolkit.updateEdge(edge, { label: data.text });
}
});
};
// ---------------------------- / operations on nodes, edges ---------------------------------------------------------
//
// scope contains
// jtk - the toolkit
// surface - the surface
//
// element is the DOM element into which the toolkit was rendered
//
this.init = function (scope, element, attrs) {
toolkit = scope.toolkit;
toolkit.load({
url: "data/data.json"
});
// -------------- configure buttons --------------------------------
var controls = element[0].querySelector(".controls");
// pan mode/select mode
jsPlumb.on(controls, "tap", "[mode]", function () {
scope.surface.setMode(this.getAttribute("mode"));
});
// on home button click, zoom content to fit.
jsPlumb.on(controls, "tap", "[reset]", function () {
scope.toolkit.clearSelection();
scope.surface.zoomToFit();
});
// configure Drawing tools. This is an optional include.
new jsPlumbToolkit.DrawingTools({
renderer: scope.surface
});
// initialize dialogs
jsPlumbToolkit.Dialogs.initialize({
selector: ".dlg"
});
//
// any operation that caused a data update (and would have caused an autosave), fires a dataUpdated event.
//
new jsPlumbSyntaxHighlighter(toolkit, ".jtk-demo-dataset");
};
// ----------------------------- data for the app ----------------------------------------------------------
$scope.nodeTypes = [
{ label: "Question", type: "question" },
{ label: "Action", type: "action" },
{ label: "Output", type: "output" }
];
$scope.removeNode = function (node) {
var info = this.surface.getObjectInfo(node);
jsPlumbToolkit.Dialogs.show({
id: "dlgConfirm",
data: {
msg: "Delete '" + info.obj.data.text + "'"
},
onOK: function () {
toolkit.removeNode(info.obj);
}
});
};
$scope.editNode = function (node) {
// getObjectInfo is a method that takes some DOM element (this function's `this` is
// set to the element that fired the event) and returns the toolkit data object that
// relates to the element. it ascends through parent nodes until it finds a node that is
// registered with the toolkit.
var info = this.surface.getObjectInfo(node);
jsPlumbToolkit.Dialogs.show({
id: "dlgText",
data: info.obj.data,
title: "Edit " + info.obj.type + " name",
onOK: function (data) {
if (data.text && data.text.length > 2) {
// if name is at least 2 chars long, update the underlying data and
// update the UI.
toolkit.updateNode(info.obj, data);
}
}
});
};
// -------------------------------- render parameters ---------------------------------------------------
this.typeExtractor = function (el) {
return el.getAttribute("jtk-node-type");
};
/**
* This is wired into the jsplumb-palette, and called whenever a new node is dropped onto the surface.
* @param node
*/
this.onDrop = function (node) {
console.log("A node was dropped: " + node.id);
};
var nodeDimensions = {
question: { w: 120, h: 120 },
action: { w: 120, h: 70 },
start: { w: 50, h: 50 },
output: { w: 120, h: 70 }
};
this.toolkitParams = {
nodeFactory: function (type, data, callback) {
jsPlumbToolkit.Dialogs.show({
id: "dlgText",
title: "Enter " + type + " name:",
onOK: function (d) {
data.text = d.text;
// if the user entered a name...
if (data.text) {
// and it was at least 2 chars
if (data.text.length >= 2) {
// set width and height.
jsPlumb.extend(data, nodeDimensions[type]);
// set an id and continue.
data.id = jsPlumbToolkitUtil.uuid();
callback(data);
}
else
// else advise the user.
alert(type + " names must be at least 2 characters!");
}
// else...do not proceed.
}
});
},
beforeStartConnect: function (node, edgeType) {
return { label: "..." };
}
};
this.renderParams = {
view: {
nodes: {
"start": { template: "start" },
"selectable": {
events: {
tap: function (params) {
toolkit.toggleSelection(params.node);
}
}
},
"question": {
parent: "selectable",
template: "question"
},
"action": {
parent: "selectable",
template: "action"
},
"output": {
parent: "selectable",
template: "output"
}
},
// There are two edge types defined - 'yes' and 'no', sharing a common
// parent.
edges: {
"default": {
anchor: "AutoDefault",
endpoint: "Blank",
connector: ["Flowchart", { cornerRadius: 5 }],
paintStyle: { strokeWidth: 2, stroke: "#f76258", outlineWidth: 3, outlineStroke: "transparent" }, // paint style for this edge type.
hoverPaintStyle: { strokeWidth: 2, stroke: "rgb(67,67,67)" }, // hover paint style for this edge type.
events: {
"dblclick": function (params) {
jsPlumbToolkit.Dialogs.show({
id: "dlgConfirm",
data: {
msg: "Delete Edge"
},
onOK: function () {
toolkit.removeEdge(params.edge);
}
});
}
},
overlays: [
["Arrow", { location: 1, width: 10, length: 10 }],
["Arrow", { location: 0.3, width: 10, length: 10 }]
]
},
"connection": {
parent: "default",
overlays: [
[
"Label", {
label: "${label}",
events: {
click: function (params) {
_editLabel(params.edge);
}
}
}
]
]
}
},
ports: {
"start": {
endpoint: "Blank",
anchor: "Continuous",
uniqueEndpoint: true,
edgeType: "default"
},
"source": {
endpoint: "Blank",
paintStyle: { fill: "#84acb3" },
anchor: "AutoDefault",
maxConnections: -1,
edgeType: "connection"
},
"target": {
maxConnections: -1,
endpoint: "Blank",
anchor: "AutoDefault",
paintStyle: { fill: "#84acb3" },
isTarget: true
}
}
},
// Layout the nodes using an absolute layout
layout: {
type: "Absolute"
},
events: {
canvasClick: function (e) {
toolkit.clearSelection();
},
edgeAdded: function (params) {
if (params.addedByMouse) {
_editLabel(params.edge);
}
},
// listener for mode change on renderer.
modeChanged: function (mode) {
var controls = document.querySelector(".controls");
jsPlumb.removeClass(controls.querySelectorAll("[mode]"), "selected-mode");
jsPlumb.addClass(controls.querySelectorAll("[mode='" + mode + "']"), "selected-mode");
},
click: function (e) {
alert(e)
}
},
consumeRightClick: false,
dragOptions: {
filter: ".jtk-draw-handle, .node-action, .node-action i"
}
};
});