You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

88 lines
2.3 KiB

  1. (function($K)
  2. {
  3. $K.add('module', 'number', {
  4. init: function(app, context)
  5. {
  6. this.app = app;
  7. // context
  8. this.context = context;
  9. this.$element = context.getElement();
  10. },
  11. // public
  12. start: function()
  13. {
  14. this.$input = this.$element.find('input[type="number"]');
  15. this.$btnUp = this.$element.find('.is-up');
  16. this.$btnDown = this.$element.find('.is-down');
  17. this._buildStep();
  18. this._buildMin();
  19. this._buildMax();
  20. if (!this._isDisabled())
  21. {
  22. this.$btnUp.on('click.kube.number', this._increase.bind(this));
  23. this.$btnDown.on('click.kube.number', this._decrease.bind(this));
  24. }
  25. },
  26. stop: function()
  27. {
  28. this.$btnUp.off('.kube.number');
  29. this.$btnDown.off('.kube.number');
  30. },
  31. // private
  32. _buildStep: function()
  33. {
  34. var step = this.$input.attr('step');
  35. this.step = (step) ? parseFloat(step) : 1;
  36. },
  37. _buildMin: function()
  38. {
  39. var min = this.$input.attr('min');
  40. this.min = (min) ? parseFloat(min) : false;
  41. },
  42. _buildMax: function()
  43. {
  44. var max = this.$input.attr('max');
  45. this.max = (max) ? parseFloat(max) : false;
  46. },
  47. _isDisabled: function()
  48. {
  49. return this.$input.attr('disabled');
  50. },
  51. _getValue: function()
  52. {
  53. var value = parseFloat(this.$input.val());
  54. var min = (this.min === false) ? 0 : this.min;
  55. return (isNaN(value)) ? min : value;
  56. },
  57. _increase: function(e)
  58. {
  59. if (e)
  60. {
  61. e.preventDefault();
  62. e.stopPropagation();
  63. }
  64. var oldValue = this._getValue();
  65. var newVal = (this.max !== false && oldValue >= this.max) ? oldValue : oldValue + this.step;
  66. this.$input.val(newVal);
  67. },
  68. _decrease: function(e)
  69. {
  70. if (e)
  71. {
  72. e.preventDefault();
  73. e.stopPropagation();
  74. }
  75. var oldValue = this._getValue();
  76. var newVal = (this.min !== false && oldValue <= this.min) ? oldValue : oldValue - this.step;
  77. this.$input.val(newVal);
  78. }
  79. });
  80. })(Kube);