/
value.js
203 lines (183 loc) · 3.93 KB
/
value.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
import { ERROR_REF } from 'hot-formula-parser';
import { arrayFilter } from '../../../helpers/array';
import BaseCell from './_base';
const STATE_OUT_OFF_DATE = 1;
const STATE_COMPUTING = 2;
const STATE_UP_TO_DATE = 3;
const states = [STATE_OUT_OFF_DATE, STATE_COMPUTING, STATE_UP_TO_DATE];
/**
* Class responsible for wrapping formula expression. It contains calculated value of
* the formula, an error if it has happened and cell references which indicates a relationship with regular
* cells. This object uses physical cell coordinates.
*
* @class CellValue
* @util
*/
class CellValue extends BaseCell {
/**
* Out of date state indicates cells ready for recomputing.
*
* @returns {Number}
*/
static get STATE_OUT_OFF_DATE() {
return 1; // PhantomJS crashes when we want to use constant above
}
/**
* Computing state indicates cells under processing.
*
* @returns {Number}
*/
static get STATE_COMPUTING() {
return 2; // PhantomJS crashes when we want to use constant above
}
/**
* Up to date state indicates cells with fresh computed value.
*
* @returns {Number}
*/
static get STATE_UP_TO_DATE() {
return 3; // PhantomJS crashes when we want to use constant above
}
constructor(row, column) {
super(row, column);
/**
* List of precedents cells.
*
* @type {Array}
*/
this.precedents = [];
/**
* Computed value.
*
* @type {*}
*/
this.value = null;
/**
* Error name.
*
* @type {String|null}
*/
this.error = null;
/**
* Indicates cell state.
*
* @type {String}
*/
this.state = CellValue.STATE_UP_TO_DATE;
}
/**
* Set computed value.
*
* @param {*} value
*/
setValue(value) {
this.value = value;
}
/**
* Get computed value.
*
* @returns {*}
*/
getValue() {
return this.value;
}
/**
* Set error message for this cell.
*
* @param {String} error Error name.
*/
setError(error) {
this.error = error;
}
/**
* Get error name for this cell.
*
* @returns {String|null}
*/
getError() {
return this.error;
}
/**
* Check if cell value is marked as error.
*
* @returns {Boolean}
*/
hasError() {
return this.error !== null;
}
/**
* Set cell state.
*
* @param {Number} state Cell state.
*/
setState(state) {
if (states.indexOf(state) === -1) {
throw Error(`Unrecognized state: ${state}`);
}
this.state = state;
}
/**
* Check cell state.
*
* @returns {Boolean}
*/
isState(state) {
return this.state === state;
}
/**
* Add precedent cell to the collection.
*
* @param {CellReference} cellReference Cell reference object.
*/
addPrecedent(cellReference) {
if (this.isEqual(cellReference)) {
throw Error(ERROR_REF);
}
if (!this.hasPrecedent(cellReference)) {
this.precedents.push(cellReference);
}
}
/**
* Remove precedent cell from the collection.
*
* @param {CellReference} cellReference Cell reference object.
*/
removePrecedent(cellReference) {
if (this.isEqual(cellReference)) {
throw Error(ERROR_REF);
}
this.precedents = arrayFilter(this.precedents, cell => !cell.isEqual(cellReference));
}
/**
* Clear all precedent cells.
*/
clearPrecedents() {
this.precedents.length = 0;
}
/**
* Get precedent cells.
*
* @returns {Array}
*/
getPrecedents() {
return this.precedents;
}
/**
* Check if cell value has precedents cells.
*
* @returns {Boolean}
*/
hasPrecedents() {
return this.precedents.length > 0;
}
/**
* Check if cell reference is precedents this cell.
*
* @param {CellReference} cellReference Cell reference object.
* @returns {Boolean}
*/
hasPrecedent(cellReference) {
return arrayFilter(this.precedents, cell => cell.isEqual(cellReference)).length > 0;
}
}
export default CellValue;