2020-09-21 14:06:31 +08:00
|
|
|
var app = null;
|
|
|
|
|
2021-10-11 12:34:00 +02:00
|
|
|
|
|
|
|
document.addEventListener("DOMContentLoaded",function (ev) {
|
2020-09-21 14:06:31 +08:00
|
|
|
app = new Vue({
|
|
|
|
el: '#app',
|
|
|
|
data: function(){
|
|
|
|
var displayFontSize = 80;
|
|
|
|
|
|
|
|
return {
|
|
|
|
srvModel: window.srvModel,
|
|
|
|
payTotal: '0',
|
|
|
|
payTotalNumeric: 0,
|
2021-10-25 03:06:32 -07:00
|
|
|
tipTotal: null,
|
|
|
|
tipTotalNumeric: 0,
|
2020-09-21 14:06:31 +08:00
|
|
|
fontSize: displayFontSize,
|
|
|
|
defaultFontSize: displayFontSize,
|
2022-01-26 17:58:25 -08:00
|
|
|
keys: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '.', '0', 'C'],
|
|
|
|
payButtonLoading: false,
|
2020-09-21 14:06:31 +08:00
|
|
|
}
|
|
|
|
},
|
2022-01-26 17:58:25 -08:00
|
|
|
created: function() {
|
|
|
|
/** We need to unset state in case user clicks the browser back button */
|
|
|
|
window.addEventListener('pagehide', this.unsetPayButtonLoading);
|
|
|
|
},
|
|
|
|
destroyed: function() {
|
|
|
|
window.removeEventListener('pagehide', this.unsetPayButtonLoading);
|
|
|
|
},
|
2020-09-21 14:06:31 +08:00
|
|
|
computed: {
|
|
|
|
Currency: function(){
|
|
|
|
return this.srvModel.Currency.toUpperCase();
|
|
|
|
},
|
|
|
|
},
|
|
|
|
watch: {
|
|
|
|
payTotal: function() {
|
|
|
|
// This must be timeouted because the updated width is not available yet
|
|
|
|
this.$nextTick(function(){
|
|
|
|
var displayWidth = this.getWidth(this.$refs.display),
|
|
|
|
amountWidth = this.getWidth(this.$refs.amount),
|
|
|
|
gamma = displayWidth / amountWidth || 0,
|
|
|
|
isAmountWider = displayWidth < amountWidth;
|
|
|
|
|
|
|
|
if (isAmountWider) {
|
|
|
|
// Font size will get smaller
|
|
|
|
this.fontSize = Math.floor(this.fontSize * gamma);
|
|
|
|
} else if (!isAmountWider && this.fontSize < this.defaultFontSize) {
|
|
|
|
// Font size will get larger up to the max size
|
|
|
|
this.fontSize = Math.min(this.fontSize * gamma, this.defaultFontSize);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
getWidth: function(el) {
|
|
|
|
var styles = window.getComputedStyle(el),
|
|
|
|
width = parseFloat(el.clientWidth),
|
|
|
|
padL = parseFloat(styles.paddingLeft),
|
|
|
|
padR = parseFloat(styles.paddingRight);
|
|
|
|
|
|
|
|
return width - padL - padR;
|
|
|
|
},
|
|
|
|
clearTotal: function() {
|
|
|
|
this.payTotal = '0';
|
|
|
|
this.payTotalNumeric = 0;
|
2021-10-25 03:06:32 -07:00
|
|
|
this.tipTotal = null;
|
|
|
|
this.tipTotalNumeric = 0;
|
2020-09-21 14:06:31 +08:00
|
|
|
},
|
2022-01-26 17:58:25 -08:00
|
|
|
handleFormSubmit: function() {
|
|
|
|
this.payButtonLoading = true;
|
|
|
|
},
|
|
|
|
unsetPayButtonLoading: function() {
|
|
|
|
this.payButtonLoading = false;
|
|
|
|
},
|
2020-09-21 14:06:31 +08:00
|
|
|
buttonClicked: function(key) {
|
|
|
|
var payTotal = this.payTotal;
|
|
|
|
|
|
|
|
if (key === 'C') {
|
|
|
|
payTotal = payTotal.substring(0, payTotal.length - 1);
|
|
|
|
payTotal = payTotal === '' ? '0' : payTotal;
|
|
|
|
} else if (key === '.') {
|
|
|
|
// Only add decimal point if it doesn't exist yet
|
|
|
|
if (payTotal.indexOf('.') === -1) {
|
|
|
|
payTotal += key;
|
|
|
|
}
|
|
|
|
} else { // Is a digit
|
|
|
|
if (payTotal === '0') {
|
|
|
|
payTotal = '';
|
|
|
|
}
|
|
|
|
payTotal += key;
|
|
|
|
|
2021-10-25 03:06:32 -07:00
|
|
|
var divisibility = this.srvModel.currencyInfo.divisibility;
|
2020-09-21 14:06:31 +08:00
|
|
|
var decimalIndex = payTotal.indexOf('.')
|
2021-10-25 03:06:32 -07:00
|
|
|
if (decimalIndex !== -1 && (payTotal.length - decimalIndex - 1 > divisibility)) {
|
2020-09-21 14:06:31 +08:00
|
|
|
payTotal = payTotal.replace(".", "");
|
2021-10-25 03:06:32 -07:00
|
|
|
payTotal = payTotal.substr(0, payTotal.length - divisibility) + "." + payTotal.substr(payTotal.length - divisibility);
|
2020-09-21 14:06:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.payTotal = payTotal;
|
|
|
|
this.payTotalNumeric = parseFloat(payTotal);
|
2021-10-25 03:06:32 -07:00
|
|
|
this.tipTotalNumeric = 0;
|
|
|
|
this.tipTotal = null;
|
|
|
|
},
|
|
|
|
tipClicked: function(percentage) {
|
|
|
|
this.payTotalNumeric -= this.tipTotalNumeric;
|
|
|
|
this.tipTotalNumeric = parseFloat((this.payTotalNumeric * (percentage / 100)).toFixed(this.srvModel.currencyInfo.divisibility));
|
|
|
|
this.payTotalNumeric = parseFloat((this.payTotalNumeric + this.tipTotalNumeric).toFixed(this.srvModel.currencyInfo.divisibility));
|
|
|
|
|
|
|
|
this.payTotal = this.payTotalNumeric.toString(10);
|
|
|
|
this.tipTotal = this.tipTotalNumeric === 0 ? null : this.tipTotalNumeric.toFixed(this.srvModel.currencyInfo.divisibility);
|
|
|
|
},
|
|
|
|
removeTip: function() {
|
|
|
|
this.payTotalNumeric -= this.tipTotalNumeric;
|
|
|
|
this.payTotal = this.payTotalNumeric.toString(10);
|
|
|
|
this.tipTotalNumeric = 0;
|
|
|
|
this.tipTotal = null;
|
|
|
|
},
|
2020-09-21 14:06:31 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|