index.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. var component_1 = require("../common/component");
  4. var utils_1 = require("../common/utils");
  5. var validator_1 = require("../common/validator");
  6. var DEFAULT_DURATION = 200;
  7. (0, component_1.VantComponent)({
  8. classes: ['active-class'],
  9. props: {
  10. valueKey: String,
  11. className: String,
  12. itemHeight: Number,
  13. visibleItemCount: Number,
  14. initialOptions: {
  15. type: Array,
  16. value: [],
  17. },
  18. defaultIndex: {
  19. type: Number,
  20. value: 0,
  21. observer: function (value) {
  22. this.setIndex(value);
  23. },
  24. },
  25. },
  26. data: {
  27. startY: 0,
  28. offset: 0,
  29. duration: 0,
  30. startOffset: 0,
  31. options: [],
  32. currentIndex: 0,
  33. },
  34. created: function () {
  35. var _this = this;
  36. var _a = this.data, defaultIndex = _a.defaultIndex, initialOptions = _a.initialOptions;
  37. this.set({
  38. currentIndex: defaultIndex,
  39. options: initialOptions,
  40. }).then(function () {
  41. _this.setIndex(defaultIndex);
  42. });
  43. },
  44. methods: {
  45. getCount: function () {
  46. return this.data.options.length;
  47. },
  48. onTouchStart: function (event) {
  49. this.setData({
  50. startY: event.touches[0].clientY,
  51. startOffset: this.data.offset,
  52. duration: 0,
  53. });
  54. },
  55. onTouchMove: function (event) {
  56. var data = this.data;
  57. var deltaY = event.touches[0].clientY - data.startY;
  58. this.setData({
  59. offset: (0, utils_1.range)(data.startOffset + deltaY, -(this.getCount() * data.itemHeight), data.itemHeight),
  60. });
  61. },
  62. onTouchEnd: function () {
  63. var data = this.data;
  64. if (data.offset !== data.startOffset) {
  65. this.setData({ duration: DEFAULT_DURATION });
  66. var index = (0, utils_1.range)(Math.round(-data.offset / data.itemHeight), 0, this.getCount() - 1);
  67. this.setIndex(index, true);
  68. }
  69. },
  70. onClickItem: function (event) {
  71. var index = event.currentTarget.dataset.index;
  72. this.setIndex(index, true);
  73. },
  74. adjustIndex: function (index) {
  75. var data = this.data;
  76. var count = this.getCount();
  77. index = (0, utils_1.range)(index, 0, count);
  78. for (var i = index; i < count; i++) {
  79. if (!this.isDisabled(data.options[i]))
  80. return i;
  81. }
  82. for (var i = index - 1; i >= 0; i--) {
  83. if (!this.isDisabled(data.options[i]))
  84. return i;
  85. }
  86. },
  87. isDisabled: function (option) {
  88. return (0, validator_1.isObj)(option) && option.disabled;
  89. },
  90. getOptionText: function (option) {
  91. var data = this.data;
  92. return (0, validator_1.isObj)(option) && data.valueKey in option
  93. ? option[data.valueKey]
  94. : option;
  95. },
  96. setIndex: function (index, userAction) {
  97. var _this = this;
  98. var data = this.data;
  99. index = this.adjustIndex(index) || 0;
  100. var offset = -index * data.itemHeight;
  101. if (index !== data.currentIndex) {
  102. return this.set({ offset: offset, currentIndex: index }).then(function () {
  103. userAction && _this.$emit('change', index);
  104. });
  105. }
  106. return this.set({ offset: offset });
  107. },
  108. setValue: function (value) {
  109. var options = this.data.options;
  110. for (var i = 0; i < options.length; i++) {
  111. if (this.getOptionText(options[i]) === value) {
  112. return this.setIndex(i);
  113. }
  114. }
  115. return Promise.resolve();
  116. },
  117. getValue: function () {
  118. var data = this.data;
  119. return data.options[data.currentIndex];
  120. },
  121. },
  122. });