From c34f01458ed993fc9bd0c726dd84b3bfadb7696a Mon Sep 17 00:00:00 2001 From: Ian Gulliver Date: Sun, 27 Dec 2015 20:48:35 -0800 Subject: [PATCH] Store child name in hogfather chat. --- static/babystats.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/static/babystats.js b/static/babystats.js index 9445097..faba655 100644 --- a/static/babystats.js +++ b/static/babystats.js @@ -50,6 +50,22 @@ BabyStats.prototype.onChatReady_ = function(chat) { this.gridWidthCells_ = grid.gridWidthCells; this.gridHeightCells_ = grid.gridHeightCells; this.buildGrid_(); + + var messages = this.chat_.getMessages(); + messages.forEach(this.handleMessage_.bind(this)); +}; + + +BabyStats.prototype.handleMessage_ = function(message) { + switch (message.type) { + case 'child_name_change': + this.childName_.value = message.child_name; + this.checkOverlay_(); + break; + default: + console.log('Unknown message type:', message); + break; + } }; @@ -347,6 +363,7 @@ BabyStats.prototype.buildLayout_ = function() { this.addCSSClass_(this.childName_, 'babyStatsChildName'); this.childName_.placeholder = 'Child name'; this.childName_.addEventListener('input', this.checkOverlay_.bind(this)); + this.childName_.addEventListener('input', this.onChildNameChange_.bind(this)); this.container_.appendChild(this.childName_); this.childName_.focus(); @@ -391,6 +408,14 @@ BabyStats.prototype.checkOverlay_ = function() { }; +BabyStats.prototype.onChildNameChange_ = function() { + this.chat_.sendMessage({ + type: 'child_name_change', + child_name: this.childName_.value, + }); +}; + + /** * Store your name value locally. * @private