NumInput.vue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <template>
  2. <q-input outlined dense
  3. v-model="filteredValue"
  4. input-style="text-align: center"
  5. class="no-mp"
  6. :class="(error ? 'error' : '')"
  7. >
  8. <slot></slot>
  9. <template v-slot:prepend>
  10. <q-icon :class="(validate(value - step) ? '' : 'disable')"
  11. name="la la-minus-circle"
  12. class="button"
  13. v-ripple="validate(value - step)"
  14. @click="minus"
  15. @mousedown.prevent.stop="onMouseDown($event, 'minus')"
  16. @mouseup.prevent.stop="onMouseUp"
  17. @mouseout.prevent.stop="onMouseUp"
  18. @touchstart.stop="onTouchStart($event, 'minus')"
  19. @touchend.stop="onTouchEnd"
  20. @touchcancel.prevent.stop="onTouchEnd"
  21. />
  22. </template>
  23. <template v-slot:append>
  24. <q-icon :class="(validate(value + step) ? '' : 'disable')"
  25. name="la la-plus-circle"
  26. class="button"
  27. v-ripple="validate(value + step)"
  28. @click="plus"
  29. @mousedown.prevent.stop="onMouseDown($event, 'plus')"
  30. @mouseup.prevent.stop="onMouseUp"
  31. @mouseout.prevent.stop="onMouseUp"
  32. @touchstart.stop="onTouchStart($event, 'plus')"
  33. @touchend.stop="onTouchEnd"
  34. @touchcancel.prevent.stop="onTouchEnd"
  35. />
  36. </template>
  37. </q-input>
  38. </template>
  39. <script>
  40. //-----------------------------------------------------------------------------
  41. import Vue from 'vue';
  42. import Component from 'vue-class-component';
  43. import * as utils from '../../share/utils';
  44. const NumInputProps = Vue.extend({
  45. props: {
  46. value: Number,
  47. min: { type: Number, default: -Number.MAX_VALUE },
  48. max: { type: Number, default: Number.MAX_VALUE },
  49. step: { type: Number, default: 1 },
  50. }
  51. })
  52. export default @Component({
  53. watch: {
  54. filteredValue: function(newValue) {
  55. if (this.validate(newValue)) {
  56. this.error = false;
  57. this.$emit('input', Number.parseFloat(newValue));
  58. } else {
  59. this.error = true;
  60. }
  61. },
  62. value: function(newValue) {
  63. this.filteredValue = newValue;
  64. },
  65. }
  66. })
  67. class NumInput extends NumInputProps {
  68. filteredValue = 0;
  69. error = false;
  70. created() {
  71. this.mask = '#'.repeat(this.max.toString().length);
  72. this.filteredValue = this.value;
  73. }
  74. validate(value) {
  75. let n = Number.parseFloat(value);
  76. if (isNaN(n))
  77. return false;
  78. if (n < this.min)
  79. return false;
  80. if (n > this.max)
  81. return false;
  82. return true;
  83. }
  84. plus() {
  85. const newValue = this.value + this.step;
  86. if (this.validate(newValue))
  87. this.filteredValue = newValue;
  88. }
  89. minus() {
  90. const newValue = this.value - this.step;
  91. if (this.validate(newValue))
  92. this.filteredValue = newValue;
  93. }
  94. onMouseDown(event, way) {
  95. this.startClickRepeat = true;
  96. this.clickRepeat = false;
  97. if (event.button == 0) {
  98. (async() => {
  99. await utils.sleep(300);
  100. if (this.startClickRepeat) {
  101. this.clickRepeat = true;
  102. while (this.clickRepeat) {
  103. if (way == 'plus') {
  104. this.plus();
  105. } else {
  106. this.minus();
  107. }
  108. await utils.sleep(50);
  109. }
  110. }
  111. })();
  112. }
  113. }
  114. onMouseUp() {
  115. if (this.inTouch)
  116. return;
  117. this.startClickRepeat = false;
  118. this.clickRepeat = false;
  119. }
  120. onTouchStart(event, way) {
  121. if (!this.$isMobileDevice)
  122. return;
  123. if (event.touches.length == 1) {
  124. this.inTouch = true;
  125. this.onMouseDown({button: 0}, way);
  126. }
  127. }
  128. onTouchEnd() {
  129. if (!this.$isMobileDevice)
  130. return;
  131. this.inTouch = false;
  132. this.onMouseUp();
  133. }
  134. }
  135. //-----------------------------------------------------------------------------
  136. </script>
  137. <style scoped>
  138. .no-mp {
  139. margin: 0;
  140. padding: 0;
  141. }
  142. .button {
  143. font-size: 130%;
  144. border-radius: 20px;
  145. color: #bbb;
  146. }
  147. .button:hover {
  148. color: #616161;
  149. background-color: #efebe9;
  150. }
  151. .error {
  152. background-color: #ffabab;
  153. border-radius: 3px;
  154. }
  155. .disable, .disable:hover {
  156. cursor: not-allowed;
  157. color: #bbb;
  158. background-color: white;
  159. }
  160. </style>