|
1 | 1 | if (typeof jQuery != 'undefined') {
|
2 | 2 | var parse = require('./flowchart.parse');
|
| 3 | + |
3 | 4 | (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 | + } |
11 | 65 | };
|
| 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 | + |
12 | 78 | })(jQuery); // eslint-disable-line
|
13 | 79 | }
|
0 commit comments