gift-options.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /**
  2. * Copyright © Magento, Inc. All rights reserved.
  3. * See COPYING.txt for license details.
  4. */
  5. define([
  6. 'jquery',
  7. 'jquery/ui'
  8. ], function ($) {
  9. 'use strict';
  10. $.widget('mage.giftOptions', {
  11. options: {
  12. mageError: 'mage-error',
  13. noDisplay: 'no-display',
  14. requiredEntry: 'required-entry'
  15. },
  16. /**
  17. * Initial toggle of the various gift options after widget instantiation.
  18. * @private
  19. */
  20. _init: function () {
  21. this._toggleVisibility();
  22. },
  23. /**
  24. * Bind a click handler to the widget's context element.
  25. * @private
  26. */
  27. _create: function () {
  28. this.element.on('click', $.proxy(this._toggleVisibility, this));
  29. $(this.element.data('selector').id).find('.giftmessage-area')
  30. .on('change', $.proxy(this._toggleRequired, this));
  31. },
  32. /**
  33. * Toggle the visibility of the widget's context element's selector(s).
  34. * @private
  35. * @param {jQuery.Event} event - Click event. Target is a checkbox.
  36. */
  37. _toggleVisibility: function (event) {
  38. var checkbox = event ? $(event.target) : this.element,
  39. container = $(checkbox.data('selector').id),
  40. _this;
  41. if (checkbox.is(':checked')) {
  42. container.show()
  43. .find('.giftmessage-area:not(:visible)').each(function (x, element) {
  44. if ($(element).val().length > 0) {
  45. $(element).change();
  46. container.find('a').click();
  47. }
  48. });
  49. } else {
  50. _this = this;
  51. container.hide()
  52. .find('.input-text:not(.giftmessage-area)').each(function (x, element) {
  53. $(element).val(element.defaultValue).removeClass(_this.options.mageError)
  54. .next('div.' + _this.options.mageError).remove();
  55. }).end()
  56. .find('.giftmessage-area').val('').change().end()
  57. .find('.select').val('').change().end()
  58. .find('.checkbox:checked').prop('checked', false).click().prop('checked', false).end()
  59. .find('.price-box').addClass(this.options.noDisplay).end();
  60. }
  61. },
  62. /**
  63. * Make the From and To input fields required if a gift message has been written.
  64. * @private
  65. * @param {jQuery.Event} event - Change event. Target is a textarea.
  66. */
  67. _toggleRequired: function (event) {
  68. var textArea = $(event.target),
  69. length = textArea.val().length;
  70. textArea.closest('li').prev('.fields')
  71. .find('.input-text').toggleClass(this.options.requiredEntry, length > 0);
  72. }
  73. });
  74. return $.mage.giftOptions;
  75. });