NumInput.vue 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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: function(newValue) {
  50. if (this.validate(newValue)) {
  51. this.error = false;
  52. this.$emit('update:modelValue', this.string2number(newValue));
  53. } else {
  54. this.error = true;
  55. }
  56. },
  57. modelValue: function(newValue) {
  58. this.filteredValue = newValue;
  59. },
  60. }
  61. };
  62. class NumInput {
  63. _options = componentOptions;
  64. _props = {
  65. modelValue: Number,
  66. min: { type: Number, default: -Number.MAX_VALUE },
  67. max: { type: Number, default: Number.MAX_VALUE },
  68. step: { type: Number, default: 1 },
  69. digits: { type: Number, default: 0 },
  70. disable: Boolean,
  71. minusIcon: {type: String, default: 'la la-minus-circle'},
  72. plusIcon: {type: String, default: 'la la-plus-circle'},
  73. };
  74. filteredValue = 0;
  75. error = false;
  76. created() {
  77. this.filteredValue = this.modelValue;
  78. }
  79. string2number(value) {
  80. return Number.parseFloat(Number.parseFloat(value).toFixed(this.digits));
  81. }
  82. validate(value) {
  83. let n = this.string2number(value);
  84. if (isNaN(n))
  85. return false;
  86. if (n < this.min)
  87. return false;
  88. if (n > this.max)
  89. return false;
  90. return true;
  91. }
  92. plus() {
  93. const newValue = this.modelValue + this.step;
  94. if (this.validate(newValue))
  95. this.filteredValue = newValue;
  96. }
  97. minus() {
  98. const newValue = this.modelValue - this.step;
  99. if (this.validate(newValue))
  100. this.filteredValue = newValue;
  101. }
  102. onClick(way) {
  103. if (this.clickRepeat)
  104. return;
  105. if (way == 'plus') {
  106. this.plus();
  107. } else {
  108. this.minus();
  109. }
  110. }
  111. onMouseDown(event, way) {
  112. this.startClickRepeat = true;
  113. this.clickRepeat = false;
  114. if (event.button == 0) {
  115. (async() => {
  116. if (this.inRepeatFunc)
  117. return;
  118. this.inRepeatFunc = true;
  119. try {
  120. await utils.sleep(300);
  121. if (this.startClickRepeat) {
  122. this.clickRepeat = true;
  123. while (this.clickRepeat) {
  124. if (way == 'plus') {
  125. this.plus();
  126. } else {
  127. this.minus();
  128. }
  129. await utils.sleep(200);
  130. }
  131. }
  132. } finally {
  133. this.inRepeatFunc = false;
  134. }
  135. })();
  136. }
  137. }
  138. onMouseUp() {
  139. if (this.inTouch)
  140. return;
  141. this.startClickRepeat = false;
  142. if (this.clickRepeat) {
  143. (async() => {
  144. await utils.sleep(50);
  145. this.clickRepeat = false;
  146. })();
  147. }
  148. }
  149. onTouchStart(event, way) {
  150. if (!this.$root.isMobileDevice)
  151. return;
  152. if (event.touches.length == 1) {
  153. this.inTouch = true;
  154. this.onMouseDown({button: 0}, way);
  155. }
  156. }
  157. onTouchEnd() {
  158. if (!this.$root.isMobileDevice)
  159. return;
  160. this.inTouch = false;
  161. this.onMouseUp();
  162. }
  163. }
  164. export default vueComponent(NumInput);
  165. //-----------------------------------------------------------------------------
  166. </script>
  167. <style scoped>
  168. .no-mp {
  169. margin: 0;
  170. padding: 0;
  171. }
  172. .button {
  173. font-size: 130%;
  174. border-radius: 20px;
  175. color: #bbb;
  176. cursor: pointer;
  177. }
  178. .button:hover {
  179. color: #616161;
  180. background-color: #efebe9;
  181. }
  182. .error {
  183. background-color: #ffabab;
  184. border-radius: 3px;
  185. }
  186. .disable, .disable:hover {
  187. cursor: not-allowed;
  188. color: #bbb;
  189. background-color: white;
  190. }
  191. </style>