Clean up tile object passing. Add support for tile implications, e.g. feeding implies awake.

This commit is contained in:
Ian Gulliver
2015-12-28 20:35:06 -08:00
parent 35b9de8932
commit 0656aff2b7

View File

@@ -20,13 +20,46 @@ var BabyStats = function(container) {
this.tileScaleWidth_ = 1; this.tileScaleWidth_ = 1;
this.tiles_ = [ this.tiles_ = [
['asleep', 'Asleep'], {
['awake', 'Awake'], type: 'asleep',
['diaper_feces', 'Diaper change\n(feces)'], description: 'Asleep',
['diaper_urine', 'Diaper change\n(urine only)'], cancels: ['awake'],
['feeding_breast', 'Feeding\n(breast)'], },
['feeding_bottle_milk', 'Feeding\n(bottled breast milk)'], {
['feeding_formula', 'Feeding\n(formula)'], type: 'awake',
description: 'Awake',
cancels: ['asleep'],
},
{
type: 'diaper_feces',
description: 'Diaper change\n(feces)',
implies: ['awake'],
timeout: 60 * 30,
},
{
type: 'diaper_urine',
description: 'Diaper change\n(urine only)',
implies: ['awake'],
timeout: 60 * 30,
},
{
type: 'feeding_breast',
description: 'Feeding\n(breast)',
implies: ['awake'],
timeout: 60 * 30,
},
{
type: 'feeding_bottle_milk',
description: 'Feeding\n(bottled breast milk)',
implies: ['awake'],
timeout: 60 * 30,
},
{
type: 'feeding_formula',
description: 'Feeding\n(formula)',
implies: ['awake'],
timeout: 60 * 30,
},
]; ];
this.intervals_ = {}; this.intervals_ = {};
@@ -241,46 +274,50 @@ BabyStats.prototype.buildStylesheet_ = function() {
*/ */
BabyStats.prototype.buildCells_ = function() { BabyStats.prototype.buildCells_ = function() {
this.cells_ = []; this.cells_ = [];
this.tiles_.forEach(function(tiles) { this.tiles_.forEach(function(tile) {
var cell = document.createElement('babyStatsCell'); var cell = document.createElement('babyStatsCell');
this.cells_.push(cell); this.cells_.push(cell);
var contents = document.createElement('babyStatsCellContents'); var contents = document.createElement('babyStatsCellContents');
contents.textContent = tiles[1]; contents.textContent = tile.description;
cell.appendChild(contents); cell.appendChild(contents);
var overlay = document.createElement('babyStatsCellOverlay'); var overlay = document.createElement('babyStatsCellOverlay');
cell.appendChild(overlay); cell.appendChild(overlay);
cell.addEventListener('click', this.onClick_.bind(this, tiles[0], overlay)); cell.addEventListener('click', this.onClick_.bind(this, tile, overlay));
}, this); }, this);
}; };
/** /**
* Handle a click event on a button. * Handle a click event on a button.
* @param {string} eventName short name of event to send * @param {Object} tile tile description struct
* @param {Element} overlay element to make visible with countdown timer * @param {Element} overlay element to make visible with countdown timer
* @private * @private
*/ */
BabyStats.prototype.onClick_ = function(eventName, overlay) { BabyStats.prototype.onClick_ = function(tile, overlay) {
if (this.intervals_[eventName]) { if (this.intervals_[tile.type]) {
window.clearInterval(this.intervals_[eventName]); window.clearInterval(this.intervals_[tile.type]);
delete this.intervals_[eventName]; delete this.intervals_[tile.type];
overlay.style.opacity = 0.0; overlay.style.opacity = 0.0;
return; return;
} }
var timer = 5; var timer = 5;
overlay.textContent = timer; overlay.textContent = timer;
overlay.style.opacity = 0.5; overlay.style.opacity = 0.5;
this.intervals_[eventName] = window.setInterval(function() { this.intervals_[tile.type] = window.setInterval(function() {
timer--; timer--;
switch (timer) { switch (timer) {
case 0: case 0:
var types = tile.implies || [];
types.push(tile.type);
types.forEach(function(type) {
this.chat_.sendMessage({ this.chat_.sendMessage({
type: eventName, type: type,
sender_name: this.yourName_.value, sender_name: this.yourName_.value,
}); });
}.bind(this));
overlay.textContent = '✓'; overlay.textContent = '✓';
break; break;
@@ -288,8 +325,8 @@ BabyStats.prototype.onClick_ = function(eventName, overlay) {
break; break;
case -2: case -2:
window.clearInterval(this.intervals_[eventName]); window.clearInterval(this.intervals_[tile.type]);
delete this.intervals_[eventName]; delete this.intervals_[tile.type];
overlay.style.opacity = 0.0; overlay.style.opacity = 0.0;
break; break;