NumInput.vue 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. digits: { type: Number, default: 0 },
  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', this.string2number(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. created() {
  72. this.filteredValue = this.value;
  73. }
  74. string2number(value) {
  75. return Number.parseFloat(Number.parseFloat(value).toFixed(this.digits));
  76. }
  77. validate(value) {
  78. let n = this.string2number(value);
  79. if (isNaN(n))
  80. return false;
  81. if (n < this.min)
  82. return false;
  83. if (n > this.max)
  84. return false;
  85. return true;
  86. }
  87. plus() {
  88. const newValue = this.value + this.step;
  89. if (this.validate(newValue))
  90. this.filteredValue = newValue;
  91. }
  92. minus() {
  93. const newValue = this.value - this.step;
  94. if (this.validate(newValue))
  95. this.filteredValue = newValue;
  96. }
  97. onMouseDown(event, way) {
  98. this.startClickRepeat = true;
  99. this.clickRepeat = false;
  100. if (event.button == 0) {
  101. (async() => {
  102. await utils.sleep(300);
  103. if (this.startClickRepeat) {
  104. this.clickRepeat = true;
  105. while (this.clickRepeat) {
  106. if (way == 'plus') {
  107. this.plus();
  108. } else {
  109. this.minus();
  110. }
  111. await utils.sleep(50);
  112. }
  113. }
  114. })();
  115. }
  116. }
  117. onMouseUp() {
  118. if (this.inTouch)
  119. return;
  120. this.startClickRepeat = false;
  121. this.clickRepeat = false;
  122. }
  123. onTouchStart(event, way) {
  124. if (!this.$isMobileDevice)
  125. return;
  126. if (event.touches.length == 1) {
  127. this.inTouch = true;
  128. this.onMouseDown({button: 0}, way);
  129. }
  130. }
  131. onTouchEnd() {
  132. if (!this.$isMobileDevice)
  133. return;
  134. this.inTouch = false;
  135. this.onMouseUp();
  136. }
  137. }
  138. //-----------------------------------------------------------------------------
  139. </script>
  140. <style scoped>
  141. .no-mp {
  142. margin: 0;
  143. padding: 0;
  144. }
  145. .button {
  146. font-size: 130%;
  147. border-radius: 20px;
  148. color: #bbb;
  149. }
  150. .button:hover {
  151. color: #616161;
  152. background-color: #efebe9;
  153. }
  154. .error {
  155. background-color: #ffabab;
  156. border-radius: 3px;
  157. }
  158. .disable, .disable:hover {
  159. cursor: not-allowed;
  160. color: #bbb;
  161. background-color: white;
  162. }
  163. </style>