-
Notifications
You must be signed in to change notification settings - Fork 0
/
eoi.js
134 lines (113 loc) · 5.22 KB
/
eoi.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
(function () {
function loadScript(url, callback) {
var script = document.createElement("script")
script.type = "text/javascript";
if (script.readyState) { //IE
script.onreadystatechange = function () {
if (script.readyState == "loaded" || script.readyState == "complete") {
script.onreadystatechange = null;
callback();
}
};
} else { //Others
script.onload = function () {
callback();
};
}
script.src = url;
document.getElementsByTagName("head")[0].appendChild(script);
}
(function($) {
var $TITLE = $('title').text();
var $YELP = 'https://www.yelp.com/search?find_desc=' + encodeURI($TITLE);
$('h1').prepend('<a href="' + $YELP + '" target="_blank" style="position: inherit; left: inherit; top: inherit; font-size: inherit; display: inherit; white-space: inherit; float: left;"><img src="https://c1.staticflickr.com/6/5180/5540543936_423e37dae4_b.jpg" style="width: 1em; height: 1em; margin-right: 0.3em; vertical-align: -0.1em;"></a>');
var $BUDGET = parseFloat(prompt("Please enter your budget", "12.00")) || 12.00;
var $TAX = parseFloat(prompt("Please enter your tax rate", "0.0825")) || 0.0825;
var $TIP = parseFloat(prompt("Please enter your tip percentage", "0.1")) || 0.1;
var isCartEmpty = $('#total').length == 0;
var getTip = function() {
var tip = $('#CartGratuity');
if (tip.length == 0) {
return $TIP;
}
else
{
return tip.val() / 100;
}
};
var changing = 0;
var forceChange = function(fn) {
changing++;
fn();
changing--;
};
var onCartChange = function() {
if (changing == 0) {
requestAnimationFrame(function() {
isCartEmpty = $('#total').length == 0;
console.log('CHANGE!', isCartEmpty);
updateBudget();
changing--;
});
changing++;
}
};
var observer = new MutationObserver(onCartChange);
observer.observe($('#cart').get(0), { attributes: true, childList: true, characterData: true });
var updateBudget = function() {
var formatDollars = function(dollars) {
dollars = dollars.toFixed(2);
var dpos = dollars.toString().indexOf('.');
if (dpos == -1) {
return dollars + '.00';
}
else if (dollars.toString().length - dpos < 3) {
return dollars + '0';
}
return dollars;
};
var budget = (function() {
if (isCartEmpty) return $BUDGET;
var total = $(":contains('Grand Total')");
var totalText = total.children('span').text();
var amount = parseFloat(totalText.substring(totalText.indexOf("$") + 1));
console.log("Grand Total", amount);
var over = '$' + (amount > $BUDGET ? formatDollars(Math.abs($BUDGET - amount)) : formatDollars(0));
console.log("You Pay", over);
forceChange(function() {
$('.youPay, .yourBudget').remove();
$('#total').append('<p class="yourBudget"><strong>Your Budget<span>$' + formatDollars($BUDGET) + '</span></strong></p>');
$('#total').append('<p class="youPay"><strong>You Pay<span>' + over + '</span></strong></p>');
});
return $BUDGET - amount;
})();
var getCost = function(amount) { return budget - (amount * (1 + $TAX) * (1 + getTip())); }
$('em')
.each(function() {
var t = $(this), $pos = t.text().indexOf("$");
t.find('.costDetails').remove();
if ($pos > -1) {
var amount = parseFloat(t.text().substring($pos + 1));
var cost = getCost(amount);
var formattedCost = formatDollars(Math.abs((cost).toFixed(2)));
t
.css('text-align', 'right')
.parent()
.css('height', '3em')
.css('background-color', cost < 0 ? 'rgba(256,0,0,0.1)' : 'rgba(0,256,0,0.1)')
.css('border', cost < 0 ? '1px solid red' : '1px solid green');
var details = 'budget left:';
if (cost < 0) {
details = 'over budget by:'
}
t.append('<div class="costDetails">'
+ '<i style="font-style: italic !important; padding-right: 1em;">' + details + '</i>'
+ " $"
+ formattedCost
+ '</div>');
}
});
}
updateBudget();
})(jQuery);
})();