NumInput.vue 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <template>
  2. <q-input
  3. v-model="filteredValue"
  4. outlined dense
  5. input-style="text-align: center"
  6. class="no-mp"
  7. :class="(error ? 'error' : '')"
  8. :disable="disable"
  9. >
  10. <slot></slot>
  11. <template #prepend>
  12. <q-icon
  13. v-ripple="validate(modelValue - step)"
  14. :class="(validate(modelValue - step) ? '' : 'disable')"
  15. :name="minusIcon"
  16. class="button"
  17. @click="onClick('minus')"
  18. @mousedown.prevent.stop="onMouseDown($event, 'minus')"
  19. @mouseup.prevent.stop="onMouseUp"
  20. @mouseout.prevent.stop="onMouseUp"
  21. @touchstart.stop="onTouchStart($event, 'minus')"
  22. @touchend.stop="onTouchEnd"
  23. @touchcancel.prevent.stop="onTouchEnd"
  24. />
  25. </template>
  26. <template #append>
  27. <q-icon
  28. v-ripple="validate(modelValue + step)"
  29. :class="(validate(modelValue + step) ? '' : 'disable')"
  30. :name="plusIcon"
  31. class="button"
  32. @click="onClick('plus')"
  33. @mousedown.prevent.stop="onMouseDown($event, 'plus')"
  34. @mouseup.prevent.stop="onMouseUp"
  35. @mouseout.prevent.stop="onMouseUp"
  36. @touchstart.stop="onTouchStart($event, 'plus')"
  37. @touchend.stop="onTouchEnd"
  38. @touchcancel.prevent.stop="onTouchEnd"
  39. />
  40. </template>
  41. </q-input>
  42. </template>
  43. <script>
  44. //-----------------------------------------------------------------------------
  45. import vueComponent from '../vueComponent.js';
  46. import * as utils from '../../share/utils';
  47. const componentOptions = {
  48. watch: {
  49. filteredValue() {
  50. this.checkErrorAndEmit(true);
  51. },
  52. modelValue(newValue) {
  53. this.filteredValue = newValue;
  54. },
  55. min() {
  56. this.checkErrorAndEmit();
  57. },
  58. max() {
  59. this.checkErrorAndEmit();
  60. }
  61. }
  62. };
  63. class NumInput {
  64. _options = componentOptions;
  65. _props = {
  66. modelValue: Number,
  67. min: { type: Number, default: -Number.MAX_VALUE },
  68. max: { type: Number, default: Number.MAX_VALUE },
  69. step: { type: Number, default: 1 },
  70. digits: { type: Number, default: 0 },
  71. disable: Boolean,
  72. minusIcon: {type: String, default: 'la la-minus-circle'},
  73. plusIcon: {type: String, default: 'la la-plus-circle'},
  74. };
  75. filteredValue = 0;
  76. error = false;
  77. created() {
  78. this.filteredValue = this.modelValue;
  79. }
  80. string2number(value) {
  81. return Number.parseFloat(Number.parseFloat(value).toFixed(this.digits));
  82. }
  83. validate(value) {
  84. let n = this.string2number(value);
  85. if (isNaN(n))
  86. return false;
  87. if (n < this.min)
  88. return false;
  89. if (n > this.max)
  90. return false;
  91. return true;
  92. }
  93. checkErrorAndEmit(emit = false) {
  94. if (this.validate(this.filteredValue)) {
  95. this.error = false;
  96. if (emit)
  97. this.$emit('update:modelValue', this.string2number(this.filteredValue));
  98. } else {
  99. this.error = true;
  100. }
  101. }
  102. plus() {
  103. const newValue = this.modelValue + this.step;
  104. if (this.validate(newValue))
  105. this.filteredValue = newValue;
  106. }
  107. minus() {
  108. const newValue = this.modelValue - this.step;
  109. if (this.validate(newValue))
  110. this.filteredValue = newValue;
  111. }
  112. onClick(way) {
  113. if (this.clickRepeat)
  114. return;
  115. if (way == 'plus') {
  116. this.plus();
  117. } else {
  118. this.minus();
  119. }
  120. }
  121. onMouseDown(event, way) {
  122. this.startClickRepeat = true;
  123. this.clickRepeat = false;
  124. if (event.button == 0) {
  125. (async() => {
  126. if (this.inRepeatFunc)
  127. return;
  128. this.inRepeatFunc = true;
  129. try {
  130. await utils.sleep(300);
  131. if (this.startClickRepeat) {
  132. this.clickRepeat = true;
  133. while (this.clickRepeat) {
  134. if (way == 'plus') {
  135. this.plus();
  136. } else {
  137. this.minus();
  138. }
  139. await utils.sleep(200);
  140. }
  141. }
  142. } finally {
  143. this.inRepeatFunc = false;
  144. }
  145. })();
  146. }
  147. }
  148. onMouseUp() {
  149. if (this.inTouch)
  150. return;
  151. this.startClickRepeat = false;
  152. if (this.clickRepeat) {
  153. (async() => {
  154. await utils.sleep(50);
  155. this.clickRepeat = false;
  156. })();
  157. }
  158. }
  159. onTouchStart(event, way) {
  160. if (!this.$root.isMobileDevice)
  161. return;
  162. if (event.touches.length == 1) {
  163. this.inTouch = true;
  164. this.onMouseDown({button: 0}, way);
  165. }
  166. }
  167. onTouchEnd() {
  168. if (!this.$root.isMobileDevice)
  169. return;
  170. this.inTouch = false;
  171. this.onMouseUp();
  172. }
  173. }
  174. export default vueComponent(NumInput);
  175. //-----------------------------------------------------------------------------
  176. </script>
  177. <style scoped>
  178. .no-mp {
  179. margin: 0;
  180. padding: 0;
  181. }
  182. .button {
  183. font-size: 130%;
  184. border-radius: 20px;
  185. color: #bbb;
  186. cursor: pointer;
  187. }
  188. .button:hover {
  189. color: #616161;
  190. background-color: #efebe9;
  191. }
  192. .error {
  193. background-color: #ffabab;
  194. border-radius: 3px;
  195. }
  196. .disable, .disable:hover {
  197. cursor: not-allowed;
  198. color: #bbb;
  199. background-color: white;
  200. }
  201. </style>