forked from lidel/pls.watch
-
Notifications
You must be signed in to change notification settings - Fork 1
/
yt-looper.editor.js
319 lines (266 loc) · 9.89 KB
/
yt-looper.editor.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
'use strict';
/* global GOOGLE_API_KEY, jackiechanMyIntervals, logLady, osd, urlFlag, urlParams, urlArgs, detectHTML5Video */
function _humanReadableTime(interval) {
var time = '';
if (interval.start) {
var start_h = Math.floor(interval.start / 3600);
var start_m = Math.floor((interval.start % 3600) / 60);
var start_s = (interval.start % 3600) % 60;
time += (start_h ? start_h + 'h' : '')
+ (start_m ? start_m + 'm' : '')
+ (start_s ? start_s + 's' : '');
}
if (interval.end) {
var end_h = Math.floor(interval.end / 3600);
var end_m = Math.floor((interval.end % 3600) / 60);
var end_s = (interval.end % 3600) % 60;
time += ';'
+ (end_h ? end_h + 'h' : '')
+ (end_m ? end_m + 'm' : '')
+ (end_s ? end_s + 's' : '');
}
return time ? '&t=' + time : '';
}
function _assembleInterval(interval) {
return interval.urlKey + '='
+ interval.videoId
+ _humanReadableTime(interval);
}
// reloadable singleton! d8> ...kek wat? fuf! o_0
function Editor(Playlist, Player) { /*jshint ignore:line*/
var _Playlist = Playlist; // these are singletons!
var _Player = Player; // but still we pass them in params to indicate dependency q:'V
// Mother, forgive us for what we have done
Editor._createAsyncVideoTitle = function (videoId, intervalLink) {
var setTitle = function(title, intervalLink) {
var intervalUri = intervalLink.text();
intervalLink.addClass('truncate');
intervalLink.attr('title', intervalUri);
intervalLink.text(title);
};
var apiRequest = 'https://www.googleapis.com/youtube/v3/videos'
+ '?part=snippet&id=' + videoId
+ '&maxResults=1'
+ '&fields=kind%2Citems%2Fsnippet(title)'
+ '&key=' + GOOGLE_API_KEY;
var retries = 3;
$.ajax({
url: apiRequest,
async: true,
success: function(data) {
if (data.kind === 'youtube#videoListResponse' && data.items.length) {
setTitle(data.items[0].snippet.title, intervalLink);
}
},
error: function(jqxhr, textStatus) {
logLady('Unable to get video title for id='+videoId+' ('+ textStatus +'): ', jqxhr);
retries = retries - 1;
}
});
};
Editor._createDrop = function (interval, caption) {
return $('<a/>').unbind().click(function () {
$(this).parent('td').parent('tr').remove();
Editor.updateHash();
}).append(caption);
};
Editor._createGoto = function(interval, index) {
var intervalLink = $('<a/>').unbind().click(function () {
_Playlist.index = index;
_Player.newPlayer(_Playlist.current());
}).append(_assembleInterval(interval));
// Keep canonical URI of a single interval in data attribute
intervalLink.attr('data-interval-uri', intervalLink.text());
if (interval.urlKey === 'v' && !detectHTML5Video(interval.videoId)) {
Editor._createAsyncVideoTitle(interval.videoId, intervalLink);
}
return intervalLink;
};
Editor._createEdit = function (interval, index, caption) {
var saveIntervalItem = function ($input) {
var val = $input.val();
var $tr = $input.parent('td').parent('tr').empty();
var newInterval = jackiechanMyIntervals(val).intervals[0];
// use original interval for failsafe
Editor._createRow(newInterval || interval, index, $tr);
Editor.editInProgress = (function () { return; })();// kek'd safe undefined
Editor.updateHash();
};
return $('<a/>').unbind().click(function () {
if (Editor.editInProgress !== undefined) {
saveIntervalItem(Editor.editInProgress);
}
var $input = $('<input type="text"/>');
$input.attr('value', _assembleInterval(interval));
// only single edit is allowed at a time
Editor.editInProgress = $input;
$input.unbind().keypress(function (ev) {
var key = ev.keyCode ? ev.keyCode : ev.which;
logLady(key);
if (key == 13 || key == 27 || key == 9) { // enter || escape || tab
saveIntervalItem($input);
return false;
}
});
$input.focusout(function() {
saveIntervalItem($input);
});
var $td1 = $('<td/>').addClass('editor-col1').html('✎');
var $td2 = $('<td/>').attr('colspan', 3).html($input);
var $tr = $(this).parent('td').parent('tr');
$tr.html($td2).prepend($td1);
// set focus to input and move cursor to its end
var inputVal = $input.focus().val();
$input.val('').val(inputVal);
}).append(caption);
};
Editor._createLink = function (interval, caption) {
return $('<a/>', {
href: '#' + _assembleInterval(interval),
target: '_blank'
}).append(caption || interval.videoId);
};
Editor._createRow = function (interval, index, $tr) {
$tr
.append($('<td/>',{ class: 'editor-col1' })
.append(Editor._createDrop(interval, '✗'))) // x
.append($('<td/>',{ class: 'editor-col2' })
.append(Editor._createGoto(interval, index)))
.append($('<td/>',{ class: 'editor-col3' })
.append(Editor._createEdit(interval, index, '✎'))) // pencil
.append($('<td/>',{ class: 'editor-col4' })
.append(Editor._createLink(interval, '⤴'))); // arrow
};
Editor.updateHighlight = function () {
logLady('Editor.updateHighlight()');
var $editor = $('#editor');
// unhighlight multiple table rows (just to be safe)
$('table tr.highlighted', $editor).removeClass('highlighted');
// highlight specific table row
var $highlight = $('table tr:nth-child('+ (Playlist.index + 1) +')', $editor).addClass('highlighted');
if ($.mCustomScrollbar) {
$editor.mCustomScrollbar('scrollTo',$highlight);
}
};
Editor.updateHash = function () {
logLady('Editor.updateHash()');
var $editor = $('#editor');
if ($editor.length) {
var href = '';
var last = href;
var params = urlParams();
$('.editor-col2>a', $editor).each(function (index) {
var $this = $(this); // such optimization! c/\o
// reindex 'goto' links
$this.unbind().click(function () {
_Playlist.index = index;
_Player.newPlayer(_Playlist.current());
});
var text = $this.attr('title') || $this.text();
var part = text.split('&');
if (last === part[0] && part.length > 1) {
// join time ranges
var time = part[1].split('=')[1];
if (time) {
href += ('+' + time);
}
} else {
// add full interval
href += ('&' + text);
last = part[0];
}
});
document.location.replace('#' + href.substr(1) + urlArgs(params));
}
};
Editor._renderRows = function ($tbody) {
_(_Playlist.intervals).each(function (interval, index) {
Editor._createRow(interval, index, $('<tr/>').appendTo($tbody));
});
Editor.updateHighlight();
};
Editor.create = _.once(function() {
var $editor = $('<div/>', { id: 'editor' });
var $table = $('<table/>');
var $tbody = $('<tbody/>').sortable({ update: Editor.updateHash })
.appendTo($table);
$table.appendTo($editor.hide());
$editor.appendTo('body');
Editor._renderRows($tbody);
});
Editor.reload = function () {
logLady('Editor.reload()');
var $editor = $('#editor');
if ($editor.length > 0) {
var $table = $('table', $editor).first();
var $tbody = $('tbody', $table).first();
$tbody.children('tr').remove();
Editor._renderRows($tbody);
}
};
Editor.show = function() {
Editor.toggle(true);
};
Editor.toggle = function (show) {
logLady('Editor.toggle()' + (show ? show : ''));
var showGui = function() {
$('#editor').show();
$('#editor-ui').addClass('ticker');
};
var toggleGui = function() {
osd('Toggled editor');
$('#editor').toggle('slide');
$('#editor-ui').toggleClass('ticker');
};
var $editor = $('#editor');
if ($editor.length > 0) {
if (show === true) {showGui();} else {toggleGui();}
} else {
$LAB
// load jQuery UI if editor has been requested for the first time
.script('https://cdn.jsdelivr.net/jquery.ui/1.11.4/jquery-ui.min.js')
.wait(function () {
Editor.create();
$editor = $('#editor');
})
.script(function () {
// load scrollbar assets only when it potentially makes sense
if (Playlist.intervals.length > 10 && !$.mCustomScrollbar) {
// Load CDN version of mCustomScrollbar by malihu (MIT)
// http://manos.malihu.gr/jquery-custom-content-scroller/
$('<link>')
.appendTo('head')
.attr({type : 'text/css', rel : 'stylesheet'})
.attr('href', 'https://cdn.jsdelivr.net/jquery.mcustomscrollbar/3.1.1/jquery.mCustomScrollbar.min.css');
return 'https://cdn.jsdelivr.net/jquery.mcustomscrollbar/3.1.1/jquery.mCustomScrollbar.concat.min.js';
} else {
return null;
}
})
.wait(function() {
if ($.mCustomScrollbar && !$editor.hasClass('mCustomScrollbar')) {
$editor.mCustomScrollbar({
axis: 'y',
mouseWheel: { axis: 'y' },
scrollInertia: 0,
theme: 'minimal'
}).css('padding-right','16px');
}
Editor.updateHighlight();
if (show === true) {showGui();} else {toggleGui();}
});
}
};
Editor.register = function () {
logLady('Editor.register()');
$('#editor-toggle').unbind().click(Editor.toggle);
_Player.registerEditorNotification(Editor.updateHighlight); // update on interval switch
Editor.reload(); // update on hash change
// show editor if requested via URL
if (urlFlag('editor')) {
Editor.show();
}
};
Editor.register();
}
// vim:ts=2:sw=2:et: