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

193 lines
4.0 KiB
JavaScript
Executable File

Vn.Checkout = new Class
({
Extends: Vn.Module
,activate: function ()
{
this.autoStepLocked = true;
this.today = new Date ();
this.today.setHours (0,0,0,0);
}
,onValuesReady: function ()
{
var orderForm = this.$('order-form');
var defaultsForm = this.$('defaults');
if (!(orderForm.ready && defaultsForm.ready))
return;
if (orderForm.numRows > 0)
{
var i = orderForm;
var date = i.get ('date_send');
}
else
{
var i = defaultsForm;
var date = new Date ();
if (i.get('delivery_method') != 'PICKUP')
date.setTime (date.getTime () + 86400000);
}
this.$('date').value = date;
this.$('method').value = i.get ('delivery_method');
this.$('agency').value = i.get ('agency_id');
this.$('address').value = i.get ('address_id');
this.autoStepLocked = false;
}
,onConfirmClick: function ()
{
var query = 'CALL basket_configure (#date, #method, #agency, #address)';
var batch = new Sql.Batch ();
batch.addParam ('method', this.$('method'));
batch.addParam ('date', this.$('date'));
batch.addParam ('agency', this.$('agency'));
batch.addParam ('address', this.$('address'));
this.conn.execQuery (query,
this.onBasketConfigured.bind (this), batch);
}
,onBasketConfigured: function (resultSet)
{
if (!resultSet.fetchResult ())
return;
if (this.$('order-form').numRows > 0)
{
Htk.Toast.showMessage (_('OrderUpdated'));
window.history.back();
}
else
{
Htk.Toast.showMessage (_('OrderStarted'));
this.hash.set ({'form': 'ecomerce/catalog'});
}
}
,onCancelClick: function ()
{
if (this.$('order-form').numRows > 0)
this.hash.set ({'form': 'ecomerce/basket'});
else
this.hash.set ({'form': 'ecomerce/orders'});
}
,agencySteps: ['method', 'date', 'address', 'agency', 'confirm-agency']
,deliverySteps: ['method', 'date', 'address', null, 'confirm-delivery']
,pickupSteps: ['method', 'date', 'pickup', null, 'confirm-pickup']
,stepFunc: function (stepIndex)
{
var steps;
var isDelivery;
switch (this.$('rg-method').value)
{
case 'AGENCY':
steps = this.agencySteps;
isDelivery = true;
break;
case 'DELIVERY':
steps = this.deliverySteps;
isDelivery = true;
break;
case 'PICKUP':
default:
steps = this.pickupSteps;
isDelivery = false;
break;
}
var stepId = steps[stepIndex];
if (!stepId)
return null;
switch (stepId)
{
case 'date':
Vn.Node.setText (this.$('date-question'), isDelivery ?
_('OrderDateDeliveryQuestion'):
_('OrderDatePickupQuestion'));
this.$('calendar').goToSelectedMonth ();
break;
case 'agency':
this.$('agencies').refresh ();
break;
case 'pickup':
this.$('warehouses').refresh ();
break;
}
return this.$(stepId +'-step');
}
,onFieldChange: function ()
{
if (!this.autoStepLocked)
setTimeout (this.goNextStep.bind (this), 75);
}
,goNextStep: function ()
{
this.$('assistant').moveNext ();
}
,addressRenderer: function (builder, form)
{
builder.$('address').addEventListener ('click',
this.onAddressClick.bind (this, form.get ('id')));
}
,onAddressClick: function (addressId)
{
this.$('address').value = addressId;
this.goNextStep ();
}
,onAddressChange: function ()
{
if (this.selectedNode)
Vn.Node.removeClass (this.selectedNode, 'selected');
var row = this.$('addresses').search ('id', this.$('address').value);
if (row != -1)
{
var builder = this.$('repeater').getBuilder (row);
this.selectedNode = builder.$('address');
Vn.Node.addClass (this.selectedNode, 'selected');
}
this.$('address-form').row = row;
}
,onAgenciesReady: function (model)
{
if (model.ready && model.numRows == 0)
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 ()
&& date.getDay () != 0;
}
});