-
Notifications
You must be signed in to change notification settings - Fork 1
/
meal_calculator.html
185 lines (157 loc) · 5.78 KB
/
meal_calculator.html
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<html>
<head>
<title>買餐計價</title>
<link rel="stylesheet" href="style.css" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js"></script>
<script type="text/javascript" src="meal_calculator.js"></script>
<script type="text/javascript">
/* UI Controller*/
var mealCal = {
menusTarget: "#menus",
foodsTarget: "#foods",
selectedTarget: "#selected",
outputTarget: "#output",
suggestionTarget: "#suggestion",
findButton: "#find",
clearLink: "#clear",
menuStore: {
'McDonald': {url: 'mac_menu.js'},
'MOSBurger': {url: 'mos_menu.js'}
},
init: function(){
//load menu and first item
$(this.menusTarget).html(this.loadMenuStore());
this.loadMenu($(this.menusTarget + " a.menu:first").text());
//Components
var ui = this;
$(this.menusTarget).click(function(e){
var t = $(e.target);
if(t.is('a.menu')){
ui.loadMenu(t.text());
}
});
$(this.foodsTarget).click(function(e){
var t = $(e.target);
if(t.is("a.food")){
ui.renderSelectedFood(t).appendTo("#selected");
}
return false;
});
$(this.clearLink).click(function(){
$([ui.selectedTarget,ui.outputTarget].join(',')).html("");
$(ui.suggestionTarget).hide();
return false;
});
$(this.findButton).click(function(){
var items = ui.calculateItems(); //calculate
$(ui.outputTarget).html(ui.renderReport(items)); //report
$(ui.suggestionTarget).show();
});
},
calculateItems: function(){
var selected = [];
$(this.selectedTarget + " a.food").each(function(){
selected.push($(this).text());
});
return mealCalculator.calculate(selected);
},
loadMenuStore: function(){
return this.renderMenus(this.menuStore);
},
loadMenu: function(name){
var activeMenu = $(this.menusTarget + " .active").text();
if(activeMenu != name){
this.clearMenu();
var url = this.menuStore[name].url, ui = this;
$.getScript(url, function(data){
mealCalculator.setMenu(window.menu); //evil!
ui.activateMenu(name);
});
}
},
activateMenu: function(name){
$(this.menusTarget)
.find("a.menu").removeClass('active').end()
.find("a:contains('"+name+"')").addClass('active');
$(this.foodsTarget).html(this.loadFoods());
},
loadFoods: function(){
var foods = [], menuFoods = mealCalculator.menu.foods;
for(var f in menuFoods){
foods.push($.extend({name:f}, menuFoods[f]));
}
return this.renderFoods(foods);
},
clearMenu: function(){
$([this.foodsTarget,this.selectedTarget,this.outputTarget].join(",")).html("");
$(this.suggestionTarget).hide();
},
// Renderers
renderMenus: function(menus){
var menuHtml = [];
for(var m in menus){
menuHtml.push("<a href='#' class='menu'>"+m+"</a>");
}
return menuHtml.join(',');
},
renderFoods: function(foods){
return $.map(foods, function(n,i){
return '<a class="food" href="#">' + n.name + '</a>($' + n.price + ')';
}).join(",");
},
renderSelectedFood: function(cloneTarget){
return $("<span/>")
.append(cloneTarget.clone())
.append("<a class='close' href='#'>x</a>")
.click(function(){
$(this).remove();
return false;
});
},
renderReport: function(items){
var outputHtml = "", total = 0, menu = mealCalculator.menu;
for(var i in items){
var item = menu.foods[items[i]] || menu.meals[items[i]];
if(item){
outputHtml += "<li>" + items[i] + " ($" + item.price + ") </li>";
total += item.price;
}
}
if(outputHtml) outputHtml = '<ul>' + outputHtml + '</ul>';
outputHtml += '<p class="total">總額: $' + Math.round(total*1000)/1000 + '</p>'; //prevent floating point problem
return outputHtml;
}
};
$(document).ready(function(){
mealCal.init();
});
</script>
</head>
<body>
<h1>買餐計價</h1>
<div class="section">
<h2>餐牌:</h2>
<div id="menus"></div>
</div>
<div class="section">
<h2>點擊加入食物:</h2>
<div id="foods"></div>
</div>
<div class="section">
<h2>已選食物:</h2>
<div id="selected"></div>
<input type="button" value="計算組合!" id="find"/>
<a href="#" id="clear">清除</a>
</div>
<div class="section" id="suggestion">
<h2>建議購買組合:</h2>
<div id="output"></div>
</div>
<div id="footer">
This app is created by <a href="http://github.com/jackysee">JackySee</a>,
inspired by Chainsawriot's <a href="http://blog.tiney.com/?p=2345">question</a>
and Siuying's <a href="http://www.reality.hk/articles/2009/04/19/929/">Java quiz</a>.
See details <a href="http://jackysee.github.com/mealcalculator/index.html">here</a>
</div>
</body>
</html>