-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathscript.js
More file actions
739 lines (588 loc) · 16.5 KB
/
Copy pathscript.js
File metadata and controls
739 lines (588 loc) · 16.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
(function () {
"use strict";
// Module for performing actions as soon as possible
var on_ready = (function () {
// Vars
var callbacks = [],
check_interval = null,
check_interval_time = 250;
// Check if ready and run callbacks
var callback_check = function () {
if (
(document.readyState === "interactive" || document.readyState === "complete") &&
callbacks !== null
) {
// Run callbacks
var cbs = callbacks,
cb_count = cbs.length,
i;
// Clear
callbacks = null;
for (i = 0; i < cb_count; ++i) {
cbs[i].call(null);
}
// Clear events and checking interval
window.removeEventListener("load", callback_check, false);
window.removeEventListener("readystatechange", callback_check, false);
if (check_interval !== null) {
clearInterval(check_interval);
check_interval = null;
}
// Okay
return true;
}
// Not executed
return false;
};
// Listen
window.addEventListener("load", callback_check, false);
window.addEventListener("readystatechange", callback_check, false);
// Callback adding function
return function (cb) {
if (callbacks === null) {
// Ready to execute
cb.call(null);
}
else {
// Delay
callbacks.push(cb);
// Set a check interval
if (check_interval === null && callback_check() !== true) {
check_interval = setInterval(callback_check, check_interval_time);
}
}
};
})();
var bind = function (callback, self) {
if (arguments.length > 2) {
var slice = Array.prototype.slice,
push = Array.prototype.push,
args = slice.call(arguments, 2);
return function () {
var full_args = slice.call(args);
push.apply(full_args, arguments);
return callback.apply(self, full_args);
};
}
else {
return function () {
return callback.apply(self, arguments);
};
}
};
var restyle_noscript = function () {
// Script
var nodes = document.querySelectorAll(".script_disabled"),
i;
for (i = 0; i < nodes.length; ++i) {
nodes[i].classList.remove("script_visible");
}
nodes = document.querySelectorAll(".script_enabled");
for (i = 0; i < nodes.length; ++i) {
nodes[i].classList.add("script_visible");
}
};
var script_add = (function () {
var script_on_load = function (state, event) {
// Okay
script_remove_event_listeners.call(this, state, true);
};
var script_on_error = function (state, event) {
// Error
script_remove_event_listeners.call(this, state, false);
};
var script_on_readystatechange = function (state, event) {
if (this.readyState === "loaded" || this.readyState === "complete") {
// Okay
script_remove_event_listeners.call(this, state, true);
}
};
var script_remove_event_listeners = function (state, okay) {
// Remove event listeners
this.addEventListener("load", state.on_load, false);
this.addEventListener("error", state.on_error, false);
this.addEventListener("readystatechange", state.on_readystatechange, false);
state.on_load = null;
state.on_error = null;
state.on_readystatechange = null;
// Trigger
if (state.callback) state.callback.call(null, okay, this);
// Remove
var par = this.parentNode;
if (par) par.removeChild(this);
};
return function (url, callback) {
var head = document.head,
script, state;
if (!head) {
// Callback and done
callback.call(null, false, null);
return false;
}
// Load state
state = {
on_load: null,
on_error: null,
on_readystatechange: null,
callback: callback,
};
// New script tag
script = document.createElement("script");
script.async = true;
script.setAttribute("src", url);
// Events
script.addEventListener("load", (state.on_load = script_on_load.bind(script, state)), false);
script.addEventListener("error", (state.on_error = script_on_error.bind(script, state)), false);
script.addEventListener("readystatechange", (state.on_readystatechange = script_on_readystatechange.bind(script, state)), false);
// Add
head.appendChild(script);
// Done
return true;
};
})();
// Class to manage URL navigation with the hash fragment
var HashNavigation = (function () {
var HashNavigation = function () {
this.sep = "#!";
this.events = {
"change": [],
};
};
var re_encode_pretty = /\%20/g,
re_decode_pretty = /\+/g,
re_decode_var = /^(.*?)(?:=(.*))?$/,
re_encode_simple = /[ %]/g,
re_encode_simple_map = {
" ": "+",
"%": "%25",
},
re_url_info = /^(?:([a-z][a-z0-9\-\+\.]*):)?(?:\/*)([^\/\?\#]*)([^\?\#]*)([^\#]*)(.*)$/i,
re_url_offset_str = "x:x/x?x#";
var escape_var = function (str) {
return encodeURIComponent(str).replace(re_encode_pretty, "+");
};
var escape_var_simple = function (str) {
return str.replace(re_encode_simple, escape_var_simple_replacer);
};
var escape_var_simple_replacer = function (m) {
return re_encode_simple_map[m[0]];
};
var unescape_var = function (str) {
return decodeURIComponent(str.replace(re_decode_pretty, "%20"));
};
var on_window_popstate = function (event) {
// Trigger
trigger.call(this, "change", {
init: false,
pop: true,
});
}
var trigger = function (event, data) {
// Trigger an event
var callbacks = this.events[event],
i;
for (i = 0; i < callbacks.length; ++i) {
callbacks[i].call(this, data, event);
}
};
HashNavigation.prototype = {
constructor: HashNavigation,
/**
Get information from a URL string.
@param url
The stringified URL
@param offset
An integer representing the URL component to start at.
Valid values are:
0: start at the protocol (this is the default)
1: start at the website name
2: start at the website path
3: start at the search
4: start at the hash fragment
Invalid values will cause undefined behavior
@return
An object of the form:
{
protocol: string,
site: string,
path: string,
search: string,
hash: string,
}
*/
get_url_parts: function (url, offset) {
var default_value = "",
parts = [
default_value, // protocol
default_value, // site
default_value, // path
default_value, // search
default_value, // hash
],
match, i, obj;
// Update string and match
offset = offset || 0;
if (offset > 0) url = re_url_offset_str.substr(0, offset * 2) + url;
match = re_url_info.exec(url);
// Parts
for (i = offset; i < 5; ++i) {
parts[i] = match[i + 1] || default_value;
}
// Object
obj = {
protocol: parts[0],
site: parts[1],
path: parts[2],
search: parts[3],
hash: parts[4],
};
// Done
return obj;
},
strip_hash: function (hash) {
var i;
for (i = 0; i < this.sep.length; ++i) {
if (hash[i] != this.sep[i]) break;
}
if (i > 0) {
hash = hash.substr(i);
}
return hash;
},
strip_search: function (search) {
return (search[0] == "?") ? search.substr(1) : search;
},
encode_vars: function (vars, escape_components) {
var str = "",
first = true,
escape_fcn = (escape_components == null || escape_components) ? escape_var : escape_var_simple,
v;
if (Array.isArray(vars)) {
for (v = 0; v < vars.length; ++v) {
if (v > 0) str += "&";
str += escape_fcn(vars[v][0]);
if (vars[v].length > 1) {
str += "=";
str += escape_fcn(vars[v][1]);
}
}
}
else {
for (v in vars) {
if (first) first = false;
else str += "&";
str += escape_fcn(v);
if (vars[v] != null) {
str += "=";
str += escape_fcn(vars[v]);
}
}
}
return str;
},
decode_vars: function (str) {
var vars = {},
str_split = str.split("&"),
match, i;
for (i = 0; i < str_split.length; ++i) {
// Get the match
if (str_split[i].length == 0) continue;
match = re_decode_var.exec(str_split[i]);
// Set the var
vars[unescape_var(match[1])] = (match[2] == null) ? null : unescape_var(match[2]);
}
// Return the vars
return vars;
},
setup: function () {
// Events
window.addEventListener("popstate", on_window_popstate.bind(this), false);
// Init trigger
trigger.call(this, "change", {
init: true,
pop: false,
});
},
go: function (hash, replace) {
// Setup url
var url = window.location.pathname,
i;
if (hash != null) {
url += this.sep + this.strip_hash(hash);
}
if (replace) {
window.history.replaceState({}, "", url);
}
else {
window.history.pushState({}, "", url);
}
// Trigger
trigger.call(this, "change", {
init: false,
pop: false,
});
},
on: function (event, callback) {
if (event in this.events) {
this.events[event].push(callback);
return true;
}
return false;
},
off: function (event, callback) {
if (event in this.events) {
var callbacks = this.events[event],
i;
for (i = 0; i < callbacks.length; ++i) {
if (callbacks[i] == callback) {
callbacks.splice(i, 1);
return true;
}
}
}
return false;
},
};
return HashNavigation;
})();
// Module to get geometry
var Geometry = (function () {
var Rect = function (left, top, right, bottom) {
this.left = left;
this.top = top;
this.right = right;
this.bottom = bottom;
};
var functions = {
document_rect: function () {
var win = window,
doc = document.documentElement,
left = (win.pageXOffset || doc.scrollLeft || 0) - (doc.clientLeft || 0),
top = (win.pageYOffset || doc.scrollTop || 0) - (doc.clientTop || 0);
return new Rect(
left,
top,
left + (doc.clientWidth || win.innerWidth || 0),
top + (doc.clientHeight || win.innerHeight || 0)
);
},
object_rect: function (obj) {
var bounds = obj.getBoundingClientRect(),
win = window,
doc = document.documentElement,
left = (win.pageXOffset || doc.scrollLeft || 0) - (doc.clientLeft || 0),
top = (win.pageYOffset || doc.scrollTop || 0) - (doc.clientTop || 0);
return new Rect(
left + bounds.left,
top + bounds.top,
left + bounds.right,
top + bounds.bottom
);
},
};
return functions;
})();
// Navigation panel control class
var NavigationPanel = (function () {
var NavigationPanel = function () {
this.okay = (
(this.column = document.querySelector(".navigation_column")) !== null &&
(this.container = this.column.querySelector(".navigation_container")) !== null &&
(this.main = this.container.querySelector(".navigation")) !== null &&
(this.padding = this.main.querySelector(".navigation_padding")) !== null &&
(this.body = this.padding.querySelector(".navigation_body")) !== null
);
this.floating = (this.okay && this.column.classList.contains("navigation_column_floating"));
this.bottom = (this.okay && this.column.classList.contains("navigation_bottom"));
};
var update_navigation_scroll = function () {
if (!this.floating) {
var doc_rect = Geometry.document_rect(),
main_rect = Geometry.object_rect(this.main);
if (this.bottom) {
if (main_rect.bottom > doc_rect.bottom) {
this.column.classList.add("navigation_column_floating");
this.floating = true;
}
}
else {
if (main_rect.top < doc_rect.top) {
this.column.classList.add("navigation_column_floating");
this.floating = true;
}
}
}
if (this.floating) {
var column_rect = Geometry.object_rect(this.column),
body_rect = Geometry.object_rect(this.body);
if (body_rect.top <= column_rect.top) {
// Align to top
this.column.classList.remove("navigation_column_floating");
this.column.classList.remove("navigation_bottom");
this.floating = false;
this.bottom = false;
}
else if (body_rect.bottom >= column_rect.bottom) {
// Align to bottom
this.column.classList.remove("navigation_column_floating");
this.column.classList.add("navigation_bottom");
this.floating = false;
this.bottom = true;
}
}
};
NavigationPanel.prototype = {
constructor: NavigationPanel,
setup: function () {
var self = this;
update_navigation_scroll.call(this);
window.addEventListener("scroll", function (event) {
update_navigation_scroll.call(self);
}, false);
},
};
return NavigationPanel;
})();
// Functions
var nav = new HashNavigation();
nav.sep = "#";
var on_doc_name_click = function (event) {
// find doc_block parent
var block;
for (block = this.parentNode; block !== null; block = block.parentNode) {
if (block.classList.contains("doc_block")) {
// Perform
var id = block.getAttribute("id"),
display_mode = block.querySelector(".doc_block_display_mode:checked"),
display_mode_id = (display_mode ? 1 - (+display_mode.value) : 0),
target;
if ((target = block.querySelector(".doc_block_display_mode_" + display_mode_id)) !== null) {
target.checked = true;
if (id) {
if (display_mode_id == 0) {
if (window.location.hash == "#" + id) {
nav.go(null, true);
}
}
else {
nav.go("#" + id, true);
}
}
}
// Done
event.preventDefault();
event.stopPropagation();
break;
}
}
};
var on_demo_start_click = function (event) {
// Start demo
begin_demo();
// Update URL
nav.go(this.getAttribute("href") || "", true);
// Stop click
event.preventDefault();
event.stopPropagation();
return false;
};
var on_doc_expand_all_click = function (event) {
var nodes = document.querySelectorAll(".doc_block_display_mode.doc_block_display_mode_1"),
i;
for (i = 0; i < nodes.length; ++i) {
nodes[i].checked = true;
}
};
var on_doc_shrink_all_click = function (event) {
var nodes = document.querySelectorAll(".doc_block_display_mode.doc_block_display_mode_0"),
i;
for (i = 0; i < nodes.length; ++i) {
nodes[i].checked = true;
}
};
var begin_demo = (function () {
var already_started = false;
var on_script_ready = function (data, error_message, okay, script) {
var error_message, node, youtube_module, demo_class;
++data.load_count;
if (!okay) {
data.errors.push(error_message);
}
if (data.load_count == data.load_count_total) {
if (data.errors.length > 0) {
error_message = data.errors.join("; ") + ".";
}
else {
// Get vars
try {
youtube_module = Youtube;
}
catch (e) {
youtube_module = null;
}
try {
demo_class = Demo;
}
catch (e) {
demo_class = null;
}
if (youtube_module && demo_class) {
// Show
node = document.querySelector(".demo_region_preload");
if (node) node.classList.remove("demo_region_preload_visible");
// Run, and expose demo for testing using console if desired
window.demo = new demo_class(youtube_module);
// Done
return;
}
// Error
error_message = "Script load failed.";
}
// Display error
node = document.querySelector(".demo_status_message");
if (node) node.textContent = error_message;
}
};
return function () {
// Only run once
if (already_started) return;
already_started = true;
var data = {
status_node: document.querySelector(".demo_status_message"),
load_count: 0,
load_count_total: 2,
errors: [],
};
// Add script
script_add("src/youtube_iframe_api.js", bind(on_script_ready, null, data, "Error loading iframe API"));
script_add("src/demo.js", bind(on_script_ready, null, data, "Error loading demo script"));
// Update node display
if (data.status_node) {
data.status_node.classList.add("demo_status_message_visible");
}
};
})();
// Execute
on_ready(function () {
// Noscript
restyle_noscript();
// Window scrolling
var np = new NavigationPanel();
if (np.okay) np.setup();
// Change some label links
var nodes, i;
nodes = document.querySelectorAll(".doc_name,.doc_member_name");
for (i = 0; i < nodes.length; ++i) {
nodes[i].addEventListener("click", on_doc_name_click, false);
}
if ((i = document.getElementById("doc_expand_all"))) {
i.addEventListener("click", on_doc_expand_all_click, false);
}
if ((i = document.getElementById("doc_shrink_all"))) {
i.addEventListener("click", on_doc_shrink_all_click, false);
}
if ((i = document.getElementById("demo_start"))) {
i.addEventListener("click", on_demo_start_click, false);
}
});
})();