vault.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /**
  2. * Copyright © Magento, Inc. All rights reserved.
  3. * See COPYING.txt for license details.
  4. */
  5. /*browser:true*/
  6. /*global define*/
  7. /* @api */
  8. define([
  9. 'jquery',
  10. 'uiComponent'
  11. ], function ($, Class) {
  12. 'use strict';
  13. return Class.extend({
  14. defaults: {
  15. $selector: null,
  16. selector: 'edit_form',
  17. fieldset: '',
  18. active: false,
  19. imports: {
  20. onActiveChange: 'active'
  21. }
  22. },
  23. /**
  24. * Set list of observable attributes
  25. * @returns {exports.initObservable}
  26. */
  27. initObservable: function () {
  28. var self = this;
  29. self.$selector = $('#' + self.selector);
  30. this._super()
  31. .observe(['active']);
  32. // re-init payment method events
  33. self.$selector.off('changePaymentMethod.' + this.getCode())
  34. .on('changePaymentMethod.' + this.getCode(), this.changePaymentMethod.bind(this));
  35. if (this.active()) {
  36. $('#' + this.fieldset + ' input:radio:first').trigger('click');
  37. }
  38. return this;
  39. },
  40. /**
  41. * Enable/disable current payment method
  42. * @param {Object} event
  43. * @param {String} method
  44. * @returns {exports.changePaymentMethod}
  45. */
  46. changePaymentMethod: function (event, method) {
  47. this.active(method === this.getCode());
  48. return this;
  49. },
  50. /**
  51. * Triggered when payment changed
  52. * @param {Boolean} isActive
  53. */
  54. onActiveChange: function (isActive) {
  55. if (!isActive) {
  56. this.$selector.trigger('setVaultNotActive.' + this.getCode());
  57. return;
  58. }
  59. $('#' + this.fieldset + ' input:radio:first').trigger('click');
  60. window.order.addExcludedPaymentMethod(this.getCode());
  61. },
  62. /**
  63. * Get payment method code
  64. * @returns {String}
  65. */
  66. getCode: function () {
  67. return this.code;
  68. }
  69. });
  70. });