bootstrap-number-input.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /* ========================================================================
  2. * bootstrap-spin - v1.0
  3. * https://github.com/wpic/bootstrap-spin
  4. * ========================================================================
  5. * Copyright 2014 WPIC, Hamed Abdollahpour
  6. *
  7. * ========================================================================
  8. * Licensed under the Apache License, Version 2.0 (the "License");
  9. * you may not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. * ========================================================================
  20. */
  21. (function ($) {
  22. $.fn.bootstrapNumber = function (options) {
  23. var settings = $.extend({
  24. upClass: 'default',
  25. downClass: 'default',
  26. center: true
  27. }, options);
  28. return this.each(function (e) {
  29. var self = $(this);
  30. var clone = self.clone();
  31. var min = self.attr('min');
  32. var max = self.attr('max');
  33. function getVal() {
  34. var val = clone.val();
  35. if (! val || val === "NaN") {
  36. return 0;
  37. }
  38. return parseInt(val);
  39. }
  40. function setText(n) {
  41. if ((min && n < min) || (max && n > max)) {
  42. return false;
  43. }
  44. clone.focus().val(n);
  45. clone.trigger('change');
  46. return true;
  47. }
  48. var group = $("<div class='input-group'></div>");
  49. var down = $("<button type='button'>-</button>").attr({'class': 'btn btn-' +settings.downClass, 'disabled': options.disabled}).click(function () {
  50. setText(getVal() - 1);
  51. });
  52. var up = $("<button type='button'>+</button>").attr({'class': 'btn btn-' +settings.upClass, 'disabled': options.disabled}).click(function () {
  53. setText(getVal() + 1);
  54. });
  55. $("<span class='input-group-btn'></span>").append(down).appendTo(group);
  56. clone.appendTo(group);
  57. if (clone) {
  58. clone.css('text-align', 'center');
  59. }
  60. $("<span class='input-group-btn'></span>").append(up).appendTo(group);
  61. // remove spins from original
  62. clone.prop('type', 'text').keydown(function (e) {
  63. if ($.inArray(e.keyCode, [46, 8, 9, 27, 13, 110, 190]) !== -1 ||
  64. (e.keyCode == 65 && e.ctrlKey === true) ||
  65. (e.keyCode >= 35 && e.keyCode <= 39)) {
  66. return;
  67. }
  68. if ((e.shiftKey || (e.keyCode < 48 || e.keyCode > 57)) && (e.keyCode < 96 || e.keyCode > 105)) {
  69. e.preventDefault();
  70. }
  71. var c = String.fromCharCode(e.which);
  72. var n = (getVal() + c);
  73. //if ((min && n < min) || (max && n > max)) {
  74. // e.preventDefault();
  75. //}
  76. });
  77. clone.prop('type', 'text').blur(function (e) {
  78. var c = parseInt(String.fromCharCode(e.which));
  79. if (isNaN(c)) {
  80. c = 0;
  81. }
  82. var n = getVal() + c;
  83. if ((min && n < min)) {
  84. setText(min);
  85. }
  86. else if (max && n > max) {
  87. setText(max);
  88. }
  89. });
  90. self.replaceWith(group);
  91. });
  92. };
  93. }(jQuery));