NumInput.vue 6.6 KB

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