מדיה ויקי:Gadget-pgnviewer.js: הבדלים בין גרסאות בדף

default start for first position when there's fen
support chess960
שורה 33: שורה 33:


// some global, utility functions.
// some global, utility functions.
function bindex(file, row) { return 8 * file + row; }
    function bindex(file, row) { return 8 * file + row; }
function file(ind) { return Math.floor(ind / 8);}
function file(ind) { return Math.floor(ind / 8);}
function row(ind) { return ind % 8; }
function row(ind) { return ind % 8; }
שורה 177: שורה 177:
clearTimeout( this.timer );
clearTimeout( this.timer );
if ( this.autoPlayButton.state ) {
if ( this.autoPlayButton.state ) {
var gs = this;
                    if ( this.currentGame.done() )
this.timer = setTimeout( function() { gs.autoPlayCallback(); }, this.autoPlayRate );
                        this.toggleAutoPlay();
                    else {
                        var gs = this;
                        this.timer = setTimeout( function() { gs.autoPlayCallback(); }, this.autoPlayRate );
                    }
}
}
},
},
שורה 236: שורה 240:
   
   
ChessPiece.prototype.move = function(file, row) {
ChessPiece.prototype.move = function(file, row) {
this.game.clearSquare(this.file, this.row);
        if ( this.game.pieceAt( this.file, this.row ) == this ) // with chess960 castling, we sometimes have to test.
  this.game.clearSquare(this.file, this.row);
this.game.pieceAt(file, row, this); // place it on the board)
this.game.pieceAt(file, row, this); // place it on the board)
this.game.registerMove({what:'m', piece: this, file: file, row: row});
this.game.registerMove({what:'m', piece: this, file: file, row: row});
שורה 358: שורה 363:
}
}
   
   
Game.prototype.copyBoard = function() { return this.board.slice(); }
    Game.prototype.done = function() { return this.moves.length - 1 <= this.index; }
   
   
Game.prototype.pieceAt = function(file, row, piece) {
Game.prototype.pieceAt = function(file, row, piece) {
שורה 432: שורה 437:
this.advance(delta);
this.advance(delta);
}
}
if (this.index == this.moves.length - 1)
this.gs.toggleAutoPlay( false );
}
}
   
   
שורה 474: שורה 477:
this.gotoBoard(0);
this.gotoBoard(0);
}
}
 
Game.prototype.kingSideCastle = function(color) {
    Game.prototype.castle = function( color, side, kingTargetFile, rookTargetFile ) {
var king = this.piecesByTypeCol['k'][color][0];
var king = this.piecesByTypeCol['k'][color][0],
var rook = this.pieceAt(7, (color == WHITE ? 0 : 7));
            rook = this.piecesByTypeCol['r'][color][side];
if (!rook || rook.type != 'r')
if (!rook || rook.type != 'r')
throw 'attempt to castle without rook on appropriate square';
throw 'attempt to castle without rook on appropriate square';
king.move(fileOfStr('g'), king.row);
king.move(fileOfStr(kingTargetFile), king.row);
rook.move(fileOfStr('f'), rook.row);
rook.move(fileOfStr(rookTargetFile), rook.row);
       
    }
   
Game.prototype.kingSideCastle = function(color) {
        this.castle( color, 1, 'g', 'f' );
}
}
   
   
Game.prototype.queenSideCastle = function(color) {
Game.prototype.queenSideCastle = function(color) {
var king = this.piecesByTypeCol['k'][color][0];
        this.castle( color, 0, 'c', 'd' );
var rook = this.pieceAt(0, (color == WHITE ? 0 : 7));
if (!rook || rook.type != 'r')
throw 'attempt to castle without rook on appropriate square';
king.move(fileOfStr('c'), king.row);
rook.move(fileOfStr('d'), rook.row);
}
}
   
   
שורה 594: שורה 597:


Game.prototype.preAnalyzePgn = function(pgn) {
Game.prototype.preAnalyzePgn = function(pgn) {
var
match,
turn,
indexOfMove = {},
moveNum = '';
function removeHead(match) {
var ind = pgn.indexOf(match) + match.length;
pgn = pgn.substring(ind);
return match;
}
function tryMatch(regex) {
function tryMatch(regex) {
var rmatch = pgn.match(regex);
var match = pgn.match(regex);
if (rmatch) {
            if (match)
removeHead(rmatch[0]);
                pgn = pgn.replace( match, '' );
moveNum = rmatch[1] || moveNum;
return match && match[0];
}
return rmatch && rmatch[0];
}
}
       
while (match = tryMatch(/^\s*\[[^\]]*\]/))
        var match;
while (match = tryMatch(/^\s*\[[^\]]*\]/))
this.addDescription(match);
this.addDescription(match);
this.pgn = pgn;  
this.pgn = pgn;  
}
}
שורה 645: שורה 634:
return rmatch && rmatch[0];
return rmatch && rmatch[0];
}
}
       
pgn = pgn.replace(/;(.*)\n/g, ' {$1} ').replace(/\s+/g, ' '); // replace to-end-of-line comments with block comments, remove newlines and noramlize spaces to 1
pgn = pgn.replace(/;(.*)\n/g, ' {$1} ').replace(/\s+/g, ' '); // replace to-end-of-line comments with block comments, remove newlines and noramlize spaces to 1
this.populateBoard(this.descriptions.FEN || 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR');
this.populateBoard( this.descriptions.FEN || 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR' );
var prevLen = -1;
var prevLen = -1;
this.addMoveLink();
this.addMoveLink();
שורה 668: שורה 657:
}
}
}
}
this.index = indexOfMove[this.descriptions.FirstMove || 'nofirstnmove'] ||  
var showFirst = this.descriptions['FirstMove'];
             ( this.descriptions.FEN ? 0 : this.moves.length - 1 );
this.index = (showFirst && indexOfMove[showFirst]) ||  
             (this.descriptions.FEN  
                ? 0
                : this.moves.length - 1
            );
}
}
   
   
שורה 680: שורה 664:
var fenar = fen.split(/[\/\s]/);
var fenar = fen.split(/[\/\s]/);
if (fenar.length < 8)
if (fenar.length < 8)
throw 'illegal fen: "' + fen + '"';
            throw 'illegal fen: "' + fen + '"';
for (var row = 0; row < 8; row++) {
for (var row = 0; row < 8; row++) {
var file = 0;
var file = 0;
שורה 864: שורה 848:
function populateImages() {
function populateImages() {
var
var
colors = [WHITE, BLACK],
types = ['p', 'r', 'n', 'b', 'q', 'k'],
flist = [];
flist = [];


שורה 880: שורה 862:
};
};


for (var c in colors)
$( [WHITE, BLACK] ).each( function( _, c ) {
for (var t in types)
$( ['p', 'r', 'n', 'b', 'q', 'k'] ).each( function( _, t ) {
images[types[t] + colors[c]] = { fn: 'Chess ' + types[t] + colors[c] + 't45.svg'};
                var tc = t + c;
images[tc] = { fn: 'Chess ' + tc + 't45.svg' };
            } );
        } );
       
for ( var i in images ) flist.push( 'File:' + images[i].fn );  
for ( var i in images ) flist.push( 'File:' + images[i].fn );  
var params = { titles: flist.join( '|' ), prop: 'imageinfo', iiprop: 'url' };
var params = { titles: flist.join( '|' ), prop: 'imageinfo', iiprop: 'url' };
if ( brainDamage )
if ( brainDamage )
params.iiurlwidth = 48;
params.iiurlwidth = 48;


new mw.Api().get(params)
new mw.Api().get(params)
.done( function( data ) {
.done( function( data ) {
if (data && data.query)  
if (data && data.query)  
$.each(data.query.pages, function(index, page) {
$.each(data.query.pages, function(_, page) {
var
var
ia = page.imageinfo[0],
ia = page.imageinfo[0],
title = page.title,
title = page.title,
url = ( brainDamage && /\.svg$/.test( title ) ) ? ia.thumburl : ia.url;
url = ( brainDamage && /\.svg$/.test( title ) ) ? ia.thumburl : ia.url;
for ( var i in images )
                        for ( key in images )  
if ( title.indexOf( images[i].fn ) + 1 ) // not strictly equal -
                            if ( ~ title.indexOf( images[key].fn ) )  
images[i].url = url;
                                images[key].url = url;
} );
} );
doIt();
doIt();