From 8837f994d3908f59a88359c48ca9908457a8290f Mon Sep 17 00:00:00 2001 From: Ty Potter Date: Sat, 2 Sep 2017 22:12:48 -0600 Subject: [PATCH] add action strings to a const-like structure. Also add 'brewery-info' as data to be retrieved --- background.html | 1 + background.js | 16 +++++++++--- const.js | 7 ++++++ content_script.js | 62 ++++++++++++++++++++++++++++++++++++----------- manifest.json | 2 +- pdb-styles.css | 5 +++- 6 files changed, 73 insertions(+), 20 deletions(-) create mode 100644 const.js diff --git a/background.html b/background.html index 705fca3..aea47c9 100644 --- a/background.html +++ b/background.html @@ -27,6 +27,7 @@ * TODO(DEVELOPER): Make sure you are importing the latest version of the Firebase JS. * *************************************************************************************** --> + diff --git a/background.js b/background.js index 7862584..543bc20 100644 --- a/background.js +++ b/background.js @@ -37,17 +37,17 @@ window.onload = function() { */ function notifyOnAuth(tabId) { firebase.auth().onAuthStateChanged(function(user) { - chrome.tabs.sendMessage(tabId, {action: "AUTH_STATE", state: !!user}); + chrome.tabs.sendMessage(tabId, {action: MSG_ACTIONS.AUTH_STATE, state: !!user}); }); } chrome.runtime.onMessage.addListener(function(msg, sender, sendResponse) { console.log("Received %o from %o, frame", msg, sender.tab, sender.frameId); switch (msg.action) { - case "SUB_AUTH_STATE": + case MSG_ACTIONS.SUB_AUTH_STATE: notifyOnAuth(sender.tab.id); break; - case "GET": + case MSG_ACTIONS.GET: if (fbAuthenticated) { firebase.database().ref(msg.path).once('value', sendResponse); return true; // Indicates Async resolution @@ -55,7 +55,15 @@ chrome.runtime.onMessage.addListener(function(msg, sender, sendResponse) { sendResponse(null); } break; - case "showPageAction": + case MSG_ACTIONS.GET_BREWERY_DATA: + if (fbAuthenticated) { + firebase.database().ref("/brewery").orderByChild('batch_id').equalTo(msg.batchId).once('value', sendResponse); + return true; // Indicates Async resolution + } else { + sendResponse(null); + } + break; + case MSG_ACTIONS.SHOW_PAGE_ACTION: chrome.pageAction.show(sender.tab.id); break; } diff --git a/const.js b/const.js new file mode 100644 index 0000000..8f94b6b --- /dev/null +++ b/const.js @@ -0,0 +1,7 @@ +var MSG_ACTIONS = { + GET: "GET", + GET_BREWERY_DATA: "get_brewery_data", + SHOW_PAGE_ACTION: "show_page_action", + SUB_AUTH_STATE: "sub_auth_state", + AUTH_STATE: "auth_state" +} diff --git a/content_script.js b/content_script.js index b8d9cd8..c2623a9 100644 --- a/content_script.js +++ b/content_script.js @@ -46,7 +46,7 @@ var loadBatchPageOverlay = function(domRoot) { console.log("Batch ID: " + batchId); - chrome.runtime.sendMessage({action: "GET", path: '/batches/' + batchId}, function(response) { + chrome.runtime.sendMessage({action: MSG_ACTIONS.GET, path: '/batches/' + batchId}, function(response) { paintBatchPageOverlay(domRoot, response, batchId); }); @@ -64,6 +64,11 @@ var paintBatchPageOverlay = function(domRoot, batchData, batchId) { parent.appendChild(d); } + var addStat = function(table, label, data) { + var r = domRoot.createElement("tr"); + r.innerHTML = "" + label + "" + data + ""; + table.appendChild(r); + } var layoutTable = domRoot.querySelector("#batch_main_info_panel div.section_inner table"); var pdbCell = domRoot.createElement('td'); @@ -84,22 +89,51 @@ var paintBatchPageOverlay = function(domRoot, batchData, batchId) { pdbCell.appendChild(img); pdbCell.appendChild(txt); + if (!!(batchData.documents)) { - // Add links to documents/ - var docs = { - "Brewsheet": batchData.documents.brewsheet, - "Fermentation Log": batchData.documents["fermentation log"] - }; + // Add links to documents/ + var docs = { + "Brewsheet": batchData.documents.brewsheet, + "Fermentation Log": batchData.documents["fermentation log"] + }; - // There's a couple of hidden rows in the table to account for. - for (var doc in docs) { - if (!!(docs[doc])) { - addLink(pdbCell, doc, docs[doc]); + for (var doc in docs) { + if (!!(docs[doc])) { + addLink(pdbCell, doc, docs[doc]); + } } - } + } addLink(pdbCell, "Fermentation Graph", "http://prairiedogbrewing.ca:3000/dashboard/db/batch-status?orgId=1&from=now-7d&to=now&refresh=1m&var-batch_id=" + batchId); + var sgToPlato = function(sg) { + return (259-(259 / (sg) )).toFixed(2); + } + + var addBreweryData = function(data) { + if (data == null) return; + + readings = data[Object.keys(data)[0]].readings; + + stats = { + "Gravity" : sgToPlato(readings.gravity.value / 1000), + "Temperature (tilt)" : readings.tilt_temperature.value, + "Temperature (onewire)" : readings['1w_temperature'].value + } + + var table = domRoot.createElement("table"); + table.classList.add("pdb-stat-table"); + for (var stat in stats) { + addStat(table, stat, stats[stat]); + } + pdbCell.appendChild(table); + } + + chrome.runtime.sendMessage({action: MSG_ACTIONS.GET_BREWERY_DATA, batchId: batchId}, function(response) { + addBreweryData(response); + }); + + } var afterViewRecord = function(ele) { @@ -139,13 +173,13 @@ var teardown = function() { // Inform the background page that this tab should have a page-action. chrome.runtime.sendMessage({ - action: 'showPageAction' + action: MSG_ACTIONS.SHOW_PAGE_ACTION }); // Listen for changes to firebase authentication. chrome.runtime.onMessage.addListener( function(request, sender, sendResponse) { - if (request.action == "AUTH_STATE") { + if (request.action == MSG_ACTIONS.AUTH_STATE) { if (request.state) { integrate(); } else { @@ -156,4 +190,4 @@ chrome.runtime.onMessage.addListener( // Subscribe to the auth state change. The AUTH_STATE message will be sent // immediately with the current state, and sent again as the state changes. -chrome.runtime.sendMessage({action: "SUB_AUTH_STATE"}); +chrome.runtime.sendMessage({action: MSG_ACTIONS.SUB_AUTH_STATE}); diff --git a/manifest.json b/manifest.json index 1b00ba0..77ec79d 100644 --- a/manifest.json +++ b/manifest.json @@ -18,7 +18,7 @@ "content_scripts": [ { "matches": ["https://na2.goekos.com/*"], - "js":["jquery.min.js", "content_inject.js", "content_script.js"], + "js":["jquery.min.js", "const.js", "content_inject.js", "content_script.js"], "run_at": "document_end", /*"all_frames": true,*/ "css": ["pdb-styles.css"] diff --git a/pdb-styles.css b/pdb-styles.css index b2a413a..e80a615 100644 --- a/pdb-styles.css +++ b/pdb-styles.css @@ -28,6 +28,7 @@ h4 { color: #5A4638; font-family: After Disaster; font-size: 1.5em; + margin-bottom: 32px; text-align: center; } .pdb-logo { @@ -42,6 +43,8 @@ div.pdb-column-link { } .pdb-column { - width: 360px; + font-face: Roboto Condensed Bold; + color: #5A4638; padding: 8px; + width: 240px; }