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

207 lines
4.0 KiB
JavaScript
Raw Normal View History

2017-10-28 15:13:00 +00:00
(function () {
var methodsData = {
AGENCY: {
steps: ['method', 'date', 'address', 'agency', 'confirm'],
isDelivery: true,
textId: 'agencyText'
},
DELIVERY: {
steps: ['method', 'date', 'address', null, 'confirm'],
isDelivery: true,
textId: 'deliveryText'
},
PICKUP: {
steps: ['method', 'date', 'address', 'pickup', 'confirm'],
isDelivery: false,
textId: 'pickupText'
}
};
2015-02-01 03:21:54 +00:00
2016-09-26 09:28:47 +00:00
Hedera.Checkout = new Class
2015-02-01 03:21:54 +00:00
({
2016-09-26 09:28:47 +00:00
Extends: Hedera.Form
2015-02-01 03:21:54 +00:00
2017-04-19 06:16:37 +00:00
,initialize: function (props)
{
this.today = new Date ();
this.today.setHours (0, 0, 0, 0);
this.parent (props);
}
2015-07-10 12:30:08 +00:00
,activate: function ()
{
this.autoStepLocked = true;
}
2015-07-15 13:39:07 +00:00
,onValuesReady: function ()
2015-07-10 12:30:08 +00:00
{
2017-10-20 17:09:06 +00:00
var orderForm = this.$.orderForm;
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;
2017-10-28 15:13:00 +00:00
date = i.$.date_send;
2015-07-15 13:39:07 +00:00
}
else
var i = defaultsForm;
if (!date || date.getTime () < (new Date ()).getTime ())
{
date = new Date ();
2017-10-28 15:13:00 +00:00
if (i.$.delivery_method != 'PICKUP')
2017-04-05 14:06:07 +00:00
date.setDate (date.getDate () + 1);
date.setHours (0, 0, 0, 0);
2015-07-10 12:30:08 +00:00
}
2015-07-15 13:39:07 +00:00
2017-10-20 17:09:06 +00:00
this.$.lot.assign ({
2017-04-07 11:00:33 +00:00
date: date,
2017-10-28 15:13:00 +00:00
method: i.$.delivery_method,
agency: i.$.agency_id,
address: i.$.address_id
2017-04-07 11:00:33 +00:00
});
2015-07-15 13:39:07 +00:00
this.autoStepLocked = false;
2015-07-10 12:30:08 +00:00
}
2015-12-10 13:48:43 +00:00
,disableButtons: function (disable)
{
2017-10-28 15:13:00 +00:00
this.$.confirmButton.disabled = disable;
2015-12-10 13:48:43 +00:00
}
2015-07-15 13:39:07 +00:00
,onConfirmClick: function ()
2015-07-10 12:30:08 +00:00
{
2015-12-10 13:48:43 +00:00
this.disableButtons (true);
2017-04-07 11:00:33 +00:00
var query = 'CALL basketConfigure (#date, #method, #agency, #address)';
2015-07-15 13:39:07 +00:00
this.conn.execQuery (query,
2017-10-20 17:09:06 +00:00
this.onBasketConfigured.bind (this), this.$.lot.params);
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-12-10 13:48:43 +00:00
this.disableButtons (false);
2015-07-15 13:39:07 +00:00
if (!resultSet.fetchResult ())
return;
2017-10-28 15:13:00 +00:00
var isUpdated = this.$.orderForm.numRows > 0;
Htk.Toast.showMessage (_(isUpdated ? 'OrderUpdated' : 'OrderStarted'));
2017-04-07 11:00:33 +00:00
this.hash.setAll ({form: 'ecomerce/catalog'});
2015-07-10 12:30:08 +00:00
}
,onCancelClick: function ()
{
2017-10-20 17:09:06 +00:00
if (this.$.orderForm.numRows > 0)
window.history.back();
2015-07-15 13:39:07 +00:00
else
2017-10-28 15:13:00 +00:00
this.hash.setAll ({form: 'ecomerce/orders'});
2015-07-10 12:30:08 +00:00
}
,stepFunc: function (stepIndex)
{
2017-10-28 15:13:00 +00:00
var method = this.$.rgMethod.value;
var methodData = methodsData[method];
var stepName;
2015-07-03 05:49:45 +00:00
2017-10-28 15:13:00 +00:00
if (methodData !== undefined)
stepName = methodData.steps[stepIndex];
else
stepName = 'method';
if (!stepName)
2015-07-10 12:30:08 +00:00
return null;
2017-10-28 15:13:00 +00:00
switch (stepName)
{
2015-07-10 12:30:08 +00:00
case 'date':
2017-10-28 15:13:00 +00:00
var dateQuestion = methodData.isDelivery ?
'OrderDateDeliveryQuestion' : 'OrderDatePickupQuestion';
Vn.Node.setText (_(dateQuestion));
2017-10-20 17:09:06 +00:00
this.$.calendar.goToSelectedMonth ();
2015-07-10 12:30:08 +00:00
break;
case 'agency':
case 'pickup':
2017-10-20 17:09:06 +00:00
this.$.agencies.refresh ();
2015-07-10 12:30:08 +00:00
break;
2017-10-28 15:13:00 +00:00
case 'confirm':
for (var myMethod in methodsData)
{
var display = myMethod == method ? 'block': 'none';
this.$[methodsData[myMethod].textId].style.display = display;
}
break;
}
2015-07-10 12:30:08 +00:00
2017-10-28 15:13:00 +00:00
return stepName;
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 ()
{
2017-10-20 17:09:06 +00:00
this.$.assistant.moveNext ();
2015-07-23 15:58:48 +00:00
}
,addressRenderer: function (builder, form)
{
2017-10-20 17:09:06 +00:00
builder.$.address.addEventListener ('click',
2017-10-28 15:13:00 +00:00
this.onAddressClick.bind (this, form.$.id));
}
2015-07-23 15:58:48 +00:00
,onAddressClick: function (addressId)
{
2017-10-20 17:09:06 +00:00
this.$.lot.set ('address', 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');
2017-10-28 15:13:00 +00:00
var row = this.$.addresses.search ('id', this.$.lot.$.address);
2015-07-23 15:58:48 +00:00
if (row != -1)
{
2017-10-20 17:09:06 +00:00
var builder = this.$.repeater.getScope (row);
2015-07-23 15:58:48 +00:00
2017-10-20 17:09:06 +00:00
this.selectedNode = builder.$.address;
2015-07-23 15:58:48 +00:00
Vn.Node.addClass (this.selectedNode, 'selected');
}
2017-10-20 17:09:06 +00:00
this.$.addressForm.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)
{
return date.getTime () >= this.today.getTime ();
2015-07-15 13:39:07 +00:00
}
2015-02-01 03:21:54 +00:00
});
2017-10-28 15:13:00 +00:00
})();