index.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. function handle(e, num) {
  2. var dataset = e.currentTarget.dataset;
  3. var componentId = dataset.componentId;
  4. var disabled = dataset.disabled;
  5. var quantity = +dataset.quantity;
  6. if (disabled) return null;
  7. callback.call(this, componentId, quantity + num);
  8. }
  9. function callback(componentId, quantity) {
  10. quantity = +quantity;
  11. var e = { componentId, quantity };
  12. console.info('[zan:quantity:change]', e);
  13. if (this.handleZanQuantityChange) {
  14. this.handleZanQuantityChange(e);
  15. } else {
  16. console.warn('页面缺少 handleZanQuantityChange 回调函数');
  17. }
  18. }
  19. var Quantity = {
  20. _handleZanQuantityMinus(e) {
  21. handle.call(this, e, -1);
  22. },
  23. _handleZanQuantityPlus(e) {
  24. handle.call(this, e, +1);
  25. },
  26. _handleZanQuantityBlur(e) {
  27. var dataset = e.currentTarget.dataset;
  28. var componentId = dataset.componentId;
  29. var max = +dataset.max;
  30. var min = +dataset.min;
  31. var value = e.detail.value;
  32. if (!value) {
  33. setTimeout(() => {
  34. callback.call(this, componentId, min);
  35. }, 16);
  36. callback.call(this, componentId, value);
  37. return '' + value;
  38. }
  39. value = +value;
  40. if (value > max) {
  41. value = max;
  42. } else if (value < min) {
  43. value = min;
  44. }
  45. callback.call(this, componentId, value);
  46. return '' + value;
  47. }
  48. };
  49. module.exports = Quantity;