@omegagrid/grid-core 0.2.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (185) hide show
  1. package/dist/calculations/autofill.d.ts +7 -0
  2. package/dist/calculations/autofill.d.ts.map +1 -0
  3. package/dist/calculations/autofill.js +81 -0
  4. package/dist/calculations/autofill.js.map +1 -0
  5. package/dist/calculations/calculationModel.d.ts +35 -0
  6. package/dist/calculations/calculationModel.d.ts.map +1 -0
  7. package/dist/calculations/calculationModel.js +199 -0
  8. package/dist/calculations/calculationModel.js.map +1 -0
  9. package/dist/calculations/conditions.d.ts +4 -0
  10. package/dist/calculations/conditions.d.ts.map +1 -0
  11. package/dist/calculations/conditions.js +46 -0
  12. package/dist/calculations/conditions.js.map +1 -0
  13. package/dist/calculations/context.d.ts +53 -0
  14. package/dist/calculations/context.d.ts.map +1 -0
  15. package/dist/calculations/context.js +83 -0
  16. package/dist/calculations/context.js.map +1 -0
  17. package/dist/calculations/formula.d.ts +69 -0
  18. package/dist/calculations/formula.d.ts.map +1 -0
  19. package/dist/calculations/formula.js +360 -0
  20. package/dist/calculations/formula.js.map +1 -0
  21. package/dist/calculations/functions/dates.d.ts +6 -0
  22. package/dist/calculations/functions/dates.d.ts.map +1 -0
  23. package/dist/calculations/functions/dates.js +28 -0
  24. package/dist/calculations/functions/dates.js.map +1 -0
  25. package/dist/calculations/functions/helpers.d.ts +11 -0
  26. package/dist/calculations/functions/helpers.d.ts.map +1 -0
  27. package/dist/calculations/functions/helpers.js +38 -0
  28. package/dist/calculations/functions/helpers.js.map +1 -0
  29. package/dist/calculations/functions/index.d.ts +5 -0
  30. package/dist/calculations/functions/index.d.ts.map +1 -0
  31. package/dist/calculations/functions/index.js +73 -0
  32. package/dist/calculations/functions/index.js.map +1 -0
  33. package/dist/calculations/functions/logical.d.ts +10 -0
  34. package/dist/calculations/functions/logical.d.ts.map +1 -0
  35. package/dist/calculations/functions/logical.js +49 -0
  36. package/dist/calculations/functions/logical.js.map +1 -0
  37. package/dist/calculations/functions/lookup.d.ts +17 -0
  38. package/dist/calculations/functions/lookup.d.ts.map +1 -0
  39. package/dist/calculations/functions/lookup.js +167 -0
  40. package/dist/calculations/functions/lookup.js.map +1 -0
  41. package/dist/calculations/functions/math.d.ts +62 -0
  42. package/dist/calculations/functions/math.d.ts.map +1 -0
  43. package/dist/calculations/functions/math.js +228 -0
  44. package/dist/calculations/functions/math.js.map +1 -0
  45. package/dist/calculations/functions/special.d.ts +2 -0
  46. package/dist/calculations/functions/special.d.ts.map +1 -0
  47. package/dist/calculations/functions/special.js +55 -0
  48. package/dist/calculations/functions/special.js.map +1 -0
  49. package/dist/calculations/functions/statistical.d.ts +5 -0
  50. package/dist/calculations/functions/statistical.d.ts.map +1 -0
  51. package/dist/calculations/functions/statistical.js +47 -0
  52. package/dist/calculations/functions/statistical.js.map +1 -0
  53. package/dist/calculations/functions/text.d.ts +5 -0
  54. package/dist/calculations/functions/text.d.ts.map +1 -0
  55. package/dist/calculations/functions/text.js +23 -0
  56. package/dist/calculations/functions/text.js.map +1 -0
  57. package/dist/calculations/index.d.ts +6 -0
  58. package/dist/calculations/index.d.ts.map +1 -0
  59. package/dist/calculations/index.js +22 -0
  60. package/dist/calculations/index.js.map +1 -0
  61. package/dist/calculations/operators.d.ts +19 -0
  62. package/dist/calculations/operators.d.ts.map +1 -0
  63. package/dist/calculations/operators.js +66 -0
  64. package/dist/calculations/operators.js.map +1 -0
  65. package/dist/calculations/tokenization.d.ts +34 -0
  66. package/dist/calculations/tokenization.d.ts.map +1 -0
  67. package/dist/calculations/tokenization.js +192 -0
  68. package/dist/calculations/tokenization.js.map +1 -0
  69. package/dist/comments/comments.d.ts +37 -0
  70. package/dist/comments/comments.d.ts.map +1 -0
  71. package/dist/comments/comments.js +39 -0
  72. package/dist/comments/comments.js.map +1 -0
  73. package/dist/comments/index.d.ts +2 -0
  74. package/dist/comments/index.d.ts.map +1 -0
  75. package/dist/comments/index.js +18 -0
  76. package/dist/comments/index.js.map +1 -0
  77. package/dist/common/index.d.ts +2 -0
  78. package/dist/common/index.d.ts.map +1 -0
  79. package/dist/common/index.js +18 -0
  80. package/dist/common/index.js.map +1 -0
  81. package/dist/common/range.d.ts +65 -0
  82. package/dist/common/range.d.ts.map +1 -0
  83. package/dist/common/range.js +220 -0
  84. package/dist/common/range.js.map +1 -0
  85. package/dist/constants.d.ts +60 -0
  86. package/dist/constants.d.ts.map +1 -0
  87. package/dist/constants.js +75 -0
  88. package/dist/constants.js.map +1 -0
  89. package/dist/formatting/conditionalFormats.d.ts +34 -0
  90. package/dist/formatting/conditionalFormats.d.ts.map +1 -0
  91. package/dist/formatting/conditionalFormats.js +107 -0
  92. package/dist/formatting/conditionalFormats.js.map +1 -0
  93. package/dist/formatting/index.d.ts +2 -0
  94. package/dist/formatting/index.d.ts.map +1 -0
  95. package/dist/formatting/index.js +18 -0
  96. package/dist/formatting/index.js.map +1 -0
  97. package/dist/index.d.ts +9 -0
  98. package/dist/index.d.ts.map +1 -0
  99. package/dist/index.js +30 -0
  100. package/dist/index.js.map +1 -0
  101. package/dist/model/cellModel.d.ts +105 -0
  102. package/dist/model/cellModel.d.ts.map +1 -0
  103. package/dist/model/cellModel.js +251 -0
  104. package/dist/model/cellModel.js.map +1 -0
  105. package/dist/model/columnModel.d.ts +132 -0
  106. package/dist/model/columnModel.d.ts.map +1 -0
  107. package/dist/model/columnModel.js +147 -0
  108. package/dist/model/columnModel.js.map +1 -0
  109. package/dist/model/columnSorting.d.ts +50 -0
  110. package/dist/model/columnSorting.d.ts.map +1 -0
  111. package/dist/model/columnSorting.js +164 -0
  112. package/dist/model/columnSorting.js.map +1 -0
  113. package/dist/model/cssStyle.d.ts +18 -0
  114. package/dist/model/cssStyle.d.ts.map +1 -0
  115. package/dist/model/cssStyle.js +197 -0
  116. package/dist/model/cssStyle.js.map +1 -0
  117. package/dist/model/dependencies.d.ts +28 -0
  118. package/dist/model/dependencies.d.ts.map +1 -0
  119. package/dist/model/dependencies.js +129 -0
  120. package/dist/model/dependencies.js.map +1 -0
  121. package/dist/model/elementModel.d.ts +98 -0
  122. package/dist/model/elementModel.d.ts.map +1 -0
  123. package/dist/model/elementModel.js +109 -0
  124. package/dist/model/elementModel.js.map +1 -0
  125. package/dist/model/filterModel.d.ts +22 -0
  126. package/dist/model/filterModel.d.ts.map +1 -0
  127. package/dist/model/filterModel.js +83 -0
  128. package/dist/model/filterModel.js.map +1 -0
  129. package/dist/model/gridModel.d.ts +172 -0
  130. package/dist/model/gridModel.d.ts.map +1 -0
  131. package/dist/model/gridModel.js +639 -0
  132. package/dist/model/gridModel.js.map +1 -0
  133. package/dist/model/groupModel.d.ts +23 -0
  134. package/dist/model/groupModel.d.ts.map +1 -0
  135. package/dist/model/groupModel.js +102 -0
  136. package/dist/model/groupModel.js.map +1 -0
  137. package/dist/model/history.d.ts +21 -0
  138. package/dist/model/history.d.ts.map +1 -0
  139. package/dist/model/history.js +39 -0
  140. package/dist/model/history.js.map +1 -0
  141. package/dist/model/index.d.ts +17 -0
  142. package/dist/model/index.d.ts.map +1 -0
  143. package/dist/model/index.js +33 -0
  144. package/dist/model/index.js.map +1 -0
  145. package/dist/model/iterableRange.d.ts +32 -0
  146. package/dist/model/iterableRange.d.ts.map +1 -0
  147. package/dist/model/iterableRange.js +79 -0
  148. package/dist/model/iterableRange.js.map +1 -0
  149. package/dist/model/namingModel.d.ts +24 -0
  150. package/dist/model/namingModel.d.ts.map +1 -0
  151. package/dist/model/namingModel.js +75 -0
  152. package/dist/model/namingModel.js.map +1 -0
  153. package/dist/model/options.d.ts +56 -0
  154. package/dist/model/options.d.ts.map +1 -0
  155. package/dist/model/options.js +27 -0
  156. package/dist/model/options.js.map +1 -0
  157. package/dist/model/rowModel.d.ts +91 -0
  158. package/dist/model/rowModel.d.ts.map +1 -0
  159. package/dist/model/rowModel.js +137 -0
  160. package/dist/model/rowModel.js.map +1 -0
  161. package/dist/model/selectionModel.d.ts +20 -0
  162. package/dist/model/selectionModel.d.ts.map +1 -0
  163. package/dist/model/selectionModel.js +45 -0
  164. package/dist/model/selectionModel.js.map +1 -0
  165. package/dist/model/style.d.ts +73 -0
  166. package/dist/model/style.d.ts.map +1 -0
  167. package/dist/model/style.js +38 -0
  168. package/dist/model/style.js.map +1 -0
  169. package/dist/model/styleModel.d.ts +20 -0
  170. package/dist/model/styleModel.d.ts.map +1 -0
  171. package/dist/model/styleModel.js +85 -0
  172. package/dist/model/styleModel.js.map +1 -0
  173. package/dist/sparklines/index.d.ts +2 -0
  174. package/dist/sparklines/index.d.ts.map +1 -0
  175. package/dist/sparklines/index.js +18 -0
  176. package/dist/sparklines/index.js.map +1 -0
  177. package/dist/sparklines/sparklines.d.ts +61 -0
  178. package/dist/sparklines/sparklines.d.ts.map +1 -0
  179. package/dist/sparklines/sparklines.js +185 -0
  180. package/dist/sparklines/sparklines.js.map +1 -0
  181. package/dist/types.d.ts +27 -0
  182. package/dist/types.d.ts.map +1 -0
  183. package/dist/types.js +9 -0
  184. package/dist/types.js.map +1 -0
  185. package/package.json +56 -0
@@ -0,0 +1,639 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.GridModel = void 0;
4
+ const core_1 = require("@omegagrid/core");
5
+ const calculationModel_1 = require("../calculations/calculationModel");
6
+ const comments_1 = require("../comments");
7
+ const range_1 = require("../common/range");
8
+ const formatting_1 = require("../formatting");
9
+ const sparklines_1 = require("../sparklines/sparklines");
10
+ const columnModel_1 = require("./columnModel");
11
+ const iterableRange_1 = require("./iterableRange");
12
+ const namingModel_1 = require("./namingModel");
13
+ const options_1 = require("./options");
14
+ const rowModel_1 = require("./rowModel");
15
+ const styleModel_1 = require("./styleModel");
16
+ class GridModel {
17
+ get comments() { return this._comments; }
18
+ get calculations() { return this._calculations; }
19
+ get names() { return this._names; }
20
+ get sparklines() { return this._sparklines; }
21
+ get conditionalFormats() { return this._conditionalFormats; }
22
+ get mergedHeaders() { return this._mergedHeaders; }
23
+ get mergedCells() { return this._mergedCells; }
24
+ get hiddenCells() { return this._hiddenCells; }
25
+ get hasFilter() { return this._filter; }
26
+ get hasRowTree() { return this._maxRowLevel > 0 && this.options.rowTree !== false; }
27
+ get maxRowLevel() { return this._maxRowLevel; }
28
+ get hasColumnTree() { return this._maxColumnLevel > 0; }
29
+ get maxColumnLevel() { return this._maxColumnLevel; }
30
+ get headerCount() { return this._headerCount; }
31
+ get columnHeaderCount() { return this.headerCount + (this.hasColumnTree ? 1 : 0) + (this.hasFilter ? 1 : 0); }
32
+ get properties() { return this._properties; }
33
+ set properties(v) { this._properties = v; }
34
+ constructor(source, options, logger) {
35
+ this.styles = new styleModel_1.StyleModel(this);
36
+ this._comments = new comments_1.Comments(this);
37
+ this._calculations = new calculationModel_1.CalculationModel(this);
38
+ this._names = new namingModel_1.NamingModel(this);
39
+ this._sparklines = new sparklines_1.Sparklines(this);
40
+ this._conditionalFormats = new formatting_1.ConditionalFormats(this);
41
+ this._hiddenCells = new Map();
42
+ this._filter = false;
43
+ this._maxRowLevel = 0;
44
+ this._maxColumnLevel = 0;
45
+ this._headerCount = 0;
46
+ this._levelBasedRowTree = true;
47
+ this._levelBasedColTree = true;
48
+ this.clearRows = () => {
49
+ this.rowRoot.clearChildren();
50
+ this.rows = [];
51
+ return this.rows;
52
+ };
53
+ this.clearColumns = () => this.columns = [];
54
+ this.logger = logger;
55
+ this.options = options !== null && options !== void 0 ? options : (0, options_1.initOptions)(source.options);
56
+ this.populate(source);
57
+ }
58
+ getConnectedModel(name) {
59
+ return this.group ? this.group.get(name) : null;
60
+ }
61
+ getCellByA1(a1) {
62
+ return this.getCellByRange(range_1.CellRange.fromA1(a1));
63
+ }
64
+ getCellByRange(r) {
65
+ return this.rows[r.r1].cell(r.c1);
66
+ }
67
+ createRows(source, root = new rowModel_1.RowModel(this, { l: -1 }), indexOffset = 0) {
68
+ let hasRowLevelBasedTree = false;
69
+ let hasRowParentBasedTree = false;
70
+ const rows = source.map((rowSource, r) => {
71
+ if (!rowSource)
72
+ rowSource = {};
73
+ if (!isNaN(rowSource.l))
74
+ hasRowLevelBasedTree = true;
75
+ if (rowSource.p != null)
76
+ hasRowParentBasedTree = true;
77
+ const rowModel = new rowModel_1.RowModel(this, rowSource);
78
+ rowModel.origIndex = r + indexOffset;
79
+ if (rowModel.name)
80
+ this.names.set(rowModel.name, range_1.CellRange.fullRow(r));
81
+ return rowModel;
82
+ });
83
+ let maxRowLevel = -1;
84
+ if (hasRowLevelBasedTree)
85
+ maxRowLevel = core_1.tree.buildLevelBasedTree(rows, root);
86
+ else if (hasRowParentBasedTree)
87
+ maxRowLevel = core_1.tree.buildParentBasedTree(rows, root);
88
+ else
89
+ root.children = rows;
90
+ return [root, rows, maxRowLevel, hasRowLevelBasedTree];
91
+ }
92
+ insertRowData(parent, offset, source) {
93
+ const res = this.createRows(source, new rowModel_1.RowModel(this, { l: parent.level }), offset);
94
+ this._maxRowLevel = Math.max(this._maxRowLevel, res[2]);
95
+ parent.insertChildren(res[0].children, offset);
96
+ this.indexRowsByTree();
97
+ }
98
+ updateHeaders() {
99
+ this._mergedHeaders = [];
100
+ this.columns.forEach(column => {
101
+ column.mergedHeaders.forEach(h => { var _a, _b; return this._mergedHeaders.push({ h: h, c: column.index, r: h.r, cs: (_a = h.cs) !== null && _a !== void 0 ? _a : 1, rs: (_b = h.rs) !== null && _b !== void 0 ? _b : 1 }); });
102
+ });
103
+ this.calcHeaderHeights();
104
+ }
105
+ removeHeader(r) {
106
+ this.columns.forEach(column => {
107
+ if (r < column.header.length)
108
+ column.header.splice(r, 1);
109
+ });
110
+ this.updateHeaders();
111
+ }
112
+ populate(source) {
113
+ var _a, _b;
114
+ this.index = source.index;
115
+ this.name = (_a = source.name) !== null && _a !== void 0 ? _a : this.name;
116
+ let hasColLevelBasedTree = false;
117
+ let hasColParentBasedTree = false;
118
+ if (!source.columns)
119
+ source.columns = new Array(this.options.columns).fill({});
120
+ if (!source.rows)
121
+ source.rows = new Array(this.options.rows).fill({});
122
+ // styles
123
+ if (source.styles)
124
+ this.styles.populate(source.styles);
125
+ // columns
126
+ this._mergedHeaders = [];
127
+ this.columns = source.columns.map((columnSource, c) => {
128
+ if (!columnSource)
129
+ columnSource = {};
130
+ if (columnSource.l > 0)
131
+ hasColLevelBasedTree = true;
132
+ if (columnSource.p != null)
133
+ hasColParentBasedTree = true;
134
+ const colModel = new columnModel_1.ColumnModel(this, columnSource);
135
+ colModel.mergedHeaders.forEach(h => { var _a, _b; return this._mergedHeaders.push({ h: h, c: c, r: h.r, cs: (_a = h.cs) !== null && _a !== void 0 ? _a : 1, rs: (_b = h.rs) !== null && _b !== void 0 ? _b : 1 }); });
136
+ this._filter = colModel.filter || this._filter;
137
+ if (colModel.name)
138
+ this.names.set(colModel.name, range_1.CellRange.fullColumn(c));
139
+ return colModel;
140
+ });
141
+ this._levelBasedColTree = hasColLevelBasedTree;
142
+ // hidden header cells
143
+ this._mergedHeaders.forEach(h => {
144
+ if (h.h.hidden)
145
+ return;
146
+ for (let c = h.c; c < h.c + h.cs; c++) {
147
+ for (let r = h.r; r < h.r + h.rs; r++) {
148
+ if ((r > h.r || c > h.c) && this.columns[c] && this.columns[c].header[r]) {
149
+ this.columns[c].header[r].hidden = true;
150
+ }
151
+ }
152
+ }
153
+ });
154
+ this._maxColumnLevel = -1;
155
+ this.colRoot = new columnModel_1.ColumnModel(this, { l: -1 });
156
+ if (hasColLevelBasedTree)
157
+ this._maxColumnLevel = core_1.tree.buildLevelBasedTree(this.columns, this.colRoot);
158
+ else if (hasColParentBasedTree)
159
+ this._maxColumnLevel = core_1.tree.buildParentBasedTree(this.columns, this.colRoot);
160
+ else
161
+ this.colRoot.children = this.columns;
162
+ this.indexColumns();
163
+ this.indexVisibleColumns();
164
+ // rows
165
+ [this.rowRoot, this.rows, this._maxRowLevel, this._levelBasedRowTree] = this.createRows(source.rows);
166
+ this.indexRows();
167
+ this.indexVisibleRows();
168
+ if (this.options.pageSize && this.options.pageSize <= this.rows.length)
169
+ this.rowRoot.count = Infinity;
170
+ // cells
171
+ this._mergedCells = [];
172
+ (_b = source.cells) === null || _b === void 0 ? void 0 : _b.forEach((cellSource, i) => {
173
+ var _a;
174
+ const cell = this.rows[cellSource.r].cell(cellSource.c, cellSource);
175
+ if (!cell) {
176
+ (_a = this.logger) === null || _a === void 0 ? void 0 : _a.error(`invalid cell definition (index ${i}): ${JSON.stringify(cellSource)}`);
177
+ }
178
+ else if (cell.colspan > 1 || cell.rowspan > 1) {
179
+ this._mergedCells.push({ r: cell.r, c: cell.c, rs: cell.rowspan, cs: cell.colspan });
180
+ }
181
+ });
182
+ this.indexHiddenCells();
183
+ // sparklines
184
+ if (source.sparklines)
185
+ this.sparklines.populate(source.sparklines);
186
+ // conditional formats
187
+ if (source.rules)
188
+ this.conditionalFormats.populate(source.rules);
189
+ // comments
190
+ if (source.comments)
191
+ this.comments.populate(source.comments);
192
+ // names
193
+ if (source.names)
194
+ this.names.populate(source.names);
195
+ this.calcHeaderHeights();
196
+ }
197
+ getSourceData() {
198
+ const data = {
199
+ index: this.index,
200
+ name: this.name,
201
+ tabColor: this.tabColor,
202
+ columns: this.columns.map(col => col.getSourceData()),
203
+ rows: this.rows.map(row => row.getSourceData()),
204
+ styles: this.styles.getSourceData(),
205
+ rules: this.conditionalFormats.getSourceData(),
206
+ sparklines: this.sparklines.getSourceData()
207
+ };
208
+ const cells = [];
209
+ let cellData;
210
+ for (let r = 0; r < this.rows.length; r++) {
211
+ for (let c = 0; c < this.columns.length; c++) {
212
+ cellData = this.rows[r].cell(c).getSourceData();
213
+ if (Object.keys(cellData).length > 0) {
214
+ cellData.r = r;
215
+ cellData.c = c;
216
+ cells.push(cellData);
217
+ }
218
+ }
219
+ }
220
+ if (cells.length > 0)
221
+ data.cells = cells;
222
+ const styles = [];
223
+ this.styles.clean();
224
+ this.styles.map.forEach(style => styles.push(style));
225
+ if (styles.length > 0)
226
+ data.styles = styles;
227
+ return data;
228
+ }
229
+ calcHeaderHeights() {
230
+ this._headerCount = 0;
231
+ this._headerHeights = [];
232
+ this.columns.forEach((column) => {
233
+ var _a;
234
+ this._headerCount = column.header ? column.header.length : this._headerCount;
235
+ if (this._headerHeights.length < this._headerCount) {
236
+ this._headerHeights.push(...Array(this._headerCount - this._headerHeights.length).fill(0));
237
+ }
238
+ (_a = column.header) === null || _a === void 0 ? void 0 : _a.forEach((h, r) => {
239
+ var _a;
240
+ if (r >= this._headerHeights.length) {
241
+ this._headerHeights.push(0);
242
+ }
243
+ this._headerHeights[r] = Math.max(this._headerHeights[r], (_a = h.height) !== null && _a !== void 0 ? _a : 0);
244
+ });
245
+ });
246
+ }
247
+ indexRows() {
248
+ this.visibleRows = [];
249
+ this.rows.forEach((row, r) => {
250
+ row.index = r;
251
+ if (!row.isLoaded)
252
+ row.collapsed = true;
253
+ if (!row.hidden && row.filterState != core_1.tree.FilterState.Filtered)
254
+ this.visibleRows.push(row);
255
+ });
256
+ }
257
+ indexVisibleRows() {
258
+ this.visibleRows = [];
259
+ let r = 0;
260
+ if (this._levelBasedRowTree) {
261
+ this.rowRoot.walkVisible((node) => {
262
+ node.visibleIndex = r++;
263
+ this.visibleRows.push(node);
264
+ });
265
+ }
266
+ else {
267
+ // in parent based node rows do not have to respect tree order
268
+ this.rowRoot.walkVisible((node) => this.visibleRows[node.index] = node);
269
+ this.visibleRows = this.visibleRows.filter(row => {
270
+ if (row) {
271
+ row.visibleIndex = r++;
272
+ return true;
273
+ }
274
+ return false;
275
+ });
276
+ }
277
+ }
278
+ indexRowsByTree() {
279
+ this.rows = [];
280
+ let r = 0;
281
+ this.rowRoot.walk((row) => {
282
+ row.index = r++;
283
+ this.rows.push(row);
284
+ });
285
+ this.indexVisibleRows();
286
+ }
287
+ indexColumns() {
288
+ this.visibleColumns = [];
289
+ this.columns.forEach((column, c) => {
290
+ column.index = c;
291
+ if (!column.hidden)
292
+ this.visibleColumns.push(column);
293
+ });
294
+ }
295
+ indexVisibleColumns() {
296
+ this.visibleColumns = [];
297
+ this.visibleColumnIndex = {};
298
+ let r = 0;
299
+ this.colRoot.walkVisible((node) => {
300
+ node.visibleIndex = r;
301
+ this.visibleColumnIndex[node.index] = r++;
302
+ this.visibleColumns.push(node);
303
+ });
304
+ }
305
+ clear() {
306
+ this.clearRows();
307
+ this.clearColumns();
308
+ this.resetViewport();
309
+ }
310
+ resetViewport() {
311
+ this._viewport = null;
312
+ return this;
313
+ }
314
+ getViewport(offset, viewportSize, freeze) {
315
+ const viewportRows = [];
316
+ const viewportColumns = [];
317
+ let row;
318
+ let remainingHeight = viewportSize.h;
319
+ let r = 0;
320
+ // top rows
321
+ for (r = 0; r < freeze.t; r++) {
322
+ row = this.visibleRows[r];
323
+ remainingHeight -= row.height;
324
+ viewportRows.push(row);
325
+ }
326
+ // bottom rows
327
+ for (r = this.visibleRows.length - freeze.b; r < this.visibleRows.length; r++) {
328
+ row = this.visibleRows[r];
329
+ remainingHeight -= row.height;
330
+ }
331
+ // center rows
332
+ r = freeze.t + offset.r;
333
+ while (remainingHeight > 0 && r < this.visibleRows.length) {
334
+ row = this.visibleRows[r++];
335
+ remainingHeight -= row.height;
336
+ viewportRows.push(row);
337
+ }
338
+ // bottom rows again
339
+ for (r = this.visibleRows.length - freeze.b; r < this.visibleRows.length; r++) {
340
+ viewportRows.push(this.visibleRows[r]);
341
+ }
342
+ let column;
343
+ let remainingWidth = viewportSize.w;
344
+ let c = 0;
345
+ // left columns
346
+ for (c = 0; c < freeze.l; c++) {
347
+ column = this.visibleColumns[c];
348
+ remainingWidth -= column.width;
349
+ viewportColumns.push(column);
350
+ }
351
+ // right columns
352
+ for (c = this.visibleColumns.length - freeze.r; c < this.visibleColumns.length; c++) {
353
+ column = this.visibleColumns[c];
354
+ remainingWidth -= column.width;
355
+ }
356
+ // center columns
357
+ c = freeze.l + offset.c;
358
+ while (remainingWidth > 0 && c < this.visibleColumns.length) {
359
+ column = this.visibleColumns[c++];
360
+ remainingWidth -= column.width;
361
+ viewportColumns.push(column);
362
+ }
363
+ // right columns again
364
+ for (c = this.visibleColumns.length - freeze.r; c < this.visibleColumns.length; c++) {
365
+ viewportColumns.push(this.visibleColumns[c]);
366
+ }
367
+ return (this._viewport = { rows: viewportRows, columns: viewportColumns });
368
+ }
369
+ calculateViewport() {
370
+ return this._viewport ? this.calculations.calculateViewport(this._viewport) : [];
371
+ }
372
+ getValuesInRange(range, evaluate = true) {
373
+ range = range.intersect(this.gridRange);
374
+ if (!range)
375
+ return [];
376
+ const { columns, rows } = range.size;
377
+ const values = new Array(rows);
378
+ let i = 0, j = 0;
379
+ let row;
380
+ for (let r = range.r1; r <= range.r2; r++) {
381
+ values[i] = new Array(columns);
382
+ j = 0;
383
+ row = this.rows[r];
384
+ for (let c = range.c1; c <= range.c2; c++) {
385
+ values[i][j++] = evaluate ? this.calculations.evaluate(row.cell(c)) : row.cell(c).value;
386
+ }
387
+ ++i;
388
+ }
389
+ return values;
390
+ }
391
+ setRowExpandLevel(level) {
392
+ this.rows.forEach(row => row.collapsed = (!row.isLoaded && !row.isPartialyLoaded) || row.level >= level);
393
+ this.indexVisibleRows();
394
+ }
395
+ setColumnExpandLevel(level) {
396
+ this.columns.forEach(column => column.collapsed = column.level >= level);
397
+ this.indexVisibleColumns();
398
+ }
399
+ getViewportRange() {
400
+ return this._viewport ? new range_1.CellRange(this._viewport.columns[0].index, this._viewport.rows[0].index, this._viewport.columns[this._viewport.columns.length - 1].index, this._viewport.rows[this._viewport.rows.length - 1].index) : null;
401
+ }
402
+ get gridRange() {
403
+ return new range_1.CellRange(0, 0, this.columns.length - 1, this.rows.length - 1);
404
+ }
405
+ getRangeIterator(range, withinViewport = false) {
406
+ return new iterableRange_1.IterableRange(this, withinViewport && this._viewport
407
+ ? range.intersect(this.getViewportRange())
408
+ : range.intersect(this.gridRange));
409
+ }
410
+ getInitializedCellIterator() {
411
+ return new iterableRange_1.IterableRange(this, this.gridRange, true);
412
+ }
413
+ getCellArray(range) {
414
+ const cells = [];
415
+ for (const cell of this.getRangeIterator(range)) {
416
+ cells.push(cell);
417
+ }
418
+ return cells;
419
+ }
420
+ transformRangeToMatrix(range, transform) {
421
+ let row, cell, rowValues;
422
+ range = range.intersect(this.gridRange);
423
+ const cells = [];
424
+ for (let r = range.r1; r <= range.r2; r++) {
425
+ row = this.rows[r];
426
+ if (!row)
427
+ continue;
428
+ rowValues = [];
429
+ for (let c = range.c1; c <= range.c2; c++) {
430
+ cell = row.cell(c);
431
+ if (cell)
432
+ rowValues.push(transform(cell));
433
+ }
434
+ cells.push(rowValues);
435
+ }
436
+ return cells;
437
+ }
438
+ getCellMatrix(range) {
439
+ return this.transformRangeToMatrix(range, c => c);
440
+ }
441
+ getValueMatrix(range) {
442
+ return this.transformRangeToMatrix(range, c => this.calculations.evaluate(c));
443
+ }
444
+ shiftMerges(index, offset, direction, _maxIndex = null) {
445
+ const fn = {
446
+ 'down': m => {
447
+ if (index <= m.r)
448
+ m.r += offset;
449
+ else if (index > m.r && index < m.r + m.rs) // && m.r + m.rs < maxIndex)
450
+ this.rows[m.r].cell(m.c).rowspan = (m.rs += offset);
451
+ },
452
+ 'up': m => {
453
+ if (index <= m.r)
454
+ m.r -= offset;
455
+ else if (index > m.r && index < m.r + m.rs) // && m.r + m.rs < maxIndex)
456
+ this.rows[m.r].cell(m.c).rowspan = (m.rs -= offset);
457
+ },
458
+ 'right': m => {
459
+ if (index <= m.c)
460
+ m.c += offset;
461
+ else if (index > m.c && index < m.c + m.cs)
462
+ this.rows[m.r].cell(m.c).colspan = (m.cs += offset);
463
+ },
464
+ 'left': m => {
465
+ if (index <= m.c)
466
+ m.c -= offset;
467
+ else if (index > m.c && index < m.c + m.cs) // && m.r + m.rs < maxIndex)
468
+ this.rows[m.r].cell(m.c).colspan = (m.cs -= offset);
469
+ }
470
+ };
471
+ this.mergedCells.forEach(fn[direction]);
472
+ this.indexHiddenCells();
473
+ }
474
+ indexHiddenCells() {
475
+ this._hiddenCells.clear();
476
+ this.mergedCells.forEach(m => {
477
+ if (m.r > 0)
478
+ return;
479
+ if ((this._hiddenCells.has(m.r) && this._hiddenCells.get(m.r).has(m.c)))
480
+ return;
481
+ for (let r = m.r; r < m.r + m.rs; r++) {
482
+ for (let c = m.c; c < m.c + m.cs; c++) {
483
+ if (r > m.r || c > m.c) {
484
+ if (!this._hiddenCells.has(r))
485
+ this._hiddenCells.set(r, new Map());
486
+ this._hiddenCells.get(r).set(c, m);
487
+ }
488
+ }
489
+ }
490
+ });
491
+ }
492
+ shift(index, offset, direction, maxIndex = null) {
493
+ this.shiftMerges(index, offset, direction, maxIndex);
494
+ this.calculations.shift(index, offset, direction, maxIndex);
495
+ this.names.shift(index, offset, direction, maxIndex);
496
+ this.comments.shift(index, offset, direction);
497
+ this.sparklines.shift(index, offset, direction);
498
+ }
499
+ clearCalculations(all = false) {
500
+ if (all && this.group)
501
+ this.group.models.forEach(m => m.calculations.refresh());
502
+ else
503
+ this.calculations.refresh();
504
+ }
505
+ refreshSparklines() {
506
+ this.sparklines.updateRangeMaps();
507
+ for (const cell of this.getInitializedCellIterator()) {
508
+ cell.invalidateStyle();
509
+ }
510
+ }
511
+ clearCalculatedStyles() {
512
+ this.conditionalFormats.refresh();
513
+ const cells = [];
514
+ for (const cell of this.getInitializedCellIterator()) {
515
+ cells.push(cell);
516
+ cell.invalidateStyle();
517
+ }
518
+ }
519
+ insertColumns(index, count, after = false) {
520
+ const parent = this.columns[Math.min(this.columns.length - 1, index)].parent;
521
+ if (after)
522
+ ++index;
523
+ this.shift(index, count, 'right');
524
+ for (let r = 0; r < this.rows.length; r++)
525
+ this.rows[r].insertColumns(index, count, false);
526
+ const newColumns = (new Array(count)).fill(null).map(() => new columnModel_1.ColumnModel(this));
527
+ this.columns.splice(index, 0, ...newColumns);
528
+ if (this.maxColumnLevel > 0)
529
+ parent.insertChildren(newColumns, index - (parent.index || -1) - 1);
530
+ this.indexColumns();
531
+ this.clearCalculations(true);
532
+ return newColumns;
533
+ }
534
+ deleteColumns(index, count) {
535
+ const parent = this.columns[index].parent;
536
+ this.shift(index, count, 'left');
537
+ const deletedCells = [];
538
+ for (let r = 0; r < this.rows.length; r++)
539
+ deletedCells.push(this.rows[r].deleteColumns(index, count, false));
540
+ // const deletedColumns: [ColumnModel, CellModel[]][] = this.columns.splice(index, count)
541
+ // .map((column, i) => [column, deletedCells[i]]);
542
+ const deletedColumns = this.columns.splice(index, count);
543
+ parent === null || parent === void 0 ? void 0 : parent.deleteChildren(deletedColumns);
544
+ this.indexColumns();
545
+ this.clearCalculations(true);
546
+ return deletedColumns;
547
+ }
548
+ insertRows(index, rows, after = false) {
549
+ const parentIndex = Math.min(this.rows.length - 1, index);
550
+ const parent = parentIndex < 0 ? this.rowRoot : this.rows[parentIndex].parent;
551
+ if (after)
552
+ ++index;
553
+ const count = isNaN(rows) ? rows.length : rows;
554
+ this.shift(index, count, 'down');
555
+ const newRows = isNaN(rows) ? rows : (new Array(count)).fill(null).map(() => new rowModel_1.RowModel(this));
556
+ this.rows.splice(index, 0, ...newRows);
557
+ if (this.maxRowLevel > 0)
558
+ parent.insertChildren(newRows, index - (parent.index || -1) - 1);
559
+ this.indexRows();
560
+ this.clearCalculations(true);
561
+ return newRows;
562
+ }
563
+ deleteRows(index, count) {
564
+ const parent = this.rows[index].parent;
565
+ this.shift(index, count, 'up');
566
+ const deletedRows = this.rows.splice(index, count);
567
+ parent === null || parent === void 0 ? void 0 : parent.deleteChildren(deletedRows);
568
+ this.indexRows();
569
+ this.clearCalculations(true);
570
+ return deletedRows;
571
+ }
572
+ moveRows(index, count, targetIndex) {
573
+ const movingRows = this.rows.splice(index, count);
574
+ this.rows.splice(targetIndex, 0, ...movingRows);
575
+ this.shift(index, targetIndex - index, 'down', index + count - 1);
576
+ this.indexRows();
577
+ this.clearCalculations(true);
578
+ return movingRows;
579
+ }
580
+ moveColumns(index, count, targetIndex) {
581
+ const movingColumns = this.columns.splice(index, count);
582
+ this.columns.splice(targetIndex, 0, ...movingColumns);
583
+ this.shift(index, targetIndex - index, 'right', index + count - 1);
584
+ this.indexColumns();
585
+ this.clearCalculations(true);
586
+ return movingColumns;
587
+ }
588
+ spillValues(cell, values, propertyCell, displayValues) {
589
+ const cells = [];
590
+ const oldValues = [];
591
+ let row, cell2;
592
+ for (let r = 0; r < values.length; r++) {
593
+ row = this.rows[cell.r + r];
594
+ if (row) {
595
+ for (let c = 0; c < values[r].length; c++) {
596
+ cell2 = row.cell(cell.c + c);
597
+ if (cell2 && cell2.editable) {
598
+ oldValues.push(cell2.value2);
599
+ cell2.value = values[r][c];
600
+ if (propertyCell) {
601
+ cell2.format = propertyCell.format;
602
+ cell2.type = propertyCell.type;
603
+ }
604
+ if (displayValues && displayValues[r][c] != null)
605
+ cell2.displayValue = displayValues[r][c];
606
+ cells.push(cell2);
607
+ }
608
+ }
609
+ }
610
+ }
611
+ return [cells, oldValues];
612
+ }
613
+ getCellByName(name, contextRange, searchGroup = true) {
614
+ const cell = this.names.getCellByName(name, contextRange);
615
+ if (cell)
616
+ return cell;
617
+ else if (this.group && searchGroup)
618
+ return this.group.getCellByName(name, contextRange);
619
+ return null;
620
+ }
621
+ getRangeByName(name, searchGroup = true) {
622
+ const range = this.names.getRangeByName(name);
623
+ if (range)
624
+ return range;
625
+ else if (this.group && searchGroup)
626
+ return this.group.getRangeByName(name);
627
+ return null;
628
+ }
629
+ getValueMatrixByName(name, searchGroup = true) {
630
+ const matrix = this.names.getRangeByName(name);
631
+ if (matrix)
632
+ return this.getValueMatrix(matrix);
633
+ else if (this.group && searchGroup)
634
+ return this.group.getValueMatrixByName(name);
635
+ return null;
636
+ }
637
+ }
638
+ exports.GridModel = GridModel;
639
+ //# sourceMappingURL=gridModel.js.map