Skip to content

Commit 56cf108

Browse files
murichadrai
authored andcommitted
Cleaning error fix (adrai#188)
* Cleaning error fix * Linestyle parsing cycle fix * Dynamic flowState update
1 parent 38acdbd commit 56cf108

File tree

3 files changed

+80
-11
lines changed

3 files changed

+80
-11
lines changed

src/flowchart.chart.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ FlowChart.prototype.render = function() {
170170
FlowChart.prototype.clean = function() {
171171
if (this.paper) {
172172
var paperDom = this.paper.canvas;
173-
paperDom.parentNode.removeChild(paperDom);
173+
paperDom.parentNode && paperDom.parentNode.removeChild(paperDom);
174174
}
175175
};
176176

src/flowchart.parse.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,9 @@ function parse(input) {
119119
},
120120
clean: function() {
121121
this.diagram.clean();
122+
},
123+
options: function() {
124+
return this.diagram.options;
122125
}
123126
};
124127

@@ -311,12 +314,12 @@ function parse(input) {
311314

312315
// line style
313316
var lineStyleSymbols = line.split('@>');
314-
for (var iSS = 0, lenSS = lineStyleSymbols.length; iSS < lenSS; i++) {
315-
if ((iSS + 1) != lenSS) {
317+
for (var iSS = 0, lenSS = lineStyleSymbols.length; iSS < lenSS; iSS++) {
318+
if ((iSS + 1) !== lenSS) {
316319
var curSymb = getSymbol(lineStyleSymbols[iSS]);
317320
var nextSymbol = getSymbol(lineStyleSymbols[iSS+1]);
318321

319-
curSymb['lineStyle'][nextSymbol.key] = JSON.parse(getStyle(lineStyleSymbols[i + 1]));
322+
curSymb['lineStyle'][nextSymbol.key] = JSON.parse(getStyle(lineStyleSymbols[iSS + 1]));
320323
}
321324
}
322325
}

src/jquery-plugin.js

Lines changed: 73 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,79 @@
11
if (typeof jQuery != 'undefined') {
22
var parse = require('./flowchart.parse');
3+
34
(function( $ ) {
4-
$.fn.flowChart = function( options ) {
5-
return this.each(function() {
6-
var $this = $(this);
7-
var chart = parse($this.text());
8-
$this.html('');
9-
chart.drawSVG(this, options);
10-
});
5+
function paramFit(needle, haystack) {
6+
return needle == haystack ||
7+
( Array.isArray(haystack) && (haystack.includes(needle) || haystack.includes(Number(needle)) ))
8+
}
9+
var methods = {
10+
init : function(options) {
11+
return this.each(function() {
12+
var $this = $(this);
13+
this.chart = parse($this.text());
14+
$this.html('');
15+
this.chart.drawSVG(this, options);
16+
});
17+
},
18+
setFlowStateByParam : function(param, paramValue, newFlowState) {
19+
return this.each(function() {
20+
var chart = this.chart;
21+
22+
// @todo this should be part of Symbol API
23+
var nextSymbolKeys = ['next', 'yes', 'no', 'path1', 'path2', 'path3'];
24+
25+
for (var property in chart.symbols) {
26+
if (chart.symbols.hasOwnProperty(property)) {
27+
var symbol = chart.symbols[property];
28+
var val = symbol.params[param];
29+
if (paramFit(val, paramValue)) {
30+
symbol.flowstate = newFlowState;
31+
for (var nextSymbolKey of nextSymbolKeys) {
32+
if (
33+
symbol[nextSymbolKey] &&
34+
symbol[nextSymbolKey]['params'] &&
35+
symbol[nextSymbolKey]['params'][param] &&
36+
paramFit(symbol[nextSymbolKey]['params'][param], paramValue)
37+
) {
38+
symbol.lineStyle[symbol[nextSymbolKey]['key']] = {stroke: chart.options()['flowstate'][newFlowState]['fill']};
39+
}
40+
}
41+
}
42+
}
43+
}
44+
45+
chart.clean();
46+
chart.drawSVG(this);
47+
});
48+
49+
},
50+
clearFlowState: function () {
51+
return this.each(function() {
52+
var chart = this.chart;
53+
54+
for (var property in chart.symbols) {
55+
if (chart.symbols.hasOwnProperty(property)) {
56+
var node = chart.symbols[property];
57+
node.flowstate = '';
58+
}
59+
}
60+
61+
chart.clean();
62+
chart.drawSVG(this);
63+
});
64+
}
1165
};
66+
67+
$.fn.flowChart = function(methodOrOptions) {
68+
if ( methods[methodOrOptions] ) {
69+
return methods[ methodOrOptions ].apply( this, Array.prototype.slice.call( arguments, 1 ));
70+
} else if ( typeof methodOrOptions === 'object' || ! methodOrOptions ) {
71+
// Default to "init"
72+
return methods.init.apply( this, arguments );
73+
} else {
74+
$.error( 'Method ' + methodOrOptions + ' does not exist on jQuery.flowChart' );
75+
}
76+
};
77+
1278
})(jQuery); // eslint-disable-line
1379
}

0 commit comments

Comments
 (0)