-
Notifications
You must be signed in to change notification settings - Fork 3
/
engine.js
117 lines (98 loc) · 5.88 KB
/
engine.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
/*
* pgn4web javascript chessboard
* copyright (C) 2009-2014 Paolo Casaschi
* see README file and http://pgn4web.casaschi.net
* for credits, license and more details
*/
"use strict";
if (typeof(pgn4web_engineWindowDisableAnalysisBoard) == "undefined") { var pgn4web_engineWindowDisableAnalysisBoard = false; }
var pgn4web_engineWindowTarget = "pgn4webEngineAnalysisBoard";
var pgn4web_engineWindowUrlParameters = "";
var pgn4web_engineWindowHeight = 30 * 12; // window height/width corresponding to default squareSize = 30
var pgn4web_engineWindowWidth = 30 * 10;
// notes:
// - all pages on the same site will use the same analysis board popup; if the analysis board is embedded as iframe within a page (see the live-results.html example) the pgn4web_engineWindowTarget variable should be customized in order to prevent conflicts
// - if pgn4web_engineWindowUrlParameters is customized using the corresponding URL parameter of the main page, the value must be encoded with encodeURIComponent()
if (typeof(thisRegExp) == "undefined") { var thisRegExp; }
thisRegExp = /(&|\?)(engineWindowDisableAnalysisBoard|ewdab)=(true|t)(&|$)/i;
if (window.location.search.match(thisRegExp) !== null) {
pgn4web_engineWindowDisableAnalysisBoard = true;
}
thisRegExp = /(&|\?)(engineWindowTarget|ewt)=([^&]+)(&|$)/i;
if (window.location.search.match(thisRegExp) !== null) {
pgn4web_engineWindowTarget = unescape(window.location.search.match(thisRegExp)[3]);
}
thisRegExp = /(&|\?)(engineWindowUrlParameters|ewup)=([^&]+)(&|$)/i;
if (window.location.search.match(thisRegExp) !== null) {
pgn4web_engineWindowUrlParameters = unescape(window.location.search.match(thisRegExp)[3]);
}
thisRegExp = /(&|\?)(engineWindowHeight|ewh)=([1-9][0-9]*)(&|$)/i;
if (window.location.search.match(thisRegExp) !== null) {
pgn4web_engineWindowHeight = parseInt(unescape(window.location.search.match(thisRegExp)[3]), 10);
}
thisRegExp = /(&|\?)(engineWindowWidth|eww)=([1-9][0-9]*)(&|$)/i;
if (window.location.search.match(thisRegExp) !== null) {
pgn4web_engineWindowWidth = parseInt(unescape(window.location.search.match(thisRegExp)[3]), 10);
}
if (!pgn4web_engineWindowDisableAnalysisBoard) {
boardShortcut("A8", "pgn4web v" + pgn4web_version + " debug info", function(t,e){ if (e.shiftKey) { if (engineWinCheck()) { engineWin.displayDebugInfo(); } } else { displayDebugInfo(); } }, true);
boardShortcut("E8", "open/update analysis board", function(t,e){ showEngineAnalysisBoard(e.shiftKey); });
boardShortcut("F8", "close/pause analysis board", function(t,e){ if (engineWinCheck()) { if (e.shiftKey) { if ((engineWin.top === engineWin.self) && (engineWin.focus)) { engineWin.focus(); } } else { engineWin.StopBackgroundEngine(); if ((engineWin.top === engineWin.self) && (engineWin.close)) { engineWin.close(); } } } });
}
function customShortcutKey_Shift_8() { showEngineAnalysisBoard(true); }
function customShortcutKey_Shift_9() { showEngineAnalysisBoard(false); }
function customShortcutKey_Shift_0() { showEngineAnalysisBoard(); }
var pgn4web_engineWinSignature = Math.ceil(1073741822 * Math.random()); // from 1 to (2^30 -1) = 1073741823
var engineWinParametersSeparator = "?";
function detectEngineLocation() {
return detectJavascriptLocation().replace(/(pgn4web|pgn4web-compacted)\.js/, "engine.html");
}
var engineWin;
var engineWinLastFen = "";
var warnedAboutUnsupportedVariation = "";
function showEngineAnalysisBoard(engineDisabled, startFen) {
if (pgn4web_engineWindowDisableAnalysisBoard) { return null; }
if ((typeof(gameVariant[currentGame]) == "undefined") || (gameVariant[currentGame].match(/^\s*(|chess|normal|standard)\s*$/i) !== null)) {
warnedAboutUnsupportedVariation = "";
engineWinLastFen = startFen ? FenStringStart : CurrentFEN();
var doneAccessingDOM = false;
try {
if (engineWinCheck()) {
if (typeof(engineDisabled) != "undefined") {
engineWin.setDisableEngine(engineDisabled);
}
engineWin.updateFEN(engineWinLastFen);
doneAccessingDOM = true;
}
} catch(e) {}
if (!doneAccessingDOM) {
var parameters = "fs=" + encodeURIComponent(engineWinLastFen) + "&es=" + pgn4web_engineWinSignature;
if (engineDisabled) { parameters += "&de=t"; }
if (pgn4web_engineWindowUrlParameters) { parameters += "&" + pgn4web_engineWindowUrlParameters; }
var options = "resizable=no,scrollbars=no,toolbar=no,location=no,menubar=no,status=no";
if (pgn4web_engineWindowHeight) { options = "height=" + pgn4web_engineWindowHeight + "," + options; }
if (pgn4web_engineWindowWidth) { options = "width=" + pgn4web_engineWindowWidth + "," + options; }
engineWin = window.open(detectEngineLocation() + engineWinParametersSeparator + parameters, pgn4web_engineWindowTarget, options);
// note bug with IE and Opera failing to set window.opener at this point, resulting in no autoUpdate possible and no update from the engine window possible
}
if ((engineWinCheck(true)) && (engineWin.top === engineWin.self) && (window.focus)) { engineWin.focus(); }
return engineWin;
} else if (warnedAboutUnsupportedVariation != gameVariant[currentGame]) {
warnedAboutUnsupportedVariation = gameVariant[currentGame];
myAlert("warning: analysis board unavailable for the " + gameVariant[currentGame] + " variant", true);
}
return null;
}
function engineWinCheck(skipSignature) {
return ((!pgn4web_engineWindowDisableAnalysisBoard) && (typeof(engineWin) == "object") && (engineWin !== null) && (!engineWin.closed) && (typeof(engineWin.engineSignature) != "undefined") && ((pgn4web_engineWinSignature === engineWin.engineSignature) || (skipSignature)));
}
function engineWinOnMove() {
if (engineWinCheck()) {
if ((engineWin.autoUpdate === true) && (CurrentFEN() != engineWinLastFen) && (engineWin.CurrentFEN() == engineWinLastFen)) {
showEngineAnalysisBoard();
}
engineWin.updateGameAnalysisFlag();
} else {
engineWinLastFen = "";
}
}