Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

352 строки
10 KiB

  1. /**
  2. * Copyright © Magento, Inc. All rights reserved.
  3. * See COPYING.txt for license details.
  4. */
  5. define([
  6. 'jquery',
  7. 'Magento_Customer/js/model/authentication-popup',
  8. 'Magento_Customer/js/customer-data',
  9. 'Magento_Ui/js/modal/alert',
  10. 'Magento_Ui/js/modal/confirm',
  11. 'underscore',
  12. 'jquery-ui-modules/widget',
  13. 'mage/decorate',
  14. 'mage/collapsible',
  15. 'mage/cookies',
  16. 'jquery-ui-modules/effect-fade'
  17. ], function ($, authenticationPopup, customerData, alert, confirm, _) {
  18. 'use strict';
  19. $.widget('mage.sidebar', {
  20. options: {
  21. isRecursive: true,
  22. minicart: {
  23. maxItemsVisible: 3
  24. }
  25. },
  26. scrollHeight: 0,
  27. shoppingCartUrl: window.checkout.shoppingCartUrl,
  28. /**
  29. * Create sidebar.
  30. * @private
  31. */
  32. _create: function () {
  33. this._initContent();
  34. },
  35. /**
  36. * Update sidebar block.
  37. */
  38. update: function () {
  39. $(this.options.targetElement).trigger('contentUpdated');
  40. this._calcHeight();
  41. },
  42. /**
  43. * @private
  44. */
  45. _initContent: function () {
  46. var self = this,
  47. events = {};
  48. this.element.decorate('list', this.options.isRecursive);
  49. /**
  50. * @param {jQuery.Event} event
  51. */
  52. events['click ' + this.options.button.close] = function (event) {
  53. event.stopPropagation();
  54. $(self.options.targetElement).dropdownDialog('close');
  55. };
  56. events['click ' + this.options.button.checkout] = $.proxy(function () {
  57. var cart = customerData.get('cart'),
  58. customer = customerData.get('customer'),
  59. element = $(this.options.button.checkout);
  60. if (!customer().firstname && cart().isGuestCheckoutAllowed === false) {
  61. // set URL for redirect on successful login/registration. It's postprocessed on backend.
  62. $.cookie('login_redirect', this.options.url.checkout);
  63. if (this.options.url.isRedirectRequired) {
  64. element.prop('disabled', true);
  65. location.href = this.options.url.loginUrl;
  66. } else {
  67. authenticationPopup.showModal();
  68. }
  69. return false;
  70. }
  71. element.prop('disabled', true);
  72. location.href = this.options.url.checkout;
  73. }, this);
  74. /**
  75. * @param {jQuery.Event} event
  76. */
  77. events['click ' + this.options.button.remove] = function (event) {
  78. event.stopPropagation();
  79. confirm({
  80. content: self.options.confirmMessage,
  81. actions: {
  82. /** @inheritdoc */
  83. confirm: function () {
  84. self._removeItem($(event.currentTarget));
  85. },
  86. /** @inheritdoc */
  87. always: function (e) {
  88. e.stopImmediatePropagation();
  89. }
  90. }
  91. });
  92. };
  93. /**
  94. * @param {jQuery.Event} event
  95. */
  96. events['keyup ' + this.options.item.qty] = function (event) {
  97. self._showItemButton($(event.target));
  98. };
  99. /**
  100. * @param {jQuery.Event} event
  101. */
  102. events['change ' + this.options.item.qty] = function (event) {
  103. self._showItemButton($(event.target));
  104. };
  105. /**
  106. * @param {jQuery.Event} event
  107. */
  108. events['click ' + this.options.item.button] = function (event) {
  109. event.stopPropagation();
  110. self._updateItemQty($(event.currentTarget));
  111. };
  112. /**
  113. * @param {jQuery.Event} event
  114. */
  115. events['focusout ' + this.options.item.qty] = function (event) {
  116. self._validateQty($(event.currentTarget));
  117. };
  118. this._on(this.element, events);
  119. this._calcHeight();
  120. },
  121. /**
  122. * @param {HTMLElement} elem
  123. * @private
  124. */
  125. _showItemButton: function (elem) {
  126. var itemId = elem.data('cart-item'),
  127. itemQty = elem.data('item-qty');
  128. if (this._isValidQty(itemQty, elem.val())) {
  129. $('#update-cart-item-' + itemId).show('fade', 300);
  130. } else if (elem.val() == 0) { //eslint-disable-line eqeqeq
  131. this._hideItemButton(elem);
  132. } else {
  133. this._hideItemButton(elem);
  134. }
  135. },
  136. /**
  137. * @param {*} origin - origin qty. 'data-item-qty' attribute.
  138. * @param {*} changed - new qty.
  139. * @returns {Boolean}
  140. * @private
  141. */
  142. _isValidQty: function (origin, changed) {
  143. return origin != changed && //eslint-disable-line eqeqeq
  144. changed.length > 0 &&
  145. changed - 0 == changed && //eslint-disable-line eqeqeq
  146. changed - 0 > 0;
  147. },
  148. /**
  149. * @param {Object} elem
  150. * @private
  151. */
  152. _validateQty: function (elem) {
  153. var itemQty = elem.data('item-qty');
  154. if (!this._isValidQty(itemQty, elem.val())) {
  155. elem.val(itemQty);
  156. }
  157. },
  158. /**
  159. * @param {HTMLElement} elem
  160. * @private
  161. */
  162. _hideItemButton: function (elem) {
  163. var itemId = elem.data('cart-item');
  164. $('#update-cart-item-' + itemId).hide('fade', 300);
  165. },
  166. /**
  167. * @param {HTMLElement} elem
  168. * @private
  169. */
  170. _updateItemQty: function (elem) {
  171. var itemId = elem.data('cart-item');
  172. this._ajax(this.options.url.update, {
  173. 'item_id': itemId,
  174. 'item_qty': $('#cart-item-' + itemId + '-qty').val()
  175. }, elem, this._updateItemQtyAfter);
  176. },
  177. /**
  178. * Update content after update qty
  179. *
  180. * @param {HTMLElement} elem
  181. */
  182. _updateItemQtyAfter: function (elem) {
  183. var productData = this._getProductById(Number(elem.data('cart-item')));
  184. if (!_.isUndefined(productData)) {
  185. $(document).trigger('ajax:updateCartItemQty');
  186. if (window.location.href === this.shoppingCartUrl) {
  187. window.location.reload(false);
  188. }
  189. }
  190. this._hideItemButton(elem);
  191. },
  192. /**
  193. * @param {HTMLElement} elem
  194. * @private
  195. */
  196. _removeItem: function (elem) {
  197. var itemId = elem.data('cart-item');
  198. this._ajax(this.options.url.remove, {
  199. 'item_id': itemId
  200. }, elem, this._removeItemAfter);
  201. },
  202. /**
  203. * Update content after item remove
  204. *
  205. * @param {Object} elem
  206. * @private
  207. */
  208. _removeItemAfter: function (elem) {
  209. var productData = this._getProductById(Number(elem.data('cart-item')));
  210. if (!_.isUndefined(productData)) {
  211. $(document).trigger('ajax:removeFromCart', {
  212. productIds: [productData['product_id']],
  213. productInfo: [
  214. {
  215. 'id': productData['product_id']
  216. }
  217. ]
  218. });
  219. if (window.location.href.indexOf(this.shoppingCartUrl) === 0) {
  220. window.location.reload();
  221. }
  222. }
  223. },
  224. /**
  225. * Retrieves product data by Id.
  226. *
  227. * @param {Number} productId - product Id
  228. * @returns {Object|undefined}
  229. * @private
  230. */
  231. _getProductById: function (productId) {
  232. return _.find(customerData.get('cart')().items, function (item) {
  233. return productId === Number(item['item_id']);
  234. });
  235. },
  236. /**
  237. * @param {String} url - ajax url
  238. * @param {Object} data - post data for ajax call
  239. * @param {Object} elem - element that initiated the event
  240. * @param {Function} callback - callback method to execute after AJAX success
  241. */
  242. _ajax: function (url, data, elem, callback) {
  243. $.extend(data, {
  244. 'form_key': $.mage.cookies.get('form_key')
  245. });
  246. $.ajax({
  247. url: url,
  248. data: data,
  249. type: 'post',
  250. dataType: 'json',
  251. context: this,
  252. /** @inheritdoc */
  253. beforeSend: function () {
  254. elem.attr('disabled', 'disabled');
  255. },
  256. /** @inheritdoc */
  257. complete: function () {
  258. elem.attr('disabled', null);
  259. }
  260. })
  261. .done(function (response) {
  262. var msg;
  263. if (response.success) {
  264. callback.call(this, elem, response);
  265. } else {
  266. msg = response['error_message'];
  267. if (msg) {
  268. alert({
  269. content: msg
  270. });
  271. }
  272. }
  273. })
  274. .fail(function (error) {
  275. console.log(JSON.stringify(error));
  276. });
  277. },
  278. /**
  279. * Calculate height of minicart list
  280. *
  281. * @private
  282. */
  283. _calcHeight: function () {
  284. var self = this,
  285. height = 0,
  286. counter = this.options.minicart.maxItemsVisible,
  287. target = $(this.options.minicart.list),
  288. outerHeight;
  289. self.scrollHeight = 0;
  290. target.children().each(function () {
  291. if ($(this).find('.options').length > 0) {
  292. $(this).collapsible();
  293. }
  294. outerHeight = $(this).outerHeight(true);
  295. if (counter-- > 0) {
  296. height += outerHeight;
  297. }
  298. self.scrollHeight += outerHeight;
  299. });
  300. target.parent().height(height);
  301. }
  302. });
  303. return $.mage.sidebar;
  304. });