bootstrap-number-input.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. <<<<<<< HEAD
  50. var down = $("<button type='button'>-</button>").attr({'class': 'btn btn-' +settings.downClass, 'disabled': options.disabled}).click(function () {
  51. setText(getVal() - 1);
  52. });
  53. var up = $("<button type='button'>+</button>").attr({'class': 'btn btn-' +settings.upClass, 'disabled': options.disabled}).click(function () {
  54. =======
  55. var down = $("<button type='button'>-</button>").attr({
  56. 'class': 'btn btn-' + settings.downClass,
  57. 'disabled': options.disabled
  58. }).click(function () {
  59. setText(getVal() - 1);
  60. });
  61. var up = $("<button type='button'>+</button>").attr({
  62. 'class': 'btn btn-' + settings.upClass,
  63. 'disabled': options.disabled
  64. }).click(function () {
  65. >>>>>>> dev
  66. setText(getVal() + 1);
  67. });
  68. $("<span class='input-group-btn'></span>").append(down).appendTo(group);
  69. clone.appendTo(group);
  70. if (clone) {
  71. clone.css('text-align', 'center');
  72. }
  73. $("<span class='input-group-btn'></span>").append(up).appendTo(group);
  74. // remove spins from original
  75. clone.prop('type', 'text').keydown(function (e) {
  76. if ($.inArray(e.keyCode, [46, 8, 9, 27, 13, 110, 190]) !== -1 ||
  77. (e.keyCode == 65 && e.ctrlKey === true) ||
  78. (e.keyCode >= 35 && e.keyCode <= 39)) {
  79. return;
  80. }
  81. if ((e.shiftKey || (e.keyCode < 48 || e.keyCode > 57)) && (e.keyCode < 96 || e.keyCode > 105)) {
  82. e.preventDefault();
  83. }
  84. var c = String.fromCharCode(e.which);
  85. var n = (getVal() + c);
  86. //if ((min && n < min) || (max && n > max)) {
  87. // e.preventDefault();
  88. //}
  89. });
  90. clone.prop('type', 'text').blur(function (e) {
  91. var c = parseInt(String.fromCharCode(e.which));
  92. if (isNaN(c)) {
  93. c = 0;
  94. }
  95. var n = getVal() + c;
  96. if ((min && n < min)) {
  97. setText(min);
  98. }
  99. else if (max && n > max) {
  100. setText(max);
  101. }
  102. });
  103. self.replaceWith(group);
  104. });
  105. };
  106. }(jQuery));