Skip to content

Commit

Permalink
add action strings to a const-like structure. Also add 'brewery-info'…
Browse files Browse the repository at this point in the history
… as data to be retrieved
  • Loading branch information
typotter committed Sep 3, 2017
1 parent 1c83fbb commit 8837f99
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 20 deletions.
1 change: 1 addition & 0 deletions background.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
* TODO(DEVELOPER): Make sure you are importing the latest version of the Firebase JS. *
*************************************************************************************** -->
<script src="https://www.gstatic.com/firebasejs/4.0.0/firebase.js"></script>
<script src="const.js"></script>
<script src="background.js"></script>
</head>
<body>
Expand Down
16 changes: 12 additions & 4 deletions background.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,25 +37,33 @@ 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
} else {
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;
}
Expand Down
7 changes: 7 additions & 0 deletions const.js
Original file line number Diff line number Diff line change
@@ -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"
}
62 changes: 48 additions & 14 deletions content_script.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

Expand All @@ -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 = "<td>" + label + "</td><td>" + data + "</td>";
table.appendChild(r);
}

var layoutTable = domRoot.querySelector("#batch_main_info_panel div.section_inner table");
var pdbCell = domRoot.createElement('td');
Expand All @@ -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) {
Expand Down Expand Up @@ -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 {
Expand All @@ -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});
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
5 changes: 4 additions & 1 deletion pdb-styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ h4 {
color: #5A4638;
font-family: After Disaster;
font-size: 1.5em;
margin-bottom: 32px;
text-align: center;
}
.pdb-logo {
Expand All @@ -42,6 +43,8 @@ div.pdb-column-link {
}

.pdb-column {
width: 360px;
font-face: Roboto Condensed Bold;
color: #5A4638;
padding: 8px;
width: 240px;
}

0 comments on commit 8837f99

Please sign in to comment.