NumInput.vue 4.8 KB

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