commit
message
Cleaning up unused functions
author
Ben Vogt <[email protected]>
date
2016-12-24 20:23:58
stats
1 file(s) changed,
10 insertions(+),
547 deletions(-)
files
js/ruleJS.js
1diff --git a/js/ruleJS.js b/js/ruleJS.js
2index 71f2b18..dba5e58 100644
3--- a/js/ruleJS.js
4+++ b/js/ruleJS.js
5@@ -1,26 +1,9 @@
6 var ruleJS = (function (root) {
7 'use strict';
8-
9- /**
10- * object instance
11- */
12 var instance = this;
13
14- /**
15- * root element
16- */
17 var rootElement = document.getElementById(root) || null;
18
19- /**
20- * current version
21- * @type {string}
22- */
23- var version = '0.0.1';
24-
25- /**
26- * parser object delivered by jison library
27- * @type {Parser|*|{}}
28- */
29 var parser = {};
30
31 var FormulaParser = function(handler) {
32@@ -39,12 +22,6 @@ var ruleJS = (function (root) {
33 };
34
35 newParser.yy.parseError = function (str, hash) {
36-// if (!((hash.expected && hash.expected.indexOf("';'") >= 0) &&
37-// (hash.token === "}" || hash.token === "EOF" ||
38-// parser.newLine || parser.wasNewLine)))
39-// {
40-// throw new SyntaxError(hash);
41-// }
42 throw {
43 name: 'Parser error',
44 message: str,
45@@ -57,14 +34,7 @@ var ruleJS = (function (root) {
46 return newParser;
47 };
48
49- /**
50- * Exception object
51- * @type {{errors: {type: string, output: string}[], get: get}}
52- */
53 var Exception = {
54- /**
55- * error types
56- */
57 errors: [
58 {type: 'NULL', output: '#NULL'},
59 {type: 'DIV_ZERO', output: '#DIV/0!'},
60@@ -75,11 +45,6 @@ var ruleJS = (function (root) {
61 {type: 'NOT_AVAILABLE', output: '#N/A!'},
62 {type: 'ERROR', output: '#ERROR'}
63 ],
64- /**
65- * get error by type
66- * @param {String} type
67- * @returns {*}
68- */
69 get: function (type) {
70 var error = Exception.errors.filter(function (item) {
71 return item.type === type || item.output === type;
72@@ -89,34 +54,19 @@ var ruleJS = (function (root) {
73 }
74 };
75
76- /**
77- * matrix collection for each form, contains cache of all form element
78- */
79 var Matrix = function () {
80
81- /**
82- * single item (cell) object
83- * @type {{id: string, formula: string, value: string, error: string, deps: Array, formulaEdit: boolean}}
84- */
85- var item = {
86- id: '',
87- formula: '',
88- value: '',
89- error: '',
90- deps: [],
91- formulaEdit: false
92- };
93+ // var item = {
94+ // id: '',
95+ // formula: '',
96+ // value: '',
97+ // error: '',
98+ // deps: [],
99+ // formulaEdit: false
100+ // };
101
102- /**
103- * array of items
104- * @type {Array}
105- */
106 this.data = [];
107
108- /**
109- * form elements, which can be parsed
110- * @type {string[]}
111- */
112 var formElements = ['input[type=text]', '[data-formula]'];
113
114 var listen = function () {
115@@ -128,72 +78,18 @@ var ruleJS = (function (root) {
116 }
117 };
118
119- /**
120- * get item from data array
121- * @param {String} id
122- * @returns {*}
123- */
124 this.getItem = function (id) {
125 return instance.matrix.data.filter(function (item) {
126 return item.id === id;
127 })[0];
128 };
129
130- /**
131- * remove item from data array
132- * @param {String} id
133- */
134 this.removeItem = function (id) {
135 instance.matrix.data = instance.matrix.data.filter(function (item) {
136 return item.id !== id;
137 });
138 };
139
140- /**
141- * remove items from data array in col
142- * @param {Number} col
143- */
144- this.removeItemsInCol = function (col) {
145- instance.matrix.data = instance.matrix.data.filter(function (item) {
146- return item.col !== col;
147- });
148- };
149-
150- /**
151- * remove items from data array in row
152- * @param {Number} row
153- */
154- this.removeItemsInRow = function (row) {
155- instance.matrix.data = instance.matrix.data.filter(function (item) {
156- return item.row !== row;
157- })
158- };
159-
160- /**
161- * remove items from data array below col
162- * @param col
163- */
164- this.removeItemsBelowCol = function (col) {
165- instance.matrix.data = instance.matrix.data.filter(function (item) {
166- return item.col < col;
167- });
168- };
169-
170- /**
171- * remove items from data array below row
172- * @param row
173- */
174- this.removeItemsBelowRow = function (row) {
175- instance.matrix.data = instance.matrix.data.filter(function (item) {
176- return item.row < row;
177- })
178- };
179-
180- /**
181- * update item properties
182- * @param {Object|String} item or id
183- * @param {Object} props
184- */
185 this.updateItem = function (item, props) {
186 if (instance.utils.isString(item)) {
187 item = instance.matrix.getItem(item);
188@@ -221,10 +117,6 @@ var ruleJS = (function (root) {
189 }
190 };
191
192- /**
193- * add item to data array
194- * @param {Object} item
195- */
196 this.addItem = function (item) {
197 var cellId = item.id,
198 coords = instance.utils.cellCoords(cellId);
199@@ -245,65 +137,7 @@ var ruleJS = (function (root) {
200 return instance.matrix.getItem(cellId);
201 };
202
203- /**
204- * get references items to column
205- * @param {Number} col
206- * @returns {Array}
207- */
208- this.getRefItemsToColumn = function (col) {
209- var result = [];
210-
211- if (!instance.matrix.data.length) {
212- return result;
213- }
214-
215- instance.matrix.data.forEach(function (item) {
216- if (item.deps) {
217- var deps = item.deps.filter(function (cell) {
218-
219- var alpha = instance.utils.getCellAlphaNum(cell).alpha,
220- num = instance.utils.toNum(alpha);
221-
222- return num >= col;
223- });
224-
225- if (deps.length > 0 && result.indexOf(item.id) === -1) {
226- result.push(item.id);
227- }
228- }
229- });
230-
231- return result;
232- };
233-
234- this.getRefItemsToRow = function (row) {
235- var result = [];
236-
237- if (!instance.matrix.data.length) {
238- return result;
239- }
240-
241- instance.matrix.data.forEach(function (item) {
242- if (item.deps) {
243- var deps = item.deps.filter(function (cell) {
244- var num = instance.utils.getCellAlphaNum(cell).num;
245- return num > row;
246- });
247
248- if (deps.length > 0 && result.indexOf(item.id) === -1) {
249- result.push(item.id);
250- }
251- }
252- });
253-
254- return result;
255- };
256-
257- /**
258- * update element item properties in data array
259- * @param {Element} element
260- * @param {Object} props
261- */
262 this.updateElementItem = function (element, props) {
263 var id = element.getAttribute('id'),
264 item = instance.matrix.getItem(id);
265@@ -311,17 +145,7 @@ var ruleJS = (function (root) {
266 instance.matrix.updateItem(item, props);
267 };
268
269- /**
270- * get cell dependencies
271- * @param {String} id
272- * @returns {Array}
273- */
274 this.getDependencies = function (id) {
275- /**
276- * get dependencies by element
277- * @param {String} id
278- * @returns {Array}
279- */
280 var getDependencies = function (id) {
281 var filtered = instance.matrix.data.filter(function (cell) {
282 if (cell.deps) {
283@@ -341,10 +165,6 @@ var ruleJS = (function (root) {
284
285 var allDependencies = [];
286
287- /**
288- * get total dependencies
289- * @param {String} id
290- */
291 var getTotalDependencies = function (id) {
292 var deps = getDependencies(id);
293
294@@ -367,19 +187,10 @@ var ruleJS = (function (root) {
295 return allDependencies;
296 };
297
298- /**
299- * get total element cell dependencies
300- * @param {Element} element
301- * @returns {Array}
302- */
303 this.getElementDependencies = function (element) {
304 return instance.matrix.getDependencies(element.getAttribute('id'));
305 };
306
307- /**
308- * recalculate refs cell
309- * @param {Element} element
310- */
311 var recalculateElementDependencies = function (element) {
312 var allDependencies = instance.matrix.getElementDependencies(element),
313 id = element.getAttribute('id');
314@@ -393,12 +204,6 @@ var ruleJS = (function (root) {
315 });
316 };
317
318- /**
319- * calculate element formula
320- * @param {String} formula
321- * @param {Element} element
322- * @returns {Object}
323- */
324 var calculateElementFormula = function (formula, element) {
325 // to avoid double translate formulas, update item data in parser
326 var parsed = parse(formula, element),
327@@ -497,29 +302,9 @@ var ruleJS = (function (root) {
328 });
329 };
330
331- this.depsInFormula = function (item) {
332-
333- var formula = item.formula,
334- deps = item.deps;
335-
336- if (deps) {
337- deps = deps.filter(function (id) {
338- return formula.indexOf(id) !== -1;
339- });
340-
341- return deps.length > 0;
342- }
343-
344- return false;
345- };
346-
347- /**
348- * scan the form and build the calculation matrix
349- */
350 this.scan = function () {
351 var $totalElements = rootElement.querySelectorAll(formElements);
352
353- // iterate through elements contains specified attributes
354 [].slice.call($totalElements).forEach(function ($item) {
355 registerElementInMatrix($item);
356 registerElementEvents($item);
357@@ -527,88 +312,35 @@ var ruleJS = (function (root) {
358 };
359 };
360
361- /**
362- * utils methods
363- * @type {{isArray: isArray, toNum: toNum, toChar: toChar, cellCoords: cellCoords}}
364- */
365 var utils = {
366- /**
367- * check if value is array
368- * @param value
369- * @returns {boolean}
370- */
371 isArray: function (value) {
372 return Object.prototype.toString.call(value) === '[object Array]';
373 },
374
375- /**
376- * check if value is number
377- * @param value
378- * @returns {boolean}
379- */
380 isNumber: function (value) {
381 return Object.prototype.toString.call(value) === '[object Number]';
382 },
383
384- /**
385- * check if value is string
386- * @param value
387- * @returns {boolean}
388- */
389 isString: function (value) {
390 return Object.prototype.toString.call(value) === '[object String]';
391 },
392
393- /**
394- * check if value is function
395- * @param value
396- * @returns {boolean}
397- */
398 isFunction: function (value) {
399 return Object.prototype.toString.call(value) === '[object Function]';
400 },
401
402- /**
403- * check if value is undefined
404- * @param value
405- * @returns {boolean}
406- */
407 isUndefined: function (value) {
408 return Object.prototype.toString.call(value) === '[object Undefined]';
409 },
410
411- /**
412- * check if value is null
413- * @param value
414- * @returns {boolean}
415- */
416 isNull: function (value) {
417 return Object.prototype.toString.call(value) === '[object Null]';
418 },
419
420- /**
421- * check if value is set
422- * @param value
423- * @returns {boolean}
424- */
425 isSet: function (value) {
426 return !instance.utils.isUndefined(value) && !instance.utils.isNull(value);
427 },
428
429- /**
430- * check if value is cell
431- * @param {String} value
432- * @returns {Boolean}
433- */
434- isCell: function (value) {
435- return value.match(/^[A-Za-z]+[0-9]+/) ? true : false;
436- },
437-
438- /**
439- * get row name and column number
440- * @param cell
441- * @returns {{alpha: string, num: number}}
442- */
443 getCellAlphaNum: function (cell) {
444 var num = cell.match(/\d+$/),
445 alpha = cell.replace(num, '');
446@@ -619,144 +351,7 @@ var ruleJS = (function (root) {
447 }
448 },
449
450- /**
451- * change row cell index A1 -> A2
452- * @param {String} cell
453- * @param {Number} counter
454- * @returns {String}
455- */
456- changeRowIndex: function (cell, counter) {
457- var alphaNum = instance.utils.getCellAlphaNum(cell),
458- alpha = alphaNum.alpha,
459- col = alpha,
460- row = parseInt(alphaNum.num + counter, 10);
461-
462- if (row < 1) {
463- row = 1;
464- }
465-
466- return col + '' + row;
467- },
468-
469- /**
470- * change col cell index A1 -> B1 Z1 -> AA1
471- * @param {String} cell
472- * @param {Number} counter
473- * @returns {String}
474- */
475- changeColIndex: function (cell, counter) {
476- var alphaNum = instance.utils.getCellAlphaNum(cell),
477- alpha = alphaNum.alpha,
478- col = instance.utils.toChar(parseInt(instance.utils.toNum(alpha) + counter, 10)),
479- row = alphaNum.num;
480-
481- if (!col || col.length === 0) {
482- col = 'A';
483- }
484-
485- var fixedCol = alpha[0] === '$' || false,
486- fixedRow = alpha[alpha.length - 1] === '$' || false;
487-
488- col = (fixedCol ? '$' : '') + col;
489- row = (fixedRow ? '$' : '') + row;
490-
491- return col + '' + row;
492- },
493-
494-
495- changeFormula: function (formula, delta, change) {
496- if (!delta) {
497- delta = 1;
498- }
499-
500- return formula.replace(/(\$?[A-Za-z]+\$?[0-9]+)/g, function (match) {
501- var alphaNum = instance.utils.getCellAlphaNum(match),
502- alpha = alphaNum.alpha,
503- num = alphaNum.num;
504-
505- if (instance.utils.isNumber(change.col)) {
506- num = instance.utils.toNum(alpha);
507-
508- if (change.col <= num) {
509- return instance.utils.changeColIndex(match, delta);
510- }
511- }
512-
513- if (instance.utils.isNumber(change.row)) {
514- if (change.row < num) {
515- return instance.utils.changeRowIndex(match, delta);
516- }
517- }
518-
519- return match;
520- });
521- },
522-
523- /**
524- * update formula cells
525- * @param {String} formula
526- * @param {String} direction
527- * @param {Number} delta
528- * @returns {String}
529- */
530- updateFormula: function (formula, direction, delta) {
531- var type,
532- counter;
533-
534- // left, right -> col
535- if (['left', 'right'].indexOf(direction) !== -1) {
536- type = 'col';
537- } else if (['up', 'down'].indexOf(direction) !== -1) {
538- type = 'row'
539- }
540-
541- // down, up -> row
542- if (['down', 'right'].indexOf(direction) !== -1) {
543- counter = delta * 1;
544- } else if(['up', 'left'].indexOf(direction) !== -1) {
545- counter = delta * (-1);
546- }
547-
548- if (type && counter) {
549- return formula.replace(/(\$?[A-Za-z]+\$?[0-9]+)/g, function (match) {
550-
551- var alpha = instance.utils.getCellAlphaNum(match).alpha;
552-
553- var fixedCol = alpha[0] === '$' || false,
554- fixedRow = alpha[alpha.length - 1] === '$' || false;
555-
556- if (type === 'row' && fixedRow) {
557- return match;
558- }
559-
560- if (type === 'col' && fixedCol) {
561- return match;
562- }
563-
564- return (type === 'row' ? instance.utils.changeRowIndex(match, counter) : instance.utils.changeColIndex(match, counter));
565- });
566- }
567-
568- return formula;
569- },
570-
571- /**
572- * convert string char to number e.g A => 0, Z => 25, AA => 27
573- * @param {String} chr
574- * @returns {Number}
575- */
576 toNum: function (chr) {
577-// chr = instance.utils.clearFormula(chr).split('');
578-//
579-// var base = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"],
580-// i, j, result = 0;
581-//
582-// for (i = 0, j = chr.length - 1; i < chr.length; i += 1, j -= 1) {
583-// result += Math.pow(base.length, j) * (base.indexOf(chr[i]));
584-// }
585-//
586-// return result;
587-
588 chr = instance.utils.clearFormula(chr);
589 var base = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', i, j, result = 0;
590
591@@ -771,11 +366,6 @@ var ruleJS = (function (root) {
592 return result;
593 },
594
595- /**
596- * convert number to string char, e.g 0 => A, 25 => Z, 26 => AA
597- * @param {Number} num
598- * @returns {String}
599- */
600 toChar: function (num) {
601 var s = '';
602
603@@ -787,11 +377,6 @@ var ruleJS = (function (root) {
604 return s.toUpperCase();
605 },
606
607- /**
608- * get cell coordinates
609- * @param {String} cell A1
610- * @returns {{row: Number, col: number}}
611- */
612 cellCoords: function (cell) {
613 var num = cell.match(/\d+$/),
614 alpha = cell.replace(num, '');
615@@ -802,31 +387,14 @@ var ruleJS = (function (root) {
616 };
617 },
618
619- /**
620- * remove $ from formula
621- * @param {String} formula
622- * @returns {String|void}
623- */
624 clearFormula: function (formula) {
625 return formula.replace(/\$/g, '');
626 },
627
628- /**
629- * translate cell coordinates to merged form {row:0, col:0} -> A1
630- * @param coords
631- * @returns {string}
632- */
633 translateCellCoords: function (coords) {
634 return instance.utils.toChar(coords.col) + '' + parseInt(coords.row + 1, 10);
635 },
636
637- /**
638- * iterate cell range and get theirs indexes and values
639- * @param {Object} startCell ex.: {row:1, col: 1}
640- * @param {Object} endCell ex.: {row:10, col: 1}
641- * @param {Function=} callback
642- * @returns {{index: Array, value: Array}}
643- */
644 iterateCells: function (startCell, endCell, callback) {
645 var result = {
646 index: [], // list of cell index: A1, A2, A3
647@@ -891,14 +459,7 @@ var ruleJS = (function (root) {
648 }
649 };
650
651- /**
652- * helper with methods using by parser
653- * @type {{number: number, numberInverted: numberInverted, mathMatch: mathMatch, callFunction: callFunction}}
654- */
655 var helper = {
656- /**
657- * list of supported formulas
658- */
659 SUPPORTED_FORMULAS: [
660 'ABS', 'ACCRINT', 'ACOS', 'ACOSH', 'ACOTH', 'AND', 'ARABIC', 'ASIN', 'ASINH', 'ATAN', 'ATAN2', 'ATANH', 'AVEDEV', 'AVERAGE', 'AVERAGEA', 'AVERAGEIF',
661 'BASE', 'BESSELI', 'BESSELJ', 'BESSELK', 'BESSELY', 'BETADIST', 'BETAINV', 'BIN2DEC', 'BIN2HEX', 'BIN2OCT', 'BINOMDIST', 'BINOMDISTRANGE', 'BINOMINV', 'BITAND', 'BITLSHIFT', 'BITOR', 'BITRSHIFT', 'BITXOR',
662@@ -918,11 +479,6 @@ var ruleJS = (function (root) {
663 'XOR'
664 ],
665
666- /**
667- * get number
668- * @param {Number|String} num
669- * @returns {Number}
670- */
671 number: function (num) {
672 switch (typeof num) {
673 case 'number':
674@@ -936,31 +492,14 @@ var ruleJS = (function (root) {
675 return num;
676 },
677
678- /**
679- * get string
680- * @param {Number|String} str
681- * @returns {string}
682- */
683 string: function (str) {
684 return str.substring(1, str.length - 1);
685 },
686
687- /**
688- * invert number
689- * @param num
690- * @returns {Number}
691- */
692 numberInverted: function (num) {
693 return this.number(num) * (-1);
694 },
695
696- /**
697- * match special operation
698- * @param {String} type
699- * @param {String} exp1
700- * @param {String} exp2
701- * @returns {*}
702- */
703 specialMatch: function (type, exp1, exp2) {
704 var result;
705
706@@ -972,13 +511,6 @@ var ruleJS = (function (root) {
707 return result;
708 },
709
710- /**
711- * match logic operation
712- * @param {String} type
713- * @param {String|Number} exp1
714- * @param {String|Number} exp2
715- * @returns {Boolean} result
716- */
717 logicMatch: function (type, exp1, exp2) {
718 var result;
719
720@@ -1015,13 +547,6 @@ var ruleJS = (function (root) {
721 return result;
722 },
723
724- /**
725- * match math operation
726- * @param {String} type
727- * @param {Number} number1
728- * @param {Number} number2
729- * @returns {*}
730- */
731 mathMatch: function (type, number1, number2) {
732 var result;
733
734@@ -1058,12 +583,6 @@ var ruleJS = (function (root) {
735 return result;
736 },
737
738- /**
739- * call function from formula
740- * @param {String} fn
741- * @param {Array} args
742- * @returns {*}
743- */
744 callFunction: function (fn, args) {
745 fn = fn.toUpperCase();
746 args = args || [];
747@@ -1077,11 +596,6 @@ var ruleJS = (function (root) {
748 throw Error('NAME');
749 },
750
751- /**
752- * get variable from formula
753- * @param {Array} args
754- * @returns {*}
755- */
756 callVariable: function (args) {
757 args = args || [];
758 var str = args[0];
759@@ -1096,11 +610,6 @@ var ruleJS = (function (root) {
760 throw Error('NAME');
761 },
762
763- /**
764- * Get cell value
765- * @param {String} cell => A1 AA1
766- * @returns {*}
767- */
768 cellValue: function (cell) {
769 var value,
770 fnCellValue = instance.custom.cellValue,
771@@ -1157,12 +666,6 @@ var ruleJS = (function (root) {
772 throw Error('NOT_AVAILABLE');
773 },
774
775- /**
776- * Get cell range values
777- * @param {String} start cell A1
778- * @param {String} end cell B3
779- * @returns {Array}
780- */
781 cellRangeValue: function (start, end) {
782 var fnCellValue = instance.custom.cellValue,
783 coordsStart = instance.utils.cellCoords(start),
784@@ -1191,22 +694,11 @@ var ruleJS = (function (root) {
785 return result;
786 },
787
788- /**
789- * Get fixed cell value
790- * @param {String} id
791- * @returns {*}
792- */
793 fixedCellValue: function (id) {
794 id = id.replace(/\$/g, '');
795 return instance.helper.cellValue.call(this, id);
796 },
797
798- /**
799- * Get fixed cell range values
800- * @param {String} start
801- * @param {String} end
802- * @returns {Array}
803- */
804 fixedCellRangeValue: function (start, end) {
805 start = start.replace(/\$/g, '');
806 end = end.replace(/\$/g, '');
807@@ -1215,12 +707,6 @@ var ruleJS = (function (root) {
808 }
809 };
810
811- /**
812- * parse input string using parser
813- * @returns {Object} {{error: *, result: *}}
814- * @param formula
815- * @param element
816- */
817 var parse = function (formula, element) {
818 var result = null,
819 error = null;
820@@ -1259,11 +745,6 @@ var ruleJS = (function (root) {
821 } else {
822 error = Exception.get('ERROR');
823 }
824-
825- //console.debug(ex.prop);
826- //debugger;
827- //error = ex.message;
828- //error = Exception.get('ERROR');
829 }
830
831 return {
832@@ -1272,9 +753,6 @@ var ruleJS = (function (root) {
833 }
834 };
835
836- /**
837- * initial method, create formulas, parser and matrix objects
838- */
839 var init = function () {
840 instance = this;
841
842@@ -1292,7 +770,6 @@ var ruleJS = (function (root) {
843
844 return {
845 init: init,
846- version: version,
847 utils: utils,
848 helper: helper,
849 parse: parse