-
Notifications
You must be signed in to change notification settings - Fork 4
/
huviz.js
16634 lines (15325 loc) · 560 KB
/
huviz.js
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
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
SortedSet
SortedSet is a javascript Array which stays sorted and permits only
once instance of each itm to be added.
It adds these methods:
add(itm)
has(itm) => bool
acquire(itm) # remove(itm) from prior set then add(itm) to this
sort_on(f_or_k) # eg
sort_on('some_property_name')
sort_on(function(a,b){ return -1,0 or 1})
remove(itm)
binary_search(sought[,ret_ins_idx])
clear() # empty the set
SortedSet also supports the notion of items belonging to mutually exclusive
sets, represented as "being in mutually exclusive states".
isState(state_name) # makes the sortedset act in mutual exclusion with others
If an item is "in a state" then its .state property contains a link to
the sortedset "it is currently in".
If one wants to record membership on items by attaching flags to them
this can be accomplished with SortedSet.isFlag(flag_name)
isFlag(flag_name) # items in the set get the property [flag_name]
dead = new SortedSet().isState('dead')
alive = new SortedSet().isState('alive')
sick = new SortedSet().isFlag('sick')
amputee = new SortedSet().isFlag('amputee')
alice = {'id':'alice'};
alive.add(alice);
amputee.add(alice)
alice.state == alive; // ==> true
alice.state == dead; // ==> false
dead.acquire(alice);
!!alice.amputee == true; // ==> true
author: Shawn Murphy <smurp@smurp.com>
written: 2013-11-15
funder: TM&V -- The Text Mining and Visualization project
Copyright CC BY-SA 3.0 See: http://creativecommons.org/licenses/by-sa/3.0/
http://stackoverflow.com/a/17866143/1234699
provided guidance on how to 'subclass' Array
"use strict";
*/
var SortedSet = function(){
if (arguments.callee) {arguments.callee.NUMBER_OF_COPIES += 1;}
if (window) {
if (!window.NUM_SORTEDSET){window.NUM_SORTEDSET = 0}
window.NUM_SORTEDSET += 1;
}
var array = [];
array.push.apply(array,arguments);
array.case_insensitive = false;
array.case_insensitive_sort = function(b) {
if (typeof b == 'boolean') {
if (b) { // INSENSITIVE
array.cmp_options.caseFirst = false;
array.cmp_options.sensisitivity = 'base';
} else { // SENSITIVE
array.cmp_options.caseFirst = 'upper';
array.cmp_options.sensisitivity = 'case';
}
array.case_insensitive = b;
array.resort();
}
return array;
}
array.cmp_options = {numeric: true, caseFirst: 'upper', sensitivity: 'case'};
array._f_or_k = 'id';
array._cmp_instrumented = function(a, b){
/*
Return a negative number if a < b
Return a zero if a == b
Return a positive number if a > b
*/
var f_or_k = array._f_or_k,
av = (''+(a && a[f_or_k]) || ''),
bv = (''+(b && b[f_or_k]) || '');
//console.log(f_or_k, array.case_insensitive);
// xcase are squashed iff needed
var acase = array.case_insensitive && av.toLowerCase() || av,
bcase = array.case_insensitive && bv.toLowerCase() || bv;
if (!array.case_insensitive && acase != av) {
throw new Error("" + acase + " <> " + av + " BUT SHOULD EQUAL")
}
//var retval = av.localeCompare(bv, 'en', array.cmp_options);
var retval = (acase < bcase) && -1 || ((acase > bcase) && 1 || 0);
/*
if (window.SORTLOG) {
console.error(`${array.id}.${f_or_k} ${array.case_insensitive&&'IN'||''}SENSITIVE "${av}" ${DIR[retval+1]} "${bv} (${retval})"`, array.cmp_options);
}
*/
array._verify_cmp(av, bv, retval);
return retval;
}
array._cmp = function(a, b) {
var f_or_k = array._f_or_k,
av = (''+(a && a[f_or_k]) || ''),
bv = (''+(b && b[f_or_k]) || '');
// xcase are squashed iff needed
var acase = array.case_insensitive && av.toLowerCase() || av,
bcase = array.case_insensitive && bv.toLowerCase() || bv;
return (acase < bcase) && -1 || ((acase > bcase) && 1 || 0);
}
array._verify_cmp = function(av, bv, retval) {
var dir = ['less than', 'equal to', 'greater than'][retval+1],
right_or_wrong = 'wrongly',
sense = (!array.case_insensitive && 'UN' || '') + 'SQUASHED',
acase = array.case_insensitive && av.toLowerCase() || av,
bcase = array.case_insensitive && bv.toLowerCase() || bv;
if (sense == 'UNSQUASHED' &&
acase != av &&
av.toLowerCase() != av) {
/*
Is case INSENSITIVE comparison happening on the right values?
Confirm that when a case_insensitive sort is happening
AND the value upon which comparison actually happens (acase) differs from av
AND that there are uppercase characters to squash
*/
throw new Error(""+sense+"("+array.case_insensitive + ") but av(" + av + ") compared as acase(" + acase +")");
}
var tests = ['(retval > 0 && acase <= bcase)',
'(retval < 0 && acase >= bcase)',
'(retval == 0 && acase != bcase)'];
tests.forEach(function(test){
if (eval(test)) {
throw new Error("" +test + " SHOWS _cmp(" + sense + ") " + right_or_wrong +
" calling a(" + acase + ") " + dir +" b(" + bcase + ")");
}
});
right_or_wrong = 'rightly';
//console.error(`_cmp(${sense}) ${right_or_wrong} calling a(${acase}) ${dir} b(${bcase})`);
}
array.sort_on = function(f_or_k){ // f_or_k AKA "Function or Key"
// f_or_k: a comparison function returning -1,0,1
var DIR = ['<','=','>'];
if (typeof f_or_k == 'string'){ // item object key to sort on the value of
array._f_or_k = f_or_k
} else if (typeof f_or_k == 'function'){
array._cmp = f_or_k;
} else {
throw new Error("sort_on() expects a function or a property name");
}
array.resort()
return array;
}
array.resort = function() {
//if (window.SORTLOG) { console.groupCollapsed('resort') }
array.sort(array._cmp);
//if (window.SORTLOG) { console.groupEnd('resort') }
}
array.clear = function(){
array.length = 0;
for (var i = array.length - 1; i > -1; i--) {
array[i].remove()
}
return array.length == 0; // should be zero now
};
array.isState = function(state_property){
/*
* Calling isState() on a SortedSet() prepares it so that
* when add() is called then the SortedSet is registered on
* the itm.state property. This means that if the item
* is moved to a different SortedSet then it's state can
* be tested and altered. This enforces mutually exlusive item
* membership among the sets which all have isState() asserted.
*/
array.state_property = state_property || 'state';
return array;
};
array.isFlag = function(flag_property){
/*
* Calling isFlag() on a SortedSet() prepares it so that
* when add() is called then the SortedSet is registered on
* the itm[flag_property]. When the item is removed from
* the isFlagged SortedSet then that flag_property is deleted.
* The default value for the name of the flag_property is
* simply the name of SortedSet(). The motivation of course
* is for many flags to be able to be set on each node, unlike
* states, which are mutually exclusive.
*/
array.flag_property = flag_property || array.id; // array.state_name
return array;
};
/*
Maintain a containership heirarchy on the slots 'subsets' and 'superset'
*/
array.subsets = [];
array.sub_of = function(superset) {
array.superset = superset;
superset.has_sub(array);
return array;
};
array.has_sub = function(subset) {
array.subsets.push(subset);
if (subset.superset != array) {
subset.superset = array;
}
return array;
};
array.named = function(name){
array.id = name;
return array;
};
array.labelled = function(label) {
array.label = label;
return array;
};
array.get_label = function() {
return array.label || array.id;
};
array.sort_on('id');
array.toggle = function(itm){
// Objective:
// add() or remove() as needed
if (array.has(itm)) {
array.remove(itm);
} else {
array.add(itm);
}
};
array.alter = function(itm, callback) {
/*
* Objective:
* Alter supports making a possibly position-altering change to an item.
* Naively changing an item could invalidate its sort order breaking
* operations which depend on that order.
* Means:
* Alter finds itm then calls the callback, which might change itm
* in a way which ought to cause a repositioning in the array.
* If the sorted position of itm is now invalid then figure out where
* it should be positioned and move it there, taking care to not be
* distracted during binary_search by the fact that item is already
* in the set but possibly misorderedly so.
*/
var current_idx = array.binary_search(itm, true);
callback();
if (array.validate_sort_at(current_idx, true)) {
return current_idx;
}
// It was NOT in the right position, so remove it and try again
//array.remove(itm); // remove does not work because itm is mis-sorted
return array.nudge_itm_at_(itm, current_idx);
}
array.nudge_itm_at_ = function(itm, current_idx) {
var removed = array.splice(current_idx, 1);
if (removed.length != 1 || removed[0] != itm) {
var msg = "failed to remove " + itm[array._f_or_k] + " during .add()";
console.debug(msg);
//throw new Error(msg);
}
var ideal = array.binary_search(itm, true);
var before_removal_count = array.length;
array.remove(itm);
var after_removal_count = array.length;
if (false && !(before_removal_count - after_removal_count == 1)) {
var msg = "temporarily removing itm extracted " +
(before_removal_count - after_removal_count) +
" items";
console.debug(msg);
// throw new Error(msg);
}
//array.splice(ideal.idx, 0, itm);
array._engage_at(itm, ideal.idx);
return array;
}
array.add = function(itm){
/*
* Objective:
* Maintain a sorted array which acts like a set.
* It is sorted so insertions and tests can be fast.
* Return:
* The index at which it was inserted (or already found)
*/
var c = array.binary_search(itm, true)
if (typeof c == 'number'){ // an integer was returned, ie it was found
return c;
}
array._engage_at(itm, c.idx);
//array.is_sorted();
return c.idx;
}
array._engage_at = function(itm, idx) {
array.splice(idx,0,itm);
if (array.state_property){
itm[array.state_property] = array;
}
if (array.flag_property){
itm[array.flag_property] = array;
}
}
array.has = function(itm){ // AKA contains() or is_state_of()
if (array.state_property){
return itm[array.state_property] == array;
}
if (array.flag_property){
return itm[array.flag_property] == array;
}
alert("we should never get here");
};
array.remove = function(itm){
/*
* Objective:
* Remove item from an array acting like a set.
* It is sorted by cmp, so we can use binary_search for removal
*/
var duds = [];
var c = array.binary_search(itm);
//console.log("c:", c, array.is_sorted());
if (c > -1){ // it was found
duds = array.splice(c, 1); // remove itm into the duds array (expecting length == 1)
if (true) { // report because
var because = ((duds.length != 1) && (""+ duds.length + " removed, not 1")) ||
((duds[0] != itm) && (duds[0].lid + " was removed instead of " + itm.lid)) || "";
if (because) {
console.log(itm[array._f_or_k], '??', duds[0][array._f_or_k])
var msg = "remove failed at idx " + c + " to splice " + itm.id +
" out of "+ array.label + " because "+ because;
console.debug(msg);
//throw new Error(msg);
}
}
} else {
//debugger;
//throw new Error(`remove() is failing at idx ${c} because array.binary_search() failed to find ${itm.lid} in ${array.label}`);
}
if (array.state_property){
itm[array.state_property] = true; // EXAMINE delete instead?
}
if (array.flag_property){
delete itm[array.flag_property];
}
return duds[0];
}
array.acquire = function(itm){
// acquire() is like add() for SortedSet() but it takes care
// of removing itm from the previous SortedSet
var last_state = itm[array.state_property];
if (last_state && last_state.remove){
last_state.remove(itm);
}
return array.add(itm);
};
array.get = function(sought){
var idx = array.binary_search(sought);
if (idx > -1){
return array[idx];
}
};
array.get_by = function(key,val){
var o = {};
o[key] = val;
return this.get(o);
};
array.binary_search = function(sought, ret_ins_idx, callback){
/*
* This method performs a binary-search-powered version of indexOf(),
* that is; it returns the index of sought or returns -1 to report that
* it was not found.
*
* If ret_ins_idx (ie "RETurn the INSertion INdeX") is true then
* instead of returning -1 upon failure, it returns the index at which
* sought should be inserted to keep the array sorted.
*/
ret_ins_idx = ret_ins_idx || false;
var step = 0;
var seeking = true;
if (array.length < 1) {
if (ret_ins_idx) {
return {idx:0};
}
return -1;
}
var mid, mid_node, prior_node, c,
bot = 0,
top = array.length;
while (seeking){
mid = bot + Math.floor((top - bot)/2);
mid_node = array[mid];
c = array._cmp(mid_node, sought);
if (callback) {
callback(array, sought, mid_node, prior_node, {mid: mid, bot: bot, top: top, c: c, step: step})
}
step++;
prior_node = mid_node;
//console.log(" c =",c);
if (c == 0) {
if (callback) {callback(array, null, null, null, {done: true, retval: mid});}
return mid;
}
if (c < 0){ // ie this[mid] < sought
bot = mid + 1;
} else {
top = mid;
}
if (bot == top){
if (ret_ins_idx){
if (callback) {callback(array, null, null, null, {done: true, retval: bot});}
return {idx:bot};
}
if (callback) {callback(array, null, null, null, {done: true, retval: -1});}
return -1;
};
}
}
array.is_sorted = function() { // return true or throw
for (var i = 0; (i + 1) < array.length; i++) {
if (array.length > 1) {
array.validate_sort_at(i);
}
}
return true;
}
array.validate_sort_at = function(i, or_return) {
var
key = array._f_or_k,
after = array[i+1],
tween = array[i],
before = array[i-1],
or_return = !or_return; // defaults to true
// ensure monotonic increase
if (typeof after != 'undefined' && array._cmp(tween, after) > 0) {
if (or_return) {
throw new Error('"' + tween[key] + '" is before "' + after[key] + '"');
} else {
return false;
}
}
if (typeof before != 'undefined' && array._cmp(before, tween) > 0) {
if (or_return) {
throw new Error('"' + before[key] + '" is before "' + tween[key] + '"');
} else {
return false;
}
}
return true;
}
array.dump = function() {
for (var i = 0; i < array.length; i++) {
var node = array[i];
console.log(node.lid, node.name.toString(), node.name);
}
}
array.roll_call = function() {
var out = [];
for (var i = 0; i < array.length; i++) {
out.push(array[i].lid || array[i].id);
}
return out.join(', ');
}
return array;
};
// Instrument SortedSet so we can track the number of 'instances'.
// This is motivated by the fact that it is currently implemented
// not as a regular subclass of Array (which was not possible when it was created)
// but as a set of adornments on an Array instances, thereby incurring
// the burden of each such copy carrying its own copies of the SortedSet
// methods with it. See https://github.com/cwrc/HuViz/issues/259
SortedSet.NUMBER_OF_COPIES = 0;
// These tests are included in the module to simplify testing in the browser.
var SortedSets_tests = function(verbose){
verbose = verbose || false;
var n = function(a,b){
if (a == b) return 0;
if (a < b) return -1;
return 1;
}
var
a = {id:1},
b = {id:2},
c = {id:0},
d = {id:3},
stuff = SortedSet(a,b),
a_d = SortedSet(a,d).sort_on('id'),
ints = SortedSet(0,1,2,3,4,5,6,7,8,10).sort_on(n),
even = SortedSet(0,2,4,6,8,10).sort_on(n),
some_dupes = SortedSet(0,1,2,2,5,7,2,9).sort_on(n);
function expect(stmt,want){
var got = eval(stmt);
if (verbose) console.log(stmt,"==>",got);
if (got != want){
throw stmt + " returned "+got+" expected "+want;
}
}
function assert(be_good, or_throw){
if (! be_good) throw or_throw;
}
function cmp_on_name(a,b){
if (a.name == b.name) return 0;
if (a.name < b.name) return -1;
return 1;
}
function cmp_on_id(a,b){
if (a.id == b.id) return 0;
if (a.id < b.id) return -1;
return 1;
}
expect("cmp_on_id(a,a)",0);
expect("cmp_on_id(a,b)",-1);
expect("cmp_on_id(b,a)",1);
expect("ints.binary_search(0)",0);
expect("ints.binary_search(4)",4);
expect("ints.binary_search(8)",8);
expect("ints.binary_search(9)",-1);
expect("ints.binary_search(9,true).idx",9);
expect("ints.binary_search(-3)",-1);
expect("even.binary_search(1,true).idx",1);
expect("even.binary_search(3,true).idx",2);
expect("even.binary_search(5,true).idx",3);
expect("even.binary_search(7,true).idx",4);
expect("even.binary_search(9,true).idx",5);
expect("even.binary_search(9)",-1);
expect("even.binary_search(11,true).idx",6);
expect("stuff.binary_search(a)",0);
expect("stuff.binary_search(b)",1);
expect("stuff.binary_search(c)",-1);
expect("stuff.binary_search(d)",-1);
expect("a_d.binary_search(c)",-1);
expect("a_d.binary_search(c,true).idx",0);
expect("a_d.binary_search(b,true).idx",1);
expect("a_d.add(b)",1);
expect("a_d.binary_search(a)",0);
expect("a_d.binary_search(b)",1);
expect("a_d.binary_search(d)",2);
expect("a_d.add(c)",0);
};
//Sortedsets_tests();
if (typeof module !== 'undefined' && module.exports) {
module.exports.SortedSet = SortedSet;
}
function hsv2rgb(hue, sat, val) {
// See
// http://en.wikipedia.org/wiki/HSL_and_HSV
// from:
// http://www.actionscript.org/forums/archive/index.php3/t-15155.html
// see also:
// http://www.webreference.com/programming/javascript/mk/column3/creating/cp_mini_gradient_details.png
var red, grn, blu, i, f, p, q, t;
hue%=360; // probably not needed
if(val==0) {return("rgb(0,0,0)");}
sat/=100;
val/=100;
hue/=60;
i = Math.floor(hue);
f = hue-i;
p = val*(1-sat);
q = val*(1-(sat*f));
t = val*(1-(sat*(1-f)));
if (i==0) {red=val; grn=t; blu=p;}
else if (i==1) {red=q; grn=val; blu=p;}
else if (i==2) {red=p; grn=val; blu=t;}
else if (i==3) {red=p; grn=q; blu=val;}
else if (i==4) {red=t; grn=p; blu=val;}
else if (i==5) {red=val; grn=p; blu=q;}
red = Math.floor(red*255);
grn = Math.floor(grn*255);
blu = Math.floor(blu*255);
var r_g_b = [red,grn,blu];
//document.spangle_controls.status.value = r_g_b.valueOf();
return "rgb(" + r_g_b.valueOf() + ")";
}
/* http://jsfiddle.net/EPWF6/9/
* https://en.wikipedia.org/wiki/HSL_and_HSV#Converting_to_RGB
* Unfortunately this is seriously buggy!
*
*/
function hsl2rgb(H, S, L) {
var input_s = "hsl2rgb(" + [H, S, L].toString() + ")";
/*
* H ∈ [0°, 360°)
* S ∈ [0, 1]
* L ∈ [0, 1]
*/
if (H == 360) { H = 359.9999; };
H %= 360;
/* calculate chroma */
var C = (1 - Math.abs((2 * L) - 1)) * S;
/* Find a point (R1, G1, B1) along the bottom three faces of the RGB cube, with the same hue and chroma as our color (using the intermediate value X for the second largest component of this color) */
var H_ = H / 60;
var X = C * (1 - Math.abs((H_ % 2) - 1));
var R1, G1, B1;
if (H === undefined || isNaN(H) || H === null) {
R1 = G1 = B1 = 0;
}
else {
if (H_ >= 0 && H_ < 1) {
R1 = C;
G1 = X;
B1 = 0;
}
else if (H_ >= 1 && H_ < 2) {
R1 = X;
G1 = C;
B1 = 0;
} else if (H_ >= 2 && H_ < 3) {
R1 = 0;
G1 = C;
B1 = X;
} else if (H_ >= 3 && H_ < 4) {
R1 = 0;
G1 = X;
B1 = C;
} else if (H_ >= 4 && H_ < 5) {
R1 = X;
G1 = 0;
B1 = C;
}
else if (H_ >= 5 && H_ < 6) {
R1 = C;
G1 = 0;
B1 = X;
}
}
/* Find R, G, and B by adding the same amount to each component, to match lightness */
var m = L - (C / 2);
var R, G, B;
/* Normalise to range [0,255] by multiplying 255 */
R = (R1 + m) * 255;
G = (G1 + m) * 255;
B = (B1 + m) * 255;
R = Math.round(R);
G = Math.round(G);
B = Math.round(B);
retval = "rgb(" + R + ", " + G + ", " + B + ")";
// console.info(input_s,retval);
return retval;
}
(function() {
d3.fisheye = {
scale: function(scaleType) {
return d3_fisheye_scale(scaleType(), 3, 0);
},
circular: function() {
var radius = 200,
distortion = 2,
k0,
k1,
focus = [0, 0];
function fisheye(d) {
var dx = d.x - focus[0],
dy = d.y - focus[1],
dd = Math.sqrt(dx * dx + dy * dy);
if (!dd || dd >= radius) return {x: d.x, y: d.y, z: 1};
var k = k0 * (1 - Math.exp(-dd * k1)) / dd * .75 + .25;
return {x: focus[0] + dx * k, y: focus[1] + dy * k, z: Math.min(k, 10)};
}
function rescale() {
k0 = Math.exp(distortion);
k0 = k0 / (k0 - 1) * radius;
k1 = distortion / radius;
return fisheye;
}
fisheye.radius = function(_) {
if (!arguments.length) return radius;
radius = +_;
return rescale();
};
fisheye.distortion = function(_) {
if (!arguments.length) return distortion;
distortion = +_;
return rescale();
};
fisheye.focus = function(_) {
if (!arguments.length) return focus;
focus = _;
return fisheye;
};
return rescale();
}
};
function d3_fisheye_scale(scale, d, a) {
function fisheye(_) {
var x = scale(_),
left = x < a,
range = d3.extent(scale.range()),
min = range[0],
max = range[1],
m = left ? a - min : max - a;
if (m == 0) m = max - min;
return (left ? -1 : 1) * m * (d + 1) / (d + (m / Math.abs(x - a))) + a;
}
fisheye.distortion = function(_) {
if (!arguments.length) return d;
d = +_;
return fisheye;
};
fisheye.focus = function(_) {
if (!arguments.length) return a;
a = +_;
return fisheye;
};
fisheye.copy = function() {
return d3_fisheye_scale(scale.copy(), d, a);
};
fisheye.nice = scale.nice;
fisheye.ticks = scale.ticks;
fisheye.tickFormat = scale.tickFormat;
return d3.rebind(fisheye, scale, "domain", "range");
}
})();
// based on https://github.com/talis/rdfquads.js
function Quad(subject,pred,obj,graph) {
this.s = new RdfUri(subject);
this.p = new RdfUri(pred);
this.o = new RdfObject(obj);
this.g = new RdfUri(graph);
}
Quad.prototype.toString = function() {
return '<' + this.s + '> <' + this.p + '> ' + this.o + ' <' + this.g + '> .\n'
}
Quad.prototype.toNQuadString = function() {
return '<' + this.s + '> <' + this.p + '> ' + this.o + ' <' + this.g + '> .\n'
}
var uriRegex = /<([^>]*)>/ ;
function RdfUri(url) {
self = this;
var match = url.match(uriRegex);
if (match) {
self.raw = match[1];
} else {
self.raw = url;
}
}
RdfUri.prototype.toString = function() {
return this.raw;
}
function RdfObject(val) {
self = this;
var match = val.match(uriRegex);
if (match) {
self.raw = match[1];
self.type = 'uri';
} else {
self.raw = val;
self.type = 'literal';
}
}
RdfObject.prototype.toString = function() {
return this.raw;
}
RdfObject.prototype.isUri = function() {
return this.type == 'uri';
}
RdfObject.prototype.isLiteral = function() {
return this.type == 'literal';
}
var quadRegex = /\s*(<[^>]*>|_:[A-Za-z][A-Za-z0-9]*)\s*(<[^>]*>)\s*(".*"?|<[^>]*>|_:[A-Za-z][A-Za-z0-9]*)\s*(<[^>]*>).*\s*\.\s*\#*.*$/ ;
var isComment = /^\s*\/\// ;
function parseQuadLine(line) {
if (line == null || line === "" || line.match(isComment)) {
return null;
} else {
//console.log ("parseQuadLine(",line,")");
var match = line.match(quadRegex);
//console.log("match",match,line);
if (match){
var s = match[1].trim();
var p = match[2].trim();
var o = match[3].trim();
var g = match[4].trim();
return new Quad(s,p,o,g);
//} else {
// console.log("no match: "+line);
}
}
}
"use strict";
/*
MultiString
purpose:
Provide an object that can be used in place of a string, in that
it returns a string value, but actually stores multiple per-language
versions of the string and can be reconfigured as to which different
language version gets returned.
see:
https://stackoverflow.com/a/28188150/1234699
features:
Expects langpath values which are colon-delimted lists of 2-letter
language codes. The final value in the list can instead be the
word ANY, a wildcard meaning that every language not yet represented
in the language path would be respected too.
Note that MultiStrings can also have 'NOLANG' values, that is, values
with no associated language. These are always on the priority list,
but at the end, even after ANY is respected.
If one wants to make referencde to the "NOLANG" value, one can
do so explicitly with the term NOLANG in the langpath.
examples:
"en"
means that if an English value has been provided it will be
respected and that if there is a NOLANG value it will be shown
otherwise. On no account will values in other languages be shown.
"en:fr"
If a MultiString instance has an English value it will be shown,
otherwise if a French value is available it will be shown, failing
that if a NOLANG value is present, it will be shown.
"en:ANY"
If a MultiString instance has an English value it will be shown,
otherwise if value tagged with any other lanugage is available
it will be shown (in no particular order) and finally if a
"NOLANG" value is present, it will be shown.
"en:NOLANG:ANY"
Show English if available, or the NOLANG value or a value from
ANY other language if present -- in that priority order.
*/
function MultiString() {
var i;
if (arguments.length == 0) {
this.set_val_lang()
} else {
i = -1;
while (typeof(arguments[i+=1]) != 'undefined') { // an empty string is a legal name
// process value/lang pairs
this.set_val_lang(arguments[i] || '', arguments[i+=1]);
}
}
Object.defineProperty(
this, 'length',
{get: function () { return (this.valueOf()||'').length; }});
};
// inherit all properties from native class String
MultiString.prototype = Object.create(String.prototype);
MultiString.prototype.set_val_lang = function(value, lang) {
// set a value/lang pair where undefined lang sets NOLANG value
if (lang) {
this[lang] = value;
} else {
this.NOLANG = value || '';
}
};
MultiString.langs_in_path = []; // default value
MultiString.prototype.set_lang_val = function(lang, value) {
this.set_val_lang(value, lang);
};
MultiString.prototype.get_ANY_but_langs_in_path = function() {
var langs_in_path = MultiString.langs_in_path;
for (var key in this) {
if (this.hasOwnProperty(key)) {
if (langs_in_path.indexOf(key) == -1) {
return this[key];
}
}
};
};
MultiString.prototype.get_ALL = function() {
var retval = '';
for (var key in this) {
if (key.length == 2) {
if (retval) {
retval += ', '
}
retval += '"' + this[key] + '"@' + key;
} else if (key == 'NOLANG') {
if (retval) {
retval += ', '
}
retval += '"' + this[key] + '"';
}
};
return retval;
};
const LANGCODE_RE = /^[a-z]{2}$/
MultiString.set_langpath = function(langpath){
var langs = [],
parts = [],
langs_in_path = [],
nolang_used = false;
if (langpath) {
parts = langpath.split(':');
parts.forEach(function(p,idx){
if (p.match(LANGCODE_RE)) {
langs.push(p);
langs_in_path.push(p);
} else if (p == 'NOLANG') {
nolang_used = true;
langs.push(p);
} else if (p == 'ANY') {
langs.push("get_ANY_but_langs_in_path()")
} else if (p == 'ALL') {
langs.push("get_ALL()")
} else {
throw new Error("<" + p + "> is not a legal term in LANGPATH");
}
});
}
MultiString.langs_in_path = langs_in_path;
var body = "return";
if (langs.length) {
body += " this."+langs.join(' || this.');
body += " || ";
}
body += "''";
// Compile a new function which follows the langpath for the value
// so String.prototype methods can get to the value
MultiString.prototype.toString =
MultiString.prototype.valueOf =
new Function(body);
};
MultiString.set_langpath('ANY:NOLANG'); // set the default langpath
// Extend class with a trivial method
MultiString.prototype.behead = function(){
return this.substr(1);
}
if (typeof module !== 'undefined' && module.exports) {
module.exports.MultiString = MultiString;
}
"use strict";
function OnceRunner(verbosity, name) {
this.verbosity = verbosity || 0;
this.profileName = (name || 'OnceRunner') + '_' + (new Date()).toISOString();
this.active = false;
};
OnceRunner.prototype.setTimeout = function(cb, msec) {
//console.log(`afterMsec_call(${msec}) typeof cb ==>`,typeof cb);
if (! this.firstCallTime) { // when a new string of calls begins
this.firstCallTime = Date.now();
this.active = true;
this.stats = {};
if (this.verbosity > 39) {
console.profile(this.profileName);
}
this.clearCount = 0; // the number of times execution has been delayed
}
if (this.timeoutID) {
if (this.verbosity > 29) {
console.warn("clearTimeout()", this.timeoutID._idleStart || this.timeoutID);
}
clearTimeout(this.timeoutID);
this.clearCount++;
}
//cb = function() {console.log("mockFunc() called")};
return this.timeoutID = setTimeout(this.makeWrapper(cb), msec);
};
OnceRunner.prototype.makeWrapper = function(callback) {