/
utils.ts
708 lines (607 loc) · 20.2 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
import {Balloon, Color} from './widgets';
import {toDart, toJs} from './wrappers';
import {ColorType, MARKER_TYPE} from "./const";
import {Point, Rect, GridCell} from "./grid";
import {IDartApi} from "./api/grok_api.g";
const api: IDartApi = <any>window;
declare global {
interface CanvasRenderingContext2D {
setFillStyle(fill: string | CanvasGradient | CanvasPattern): CanvasRenderingContext2D;
setStrokeStyle(stroke: string | CanvasGradient | CanvasPattern): CanvasRenderingContext2D;
roundRect(x: number, y: number, w: number, h: number, r: number): CanvasRenderingContext2D
line(x1: number, y1: number, x2: number, y2: number, color: ColorType): CanvasRenderingContext2D;
/**
* Use stroke() or fill() after.
*/
polygon(pa: Point[]): CanvasRenderingContext2D;
}
}
CanvasRenderingContext2D.prototype.setFillStyle = function (fill: string | CanvasGradient | CanvasPattern) {
this.fillStyle = fill;
return this;
}
CanvasRenderingContext2D.prototype.setStrokeStyle = function (stroke: string | CanvasGradient | CanvasPattern) {
this.strokeStyle = stroke;
return this;
}
CanvasRenderingContext2D.prototype.roundRect = function (x: number, y: number, w: number, h: number, r: number) {
if (w < 2 * r) r = w / 2;
if (h < 2 * r) r = h / 2;
this.beginPath();
this.moveTo(x + r, y);
this.arcTo(x + w, y, x + w, y + h, r);
this.arcTo(x + w, y + h, x, y + h, r);
this.arcTo(x, y + h, x, y, r);
this.arcTo(x, y, x + w, y, r);
this.closePath();
return this;
}
CanvasRenderingContext2D.prototype.line = function (x1, y1, x2, y2, color) {
this.beginPath();
this.strokeStyle = Color.toRgb(color);
this.moveTo(x1, y1);
this.lineTo(x2, y2);
this.stroke();
return this;
}
CanvasRenderingContext2D.prototype.polygon = function (pa: Point[]) {
this.beginPath();
const last_p = pa[pa.length - 1]
this.moveTo(last_p.x, last_p.y);
for (let p of pa) {
this.lineTo(p.x, p.y);
}
this.closePath();
return this;
}
export namespace Paint {
export function coordinateGrid(g: CanvasRenderingContext2D, worldBounds: Rect, xAxis?: Rect, yAxis?: Rect, viewport?: Rect) {
if (xAxis != null)
horzAxis(g, worldBounds.minX, worldBounds.maxX, xAxis.x, xAxis.y, xAxis.width, xAxis.height);
if (yAxis != null)
vertAxis(g, worldBounds.minY, worldBounds.maxY, yAxis.x, yAxis.y, yAxis.width, yAxis.height);
}
export function horzAxis(g: CanvasRenderingContext2D, min: number, max: number, x: number, y: number, w: number, h: number, log: boolean = false, inverse: boolean = false) {
api.grok_Paint_HorzAxis(g, min, max, x, y, w, h, log, inverse);
}
export function vertAxis(g: CanvasRenderingContext2D, min: number, max: number, x: number, y: number, w: number, h: number, log: boolean = false, inverse: boolean = false) {
api.grok_Paint_VertAxis(g, min, max, x, y, w, h, log, inverse);
}
export function roundRect(g: CanvasRenderingContext2D, x: number, y: number, w: number, h: number, r: number): CanvasRenderingContext2D {
if (w < 2 * r) r = w / 2;
if (h < 2 * r) r = h / 2;
g.beginPath();
g.moveTo(x + r, y);
g.arcTo(x + w, y, x + w, y + h, r);
g.arcTo(x + w, y + h, x, y + h, r);
g.arcTo(x, y + h, x, y, r);
g.arcTo(x, y, x + w, y, r);
g.closePath();
return g;
}
/** Renders a marker */
export function marker(g: CanvasRenderingContext2D, markerType: MARKER_TYPE, x: number, y: number, color: number | string, size: number) {
const c: number = typeof color === 'string' ? Color.fromHtml(color) : color;
api.grok_Paint_Marker(g, markerType, x, y, c, size);
}
/** Renders a PNG image from bytes */
export function pngImage(g: CanvasRenderingContext2D, bounds: Rect, imageBytes: Uint8Array) {
let r = new FileReader();
r.readAsBinaryString(new Blob([imageBytes]));
r.onload = function() {
let img = new Image();
img.onload = function() {
bounds = new Rect(bounds.x, bounds.y, bounds.width, bounds.height).fit(img.width, img.height)
g.drawImage(img, bounds.x, bounds.y, bounds.width, bounds.height);
}
img.src = "data:image/jpeg;base64," + window.btoa(r.result as string);
};
}
}
export class Utils {
/** @param {Iterable} iterable*/
static firstOrNull<T>(iterable: Iterable<T>): T | null {
let first = iterable[Symbol.iterator]().next();
return first.done ? null : first.value;
}
/** Returns an 'identity' array where the element in idx-th position is equals to idx. */
static identity(length: number) {
let res = new Uint32Array(length);
for (let i = 0; i < length; i++)
res[i] = i;
return res;
}
static replaceAll(string: string, search: string, replace: string) {
return string.split(search).join(replace);
}
static isEmpty(s?: string) {
return !s || s === '';
}
static nullIfEmpty(s?: string) {
return Utils.isEmpty(s) ? null : s;
}
/** Shows "Open File" dialog, and lets you process the result. */
static openFile(options: {accept: string, open: (file: File) => void}): void {
let input: HTMLInputElement = document.createElement('input');
input.type = 'file';
input.multiple = false;
input.accept = options.accept ?? '';
input.onchange = _this => {
options.open(input.files![0]);
};
input.click();
}
/** Shows "Open File" dialog, and lets you process the result. */
static openFileBytes(options: {accept: string, open: (bytes: Uint8Array) => void}): void {
Utils.openFile({
accept: options.accept,
open: (f) => {
var reader = new FileReader();
reader.onload = () => {
options.open(new Uint8Array(reader.result as ArrayBuffer));
};
reader.readAsArrayBuffer(f);
}
})
}
/** Downloads the specified content locally */
static download(filename: string, content: BlobPart, contentType?: string) {
contentType = contentType ?? 'application/octet-stream';
const a = document.createElement('a');
const blob = new Blob([content], {'type':contentType});
a.href = window.URL.createObjectURL(blob);
a.download = filename;
a.click();
}
/** Loads the specified common libraries, if they were not loaded already.
* Use it in plugins when a big JS dependency is used infrequently,
* such as exporting to Excel, or showing a 3d structure.
* Example: `loadJsCss(['common/exceljs.min.js', 'common/exceljs.min.css'])` */
static async loadJsCss(files: string[]): Promise<null> {
return toJs(api.grok_Utils_LoadJsCss(files));
}
}
/** A proxy to a Dart `List<T>`. */
export class DartList<T> implements Iterable<T> {
dart: any;
/** Creates a proxy to an existing Dart list. */
static fromDart<T>(dart: any): DartList<T> {
let list = new DartList();
list.dart = dart;
return list as DartList<T>;
}
[key: number]: any;
/** Returns the number of objects in this list. */
get length(): number { return api.grok_List_Get_Length(this.dart); }
/** Removes all objects from this list; the length of the list becomes zero. */
clear() { api.grok_List_Clear(this.dart); }
/** Sorts this list. */
sort() { api.grok_List_Sort(this.dart); }
/** Adds [value] to the end of this list, extending the length by one. */
push(value: T) { api.grok_List_Add(this.dart, value); }
/** Returns the object at the given [index] in the list. */
get(index: number): T { return api.grok_List_Get(this.dart, index); }
/** Sets the value at the given [index] in the list to [value]. */
set(index: number, value: T): T { return api.grok_List_Set(this.dart, index, value); }
/** Removes the first occurrence of [value] from the list. */
remove(value: T) { api.grok_List_Remove(this.dart, value); }
includes(item: T, start?: number) {
const length = this.length;
for (let i = (start ? start : 0); i < length; i++) {
if (this.get(i) === item)
return true;
}
return false;
}
* [Symbol.iterator](): Iterator<T> {
for (let i = 0; i < this.length; i++)
yield this.get(i);
}
}
/** Html-related utilities */
export namespace HtmlUtils {
/** Renders the element to canvas */
export function renderToCanvas(element: HTMLElement, ratio: number = 1): Promise<HTMLCanvasElement> {
return api.grok_HtmlUtils_RenderToCanvas(element, ratio);
}
export function htmlGetBounds(element: HTMLElement): Rect {
return Rect.fromDart(api.grok_HtmlUtils_HtmlGetBounds(element));
}
}
/**
* Proxies a Dart Map, API-compliant to ES 2015+
*/
export const MapProxy = new Proxy(class {
dart: any;
objectName: string | null;
valueType: string | null;
constructor(dart: any, objectName: string | null = null, valueType: string | null = null) {
this.dart = dart;
this.objectName = objectName;
this.valueType = valueType;
}
keys(): Iterable<any> {
return _toIterable(api.grok_Map_Keys(this.dart));
}
values() {
return _toIterable(toJs(api.grok_Map_Values(this.dart)));
}
* [Symbol.iterator] () {
for (let key of this.keys()) {
const value = toJs(api.grok_Map_Get(this.dart, key));
yield [key, toJs(value)];
}
}
entries() {
return this;
}
forEach(callback: (key: string, value: any) => void) {
for (const [key, value] of this) {
callback(key, toJs(value));
}
}
delete(key: string) {
return toJs(api.grok_Map_Delete(this.dart, toDart(key)));
}
get(key: any) {
return toJs(api.grok_Map_Get(this.dart, key));
}
has(key: string) {
return api.grok_Map_Has(this.dart, toDart(key));
}
set(key: string, value: any) {
api.grok_Map_Set(this.dart, key, toDart(value));
return this;
}
clear() {
api.grok_Map_Clear(this.dart);
}
size() {
return toJs(api.grok_Map_Size(this.dart));
}
}, {
construct(target, args) {
// @ts-ignore
return new Proxy(new target(...args), {
get: function (target: any, prop) {
const val = target[prop];
if (typeof val === 'function') {
return function (...args :string[]) {
return val.apply(target, args);
};
} else {
return toJs(api.grok_Map_Get(target.dart, prop));
}
},
set: function (target, prop, value) {
const valueType = typeof(value);
if (!target.valueType || target.valueType === valueType) {
api.grok_Map_Set(target.dart, prop, toDart(value));
} else {
throw new Error(`Entries of ${target.objectName} require type '${target.valueType}', passed '${valueType}'`);
}
return true;
},
deleteProperty: function (target, prop) {
api.grok_Map_Delete(target.dart, toDart(prop));
return true;
},
has: function (target, prop) {
return api.grok_Map_Has(target.dart, toDart(prop));
},
getOwnPropertyDescriptor(target, prop) {
return {
enumerable: true,
configurable: true
};
},
ownKeys: function (target) {
return Array.from(target.keys());
}
});
}
}
);
// export class PropProxy {
// constructor(dart) {
// this.dart = dart;
// return new Proxy({}, {
// get: function(target, prop) { return DG.toJs(api.grok_PropMixin_Get(dart, prop)); },
// set: function(target, prop, value) { api.grok_PropMixin_Set(dart, prop, DG.toDart(value)); }
// })
// }
// }
export function _toIterable(dart: any): Iterable<any> {
let iterable = {};
// @ts-ignore
iterable[Symbol.iterator] = () => _getIterator(dart);
// @ts-ignore
return iterable;
}
export function _getIterator(dart: any) {
let iterator = api.grok_Iterable_Get_Iterator(dart);
return {
next: function () {
return api.grok_Iterator_MoveNext(iterator) ?
{value: toJs(api.grok_Iterator_Current(iterator)), done: false} :
{done: true};
}
};
}
export function _isDartium() {
return document.head.querySelectorAll('script[src*=".dart.js"]').length == 0;
}
export function _toJson(x: any) {
return x === null ? null : JSON.stringify(x);
}
export function* range(length: number) {
for (let i = 0; i < length; i++)
yield i;
}
/** Returns an 'identity' array where the element in idx-th position is equals to idx. */
export function identity(length: number) {
let res = new Uint32Array(length);
for (let i = 0; i < length; i++)
res[i] = i;
return res;
}
/*window.onerror = function (message, url, lineNumber, columnNumber, errorObject) {
return api.grok_Error(message, url, lineNumber, columnNumber, errorObject);
};*/
/** Times the execution of function f
* @param {string} name - a label for the execution time to display
* @param {Function} f - function with no parameters that will get measured
* @returns {value} - a value which f returns
* */
export function time(name: string, f: Function) {
let start = new Date();
let result = f();
let stop = new Date();
// @ts-ignore
console.log(`${name}: ${stop - start} ms`);
// @ts-ignore
new Balloon().info(`${name}: ${stop - start} ms`);
return result;
}
/** Times the execution of asyncronous function f
* @async
* @param {string} name - a label for the execution time to display
* @param {Function} f - async function with no parameters that will get measured
* @returns {Promise<value>} - a promise for the value which f returns
* */
export async function timeAsync(name: string, f: Function) {
let start = new Date();
let result = await f();
let stop = new Date();
// @ts-ignore
console.log(`${name}: ${stop - start} ms`);
// @ts-ignore
new Balloon().info(`${name}: ${stop - start} ms`);
return result;
}
export function _identityInt32(length: number): Int32Array {
let values = new Int32Array(length);
for (let i = 0; i < length; i++)
values[i] = i;
return values;
}
/**
* Least recently used cache.
* Inspired by https://github.com/Yomguithereal/mnemonist/blob/master/lru-cache.js
* */
export class LruCache<K = any, V = any> {
private capacity: number;
public onItemEvicted: Function | null;
private items: {};
private tail: number;
private forward: Uint16Array;
private backward: Uint16Array;
private V: any[];
private K: any[];
private size: number;
private head: number;
constructor(capacity: number = 100) {
this.capacity = capacity;
this.forward = new Uint16Array(this.capacity);
this.backward = new Uint16Array(this.capacity);
this.V = new Array(this.capacity);
this.K = new Array(this.capacity);
this.size = 0;
this.head = 0;
this.tail = 0;
this.items = {};
this.onItemEvicted = null;
}
/**
* Splays a value on top.
* @param {number} pointer - Pointer of the value to splay on top.
* @return {LruCache}
*/
splayOnTop(pointer: number): LruCache<K, V> {
let oldHead = this.head;
if (this.head === pointer)
return this;
let previous = this.backward[pointer];
let next = this.forward[pointer];
if (this.tail === pointer)
this.tail = previous;
else
this.backward[next] = previous;
this.forward[previous] = next;
this.backward[oldHead] = pointer;
this.head = pointer;
this.forward[pointer] = oldHead;
return this;
}
/**
* Checks whether the key exists in the cache.
*
* @param {any} key - Key.
*/
has(key: any): boolean {
return key in this.items;
}
/**
* Sets the value for the given key in the cache.
*
* @param {any} key - Key.
* @param {any} value - Value.
*/
set(key: any, value: any): void {
// The key already exists, we just need to update the value and splay on top
// @ts-ignore
let pointer = this.items[key];
if (typeof pointer !== 'undefined') {
this.splayOnTop(pointer);
this.V[pointer] = value;
return;
}
// The cache is not yet full
if (this.size < this.capacity) {
pointer = this.size++;
}
// Cache is full, we need to drop the last value
else {
pointer = this.tail;
this.tail = this.backward[pointer];
if (this.onItemEvicted != null)
this.onItemEvicted(this.V[pointer]);
// @ts-ignore
delete this.items[this.K[pointer]];
}
// Storing key & value
// @ts-ignore
this.items[key] = pointer;
this.K[pointer] = key;
this.V[pointer] = value;
// Moving the item at the front of the list
this.forward[pointer] = this.head;
this.backward[this.head] = pointer;
this.head = pointer;
}
/**
* Gets the value attached to the given key, and makes it the most recently used item.
*
* @param {any} key - Key.
*/
get(key: any): V | undefined {
// @ts-ignore
let pointer = this.items[key];
if (typeof pointer === 'undefined')
return undefined;
this.splayOnTop(pointer);
return this.V[pointer];
}
/**
* Returns the value with the specified key, if it already exists in the cache,
* or creates a new one by calling the provided function.
*
* @param {any} key - Key.
* @param {Function} createFromKey - Function to create a new item.
* @return {any}
*/
getOrCreate(key: K, createFromKey: (key: K) => V): V {
let value = this.get(key);
if (value !== undefined)
return value;
else {
let item = createFromKey(key);
this.set(key, item);
return item;
}
}
}
/**
* @param {HTMLElement} element
* @param {string | ElementOptions | null} options
* @returns {HTMLElement}
* */
export function _options(element: HTMLElement, options: any) {
if (options == null)
return element;
if (typeof options === 'string')
element.className += ` ${options.replace(/,/g, ' ')}`;
if (options.id != null)
element.id = options.id;
if (options.classes != null)
element.className += ` ${options.classes}`;
if (options.style != null)
Object.assign(element.style, options.style);
if (options.processNode != null)
options.processNode(element);
if (options.onClick != null)
element.addEventListener('click', options.onClick);
return element;
}
/**
* Converts entity properties between JavaScript and Dart.
* See also: {@link include}
*/
export function _propsToDart(s: string, cls: string): string {
const jsToDart: { [indes:string] : {[index: string]: string} } = {
'Group' : {
'adminMemberships': 'parents.parent',
'memberships': 'parents.parent',
'members': 'children.child',
'adminMembers': 'children.child',
},
'Project': {
'children': 'relations.entity',
'links': 'relations.entity'
},
'Function': {
'inputs': 'params',
'outputs': 'params'
}
};
let propsMap = jsToDart[cls];
if (!propsMap)
return s;
let res = '';
if (s === res) return res;
let ents = s.split(',');
for (let ent of ents) {
let props = ent.trim();
while (props) {
let idx = props.indexOf('.');
let match = propsMap[props];
if (match) res += match;
else {
let p = (idx === -1) ? props : props.slice(0, idx);
res += propsMap[p] || p;
}
if (idx === -1) props = '';
else {
props = props.slice(idx + 1);
res += '.';
}
}
res += ',';
}
return res;
}
export function format(x: number, format?: string): string {
return api.grok_Utils_FormatNumber(x, format);
}
/** Autotest-related helpers */
export namespace Test {
/** Specifies whether the TestManager works in benchmark on unit test mode.
* This is controlled by the TestManager's "Benchmark" check box.
*
* Use this variable in the individual tests to control whether it
* runs as a quick unit test (for instance, set small number of iterations)
* or a benchmark (set big number of iterations).
*
* Results of the benchmarks, including time, are captured during the overnight
* automated testing and are reported to the metrics database. This allows to
* identify performance regressions, compare how fast tests perform under
* different conditions, etc.
* */
export let isInBenchmark = false;
export function getTestDataGeneratorByType(type: string) {
return api.grok_Test_GetTestDataGeneratorByType(type);
}
export function getInputTestDataGeneratorByType(inputType: string) {
return api.grok_Test_GetInputTestDataGeneratorByType(inputType);
}
}