x4js 1.4.2

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 (176) hide show
  1. package/lib/application.d.ts +95 -0
  2. package/lib/application.js +137 -0
  3. package/lib/base64.d.ts +31 -0
  4. package/lib/base64.js +135 -0
  5. package/lib/base_component.d.ts +64 -0
  6. package/lib/base_component.js +77 -0
  7. package/lib/button.d.ts +145 -0
  8. package/lib/button.js +235 -0
  9. package/lib/calendar.d.ts +77 -0
  10. package/lib/calendar.js +236 -0
  11. package/lib/canvas.d.ts +88 -0
  12. package/lib/canvas.js +354 -0
  13. package/lib/cardview.d.ts +83 -0
  14. package/lib/cardview.js +152 -0
  15. package/lib/checkbox.d.ts +72 -0
  16. package/lib/checkbox.js +126 -0
  17. package/lib/color.d.ts +144 -0
  18. package/lib/color.js +584 -0
  19. package/lib/colorpicker.d.ts +98 -0
  20. package/lib/colorpicker.js +1457 -0
  21. package/lib/combobox.d.ts +97 -0
  22. package/lib/combobox.js +246 -0
  23. package/lib/component.d.ts +572 -0
  24. package/lib/component.js +1712 -0
  25. package/lib/datastore.d.ts +392 -0
  26. package/lib/datastore.js +986 -0
  27. package/lib/dialog.d.ts +171 -0
  28. package/lib/dialog.js +468 -0
  29. package/lib/dom_events.d.ts +284 -0
  30. package/lib/dom_events.js +13 -0
  31. package/lib/drag_manager.d.ts +26 -0
  32. package/lib/drag_manager.js +118 -0
  33. package/lib/drawtext.d.ts +43 -0
  34. package/lib/drawtext.js +261 -0
  35. package/lib/fileupload.d.ts +60 -0
  36. package/lib/fileupload.js +158 -0
  37. package/lib/form.d.ts +122 -0
  38. package/lib/form.js +293 -0
  39. package/lib/formatters.d.ts +31 -0
  40. package/lib/formatters.js +75 -0
  41. package/lib/gridview.d.ts +171 -0
  42. package/lib/gridview.js +786 -0
  43. package/lib/hosts/host.d.ts +44 -0
  44. package/lib/hosts/host.js +69 -0
  45. package/lib/i18n.d.ts +67 -0
  46. package/lib/i18n.js +169 -0
  47. package/lib/icon.d.ts +56 -0
  48. package/lib/icon.js +173 -0
  49. package/lib/image.d.ts +51 -0
  50. package/lib/image.js +149 -0
  51. package/lib/index.js +1 -0
  52. package/lib/input.d.ts +86 -0
  53. package/lib/input.js +172 -0
  54. package/lib/label.d.ts +54 -0
  55. package/lib/label.js +86 -0
  56. package/lib/layout.d.ts +77 -0
  57. package/lib/layout.js +261 -0
  58. package/lib/link.d.ts +46 -0
  59. package/lib/link.js +55 -0
  60. package/lib/listview.d.ts +173 -0
  61. package/lib/listview.js +532 -0
  62. package/lib/md5.d.ts +56 -0
  63. package/lib/md5.js +397 -0
  64. package/lib/menu.d.ts +122 -0
  65. package/lib/menu.js +276 -0
  66. package/lib/messagebox.d.ts +64 -0
  67. package/lib/messagebox.js +141 -0
  68. package/lib/panel.d.ts +42 -0
  69. package/lib/panel.js +61 -0
  70. package/lib/popup.d.ts +71 -0
  71. package/lib/popup.js +373 -0
  72. package/lib/property_editor.d.ts +67 -0
  73. package/lib/property_editor.js +247 -0
  74. package/lib/radiobtn.d.ts +68 -0
  75. package/lib/radiobtn.js +131 -0
  76. package/lib/rating.d.ts +49 -0
  77. package/lib/rating.js +93 -0
  78. package/lib/request.d.ts +48 -0
  79. package/lib/request.js +220 -0
  80. package/lib/router.d.ts +13 -0
  81. package/lib/router.js +27 -0
  82. package/lib/settings.d.ts +33 -0
  83. package/lib/settings.js +63 -0
  84. package/lib/sidebarview.d.ts +44 -0
  85. package/lib/sidebarview.js +73 -0
  86. package/lib/smartedit.d.ts +103 -0
  87. package/lib/smartedit.js +381 -0
  88. package/lib/spreadsheet.d.ts +214 -0
  89. package/lib/spreadsheet.js +1073 -0
  90. package/lib/styles.d.ts +81 -0
  91. package/lib/styles.js +262 -0
  92. package/lib/svgcomponent.d.ts +165 -0
  93. package/lib/svgcomponent.js +350 -0
  94. package/lib/tabbar.d.ts +41 -0
  95. package/lib/tabbar.js +66 -0
  96. package/lib/tabview.d.ts +45 -0
  97. package/lib/tabview.js +79 -0
  98. package/lib/textarea.d.ts +59 -0
  99. package/lib/textarea.js +119 -0
  100. package/lib/textedit.d.ts +118 -0
  101. package/lib/textedit.js +406 -0
  102. package/lib/texthiliter.d.ts +56 -0
  103. package/lib/texthiliter.js +219 -0
  104. package/lib/toaster.d.ts +38 -0
  105. package/lib/toaster.js +58 -0
  106. package/lib/tools.d.ts +382 -0
  107. package/lib/tools.js +1096 -0
  108. package/lib/tooltips.d.ts +42 -0
  109. package/lib/tooltips.js +148 -0
  110. package/lib/treeview.d.ts +128 -0
  111. package/lib/treeview.js +490 -0
  112. package/lib/x4_events.d.ts +253 -0
  113. package/lib/x4_events.js +363 -0
  114. package/package.json +21 -0
  115. package/src/README.md +2 -0
  116. package/src/application.ts +191 -0
  117. package/src/base64.ts +162 -0
  118. package/src/base_component.ts +118 -0
  119. package/src/button.ts +327 -0
  120. package/src/calendar.ts +312 -0
  121. package/src/canvas.ts +501 -0
  122. package/src/cardview.ts +220 -0
  123. package/src/checkbox.ts +178 -0
  124. package/src/color.ts +748 -0
  125. package/src/colorpicker.ts +1618 -0
  126. package/src/combobox.ts +348 -0
  127. package/src/component.ts +2330 -0
  128. package/src/datastore.ts +1318 -0
  129. package/src/dialog.ts +631 -0
  130. package/src/dom_events.ts +297 -0
  131. package/src/drag_manager.ts +168 -0
  132. package/src/drawtext.ts +342 -0
  133. package/src/fileupload.ts +208 -0
  134. package/src/form.ts +362 -0
  135. package/src/formatters.ts +96 -0
  136. package/src/gridview.ts +1051 -0
  137. package/src/hosts/electron.ts +161 -0
  138. package/src/hosts/host.ts +100 -0
  139. package/src/hosts/nwjs.ts +141 -0
  140. package/src/hosts/nwjs_types.ts +339 -0
  141. package/src/i18n.ts +205 -0
  142. package/src/icon.ts +237 -0
  143. package/src/image.ts +198 -0
  144. package/src/input.ts +236 -0
  145. package/src/label.ts +124 -0
  146. package/src/layout.ts +366 -0
  147. package/src/link.ts +82 -0
  148. package/src/listview.ts +749 -0
  149. package/src/md5.ts +432 -0
  150. package/src/menu.ts +394 -0
  151. package/src/messagebox.ts +199 -0
  152. package/src/panel.ts +81 -0
  153. package/src/popup.ts +488 -0
  154. package/src/property_editor.ts +333 -0
  155. package/src/radiobtn.ts +190 -0
  156. package/src/rating.ts +131 -0
  157. package/src/request.ts +296 -0
  158. package/src/router.ts +43 -0
  159. package/src/settings.ts +75 -0
  160. package/src/sidebarview.ts +97 -0
  161. package/src/smartedit.ts +532 -0
  162. package/src/spreadsheet.ts +1423 -0
  163. package/src/styles.ts +332 -0
  164. package/src/svgcomponent.ts +440 -0
  165. package/src/tabbar.ts +105 -0
  166. package/src/tabview.ts +106 -0
  167. package/src/textarea.ts +183 -0
  168. package/src/textedit.ts +535 -0
  169. package/src/texthiliter.ts +284 -0
  170. package/src/toaster.ts +76 -0
  171. package/src/tools.ts +1391 -0
  172. package/src/tooltips.ts +185 -0
  173. package/src/treeview.ts +670 -0
  174. package/src/x4.less +1940 -0
  175. package/src/x4_events.ts +558 -0
  176. package/tsconfig.json +14 -0
@@ -0,0 +1,1423 @@
1
+ /**
2
+ * ___ ___ __
3
+ * \ \_/ / / _
4
+ * \ / /_| |_
5
+ * / _ \____ _|
6
+ * /__/ \__\ |_|
7
+ *
8
+ * @file spreadsheet.ts
9
+ * @author Etienne Cochard
10
+ * @license
11
+ * Copyright (c) 2019-2021 R-libre ingenierie
12
+ *
13
+ * This program is free software; you can redistribute it and/or modify
14
+ * it under the terms of the GNU General Public License as published by
15
+ * the Free Software Foundation; either version 3 of the License, or
16
+ * (at your option) any later version.
17
+ *
18
+ * This program is distributed in the hope that it will be useful,
19
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21
+ * GNU General Public License for more details.
22
+ *
23
+ * You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
24
+ **/
25
+
26
+ import { Component, CProps, SizerOverlay, EvDblClick, EvSize, ContainerEventMap, ContainerProps } from './component'
27
+ import { Input, InputProps } from './input';
28
+ import { HLayout, VLayout } from './layout';
29
+ import { TextEditProps, TextEdit } from './textedit';
30
+ import * as Formatters from './formatters';
31
+ import { asap, parseIntlFloat } from './tools';
32
+ import { deferCall } from './tools';
33
+ import { EvContextMenu, EvChange, EvSelectionChange, EventCallback } from './x4_events'
34
+ import { ComboBox } from './combobox';
35
+
36
+
37
+ export interface EditorFactory {
38
+ (props: InputProps, row: number, col: number) : Component;
39
+ }
40
+
41
+
42
+
43
+ /**
44
+ *
45
+ */
46
+ export interface ColProp {
47
+ title: string;
48
+ width: number;
49
+ formatter?: string;
50
+ align?: string;
51
+ cls?: string;
52
+ min_width?: number;
53
+ renderer?: Formatters.FormatFunc;
54
+ createEditor?: EditorFactory; // null => disable edition
55
+ }
56
+
57
+ class CellData {
58
+ text: string;
59
+ cls?: string;
60
+
61
+ static empty_cell: CellData = {
62
+ text: ''
63
+ }
64
+ }
65
+
66
+ /**
67
+ *
68
+ */
69
+
70
+ export interface SpreadsheetEventSet extends ContainerEventMap {
71
+ change?: EvChange;
72
+ dblClick?: EvDblClick;
73
+ selectionChange?: EvSelectionChange;
74
+ contextMenu?: EvContextMenu;
75
+ }
76
+
77
+
78
+ /**
79
+ *
80
+ */
81
+
82
+ export interface SpreadsheetProps extends ContainerProps {
83
+ columns: ColProp[];
84
+ maxrows?: number;
85
+ autoedit?: boolean;
86
+
87
+ change?: EventCallback<EvChange>; // shortcut to event { change: ... }
88
+ dblClick?: EventCallback<EvDblClick>;// shortcut to event { dblclick: ... }
89
+ selectionChange?: EventCallback<EvSelectionChange>;// shortcut to event { selectionChange: ... }
90
+ contextMenu?: EventCallback<EvContextMenu>;// shortcut to event { contextMenu: ... }
91
+ }
92
+
93
+
94
+ /**
95
+ *
96
+ */
97
+
98
+ export class Spreadsheet extends VLayout<SpreadsheetProps, SpreadsheetEventSet> {
99
+
100
+ private m_columns: ColProp[];
101
+ private m_row_limit: number;
102
+ private m_cells_data: Map<number, CellData>;
103
+ private m_rows_data: Map<number, string>;
104
+
105
+ protected m_view: Component;
106
+ protected m_container: Component;
107
+ protected m_header: Component;
108
+
109
+ private m_itemHeight: number;
110
+ private m_topIndex: number;
111
+
112
+ private m_visible_cells: Map<number, Component>;
113
+ private m_row_count: number; // visible row count
114
+
115
+ private m_selection: { row: number, col: number };
116
+ private m_editor: Component;
117
+ private m_autoedit: boolean;
118
+ private m_lockupdate: number;
119
+ private m_auto_row_count: boolean;
120
+
121
+ private m_recycler: Component[];
122
+ private m_used_cells: Component[];
123
+
124
+ constructor(props: SpreadsheetProps) {
125
+ super(props);
126
+
127
+ this.m_columns = props.columns;
128
+ this.m_autoedit = props.autoedit;
129
+ this.m_lockupdate = 0;
130
+
131
+ this.m_cells_data = new Map<number, CellData>();
132
+ this.m_rows_data = new Map<number, string>();
133
+ this.m_itemHeight = 0;
134
+ this.m_selection = { row: 0, col: 0 };
135
+ this.m_row_count = 0;
136
+ this.m_auto_row_count = false;
137
+ this.m_recycler = [];
138
+ this.m_used_cells = [];
139
+
140
+ if (props.maxrows === undefined) {
141
+ this.m_row_limit = 0;
142
+ this.m_auto_row_count = true;
143
+ }
144
+ else if (props.maxrows < 0) {
145
+ this.m_row_limit = 0;
146
+ this.m_auto_row_count = true;
147
+ }
148
+ else {
149
+ this.m_row_limit = props.maxrows;
150
+ }
151
+
152
+ this.setAttribute('tabIndex', 0);
153
+
154
+ this.setDomEvent('click', (e) => this._itemClick(e) );
155
+ this.setDomEvent('dblclick', (e) => this._itemDblClick(e) );
156
+ this.setDomEvent('keydown', (e) => this._handleKey(e) );
157
+ this.setDomEvent('keypress', (e) => this._keyPress(e) );
158
+ this.setDomEvent('focus', () => this._focus(true));
159
+ this.setDomEvent('focusout', () => this._focus(false));
160
+ this.setDomEvent('contextmenu', (e) => this._ctxMenu(e));
161
+
162
+ this.mapPropEvents( props, 'dblClick', 'selectionChange', 'contextMenu', 'change' );
163
+ }
164
+
165
+ componentCreated() {
166
+ super.componentCreated( );
167
+ this._updateScroll(true);
168
+ }
169
+
170
+ setColWidth(col: number, width: number) {
171
+ this._on_col_resize(col, width);
172
+ this.update(10);
173
+ }
174
+
175
+ getColWidth(col: number): number {
176
+ if (!this.m_columns[col]) {
177
+ return;
178
+ }
179
+
180
+ return this.m_columns[col].width;
181
+ }
182
+
183
+ setColTitle(col: number, title: string) {
184
+ console.assert(this.m_columns[col] !== undefined); // what ?
185
+ this.m_columns[col].title = title;
186
+ this.update(10);
187
+ }
188
+
189
+ reset(columns: ColProp[]) {
190
+ this.m_columns = columns;
191
+ this.m_cells_data = new Map<number, CellData>();
192
+ this.m_rows_data = new Map<number, string>();
193
+ this.update(10);
194
+ }
195
+
196
+ /**
197
+ * insert a row
198
+ * @param before row number before wich insert the new row
199
+ */
200
+
201
+ public insertRow(before: number) {
202
+
203
+ let new_cells_data = new Map<number, CellData>();
204
+ this.m_cells_data.forEach((celldata, key) => {
205
+
206
+ let { row, col } = _getid(key);
207
+
208
+ if (row >= before) {
209
+ new_cells_data.set(_mkid(row + 1, col), celldata);
210
+ }
211
+ else {
212
+ new_cells_data.set(key, celldata);
213
+ }
214
+ });
215
+
216
+ let new_rows_data = new Map<number, string>();
217
+ this.m_rows_data.forEach((rowdata, row) => {
218
+
219
+ if (row >= before) {
220
+ new_rows_data.set(row + 1, rowdata);
221
+ }
222
+ else {
223
+ new_rows_data.set(row, rowdata);
224
+ }
225
+ });
226
+
227
+ this.m_cells_data = new_cells_data;
228
+ this.m_rows_data = new_rows_data;
229
+
230
+ this._buildItems();
231
+ }
232
+
233
+ /**
234
+ * remove a row
235
+ * @param rowtodel row number to remove
236
+ */
237
+
238
+ public deleteRow(rowtodel: number) {
239
+ let new_cells_data = new Map<number, CellData>();
240
+ let new_rows_data = new Map<number, string>();
241
+
242
+ this.m_cells_data.forEach((celldata, key) => {
243
+
244
+ let { row, col } = _getid(key);
245
+
246
+ if (row > rowtodel) {
247
+ new_cells_data.set(_mkid(row - 1, col), celldata);
248
+ }
249
+ else if (row < rowtodel) {
250
+ new_cells_data.set(key, celldata);
251
+ }
252
+ });
253
+
254
+ this.m_rows_data.forEach((rowdata, row) => {
255
+
256
+ if (row > rowtodel) {
257
+ new_rows_data.set(row - 1, rowdata);
258
+ }
259
+ else if (row < rowtodel) {
260
+ new_rows_data.set(row, rowdata);
261
+ }
262
+ });
263
+
264
+ this.m_cells_data = new_cells_data;
265
+ this.m_rows_data = new_rows_data;
266
+
267
+ this._buildItems();
268
+ }
269
+
270
+ /**
271
+ * insert a new column
272
+ * @param before column index before to insert the new column or <0 to append
273
+ */
274
+
275
+ public insertCol(before: number, column: ColProp) {
276
+
277
+ let inspos = before;
278
+ if (inspos < 0) {
279
+ inspos = this.m_columns.length + 1;
280
+ }
281
+
282
+ // insert the col at the right place
283
+ this.m_columns.splice(inspos, 0, column);
284
+
285
+ if (before >= 0) {
286
+ let new_cells_data = new Map<number, CellData>();
287
+
288
+ this.m_cells_data.forEach((celldata, key) => {
289
+
290
+ let { row, col } = _getid(key);
291
+
292
+ if (col >= before) {
293
+ new_cells_data.set(_mkid(row, col + 1), celldata);
294
+ }
295
+ else {
296
+ new_cells_data.set(key, celldata);
297
+ }
298
+ });
299
+
300
+ this.m_cells_data = new_cells_data;
301
+ }
302
+
303
+ this.update();
304
+ }
305
+
306
+ /**
307
+ * remove a column
308
+ * @param coltodel
309
+ */
310
+
311
+ public deleteCol(coltodel: number) {
312
+
313
+ // insert the col at the right place
314
+ this.m_columns.splice(coltodel, 1);
315
+
316
+ let new_cells_data = new Map<number, CellData>();
317
+ this.m_cells_data.forEach((celldata, key) => {
318
+
319
+ let { row, col } = _getid(key);
320
+
321
+ if (col > coltodel) {
322
+ new_cells_data.set(_mkid(row, col - 1), celldata);
323
+ }
324
+ else if (col < coltodel) {
325
+ new_cells_data.set(key, celldata);
326
+ }
327
+ });
328
+
329
+ this.m_cells_data = new_cells_data;
330
+ this.update();
331
+ }
332
+
333
+ /**
334
+ *
335
+ * @param row
336
+ * @param col
337
+ */
338
+
339
+ private _getCellData(row: number, col: number, raw: boolean = false): CellData {
340
+ let value = this.m_cells_data.get(_mkid(row, col));
341
+ if (value === undefined) {
342
+ return raw ? null : CellData.empty_cell;
343
+ }
344
+
345
+ return value;
346
+ }
347
+
348
+ private _focus(focus: boolean) {
349
+ this.setClass('@focus', focus);
350
+ }
351
+
352
+ private _ctxMenu(e) {
353
+
354
+ let dom = e.target,
355
+ self = this.dom;
356
+
357
+ while (dom && dom != self) {
358
+ let itm = Component.getElement(dom),
359
+ row = itm.getData('row-id'),
360
+ col = itm.getData('col-id');
361
+
362
+ if (row !== undefined) {
363
+ this._selectItem(row, col);
364
+ this.emit( 'contextMenu', EvContextMenu(e, { row, col, item: itm }));
365
+ e.preventDefault();
366
+ return;
367
+ }
368
+
369
+ dom = dom.parentElement;
370
+ }
371
+ }
372
+
373
+ /** @ignore */
374
+
375
+ render() {
376
+
377
+ this.m_recycler = [];
378
+ this.m_container = new Component({
379
+ cls: 'content',
380
+ });
381
+
382
+ this.m_view = new Component({
383
+ cls: '@scroll-view',
384
+ flex: 1,
385
+ dom_events: {
386
+ sizechange: ( ) => this._updateScroll(true),
387
+ scroll: ( ) => this._updateScroll(false)
388
+ },
389
+ content: this.m_container
390
+ });
391
+
392
+ let cols = this.m_columns.map((col, index) => {
393
+
394
+ let comp = new Component({
395
+ cls: '@cell c'+index,
396
+ content: col.title ? col.title : '&nbsp',
397
+ flex: col.width < 0 ? -col.width : undefined,
398
+ attrs: {
399
+ title: col.title
400
+ },
401
+ style: {
402
+ width: col.width >= 0 ? col.width : undefined,
403
+ minWidth: col.min_width
404
+ },
405
+ });
406
+
407
+ new SizerOverlay({
408
+ target: comp,
409
+ sens: 'right',
410
+ resize: (ev: EvSize) => {
411
+ this._on_col_resize(index, ev.size.width);
412
+ }
413
+ });
414
+
415
+ (<any>col).$col = comp;
416
+ return comp;
417
+ });
418
+
419
+ this.m_header = new HLayout({
420
+ cls: '@header',
421
+ content: <any>cols,
422
+ });
423
+
424
+ this.setContent([
425
+ this.m_header,
426
+ this.m_view
427
+ ]);
428
+ }
429
+
430
+ /**
431
+ *
432
+ */
433
+
434
+ private _on_col_resize(col, width) {
435
+
436
+ if (!this.m_columns[col]) {
437
+ return;
438
+ }
439
+
440
+ // -> flex
441
+ if (width <= 0) {
442
+ this.m_columns[col].width = -1; // flex default
443
+ }
444
+ else {
445
+ this.m_columns[col].width = width;
446
+ }
447
+
448
+ this._updateScroll(true);
449
+ }
450
+
451
+ /**
452
+ * compute misc dimensions
453
+ * - item height
454
+ * - scroll width
455
+ */
456
+
457
+ private _computeItemHeight() {
458
+ let g1 = document.createElement('div');
459
+ g1.classList.add('x-spreadsheet');
460
+
461
+ let g2 = document.createElement('div');
462
+ g2.classList.add('content');
463
+
464
+ let g3 = document.createElement('div');
465
+ g3.classList.add('x-cell');
466
+ g3.append( '&nbsp;' )
467
+
468
+ g2.appendChild(g3);
469
+ g1.appendChild(g2);
470
+
471
+ this.dom.appendChild(g1);
472
+ let rc = g3.getBoundingClientRect();
473
+ this.dom.removeChild(g1);
474
+
475
+ this.m_itemHeight = rc.height;
476
+ }
477
+
478
+ /**
479
+ * compute columns widths
480
+ * use col.width for fixed size columns
481
+ * if col.width < 0 that mean that this is a proportion of the remaining space
482
+ */
483
+
484
+ private _calcColWidths(width: number) {
485
+
486
+ let fullw = 0;
487
+ let nwide = 0;
488
+
489
+ let calcw = new Int32Array(this.m_columns.length);
490
+ let calcz = new Int32Array(this.m_columns.length);
491
+ let calcm = new Int32Array(this.m_columns.length);
492
+
493
+ this.m_columns.forEach((col, colIdx) => {
494
+
495
+ let minw = Math.max(10, col.min_width ?? 0);
496
+
497
+ if (col.width > 0) {
498
+ let cw = Math.max(col.width, minw);
499
+ fullw += cw;
500
+ calcw[colIdx] = cw;
501
+ }
502
+ else {
503
+ let z = -col.width;
504
+ calcz[colIdx] = z;
505
+ nwide += z;
506
+ }
507
+
508
+ calcm[colIdx] = minw;
509
+ });
510
+
511
+ if (nwide) {
512
+ let restw = width - fullw;
513
+
514
+ for (let i = 0; i < this.m_columns.length && nwide; i++) {
515
+
516
+ if (!calcw[i]) {
517
+ let rest = Math.round(restw / nwide) * calcz[i];
518
+ if (rest < calcm[i]) {
519
+ rest = calcm[i];
520
+ }
521
+
522
+ calcw[i] = rest;
523
+ restw -= rest;
524
+ nwide -= calcz[i];
525
+ }
526
+ }
527
+ }
528
+
529
+ return calcw;
530
+ }
531
+
532
+ /**
533
+ * create a cell (component)
534
+ * and append it to the parent view
535
+ * if a cell was reviously recyled, use it
536
+ */
537
+ private _createCell(): Component {
538
+
539
+ let cell;
540
+ if (this.m_recycler.length) {
541
+ cell = this.m_recycler.pop();
542
+ cell.clearClasses();
543
+ }
544
+ else {
545
+ cell = new Component({
546
+ cls: '@cell'
547
+ });
548
+ }
549
+
550
+ if (!cell.dom) {
551
+ this.m_container.appendChild(cell);
552
+ }
553
+
554
+ return cell;
555
+ }
556
+
557
+ /**
558
+ * build cells of the spreadsheet
559
+ * cells are recycled when scrolling,
560
+ * only visibles cells exists
561
+ */
562
+
563
+ private _buildItems() {
564
+
565
+ let rc = this.getBoundingRect();
566
+ let rh = this.m_header.getBoundingRect();
567
+ let height = rc.height - rh.height;
568
+
569
+ if (this.m_itemHeight == 0) {
570
+ this._computeItemHeight();
571
+ }
572
+
573
+ let top = this.m_topIndex * this.m_itemHeight;
574
+ let y = 0;
575
+ let cidx = 0;
576
+ let rowIdx = this.m_topIndex;
577
+ let count = this.m_row_limit;
578
+
579
+ if (this.m_auto_row_count) {
580
+ //@review should be evaluated only when row count change
581
+ this.m_row_limit = count = this.getMaxRowCount();
582
+ }
583
+
584
+ let right_pos = 0;
585
+ if ((count * this.m_itemHeight) > height) {
586
+ let w = Component.getScrollbarSize();
587
+ rc.width -= w;
588
+ right_pos = w;
589
+ }
590
+
591
+ let even = this.m_topIndex & 1 ? true : false;
592
+ this.m_visible_cells = new Map<number, Component>();
593
+
594
+ // passe 0 - all created cells are moved to the recycler
595
+ this.m_used_cells.forEach((c) => {
596
+ this.m_recycler.push(c);
597
+ });
598
+
599
+ this.m_used_cells = [];
600
+
601
+ // pass 1 - compute column widths
602
+ let calcw = this._calcColWidths(rc.width);
603
+
604
+ //
605
+ let full_width = 0;
606
+ for (let i = 0; i < calcw.length; i++) {
607
+ full_width += calcw[i];
608
+ }
609
+
610
+ if (full_width <= rc.width) {
611
+
612
+ this.m_view.setStyleValue('overflow-x', 'hidden');
613
+ this.m_header.setStyleValue('width', rc.width);
614
+ this.m_container.setStyleValue('width', rc.width);
615
+
616
+ this.m_container.setStyle({
617
+ height: count * this.m_itemHeight,
618
+ });
619
+ }
620
+ else {
621
+ this.m_header.setStyleValue('width', full_width);
622
+ this.m_container.setStyleValue('width', full_width);
623
+ this.m_view.setStyleValue('overflow-x', 'visible');
624
+
625
+ this.m_container.setStyle({
626
+ height: count * this.m_itemHeight,
627
+ width: full_width
628
+ });
629
+ }
630
+
631
+ this.m_view.addClass('@hidden');
632
+
633
+ // pass 2 - build cells
634
+ let limit = 100;
635
+ while (y < height && rowIdx < count && --limit > 0) {
636
+
637
+ let rowdata = this.m_rows_data.get(rowIdx);
638
+ let x = 0;
639
+
640
+ let cols = this.m_columns.map((col, colIdx) => {
641
+
642
+ let cls = '@cell c'+colIdx ;
643
+ if (col.align) {
644
+ cls += ' ' + col.align;
645
+ }
646
+
647
+ if (col.cls) {
648
+ cls += ' ' + col.cls;
649
+ }
650
+
651
+ let cell: Component;
652
+ let celldata = this._getCellData(rowIdx, colIdx);
653
+
654
+ let text: string = celldata.text;
655
+
656
+ if (col.renderer && text.length) {
657
+ text = col.renderer(text, { row: rowIdx, col: colIdx });
658
+ }
659
+
660
+ //if( text.length==0 ) {
661
+ // text = '&nbsp;'
662
+ //}
663
+
664
+ cls += (even ? ' even' : ' odd');
665
+ if (rowdata) {
666
+ cls += ' ' + rowdata;
667
+ }
668
+
669
+ cell = this._createCell();
670
+ this.m_used_cells.push(cell);
671
+
672
+ cell.setContent(text); // always because cell reuse
673
+
674
+ cell.addClass(cls);
675
+ cell.setStyle({
676
+ left: x,
677
+ top: top + y,
678
+ width: calcw[colIdx]
679
+ });
680
+
681
+ if (this.m_selection.row == rowIdx && this.m_selection.col == colIdx) {
682
+ cell.addClass('@selected');
683
+ }
684
+
685
+ cell.setData('row-id', rowIdx);
686
+ cell.setData('col-id', colIdx);
687
+
688
+ if (celldata.cls) {
689
+ cell.addClass(celldata.cls);
690
+ }
691
+
692
+ this.m_visible_cells.set(_mkid(rowIdx, colIdx), cell);
693
+
694
+ x += calcw[colIdx];
695
+ return cell;
696
+ });
697
+
698
+ even = !even;
699
+
700
+ y += this.m_itemHeight;
701
+
702
+ rowIdx++;
703
+ cidx++;
704
+
705
+ //rows.splice( rows.length, 0, ...cols );
706
+ }
707
+
708
+ // if some cells are still in cache, hide them
709
+ this.m_recycler.forEach((c) => {
710
+ c.addClass('@hidden');
711
+ })
712
+
713
+ this.m_row_count = cidx;
714
+ //this.m_container.setContent( <ComponentContent>rows);
715
+
716
+ this.m_view.removeClass('@hidden');
717
+ this.setClass('empty', count == 0);
718
+ }
719
+
720
+ /** @ignore */
721
+ private _itemClick(e: MouseEvent) {
722
+ let dom = e.target as HTMLElement;
723
+ if (this.m_editor && this.m_editor.dom.contains(dom)) {
724
+ return;
725
+ }
726
+
727
+ let itm = Component.getElement(dom, Component);
728
+
729
+ if (!itm) {
730
+ return;
731
+ }
732
+
733
+ let rowIdx = itm.getData('row-id'),
734
+ colIdx = itm.getData('col-id');
735
+
736
+ if (rowIdx === undefined || colIdx === undefined) {
737
+ return;
738
+ }
739
+
740
+ this._selectItem(rowIdx, colIdx);
741
+ }
742
+
743
+ private _itemDblClick(e: MouseEvent) {
744
+ let dom = e.target as HTMLElement;
745
+ if (this.m_editor && this.m_editor.dom.contains(dom)) {
746
+ return;
747
+ }
748
+
749
+ let itm = Component.getElement(dom),
750
+ rowIdx = itm.getData('row-id'),
751
+ colIdx = itm.getData('col-id');
752
+
753
+ if (rowIdx === undefined || colIdx === undefined) {
754
+ return;
755
+ }
756
+
757
+ this.emit( 'dblClick', EvDblClick({ row: rowIdx, col: colIdx }));
758
+ this.editCell(rowIdx, colIdx);
759
+ }
760
+
761
+ /**
762
+ *
763
+ * @param rowIdx
764
+ * @param colIdx
765
+ * @param scrollIntoView
766
+ */
767
+
768
+ protected _selectItem(rowIdx: number, colIdx: number, scrollIntoView?: boolean) {
769
+
770
+ if (rowIdx < 0) {
771
+ rowIdx = 0;
772
+ }
773
+
774
+ if (rowIdx > this.m_row_limit - 1) {
775
+ rowIdx = this.m_row_limit - 1;
776
+ }
777
+
778
+ if (colIdx < 0) {
779
+ colIdx = 0;
780
+ }
781
+
782
+ let lastcol = this.m_columns.length - 1;
783
+ if (colIdx > lastcol) {
784
+ colIdx = lastcol;
785
+ }
786
+
787
+ if (this.m_selection.row == rowIdx && this.m_selection.col == colIdx) {
788
+ return;
789
+ }
790
+
791
+ this.select(rowIdx, colIdx, scrollIntoView);
792
+ }
793
+
794
+ private _scrollIntoView(row: number, col: number) {
795
+
796
+ let doscroll = (itm: Component, mode: string = 'nearest') => {
797
+ itm.scrollIntoView({
798
+ block: <ScrollLogicalPosition>mode //<ScrollLogicalPosition>sens ?? 'nearest'
799
+ });
800
+ }
801
+
802
+ let last = this.m_topIndex + this.m_row_count - 1;
803
+
804
+ if (row < this.m_topIndex) {
805
+ this.m_topIndex = row;
806
+ this.m_view.dom.scrollTop = this.m_topIndex * this.m_itemHeight;
807
+ this._buildItems();
808
+ doscroll(this._findItem(row, col), 'start');
809
+ }
810
+ else if (row > last) {
811
+ this.m_topIndex = row - this.m_row_count + 1;
812
+ this.m_view.dom.scrollTop = this.m_topIndex * this.m_itemHeight;
813
+ this._buildItems();
814
+ doscroll(this._findItem(row, col), 'end');
815
+ }
816
+ else {
817
+ doscroll(this._findItem(row, col));
818
+ }
819
+ }
820
+
821
+ /**
822
+ *
823
+ * @param row
824
+ * @param col
825
+ */
826
+
827
+ private _findItem(row, col): Component {
828
+ if (!this.m_visible_cells) {
829
+ return null;
830
+ }
831
+
832
+ return this.m_visible_cells.get(_mkid(row, col));
833
+ }
834
+
835
+ /**
836
+ *
837
+ */
838
+
839
+ private _updateScroll(forceUpdate) {
840
+ if (!this?.m_view?.dom) {
841
+ return;
842
+ }
843
+
844
+ let newTop = Math.floor(this.m_view.dom.scrollTop / (this.m_itemHeight || 1));
845
+
846
+ if (newTop != this.m_topIndex || forceUpdate) {
847
+ this.m_topIndex = newTop;
848
+ this._buildItems();
849
+ }
850
+
851
+ let newLeft = this.m_view.dom.scrollLeft;
852
+ this.m_header.setStyleValue('left', -newLeft);
853
+ }
854
+
855
+ /**
856
+ *
857
+ * @param event
858
+ * @param t
859
+ */
860
+
861
+ private _moveSel(sensy: number, sensx: number) {
862
+
863
+ let sel = this.m_selection;
864
+
865
+ let newRow = sel.row ?? 0;
866
+ let newCol = sel.col ?? 0;
867
+
868
+ if (sensy == 1) {
869
+ newRow++;
870
+ }
871
+ else if (sensy == -1) {
872
+ newRow--;
873
+ }
874
+ else if (sensy == 2) {
875
+ newRow += this.m_row_count - 1;
876
+ }
877
+ else if (sensy == -2) {
878
+ newRow -= this.m_row_count - 1;
879
+ }
880
+ else if (sensy == 3) {
881
+ newRow = this.m_row_limit - 1;
882
+ }
883
+ else if (sensy == -3) {
884
+ newRow = 0;
885
+ }
886
+
887
+ if (sensx == 1) {
888
+ newCol++;
889
+ }
890
+ else if (sensx == -1) {
891
+ newCol--;
892
+ }
893
+ else if (sensx == 2) {
894
+ newCol = this.m_columns.length - 1;
895
+ }
896
+ else if (sensx == -2) {
897
+ newCol = 0;
898
+ }
899
+ else if (sensx == 3) { // new editable cell skip line if needed
900
+ newCol++;
901
+
902
+ let lastcol = this.m_columns.length - 1;
903
+
904
+ l1: for (let trys = 0; trys < 2; trys++) {
905
+ while (newCol < lastcol) {
906
+ if (this.m_columns[newCol].createEditor !== null) {
907
+ break l1;
908
+ }
909
+
910
+ newCol++;
911
+ }
912
+
913
+ if (newCol > lastcol) {
914
+ newRow++;
915
+ newCol = 0;
916
+ }
917
+ }
918
+
919
+
920
+ }
921
+ else if (sensx == -3) {
922
+ newCol--;
923
+
924
+ let lastcol = this.m_columns.length - 1;
925
+
926
+ l2: for (let trys = 0; trys < 2; trys++) {
927
+ while (newCol >= 0) {
928
+ if (this.m_columns[newCol].createEditor !== null) {
929
+ break l2;
930
+ }
931
+
932
+ newCol--;
933
+ }
934
+
935
+ if (newCol < 0) {
936
+ newRow--;
937
+ newCol = lastcol;
938
+ }
939
+ }
940
+ }
941
+
942
+ this._selectItem(newRow, newCol, true);
943
+ }
944
+
945
+ private _handleKey(event: KeyboardEvent) {
946
+ let dom = <Node>event.target;
947
+ if (this.m_editor && this.m_editor.dom.contains(dom)) {
948
+ return;
949
+ }
950
+
951
+ switch (event.key) {
952
+ case 'ArrowDown':
953
+ case 'Down': {
954
+ this._moveSel(1, 0);
955
+ break;
956
+ }
957
+
958
+ case 'ArrowUp':
959
+ case 'Up': {
960
+ this._moveSel(-1, 0);
961
+ break;
962
+ }
963
+
964
+ case 'PageUp': {
965
+ this._moveSel(-2, 0);
966
+ break;
967
+ }
968
+
969
+ case 'PageDown': {
970
+ this._moveSel(2, 0);
971
+ break;
972
+ }
973
+
974
+ case 'ArrowLeft':
975
+ case 'Left': {
976
+ this._moveSel(0, -1);
977
+ break;
978
+ }
979
+
980
+ case 'ArrowRight':
981
+ case 'Right': {
982
+ this._moveSel(0, 1);
983
+ break;
984
+ }
985
+
986
+ case 'Home': {
987
+ if (event.ctrlKey) {
988
+ this._moveSel(-3, 0);
989
+ }
990
+ else {
991
+ this._moveSel(0, -2);
992
+ }
993
+ break;
994
+ }
995
+
996
+ case 'End': {
997
+ if (event.ctrlKey) {
998
+ this._moveSel(3, 0);
999
+ }
1000
+ else {
1001
+ this._moveSel(0, 2);
1002
+ }
1003
+
1004
+ break;
1005
+ }
1006
+
1007
+ case 'Enter': {
1008
+ this.editCurCell();
1009
+ event.stopPropagation( );
1010
+ break;
1011
+ }
1012
+
1013
+ case 'Delete': {
1014
+ this.clearCell(this.m_selection.row, this.m_selection.col);
1015
+ break;
1016
+ }
1017
+
1018
+ default: {
1019
+ //console.log( "unknown key: ", event.key);
1020
+ break;
1021
+ }
1022
+ }
1023
+ }
1024
+
1025
+ private _keyPress(event: KeyboardEvent) {
1026
+ let dom = <Node>event.target;
1027
+ if (this.m_editor && this.m_editor.dom.contains(dom)) {
1028
+ return;
1029
+ }
1030
+
1031
+ if (event.ctrlKey || event.altKey) {
1032
+ return;
1033
+ }
1034
+
1035
+ this.editCurCell(event.key);
1036
+ }
1037
+
1038
+ /**
1039
+ * return the selection
1040
+ * { row, col }
1041
+ */
1042
+
1043
+ getSelection() {
1044
+ return this.m_selection;
1045
+ }
1046
+
1047
+ select(row: number, col: number, scrollIntoView: boolean = true) {
1048
+
1049
+ if (this.m_selection.row == row && this.m_selection.col == col) {
1050
+ return;
1051
+ }
1052
+
1053
+ let oldSel = this._findItem(this.m_selection.row, this.m_selection.col);
1054
+ if (oldSel) {
1055
+ oldSel.removeClass('@selected');
1056
+ }
1057
+
1058
+ this.m_selection = { row, col };
1059
+
1060
+ if (scrollIntoView) {
1061
+ this._scrollIntoView(row, col);
1062
+ }
1063
+
1064
+ let newSel = this._findItem(row, col);
1065
+ if (newSel) {
1066
+ newSel.addClass('@selected');
1067
+ }
1068
+
1069
+ this.emit( 'selectionChange', EvSelectionChange({ row, col }));
1070
+ }
1071
+
1072
+ /**
1073
+ * return the row count
1074
+ */
1075
+
1076
+ rowCount() {
1077
+ return this.m_row_limit;
1078
+ }
1079
+
1080
+ /**
1081
+ * return the maximum row index filled with something
1082
+ */
1083
+
1084
+ getMaxRowCount() {
1085
+
1086
+ let max_row = 0;
1087
+
1088
+ this.m_cells_data.forEach((c, uid) => {
1089
+ let row = Math.round(uid / 1000) + 1;
1090
+ if (max_row < row) {
1091
+ max_row = row;
1092
+ }
1093
+ });
1094
+
1095
+ return max_row;
1096
+ }
1097
+
1098
+ getColCount() {
1099
+ return this.m_columns.length;
1100
+ }
1101
+
1102
+ setRowStyle(row: number, cls: string) {
1103
+ this.m_rows_data.set(row, cls);
1104
+
1105
+ if (this.m_lockupdate == 0) {
1106
+ this._buildItems();
1107
+ }
1108
+ }
1109
+
1110
+ getRowStyle(row: number) {
1111
+ return this.m_rows_data.get(row);
1112
+ }
1113
+
1114
+ setCellStyle(row: number, col: number, cls: string) {
1115
+ let cell = this._getCellData(row, col, true);
1116
+ if (!cell) {
1117
+ cell = { text: '' };
1118
+ this.m_cells_data.set(_mkid(row, col), cell);
1119
+ }
1120
+
1121
+ cell.cls = cls;
1122
+
1123
+ if (this.m_lockupdate == 0 && this.m_visible_cells) {
1124
+ let itm = this._findItem(row, col);
1125
+ if (itm) {
1126
+ itm.setClass(cls, true); //todo: pb when changing classes
1127
+ }
1128
+ else {
1129
+ this._buildItems();
1130
+ }
1131
+ }
1132
+ }
1133
+
1134
+ getCellText(row: number, col: number): string {
1135
+ return this._getCellData(row, col).text;
1136
+ }
1137
+
1138
+ getCellNumber(row: number, col: number): number {
1139
+ let text = this._getCellData(row, col).text;
1140
+ return parseIntlFloat(text);
1141
+ }
1142
+
1143
+ clearRow(row: number) {
1144
+ for (let c = 0; c < this.m_columns.length; c++) {
1145
+ this.clearCell(row, c);
1146
+ }
1147
+ this.update(10);
1148
+ }
1149
+
1150
+ clearCell(row: number, col: number) {
1151
+ this.setCellText(row, col, null);
1152
+ }
1153
+
1154
+ editCurCell(forceText?: string) {
1155
+ this.editCell(this.m_selection.row, this.m_selection.col, forceText);
1156
+ }
1157
+
1158
+ editCell(row: number, col: number, forcedText?: string) {
1159
+
1160
+ if (!this.m_autoedit) {
1161
+ return;
1162
+ }
1163
+
1164
+ // disable edition
1165
+ if (this.m_columns[col].createEditor === null) {
1166
+ return;
1167
+ }
1168
+
1169
+ this._scrollIntoView(row, col);
1170
+
1171
+ let item = this._findItem(row, col);
1172
+ let place = item.dom;
1173
+ let parent = place.parentElement;
1174
+
1175
+ let rc = place.getBoundingClientRect();
1176
+ let prc = parent.getBoundingClientRect();
1177
+
1178
+ let cell = this._getCellData(row, col, true);
1179
+
1180
+ let edtBuilder = (props: TextEditProps, col, row): Component => {
1181
+ return new TextEdit(props);
1182
+ }
1183
+
1184
+ if (this.m_columns[col].createEditor) {
1185
+ edtBuilder = this.m_columns[col].createEditor;
1186
+ }
1187
+
1188
+ let cellvalue = forcedText ? forcedText : (cell ? cell.text : '');
1189
+
1190
+ this.m_editor = edtBuilder({
1191
+ cls: '@editor',
1192
+ style: {
1193
+ left: rc.left - prc.left,
1194
+ top: rc.top - prc.top,
1195
+ width: rc.width,
1196
+ height: rc.height
1197
+ },
1198
+ tabIndex: false,
1199
+ value: cellvalue,
1200
+ data: {
1201
+ row,
1202
+ col
1203
+ },
1204
+ }, row, col);
1205
+
1206
+ if( !this.m_editor ) {
1207
+ return;
1208
+ }
1209
+
1210
+ parent.appendChild(this.m_editor._build());
1211
+
1212
+ this._setupEditor( );
1213
+
1214
+ this.m_editor.setData('old-value', cellvalue);
1215
+ this.m_editor.focus();
1216
+
1217
+ if (this.m_editor instanceof TextEdit) {
1218
+ this.m_editor.selectAll();
1219
+ }
1220
+ }
1221
+
1222
+ _setupEditor( ) {
1223
+
1224
+ let movesel = (sensy, sensx) => {
1225
+ deferCall(() => {
1226
+ this.killEditor(true);
1227
+ this._moveSel(sensy, sensx);
1228
+ this.editCurCell();
1229
+ });
1230
+ }
1231
+
1232
+ // todo: better
1233
+ if( this.m_editor instanceof TextEdit ) {
1234
+ let editor = this.m_editor as TextEdit;
1235
+ let input: Input = editor.input;
1236
+
1237
+ input.setDomEvent( 'blur', () => {
1238
+ this.killEditor(true);
1239
+ } );
1240
+
1241
+ input.setDomEvent( 'keydown', (e: KeyboardEvent) => {
1242
+
1243
+ switch (e.key) {
1244
+ case 'Escape': {
1245
+ this.killEditor(false);
1246
+ e.stopPropagation();
1247
+ break;
1248
+ }
1249
+
1250
+ case 'Enter':
1251
+ case 'Tab': {
1252
+ let sens = 3;
1253
+ if (e.shiftKey) {
1254
+ sens = -3;
1255
+ }
1256
+
1257
+ movesel(0, sens);
1258
+ e.stopPropagation();
1259
+ break;
1260
+ }
1261
+
1262
+ case 'ArrowUp':
1263
+ case 'Up': {
1264
+ movesel(-1, 0);
1265
+ e.stopPropagation();
1266
+ break;
1267
+ }
1268
+
1269
+ case 'ArrowDown':
1270
+ case 'Down': {
1271
+ movesel(1, 0);
1272
+ e.stopPropagation();
1273
+ break;
1274
+ }
1275
+ }
1276
+ });
1277
+ }
1278
+ else if( this.m_editor instanceof ComboBox ) {
1279
+
1280
+ let input: Input = (this.m_editor as ComboBox).input;
1281
+ input.setDomEvent( 'blur', () => {
1282
+ this.killEditor(true);
1283
+ } );
1284
+
1285
+ input.setDomEvent( 'keydown', (e: KeyboardEvent) => {
1286
+
1287
+ switch (e.key) {
1288
+ case 'Escape': {
1289
+ this.killEditor(false);
1290
+ e.stopPropagation();
1291
+ break;
1292
+ }
1293
+
1294
+ case 'Enter':
1295
+ case 'Tab': {
1296
+ let sens = 3;
1297
+ if (e.shiftKey) {
1298
+ sens = -3;
1299
+ }
1300
+
1301
+ movesel(0, sens);
1302
+ e.stopPropagation();
1303
+ break;
1304
+ }
1305
+ }
1306
+ });
1307
+
1308
+ this.m_editor.showPopup( );
1309
+ this.m_editor.on( 'change', ( ev ) => {
1310
+ this.killEditor(true);
1311
+ });
1312
+
1313
+ this.m_editor.on( 'cancel', ( ev ) => {
1314
+ this.killEditor(false);
1315
+ });
1316
+ }
1317
+ }
1318
+
1319
+ killEditor(save: boolean) {
1320
+ if (this.m_editor) {
1321
+ if (save) {
1322
+
1323
+ let text, id;
1324
+
1325
+ if( this.m_editor instanceof TextEdit ) {
1326
+ text = (this.m_editor as TextEdit).value;
1327
+ }
1328
+ else if( this.m_editor instanceof ComboBox ) {
1329
+ id = (this.m_editor as ComboBox).value;
1330
+ text = (this.m_editor as ComboBox).valueText;
1331
+ }
1332
+
1333
+ let row = this.m_editor.getData('row');
1334
+ let col = this.m_editor.getData('col');
1335
+ let old = this.m_editor.getData('old-value');
1336
+
1337
+ this.setCellText(row, col, text);
1338
+ const ev = EvChange(text, { row, col, oldValue: old, id });
1339
+ this.emit('change', ev );
1340
+
1341
+ if( ev.defaultPrevented ) {
1342
+ this.setCellText(row, col, old);
1343
+ }
1344
+ }
1345
+
1346
+ // cannot dipose while handling blur event, so we defer...
1347
+ let t = this.m_editor;
1348
+ asap( ( ) => {
1349
+ t.dispose();
1350
+ })
1351
+
1352
+ this.m_editor = null;
1353
+ this.focus();
1354
+ }
1355
+ }
1356
+
1357
+ clearData() {
1358
+ this.m_cells_data = new Map<number, CellData>();
1359
+ this.m_rows_data = new Map<number, string>();
1360
+ }
1361
+
1362
+ setCellText(row: number, col: number, value: string) {
1363
+
1364
+ if (value == null || value.length == 0) {
1365
+ this.m_cells_data.delete(_mkid(row, col));
1366
+ value = ''; //'&nbsp';
1367
+ }
1368
+ else {
1369
+ let cell: any = this._getCellData(row, col, true);
1370
+ if (!cell) {
1371
+ cell = {};
1372
+ }
1373
+
1374
+ cell.text = value;
1375
+ this.m_cells_data.set(_mkid(row, col), cell);
1376
+ }
1377
+
1378
+ if (this.m_lockupdate == 0 && this.m_visible_cells) {
1379
+ let itm = this._findItem(row, col);
1380
+ if (itm) {
1381
+
1382
+ if (this.m_columns[col].renderer) {
1383
+ value = this.m_columns[col].renderer(value, { row, col });
1384
+ }
1385
+
1386
+ itm.setContent(value);
1387
+ }
1388
+ else {
1389
+ this._buildItems();
1390
+ }
1391
+ }
1392
+ }
1393
+
1394
+ lockUpdate(start: boolean) {
1395
+ if (start) {
1396
+ this.m_lockupdate++;
1397
+ }
1398
+ else {
1399
+ if (--this.m_lockupdate == 0) {
1400
+ this._updateScroll(true);
1401
+ }
1402
+ }
1403
+ }
1404
+ }
1405
+
1406
+ /**
1407
+ * @ignore
1408
+ */
1409
+
1410
+ function _mkid(row: number, col: number): number {
1411
+ return row * 1000 + col;
1412
+ }
1413
+
1414
+ /**
1415
+ * @ignore
1416
+ */
1417
+
1418
+ function _getid(key: number) {
1419
+ return {
1420
+ row: Math.floor(key / 1000) | 0,
1421
+ col: (key % 1000) | 0
1422
+ }
1423
+ }