index.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. function t(t, a) {
  2. var e = void 0,
  3. i = void 0,
  4. n = void 0;
  5. try {
  6. e = t.toString().split(".")[1].length;
  7. } catch (t) {
  8. e = 0;
  9. }
  10. try {
  11. i = a.toString().split(".")[1].length;
  12. } catch (t) {
  13. i = 0;
  14. }
  15. return n = Math.pow(10, Math.max(e, i)), (Math.round(t * n) + Math.round(a * n)) / n;
  16. }
  17. Component({
  18. externalClasses: ["i-class", "i-class-number-text"],
  19. properties: {
  20. size: String,
  21. value: {
  22. type: Number,
  23. value: 1
  24. },
  25. min: {
  26. type: Number,
  27. value: -1 / 0
  28. },
  29. max: {
  30. type: Number,
  31. value: 1 / 0
  32. },
  33. step: {
  34. type: Number,
  35. value: 1
  36. },
  37. reduceImage: {
  38. type: String,
  39. value: "../../images/icon-input-reduce.png"
  40. },
  41. addImage: {
  42. type: String,
  43. value: "../../images/icon-input-add.png"
  44. }
  45. },
  46. data: {
  47. canChange: true
  48. },
  49. methods: {
  50. handleChangeStep: function(a, e, i) {
  51. var n = this.data.value,
  52. s = a.currentTarget.dataset,
  53. h = (void 0 === s ? {} : s).disabled,
  54. u = this.data.step;
  55. if (h) return null;
  56. "minus" === e ? n = t(n, -u) : "plus" === e && (n = t(n, u)), this.handleEmit(n, e, i);
  57. },
  58. handleMinus: function(t) {
  59. this.data.canChange && this.handleChangeStep(t, "minus", true);
  60. },
  61. handlePlus: function(t) {
  62. this.data.canChange && this.handleChangeStep(t, "plus", true);
  63. },
  64. handleFocus: function() {
  65. this.data.canChange = false, this.triggerEvent("focus");
  66. },
  67. getType: function(t) {
  68. return t > this.data.value ? "plus" : t < this.data.value ? "minus" : "";
  69. },
  70. handleBlur: function(t) {
  71. this.data.canChange = true;
  72. var oldValue = this.data.value;
  73. var a = t.detail.value,
  74. e = "";
  75. "" === a && (a = 1);
  76. a = (a - oldValue);
  77. (a<=0) && (a=1);
  78. var i = this.data.min;
  79. // e = this.getType(a)
  80. if (!(a *= 1)) return a = 0 === a ? i : 1, e = 'plus', void this.handleEmit(a, e, this.data.value !== a);
  81. a = +a, e = this.getType(a);
  82. var n = this.data.value !== a;
  83. this.handleEmit(a, e, n);
  84. },
  85. handleEmit: function(t, a) {
  86. t < this.data.min && this.triggerEvent("outOfMin", t), t > this.data.max && this.triggerEvent("outOfMax", t);
  87. var e = this.data,
  88. i = e.min,
  89. n = e.max;
  90. t > n ? t = n : t < i ? t = i : t || (t = i);
  91. var s = t !== this.data.value,
  92. h = {
  93. value: t,
  94. type: a
  95. };
  96. a && (h.type = a), s ? this.triggerEvent("change", h) : (this.setData({
  97. value: t
  98. }), this.triggerEvent("change"));
  99. },
  100. returnTap: function(){}
  101. }
  102. });