commit
message
Fixing up naming conventions for mine.js, starting TS version
author
Ben Vogt <[email protected]>
date
2016-12-26 17:59:07
stats
3 file(s) changed,
40 insertions(+),
60 deletions(-)
files
js/TSParser.d.ts
js/mine.js
js/sheet.ts
1diff --git a/js/TSParser.d.ts b/js/TSParser.d.ts
2new file mode 100644
3index 0000000..ebf4c90
4--- /dev/null
5+++ b/js/TSParser.d.ts
6@@ -0,0 +1,12 @@
7+interface P {
8+ yy: any;
9+ lexer: any;
10+ parse: any;
11+ parseError: any;
12+}
13+
14+declare var Parser: P;
15+
16+export {
17+ Parser
18+}
19\ No newline at end of file
20diff --git a/js/mine.js b/js/mine.js
21index 1ea5363..da6a654 100644
22--- a/js/mine.js
23+++ b/js/mine.js
24@@ -1,6 +1,3 @@
25-var storage = {};
26-
27-
28 var mine = (function () {
29 'use strict';
30 var instance = this;
31@@ -128,7 +125,7 @@ var mine = (function () {
32 };
33
34
35- this.updateElementItem = function (id, props) {
36+ this.updateCellItem = function (id, props) {
37 var item = instance.matrix.getItem(id);
38
39 instance.matrix.updateItem(item, props);
40@@ -176,35 +173,35 @@ var mine = (function () {
41 return allDependencies;
42 };
43
44- this.getElementDependencies = function (cell) {
45+ this.getCellDependencies = function (cell) {
46 return instance.matrix.getDependencies(cell.id);
47 };
48
49- var recalculateElementDependencies = function (cell) {
50- var allDependencies = instance.matrix.getElementDependencies(cell);
51+ var recalculateCellDependencies = function (cell) {
52+ var allDependencies = instance.matrix.getCellDependencies(cell);
53
54 allDependencies.forEach(function (refId) {
55 var currentCell = instance.matrix.getItem(refId);
56 if (currentCell && currentCell.formula) {
57- calculateElementFormula(currentCell.formula, currentCell.id);
58+ calculateCellFormula(currentCell.formula, currentCell.id);
59 }
60 });
61 };
62
63- var calculateElementFormula = function (formula, id) {
64+ var calculateCellFormula = function (formula, id) {
65 // to avoid double translate formulas, update item data in parser
66 var parsed = parse(formula, id),
67 value = parsed.result,
68 error = parsed.error;
69
70- instance.matrix.updateElementItem(id, {value: value, error: error});
71+ instance.matrix.updateCellItem(id, {value: value, error: error});
72
73 return parsed;
74 };
75
76- var registerElementInMatrix = function (cell) {
77+ var registerCellInMatrix = function (cell) {
78 instance.matrix.addItem(cell);
79- calculateElementFormula(cell.formula, cell.id);
80+ calculateCellFormula(cell.formula, cell.id);
81 };
82
83 this.scan = function () {
84@@ -223,10 +220,11 @@ var mine = (function () {
85 id: id,
86 formula: input[y][x].toString()
87 };
88- registerElementInMatrix(cell);
89- recalculateElementDependencies(cell);
90+ registerCellInMatrix(cell);
91+ recalculateCellDependencies(cell);
92 }
93 }
94+ console.log(this.data);
95 };
96 };
97
98@@ -538,37 +536,12 @@ var mine = (function () {
99
100 cellValue: function (cell) {
101 var value,
102- fnCellValue = instance.custom.cellValue,
103 element = this,
104 item = instance.matrix.getItem(cell);
105-
106- // check if custom cellValue fn exists
107- if (instance.utils.isFunction(fnCellValue)) {
108-
109- var cellCoords = instance.utils.cellCoords(cell),
110- cellId = instance.utils.translateCellCoords({row: element.row, col: element.col});
111-
112- // get value
113- value = item ? item.value : fnCellValue(cellCoords.row, cellCoords.col);
114-
115- if (instance.utils.isNull(value)) {
116- value = 0;
117- }
118-
119- if (cellId) {
120- //update dependencies
121- instance.matrix.updateItem(cellId, {deps: [cell]});
122- }
123-
124- } else {
125-
126- // get value
127- value = item ? item.value : document.getElementById(cell).value;
128-
129- //update dependencies
130- instance.matrix.updateElementItem(element, {deps: [cell]});
131- }
132-
133+ // get value
134+ value = item ? item.value : "0"; // TODO: fix this, it's sloppy.
135+ //update dependencies
136+ instance.matrix.updateCellItem(element, {deps: [cell]});
137 // check references error
138 if (item && item.deps) {
139 if (item.deps.indexOf(cellId) !== -1) {
140@@ -593,28 +566,15 @@ var mine = (function () {
141 },
142
143 cellRangeValue: function (start, end) {
144- var fnCellValue = instance.custom.cellValue,
145- coordsStart = instance.utils.cellCoords(start),
146+ var coordsStart = instance.utils.cellCoords(start),
147 coordsEnd = instance.utils.cellCoords(end),
148 element = this;
149
150 // iterate cells to get values and indexes
151 var cells = instance.utils.iterateCells.call(this, coordsStart, coordsEnd),
152 result = [];
153-
154- // check if custom cellValue fn exists
155- if (instance.utils.isFunction(fnCellValue)) {
156-
157- var cellId = instance.utils.translateCellCoords({row: element.row, col: element.col});
158-
159- //update dependencies
160- instance.matrix.updateItem(cellId, {deps: cells.index});
161-
162- } else {
163-
164- //update dependencies
165- instance.matrix.updateElementItem(element, {deps: cells.index});
166- }
167+ //update dependencies
168+ instance.matrix.updateCellItem(element, {deps: cells.index});
169
170 result.push(cells.value);
171 return result;
172diff --git a/js/sheet.ts b/js/sheet.ts
173new file mode 100644
174index 0000000..2791be5
175--- /dev/null
176+++ b/js/sheet.ts
177@@ -0,0 +1,3 @@
178+import * as Formula from "../lib/formulajs"
179+/// <reference path="TSParser.d.ts"/>
180+import { Parser } from "./TSParser";