| 1 |
/* |
| 2 |
* Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 |
* contributor license agreements. See the NOTICE file distributed with |
| 4 |
* this work for additional information regarding copyright ownership. |
| 5 |
* The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 |
* (the "License"); you may not use this file except in compliance with |
| 7 |
* the License. You may obtain a copy of the License at |
| 8 |
* |
| 9 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
* |
| 11 |
* Unless required by applicable law or agreed to in writing, software |
| 12 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
* See the License for the specific language governing permissions and |
| 15 |
* limitations under the License. |
| 16 |
*/ |
| 17 |
var svgns = "http://www.w3.org/2000/svg"; |
| 18 |
var xlinkns = "http://www.w3.org/1999/xlink"; |
| 19 |
|
| 20 |
function Board(boardGroup, moveGroup, winG, helpG) { |
| 21 |
this.doc = boardGroup.getOwnerDocument(); |
| 22 |
this.root = this.doc.getRootElement(); |
| 23 |
this.helpG = (helpG)?helpG:this.doc.getElementById("help"); |
| 24 |
this.winG = (winG)?winG:this.doc.getElementById("win"); |
| 25 |
this.boardGroup = boardGroup; |
| 26 |
this.moveGroup = moveGroup; |
| 27 |
this.thePiles = new Array(); |
| 28 |
this.moves = new Array(); |
| 29 |
this.moveIndex = 0; |
| 30 |
this.numMoves = 0; |
| 31 |
this.isWon = false; |
| 32 |
|
| 33 |
this.root.addEventListener("keypress", this, false); |
| 34 |
|
| 35 |
if (helpG) { |
| 36 |
var board = this; |
| 37 |
this.helpG.addEventListener("click", function() { board.hideHelp(); }, false); |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
Board.prototype.won = function() { |
| 42 |
if (!this.winG) return; |
| 43 |
var style = this.winG.style; |
| 44 |
if (style.getPropertyValue("display") == "inline") { |
| 45 |
this.unwon(); |
| 46 |
return; |
| 47 |
} |
| 48 |
style.setProperty("display", "inline", ""); |
| 49 |
this.isWon = true; |
| 50 |
}; |
| 51 |
|
| 52 |
Board.prototype.unwon = function() { |
| 53 |
if (!this.winG) return; |
| 54 |
if (this.winG.style.getPropertyValue("display") != "none") { |
| 55 |
this.winG.style.setProperty("display", "none", ""); |
| 56 |
} |
| 57 |
}; |
| 58 |
|
| 59 |
Board.prototype.setNotifyMoveDone = function(moveDone) { |
| 60 |
this.moveDone = moveDone; |
| 61 |
}; |
| 62 |
|
| 63 |
Board.prototype.notifyMoveDone = function() { |
| 64 |
if (this.moveDone) { |
| 65 |
this.moveDone(); |
| 66 |
} |
| 67 |
}; |
| 68 |
|
| 69 |
Board.prototype.saveMove = function(moveinfo) { |
| 70 |
this.moves[this.moveIndex++] = moveinfo; |
| 71 |
this.numMoves = this.moveIndex; |
| 72 |
this.isWon = false; |
| 73 |
}; |
| 74 |
|
| 75 |
Board.prototype.setMoveInfo = function(moveinfo) { |
| 76 |
if (this.moveIndex == 0) return null; |
| 77 |
this.moves[this.moveIndex-1] = moveinfo; |
| 78 |
}; |
| 79 |
|
| 80 |
Board.prototype.getMoveInfo = function() { |
| 81 |
if (this.moveIndex == 0) return null; |
| 82 |
return this.moves[this.moveIndex-1]; |
| 83 |
}; |
| 84 |
|
| 85 |
Board.prototype.undoMove = function() { |
| 86 |
if (this.moveIndex == 0) return; |
| 87 |
if (this.isWon) this.unwon(); |
| 88 |
|
| 89 |
this.moveIndex--; |
| 90 |
var mi = this.moves[this.moveIndex]; |
| 91 |
mi.undo(); |
| 92 |
}; |
| 93 |
|
| 94 |
Board.prototype.redoMove = function() { |
| 95 |
if (this.moveIndex == this.numMoves) return; |
| 96 |
|
| 97 |
var mi = this.moves[this.moveIndex++]; |
| 98 |
mi.redo(); |
| 99 |
|
| 100 |
if (this.isWon && (this.moveIndex == this.numMoves)) |
| 101 |
this.won(); |
| 102 |
}; |
| 103 |
|
| 104 |
Board.prototype.handleEvent = function(evt) { |
| 105 |
var keycode = evt.keyCode ? evt.keyCode : |
| 106 |
evt.charCode ? evt.charCode : |
| 107 |
evt.which ? evt.which : void 0; |
| 108 |
var key = String.fromCharCode(keycode); |
| 109 |
if ((key == "z") || (key == "Z")) { |
| 110 |
this.undoMove(); |
| 111 |
} else if ((key == "r") || (key == "R")) { |
| 112 |
this.redoMove(); |
| 113 |
} else if (key == "?") { |
| 114 |
this.showHelp(); |
| 115 |
} |
| 116 |
}; |
| 117 |
|
| 118 |
Board.prototype.showHelp = function() { |
| 119 |
if (!this.helpG) return; |
| 120 |
var style = this.helpG.style; |
| 121 |
if (style.getPropertyValue("display") == "inline") { |
| 122 |
this.hideHelp(); |
| 123 |
return; |
| 124 |
} |
| 125 |
style.setProperty("display", "inline", ""); |
| 126 |
style.setProperty("pointer-events", "fill", ""); |
| 127 |
var board = this; |
| 128 |
} |
| 129 |
|
| 130 |
Board.prototype.hideHelp = function() { |
| 131 |
if (!this.helpG) return; |
| 132 |
this.helpG.style.setProperty("pointer-events", "none", ""); |
| 133 |
this.helpG.style.setProperty("display", "none", ""); |
| 134 |
} |
| 135 |
|
| 136 |
function MultiMoveInfo(mi1, mi2) { |
| 137 |
this.mi1 = mi1; |
| 138 |
this.mi2 = mi2; |
| 139 |
this.undo = function() { |
| 140 |
this.mi2.undo(); |
| 141 |
this.mi1.undo(); |
| 142 |
}; |
| 143 |
this.redo = function() { |
| 144 |
this.mi1.redo(); |
| 145 |
this.mi2.redo(); |
| 146 |
}; |
| 147 |
} |
| 148 |
|
| 149 |
function SimpleMoveInfo(fromCards, fromPile, toCards, toPile) { |
| 150 |
this.fromCards = fromCards; |
| 151 |
this.fromPile = fromPile; |
| 152 |
this.toCards = toCards; |
| 153 |
this.toPile = toPile; |
| 154 |
|
| 155 |
this.undo = function() { |
| 156 |
var len = fromCards.length; |
| 157 |
for (var i=0; i<len; i++) { |
| 158 |
fromPile.moveCardTo(fromCards[i], 40, 1); |
| 159 |
} |
| 160 |
}; |
| 161 |
this.redo = function() { |
| 162 |
var len = toCards.length; |
| 163 |
for (var i=0; i<len; i++) { |
| 164 |
toPile.moveCardTo(toCards[i], 40, 1); |
| 165 |
} |
| 166 |
}; |
| 167 |
} |