This repository has been archived by the owner on Apr 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtooltip_manager.js
1512 lines (1300 loc) · 49 KB
/
tooltip_manager.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
/**
* @license Apache-2.0
* Copyright 2018 Austin Walker Milt
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @file Manages the VGV UI, including all DOM elements in the youtube page
* and their dynamic updates as the video progresses.
* @author [Austin Milt]{@link https://github.com/austinmilt}
*/
///////////////////////////////////////////////////////////////////////////////
// CONSTANTS AND GLOBALS //////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
var youtubeVideo;
var videoControls;
var youtubePlayer;
var focalHero = {'name': null};
var vgvContainer = null;
const ICON_STR = chrome.runtime.getURL('page/assets/str.png');
const ICON_AGI = chrome.runtime.getURL('page/assets/agi.png');
const ICON_INT = chrome.runtime.getURL('page/assets/int.png');
const ICON_DMG = chrome.runtime.getURL('page/assets/dmg.png');
const ICON_SPD = chrome.runtime.getURL('page/assets/speed.png');
// user options
var refreshInterval = 1000.0; // milliseconds
var showBorders = false;
chrome.storage.sync.get(['tooltip_interval', 'show_boxes'], function(e){
if (e['tooltip_interval'] !== undefined) { refreshInterval = e['tooltip_interval'] * 1000.0; }
if (e['show_boxes'] !== undefined) { showBorders = e['show_boxes']; }
});
chrome.storage.onChanged.addListener(function(changes, namespace) {
if (namespace == 'sync') {
for (key in changes) {
if (key == 'tooltip_interval') { refreshInterval = changes[key].newValue; }
else if (key == 'show_boxes') { showBorders = changes[key].newValue; }
}
}
});
var refreshID;
var URL = window.location.href;
var controlsParent;
// box index of abilities based on the number of hero ability slots
const ABILITY_I2I = {
4: {0: 0, 1: 1, 2: 2, 3: null, 4: null, 5: 3},
5: {0: 0, 1: 1, 2: 2, 3: 3, 4: null, 5: 4},
6: {0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5}
}
// style classes for AbilityBox's with different number of hero ability slots
const ABILITY_CLASS = {
4: {
0: ['s-4', 's-4-0'], 1: ['s-4', 's-4-1'], 2: ['s-4', 's-4-2'],
3: ['s-4', 's-4-3']
},
5: {
0: ['s-5', 's-5-0'], 1: ['s-5', 's-5-1'], 2: ['s-5', 's-5-2'],
3: ['s-5', 's-5-3'], 4: ['s-5', 's-5-4']
},
6: {
0: ['s-6', 's-6-0'], 1: ['s-6', 's-6-1'], 2: ['s-6', 's-6-2'],
3: ['s-6', 's-6-3'], 4: ['s-6', 's-6-4'], 5: ['s-6', 's-6-5']
}
}
// style classes for ItemBox's with different number of hero ability slots
const ITEM_CLASS = {
4: {
0: ['i-4', 'i-4-0'], 1: ['i-4', 'i-4-1'], 2: ['i-4', 'i-4-2'],
3: ['i-4', 'i-4-3'], 4: ['i-4', 'i-4-4'], 5: ['i-4', 'i-4-5'],
6: ['b-4', 'b-4-0'], 7: ['b-4', 'b-4-1'], 8: ['b-4', 'b-4-2'],
},
5: {
0: ['i-5', 'i-5-0'], 1: ['i-5', 'i-5-1'], 2: ['i-5', 'i-5-2'],
3: ['i-5', 'i-5-3'], 4: ['i-5', 'i-5-4'], 5: ['i-5', 'i-5-5'],
6: ['b-5', 'b-5-0'], 7: ['b-5', 'b-5-1'], 8: ['b-5', 'b-5-2'],
},
6: {
0: ['i-6', 'i-6-0'], 1: ['i-6', 'i-6-1'], 2: ['i-6', 'i-6-2'],
3: ['i-6', 'i-6-3'], 4: ['i-6', 'i-6-4'], 5: ['i-6', 'i-6-5'],
6: ['b-6', 'b-6-0'], 7: ['b-6', 'b-6-1'], 8: ['b-6', 'b-6-2'],
}
}
// style classes for TalentBox's with different number of hero ability slots
const TALENT_CLASS = {
4: ['talent', 't-4'], 5: ['talent', 't-5'], 6: ['talent', 't-6']
}
// style classes for AvatarBox's across the top of screen
const AVATAR_CLASS = {
0: ['avatar', 'a-0'], 1: ['avatar', 'a-1'], 2: ['avatar', 'a-2'],
3: ['avatar', 'a-3'], 4: ['avatar', 'a-4'], 5: ['avatar', 'a-5'],
6: ['avatar', 'a-6'], 7: ['avatar', 'a-7'], 8: ['avatar', 'a-8'],
9: ['avatar', 'a-9']
}
///////////////////////////////////////////////////////////////////////////////
// INITIALIZATION /////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
var tooltips = new Set();
var boxes = {};
/**
* Activates the tooltips in the document.
*/
function activate_tooltips() {
youtubeVideo = document.getElementsByClassName('video-stream')[0];
youtubePlayer = document.getElementById('movie_player');
vgvContainer = document.createElement('div');
vgvContainer.setAttribute('id', 'vgv_container');
vgvContainer.classList.add('vgv-container');
youtubePlayer.appendChild(vgvContainer);
document.getElementsByClassName('ytp-chrome-bottom')[0].style.zIndex = '100';
initialize_boxes();
refreshID = setInterval(update, refreshInterval);
}
/**
* Intializes all the HoverBox's that will display tooltip info.
*/
function initialize_boxes() {
// build stuff for the focus hero
boxes['focus'] = {};
boxes['focus']['talents'] = new TalentBox(6, null, [], [], vgvContainer);
boxes['focus']['abilities'] = [];
for (var i = 0; i < 6; i++) {
boxes['focus']['abilities'].push(new AbilityBox(i, 6, null, [], vgvContainer));
}
boxes['focus']['items'] = [];
for (var i = 0; i < 9; i++) {
boxes['focus']['items'].push(new ItemBox(i, 6, null, vgvContainer));
}
// build hero avatar boxes
boxes['avatars'] = [];
for (var i = 0; i < 10; i++) {
boxes['avatars'].push(new AvatarBox(i, vgvContainer));
// boxes['avatars'][i].element.addEventListener('click', function(event) {console.log(TIMER)});
}
}
///////////////////////////////////////////////////////////////////////////////
// BOX ELEMENT CLASSES ////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
/** Base class of boxes in the VGV UI that contain some tooltip info. */
class HoverBox {
/**
* Creates a HoverBox.
* @param {DOMElement} [parent=vgvContainer] - parent element that the box should a child of
* @param {string} [type='div'] - HTML type that this box should be
* @param {string[]} [styles=[]] - additional CSS styles to give the box
* @param {string} [tooltipDirection='north'] - direction the tooltip should display relative to the box
* @param {string[]} [tooltipStyles=[]] - additional CSS styles to give the tooltip
*/
constructor({
parent=vgvContainer, type='div', styles=[], tooltipDirection='north',
tooltipStyles=[]
}) {
// create the document element which will reveal a tooltip on hover
var STYLES = ['hoverbox'];
this.parent = parent;
this.hovering = false;
this.element = document.createElement(type);
this.element.classList.add(...STYLES);
this.extraStyles = styles;
this.set_styles(styles);
// create a placeholder tooltip
this.tooltip = new VGVTooltip(
this.element, vgvContainer, '', tooltipStyles,
{'by': 'element', 'direction': tooltipDirection}, true
);
this.tooltip.ttmid = parseInt(Math.random()*10000);
tooltips.add(this.tooltip.ttmid);
// set a hover function to force other tooltips to close
var self = this;
this.element.addEventListener('mouseenter', function() {
close_tooltips_but(self.tooltip);
self.hovering = true;
});
this.element.addEventListener('mouseleave', function() {
self.hovering = false;
});
// activate the box
this.showing = false;
this.active = false;
this.activate();
}
/**
* Sets optional CSS styles for the box.
* @param {string[]} [styles=[]] - additional styles for the box
*/
set_styles(styles=[]) {
this.element.classList.remove(...this.extraStyles);
this.element.classList.add(...styles);
this.extraStyles = styles;
}
/**
* Shows the box element to the user (i.e. shows border).
*/
show() {
if (!this.showing) {
this.element.style.borderStyle = 'solid';
this.showing = true;
}
}
/**
* Hides the box element from the user (tooltip still works).
*/
hide() {
if (this.showing) {
this.element.style.borderStyle = 'none';
this.showing = false;
}
}
/**
* Removes the box element and its tooltip from the DOM.
*/
remove() { this.tooltip.remove(); this.element.remove(); }
/**
* Calls the set_html method of VGVTooltip.
*/
set_tooltip_html(html, childID=null) {
this.tooltip.set_html(html, childID);
}
/**
* Updates the tooltip html based on class specific conditions
* (by default does nothing).
*/
update() {}
/**
* "Activates" the box by allowing the tooltip to open and
* optionally showing the box/
*/
activate(alsoShow=showBorders) {
if (!this.active) {
if (alsoShow) { this.show(); }
else { this.hide(); }
this.parent.appendChild(this.element);
this.tooltip.enable();
this.active = true;
}
}
/**
* "Deactivates" the box by forcing the tooltip to hide (if open),
* preventing it from opening on hover, and hiding the box (if showing).
*/
deactivate() {
if (this.active) {
this.hide();
this.element.remove();
this.tooltip.disable();
this.element.remove();
this.active = false;
}
}
}
/**
* Class for UI elements that are ability boxes.
* @extends HoverBox
*/
class AbilityBox extends HoverBox {
/**
* Creates an AbilityBox.
* @param {int} [position=0] - ability index in the hero's ability set
* @param {int} [slots=4] - number of ability slots the hero has
* @param {int} [level=0] - current level of the ability
* @parent {DOMElement} [parent=vgvContainer] - containing element this box should belong to
*/
constructor(position=0, slots=4, id=null, level=0, parent=vgvContainer) {
super({
parent:parent, styles:ABILITY_CLASS[slots][position],
tooltipDirection:'north', tooltipStyles:['tooltip', 'ability-tooltip']
});
this.id = null;
this.slots = null;
this.position = null;
this.level = null;
this.update(slots, position, id, level, true);
}
/**
* Updates position and tooltip if conditions are met.
* @param {int} slots - see {@link AbilityBox}
* @param {int} position - see {@link AbilityBox}
* @param {int} [level=null] - see {@link AbilityBox}
* @param {boolean} [force=false] - force an update regardless of detected changes
*/
update(slots, position, id, level=null, force=false) {
// update the tooltip info
if (this.tooltip.visible || force) {
var changed = (this.id != id) || (this.level != level);
this.id = id;
this.level = level;
if (changed) {
this.set_tooltip_html(TOOLTIPS.html(this.id, this.level));
}
}
// update the box position
var slotsChanged = this.slots != slots;
var positionChanged = this.position != position;
this.slots = slots;
this.position = position;
if (slotsChanged || positionChanged) {
this.set_styles(ABILITY_CLASS[slots][this.position]);
}
}
}
/**
* Class for UI elements that are item boxes.
* @extends HoverBox
*/
class ItemBox extends HoverBox {
/**
* Creates an AbilityBox.
* @param {int} [position=0] - item index in the hero's item set
* @param {int} [slots=4] - number of ability (yes, ability) slots the hero has
* @param {int} [id=null] - dotapedia item id
* @parent {DOMElement} [parent=vgvContainer] - containing element this box should belong to
*/
constructor(position=0, slots=4, id=null, parent=vgvContainer) {
super({
parent: parent, styles: ITEM_CLASS[slots][position],
tooltipDirection: 'north', tooltipStyles: ['tooltip', 'ability-tooltip']
});
this.set_tooltip_html('Empty slot.');
this.id = null;
this.slots = null;
this.position = position;
this.update(slots, id, true);
}
/**
* Updates position and tooltip if conditions are met.
* @param {int} slots - see {@link ItemBox}
* @param {int} [id=null] - see {@link ItemBox}
* @param {boolean} [force=false] - force an update regardless of detected changes
*/
update(slots, id, force=false) {
// update the tooltip info
if (this.tooltip.visible || force) {
var idChanged = (this.id != id);
this.id = id;
if (idChanged) {
var html = TOOLTIPS.html(this.id, null)
if (!html.trim()) { html = 'Empty slot.'; }
this.set_tooltip_html(html);
}
}
// update the box position
var slotsChanged = this.slots != slots;
this.slots = slots;
if (slotsChanged) {
this.set_styles(ITEM_CLASS[slots][this.position]);
}
}
}
/**
* Class for UI elements that are Talent boxes.
* @extends HoverBox
*/
class TalentBox extends HoverBox {
/**
* Creates a TalentBox.
* @param {int} [slots=4] - number of ability (yes, ability) slots the hero has
* @param {string} [hero=null] - identity of the hero this is displaying info for
* @param {int[]} [ids=[]] - array of dotapedia talent ids
* @param {int[]} [levels=[]] - array of talent levels (0 or 1)
* @parent {DOMElement} [parent=vgvContainer] - containing element this box should belong to
*/
constructor(slots=4, hero=null, ids=[], levels=[], parent=vgvContainer) {
super({
parent: parent, styles: TALENT_CLASS[slots],
tooltipDirection: 'north',
tooltipStyles: ['tooltip', 'talent-tootip']
});
this.hero = null;
this.slots = null;
this.levels = [];
this.update(slots, hero, ids, levels, true);
}
/**
* Updates position and tooltip if conditions are met.
* @param {int} slots - see {@link TalentBox}
* @param {string} hero - see {@link TalentBox}
* @param {int[]} ids - see {@link TalentBox}
* @param {int[]} levels - see {@link TalentBox}
* @param {boolean} [force=false] - force an update regardless of detected changes
*/
update(slots, hero, ids, levels, force=false) {
// update the tooltip info
if (this.tooltip.visible || force) {
var changed = (this.hero != hero);
if (!changed) {
for (var i = 0; i < 8; i++) {
if (levels[i] != this.levels[i]) {
changed = true;
break;
}
}
}
this.hero = hero;
if (changed) {
this.levels = levels;
this.set_tooltip_html(TOOLTIPS.html_talents(ids, levels));
}
}
// update the box position
var slotsChanged = this.slots != slots;
this.slots = slots;
if (slotsChanged) {
this.set_styles(TALENT_CLASS[slots]);
}
}
}
/**
* Class for UI elements that are Avatar boxes. AvatarBox's are very dynamic
* and contain info from multiple other boxes (i.e. everything in
* AbilityBox, ItemBox, and TalenBox), so they are a bit more complex.
* @extends HoverBox
*/
class AvatarBox extends HoverBox {
/**
* Creates an AvatarBox.
* @param {int} [position=0] - hero index in the top row of hero avatars in the UI
* @parent {DOMElement} [parent=vgvContainer] - containing element this box should belong to
*/
constructor(position=0, parent=vgvContainer) {
super({
parent:parent, styles:AVATAR_CLASS[position],
tooltipDirection:'south', tooltipStyles:['tooltip', 'avatar-tooltip']
});
this.hero = null;
this.position = position;
this.handler = this.initialize_html();
this.ttFirstCall = true;
var self = this;
// add event listeners for child elements when the tooltip becomes visible
this.element.addEventListener('click', function(e) {
if (self.tooltip.visible) {
if (self.ttFirstCall) {
self.ttFirstCall = false;
for (var objList of self.handler.get_listeners()) {
objList[0].removeEventListener(objList[1], objList[2]);
objList[0].addEventListener(objList[1], objList[2]);
// console.log(objList[1] + ' listener added for ' + objList[0].getAttribute('id'));
}
}
}
else { self.ttFirstCall = true; }
});
// add event listener to remove showing child elements when the
// user leaves the tooltip and it is not stuck
this.element.addEventListener('mouseleave', function(e) {
if (!self.tooltip.stuck) {
self.handler.hide_children();
self.ttFirstCall = true;
}
});
// add event listener to force update if starting to hover
// (just makes sure that tooltip info is up to date when
// first viewed)
this.element.addEventListener('mouseenter', function(e) {
self.update(self.hero, true)
});
}
/**
* Updates tooltip if conditions are met.
* @param {string} name name of the hero to update info for
* @param {boolean} [force=false] - force an update regardless of detected changes
*/
update(name, force=false) {
if (name) {
this.hero = name;
if (this.tooltip.visible || force) {
var videoTime = youtubeVideo.currentTime;
var heroes = TIMER.get_all(videoTime);
var hero = heroes.get_hero(name);
var data = {};
data['hero'] = name;
data['lvl'] = hero.other['level'];
data['hp'] = [hero.other['health_current'], hero.other['health_max'], hero.other['health_regen']];
data['mana'] = [hero.other['mana_current'], hero.other['mana_max'], hero.other['mana_regen']];
data['str'] = [hero.other['str_natural'], hero.other['str_total']];
data['agi'] = [hero.other['agi_natural'], hero.other['agi_total']];
data['int'] = [hero.other['int_natural'], hero.other['int_total']];
data['dmg'] = [hero.other['dmg_min'], hero.other['dmg_max'], hero.other['dmg_bonus']];
data['speed'] = hero.other['speed'];
data['xp'] = hero.other['xp_total'];
data['worth'] = hero.other['net_worth'];
data['kda'] = [hero.other['kills'], hero.other['deaths'], hero.other['assists']];
data['cs'] = [hero.other['last_hits'], hero.other['denies']];
data['earned'] = hero.other['gold_total'];
data['talents'] = hero.talents;
data['abilities'] = hero.abilities;
data['items'] = hero.items;
var updated = this.handler.update(data, force);
if (updated) {
this.set_tooltip_html(this.handler.get_html());
for (var objList of this.handler.get_listeners()) {
objList[0].removeEventListener(objList[1], objList[2]);
objList[0].addEventListener(objList[1], objList[2]);
}
}
}
}
}
/**
* Sets up the html handler for the tooltip to facilitate dynamic updates.
* @return {AvatarTooltipHTML} handler for updates to tooltip
*/
initialize_html() {
return new AvatarTooltipHTML([
new HeroATE(), new HealthATE(), new ManaATE(),
new LevelATE(), new ExperienceATE(),
new NetWorthATE(), new GoldATE(), new KDAATE(), new CSATE(),
new DamageATE(), new SpeedATE(),
new StrengthATE(), new AgilityATE(), new IntelligenceATE(),
new TalentsATE(), new AbilitiesATE(), new ItemsATE()
]);
}
}
///////////////////////////////////////////////////////////////////////////////
// (REALLY) DYNAMIC TOOLTIP HTML //////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
/**
* Base class for AvatarBox tooltip HTML items, e.g. health, mana, etc.
*/
class AvatarTooltipElement {
/**
* Constructs an AvatarTooltipElement.
* @param {string} key - unique identifier used to handle this element in the {@link AvatarTooltipHTML}
* @param {Object} value - display and update testing value for this element
* @param {string[]} [styles=[]] - additional CSS styles this element should get
* @param {string} [elementType='div'] - HTML type of the DOMElement made from this element
*/
constructor(key, value, styles=[], elementType='div') {
this.key = key;
this.value = value;
this.element = document.createElement(elementType);
this.element.classList.add(...styles)
this.element.innerHTML = this.handler(value);
}
/**
* Updates the value and HTML of this element if conditions are met.
* @param {Object} value - new value this element should take
* @param {boolean} force - force updating regardless of detected changes
*/
update(value, force=false) {
var updated = this.check_update(value);
if (updated || force) {
this.value = value;
this.element.innerHTML = this.handler(value);
}
return updated;
}
/** Checks if the value of this element has changed. */
check_update(value) { return (this.value != value); }
/** Gets the display HTML of this element.*/
handler(value) { return String(value); }
/**
* Gets any addiitonal listeners this element needs started in order
* for the user to interact or otherwise function.
*/
get_listeners() { return null; }
/** Attempts to remove the DOMElement from the DOM.*/
remove() {
try { this.element.remove(); }
catch (e) {}
}
}
/**
* Hero name element.
* @extends AvatarTooltipElement
*/
class HeroATE extends AvatarTooltipElement {
/** Creates a HeroATE.*/
constructor() { super('hero', '', ['name'], 'div'); }
/**
* Gets the display HTML for this element.
* @param {string} value - hero name
* @return {string} HTML of name and hero icon
*/
handler(value) {
var html = '';
var heroKey = value.replace("'", "");
var heroInfo = DOTAPEDIA[heroKey];
if (heroInfo !== undefined) {
var icon = heroInfo['icon'];
html += `<img src=${icon}>`;
}
html += ` ${value}`;
var warn = "Data in this section is experimental. ";
warn += "It is impossible to fully reproduce the match information ";
warn += "at every second of the video due to limits in how accurate ";
warn += "VGV can be at analyzing videos and combining with replay information.";
html += ` <b title="${warn}" style="color:#d7be5b;cursor:help;text">( ! )</b>`;
html += '<hr>';
return html;
}
}
/**
* Hero level element.
* @extends AvatarTooltipElement
*/
class LevelATE extends AvatarTooltipElement {
/** Creates a LevelATE.*/
constructor() { super('lvl', 0.); }
/**
* Gets the display HTML for this element.
* @param {int} value - hero's current level
* @return {string} HTML of hero's level
*/
handler(value) { return `LEVEL: ${value}`; }
}
/**
* Hero movement speed element.
* @extends AvatarTooltipElement
*/
class SpeedATE extends AvatarTooltipElement {
/** Creates a SpeedATE.*/
constructor() { super('speed', 0., ['ico']); }
/**
* Gets the display HTML for this element.
* @param {int} value - hero's movement speed
* @return {string} HTML of hero's movement speed
*/
handler(value) { return `<img src=${ICON_SPD}>${value} (BASE)`; }
}
/**
* Hero experience element.
* @extends AvatarTooltipElement
*/
class ExperienceATE extends AvatarTooltipElement {
/** Creates a ExperienceATE.*/
constructor() { super('xp', 0.); }
/**
* Gets the display HTML for this element.
* @param {int} value - hero's total XP
* @return {string} HTML of hero's total experience
*/
handler(value) { return `XP: ${value}`; }
}
/**
* Hero net worth element.
* @extends AvatarTooltipElement
*/
class NetWorthATE extends AvatarTooltipElement {
/** Creates a NetWorthATE.*/
constructor() { super('worth', 0.); }
/**
* Gets the display HTML for this element.
* @param {int} value - player's net worth
* @return {string} HTML of player's net worth
*/
handler(value) { return `NET WORTH: ${value}`; }
}
/**
* Player KDA (kills, deaths, assists) element.
* @extends AvatarTooltipElement
*/
class KDAATE extends AvatarTooltipElement {
/** Creates a KDAATE.*/
constructor() { super('kda', [0, 0, 0]); }
/** @see AvatarTooltipElement */
check_update(value) { return ((this.value[0] != value[0]) || (this.value[1] != value[1]) || (this.value[2] != value[2])); }
/**
* Gets the display HTML for this element.
* @param {int[]} value - hero [kills, deaths, assists]
* @return {string} HTML of player's KDA
*/
handler(value) { return `K/D/A: ${value[0]}/${value[1]}/${value[2]}`; }
}
/**
* Player gold earned element.
* @extends AvatarTooltipElement
*/
class GoldATE extends AvatarTooltipElement {
/** Creates a GoldATE.*/
constructor() { super('earned', 0.); }
/**
* Gets the display HTML for this element.
* @param {int} value - player's earned gold
* @return {string} HTML of player's earned gold
*/
handler(value) { return `GOLD EARNED: ${value}`; }
}
/**
* Player CS (creep score) element.
* @extends AvatarTooltipElement
*/
class CSATE extends AvatarTooltipElement {
/** Creates a CSATE.*/
constructor() { super('cs', [0, 0]); }
/** @see AvatarTooltipElement */
check_update(value) { return ((this.value[0] != value[0]) || (this.value[1] != value[1])); }
/**
* Gets the display HTML for this element.
* @param {int[]} - [creep kills, creep denies]
* @return {string} HTML of hero's CS
*/
handler(value) { return `CREEP KILLS/DENIES: ${value[0]}/${value[1]}`; }
}
/**
* Hero strength level element.
* @extends AvatarTooltipElement
*/
class StrengthATE extends AvatarTooltipElement {
/** Creates a StrengthATE.*/
constructor() { super('str', [0., 0.], ['ico']); }
/** @see AvatarTooltipElement */
check_update(value) { return ((this.value[0] != value[0]) || (this.value[1] != value[1])); }
/**
* Gets the display HTML for this element.
* @param {float[]} value - [base, total, bonus] str(ength)
* @return {string} HTML of hero's strength
*/
handler(value) {
var base = Math.round(value[0]);
var total = Math.round(value[1]);
var bonus = total - base;
return `<img src="${ICON_STR}">${base} + ${bonus}`;
}
}
/**
* Hero agility element.
* @extends AvatarTooltipElement
*/
class AgilityATE extends AvatarTooltipElement {
/** Creates a AgilityATE.*/
constructor() { super('agi', [0., 0.], ['ico']); }
/** @see AvatarTooltipElement */
check_update(value) { return ((this.value[0] != value[0]) && (this.value[1] != value[1])); }
/**
* Gets the display HTML for this element.
* @param {float[]} value - [base, total, bonus] agi(lity)
* @return {string} HTML of hero's agility
*/
handler(value) {
var base = Math.round(value[0]);
var total = Math.round(value[1]);
var bonus = total - base;
return `<img src="${ICON_AGI}">${base} + ${bonus}`;
}
}
/**
* Hero intelligence element.
* @extends AvatarTooltipElement
*/
class IntelligenceATE extends AvatarTooltipElement {
/** Creates a IntelligenceATE.*/
constructor() { super('int', [0., 0.], ['ico']); }
/** @see AvatarTooltipElement */
check_update(value) { return ((this.value[0] != value[0]) || (this.value[1] != value[1])); }
/**
* Gets the display HTML for this element.
* @param {float[]} value - [base, total, bonus] int(elligence)
* @return {string} HTML of hero's intelligence
*/
handler(value) {
var base = Math.round(value[0]);
var total = Math.round(value[1]);
var bonus = total - base;
return `<img src="${ICON_INT}">${base} + ${bonus}`;
}
}
/**
* Hero attack damage element.
* @extends AvatarTooltipElement
*/
class DamageATE extends AvatarTooltipElement {
/** Creates a DamageATE.*/
constructor() { super('dmg', [0., 0., 0.], ['ico']); }
/** @see AvatarTooltipElement */
check_update(value) { return ((this.value[0] != value[0]) || (this.value[1] != value[1]) || (this.value[2] != value[2])); }
/**
* Gets the display HTML for this element.
* @param {float[]} value - [min, max, bonus] damage
* @return {string} HTML of hero damage
*/
handler(value) {
var min = value[0];
var max = value[1];
var bonus = value[2];
return `<img src="${ICON_DMG}">[${min}-${max}] + ${bonus}`;
}
}
/**
* Hero health element.
* @extends AvatarTooltipElement
*/
class HealthATE extends AvatarTooltipElement {
/** Creates a HealthATE.*/
constructor() { super('hp', [0., 0., 0.]); }
/** @see AvatarTooltipElement */
check_update(value) { return ((this.value[0] != value[0]) || (this.value[1] != value[1]) || (this.value[2] != value[2])); }
/**
* Gets the display HTML for this element.
* @param {float[]} value - [current, max, regen] health
* @return {string} HTML of hero's health
*/
handler(value) {
var cur = value[0];
var max = value[1];
var pct = Math.round(100*(cur/max));
var regen = value[2];
cur = Math.round(cur);
max = Math.round(max);
return `<div class="vgv_avbarct"><div class="vgv_avbarfg" style="width:${pct}%;" stat="hp"></div><div class="vgv_avbartxt">HEALTH: ${cur}/${max} (${pct}%) +${regen}</div></div>`;
}
}
/**
* Hero mana element.
* @extends AvatarTooltipElement
*/
class ManaATE extends AvatarTooltipElement {
/** Creates a HeroATE.*/
constructor() { super('mana', [0., 0., 0.]); }
/** @see AvatarTooltipElement */
check_update(value) { return ((this.value[0] != value[0]) || (this.value[1] != value[1]) || (this.value[2] != value[2])); }
/**
* Gets the display HTML for this element.
* @param {float[]} value - [current, max, regen] mana
* @return {string} HTML of hero mana
*/
handler(value) {
var cur = value[0];
var max = value[1];
var pct = Math.round(100*(cur/max));
var regen = value[2];
cur = Math.round(cur);
max = Math.round(max);
return `<div class="vgv_avbarct"><div class="vgv_avbarfg" style="width:${pct}%;" stat="mana"></div><div class="vgv_avbartxt">MANA: ${cur}/${max} (${pct}%) +${regen}</div></div>`;
}
}
/**
* Base class for talents, abilities, and items within an AvatarBox's tooltip.
* @extends AvatarTooltipElement
*/
class TAIATE extends AvatarTooltipElement {
/**
* Creates a TAIATE.
* @param {string} key - see {@link AvatarTooltipElement}
* @param {string[]} [substyles=[]] - additional CSS styles to give tooltip of this element
* @param {Object} [value=[]] - see {@link AvatarTooltipElement}
* @param {string[]} [styles=[]] - see {@link AvatarTooltipElement}
* @param {string} [elementType='div'] - see {@link AvatarTooltipElement}
*/
constructor(key, substyles=[], value=[], styles=[], elementType='div') {
super(key, value, styles, elementType);
this.tooltip = new VGVTooltip(
this.element, vgvContainer, '', ['tooltip'].concat(substyles),
{'by': 'element', 'direction': 'south'}, true
);
this.eid = String(parseInt(Math.random()*1e10));
this.element.setAttribute('id', this.eid);
this.element.classList.add('vgvavtt_clickable');