blob: ca3d78f8f40af10fcce33a5a7403812476eb8d69 (
plain) (
blame)
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
|
package client;
import js.html.Element;
import js.html.VideoElement;
import js.Browser.document;
import client.Main.ge;
import Types.VideoItem;
using Lambda;
class Player {
final main:Main;
final items:Array<VideoItem> = [];
final videoItemsEl = ge("#queue");
final player:Element = ge("#ytapiplayer");
var isLoaded = false;
var skipSetTime = false;
var video:VideoElement;
public function new(main:Main):Void {
this.main = main;
}
public function setVideo(item:VideoItem):Void {
isLoaded = false;
video = document.createVideoElement();
video.id = "videoplayer";
item.url = main.tryLocalIp(item.url);
video.src = item.url;
video.controls = true;
video.oncanplaythrough = e -> {
if (!isLoaded) main.send({type: VideoLoaded});
isLoaded = true;
}
video.onseeking = e -> {
if (skipSetTime) {
skipSetTime = false;
return;
}
if (!main.isLeader()) return;
main.send({
type: SetTime,
setTime: {
time: video.currentTime
}
});
}
video.onpause = e -> {
if (!main.isLeader()) return;
main.send({
type: Pause,
pause: {
time: video.currentTime
}
});
}
video.onplay = e -> {
if (!main.isLeader()) return;
main.send({
type: Play,
play: {
time: video.currentTime
}
});
}
player.innerHTML = "";
player.appendChild(video);
ge("#currenttitle").innerHTML = item.title;
}
public function addVideoItem(item:VideoItem, atEnd:Bool):Void {
items.push(item);
final itemEl = nodeFromString(
'<li class="queue_entry pluid-0 queue_temp queue_active" title="${Lang.get("addedBy")}: ${item.author}">
<a class="qe_title" href="${item.url}" target="_blank">${item.title}</a>
<span class="qe_time">${duration(item.duration)}</span>
<div class="qe_clear"></div>
<div class="btn-group" style="display: inline-block;">
<button class="btn btn-xs btn-default qbtn-play">
<span class="glyphicon glyphicon-play"></span>${Lang.get("play")}
</button>
<button class="btn btn-xs btn-default qbtn-next">
<span class="glyphicon glyphicon-share-alt"></span>${Lang.get("skip")}
</button>
<button class="btn btn-xs btn-default qbtn-tmp">
<span class="glyphicon glyphicon-flag"></span>${Lang.get("makePermanent")}
</button>
<button class="btn btn-xs btn-default qbtn-delete" id="btn-delete">
<span class="glyphicon glyphicon-trash"></span>${Lang.get("delete")}
</button>
</div>
</li>'
);
final deleteBtn = itemEl.querySelector("#btn-delete");
deleteBtn.onclick = e -> {
main.send({
type: RemoveVideo,
removeVideo: {
url: itemEl.querySelector(".qe_title").getAttribute("href")
}
});
}
if (atEnd) videoItemsEl.appendChild(itemEl);
else Utils.insertAtIndex(videoItemsEl, itemEl, 1);
updateCounters();
}
public function removeVideo():Void {
if (video == null) return;
player.removeChild(video);
video = null;
ge("#currenttitle").innerHTML = Lang.get("nothingPlaying");
}
public function removeItem(url:String):Void {
for (child in videoItemsEl.children) {
if (child.querySelector(".qe_title").getAttribute("href") == url) {
videoItemsEl.removeChild(child);
break;
}
}
items.remove(
items.find(item -> item.url == url)
);
if (video.src == url) {
if (items.length > 0) setVideo(items[0]);
}
updateCounters();
}
function updateCounters():Void {
ge("#plcount").innerHTML = '${items.length} ${Lang.get("videos")}';
ge("#pllength").innerHTML = totalDuration();
}
public function getItems():Array<VideoItem> {
return items;
}
public function setItems(list:Array<VideoItem>):Void {
clearItems();
if (list.length == 0) return;
if (video == null || video.src != list[0].url) {
setVideo(list[0]);
}
for (video in list) {
addVideoItem(video, true);
}
}
public function clearItems():Void {
items.resize(0);
videoItemsEl.innerHTML = "";
updateCounters();
}
public function refresh():Void {
if (items.length == 0) return;
removeVideo();
setVideo(items[0]);
}
function duration(time:Float):String {
final h = Std.int(time / 60 / 60);
final m = Std.int(time / 60) - h * 60;
final s = Std.int(time % 60);
var time = '$m:';
if (m < 10) time = '0$time';
if (h > 0) time = '$h:$time';
if (s < 10) time = time + "0";
time += s;
return time;
}
function totalDuration():String {
var time = 0.0;
for (item in items) time += item.duration;
return duration(time);
}
function nodeFromString(div:String):Element {
final wrapper = document.createDivElement();
wrapper.innerHTML = div;
return wrapper.firstElementChild;
}
public function isListEmpty():Bool {
return items.length == 0;
}
public function pause():Void {
if (video == null) return;
video.pause();
}
public function play():Void {
if (video == null) return;
video.play();
}
public function setTime(time:Float, isLocal = true):Void {
if (video == null) return;
skipSetTime = isLocal;
video.currentTime = time;
}
public function getTime():Float {
if (video == null) return 0;
return video.currentTime;
}
}
|