forked from wrongu/RocAlphaGo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayer.permalink.js
More file actions
76 lines (64 loc) · 1.89 KB
/
player.permalink.js
File metadata and controls
76 lines (64 loc) · 1.89 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
(function(WGo, undefined) {
"use strict";
var permalink = {
active: true,
query: {},
};
var handle_hash = function(player) {
try {
permalink.query = JSON.parse('{"'+window.location.hash.substr(1).replace('=', '":')+'}');
}
catch(e) {
permalink.query = {};
}
}
// add hashchange event
window.addEventListener("hashchange", function() {
if(window.location.hash != "" && permalink.active) {
handle_hash();
for(var key in permalink.query) {
var p_el = document.getElementById(key);
if(p_el && p_el._wgo_player) p_el._wgo_player.goTo(move_from_hash);
}
}
});
// save hash query (after DOM is loaded - you can turn this off by setting WGo.Player.permalink.active = false;
window.addEventListener("DOMContentLoaded", function() {
if(window.location.hash != "" && permalink.active) {
handle_hash();
}
});
// scroll into view of the board
window.addEventListener("load", function() {
if(window.location.hash != "" && permalink.active) {
for(var key in permalink.query) {
var p_el = document.getElementById(key);
if(p_el && p_el._wgo_player) {
p_el.scrollIntoView();
break;
}
}
}
});
var move_from_hash = function() {
if(permalink.query[this.element.id]) {
return permalink.query[this.element.id].goto;
}
}
WGo.Player.default.move = move_from_hash;
// add menu item
if(WGo.BasicPlayer && WGo.BasicPlayer.component.Control) {
WGo.BasicPlayer.component.Control.menu.push({
constructor: WGo.BasicPlayer.control.MenuItem,
args: {
name: "permalink",
click: function(player) {
var link = location.href.split("#")[0]+'#'+player.element.id+'={"goto":'+JSON.stringify(player.kifuReader.path)+'}';
player.showMessage('<h1>'+WGo.t('permalink')+'</h1><p><input class="wgo-permalink" type="text" value=\''+link+'\' onclick="this.select(); event.stopPropagation()"/></p>');
},
}
});
}
WGo.Player.permalink = permalink;
WGo.i18n.en["permalink"] = "Permanent link";
})(WGo);