x4js 1.4.53 → 1.5.1

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