1/*
2 *  pgn4web javascript chessboard
3 *  copyright (C) 2009-2015 Paolo Casaschi
4 *  see README file and http://pgn4web.casaschi.net
5 *  for credits, license and more details
6 */
7
8"use strict";
9
10if (typeof(pgn4web_engineWindowDisableAnalysisBoard) == "undefined") { var pgn4web_engineWindowDisableAnalysisBoard = false; }
11
12var pgn4web_engineWindowTarget = "pgn4webEngineAnalysisBoard";
13var pgn4web_engineWindowUrlParameters = "";
14var pgn4web_engineWindowHeight = 30 * 12; // window height/width corresponding to default squareSize = 30
15var pgn4web_engineWindowWidth = 30 * 10;
16
17// notes:
18// - 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
19// - if pgn4web_engineWindowUrlParameters is customized using the corresponding URL parameter of the main page, the value must be encoded with encodeURIComponent()
20
21if (typeof(thisRegExp) == "undefined") { var thisRegExp; }
22thisRegExp = /(&|\?)(engineWindowDisableAnalysisBoard|ewdab)=(true|t)(&|$)/i;
23if (window.location.search.match(thisRegExp) !== null) {
24  pgn4web_engineWindowDisableAnalysisBoard = true;
25}
26thisRegExp = /(&|\?)(engineWindowTarget|ewt)=([^&]+)(&|$)/i;
27if (window.location.search.match(thisRegExp) !== null) {
28  pgn4web_engineWindowTarget = unescape(window.location.search.match(thisRegExp)[3]);
29}
30thisRegExp = /(&|\?)(engineWindowUrlParameters|ewup)=([^&]+)(&|$)/i;
31if (window.location.search.match(thisRegExp) !== null) {
32  pgn4web_engineWindowUrlParameters = unescape(window.location.search.match(thisRegExp)[3]);
33}
34thisRegExp = /(&|\?)(engineWindowHeight|ewh)=([1-9][0-9]*)(&|$)/i;
35if (window.location.search.match(thisRegExp) !== null) {
36  pgn4web_engineWindowHeight = parseInt(unescape(window.location.search.match(thisRegExp)[3]), 10);
37}
38thisRegExp = /(&|\?)(engineWindowWidth|eww)=([1-9][0-9]*)(&|$)/i;
39if (window.location.search.match(thisRegExp) !== null) {
40  pgn4web_engineWindowWidth = parseInt(unescape(window.location.search.match(thisRegExp)[3]), 10);
41}
42
43
44if (!pgn4web_engineWindowDisableAnalysisBoard) {
45  boardShortcut("A8", "pgn4web v" + pgn4web_version + " debug info", function(t,e){ if (e.shiftKey) { if (engineWinCheck()) { engineWin.displayDebugInfo(); } } else { displayDebugInfo(); } }, true);
46  boardShortcut("E8", "open/update analysis board", function(t,e){ showEngineAnalysisBoard(e.shiftKey); });
47  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(); } } } });
48}
49
50function customShortcutKey_Shift_8() { showEngineAnalysisBoard(true); }
51function customShortcutKey_Shift_9() { showEngineAnalysisBoard(false); }
52function customShortcutKey_Shift_0() { showEngineAnalysisBoard(); }
53
54
55var pgn4web_engineWinSignature = Math.ceil(1073741822 * Math.random()); // from 1 to (2^30 -1) = 1073741823
56
57var engineWinParametersSeparator = "?";
58function detectEngineLocation() {
59  return detectJavascriptLocation().replace(/(pgn4web|pgn4web-compacted)\.js/, "engine.html");
60}
61
62var engineWin;
63
64var engineWinLastFen = "";
65
66var warnedAboutUnsupportedVariation = "";
67
68function showEngineAnalysisBoard(engineDisabled, startFen) {
69  if (pgn4web_engineWindowDisableAnalysisBoard) { return null; }
70  if ((typeof(gameVariant[currentGame]) == "undefined") || (gameVariant[currentGame].match(/^\s*(|chess|normal|standard)\s*$/i) !== null) || (startFen)) {
71    warnedAboutUnsupportedVariation = "";
72    engineWinLastFen = startFen ? FenStringStart : CurrentFEN();
73    var doneAccessingDOM = false;
74    try {
75      if (engineWinCheck()) {
76        if (typeof(engineDisabled) != "undefined") {
77          engineWin.setDisableEngine(engineDisabled);
78        }
79        engineWin.updateFEN(engineWinLastFen);
80        doneAccessingDOM = true;
81      }
82    } catch(e) {}
83    if (!doneAccessingDOM) {
84      var parameters = "fs=" + encodeURIComponent(engineWinLastFen) + "&es=" + pgn4web_engineWinSignature;
85      if (engineDisabled) { parameters += "&de=t"; }
86      if (pgn4web_engineWindowUrlParameters) { parameters += "&" + pgn4web_engineWindowUrlParameters; }
87      var options = "resizable=no,scrollbars=no,toolbar=no,location=no,menubar=no,status=no";
88      if (pgn4web_engineWindowHeight) { options = "height=" + pgn4web_engineWindowHeight + "," + options; }
89      if (pgn4web_engineWindowWidth) { options = "width=" + pgn4web_engineWindowWidth + "," + options; }
90      engineWin = window.open(detectEngineLocation() + engineWinParametersSeparator + parameters, pgn4web_engineWindowTarget, options);
91
92      // bugfix: IE and Opera fail to set window.opener at this point, resulting in no autoUpdate possible and no update from the engine window possible; no fix available
93    }
94    if ((engineWinCheck(true)) && (engineWin.top === engineWin.self) && (window.focus)) { engineWin.focus(); }
95    return engineWin;
96  } else if (warnedAboutUnsupportedVariation != gameVariant[currentGame]) {
97    warnedAboutUnsupportedVariation = gameVariant[currentGame];
98    myAlert("warning: analysis board unavailable for the " + gameVariant[currentGame] + " variant", true);
99  }
100  return null;
101}
102
103function engineWinCheck(skipSignature) {
104   return ((!pgn4web_engineWindowDisableAnalysisBoard) && (typeof(engineWin) == "object") && (engineWin !== null) && (!engineWin.closed) && (typeof(engineWin.engineSignature) != "undefined") && ((pgn4web_engineWinSignature === engineWin.engineSignature) || (skipSignature)));
105}
106
107function engineWinOnMove() {
108   if (engineWinCheck()) {
109      if ((engineWin.autoUpdate === true) && (CurrentFEN() != engineWinLastFen) && (engineWin.CurrentFEN() == engineWinLastFen)) {
110         showEngineAnalysisBoard();
111      }
112      engineWin.updateGameAnalysisFlag();
113   } else {
114      engineWinLastFen = "";
115   }
116}
117
118