x4js 1.5.18 → 1.5.20

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