Skip to content

Commit

Permalink
manually apply gabrielecirulli#81
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielecirulli committed Mar 23, 2014
1 parent 2f91247 commit cf01ca7
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 3 deletions.
2 changes: 2 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ <h1 class="title">2048</h1>
</p>
</div>

<script src="js/bind_polyfill.js"></script>
<script src="js/classlist_polyfill.js"></script>
<script src="js/animframe_polyfill.js"></script>
<script src="js/keyboard_input_manager.js"></script>
<script src="js/html_actuator.js"></script>
Expand Down
9 changes: 9 additions & 0 deletions js/bind_polyfill.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Function.prototype.bind = Function.prototype.bind || function (target) {
var self = this;
return function (args) {
if (!(args instanceof Array)) {
args = [args];
}
self.apply(target, args);
};
};
71 changes: 71 additions & 0 deletions js/classlist_polyfill.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
(function () {
if (typeof window.Element === "undefined" ||
"classList" in document.documentElement) {
return;
}

var prototype = Array.prototype,
push = prototype.push,
splice = prototype.splice,
join = prototype.join;

function DOMTokenList(el) {
this.el = el;
// The className needs to be trimmed and split on whitespace
// to retrieve a list of classes.
var classes = el.className.replace(/^\s+|\s+$/g, '').split(/\s+/);
for (var i = 0; i < classes.length; i++) {
push.call(this, classes[i]);
}
}

DOMTokenList.prototype = {
add: function (token) {
if (this.contains(token)) return;
push.call(this, token);
this.el.className = this.toString();
},
contains: function (token) {
return this.el.className.indexOf(token) != -1;
},
item: function (index) {
return this[index] || null;
},
remove: function (token) {
if (!this.contains(token)) return;
for (var i = 0; i < this.length; i++) {
if (this[i] == token) break;
}
splice.call(this, i, 1);
this.el.className = this.toString();
},
toString: function () {
return join.call(this, ' ');
},
toggle: function (token) {
if (!this.contains(token)) {
this.add(token);
} else {
this.remove(token);
}

return this.contains(token);
}
};

window.DOMTokenList = DOMTokenList;

function defineElementGetter(obj, prop, getter) {
if (Object.defineProperty) {
Object.defineProperty(obj, prop, {
get: getter
});
} else {
obj.__defineGetter__(prop, getter);
}
}

defineElementGetter(HTMLElement.prototype, 'classList', function () {
return new DOMTokenList(this);
});
})();
4 changes: 2 additions & 2 deletions js/game_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ function GameManager(size, InputManager, Actuator, StorageManager) {
// Restart the game
GameManager.prototype.restart = function () {
this.storageManager.clearGameState();
this.actuator.continue(); // Clear the game won/lost message
this.actuator.continueGame(); // Clear the game won/lost message
this.setup();
};

// Keep playing after winning (allows going over 2048)
GameManager.prototype.keepPlaying = function () {
this.keepPlaying = true;
this.actuator.continue(); // Clear the game won/lost message
this.actuator.continueGame(); // Clear the game won/lost message
};

// Return true if the game is lost, or has won and the user hasn't kept playing
Expand Down
2 changes: 1 addition & 1 deletion js/html_actuator.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ HTMLActuator.prototype.actuate = function (grid, metadata) {
};

// Continues the game (both restart and keep playing)
HTMLActuator.prototype.continue = function () {
HTMLActuator.prototype.continueGame = function () {
this.clearMessage();
};

Expand Down

0 comments on commit cf01ca7

Please sign in to comment.