hedera-web/forms/ecomerce/checkout/checkout.js

213 lines
4.7 KiB
JavaScript

Hedera.Checkout = new Class({
Extends: Hedera.Form,
activate: function() {
this.autoStepLocked = true;
this.$.assistant.stepsIndex = this.agencySteps;
this.today = new Date();
this.today.setHours(0, 0, 0, 0);
},
onValuesReady: function() {
var orderForm = this.$.orderForm;
var defaultsForm = this.$.defaults;
if (!(orderForm.ready && defaultsForm.ready))
return;
var date;
const row = orderForm.$ || defaultsForm.$;
if (!date || date.getTime() < (new Date()).getTime()) {
date = new Date();
date.setHours(0, 0, 0, 0);
var addDays = 0;
switch(date.getDay()) {
case 6: // Saturday
addDays += 2;
break;
case 0: // Sunday
addDays++;
break;
}
if (row.deliveryMethod != 'PICKUP')
addDays++;
date.setDate(date.getDate() + addDays);
}
this.$.lot.assign({
date: date,
method: row.deliveryMethod,
agency: row.agencyModeFk,
address: row.addressFk
});
this.autoStepLocked = false;
},
disableButtons: function(disable) {
this.$.assistantBar.disabled = disable;
},
onConfirmClick: function() {
this.disableButtons(true);
var query = 'CALL myBasket_configure(#date, #method, #agency, #address)';
this.conn.execQuery(query,
this.onBasketConfigured.bind(this), this.$.lot.$);
},
onBasketConfigured: function(resultSet) {
this.disableButtons(false);
if (!resultSet.fetchResult())
return;
if (this.$.orderForm.numRows > 0)
Htk.Toast.showMessage(_('OrderUpdated'));
else
Htk.Toast.showMessage(_('OrderStarted'));
this.hash.setAll({form: 'ecomerce/catalog'});
},
onCancelClick: function() {
if (this.$.orderForm.numRows > 0)
window.history.back();
else
this.hash.setAll({form: 'ecomerce/orders'});
},
agencySteps: ['method', 'date', 'address', 'agency', 'confirm-delivery'],
pickupSteps: ['method', 'date', 'address', 'pickup', 'confirm-pickup'],
isDelivery: function() {
return this.$.rgMethod.value != 'PICKUP';
},
onMethodChange: function() {
this.$.assistant.stepsIndex = this.isDelivery() ?
this.agencySteps : this.pickupSteps;
this.onFieldChange();
},
methodValidate: function() {
if (!this.$.rgMethod.isSelected())
throw new Error(_('Please select an option'));
},
dateShow: function() {
Vn.Node.setText(this.$.dateQuestion, this.isDelivery() ?
_('OrderDateDeliveryQuestion'):
_('OrderDatePickupQuestion'));
this.$.calendar.goToSelectedMonth();
},
dateValidate: function() {
if (!this.$.calendar.value)
throw new Error(_('Please select a date'));
},
addressShow: function() {
Vn.Node.setText(this.$.addressQuestion, this.isDelivery() ?
_('AddressQuestion'):
_('AddressQuestionPickup'));
},
addressValidate: function() {
if (this.$.addressForm.row == -1)
throw new Error(_('Please select an address'));
},
agencyShow: function() {
this.$.agencies.refresh();
},
agencyValidate: function() {
if (this.$.agencyCombo.row == -1 && this.isDelivery())
throw new Error(_('Please select an agency'));
},
pickupShow: function() {
this.$.warehouses.refresh();
},
pickupValidate: function() {
if (this.$.warehouseCombo.row == -1)
throw new Error(_('Please select a store'));
},
onFieldChange: function() {
if (!this.autoStepLocked)
this.$.assistant.moveNext();
},
goNextStep: function() {
this.$.assistant.moveNext();
},
addressRenderer: function(builder, form) {
builder.$.address.addEventListener('click',
this.onAddressClick.bind(this, form.$.id));
},
onAddressClick: function(addressId) {
this.$.lot.set('address', addressId);
this.goNextStep();
},
onAddressChange: function() {
if (this.selectedNode)
Vn.Node.removeClass(this.selectedNode, 'selected');
var row = this.$.addresses.search('id', this.$.lot.$.address);
if (row != -1) {
var builder = this.$.repeater.getBuilder(row);
this.selectedNode = builder.$.address;
Vn.Node.addClass(this.selectedNode, 'selected');
}
this.$.addressForm.row = row;
},
onAgenciesReady: function(model) {
if (!model.ready) return;
if (model.numRows > 0) {
var agency;
var defaults = [
this.$.orderForm.$.agencyModeFk,
this.$.defaults.$.agencyModeFk,
this.$.defaults.$.defaultAgencyFk
];
for (var i = 0; i < defaults.length; i++) {
agency = defaults[i];
if (model.search('id', agency) !== -1)
break;
}
this.autoStepLocked = true;
this.$.agency.value = agency;
this.autoStepLocked = false;
} else
Htk.Toast.showError(_('NoAgeciesAvailableForDate'));
},
onWarehousesReady: function(model) {
if (model.ready && model.numRows == 0)
Htk.Toast.showError(_('NoWarehousesAvailableForDate'));
},
calendarRestrict: function(date) {
return date.getTime() >= this.today.getTime();
}
});