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

193 lines
4.0 KiB
JavaScript
Raw Normal View History

2015-02-01 03:21:54 +00:00
Vn.Checkout = new Class
({
Extends: Vn.Module
2015-07-10 12:30:08 +00:00
,activate: function ()
{
this.autoStepLocked = true;
2015-07-23 15:58:48 +00:00
this.today = new Date ();
this.today.setHours (0,0,0,0);
2015-07-10 12:30:08 +00:00
}
2015-07-15 13:39:07 +00:00
,onValuesReady: function ()
2015-07-10 12:30:08 +00:00
{
2015-07-15 13:39:07 +00:00
var orderForm = this.$('order-form');
var defaultsForm = this.$('defaults');
2015-07-10 12:30:08 +00:00
2015-07-15 13:39:07 +00:00
if (!(orderForm.ready && defaultsForm.ready))
return;
var date;
2015-07-15 13:39:07 +00:00
if (orderForm.numRows > 0)
{
var i = orderForm;
date = i.get ('date_send');
2015-07-15 13:39:07 +00:00
}
else
var i = defaultsForm;
if (!date || date.getTime () < (new Date ()).getTime ())
{
date = new Date ();
2015-07-15 13:39:07 +00:00
if (i.get('delivery_method') != 'PICKUP')
date.setTime (date.getTime () + 86400000);
2015-07-10 12:30:08 +00:00
}
2015-07-15 13:39:07 +00:00
2015-07-17 14:34:42 +00:00
this.$('date').value = date;
2015-07-15 13:39:07 +00:00
this.$('method').value = i.get ('delivery_method');
this.$('agency').value = i.get ('agency_id');
this.$('address').value = i.get ('address_id');
this.autoStepLocked = false;
2015-07-10 12:30:08 +00:00
}
2015-07-15 13:39:07 +00:00
,onConfirmClick: function ()
2015-07-10 12:30:08 +00:00
{
2015-07-17 14:34:42 +00:00
var query = 'CALL basket_configure (#date, #method, #agency, #address)';
2015-07-15 13:39:07 +00:00
var batch = new Sql.Batch ();
batch.addParam ('method', this.$('method'));
2015-07-17 14:34:42 +00:00
batch.addParam ('date', this.$('date'));
2015-07-15 13:39:07 +00:00
batch.addParam ('agency', this.$('agency'));
batch.addParam ('address', this.$('address'));
this.conn.execQuery (query,
this.onBasketConfigured.bind (this), batch);
2015-07-10 12:30:08 +00:00
}
2015-07-15 13:39:07 +00:00
,onBasketConfigured: function (resultSet)
2015-07-10 12:30:08 +00:00
{
2015-07-15 13:39:07 +00:00
if (!resultSet.fetchResult ())
return;
if (this.$('order-form').numRows > 0)
2015-08-17 18:02:14 +00:00
Htk.Toast.showMessage (_('OrderUpdated'));
2015-07-15 13:39:07 +00:00
else
2015-08-17 18:02:14 +00:00
Htk.Toast.showMessage (_('OrderStarted'));
2015-10-14 11:51:43 +00:00
this.hash.set ({'form': 'ecomerce/catalog'});
2015-07-10 12:30:08 +00:00
}
,onCancelClick: function ()
{
2015-07-15 13:39:07 +00:00
if (this.$('order-form').numRows > 0)
2015-10-14 11:51:43 +00:00
window.history.back();
2015-07-15 13:39:07 +00:00
else
this.hash.set ({'form': 'ecomerce/orders'});
2015-07-10 12:30:08 +00:00
}
2015-07-15 13:39:07 +00:00
,agencySteps: ['method', 'date', 'address', 'agency', 'confirm-agency']
,deliverySteps: ['method', 'date', 'address', null, 'confirm-delivery']
,pickupSteps: ['method', 'date', 'pickup', null, 'confirm-pickup']
,stepFunc: function (stepIndex)
{
2015-07-03 05:49:45 +00:00
var steps;
var isDelivery;
2015-07-15 13:39:07 +00:00
switch (this.$('rg-method').value)
2015-07-03 05:49:45 +00:00
{
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];
2015-07-10 12:30:08 +00:00
if (!stepId)
return null;
switch (stepId)
{
2015-07-10 12:30:08 +00:00
case 'date':
Vn.Node.setText (this.$('date-question'), isDelivery ?
_('OrderDateDeliveryQuestion'):
_('OrderDatePickupQuestion'));
this.$('calendar').goToSelectedMonth ();
2015-07-10 12:30:08 +00:00
break;
case 'agency':
this.$('agencies').refresh ();
break;
case 'pickup':
2015-07-17 14:34:42 +00:00
this.$('warehouses').refresh ();
2015-07-10 12:30:08 +00:00
break;
}
2015-07-10 12:30:08 +00:00
return this.$(stepId +'-step');
2015-02-01 03:21:54 +00:00
}
2015-07-03 05:49:45 +00:00
,onFieldChange: function ()
{
2015-07-10 12:30:08 +00:00
if (!this.autoStepLocked)
2015-07-03 05:49:45 +00:00
setTimeout (this.goNextStep.bind (this), 75);
}
2015-07-23 15:58:48 +00:00
,goNextStep: function ()
{
this.$('assistant').moveNext ();
}
,addressRenderer: function (builder, form)
{
2015-07-23 15:58:48 +00:00
builder.$('address').addEventListener ('click',
this.onAddressClick.bind (this, form.get ('id')));
}
2015-07-23 15:58:48 +00:00
,onAddressClick: function (addressId)
{
2015-07-23 15:58:48 +00:00
this.$('address').value = addressId;
2015-07-03 05:49:45 +00:00
this.goNextStep ();
}
2015-07-23 15:58:48 +00:00
,onAddressChange: function ()
{
2015-07-23 15:58:48 +00:00
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;
}
2015-07-15 13:39:07 +00:00
2015-08-17 18:02:14 +00:00
,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'));
}
2015-07-15 13:39:07 +00:00
,calendarRestrict: function (date)
{
2015-07-23 15:58:48 +00:00
return date.getTime () >= this.today.getTime ()
2015-07-15 13:39:07 +00:00
&& date.getDay () != 0;
}
2015-02-01 03:21:54 +00:00
});