25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

72 satır
1.8 KiB

  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. // Safari caret position workaround: storing carter position
  40. var caretStart, caretEnd, input;
  41. caretStart = this.options.cache.input.get(0).selectionStart;
  42. caretEnd = this.options.cache.input.get(0).selectionEnd;
  43. input = this._getInputValue().trim();
  44. this.options.cache.input.val(input);
  45. // Safari caret position workaround: setting caret position to previously stored values
  46. if (caretStart !== null && caretEnd !== null) {
  47. this.options.cache.input.get(0).setSelectionRange(caretStart, caretEnd);
  48. }
  49. },
  50. /**
  51. * Get input value
  52. * @returns {*}
  53. * @private
  54. */
  55. _getInputValue: function () {
  56. return this.options.cache.input.val();
  57. }
  58. });
  59. return $.mage.trimInput;
  60. });