Skip to content

Commit

Permalink
Merge pull request #15 from dutts/https
Browse files Browse the repository at this point in the history
Rewrote plugin code to perform food.gov.uk request from addon script
  • Loading branch information
dutts committed Apr 6, 2015
2 parents 6c61aea + c940916 commit 7154ee8
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 75 deletions.
135 changes: 65 additions & 70 deletions firefox/addon/data/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
function AppendImg(element, filename) {
var img = document.createElement('img');
img.src = self.options.prefixDataURI + filename;
element.appendChild(img);
element.append(img);
}

function ApplyFilter(ratingFilterRange, restaurantEntries, excludeNoData) {
Expand Down Expand Up @@ -87,6 +87,60 @@ excludeNoDataLabel.appendChild(excludeNoDataCheckbox);

$("div.restaurants").prepend(config);

// Set up the listener for the result returned from the addon script
self.port.on("restaurantScore", function(restaurantScore) {
//console.log("id " + restaurantScore.id + ", rating " + restaurantScore.rating);
// find the score placeholder for the restaurant we've got a result for
var restaurantScorePlaceholder = $("div.restaurant[data-nomorvom-id='"+restaurantScore.id+"'] div#nomorvom");
restaurantScorePlaceholder.attr("data-rating", restaurantScore.rating);
$("p#nomorvom_loading", restaurantScorePlaceholder).remove();
$("div#nomorvom_progressbar", restaurantScorePlaceholder).remove();

if (restaurantScore.rating > 0) {
for (var i = 0; i < restaurantScore.rating; i++) {
AppendImg(restaurantScorePlaceholder, '48-fork-and-knife-icon.png');
}
for (var i = 0; i < 5 - restaurantScore.rating; i++) {
AppendImg(restaurantScorePlaceholder, 'toilet-paper-icon_32.png');
}
}

var resultText = document.createElement('div');
resultText.id = "hygieneScore"
resultText.style.fontWeight = "bold";
resultText.style.margin = "0px 5px";

if (restaurantScore.rating == "AwaitingInspection") {
$(resultText).text("This takeaway is awaiting inspection");
restaurantScore.rating = 0;
}
else {
if (restaurantScore.rating == -1) {
$(resultText).text("Sorry, no food hygiene data found");
}
else {
$(resultText).text("Hygiene Score : " + restaurantScore.rating + "/5");
}
}
restaurantScorePlaceholder.append(resultText);

// Filter accordingly
var ratingFilterRange = $(scoreFilterSlider).slider("values");
//var excludeNoData = $(excludeNoDataCheckbox).prop('checked');
//if ( ((rating == -1) && excludeNoData) || (rating < ratingFilterRange[0]) || (rating > ratingFilterRange[1]) ) {
if ((restaurantScore.rating < ratingFilterRange[0]) || (restaurantScore.rating > ratingFilterRange[1])) {
$("div.restaurant[data-nomorvom-id='"+restaurantScore.id+"']").hide();
}
else
{
$("div.restaurant[data-nomorvom-id='"+restaurantScore.id+"']").show();
}

});


var restaurantId = 0;

restaurantEntries.each(function () {
var _this = $(this);
var name = $("h2.name a:first", this).text().trim();
Expand All @@ -97,22 +151,25 @@ restaurantEntries.each(function () {
.end()
.text().trim();

var url = "http://api.ratings.food.gov.uk/Establishments?name=" + encodeURIComponent(name) + "&address=" + encodeURIComponent(address);
self.port.emit("queryRestaurant", {id:restaurantId, name:name, address:address});

// var url = "http://api.ratings.food.gov.uk/Establishments?name=" + encodeURIComponent(name) + "&address=" + encodeURIComponent(address);

var scorePlaceholder = document.createElement('div');
scorePlaceholder.id = "nomorvom"
scorePlaceholder.id = "nomorvom";
scorePlaceholder.style.border = "thin dashed red";
scorePlaceholder.style.padding = "5px";
scorePlaceholder.style.margin = "5px";
scorePlaceholder.width = "50%";

var loadingText = document.createElement('p');
loadingText.id = "nomorvom_loading";
loadingText.style.fontWeight = "bold";
loadingText.style.padding = "0px 5px";
$(loadingText).text("Loading food scores...");

var loaderImg = document.createElement('div');
loaderImg.id = "progressbar";
loaderImg.id = "nomorvom_progressbar";
$(loaderImg).progressbar({
value: false
});
Expand All @@ -121,72 +178,10 @@ restaurantEntries.each(function () {
scorePlaceholder.appendChild(loaderImg);

$(scorePlaceholder).attr("data-rating", 0);


_this.attr("data-nomorvom-id", restaurantId);

_this.append(scorePlaceholder);

var rating = 0;

$.ajax({
url: url,
type: 'GET',
dataType: 'json',
cache: false,
success: function (data, status) {
if (data.establishments.length > 0) {
scorePlaceholder.removeChild(loadingText);
scorePlaceholder.removeChild(loaderImg);
rating = data.establishments[0].RatingValue;
for (var i = 0; i < rating; i++) {
AppendImg(scorePlaceholder, '48-fork-and-knife-icon.png');
}
for (var i = 0; i < 5 - rating; i++) {
AppendImg(scorePlaceholder, 'toilet-paper-icon_32.png');
}
var resultText = document.createElement('div');
resultText.id = "hygieneScore"
resultText.style.fontWeight = "bold";
resultText.style.margin = "0px 5px";


if (rating == "AwaitingInspection") {
$(resultText).text("This takeaway is awaiting inspection");
rating = 0;
}
else {
$(resultText).text("Hygiene Score : " + rating + "/5");
}
scorePlaceholder.appendChild(resultText);

$(scorePlaceholder).attr("data-rating", rating);
}
else
{
scorePlaceholder.removeChild(loadingText);
scorePlaceholder.removeChild(loaderImg);

var resultText = document.createElement('div');
resultText.id = "hygieneScore";
resultText.style.fontWeight = "bold";
resultText.style.margin = "5px 5px";
$(resultText).text("Sorry, no food hygiene data found");

scorePlaceholder.appendChild(resultText);

$(scorePlaceholder).attr("data-rating", rating);
}

var ratingFilterRange = $(scoreFilterSlider).slider("values");
//var excludeNoData = $(excludeNoDataCheckbox).prop('checked');
//if ( ((rating == -1) && excludeNoData) || (rating < ratingFilterRange[0]) || (rating > ratingFilterRange[1]) ) {
if ((rating < ratingFilterRange[0]) || (rating > ratingFilterRange[1])) {
_this.hide();
}
else
{
_this.show();
}
},
error: function (error) { },
beforeSend: function (xhr) { xhr.setRequestHeader('x-api-version', 2); }
});
restaurantId++;
});
36 changes: 31 additions & 5 deletions firefox/addon/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,36 @@ var pageMod = require("sdk/page-mod");
var self = require("sdk/self");

pageMod.PageMod({
include: ["http://www.just-eat.co.uk/area/*", "http://just-eat.co.uk/area/*"],
//include: "http://localhost/*",
contentStyleFile: [self.data.url("jquery-ui-1.11.4/jquery-ui.min.css"), self.data.url("nomorvom.css")],
include: ["http://www.just-eat.co.uk/area/*", "http://just-eat.co.uk/area/*", "https://www.just-eat.co.uk/area/*", "https://just-eat.co.uk/area/*"],
contentStyleFile: [self.data.url("jquery-ui-1.11.4/jquery-ui.min.css"), self.data.url("nomorvom.css")],
contentScriptOptions: {prefixDataURI: self.data.url("")},
contentScriptFile: [self.data.url("jquery-2.1.3/jquery-2.1.3.min.js"), self.data.url("jquery-ui-1.11.4/jquery-ui.min.js"), self.data.url("api.js")],
contentScriptWhen: "ready"
});
contentScriptWhen: "ready",
onAttach: startListening
});

function startListening(worker) {
worker.port.on("queryRestaurant", function(restaurant) {
//console.log(restaurant);

var url = "http://api.ratings.food.gov.uk/Establishments?name=" + encodeURIComponent(restaurant.name) + "&address=" + encodeURIComponent(restaurant.address);
var rating = 0;

var Request = require("sdk/request").Request;
var foodLookupRequest = Request({
url: url,
headers: {'x-api-version':2, 'Content-Type':'application/json', 'Accept':'application/json'},
onComplete: function (response) {
if (response.json != null) {
if (response.json.establishments.length > 0) {
rating = response.json.establishments[0].RatingValue;
}
else {
rating = -1;
}
}
worker.port.emit("restaurantScore", {id:restaurant.id, rating:rating});
}
}).get();
});
}

0 comments on commit 7154ee8

Please sign in to comment.