index.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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", "i-number-view", "i-number-img"],
  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. idx: {
  46. type: Number,
  47. value: 0
  48. }
  49. },
  50. data: {
  51. canChange: true,
  52. skin: getApp().globalData.skin,
  53. },
  54. attached(){
  55. this.setData({
  56. skin: getApp().globalData.skin,
  57. })
  58. },
  59. methods: {
  60. handleChangeStep: function(a, e, i) {
  61. var n = this.data.value,
  62. s = a.currentTarget.dataset,
  63. h = (void 0 === s ? {} : s).disabled,
  64. u = this.data.step;
  65. if (h) return null;
  66. "minus" === e ? n = t(n, -u) : "plus" === e && (n = t(n, u)), this.handleEmit(n, e, i);
  67. },
  68. handleMinus: function(t) {
  69. this.data.canChange && this.handleChangeStep(t, "minus", true);
  70. },
  71. handlePlus: function(t) {
  72. this.data.canChange && this.handleChangeStep(t, "plus", true);
  73. },
  74. handleFocus: function() {
  75. this.data.canChange = false, this.triggerEvent("focus");
  76. },
  77. getType: function(t) {
  78. return t > this.data.value ? "plus" : t < this.data.value ? "minus" : "";
  79. },
  80. handleBlur: function(t) {
  81. this.data.canChange = true;
  82. var oldValue = this.data.value;
  83. var a = t.detail.value,
  84. e = "";
  85. "" === a && (a = 1);
  86. a = (a - oldValue);
  87. (a<=0) && (a=1);
  88. var i = this.data.min;
  89. // e = this.getType(a)
  90. if (!(a *= 1)) return a = 0 === a ? i : 1, e = 'plus', void this.handleEmit(a, e, this.data.value !== a);
  91. a = +a, e = this.getType(a);
  92. var n = this.data.value !== a;
  93. this.handleEmit(a, e, n);
  94. },
  95. handleEmit: function(t, a) {
  96. t < this.data.min && this.triggerEvent("outOfMin", t), t > this.data.max && this.triggerEvent("outOfMax", t);
  97. var e = this.data,
  98. i = e.min,
  99. n = e.max;
  100. t > n ? t = n : t < i ? t = i : t || (t = i);
  101. var s = t !== this.data.value,
  102. h = {
  103. value: t,
  104. type: a,
  105. idx: this.data.idx
  106. };
  107. a && (h.type = a), s ? this.triggerEvent("change", h) : (this.setData({
  108. value: t
  109. }), this.triggerEvent("change"));
  110. },
  111. returnTap: function(){}
  112. }
  113. });