x4js 1.5.17 → 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 -49
  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,1322 @@
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
+