NumInput.vue 6.7 KB

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