NumInput.vue 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. if (this.ready)//исправление бага TypeError: Cannot read properties of null (reading 'emitsOptions')
  73. this.filteredValue = newValue;
  74. },
  75. min() {
  76. this.checkErrorAndEmit();
  77. },
  78. max() {
  79. this.checkErrorAndEmit();
  80. }
  81. }
  82. };
  83. class NumInput {
  84. _options = componentOptions;
  85. _props = {
  86. modelValue: Number,
  87. min: { type: Number, default: -Number.MAX_VALUE },
  88. max: { type: Number, default: Number.MAX_VALUE },
  89. step: { type: Number, default: 1 },
  90. digits: { type: Number, default: 0 },
  91. disable: Boolean,
  92. minusIcon: {type: String, default: 'la la-minus-circle'},
  93. plusIcon: {type: String, default: 'la la-plus-circle'},
  94. mmButtons: Boolean,
  95. mask: String,
  96. };
  97. filteredValue = 0;
  98. error = false;
  99. mounted() {
  100. this.ready = true;
  101. this.filteredValue = this.modelValue;
  102. }
  103. string2number(value) {
  104. return Number.parseFloat(Number.parseFloat(value).toFixed(this.digits));
  105. }
  106. validate(value) {
  107. let n = this.string2number(value);
  108. if (isNaN(n))
  109. return false;
  110. if (n < this.min)
  111. return false;
  112. if (n > this.max)
  113. return false;
  114. return true;
  115. }
  116. checkErrorAndEmit(emit = false) {
  117. if (this.validate(this.filteredValue)) {
  118. this.error = false;
  119. if (emit)
  120. this.$emit('update:modelValue', this.string2number(this.filteredValue));
  121. } else {
  122. this.error = true;
  123. }
  124. }
  125. plus() {
  126. const newValue = this.modelValue + this.step;
  127. if (this.validate(newValue))
  128. this.filteredValue = newValue;
  129. }
  130. minus() {
  131. const newValue = this.modelValue - this.step;
  132. if (this.validate(newValue))
  133. this.filteredValue = newValue;
  134. }
  135. onClick(way) {
  136. if (this.clickRepeat)
  137. return;
  138. if (way == 'plus') {
  139. this.plus();
  140. } else {
  141. this.minus();
  142. }
  143. }
  144. onMouseDown(event, way) {
  145. this.startClickRepeat = true;
  146. this.clickRepeat = false;
  147. if (event.button == 0) {
  148. (async() => {
  149. if (this.inRepeatFunc)
  150. return;
  151. this.inRepeatFunc = true;
  152. try {
  153. await utils.sleep(300);
  154. if (this.startClickRepeat) {
  155. this.clickRepeat = true;
  156. while (this.clickRepeat) {
  157. if (way == 'plus') {
  158. this.plus();
  159. } else {
  160. this.minus();
  161. }
  162. await utils.sleep(200);
  163. }
  164. }
  165. } finally {
  166. this.inRepeatFunc = false;
  167. }
  168. })();
  169. }
  170. }
  171. onMouseUp() {
  172. if (this.inTouch)
  173. return;
  174. this.startClickRepeat = false;
  175. if (this.clickRepeat) {
  176. (async() => {
  177. await utils.sleep(50);
  178. this.clickRepeat = false;
  179. })();
  180. }
  181. }
  182. onTouchStart(event, way) {
  183. if (!this.$root.isMobileDevice)
  184. return;
  185. if (event.touches.length == 1) {
  186. this.inTouch = true;
  187. this.onMouseDown({button: 0}, way);
  188. }
  189. }
  190. onTouchEnd() {
  191. if (!this.$root.isMobileDevice)
  192. return;
  193. this.inTouch = false;
  194. this.onMouseUp();
  195. }
  196. toMin() {
  197. this.filteredValue = this.min;
  198. }
  199. toMax() {
  200. this.filteredValue = this.max;
  201. }
  202. }
  203. export default vueComponent(NumInput);
  204. //-----------------------------------------------------------------------------
  205. </script>
  206. <style scoped>
  207. .no-mp {
  208. margin: 0;
  209. padding: 0;
  210. }
  211. .button {
  212. font-size: 130%;
  213. border-radius: 15px;
  214. width: 30px;
  215. height: 30px;
  216. color: #bbb;
  217. cursor: pointer;
  218. }
  219. .button:hover {
  220. color: #616161;
  221. background-color: #efebe9;
  222. }
  223. .error {
  224. background-color: #ffabab;
  225. border-radius: 3px;
  226. }
  227. .disable, .disable:hover {
  228. cursor: not-allowed;
  229. color: #bbb;
  230. background-color: white;
  231. }
  232. </style>