x4js 1.4.20 → 1.4.24

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.
package/src/smartedit.ts DELETED
@@ -1,537 +0,0 @@
1
- /**
2
- * ___ ___ __
3
- * \ \_/ / / _
4
- * \ / /_| |_
5
- * / _ \____ _|
6
- * /__/ \__\ |_|
7
- *
8
- * @file smartedit.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 './dom-gen'
31
-
32
- import { TextEdit, TextEditProps } from './textedit'
33
- import { DataStore, DataView, Record } from './datastore'
34
- import { Popup, PopupProps, PopupEventMap } from './popup';
35
- import { Component, CProps, EvFocus } from './component';
36
- import { EvChange, EvClick } from './x4_events';
37
-
38
- type Renderer = (rec: Record) => CellData[];
39
-
40
- export interface ToolItem {
41
- text: string;
42
- callback: (target: TextEdit) => void;
43
- }
44
-
45
- export interface SmartEditProps extends TextEditProps {
46
- store: DataStore;
47
- field: string;
48
- minDisplay?: number; // min char number before displaying items
49
- maxCount?: number; // max count elements to display
50
- autoFill?: boolean; // force selected element into edit (cannot select empty)
51
- renderer: Renderer;
52
- tools?: ToolItem[]; // elements to add at end
53
- searchCallback?: (value: string, view: DataView) => boolean
54
- }
55
-
56
- export class SmartEdit extends TextEdit<SmartEditProps> {
57
-
58
- m_popup: PopupTable;
59
- m_dataview: DataView;
60
- m_field: string;
61
- m_minDisplay: number;
62
- m_maxCount: number;
63
- m_autoFill: boolean;
64
- m_renderer: Renderer;
65
- m_tools: ToolItem[];
66
- m_searchCallback: (value: string, view: DataView) => boolean;
67
-
68
- constructor(props: SmartEditProps) {
69
- super(props);
70
-
71
- this.m_dataview = props.store.createView();
72
- this.m_field = props.field;
73
- this.m_renderer = props.renderer;
74
- this.m_minDisplay = props.minDisplay ?? 0;
75
- this.m_maxCount = props.maxCount ?? 10;
76
- this.m_autoFill = props.autoFill === undefined ? true : props.autoFill;
77
- this.m_popup = null;
78
- this.m_tools = props.tools ?? [];
79
- this.m_searchCallback = props.searchCallback;
80
-
81
- this.on('change', (e) => this._onChange(e));
82
- this.on('focus', (e) => this._onFocus(e));
83
- }
84
-
85
- render(props: SmartEditProps) {
86
- super.render(props);
87
-
88
- this.m_ui_input.setDomEvent('keydown', (e) => this._onKey(e));
89
- }
90
-
91
- private _onChange(ev: EvChange) {
92
- this._showPopup(ev.value as string);
93
- }
94
-
95
- private _onFocus(ev: EvFocus) {
96
- if (ev.focus) {
97
- this._showPopup(this.value);
98
- }
99
- else if (this.m_popup) {
100
- this.m_popup.close();
101
- }
102
- }
103
-
104
- private _onKey(e) {
105
-
106
- console.log(e.key);
107
-
108
- switch (e.key) {
109
- case 'Backspace': {
110
-
111
- // remove selection
112
- let start = e.target.selectionStart;
113
- let end = e.target.selectionEnd;
114
-
115
- if (start > end) {
116
- let t = start;
117
- start = end;
118
- end = t;
119
- }
120
-
121
- let v = this.value;
122
- let a = v.substr(0, start);
123
- let b = v.substr(end);
124
-
125
- this.value = a + b;
126
- break;
127
- }
128
-
129
- case 'ArrowUp':
130
- case 'Up': {
131
- if (this.m_popup) {
132
- this._moveNext(false);
133
- e.preventDefault();
134
- }
135
-
136
- break;
137
- }
138
-
139
- case 'ArrowDown':
140
- case 'Down': {
141
- if (this.m_popup) {
142
- this._moveNext(true);
143
- e.preventDefault();
144
- }
145
-
146
- break;
147
- }
148
-
149
- case 'Enter': {
150
- if (this.m_popup) {
151
- this._checkTool(e);
152
- }
153
- break;
154
- }
155
- }
156
- }
157
-
158
- private _showSugg(text) {
159
- let sel = this.getSelection();
160
- this.value = text;
161
- this.select(sel.start, sel.length);
162
- }
163
-
164
- isOpen() {
165
- return this.m_popup !== null;
166
- }
167
-
168
-
169
- componentDisposed() {
170
- if (this.m_popup) {
171
- this.m_popup.close();
172
- }
173
-
174
- super.componentDisposed();
175
- }
176
-
177
- // enter pressed on an element
178
- private _checkTool(e: KeyboardEvent) {
179
-
180
- let sel = this.m_popup.selection;
181
- if (this._callTool(sel)) {
182
- e.preventDefault();
183
- }
184
- }
185
-
186
- private _callTool(sel: number): boolean {
187
- let data = this.m_popup.getRowData(sel) as ToolItem;
188
- if (data) {
189
- if (this.m_popup) {
190
- this.m_popup.close();
191
- }
192
-
193
- data.callback(this);
194
- return true;
195
- }
196
- else {
197
- return false;
198
- }
199
- }
200
-
201
- private _moveNext(next: boolean) {
202
- let sel = this.m_popup.selNext(next);
203
- console.log('movenext: ', sel);
204
-
205
- let data = this.m_popup.getRowData(sel);
206
-
207
- if (!data) {
208
- let text = this.m_popup.getCell(sel, 0).text;
209
- this._showSugg(text);
210
- }
211
- }
212
-
213
- //_onKey( e: KeyboardEvent ) {
214
- // if( e.key==' ' ) {
215
- // this._showPopup( this.value )
216
- // }
217
- //}
218
-
219
- private _showPopup(v: string) {
220
-
221
- if (this.m_popup) {
222
- this.m_popup.close();
223
- this.m_popup = null;
224
- }
225
-
226
- let cnt: number;
227
-
228
- let sel = this.getSelection();
229
- let search = sel.length ? v.substr(0, sel.start) : v;
230
-
231
- if (search.length < this.m_minDisplay) {
232
- return;
233
- }
234
-
235
- let autoFill = this.m_autoFill;
236
-
237
- if (search.length == 0) {
238
- cnt = this.m_dataview.filter(null);
239
- }
240
- else {
241
- if (this.m_searchCallback) {
242
- autoFill = this.m_searchCallback(search, this.m_dataview);
243
- cnt = this.m_dataview.count;
244
- }
245
- else {
246
- cnt = this.m_dataview.filter({
247
- op: '=',
248
- field: this.m_field,
249
- value: new RegExp('^' + search.trim() + '.*', 'mi')
250
- });
251
- }
252
- }
253
-
254
- if (cnt > 0) {
255
- let rec = this.m_dataview.getByIndex(0);
256
-
257
- if (autoFill) {
258
- this.value = rec.getField(this.m_field);
259
- }
260
-
261
- this.select(v.length);
262
-
263
- let count = Math.min(this.m_dataview.count, this.m_maxCount);
264
- let r2 = this.m_ui_input.getBoundingRect();
265
-
266
- this.m_popup = new PopupTable({
267
- cls: '@editor-popup',
268
- minWidth: r2.width
269
- });
270
-
271
- this.m_popup.on('click', (ev: EvClick) => {
272
-
273
- let { row, text } = ev.context;
274
-
275
- if (!this._callTool(row)) {
276
- this.value = text;
277
- this.emit('click', EvClick());
278
- }
279
- });
280
-
281
- let i;
282
- for (i = 0; i < count; i++) {
283
-
284
- let rec = this.m_dataview.getByIndex(i);
285
- let texts = this.m_renderer(rec);
286
-
287
- this.m_popup.setCell(i, 0, texts[0].text, texts[0].cls);
288
- this.m_popup.setCell(i, 1, texts[1].text, texts[1].cls);
289
- }
290
-
291
- for (let j = 0; j < this.m_tools.length; j++, i++) {
292
- this.m_popup.setCell(i, 0, this.m_tools[j].text);
293
- this.m_popup.setCell(i, 1, '');
294
- this.m_popup.setRowData(i, this.m_tools[j]);
295
- console.log('fill: ', i);
296
- }
297
-
298
- this.m_popup.displayAt(r2.left, r2.bottom);
299
- }
300
- else if (this.m_tools.length) {
301
- let r2 = this.m_ui_input.getBoundingRect();
302
-
303
- this.m_popup = new PopupTable({
304
- cls: '@editor-popup',
305
- minWidth: r2.width
306
- });
307
-
308
- this.m_popup.on('click', (ev: EvClick) => {
309
-
310
- let { row, text } = ev.context;
311
- if (!this._callTool(row)) {
312
- this.value = text;
313
- }
314
- });
315
-
316
- for (let j = 0, i = 0; j < this.m_tools.length; j++, i++) {
317
- this.m_popup.setCell(i, 0, this.m_tools[j].text);
318
- this.m_popup.setCell(i, 1, '');
319
- this.m_popup.setRowData(i, this.m_tools[j]);
320
- console.log('fill: ', i);
321
- }
322
-
323
- this.m_popup.displayAt(r2.left, r2.bottom);
324
- }
325
- }
326
- }
327
-
328
- interface CellData {
329
- text: string;
330
- cls?: string;
331
- }
332
-
333
- type CellMap = Map<number, CellData>;
334
-
335
- interface PopupTableEventMap extends PopupEventMap {
336
- click: EvClick;
337
- }
338
-
339
- interface PopupTableProps extends PopupProps<PopupTableEventMap> {
340
- rows?: number;
341
- cols?: number;
342
- minWidth?: number;
343
- }
344
-
345
-
346
-
347
- export class PopupTable extends Popup<PopupTableProps, PopupTableEventMap> {
348
-
349
- private m_rows: number;
350
- private m_cols: number;
351
- private m_cells: CellMap;
352
- private m_data: Map<number, any>;
353
- private m_minw: number;
354
- private m_defcell: CellData;
355
- private m_sel: number;
356
-
357
- constructor(props: PopupTableProps) {
358
- super(props);
359
-
360
- this.m_rows = props.rows ?? 0;
361
- this.m_cols = props.cols ?? 0;
362
- this.m_minw = props.minWidth;
363
- this.m_cells = new Map<number, CellData>();
364
- this.m_data = new Map<number, any>();
365
- this.m_defcell = { text: '', cls: undefined };
366
- this.m_sel = 0;
367
- this.enableMask(false);
368
-
369
- this.setDomEvent('create', () => {
370
- (<HTMLTableElement>this.dom).cellPadding = '0px';
371
- });
372
-
373
- this.setDomEvent('mousedown', (e: MouseEvent) => {
374
- e.preventDefault();
375
-
376
- let el = Component.getElement(<HTMLElement>e.target);
377
- let row = el.getData('row');
378
-
379
- this.m_sel = row;
380
- this.update();
381
-
382
- this.emit('click', EvClick({ row, text: this.getCell(row, 0).text }));
383
- })
384
- }
385
-
386
- setRowData(row: number, data: any) {
387
- this.m_data.set(row, data);
388
- }
389
-
390
- getRowData(row: number): any {
391
- return this.m_data.get(row);
392
- }
393
-
394
- setCell(row: number, col: number, text: string, cls?: string) {
395
- this.m_cells.set(_cid(row, col), { text, cls });
396
-
397
- if (this.m_rows < (row + 1)) {
398
- this.m_rows = (row + 1);
399
- }
400
-
401
- if (this.m_cols < (col + 1)) {
402
- this.m_cols = (col + 1);
403
- }
404
- }
405
-
406
- getCell(row, col): CellData {
407
-
408
- let cd = this.m_cells.get(_cid(row, col));
409
- if (cd == null) {
410
- return this.m_defcell;
411
- }
412
-
413
- return cd;
414
- }
415
-
416
- /** @ignore */
417
- render() {
418
- this.setProp('tag', 'table');
419
-
420
- if (this.m_minw) {
421
- this.setStyleValue('minWidth', this.m_minw);
422
- }
423
-
424
- let rows = [];
425
-
426
- for (let r = 0; r < this.m_rows; r++) {
427
-
428
- let cols = [];
429
- let data = { row: r };
430
-
431
- for (let c = 0; c < this.m_cols; c++) {
432
-
433
- let cell = this.getCell(r, c);
434
-
435
- let col = new Component({
436
- tag: 'td',
437
- content: cell.text,
438
- cls: cell.cls,
439
- data
440
- });
441
-
442
- cols.push(col);
443
- }
444
-
445
- let cls = undefined;
446
- if (r === this.m_sel) {
447
- cls = '@selected';
448
- }
449
-
450
- let row = new Component({
451
- tag: 'tr',
452
- cls,
453
- content: cols,
454
- data
455
- });
456
-
457
- rows.push(row);
458
- }
459
-
460
- this.setContent(rows);
461
- }
462
-
463
- /**
464
- * display the popup at a specific position
465
- * @param x
466
- * @param y
467
- */
468
-
469
- public displayAt(x: number, y: number, align: string = 'top left') {
470
-
471
- this.show();
472
-
473
- let halign = 'l',
474
- valign = 't';
475
-
476
- if (align.indexOf('right') >= 0) {
477
- halign = 'r';
478
- }
479
-
480
- if (align.indexOf('bottom') >= 0) {
481
- valign = 'b';
482
- }
483
-
484
- // @TODO: this is a minimal overflow problem solution
485
- let rc = x4document.body.getBoundingClientRect(),
486
- rm = this.getBoundingRect();
487
-
488
- if (halign == 'r') {
489
- x -= rm.width;
490
- }
491
-
492
- if (valign == 'b') {
493
- y -= rm.height;
494
- }
495
-
496
- if (x < 4) {
497
- x = 4;
498
- }
499
-
500
- if ((x + rm.width) > rc.right - 4) {
501
- x = rc.right - 4 - rm.width;
502
- }
503
-
504
- if (y < 4) {
505
- y = 4;
506
- }
507
-
508
- if ((y + rm.height) > rc.bottom - 4) {
509
- y = rc.bottom - 4 - rm.height;
510
- }
511
-
512
- this.setStyle({ left: x, top: y });
513
- }
514
-
515
- selNext(next: boolean): number {
516
-
517
- this.m_sel += next ? 1 : -1;
518
- if (this.m_sel >= this.m_rows) {
519
- this.m_sel = 0;
520
- }
521
- else if (this.m_sel < 0) {
522
- this.m_sel = this.m_rows - 1;
523
- }
524
-
525
- this.update();
526
- return this.m_sel;
527
- }
528
-
529
- get selection() {
530
- return this.m_sel;
531
- }
532
- }
533
-
534
-
535
- function _cid(row: number, col: number) {
536
- return row * 1000 + col;
537
- }