format_level using regexp replace (a bit over-the-top maybe)

This commit is contained in:
Gwenhael Le Moine 2011-07-05 10:14:15 +02:00
parent 59108f4491
commit c0d82707cc

View file

@ -96,15 +96,36 @@ function won_or_not( state ) {
} }
function format_level( state ) { function format_level( state ) {
var dl = ""; function Replacer( conversionObject ) {
for each ( c in state.board ) {
var classes = [ "starcell ", css_classes[ c ] ].join( '' ); var regexpStr = '';
if ( state.moving == c) { for ( var k in conversionObject ) {
classes = [ classes, " selected " ].join( '' ); regexpStr += ( regexpStr.length ? '|' : '' ) + k;
} }
dl = [ dl, "<span class=\"", classes, "\">", c, "</span>"].join( '' ); var regexpr = new RegExp( regexpStr, 'mig' ); // g: global, m:multi-line i: ignore case
return function(s) {
return s.replace( regexpr, function(str, p1, p2, offset, s) {
var a = conversionObject[ str ];
return a == undefined ? str : a;
} );
};
} }
return dl;
var substitutions = {
'#': '<span class="starcell wall">#</span>',
'x': '<span class="starcell gift">x</span>',
' ': '<span class="starcell void"> </span>'
};
if ( state.moving == cell.BALL ) {
substitutions[ '@' ] = '<span class="starcell ball selected">@</span>';
substitutions[ 'H' ] = '<span class="starcell cube">H</span>';
}
else {
substitutions[ '@' ] = '<span class="starcell ball">@</span>';
substitutions[ 'H' ] = '<span class="starcell cube selected">H</span>';
}
var myReplacer = Replacer( substitutions );
return myReplacer( state.board );
} }
function load_level( levelset, nb ) { function load_level( levelset, nb ) {