Last active
January 27, 2017 14:10
-
-
Save iKrevedko/7d51721103aad5251cc8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function($) { | |
MyExternalPickup = (function() { | |
function MyExternalPickup(deliveryId, fieldId) { | |
this.deliveryId = deliveryId; | |
this.fieldId = fieldId; | |
this.bind(); | |
} | |
MyExternalPickup.prototype.input = function() { | |
return this.element || (this.element = $(this.inputSelector)); | |
}; | |
MyExternalPickup.prototype.bind = function() { | |
$(document).on('click', '#select-postbox-' + this.deliveryId, (function(_this) { | |
return function(e) { | |
// При выборе постамата рассчитываем стоимость доставки и устанавливаем новые значения | |
e.preventDefault(); | |
_this.ensureSelected(); | |
return _this.calc(); | |
}; | |
})(this)); | |
return $(document).on('inited:insales:checkout:deliveries', (function(_this) { | |
return function(e) { | |
data = e.originalEvent.detail | |
var myDelivery, myFiledsValues; | |
myDelivery = data.deliveries[_this.deliveryId]; | |
if (!myDelivery || !myDelivery.available) { | |
return; | |
} | |
_this.order = data.order; | |
_this.inputSelector = myDelivery.html_id; | |
if (_this.inited) { | |
// Если способ доставки уже инициализирован, то просто делаем update без параметров для перерисовки стоимости доставки и т.д. | |
return _this.input().trigger('update:insales:delivery'); | |
} | |
// При вервой загрузке пытаемся найти значения доп полей в заказе | |
myFiledsValues = $.grep(data.order.fields_values, function(v, k) { | |
return v.field_id === this.fieldId; | |
}); | |
// Устанавливаем стоимость доставки, доп поля, описание в формате html и флаг внешнего способа доставки | |
_this.input().trigger('update:insales:delivery', { | |
price: myDelivery.price, | |
fields_values: myFiledsValues, | |
is_external: true, | |
description_html: "</br><a href='#'' id='select-postbox-" + _this.deliveryId + "'>Выберите постамат</a>" | |
}); | |
return _this.inited = true; | |
}; | |
})(this)); | |
}; | |
MyExternalPickup.prototype.calc = function() { | |
if (this.order.shipping_address.city === 'Москва') { | |
return this.input().trigger('update:insales:delivery', { | |
price: 100, | |
fields_values: [ | |
{ | |
field_id: this.fieldId, | |
value: '123' | |
} | |
] | |
}); | |
} | |
if (this.order.shipping_address.city === 'Санкт-Петербург') { | |
this.input().trigger('disable:insales:delivery'); | |
return this.input().trigger('error:insales:delivery', 'Доставка не доступна'); | |
} | |
return this.input().trigger('update:insales:delivery', { | |
price: 150, | |
fields_values: [ | |
{ | |
field_id: this.fieldId, | |
value: '321' | |
} | |
] | |
}); | |
}; | |
MyExternalPickup.prototype.ensureSelected = function() { | |
if (!this.input().is(':checked')) { | |
return this.input().click(); | |
} | |
}; | |
return MyExternalPickup | |
})(); | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment