address.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /**
  2. * Copyright © Magento, Inc. All rights reserved.
  3. * See COPYING.txt for license details.
  4. */
  5. define([
  6. 'jquery',
  7. 'Magento_Ui/js/modal/confirm',
  8. 'jquery/ui',
  9. 'mage/translate'
  10. ], function ($, confirm) {
  11. 'use strict';
  12. $.widget('mage.address', {
  13. /**
  14. * Options common to all instances of this widget.
  15. * @type {Object}
  16. */
  17. options: {
  18. deleteConfirmMessage: $.mage.__('Are you sure you want to delete this address?')
  19. },
  20. /**
  21. * Bind event handlers for adding and deleting addresses.
  22. * @private
  23. */
  24. _create: function () {
  25. var options = this.options,
  26. addAddress = options.addAddress,
  27. deleteAddress = options.deleteAddress;
  28. if (addAddress) {
  29. $(document).on('click', addAddress, this._addAddress.bind(this));
  30. }
  31. if (deleteAddress) {
  32. $(document).on('click', deleteAddress, this._deleteAddress.bind(this));
  33. }
  34. },
  35. /**
  36. * Add a new address.
  37. * @private
  38. */
  39. _addAddress: function () {
  40. window.location = this.options.addAddressLocation;
  41. },
  42. /**
  43. * Delete the address whose id is specified in a data attribute after confirmation from the user.
  44. * @private
  45. * @param {jQuery.Event} e
  46. * @return {Boolean}
  47. */
  48. _deleteAddress: function (e) {
  49. var self = this;
  50. confirm({
  51. content: this.options.deleteConfirmMessage,
  52. actions: {
  53. /** @inheritdoc */
  54. confirm: function () {
  55. if (typeof $(e.target).parent().data('address') !== 'undefined') {
  56. window.location = self.options.deleteUrlPrefix + $(e.target).parent().data('address') +
  57. '/form_key/' + $.mage.cookies.get('form_key');
  58. } else {
  59. window.location = self.options.deleteUrlPrefix + $(e.target).data('address') +
  60. '/form_key/' + $.mage.cookies.get('form_key');
  61. }
  62. }
  63. }
  64. });
  65. return false;
  66. }
  67. });
  68. return $.mage.address;
  69. });