-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhealth.js
More file actions
1079 lines (927 loc) · 32.5 KB
/
Copy pathhealth.js
File metadata and controls
1079 lines (927 loc) · 32.5 KB
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
function prepareGraphJson(json) {
var callableParams = ['xTickFormatter', 'xTooltipFormatter', 'xRangeFormatter', 'yTickFormatter', 'yTooltipFormatter', 'x_on_zoom', 'sideLegend'];
callableParams.forEach(function (k) {
if (typeof json[k] === 'string') {
json[k] = eval('(' + json[k] + ')');
}
});
var customFormatters;
if (customFormatters = json.yCustomTooltipFormatters) {
for (var k in customFormatters) {
if (typeof customFormatters[k] === 'string') {
customFormatters[k] = eval('(' + customFormatters[k] + ')');
}
};
}
console.log('new json', json);
return json;
}
function zoomGraphX(x, tokenData) {
var basePath = window.basePath || '';
return fetch(basePath + '/zoomed', {
method: 'post',
headers: {
"Content-type": "application/x-www-form-urlencoded; charset=UTF-8"
},
body: 'x=' + x + '&id=' + tokenData.id + '&date=' + tokenData.date + '&token=' + tokenData.token
}).then(function (response) {
if (response.status !== 200) {
console.error('Zoom fetch error. Status Code: ', response);
return Promise.reject();
}
return response.json().then(function (json) {
return prepareGraphJson(json);
});
});
}
function fetchGraph(id, tokenData, retry) {
var domEl = document.getElementById(id);
if (!domEl) {
console.warn('graph el #' + id + ' not found');
return;
}
var loadingEl = domEl.querySelector('.chart_wrap_loading');
var basePath = window.basePath || '';
retry = retry || 0;
return fetch(basePath + '/asyncgraph' + (tokenData.test ? '?_test=1' : ''), {
method: 'post',
headers: {
"Content-type": "application/x-www-form-urlencoded; charset=UTF-8"
},
body: 'id=' + tokenData.id + '&date=' + tokenData.date + '&token=' + tokenData.token
}).then(function (response) {
if (response.status !== 200) {
console.error('Async graph fetch error. Status Code: ', response);
if (response.status === 500) {
loadingEl.innerHTML = 'Internal Server Error, Retrying '+(++retry) +'...';
setTimeout(function () {
fetchGraph(id, tokenData, retry)
}, 1000 * retry);
}
return;
}
return response.json().then(function (json) {
renderGraph(id, json);
});
});
}
function renderGraph(id, json, initial) {
var domEl = document.getElementById(id);
var loadingEl = domEl.querySelector('.chart_wrap_loading');
var isScatter = json.scatter || (json.types && Object.keys(json.types).some(function(k){return json.types[k]=='scatter'}));
var dynEnabled = isDynamicEnabled(json);
var rawJson = dynEnabled ? deepCloneJson(json) : null;
if (!isScatter && (!json.columns.length || json.columns[0].length <= 2)) {
if (loadingEl) {
loadingEl.innerHTML = 'Not enough data to display.';
}
return;
}
if (dynEnabled && detectModeFromJson(rawJson) === 'relative') {
delete json.x_on_zoom;
}
json = prepareGraphJson(json);
var chart = Graph.render(domEl, json);
domEl.style.setProperty('transition-duration', '0s', 'important');
domEl.classList.add('chart_wrap_rendered');
domEl.offsetTop; // force reflow
domEl.style.removeProperty('transition-duration');
var outerWrap = domEl.closest('.chart_wrap_outer');
if (outerWrap) {
outerWrap.classList.add('chart_wrap_rendered');
}
window.charts = window.charts || {};
window.charts[id] = chart;
if (dynEnabled) {
try {
initDynamicModeToggle(domEl, chart, rawJson);
} catch (e) {
console.warn('dynamic toggle init failed', e);
}
}
setTimeout(function () {
if (loadingEl) {
loadingEl.parentNode && loadingEl.parentNode.removeChild(loadingEl)
}
}, 1000);
if (json.csvExport) {
var exportHTML = '<div class="chart_csv_export_wrap"><a class="csv_link" href="' + json.csvExport + '"><span class="glyphicon glyphicon-download-alt"></span> CSV</a>'+(json.addExport||'')+'</div>';
var t = document.createElement('div');
t.innerHTML = exportHTML;
domEl.appendChild(t.firstChild);
}
if (json.descEditData || domEl.getAttribute('data-desc-addr')) {
var editPill = document.createElement('a');
editPill.className = 'graph_desc_edit_pill';
editPill.innerHTML = '<span class="glyphicon glyphicon-pencil"></span>';
var descData = json.descEditData || {
addr: domEl.getAttribute('data-desc-addr'),
graphId: domEl.getAttribute('data-desc-graph-id'),
keywords: domEl.getAttribute('data-desc-keywords'),
descMd: domEl.getAttribute('data-desc-md')
};
editPill.setAttribute('data-addr', descData.addr);
editPill.setAttribute('data-graph-id', descData.graphId);
editPill.setAttribute('data-keywords', descData.keywords);
editPill.setAttribute('data-desc-md', descData.descMd);
var exportWrap = domEl.querySelector('.chart_csv_export_wrap');
if (exportWrap) {
exportWrap.appendChild(editPill);
} else {
var wrap = document.createElement('div');
wrap.className = 'chart_csv_export_wrap';
wrap.appendChild(editPill);
domEl.appendChild(wrap);
}
}
if (json.descHtml) {
var descRow = document.createElement('div');
descRow.className = 'graph_desc_row';
descRow.innerHTML = '<span class="graph_desc_text">' + json.descHtml + '</span>';
domEl.parentNode.insertBefore(descRow, domEl.nextSibling);
}
return chart;
}
function statsFormatXCategories(x, categories) {
return categories[x] === undefined ? '' : categories[x];
}
function statsFormatKMBT(x, kmbt, precision) {
return window.Graph.units.TUtils.statsFormatKMBT(x, kmbt, precision);
}
function statsFormatDayHourFull(hour) {
return hour + ':00-' + hour + ':59';
}
function statsFormatDayHour(hour) {
return hour + ':00';
}
var statShortMonths = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
var statShortWeekDays = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
function statsFormat(period) {
switch (period) {
case 'minute':
case '5min':
return statsFormat5min;
case 'hour':
return statsFormatHour;
case 'week':
return statsFormatWeek;
case 'month':
return statsFormatMonth;
case 'day':
default:
return null;
}
}
function statsTooltipFormat(period) {
switch (period) {
case 'week':
return statsFormatWeekFull;
case 'month':
return statsFormatMonthFull;
}
return statsFormat(period);
}
function formatNumber(number, decimals, decPoint, thousandsSep) {
number = (number + '').replace(/[^0-9+\-Ee.]/g, '')
var n = !isFinite(+number) ? 0 : +number
var prec = !isFinite(+decimals) ? 0 : Math.abs(decimals)
var sep = (typeof thousandsSep === 'undefined') ? ',' : thousandsSep
var dec = (typeof decPoint === 'undefined') ? '.' : decPoint
var s = ''
var toFixedFix = function (n, prec) {
if (('' + n).indexOf('e') === -1) {
return +(Math.round(n + 'e+' + prec) + 'e-' + prec)
} else {
var arr = ('' + n).split('e')
var sig = ''
if (+arr[1] + prec > 0) {
sig = '+'
}
return (+(Math.round(+arr[0] + 'e' + sig + (+arr[1] + prec)) + 'e-' + prec)).toFixed(prec)
}
}
s = (prec ? toFixedFix(n, prec).toString() : '' + Math.round(n)).split('.')
if (s[0].length > 3) {
s[0] = s[0].replace(/\B(?=(?:\d{3})+(?!\d))/g, sep)
}
if ((s[1] || '').length < prec) {
s[1] = s[1] || ''
s[1] += new Array(prec - s[1].length + 1).join('0')
}
return s.join(dec)
}
function statsFormatAxisPercent(value) {
return value + '%';
}
function statsFormatPercent(value) {
var decimals = Math.floor(value * 100) % 100 > 0 ? 2 : 0;
return formatNumber(value, decimals, '.', ',') + '%';
}
function statsFormatAxisAmountTpl(tpl, factor, value, decimals) {
if (value % factor > 0) {
decimals = decimals || 0;
var max_decimals = Math.log10(factor);
while (value % Math.pow(10, max_decimals - decimals)) {
decimals++;
if (decimals >= 3) break;
}
value = formatNumber(value / factor, decimals, '.', ',');
} else {
value = statsFormatKMBT(value / factor);
}
return tpl.replace('{value}', value);
}
function statsFormatAxisAmountFn(tpl, factor, decimals) {
return function(value) {
return statsFormatAxisAmountTpl(tpl, factor, value, decimals || 0);
};
}
function statsFormatAxisAmount(value) {
return statsFormatAxisAmountTpl('€ {value}', 1000000, value, 2);
}
function statsFormatAmountTpl(tpl, factor, value, decimals, rate) {
decimals = decimals || 0;
var max_decimals = Math.log10(factor);
while (value % Math.pow(10, max_decimals - decimals) &&
value < Math.pow(10, max_decimals + 4 - decimals)) {
decimals++;
}
var def_val = '';
if (rate && rate != 1) {
def_val = statsFormatAmount(value);
value /= rate;
}
value = formatNumber(value / factor, decimals, '.', ',');
return tpl.replace('{value}', value) + (def_val ? ' = ' + def_val : '');
}
function statsFormatAmountFn(tpl, factor, decimals, rate) {
return function(value) {
return statsFormatAmountTpl(tpl, factor, value, decimals || 0, rate || 0);
};
}
function statsFormatAmount(value) {
return statsFormatAmountTpl('€ {value}', 1000000, value, 2);
}
function statsFormat5min(time) {
return new Date(time).toUTCString().match(/(\d+:\d+):/)[1];
}
function statsFormatHour(time) {
var date = new Date(time);
return statShortMonths[date.getUTCMonth()] + ', ' + date.getUTCDate() + ' ' + date.toUTCString().match(/(\d+:\d+):/)[1];
}
function statsFormatPeriod(time, days) {
var dt = new Date(time),
de = new Date(time + (days - 1) * 86400000);
var dtm = dt.getUTCMonth(), dem = de.getUTCMonth(),
dtd = dt.getUTCDate(), ded = de.getUTCDate();
if (dtm == dem) {
return dtd + '-' + ded + ' ' + statShortMonths[dtm];
} else {
return dtd + ' ' + statShortMonths[dtm] + ' - ' + ded + ' ' + statShortMonths[dem];
}
}
function statsFormatPeriodFull(time, days) {
var dt = new Date(time),
de = new Date(time + (days - 1) * 86400000);
var dty = dt.getUTCFullYear(), dey = de.getUTCFullYear(),
dtm = dt.getUTCMonth(), dem = de.getUTCMonth(),
dtd = dt.getUTCDate(), ded = de.getUTCDate();
if (dty != dey) {
return dtd + ' ' + statShortMonths[dtm] + ' ' + dty + ' – ' + ded + ' ' + statShortMonths[dem] + ' ' + dey;
} else {
return dtd + ' ' + statShortMonths[dtm] + ' – ' + ded + ' ' + statShortMonths[dem] + ' ' + dey;
}
}
function statsFormatWeek(time) {
return statsFormatPeriod(time, 7);
}
function statsFormatWeekFull(time) {
return statsFormatPeriodFull(time, 7);
}
function statsFormatMonth(time) {
return statsFormatPeriod(time, 30);
}
function statsFormatMonthFull(time) {
return statsFormatPeriodFull(time, 30);
}
function statsFormatTooltipValue(val) {
if (val.toLocaleString) {
return val.toLocaleString();
}
return val + '';
// return statsFormatNumber(val)
}
function statsFormatEmpty() {
return '';
}
function statsOnZoom(zoomToken) {
return function (x) {
console.log('On Zoom', x, zoomToken);
var copyText = x
if (!(x % 1000) &&
x > 1376438400000 && // launch date
x < +(new Date()) + 365 * 86400 * 1000) {
copyText /= 1000;
}
copyToClipboard(copyText);
if (zoomToken) {
return zoomGraphX(x, zoomToken);
} else {
return Promise.reject('default');
}
}
}
function statsNeedSideLegend() {
var width = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
return width > 1200;
}
function dropdownFilterChange(el, event) {
var query = el.value.toString().trim().toLowerCase();
var menu = el.parentNode.parentNode;
var items = menu.querySelectorAll('li');
var len = items.length;
var i, item, text, matches;
for (i = 0; i < len; i++) {
item = items[i];
if (item.classList.contains('dropdown-filter')) {
continue;
}
text = item.innerText.trim().toLowerCase();
matches = !query.length ||
text.startsWith(query) ||
text.indexOf(' ' + query) != -1 ||
text.indexOf('.' + query) != -1;
item.classList.toggle('hidden', !matches);
if (event && event.keyCode == 13 && matches) {
location.href = item.querySelector('a').href;
return cancelEvent(event);
}
}
}
function dropdownToggle(el) {
var field = el.parentNode.querySelector('.dropdown-filter-field');
if (field) {
field.value = '';
dropdownFilterChange(field);
setTimeout(function () {
field.focus();
}, 100);
}
}
function cancelEvent(event) {
event = event || window.event;
if (event) event = event.originalEvent || event;
if (event.stopPropagation) event.stopPropagation();
if (event.preventDefault) event.preventDefault();
return false;
}
window.addEventListener('load', function () {
var hlWrapper = document.querySelector('#hl-wrapper');
if (hlWrapper) {
hlWrapper.classList.add('transitions-ready');
}
});
function copyToClipboard(text) {
if (navigator.clipboard && window.isSecureContext) {
navigator.clipboard.writeText(text).then(() => {
// console.log('Copied to clipboard successfully!');
}).catch(err => {
// console.error('Failed to copy:', err);
fallbackCopyToClipboard(text); // Use fallback if clipboard API fails
});
} else {
// Fallback for older browsers
fallbackCopyToClipboard(text);
}
}
function fallbackCopyToClipboard(text) {
const textArea = document.createElement("textarea");
textArea.value = text;
textArea.style.position = "fixed"; // Prevent scrolling to the bottom
textArea.style.opacity = 0; // Make it invisible
document.body.appendChild(textArea);
textArea.select();
try {
document.execCommand("copy");
// console.log('Copied to clipboard using fallback!');
} catch (err) {
// console.error('Fallback copy failed:', err);
}
document.body.removeChild(textArea);
}
/** Autogen anchors **/
function hlSlugify(text) {
return (text || '')
.toLowerCase()
.replace(/[0-9.,]+\s*[kmb]?/g, '')
.trim()
.replace(/['"]/g, '')
.replace(/[^a-z0-9]+/g, '-')
.replace(/^-+|-+$/g, '') || 'section';
}
function hlHasFollowingContent(header) {
var next = header.nextSibling;
var headingRegex = /^H[1-4]$/;
var ignoreTags = ['SCRIPT', 'STYLE', 'LINK', 'TEMPLATE', 'META'];
while (next) {
if (next.nodeType === 1) {
if (headingRegex.test(next.tagName)) {
// has txt
if (next.textContent && next.textContent.trim().length > 0) {
return false;
}
// no txt, ignore
}
// not header, check content
else if (ignoreTags.indexOf(next.tagName) === -1) {
// content
return true;
}
}
// txt node
else if (next.nodeType === 3) {
// non blnk
if (next.textContent && next.textContent.trim().length > 0) {
return true;
}
}
next = next.nextSibling;
}
// end of parent, no content
return false;
}
function hlInitAnchors() {
var selector = '.hl-page h1, .hl-page h2, .hl-page h3, .hl-page h4';
var headings = document.querySelectorAll(selector);
var usedIds = {};
headings.forEach(function(h) {
// doubl anchor
if (h.querySelector('.anchor')) return;
// no txt
if (!h.textContent || h.textContent.trim().length === 0) return;
// empty section under h
if (!hlHasFollowingContent(h)) return;
// get slug
var text = h.textContent;
var id = h.getAttribute('id');
if (!id) {
var baseSlug = hlSlugify(text);
id = baseSlug;
var counter = 1;
while (usedIds[id] || document.getElementById(id)) {
id = baseSlug + '-' + (++counter);
}
h.setAttribute('id', id);
}
usedIds[id] = true;
// add to dom
var anchor = document.createElement('a');
anchor.className = 'anchor';
anchor.href = '#' + id;
anchor.setAttribute('aria-label', 'Link to this section');
var icon = document.createElement('i');
icon.className = 'anchor-icon';
anchor.appendChild(icon);
h.prepend(anchor);
});
}
// anchor event
document.addEventListener('click', function(e) {
var link = e.target.closest('.anchor');
if (!link) return;
var href = link.getAttribute('href');
if (!href || href.indexOf('#') !== 0) return;
e.preventDefault();
var id = href.substring(1);
var fullUrl = window.location.href.split('#')[0] + '#' + id;
copyToClipboard(fullUrl);
if (history.pushState) {
history.pushState(null, null, '#' + id);
} else {
window.location.hash = id;
}
var target = document.getElementById(id);
if (target) {
target.scrollIntoView({ behavior: 'smooth', block: 'start' });
}
});
// auto anchor init
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', hlInitAnchors);
} else {
hlInitAnchors();
}
// hack bcs some tables load async but wrap their own header so it doesn't autogen anchor on load
var hlObserverTimeout;
var hlObserver = new MutationObserver(function(mutations) {
// dbounce
if (hlObserverTimeout) clearTimeout(hlObserverTimeout);
hlObserverTimeout = setTimeout(hlInitAnchors, 200);
});
hlObserver.observe(document.body, { childList: true, subtree: true });
// dynamic graph
function deepCloneJson(obj) {
return JSON.parse(JSON.stringify(obj));
}
function isScatterJson(json) {
if (json.scatter) return true;
if (json.types) {
for (var k in json.types) {
if (json.types[k] === 'scatter') return true;
}
}
return false;
}
function getYSeriesKeys(json) {
var keys = [];
if (!json.types) return keys;
for (var k in json.types) {
if (k !== 'x' && json.types[k] !== 'x') keys.push(k);
}
return keys;
}
function detectModeFromJson(json) {
// relative aka area graph
var keys = getYSeriesKeys(json);
for (var i = 0; i < keys.length; i++) {
if (json.types && json.types[keys[i]] === 'area') return 'relative';
}
return 'absolute';
}
function isDynamicEnabled(json) {
return !!(json && (json.dynamic === true || (json.dynamic && json.dynamic.enabled)));
}
function isDynamicEligible(rawJson) {
if (!isDynamicEnabled(rawJson)) return false;
if (isScatterJson(rawJson)) return false;
if (rawJson.y_scaled) return false;
var yKeys = getYSeriesKeys(rawJson);
if (yKeys.length < 2) return false;
// care for stacked-style charts for abs/rel
// stacked=true fot multi-series bars/areas
if (!rawJson.stacked) return false;
return true;
}
function buildModeJson(rawJson, mode) {
var j = deepCloneJson(rawJson);
var yKeys = getYSeriesKeys(j);
for (var i = 0; i < yKeys.length; i++) {
j.types[yKeys[i]] = (mode === 'relative') ? 'area' : 'bar';
}
// relative charts should not use server detail zoom token
// abs charts may use x_on_zoom
if (mode === 'relative') {
delete j.x_on_zoom;
}
// check pieZoomRange exists for relative pie zoom trans if bknd didnt give it
if (mode === 'relative' && j.pieZoomRange == null) {
try {
var xCol = j.columns && j.columns[0];
if (xCol && xCol.length >= 3 && xCol[0] === 'x') {
var xMin = xCol[1];
var xMax = xCol[xCol.length - 1];
j.pieZoomRange = xMax - xMin;
}
} catch (e) {}
}
return j;
}
function syncChartState(fromChart, toChart) {
var ysLen = (fromChart && fromChart.data && fromChart.data.ys) ? fromChart.data.ys.length : 0;
var enabled = new Array(ysLen);
var enabledCount = 0;
for (var i = 0; i < ysLen; i++) {
enabled[i] = !!fromChart.state['e_' + i];
if (enabled[i]) enabledCount++;
}
if (!enabledCount && ysLen) enabled[0] = true;
var x1 = fromChart.state.x1;
var x2 = fromChart.state.x2;
toChart.state.x1 = x1;
toChart.state.x2 = x2;
toChart.state.xg1 = fromChart.state.xg1;
toChart.state.xg2 = fromChart.state.xg2;
toChart.state.xgMin = fromChart.state.xgMin;
toChart.state.xgMax = fromChart.state.xgMax;
toChart.state.xg1Ind = fromChart.state.xg1Ind;
toChart.state.xg2Ind = fromChart.state.xg2Ind;
for (var j = 0; j < ysLen; j++) {
var e = enabled[j];
toChart.state['e_' + j] = e;
toChart.state['o_' + j] = e ? 1 : 0;
toChart.state['om_' + j] = e ? 1 : 0;
}
toChart.state.activeColumnsCount = enabledCount || 1;
var rangeGraph = toChart.getYMinMax(x1, x2, false, true);
var rangeMini = toChart.getYMinMax(toChart.state.xg1, toChart.state.xg2, true);
if (toChart.pairY) {
for (var k = 0; k < ysLen; k++) {
toChart.state['y1_' + k] = rangeGraph.min[k];
toChart.state['y2_' + k] = rangeGraph.max[k];
toChart.state['y1m_' + k] = rangeMini.min[k];
toChart.state['y2m_' + k] = rangeMini.max[k];
}
} else {
toChart.state.y1 = rangeGraph.min;
toChart.state.y2 = rangeGraph.max;
toChart.state.y1m = rangeMini.min;
toChart.state.y2m = rangeMini.max;
}
toChart.switchers && toChart.switchers.render(enabled);
toChart.axisX && toChart.axisX.setAnimation(false);
toChart.axisY && toChart.axisY.setAnimation(false);
toChart.axisY && toChart.axisY.setForceUpdate(true);
window.Graph && Graph.units && Graph.units.TUtils && Graph.units.TUtils.triggerEvent('chart-hide-tips', { except: null });
toChart.tip && toChart.tip.toggle(false);
toChart.composer && toChart.composer.render({ top: true, bottom: true });
}
function initDynamicModeToggle(domEl, baseChart, rawJson) {
if (!isDynamicEligible(rawJson)) return;
var wrapper = baseChart && baseChart.opts && baseChart.opts.container;
if (!wrapper) return;
// hack shift the zoom out btn to the right
// no layouts because tchart enforces weird abs positioning
// [!!] this took way too many attempts to get right, care if changing it, tchart is weird with the header Z stack
wrapper.classList.add('tchart-wrapper__dynamic');
var currentMode = detectModeFromJson(rawJson);
var charts = {
absolute: (currentMode === 'absolute') ? baseChart : null,
relative: (currentMode === 'relative') ? baseChart : null
};
function styleLayer(chart, active) {
if (!chart || !chart.$el) return;
chart.$el.classList.add('tchart__dynamic_layer');
chart.$el.style.position = chart === baseChart ? '' : 'absolute';
chart.$el.style.top = chart === baseChart ? '' : '0';
chart.$el.style.left = chart === baseChart ? '' : '0';
chart.$el.style.width = '100%';
chart.$el.style.opacity = active ? '1' : '0';
chart.$el.style.pointerEvents = active ? 'auto' : 'none';
chart.$el.style.zIndex = active ? '2' : '1';
chart.$el.style.visibility = 'visible';
}
// ui header
var btn = document.createElement('div');
btn.className = 'tchart--mode-btn'; // put btn left 17px
var icon = document.createElement('div');
btn.appendChild(icon);
var label = document.createElement('span');
btn.appendChild(label);
wrapper.appendChild(btn);
function updateButtons() {
// class based on current state to enable correct hover
// [!] there's a hack here, if changing, also fix tchart--mode-btn__clicked for hover continuity
if (currentMode === 'absolute') {
label.textContent = 'Relative';
// current is abs, vertical, hover to rel aka horiz
btn.classList.add('tchart--mode-btn__is-abs');
btn.classList.remove('tchart--mode-btn__is-rel');
} else {
label.textContent = 'Absolute';
// current rel, horiz, hover to avb aka vert
btn.classList.add('tchart--mode-btn__is-rel');
btn.classList.remove('tchart--mode-btn__is-abs');
}
}
function ensureChart(mode) {
if (charts[mode]) return charts[mode];
var modeJson = buildModeJson(rawJson, mode);
var prepared = prepareGraphJson(modeJson);
var ch = new Graph.units.TChart({ container: wrapper, data: prepared });
charts[mode] = ch;
styleLayer(ch, false);
return ch;
}
function setMode(mode) {
if (mode === currentMode) return;
var fromChart = charts[currentMode] || baseChart;
var toChart = ensureChart(mode);
var isZoomed = fromChart && (fromChart.state.zoomMode || fromChart.state.zoomModeSpecial);
if (isZoomed) {
try { fromChart.toggleZoom(false); } catch (e) {}
try { toChart.toggleZoom(false); } catch (e2) {}
setTimeout(function () {
syncChartState(fromChart, toChart);
styleLayer(fromChart, false);
styleLayer(toChart, true);
currentMode = mode;
updateButtons();
}, 520);
return;
}
syncChartState(fromChart, toChart);
styleLayer(fromChart, false);
styleLayer(toChart, true);
currentMode = mode;
updateButtons();
}
btn.addEventListener('click', function () {
var nextMode = (currentMode === 'absolute') ? 'relative' : 'absolute';
// hack to show continuity on hover after click
btn.classList.add('tchart--mode-btn__clicked');
setMode(nextMode);
});
btn.addEventListener('mouseleave', function() {
btn.classList.remove('tchart--mode-btn__clicked');
});
styleLayer(baseChart, true);
updateButtons();
}
var healthSearchModal = null;
var healthSearchTimeout = null;
var healthSearchCurrentQuery = '';
var healthSearchFocusedIndex = -1;
function healthSearchShowModal() {
if (healthSearchModal) {
healthSearchModal.show();
$('#health-search-input').focus();
return;
}
healthSearchModal = $('<div>', {'class': 'health-search-modal', 'id': 'health-search-modal'}).append(
$('<div>', {'class': 'health-search-dialog'}).append(
$('<div>', {'class': 'health-search-header'}).html('<span class="health-search-title">Search Health Pages</span><span class="health-search-close" onclick="healthSearchCloseModal();">×</span>'),
$('<div>', {'class': 'health-search-input-wrap'}).append(
$('<input>', {'type': 'text', 'id': 'health-search-input', 'class': 'form-control health-search-input', 'placeholder': 'Search pages...'}).on('input', function() {
healthSearchDoSearch(this.value);
})
),
$('<div>', {'class': 'health-search-results', 'id': 'health-search-results'})
)
);
$('body').append(healthSearchModal);
healthSearchModal.on('click', function(e) {
if ($(e.target).is(healthSearchModal)) {
healthSearchCloseModal();
}
});
$(document).on('keydown.healthsearch', function(e) {
if (!healthSearchModal || !healthSearchModal.is(':visible')) {
return;
}
if (e.key === 'Escape') {
healthSearchCloseModal();
return;
}
if (e.key === 'ArrowDown' || e.key === 'ArrowUp' || e.key === 'Enter') {
var items = $('.health-search-result');
if (!items.length) {
return;
}
e.preventDefault();
if (e.key === 'ArrowDown') {
healthSearchFocusedIndex = Math.min(healthSearchFocusedIndex + 1, items.length - 1);
} else if (e.key === 'ArrowUp') {
healthSearchFocusedIndex = Math.max(healthSearchFocusedIndex - 1, 0);
} else if (e.key === 'Enter' && healthSearchFocusedIndex >= 0) {
items.eq(healthSearchFocusedIndex)[0].click();
return;
}
healthSearchUpdateFocus(items);
}
});
$('#health-search-input').focus();
}
function healthSearchUpdateFocus(items) {
items.removeClass('health-search-result-active');
items.eq(healthSearchFocusedIndex).addClass('health-search-result-active').scrollIntoView({block: 'nearest'});
}
function healthSearchCloseModal() {
if (healthSearchModal) {
healthSearchModal.hide();
healthSearchFocusedIndex = -1;
}
}
function healthSearchDoSearch(query) {
query = $.trim(query);
if (query === healthSearchCurrentQuery) {
return;
}
healthSearchCurrentQuery = query;
healthSearchFocusedIndex = -1;
if (healthSearchTimeout) {
clearTimeout(healthSearchTimeout);
}
if (query.length < 2) {
$('#health-search-results').html('');
return;
}
healthSearchTimeout = setTimeout(function() {
var basePath = window.basePath || '';
$.getJSON(basePath + '/healthsearch?query=' + encodeURIComponent(query), function(results) {
healthSearchRenderResults(results);
}).fail(function(err) {
console.error('Search error:', err);
$('#health-search-results').html('<div style="color:red;">Search failed</div>');
});
}, 300);
}
function healthSearchRenderResults(results) {
var container = $('#health-search-results');
if (!results || !results.length) {
container.html('<div class="health-search-no-results">No results found</div>');
return;
}
var html = '';
for (var i = 0; i < results.length; i++) {
var r = results[i];
var url = typeof r.url === 'object' ? r.url[Object.keys(r.url)[0]] : r.url;
html += '<a href="' + url + '" class="health-search-result">';
html += '<div class="health-search-result-title">' + (r.title || '') + '</div>';
if (r.snippet) {
html += '<div class="health-search-result-snippet">' + r.snippet + '</div>';
}
if (r.breadcrumb) {
html += '<div class="health-search-result-breadcrumb">' + r.breadcrumb + '</div>';
}
html += '</a>';
}
container.html(html);
}
document.addEventListener('click', function(e) {
var pill = e.target.closest('.graph_desc_edit_pill');
if (!pill) return;
e.preventDefault();
var addr = pill.getAttribute('data-addr');
var graphId = pill.getAttribute('data-graph-id');
var keywords = pill.getAttribute('data-keywords') || '';
var descMd = (pill.getAttribute('data-desc-md') || '').replace(/\\n/g, '\n');
var outerWrap = pill.closest('.chart_wrap_outer');
if (!outerWrap) return;
var existingForm = outerWrap.querySelector('.graph_desc_edit_form');
if (existingForm) {
existingForm.remove();
var rowEl = outerWrap.querySelector('.graph_desc_row');
if (rowEl) rowEl.style.display = '';
return;
}