-
Notifications
You must be signed in to change notification settings - Fork 2
/
annotations.js
144 lines (122 loc) · 5.27 KB
/
annotations.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
class Annotation
{
constructor(data)
{
this.start = data.start;
this.stop = data.stop;
//Create the annotation (outer and inner portions)
this.outer = document.createElement("div");
this.inner = this.outer.appendChild(document.createElement("div"));
//Set up the outer portion (background)
this.outer.className = (data.type == "highlight") ? "outer-annotation highlight" : "outer-annotation";
this.outer.id = data.id;
this.outer.style.left = data.left;
this.outer.style.top = data.top;
this.outer.style.width = data.width;
this.outer.style.height = data.height;
this.outer.style.backgroundColor = (data.type == "highlight") ? "" : (typeof data.bgcolor !== "undefined") ? data.bgcolor : "";
this.outer.style.borderColor = (data.type == "highlight") ? (typeof data.bordercolor !== "undefined") ? data.bordercolor : "" : "";
this.outer.style.borderWidth = (data.type == "highlight") ? data.border_width + "px" : "0px";
//Set up the inner portion (text element)
this.inner.className = "inner-annotation";
this.inner.innerHTML = (data.type == "highlight") ? data.highlight.text : data.text;
this.inner.style.color = data.fontcolor;
this.inner.style.textAlign = data.textalign;
//Create the close button and add it to the inner annotation
this.closebutton = this.inner.appendChild(document.createElement("span"));
this.closebutton.className = "close-button";
this.closebutton.setAttribute("data-annotation", this.outer.id);
this.closebutton.addEventListener("click", function()
{
document.getElementById(this.getAttribute("data-annotation")).style.visibility = "hidden";
});
//Add any link icons and click actions
if(data.action_type == "openUrl")
{
this.linkicon = this.inner.appendChild(document.createElement("span")).className = "link-icon";
this.outer.addEventListener("click", function()
{
var video = document.getElementById("video");
if(data.url_id == video_id)
{
//TODO: Check if there is an actual timecode
video.currentTime = data.timecode;
}
else
{
if(data.target == "current") { location.href = "player.php?v=" + data.url_id; }
if(data.target == "new") { window.open(data.url, "_blank"); }
}
});
}
//Add hover listeners
var bgcolor = (typeof data.bgcolor !== "undefined") ? data.bgcolor : "";
var hoverbgcolor = (typeof data.hoverbgcolor !== "undefined") ? data.hoverbgcolor : "";
this.outer.addEventListener("mouseenter", function()
{
(data.type == "highlight") ? this.style.borderColor = data.hoverbordercolor : this.style.backgroundColor = hoverbgcolor;
(data.type == "highlight") ? this.style.borderWidth = data.hoverborderwidth : "0px";
});
this.outer.addEventListener("mouseleave", function()
{
(data.type == "highlight") ? this.style.borderColor = data.bordercolor : this.style.backgroundColor = bgcolor;
(data.type == "highlight") ? this.style.borderWidth = data.borderwidth : "0px";
});
//Set any dynamic properties
this.update(data);
//Add the annotation to the container
document.getElementById("annotations").appendChild(this.outer);
}
update(data)
{
//Calculate video player size
var video_width = parseFloat(window.getComputedStyle(document.querySelector("#container")).getPropertyValue("width"));
var video_height = parseFloat(window.getComputedStyle(document.querySelector("#container")).getPropertyValue("height"));
//Adjust annotation styles
this.inner.style.fontSize = (data.textsize / 100) * video_height + "px";
var padding_left = (video_width * 0.008) * ((data.style == "speech") ? 2 : 1);
var padding_top = (video_height * 0.008) * ((data.style == "speech") ? 2 : 1);
var padding_right = (padding_left * ((data.action_type == "openUrl") ? 2 : 1));
this.inner.style.paddingLeft = padding_left + "px";
this.inner.style.paddingTop = padding_top + "px";
this.inner.style.paddingRight = padding_right + "px";
this.inner.style.paddingBottom = this.inner.style.paddingTop;
//Calculate close button's position
var annotation_width = (parseFloat(data.width) / 100) * video_width;
var annotation_height = (parseFloat(data.height) / 100) * video_width;
var annotation_right = ((parseFloat(data.left) / 100) * video_width) + annotation_width;
var annotation_top = (parseFloat(data.top) / 100) * video_height;
var annotation_bottom = annotation_top + annotation_height;
var left = (annotation_right >= video_width) ? annotation_width - 16 : annotation_width - 8;
var top = (annotation_top <= 8) ? 8 : -8;
top = ((annotation_top >= (video_height - 56)) && (annotation_bottom >= (video_height - 56))) ? annotation_height - 64 : top;
this.closebutton.style.left = left + "px";
this.closebutton.style.top = top + "px";
}
}
var annotations = [];
function load_annotations(input)
{
for(var i=0; i<input.length; i+=1)
{
annotations.push(new Annotation(input[i]));
}
}
function update_annotations()
{
var video = document.getElementById("video");
var time = document.getElementById("time");
time.innerHTML = video.currentTime;
for(var i=0; i<annotations.length; i+=1)
{
var annotation = annotations[i];
if(video.currentTime <= annotation.start | video.currentTime >= annotation.stop)
{
annotation.outer.style.display = "none";
}
else
{
annotation.outer.style.display = "block";
}
}
}