Guest User

Untitled

a guest
Nov 14th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.15 KB | None | 0 0
  1. var popUp = null;
  2.  
  3. return Component.extend({
  4. defaults: {
  5. template: 'Magento_Checkout/shipping',
  6. shippingFormTemplate: 'Magento_Checkout/shipping-address/form',
  7. shippingMethodListTemplate: 'Magento_Checkout/shipping-address/shipping-method-list',
  8. shippingMethodItemTemplate: 'Magento_Checkout/shipping-address/shipping-method-item'
  9. },
  10. visible: ko.observable(!quote.isVirtual()),
  11. errorValidationMessage: ko.observable(false),
  12. isCustomerLoggedIn: customer.isLoggedIn,
  13. isFormPopUpVisible: formPopUpState.isVisible,
  14. isFormInline: addressList().length === 0,
  15. isNewAddressAdded: ko.observable(false),
  16. saveInAddressBook: 1,
  17. quoteIsVirtual: quote.isVirtual(),
  18.  
  19. /**
  20. * @return {exports}
  21. */
  22. initialize: function () {
  23. var self = this,
  24. hasNewAddress,
  25. fieldsetName = 'checkout.steps.shipping-step.shippingAddress.shipping-address-fieldset';
  26.  
  27. this._super();
  28.  
  29. if (!quote.isVirtual()) {
  30. stepNavigator.registerStep(
  31. 'shipping',
  32. '',
  33. $t('Shipping'),
  34. this.visible, _.bind(this.navigate, this),
  35. 10
  36. );
  37. }
  38. checkoutDataResolver.resolveShippingAddress();
  39.  
  40. hasNewAddress = addressList.some(function (address) {
  41. return address.getType() == 'new-customer-address'; //eslint-disable-line eqeqeq
  42. });
  43.  
  44. this.isNewAddressAdded(hasNewAddress);
  45.  
  46. this.isFormPopUpVisible.subscribe(function (value) {
  47. if (value) {
  48. self.getPopUp().openModal();
  49. }
  50. });
  51.  
  52. quote.shippingMethod.subscribe(function () {
  53. self.errorValidationMessage(false);
  54. });
  55.  
  56. registry.async('checkoutProvider')(function (checkoutProvider) {
  57. var shippingAddressData = checkoutData.getShippingAddressFromData();
  58.  
  59. if (shippingAddressData) {
  60. checkoutProvider.set(
  61. 'shippingAddress',
  62. $.extend(true, {}, checkoutProvider.get('shippingAddress'), shippingAddressData)
  63. );
  64. }
  65. checkoutProvider.on('shippingAddress', function (shippingAddrsData) {
  66. checkoutData.setShippingAddressFromData(shippingAddrsData);
  67. });
  68. shippingRatesValidator.initFields(fieldsetName);
  69. });
  70.  
  71. return this;
  72. },
  73.  
  74. /**
  75. * Navigator change hash handler.
  76. *
  77. * @param {Object} step - navigation step
  78. */
  79. navigate: function (step) {
  80. step && step.isVisible(true);
  81. },
  82.  
  83. /**
  84. * @return {*}
  85. */
  86. getPopUp: function () {
  87. var self = this,
  88. buttons;
  89.  
  90. if (!popUp) {
  91. buttons = this.popUpForm.options.buttons;
  92. this.popUpForm.options.buttons = [
  93. {
  94. text: buttons.save.text ? buttons.save.text : $t('Save Address'),
  95. class: buttons.save.class ? buttons.save.class : 'action primary action-save-address',
  96. click: self.saveNewAddress.bind(self)
  97. },
  98. {
  99. text: buttons.cancel.text ? buttons.cancel.text : $t('Cancel'),
  100. class: buttons.cancel.class ? buttons.cancel.class : 'action secondary action-hide-popup',
  101.  
  102. /** @inheritdoc */
  103. click: this.onClosePopUp.bind(this)
  104. }
  105. ];
  106.  
  107. /** @inheritdoc */
  108. this.popUpForm.options.closed = function () {
  109. self.isFormPopUpVisible(false);
  110. };
  111.  
  112. this.popUpForm.options.modalCloseBtnHandler = this.onClosePopUp.bind(this);
  113. this.popUpForm.options.keyEventHandlers = {
  114. escapeKey: this.onClosePopUp.bind(this)
  115. };
  116.  
  117. /** @inheritdoc */
  118. this.popUpForm.options.opened = function () {
  119. // Store temporary address for revert action in case when user click cancel action
  120. self.temporaryAddress = $.extend(true, {}, checkoutData.getShippingAddressFromData());
  121. };
  122. popUp = modal(this.popUpForm.options, $(this.popUpForm.element));
  123. }
  124.  
  125. return popUp;
  126. },
  127.  
  128. /**
  129. * Revert address and close modal.
  130. */
  131. onClosePopUp: function () {
  132. checkoutData.setShippingAddressFromData($.extend(true, {}, this.temporaryAddress));
  133. this.getPopUp().closeModal();
  134. },
  135.  
  136. /**
  137. * Show address form popup
  138. */
  139. showFormPopUp: function () {
  140. this.isFormPopUpVisible(true);
  141. },
  142.  
  143. /**
  144. * Save new shipping address
  145. */
  146. saveNewAddress: function () {
  147. var addressData,
  148. newShippingAddress;
  149.  
  150. this.source.set('params.invalid', false);
  151. this.triggerShippingDataValidateEvent();
  152.  
  153. if (!this.source.get('params.invalid')) {
  154. addressData = this.source.get('shippingAddress');
  155. // if user clicked the checkbox, its value is true or false. Need to convert.
  156. addressData['save_in_address_book'] = this.saveInAddressBook ? 1 : 0;
  157.  
  158. // New address must be selected as a shipping address
  159. newShippingAddress = createShippingAddress(addressData);
  160. selectShippingAddress(newShippingAddress);
  161. checkoutData.setSelectedShippingAddress(newShippingAddress.getKey());
  162. checkoutData.setNewCustomerShippingAddress($.extend(true, {}, addressData));
  163. this.getPopUp().closeModal();
  164. this.isNewAddressAdded(true);
  165. }
  166. },
  167.  
  168. /**
  169. * Shipping Method View
  170. */
  171. rates: shippingService.getShippingRates(),
  172. isLoading: shippingService.isLoading,
  173. isSelected: ko.computed(function () {
  174. return quote.shippingMethod() ?
  175. quote.shippingMethod()['carrier_code'] + '_' + quote.shippingMethod()['method_code'] :
  176. null;
  177. }),
  178.  
  179. /**
  180. * @param {Object} shippingMethod
  181. * @return {Boolean}
  182. */
  183. selectShippingMethod: function (shippingMethod) {
  184. selectShippingMethodAction(shippingMethod);
  185. checkoutData.setSelectedShippingRate(shippingMethod['carrier_code'] + '_' + shippingMethod['method_code']);
  186.  
  187. return true;
  188. },
  189.  
  190. /**
  191. * Set shipping information handler
  192. */
  193. setShippingInformation: function () {
  194. if (this.validateShippingInformation()) {
  195. setShippingInformationAction().done(
  196. function () {
  197. stepNavigator.next();
  198. }
  199. );
  200. }
  201. },
  202.  
  203. /**
  204. * @return {Boolean}
  205. */
  206. validateShippingInformation: function () {
  207. var shippingAddress,
  208. addressData,
  209. loginFormSelector = 'form[data-role=email-with-possible-login]',
  210. emailValidationResult = customer.isLoggedIn(),
  211. field;
  212.  
  213. if (!quote.shippingMethod()) {
  214. this.errorValidationMessage($t('Please specify a shipping method.'));
  215.  
  216. return false;
  217. }
  218.  
  219. if (!customer.isLoggedIn()) {
  220. $(loginFormSelector).validation();
  221. emailValidationResult = Boolean($(loginFormSelector + ' input[name=username]').valid());
  222. }
  223.  
  224. if (this.isFormInline) {
  225. this.source.set('params.invalid', false);
  226. this.triggerShippingDataValidateEvent();
  227.  
  228. if (emailValidationResult &&
  229. this.source.get('params.invalid') ||
  230. !quote.shippingMethod()['method_code'] ||
  231. !quote.shippingMethod()['carrier_code']
  232. ) {
  233. this.focusInvalid();
  234.  
  235. return false;
  236. }
  237.  
  238. shippingAddress = quote.shippingAddress();
  239. addressData = addressConverter.formAddressDataToQuoteAddress(
  240. this.source.get('shippingAddress')
  241. );
  242.  
  243. //Copy form data to quote shipping address object
  244. for (field in addressData) {
  245. if (addressData.hasOwnProperty(field) && //eslint-disable-line max-depth
  246. shippingAddress.hasOwnProperty(field) &&
  247. typeof addressData[field] != 'function' &&
  248. _.isEqual(shippingAddress[field], addressData[field])
  249. ) {
  250. shippingAddress[field] = addressData[field];
  251. } else if (typeof addressData[field] != 'function' &&
  252. !_.isEqual(shippingAddress[field], addressData[field])) {
  253. shippingAddress = addressData;
  254. break;
  255. }
  256. }
  257.  
  258. if (customer.isLoggedIn()) {
  259. shippingAddress['save_in_address_book'] = 1;
  260. }
  261. selectShippingAddress(shippingAddress);
  262. }
  263.  
  264. if (!emailValidationResult) {
  265. $(loginFormSelector + ' input[name=username]').focus();
  266.  
  267. return false;
  268. }
  269.  
  270. return true;
  271. },
  272.  
  273. /**
  274. * Trigger Shipping data Validate Event.
  275. */
  276. triggerShippingDataValidateEvent: function () {
  277. this.source.trigger('shippingAddress.data.validate');
  278.  
  279. if (this.source.get('shippingAddress.custom_attributes')) {
  280. this.source.trigger('shippingAddress.custom_attributes.data.validate');
  281. }
  282. }
  283. });
Add Comment
Please, Sign In to add comment