trim-input.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /**
  2. * Copyright © Magento, Inc. All rights reserved.
  3. * See COPYING.txt for license details.
  4. */
  5. define([
  6. 'jquery'
  7. ], function ($) {
  8. 'use strict';
  9. $.widget('mage.trimInput', {
  10. options: {
  11. cache: {}
  12. },
  13. /**
  14. * Widget initialization
  15. * @private
  16. */
  17. _create: function () {
  18. this.options.cache.input = $(this.element);
  19. this._bind();
  20. },
  21. /**
  22. * Event binding, will monitor change, keyup and paste events.
  23. * @private
  24. */
  25. _bind: function () {
  26. if (this.options.cache.input.length) {
  27. this._on(this.options.cache.input, {
  28. 'change': this._trimInput,
  29. 'keyup': this._trimInput,
  30. 'paste': this._trimInput
  31. });
  32. }
  33. },
  34. /**
  35. * Trim value
  36. * @private
  37. */
  38. _trimInput: function () {
  39. var input = this._getInputValue().trim();
  40. this.options.cache.input.val(input);
  41. },
  42. /**
  43. * Get input value
  44. * @returns {*}
  45. * @private
  46. */
  47. _getInputValue: function () {
  48. return this.options.cache.input.val();
  49. }
  50. });
  51. return $.mage.trimInput;
  52. });