x4js 1.5.16 → 1.5.18

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 (75) hide show
  1. package/lib/cjs/app_sockets.js +74 -0
  2. package/lib/cjs/index.js +1 -0
  3. package/lib/cjs/treeview.js +27 -12
  4. package/lib/cjs/version.js +1 -1
  5. package/lib/esm/app_sockets.js +70 -0
  6. package/lib/esm/index.mjs +1 -0
  7. package/lib/esm/treeview.js +26 -12
  8. package/lib/esm/version.js +1 -1
  9. package/lib/{src/version.ts → types/app_sockets.d.ts} +29 -30
  10. package/lib/types/index.d.ts +1 -0
  11. package/lib/types/treeview.d.ts +5 -2
  12. package/lib/types/version.d.ts +1 -1
  13. package/package.json +1 -4
  14. package/lib/src/MIT-license.md +0 -14
  15. package/lib/src/action.ts +0 -88
  16. package/lib/src/alpha.jpg +0 -0
  17. package/lib/src/application.ts +0 -251
  18. package/lib/src/autocomplete.ts +0 -197
  19. package/lib/src/base64.ts +0 -166
  20. package/lib/src/base_component.ts +0 -152
  21. package/lib/src/button.ts +0 -355
  22. package/lib/src/calendar.ts +0 -328
  23. package/lib/src/canvas.ts +0 -505
  24. package/lib/src/cardview.ts +0 -227
  25. package/lib/src/checkbox.ts +0 -188
  26. package/lib/src/color.ts +0 -752
  27. package/lib/src/colorpicker.ts +0 -1639
  28. package/lib/src/combobox.ts +0 -465
  29. package/lib/src/component.ts +0 -2329
  30. package/lib/src/datastore.ts +0 -1322
  31. package/lib/src/dialog.ts +0 -656
  32. package/lib/src/dom_events.ts +0 -315
  33. package/lib/src/drag_manager.ts +0 -199
  34. package/lib/src/drawtext.ts +0 -355
  35. package/lib/src/fileupload.ts +0 -213
  36. package/lib/src/form.ts +0 -375
  37. package/lib/src/formatters.ts +0 -105
  38. package/lib/src/gridview.ts +0 -1185
  39. package/lib/src/i18n.ts +0 -346
  40. package/lib/src/icon.ts +0 -335
  41. package/lib/src/image.ts +0 -204
  42. package/lib/src/index.ts +0 -88
  43. package/lib/src/input.ts +0 -249
  44. package/lib/src/label.ts +0 -128
  45. package/lib/src/layout.ts +0 -430
  46. package/lib/src/link.ts +0 -86
  47. package/lib/src/listview.ts +0 -765
  48. package/lib/src/md5.ts +0 -438
  49. package/lib/src/menu.ts +0 -425
  50. package/lib/src/messagebox.ts +0 -224
  51. package/lib/src/panel.ts +0 -86
  52. package/lib/src/popup.ts +0 -494
  53. package/lib/src/property_editor.ts +0 -337
  54. package/lib/src/radiobtn.ts +0 -197
  55. package/lib/src/rating.ts +0 -135
  56. package/lib/src/request.ts +0 -300
  57. package/lib/src/router.ts +0 -185
  58. package/lib/src/settings.ts +0 -77
  59. package/lib/src/sidebarview.ts +0 -103
  60. package/lib/src/spreadsheet.ts +0 -1449
  61. package/lib/src/styles.ts +0 -343
  62. package/lib/src/svgcomponent.ts +0 -577
  63. package/lib/src/tabbar.ts +0 -151
  64. package/lib/src/tabview.ts +0 -110
  65. package/lib/src/textarea.ts +0 -235
  66. package/lib/src/textedit.ts +0 -544
  67. package/lib/src/toaster.ts +0 -80
  68. package/lib/src/tools.ts +0 -1473
  69. package/lib/src/tooltips.ts +0 -191
  70. package/lib/src/treeview.ts +0 -696
  71. package/lib/src/x4.less +0 -2243
  72. package/lib/src/x4dom.ts +0 -57
  73. package/lib/src/x4events.ts +0 -585
  74. package/lib/src/x4react.ts +0 -90
  75. package/lib/x4.css +0 -1780
@@ -1,1322 +0,0 @@
1
- /**
2
- * ___ ___ __
3
- * \ \/ / / _
4
- * \ / /_| |_
5
- * / \____ _|
6
- * /__/\__\ |_|
7
- *
8
- * @file datastore.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 { ajaxRequest } from './request';
31
- import { isArray, isString } from './tools';
32
- import { BasicEvent, EvChange, EventSource, EventMap, MapEvents } from './x4events';
33
- import { BaseComponent, BaseComponentEventMap, BaseComponentProps } from './base_component';
34
-
35
- export type ChangeCallback = (type: string, id?: any) => void;
36
- export type CalcCallback = () => string;
37
-
38
- export type FieldType = 'string' | 'int' | 'float' | 'date' | 'bool' | 'array' | 'object' | 'any' | 'calc';
39
- export type DataIndex = Uint32Array;
40
-
41
- interface EvDataChange extends BasicEvent {
42
-
43
- type: string;
44
- id: any;
45
- }
46
-
47
- function EvDataChange( type: 'create' | 'update' | 'delete' | 'data' | 'change', id?: any ) {
48
- return BasicEvent<EvDataChange>( { type, id } );
49
- }
50
-
51
-
52
- /**
53
- * fields definition
54
- * field with index=0 is record id
55
- */
56
-
57
- export interface MetaData {
58
- type?: FieldType;
59
- prec?: number;
60
- required?: boolean;
61
- calc?: (rec: Record) => any;
62
- model?: Record; // in case of array of subtypes, the model
63
- }
64
-
65
- export interface FieldInfo extends MetaData {
66
- name: string;
67
- }
68
-
69
- /**
70
- *
71
- */
72
-
73
- class MetaInfos {
74
- name: string;
75
- id: string; // field name holding 'id' record info
76
- fields: FieldInfo[]; // field list
77
-
78
- constructor( name: string ) {
79
- this.name = name;
80
- this.id = undefined;
81
- this.fields = [];
82
- }
83
- }
84
-
85
- const metaFields = Symbol( 'metaField' );
86
-
87
- function _getMetas( obj: object, create = true ) : MetaInfos {
88
-
89
- let ctor = obj.constructor;
90
- let mfld = ctor.hasOwnProperty(metaFields) ? ctor[metaFields] : undefined;
91
-
92
- if( mfld===undefined ) {
93
- if( !create ) {
94
- console.assert( mfld!==undefined );
95
- }
96
-
97
- // construct our metas
98
- mfld = new MetaInfos( ctor.name );
99
-
100
- // merge with parent class metas
101
- let pctor = Object.getPrototypeOf(ctor);
102
- if( pctor!=Record ) {
103
- let pmetas = pctor[metaFields];
104
- mfld.fields = [...pmetas.fields, ...mfld.fields ]
105
-
106
- console.assert( mfld.id===undefined, 'cannot define mutiple record id' );
107
- if( !mfld.id ) {
108
- mfld.id = pmetas.id;
109
- }
110
- }
111
-
112
- obj.constructor[metaFields] = mfld;
113
- }
114
-
115
- return mfld;
116
- }
117
-
118
- export namespace data {
119
-
120
- /**
121
- * define a record id
122
- * @example
123
- * \@data_id()
124
- * id: string; // this field is the record id
125
- **/
126
-
127
- export function id( ) {
128
- return ( ownerCls, fldName ) => {
129
- let metas = _getMetas( ownerCls );
130
- metas.fields.push( {
131
- name: fldName,
132
- type: 'any',
133
- required: true,
134
- });
135
-
136
- metas.id = fldName;
137
- }
138
- }
139
-
140
- /**
141
- * @ignore
142
- */
143
-
144
- export function field( data: MetaData ) {
145
-
146
- return ( ownerCls, fldName ) => {
147
- let metas = _getMetas( ownerCls );
148
- metas.fields.push( {
149
- name: fldName,
150
- ...data
151
- } );
152
- }
153
- }
154
-
155
- /**
156
- * following member is a string field
157
- * @example
158
- * \@data_string()
159
- * my_field: string; // this field will be seen as a string
160
- */
161
-
162
- export function string( props?: MetaData ) {
163
- return field( { ...props, type: 'string' } );
164
- }
165
-
166
- /**
167
- * following member is an integer field
168
- * @example
169
- * \@data_string()
170
- * my_field: number; // this field will be seen as an integer
171
- */
172
-
173
- export function int( props?: MetaData ) {
174
- return field( { ...props, type: 'int' } );
175
- }
176
-
177
- /**
178
- * following member is a float field
179
- * @example
180
- * \@data_float()
181
- * my_field: number; // this field will be seen as a float
182
- */
183
-
184
- export function float( props?: MetaData ) {
185
- return field( { ...props, type: 'float' } );
186
- }
187
-
188
- /**
189
- * following member is a boolean field
190
- * @example
191
- * \@data_bool()
192
- * my_field: boolean; // this field will be seen as a boolean
193
- */
194
-
195
- export function bool( props?: MetaData ) {
196
- return field( { ...props, type: 'bool' } );
197
- }
198
-
199
- /**
200
- * following member is a date field
201
- * @example
202
- * \@data_date()
203
- * my_field: date; // this field will be seen as a date
204
- */
205
-
206
- export function date( props?: MetaData ) {
207
- return field( { ...props, type: 'date' } );
208
- }
209
-
210
- /**
211
- * following member is a calculated field
212
- * @example
213
- * \@data_calc( )
214
- * get my_field(): string => {
215
- * return 'hello';
216
- * };
217
- */
218
-
219
- export function calc( props?: MetaData ) {
220
- return field( { ...props, type: 'calc'} )
221
- }
222
-
223
-
224
-
225
- /**
226
- *
227
- */
228
-
229
- interface RecordConstructor {
230
- new ( data?: any, id?: any ): Record;
231
- }
232
-
233
- /**
234
- * following member is a record array
235
- * @example
236
- * \@data_array( )
237
- * my_field(): TypedRecord[];
238
- */
239
-
240
- export function array( ctor: RecordConstructor, props?: MetaData ) {
241
- return data.field( { ...props, type: 'array', model: new ctor() } )
242
- }
243
-
244
- }
245
-
246
-
247
-
248
-
249
- /**
250
- * record model
251
- */
252
-
253
- export class Record {
254
-
255
- constructor( data?: any, id?: any ) {
256
-
257
- if( data!==undefined ) {
258
- this.unSerialize( data, id );
259
- }
260
- }
261
-
262
- clone( source?: any ) {
263
- let rec = new (this.constructor as any)( );
264
- if( source ) {
265
- rec.unSerialize( source );
266
- }
267
- return rec;
268
- }
269
-
270
- /**
271
- * get the record unique identifier
272
- * by default the return value is the first field
273
- * @return unique identifier
274
- */
275
-
276
- getID(): any {
277
- let metas = _getMetas( this, false );
278
- return this[metas.id];
279
- }
280
-
281
- /**
282
- * MUST IMPLEMENT
283
- * @returns fields descriptors
284
- */
285
-
286
- getFields(): FieldInfo[] {
287
- let metas = _getMetas( this, false );
288
- return metas.fields;
289
- }
290
-
291
- /**
292
- *
293
- */
294
-
295
- validate( ) : Error[] {
296
-
297
- let errs: Error[] = null;
298
-
299
- let fields = this.getFields( );
300
- fields.forEach( (fi) => {
301
- if( fi.required && !this.getField(fi.name) ) {
302
- if( errs ) {
303
- errs = [];
304
- }
305
-
306
- errs.push( new Error( `field ${fi.name} is required.` ) );
307
- }
308
- })
309
-
310
- return errs;
311
- }
312
-
313
- //mapAnyFields() {
314
- // this.getFields = ( ) => {
315
- // return Object.keys( this ).map( (name) => {
316
- // return <FieldInfo>{ name };
317
- // });
318
- // }
319
- //}
320
-
321
- getFieldIndex( name: string ) : number {
322
- let fields = this.getFields( );
323
- return fields.findIndex( (fd) => fd.name == name );
324
- }
325
-
326
- /**
327
- * default serializer
328
- * @returns an object with known record values
329
- */
330
-
331
- serialize(): any {
332
- let rec = {};
333
-
334
- this.getFields().forEach((f) => {
335
- if( f.calc === undefined ) {
336
- rec[f.name] = rec[f.name];
337
- }
338
- });
339
-
340
- return rec;
341
- }
342
-
343
- /**
344
- * default unserializer
345
- * @param data - input data
346
- * @returns a new Record
347
- */
348
-
349
- unSerialize(data: any, id?: any) : Record {
350
-
351
- let fields = this.getFields();
352
-
353
- fields.forEach( (sf) => {
354
- let value = data[sf.name];
355
- if (value !== undefined) {
356
- this[sf.name] = this._convertField( sf, value );
357
- }
358
- });
359
-
360
- if( id!==undefined ) {
361
- this[fields[0].name] = id;
362
- }
363
- else {
364
- console.assert( this.getID()!==undefined ); // store do not have ID field
365
- }
366
-
367
- return this;
368
- }
369
-
370
- /**
371
- * field conversion
372
- * @param field - field descriptor
373
- * @param input - value to convert
374
- * @returns the field value in it's original form
375
- */
376
-
377
- protected _convertField( field: FieldInfo, input: any ) : any {
378
-
379
- //TODO: boolean
380
-
381
- switch( field.type ) {
382
- case 'float': {
383
- let ffv: number = typeof (input) === 'number' ? input : parseFloat(input);
384
-
385
- if (field.prec !== undefined) {
386
- let mul = Math.pow(10, field.prec);
387
- ffv = Math.round(ffv * mul) / mul;
388
- }
389
-
390
- return ffv;
391
- }
392
-
393
- case 'int': {
394
- return typeof (input) === 'number' ? input : parseInt(input);
395
- }
396
-
397
- case 'date': {
398
- return isString(input) ? new Date(input) : input;
399
- }
400
-
401
- case 'array': {
402
- let result = [];
403
-
404
- if( field.model ) {
405
- input.forEach( ( v ) => {
406
- result.push( field.model.clone( v ) );
407
- })
408
-
409
- return result;
410
- }
411
- break;
412
- }
413
- }
414
-
415
- return input;
416
- }
417
-
418
- /**
419
- * get raw value of a field
420
- * @param name - field name or field index
421
- */
422
-
423
- getRaw( name: string | number ) : any {
424
-
425
- let idx;
426
- let fields = this.getFields( );
427
-
428
- if( typeof(name) === 'string' ) {
429
- idx = fields.findIndex( fi => fi.name == name );
430
- if( idx < 0 ) {
431
- console.assert( false, 'unknown field: '+name);
432
- return undefined;
433
- }
434
- }
435
- else if( name>=0 && name<fields.length ) {
436
- idx = name;
437
- }
438
- else {
439
- console.assert( false, 'bad field name: '+name);
440
- return undefined;
441
- }
442
-
443
- let fld = fields[idx];
444
- if( fld.calc!==undefined ) {
445
- return fld.calc( this );
446
- }
447
-
448
- return this[fld.name];
449
- }
450
-
451
- /**
452
- *
453
- * @param name
454
- * @param data
455
- */
456
-
457
- setRaw( name: string, data: string ) {
458
- this[name] = data;
459
- }
460
-
461
- /**
462
- * get field value (as string)
463
- * @param name - field name
464
- * @example
465
- * let value = record.get('field1');
466
- */
467
-
468
- getField( name: string ): string {
469
- let v = this.getRaw( name );
470
- return (v===undefined || v===null) ? '' : ''+v;
471
- }
472
-
473
- /**
474
- * set field value
475
- * @param name - field name
476
- * @param value - value to set
477
- * @example
478
- * record.set( 'field1', 7 );
479
- */
480
-
481
- setField(name: string, value: any) {
482
- let fields = this.getFields( );
483
- let idx = fields.findIndex( fi => fi.name == name );
484
-
485
- if( idx < 0 ) {
486
- console.assert( false, 'unknown field: '+name);
487
- return;
488
- }
489
-
490
- let fld = fields[idx];
491
- if( fld.calc!==undefined ) {
492
- console.assert( false, 'cannot set calc field: '+name);
493
- return;
494
- }
495
-
496
- this.setRaw( fld.name, value );
497
- }
498
- }
499
-
500
- /**
501
- * by default, the field id is rhe first member or the record
502
- */
503
-
504
- export class AutoRecord extends Record {
505
-
506
- private m_data;
507
- private m_fid: string;
508
-
509
- constructor( data: any ) {
510
- super( );
511
-
512
- this.m_data = data;
513
- }
514
-
515
- getID( ) {
516
- if( !this.m_fid ) {
517
- let fnames = Object.keys( this.m_data );
518
- this.m_fid = fnames[0];
519
- }
520
-
521
- return this.m_data[this.m_fid];
522
- }
523
-
524
- getFields( ) : FieldInfo[] {
525
- let fnames = Object.keys( this.m_data );
526
- let fields: FieldInfo[] = fnames.map( (n) => {
527
- return {
528
- name: n
529
- };
530
- })
531
-
532
- return fields;
533
- }
534
-
535
- getRaw( name: string ) : string {
536
- return this.m_data[name];
537
- }
538
-
539
- setRaw( name: string, data: string ) {
540
- this.m_data[name] = data;
541
- }
542
-
543
- clone( data ) {
544
- return new AutoRecord( {...data} );
545
- }
546
- }
547
-
548
- /**
549
- *
550
- */
551
-
552
- interface DataEventMap extends BaseComponentEventMap {
553
- change?: EvChange;
554
- }
555
-
556
- export interface DataProxyProps extends BaseComponentProps<DataEventMap> {
557
- type: 'local' | 'immediate' | 'ajax';
558
- path?: string;
559
- params?: any;
560
- }
561
-
562
- export class DataProxy extends BaseComponent<DataProxyProps,DataEventMap> {
563
-
564
- constructor( props: DataProxyProps ) {
565
- super( props );
566
- }
567
-
568
- load( ) {
569
- this._refresh( );
570
- }
571
-
572
- save( data ) {
573
- if( this.m_props.type=='local' ) {
574
- console.assert( false ); // not imp
575
- /*
576
- const fs = require('fs');
577
- fs.writeFileSync( this.m_path, data );
578
- */
579
- }
580
- }
581
-
582
- private _refresh( delay: number = 0 ) {
583
-
584
- if( this.m_props.type=='local' ) {
585
- console.assert( false ); // not imp
586
- /*
587
- const fs = require('fs');
588
- fs.readFile( this.m_path, ( _, bdata ) => {;
589
- let data = JSON.parse(bdata);
590
- this.emit( 'dataready', data );
591
- } );
592
- */
593
- }
594
- else {
595
- setTimeout( ( ) => {
596
- ajaxRequest( {
597
- url: this.m_props.path,
598
- method: 'GET',
599
- params: this.m_props.params,
600
- success: ( data ) => {
601
- this.emit( 'change', EvChange(data) );
602
- }
603
- });
604
- }, delay );
605
- }
606
- }
607
- }
608
-
609
-
610
- /**
611
- *
612
- */
613
-
614
- interface DataStoreProps extends BaseComponentProps<DataStoreEventMap> {
615
- model: Record;
616
- data?: Record[];
617
- url?: string;
618
- }
619
-
620
-
621
- interface DataStoreEventMap extends EventMap {
622
- data_change: EvDataChange;
623
- }
624
-
625
-
626
-
627
- /**
628
- *
629
- */
630
-
631
- export class DataStore extends EventSource<DataStoreEventMap> {
632
-
633
- protected m_model: Record;
634
- protected m_fields: FieldInfo[];
635
- protected m_records: Record[];
636
-
637
- protected m_proxy: DataProxy;
638
- protected m_rec_index: DataIndex;
639
-
640
- constructor(props: DataStoreProps) {
641
- super( );
642
-
643
- this.m_fields = undefined;
644
- this.m_records = [];
645
- this.m_rec_index = null;
646
- this.m_model = props.model;
647
- this.m_fields = props.model.getFields();
648
-
649
- if (props.data) {
650
- this.setRawData( props.data );
651
- }
652
- else if( props.url ) {
653
- this.load( props.url );
654
- }
655
- }
656
-
657
- /**
658
- *
659
- * @param records
660
- */
661
-
662
- load( url: string ) {
663
-
664
- //todo: that
665
- if( url.substr(0,7)==='file://' ) {
666
- this.m_proxy = new DataProxy( {
667
- type: 'local',
668
- path: url.substr( 7 ),
669
- events: { change: (ev) => { this.setData( ev.value ); } }
670
- });
671
-
672
- this.m_proxy.load( );
673
- }
674
- else {
675
- this.m_proxy = new DataProxy( {
676
- type: 'ajax',
677
- path: url,
678
- events: { change: (ev) => { this.setData( ev.value ); } }
679
- });
680
-
681
- this.m_proxy.load( );
682
- }
683
- }
684
-
685
- reload( ) {
686
- this.m_proxy.load( );
687
- }
688
-
689
- /**
690
- * convert raw objects to real records from model
691
- * @param records
692
- */
693
-
694
- public setData( records: any[] ) {
695
-
696
- let realRecords: Record[] = [];
697
-
698
- records.forEach( (rec) => {
699
- realRecords.push( this.m_model.clone(rec) );
700
- });
701
-
702
- this.setRawData( realRecords );
703
- }
704
-
705
- /**
706
- * just set the records
707
- * @param records - must be of the same type as model
708
- */
709
-
710
- public setRawData(records: any[]) {
711
-
712
- this.m_records = records;
713
- this._rebuildIndex( );
714
- this.emit( 'data_change', EvDataChange( 'change' ) );
715
- }
716
-
717
-
718
- private _rebuildIndex( ) {
719
- this.m_rec_index = null; // null to signal that we have to run on records instead of index
720
- this.m_rec_index = this.createIndex( null ); // prepare index (remove deleted)
721
- this.m_rec_index = this.sortIndex( this.m_rec_index, null ); // sort by id
722
- }
723
-
724
- /**
725
- *
726
- */
727
-
728
- public update( rec: Record ) {
729
- let id = rec.getID();
730
- let index = this.indexOfId(id);
731
- if (index < 0) {
732
- return false;
733
- }
734
-
735
- this.m_records[this.m_rec_index[index]] = rec;
736
- this.emit( 'data_change', EvDataChange( 'update', id ) );
737
- return true;
738
- }
739
-
740
- /**
741
- *
742
- * @param data
743
- */
744
-
745
- public append( rec: Record | any ) {
746
-
747
- if( !(rec instanceof Record) ) {
748
- let nrec = this.m_model.clone( );
749
- rec = nrec.unSerialize( rec );
750
- }
751
-
752
- console.assert( rec.getID() );
753
-
754
- this.m_records.push( rec );
755
- this._rebuildIndex( );
756
- this.emit( 'data_change', EvDataChange( 'create', rec.getID() ) );
757
- }
758
-
759
- /**
760
- *
761
- */
762
-
763
- getMaxId( ) {
764
- let maxID = undefined;
765
- this.m_records.forEach( (r) => {
766
- let rid = r.getID( );
767
- if( maxID===undefined || maxID<rid ) {
768
- maxID = rid;
769
- }
770
- });
771
-
772
- return maxID;
773
- }
774
-
775
- /**
776
- *
777
- * @param id
778
- */
779
-
780
- public delete(id: any): boolean {
781
-
782
- let idx = this.indexOfId( id );
783
- if( idx<0 ) {
784
- return false;
785
- }
786
-
787
- idx = this.m_rec_index[idx];
788
-
789
- // mark as deleted
790
- this.m_records.splice( idx, 1 );
791
- this._rebuildIndex( );
792
- this.emit( 'data_change', EvDataChange( 'delete', id ) );
793
- return true;
794
- }
795
-
796
- /**
797
- * return the number of records
798
- */
799
-
800
- get count( ) : number {
801
- return this.m_rec_index ? this.m_rec_index.length : this.m_records.length;
802
- }
803
-
804
- /**
805
- * return the fields
806
- */
807
-
808
- get fields( ) : FieldInfo [] {
809
- return this.m_fields;
810
- }
811
-
812
- /**
813
- * find the index of the element with the given id
814
- */
815
-
816
- public indexOfId(id: any ): number {
817
-
818
- //if( this.count<10 ) {
819
- // this.forEach( (rec) => rec.getID() == id );
820
- //}
821
-
822
- for( let lim = this.count, base = 0; lim != 0; lim >>= 1 ) {
823
-
824
- let p = base + (lim >> 1); // int conversion
825
- let idx = this.m_rec_index[p];
826
- let rid = this.m_records[idx].getID( );
827
-
828
- if( rid==id ) {
829
- return p;
830
- }
831
-
832
- if( rid<id ) {
833
- base = p+1;
834
- lim--;
835
- }
836
- }
837
-
838
- return -1;
839
- }
840
-
841
- /**
842
- * return the record by it's id
843
- * @returns record or null
844
- */
845
-
846
- public getById(id: any): Record {
847
- let idx = this.indexOfId( id );
848
- if( idx<0 ) {
849
- return null;
850
- }
851
-
852
- idx = this.m_rec_index[idx];
853
- return this.m_records[idx];
854
- }
855
-
856
- /**
857
- * return a record by it's index
858
- * @returns record or null
859
- */
860
-
861
- public getByIndex( index: number ): Record {
862
- let idx = this.m_rec_index[index];
863
- return this._getRecord( idx );
864
- }
865
-
866
- private _getRecord( index: number ) : Record {
867
- return this.m_records[index] ?? null;
868
- }
869
-
870
- public moveTo( other: DataStore ) {
871
- other.setRawData( this.m_records );
872
- }
873
-
874
- /**
875
- * create a new view on the DataStore
876
- * @param opts
877
- */
878
-
879
- createView( opts?: DataViewProps ) : DataView {
880
- let eopts = { ...opts, store: this };
881
- return new DataView( eopts );
882
- }
883
-
884
- /**
885
- *
886
- */
887
-
888
- createIndex( filter: FilterInfo ) : DataIndex {
889
-
890
- if( filter && filter.op==='empty-result' ) {
891
- return new Uint32Array(0);
892
- }
893
-
894
- let index = new Uint32Array( this.m_records.length );
895
- let iidx = 0;
896
-
897
- if( !filter ) {
898
- // reset filter
899
- this.forEach( (rec, idx) => {
900
- index[iidx++] = idx;
901
- } );
902
- }
903
- else {
904
- if( typeof(filter.op)==='function' ) {
905
-
906
- let fn = <FilterFunc>filter.op;
907
-
908
- // scan all records and append only interesting ones
909
- this.forEach( (rec, idx) => {
910
-
911
- // skip deleted
912
- if( !rec ) {
913
- return;
914
- }
915
-
916
- if( fn(rec) ) {
917
- index[iidx++] = idx;
918
- }
919
- } );
920
- }
921
- else {
922
- let filterFld = this.m_model.getFieldIndex( filter.field ); // field index to filter on
923
- if( filterFld<0 ) {
924
- // unknown filter field, nothing inside
925
- console.assert( false, 'unknown field name in filter' )
926
- return new Uint32Array(0);
927
- }
928
-
929
- let filterValue = filter.value;
930
- if( isString(filterValue) && !filter.caseSensitive ) {
931
- filterValue = filterValue.toUpperCase( );
932
- }
933
-
934
- function _lt( recval: string ) : boolean {
935
- return recval < filterValue;
936
- }
937
-
938
- function _le( recval: string ) : boolean {
939
- return recval <= filterValue;
940
- }
941
-
942
- function _eq( recval: string ) : boolean {
943
- return recval == filterValue;
944
- }
945
-
946
- function _neq( recval: string ) : boolean {
947
- return recval != filterValue;
948
- }
949
-
950
- function _ge( recval: string ) : boolean {
951
- return recval >= filterValue;
952
- }
953
-
954
- function _gt( recval: string ) : boolean {
955
- return recval > filterValue;
956
- }
957
-
958
- function _re( recval: string ) : boolean {
959
- filterRe.lastIndex = -1;
960
- return filterRe.test( recval );
961
- }
962
-
963
- let filterFn; // filter fn
964
- let filterRe; // if fielter is regexp
965
- if( filterValue instanceof RegExp ) {
966
- filterRe = filterValue;
967
- filterFn = _re;
968
- }
969
- else {
970
- switch( filter.op ) {
971
- case '<': { filterFn = _lt; break; }
972
- case '<=': { filterFn = _le; break; }
973
- case '=': { filterFn = _eq; break; }
974
- case '>=': { filterFn = _ge; break; }
975
- case '>': { filterFn = _gt; break; }
976
- case '<>': { filterFn = _neq; break; }
977
- }
978
- }
979
-
980
- // scan all records and append only interesting ones
981
- this.forEach( (rec, idx) => {
982
-
983
- // skip deleted
984
- if( !rec ) {
985
- return;
986
- }
987
-
988
- let field = rec.getRaw( filterFld );
989
- if( field===null || field===undefined ) {
990
- field = '';
991
- }
992
- else {
993
- field = ''+field;
994
- if( !filter.caseSensitive ) {
995
- field = field.toUpperCase( );
996
- }
997
- }
998
-
999
- let keep = filterFn( field );
1000
- if( keep ) {
1001
- index[iidx++] = idx;
1002
- };
1003
- });
1004
- }
1005
- }
1006
-
1007
- return index.slice( 0, iidx );
1008
- }
1009
-
1010
- sortIndex( index: DataIndex, sort: SortProp[] ) {
1011
-
1012
- interface sort_info {
1013
- fidx: number,
1014
- asc: boolean
1015
- }
1016
-
1017
- let bads = 0; // unknown fields
1018
- let fidxs: sort_info[] = []; // fields indexes
1019
-
1020
- // if no fields are given, reset sort by id
1021
- if ( sort===null ) {
1022
- fidxs.push( { fidx: 0, asc: true } );
1023
- }
1024
- else {
1025
- fidxs = sort.map( (si) => {
1026
-
1027
- let fi = this.m_model.getFieldIndex( si.field );
1028
- if (fi == -1) {
1029
- console.assert( false, 'unknown field name in sort' )
1030
- bads++;
1031
- }
1032
-
1033
- return { fidx: fi, asc: si.ascending };
1034
- });
1035
- }
1036
-
1037
- // unknown field or nothing to sort on ??
1038
- if( bads || fidxs.length==0 ) {
1039
- return index;
1040
- }
1041
-
1042
- // sort only by one field : optimize it
1043
- if( fidxs.length==1 ) {
1044
-
1045
- let field = fidxs[0].fidx;
1046
- index.sort( ( ia, ib ) => {
1047
-
1048
- let va = this.getByIndex(ia).getRaw( field ) ?? '';
1049
- let vb = this.getByIndex(ib).getRaw( field ) ?? '';
1050
- if (va > vb) { return 1; }
1051
- if (va < vb) { return -1; }
1052
- return 0;
1053
- } );
1054
-
1055
- // just reverse if
1056
- if( !fidxs[0].asc ) {
1057
- index.reverse( );
1058
- }
1059
- }
1060
- else {
1061
- index.sort( ( ia, ib ) => {
1062
-
1063
- for( let fi=0; fi<fidxs.length; fi++ ) {
1064
-
1065
- let fidx = fidxs[fi].fidx;
1066
- let mul = fidxs[fi].asc ? 1 : -1;
1067
-
1068
- let va = this.getByIndex(ia).getRaw( fidx ) ?? '';
1069
- let vb = this.getByIndex(ib).getRaw( fidx ) ?? '';
1070
- if (va > vb) { return mul; }
1071
- if (va < vb) { return -mul; }
1072
- }
1073
-
1074
- return 0;
1075
- } );
1076
- }
1077
-
1078
- return index
1079
- }
1080
-
1081
- /**
1082
- *
1083
- */
1084
-
1085
- forEach( cb: ( rec:Record, index: number ) => any ) {
1086
-
1087
- if( this.m_rec_index ) {
1088
- this.m_rec_index.some( (ri,index) => {
1089
- if( cb( this.m_records[ri], index ) ) {
1090
- return index;
1091
- }
1092
- });
1093
- }
1094
- else {
1095
- this.m_records.some( ( rec, index ) => {
1096
- if( rec ) {
1097
- if( cb( rec, index ) ) {
1098
- return index;
1099
- }
1100
- }
1101
- } );
1102
- }
1103
- }
1104
-
1105
- export( ) {
1106
- return this.m_records;
1107
- }
1108
-
1109
- changed( ) {
1110
- this.emit( 'data_change', EvDataChange('change') );
1111
- }
1112
- }
1113
-
1114
-
1115
- // :: VIEWS ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
1116
-
1117
- export interface EvViewChange extends BasicEvent {
1118
- action: string;
1119
- }
1120
-
1121
- export function EvViewChange( action: 'filter' | 'sort' | 'change' ) {
1122
- return BasicEvent<EvViewChange>( { action } );
1123
- }
1124
-
1125
- interface DataViewEventMap extends BaseComponentEventMap {
1126
- view_change: EvViewChange;
1127
- }
1128
-
1129
- interface DataViewProps extends BaseComponentProps<DataViewEventMap> {
1130
- store?: DataStore;
1131
- filter?: FilterInfo;
1132
- order?: string | SortProp[] | SortProp;
1133
- }
1134
-
1135
- export type FilterFunc = ( rec: Record ) => boolean;
1136
-
1137
- export interface FilterInfo {
1138
- op: '<' | '<=' | '=' | '>=' | '>' | '<>' | 'empty-result' | FilterFunc, // emptydb mean return an empty result always
1139
- field?: string;
1140
- value?: string | RegExp; // if regexp then operator is =
1141
- caseSensitive?: boolean;
1142
- }
1143
-
1144
-
1145
- export interface SortProp {
1146
- field: string; //
1147
- ascending: boolean; //
1148
- }
1149
-
1150
-
1151
-
1152
- /**
1153
- * Dataview allow different views of the DataStore.
1154
- * You can sort the columns & filter data
1155
- * You can have multiple views for a single DataStore
1156
- */
1157
-
1158
- export class DataView extends BaseComponent<DataViewProps,DataViewEventMap>
1159
- {
1160
- protected m_index: DataIndex;
1161
- protected m_store: DataStore;
1162
-
1163
- protected m_sort: SortProp[];
1164
- protected m_filter: FilterInfo;
1165
-
1166
- constructor( props: DataViewProps ) {
1167
- super( props );
1168
-
1169
- this.m_store = props.store;
1170
- this.m_index = null;
1171
- this.m_filter = null;
1172
- this.m_sort = null;
1173
-
1174
- this.filter( props.filter );
1175
-
1176
- if( props.order ) {
1177
- if( isString(props.order) ) {
1178
- this.sort( [ { field: props.order, ascending: true } ] );
1179
- }
1180
- else if( isArray(props.order) ) {
1181
- this.sort( props.order );
1182
- }
1183
- else {
1184
- this.sort( [props.order] );
1185
- }
1186
- }
1187
- else {
1188
- this.sort( null );
1189
- }
1190
-
1191
- this.m_store.on( 'data_change', ( e ) => this._storeChange(e) );
1192
- }
1193
-
1194
- private _storeChange( ev: EvDataChange ) {
1195
-
1196
- this._filter( this.m_filter, ev.type!='change' );
1197
- this._sort( this.m_sort, ev.type!='change' );
1198
-
1199
- this.emit( 'view_change', EvViewChange( 'change' ) );
1200
- }
1201
-
1202
- /**
1203
- *
1204
- * @param filter
1205
- */
1206
-
1207
- public filter( filter?: FilterInfo ) : number {
1208
-
1209
- this.m_index = null; // null to signal that we have to run on records instead of index
1210
- return this._filter( filter, true );
1211
- }
1212
-
1213
- private _filter( filter: FilterInfo, notify: boolean) : number {
1214
-
1215
- this.m_index = this.m_store.createIndex( filter );
1216
- this.m_filter = filter;
1217
-
1218
- // need to sort again:
1219
- if( this.m_sort ) {
1220
- this.sort( this.m_sort );
1221
- }
1222
-
1223
- if( notify ) {
1224
- this.emit( 'view_change', EvViewChange( 'filter' ) );
1225
- }
1226
-
1227
- return this.m_index.length;
1228
- }
1229
-
1230
- /**
1231
- *
1232
- * @param columns
1233
- * @param ascending
1234
- */
1235
-
1236
- public sort( props: SortProp[] ) {
1237
- this._sort( props, true );
1238
- }
1239
-
1240
- private _sort( props: SortProp[], notify: boolean ) {
1241
- this.m_index = this.m_store.sortIndex( this.m_index, props );
1242
- this.m_sort = props;
1243
-
1244
- if( notify ) {
1245
- this.emit( 'view_change', EvViewChange( 'sort' ) );
1246
- }
1247
- }
1248
-
1249
- /**
1250
- *
1251
- */
1252
-
1253
- get store ( ) {
1254
- return this.m_store;
1255
- }
1256
-
1257
- /**
1258
- *
1259
- */
1260
-
1261
- public get count() {
1262
- return this.m_index.length;
1263
- }
1264
-
1265
- /**
1266
- *
1267
- * @param id
1268
- */
1269
-
1270
- public indexOfId(id): number {
1271
- let ridx = this.m_store.indexOfId( id );
1272
- return this.m_index.findIndex( (rid) => rid === ridx );
1273
- }
1274
-
1275
- /**
1276
- *
1277
- * @param index
1278
- */
1279
-
1280
- public getByIndex(index: number): Record {
1281
-
1282
- if (index >= 0 && index < this.m_index.length) {
1283
- let rid = this.m_index[index];
1284
- return this.m_store.getByIndex( rid );
1285
- }
1286
-
1287
- return null;
1288
- }
1289
-
1290
- /**
1291
- *
1292
- * @param id
1293
- */
1294
-
1295
- public getById( id: any): Record {
1296
- return this.m_store.getById( id );
1297
- }
1298
-
1299
- changed( ) {
1300
- this.emit( 'view_change', EvViewChange('change') );
1301
- }
1302
-
1303
- /**
1304
- *
1305
- */
1306
-
1307
- forEach( cb: ( rec:Record, index: number ) => any ) {
1308
-
1309
- debugger;
1310
- this.m_index.some( ( index ) => {
1311
- let rec = this.m_store.getByIndex( index );
1312
- if( rec ) {
1313
- if( cb( rec, index ) ) {
1314
- return index;
1315
- }
1316
- }
1317
- } );
1318
- }
1319
- }
1320
-
1321
-
1322
-