-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmmm-food2fork-recipes.js
166 lines (141 loc) · 6.02 KB
/
mmm-food2fork-recipes.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/* MMM2 Module */
/* spectroman's
* Module: food 2 fork Recipes
*
* By Spectroman https://juliochegedus.com
* MIT Licensed.
*/
Module.register("mmm-food2fork-recipes",{
// Default module config.
defaults: {
APIkey: "GETYOUROWNKEY", // API key from food 2 fork
updateInterval: 3600 * 1000, // every hour
animationSpeed: 1000,
listSize: 4,
maxTitleSize: 40,
fade: true,
fadePoint: 0.25, // Start on 1/4th of the list.
initialLoadDelay: 2500, // 2.5 seconds delay.
retryDelay: 2500,
apiSearch: "http://food2fork.com/api/search",
daysTable: {
"1": "natural",
"2": "chicken",
"3": "vegan",
"4": "indian",
"5": "japanese",
"6": "quick",
"0": "vegetarian"
},
},
// Define required scripts.
getScripts: function() {
return ["moment.js"];
},
// Define required scripts.
getStyles: function() {
return ["recipes.css"];
},
// Define start sequence.
start: function() {
Log.info("Starting module: " + this.name);
this.loaded = false;
this.scheduleUpdate(this.config.initialLoadDelay);
this.updateTimer = null;
},
updateRecipes: function() {
var self = this;
var d = new Date();
var n = d.getDay()
var rPage = function() { return Math.floor(Math.random() * 4); };
var pageNum = rPage();
var pRequest;
if (pageNum > 0) {
pRequest = "&page="+pageNum;
} else {
pRequest="&page=1";
}
var url = this.config.apiSearch + "?key=" + this.config.APIkey + "&q=" + this.config.daysTable[n]+pRequest;
self.sendSocketNotification("GET_RECIPE", {config: this.config, url: url});
},
socketNotificationReceived: function(notification, payload) {
if(notification === "RECIPE"){
this.processRecipe(payload);
this.scheduleUpdate();
}
},
processRecipe: function(data) {
this.foodlist = [];
var idxCheck = [];
for(count=0; count < this.config.listSize; count++) {
var rindex = function() {
return Math.floor(Math.random() * data.recipes.length);
};
var rec = rindex();
if (count > 0) {
var c=0;
while (idxCheck[c]) {
rec=rindex();
if (rec === idxCheck[c]) {
rec=rindex();
}
c++;
}
}
// console.log("From: "+data.recipes[rec].publisher+", Name: "+data.recipes[rec].title);
var titleLimit = data.recipes[rec].title.substring(0,this.config.maxTitleSize);
this.foodlist.push({
publisher: data.recipes[rec].publisher,
namedish: titleLimit,
image: data.recipes[rec].image_url
});
idxCheck[count]=rec;
}
this.loaded = true;
this.updateDom(this.config.animationSpeed);
},
scheduleUpdate: function(delay) {
var nextLoad = this.config.updateInterval;
if (typeof delay !== "undefined" && delay >= 0) {
nextLoad = delay;
}
var self = this;
clearTimeout(this.updateTimer);
this.updateTimer = setTimeout(function() {
self.updateRecipes();
}, nextLoad);
},
// Override dom generator.
getDom: function() {
var table = document.createElement("table");
table.className = "small";
for (var l in this.foodlist) {
var food = this.foodlist[l];
var row = document.createElement("tr");
table.appendChild(row);
var imgCell = document.createElement("td");
var img = "<img src='" + food.image + "' height='50' width='50'>";
imgCell.innerHTML = img;
row.appendChild(imgCell);
var dishCell = document.createElement("td");
dishCell.className = "name";
dishCell.innerHTML = food.namedish;
row.appendChild(dishCell);
var pubCell = document.createElement("td");
pubCell.innerHTML = food.publisher;
row.appendChild(pubCell);
if (this.config.fade && this.config.fadePoint < 1) {
if (this.config.fadePoint < 0) {
this.config.fadePoint = 0;
}
var startingPoint = this.foodlist.length * this.config.fadePoint;
var steps = this.foodlist.length - startingPoint;
if (l >= startingPoint) {
var currentStep = l - startingPoint;
row.style.opacity = 1 - (1 / steps * currentStep);
}
}
}
return table;
}
});