forked from lichess-org/lila
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathembed-analyse.js
157 lines (143 loc) · 4.61 KB
/
embed-analyse.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
$(function() {
var studyRegex = /lichess\.org\/study\/(?:embed\/)?(\w{8})\/(\w{8})(#\d+)?\b/;
var gameRegex = /lichess\.org\/(?:embed\/)?(\w{8})(?:(?:\/(white|black))|\w{4}|)(#\d+)?\b/;
var notGames = ['training', 'analysis', 'insights', 'practice', 'features', 'password', 'streamer'];
var parseLink = function(a) {
var yt = lichess.toYouTubeEmbedUrl(a.href);
if (yt) return {
type: 'youtube',
src: yt
};
var matches = a.href.match(studyRegex);
if (matches && matches[2] && a.text.match(studyRegex)) return {
type: 'study',
src: '/study/embed/' + matches[1] + '/' + matches[2] + (matches[3] || '')
};
var matches = a.href.match(gameRegex);
if (matches && matches[1] && notGames.indexOf(matches[1]) === -1 && a.text.match(gameRegex)) {
var src = '/embed/' + matches[1];
if (matches[2]) src += '/' + matches[2]; // orientation
if (matches[3]) src += matches[3]; // ply hash
return {
type: 'game',
src: src
};
}
};
var expandYoutube = function(a) {
var $iframe = $('<iframe>').addClass('video ' + a.type).attr('src', a.src);
$(a.element).replaceWith($iframe);
return $iframe;
};
var expandYoutubes = function(as, wait) {
var a = as.shift(),
wait = Math.min(1500, wait || 100);
if (a) expandYoutube(a).on('load', function() {
setTimeout(function() {
expandYoutubes(as, wait + 200);
}, wait);
});
};
var expand = function(a) {
var $iframe = $('<iframe>').addClass('analyse ' + a.type).attr('src', a.src);
$(a.element).replaceWith($iframe);
return $iframe.on('load', function() {
if (this.contentDocument.title.indexOf("404") === 0) this.style.height = '100px';
}).on('mouseenter', function() {
$(this).focus();
});
};
var expandStudies = function(as, wait) {
var a = as.shift(),
wait = Math.min(1500, wait || 100);
if (a) expand(a).on('load', function() {
setTimeout(function() {
expandStudies(as, wait + 200);
}, wait);
});
};
function groupByParent(as) {
var groups = [];
var current = {
parent: null,
index: -1
};
as.forEach(function(a) {
if (a.parent === current.parent) groups[current.index].push(a);
else {
current = {
parent: a.parent,
index: current.index + 1
};
groups[current.index] = [a];
}
});
return groups;
};
var expandGames = function(as) {
groupByParent(as).forEach(function(group) {
if (group.length < 3) group.forEach(expand);
else group.forEach(function(a) {
a.element.title = 'Click to expand';
a.element.classList.add('text');
a.element.setAttribute('data-icon', '=');
a.element.addEventListener('click', function(e) {
if (e.button === 0) {
e.preventDefault();
expand(a);
}
});
});
});
};
var as = $('div.embed_analyse a').toArray().map(function(el) {
var parsed = parseLink(el);
if (!parsed) return false;
return {
element: el,
parent: el.parentNode,
type: parsed.type,
src: parsed.src
};
}).filter(function(a) {
return a;
});
expandYoutubes(as.filter(function(a) {
return a.type === 'youtube'
}));
expandStudies(as.filter(function(a) {
return a.type === 'study'
}).map(function(a) {
a.element.classList.add('embedding_analyse');
a.element.innerHTML = lichess.spinnerHtml;
return a;
}));
expandGames(as.filter(function(a) {
return a.type === 'game'
}));
});
lichess.startEmbeddedAnalyse = function(opts) {
opts.socketSend = $.noop
opts.initialPly = 'url';
opts.trans = lichess.trans(opts.i18n);
var container = opts.element.parentNode;
LichessAnalyse.start(opts);
var onResize = function() {
var board = container.querySelector('.cg-board-wrap');
var ground = container.querySelector('.lichess_ground');
var w = Math.max(document.documentElement.clientWidth, window.innerWidth || 0);
var h = Math.max(document.documentElement.clientHeight, window.innerHeight || 0);
var boardSize = h - 26;
var gr = 1.618;
if (boardSize > w / gr) boardSize = w / gr;
var groundSize = Math.min(700, Math.max(120, w - boardSize));
board.style.width = boardSize + 'px';
board.style.height = boardSize + 'px';
ground.style.width = groundSize + 'px';
ground.style.maxWidth = groundSize + 'px';
ground.style.height = boardSize + 'px';
lichess.dispatchEvent(document.body, 'chessground.resize');
};
onResize();
window.addEventListener('resize', lichess.fp.debounce(onResize, 500));
};