NumInput.vue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. {{ t }}
  37. </template>
  38. </q-input>
  39. </template>
  40. <script>
  41. //-----------------------------------------------------------------------------
  42. import Vue from 'vue';
  43. import Component from 'vue-class-component';
  44. import * as utils from '../../share/utils';
  45. const NumInputProps = Vue.extend({
  46. props: {
  47. value: Number,
  48. min: { type: Number, default: -Number.MAX_VALUE },
  49. max: { type: Number, default: Number.MAX_VALUE },
  50. step: { type: Number, default: 1 },
  51. }
  52. })
  53. export default @Component({
  54. watch: {
  55. filteredValue: function(newValue) {
  56. if (this.validate(newValue)) {
  57. this.error = false;
  58. this.$emit('input', Number.parseFloat(newValue));
  59. } else {
  60. this.error = true;
  61. }
  62. },
  63. value: function(newValue) {
  64. this.filteredValue = newValue;
  65. },
  66. }
  67. })
  68. class NumInput extends NumInputProps {
  69. filteredValue = 0;
  70. error = false;
  71. t = '';
  72. created() {
  73. this.mask = '#'.repeat(this.max.toString().length);
  74. this.filteredValue = this.value;
  75. }
  76. validate(value) {
  77. let n = Number.parseFloat(value);
  78. if (isNaN(n))
  79. return false;
  80. if (n < this.min)
  81. return false;
  82. if (n > this.max)
  83. return false;
  84. return true;
  85. }
  86. plus() {
  87. const newValue = this.value + this.step;
  88. if (this.validate(newValue))
  89. this.filteredValue = newValue;
  90. }
  91. minus() {
  92. const newValue = this.value - this.step;
  93. if (this.validate(newValue))
  94. this.filteredValue = newValue;
  95. }
  96. onMouseDown(event, way) {
  97. this.startClickRepeat = true;
  98. this.clickRepeat = false;
  99. if (event.button == 0) {
  100. (async() => {
  101. await utils.sleep(300);
  102. if (this.startClickRepeat) {
  103. this.clickRepeat = true;
  104. while (this.clickRepeat) {
  105. if (way == 'plus') {
  106. this.plus();
  107. } else {
  108. this.minus();
  109. }
  110. await utils.sleep(50);
  111. }
  112. }
  113. })();
  114. }
  115. }
  116. onMouseUp() {
  117. if (this.inTouch)
  118. return;
  119. this.startClickRepeat = false;
  120. this.clickRepeat = false;
  121. }
  122. onTouchStart(event, way) {
  123. if (!this.$isMobileDevice)
  124. return;
  125. if (event.touches.length == 1) {
  126. this.inTouch = true;
  127. this.onMouseDown({button: 0}, way);
  128. }
  129. }
  130. onTouchEnd() {
  131. if (!this.$isMobileDevice)
  132. return;
  133. this.inTouch = false;
  134. this.onMouseUp();
  135. }
  136. }
  137. //-----------------------------------------------------------------------------
  138. </script>
  139. <style scoped>
  140. .no-mp {
  141. margin: 0;
  142. padding: 0;
  143. }
  144. .button {
  145. font-size: 130%;
  146. border-radius: 20px;
  147. color: #bbb;
  148. }
  149. .button:hover {
  150. color: #616161;
  151. background-color: #efebe9;
  152. }
  153. .error {
  154. background-color: #ffabab;
  155. border-radius: 3px;
  156. }
  157. .disable, .disable:hover {
  158. cursor: not-allowed;
  159. color: #bbb;
  160. background-color: white;
  161. }
  162. </style>