PickerColumn.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. exports.__esModule = true;
  4. exports.default = void 0;
  5. var _babelHelperVueJsxMergeProps = _interopRequireDefault(require("@vue/babel-helper-vue-jsx-merge-props"));
  6. var _deepClone = require("../utils/deep-clone");
  7. var _utils = require("../utils");
  8. var _number = require("../utils/format/number");
  9. var _event = require("../utils/dom/event");
  10. var _touch = require("../mixins/touch");
  11. var DEFAULT_DURATION = 200; // 惯性滑动思路:
  12. // 在手指离开屏幕时,如果和上一次 move 时的间隔小于 `MOMENTUM_LIMIT_TIME` 且 move
  13. // 距离大于 `MOMENTUM_LIMIT_DISTANCE` 时,执行惯性滑动
  14. var MOMENTUM_LIMIT_TIME = 300;
  15. var MOMENTUM_LIMIT_DISTANCE = 15;
  16. var _createNamespace = (0, _utils.createNamespace)('picker-column'),
  17. createComponent = _createNamespace[0],
  18. bem = _createNamespace[1];
  19. function getElementTranslateY(element) {
  20. var style = window.getComputedStyle(element);
  21. var transform = style.transform || style.webkitTransform;
  22. var translateY = transform.slice(7, transform.length - 1).split(', ')[5];
  23. return Number(translateY);
  24. }
  25. function isOptionDisabled(option) {
  26. return (0, _utils.isObject)(option) && option.disabled;
  27. }
  28. var _default2 = createComponent({
  29. mixins: [_touch.TouchMixin],
  30. props: {
  31. valueKey: String,
  32. readonly: Boolean,
  33. allowHtml: Boolean,
  34. className: String,
  35. itemHeight: Number,
  36. defaultIndex: Number,
  37. swipeDuration: [Number, String],
  38. visibleItemCount: [Number, String],
  39. initialOptions: {
  40. type: Array,
  41. default: function _default() {
  42. return [];
  43. }
  44. }
  45. },
  46. data: function data() {
  47. return {
  48. offset: 0,
  49. duration: 0,
  50. options: (0, _deepClone.deepClone)(this.initialOptions),
  51. currentIndex: this.defaultIndex
  52. };
  53. },
  54. created: function created() {
  55. if (this.$parent.children) {
  56. this.$parent.children.push(this);
  57. }
  58. this.setIndex(this.currentIndex);
  59. },
  60. mounted: function mounted() {
  61. this.bindTouchEvent(this.$el);
  62. },
  63. destroyed: function destroyed() {
  64. var children = this.$parent.children;
  65. if (children) {
  66. children.splice(children.indexOf(this), 1);
  67. }
  68. },
  69. watch: {
  70. initialOptions: 'setOptions',
  71. defaultIndex: function defaultIndex(val) {
  72. this.setIndex(val);
  73. }
  74. },
  75. computed: {
  76. count: function count() {
  77. return this.options.length;
  78. },
  79. baseOffset: function baseOffset() {
  80. return this.itemHeight * (this.visibleItemCount - 1) / 2;
  81. }
  82. },
  83. methods: {
  84. setOptions: function setOptions(options) {
  85. if (JSON.stringify(options) !== JSON.stringify(this.options)) {
  86. this.options = (0, _deepClone.deepClone)(options);
  87. this.setIndex(this.defaultIndex);
  88. }
  89. },
  90. onTouchStart: function onTouchStart(event) {
  91. if (this.readonly) {
  92. return;
  93. }
  94. this.touchStart(event);
  95. if (this.moving) {
  96. var translateY = getElementTranslateY(this.$refs.wrapper);
  97. this.offset = Math.min(0, translateY - this.baseOffset);
  98. this.startOffset = this.offset;
  99. } else {
  100. this.startOffset = this.offset;
  101. }
  102. this.duration = 0;
  103. this.transitionEndTrigger = null;
  104. this.touchStartTime = Date.now();
  105. this.momentumOffset = this.startOffset;
  106. },
  107. onTouchMove: function onTouchMove(event) {
  108. if (this.readonly) {
  109. return;
  110. }
  111. this.touchMove(event);
  112. if (this.direction === 'vertical') {
  113. this.moving = true;
  114. (0, _event.preventDefault)(event, true);
  115. }
  116. this.offset = (0, _number.range)(this.startOffset + this.deltaY, -(this.count * this.itemHeight), this.itemHeight);
  117. var now = Date.now();
  118. if (now - this.touchStartTime > MOMENTUM_LIMIT_TIME) {
  119. this.touchStartTime = now;
  120. this.momentumOffset = this.offset;
  121. }
  122. },
  123. onTouchEnd: function onTouchEnd() {
  124. var _this = this;
  125. if (this.readonly) {
  126. return;
  127. }
  128. var distance = this.offset - this.momentumOffset;
  129. var duration = Date.now() - this.touchStartTime;
  130. var allowMomentum = duration < MOMENTUM_LIMIT_TIME && Math.abs(distance) > MOMENTUM_LIMIT_DISTANCE;
  131. if (allowMomentum) {
  132. this.momentum(distance, duration);
  133. return;
  134. }
  135. var index = this.getIndexByOffset(this.offset);
  136. this.duration = DEFAULT_DURATION;
  137. this.setIndex(index, true); // compatible with desktop scenario
  138. // use setTimeout to skip the click event Emitted after touchstart
  139. setTimeout(function () {
  140. _this.moving = false;
  141. }, 0);
  142. },
  143. onTransitionEnd: function onTransitionEnd() {
  144. this.stopMomentum();
  145. },
  146. onClickItem: function onClickItem(index) {
  147. if (this.moving || this.readonly) {
  148. return;
  149. }
  150. this.transitionEndTrigger = null;
  151. this.duration = DEFAULT_DURATION;
  152. this.setIndex(index, true);
  153. },
  154. adjustIndex: function adjustIndex(index) {
  155. index = (0, _number.range)(index, 0, this.count);
  156. for (var i = index; i < this.count; i++) {
  157. if (!isOptionDisabled(this.options[i])) return i;
  158. }
  159. for (var _i = index - 1; _i >= 0; _i--) {
  160. if (!isOptionDisabled(this.options[_i])) return _i;
  161. }
  162. },
  163. getOptionText: function getOptionText(option) {
  164. if ((0, _utils.isObject)(option) && this.valueKey in option) {
  165. return option[this.valueKey];
  166. }
  167. return option;
  168. },
  169. setIndex: function setIndex(index, emitChange) {
  170. var _this2 = this;
  171. index = this.adjustIndex(index) || 0;
  172. var offset = -index * this.itemHeight;
  173. var trigger = function trigger() {
  174. if (index !== _this2.currentIndex) {
  175. _this2.currentIndex = index;
  176. if (emitChange) {
  177. _this2.$emit('change', index);
  178. }
  179. }
  180. }; // trigger the change event after transitionend when moving
  181. if (this.moving && offset !== this.offset) {
  182. this.transitionEndTrigger = trigger;
  183. } else {
  184. trigger();
  185. }
  186. this.offset = offset;
  187. },
  188. setValue: function setValue(value) {
  189. var options = this.options;
  190. for (var i = 0; i < options.length; i++) {
  191. if (this.getOptionText(options[i]) === value) {
  192. return this.setIndex(i);
  193. }
  194. }
  195. },
  196. getValue: function getValue() {
  197. return this.options[this.currentIndex];
  198. },
  199. getIndexByOffset: function getIndexByOffset(offset) {
  200. return (0, _number.range)(Math.round(-offset / this.itemHeight), 0, this.count - 1);
  201. },
  202. momentum: function momentum(distance, duration) {
  203. var speed = Math.abs(distance / duration);
  204. distance = this.offset + speed / 0.003 * (distance < 0 ? -1 : 1);
  205. var index = this.getIndexByOffset(distance);
  206. this.duration = +this.swipeDuration;
  207. this.setIndex(index, true);
  208. },
  209. stopMomentum: function stopMomentum() {
  210. this.moving = false;
  211. this.duration = 0;
  212. if (this.transitionEndTrigger) {
  213. this.transitionEndTrigger();
  214. this.transitionEndTrigger = null;
  215. }
  216. },
  217. genOptions: function genOptions() {
  218. var _this3 = this;
  219. var h = this.$createElement;
  220. var optionStyle = {
  221. height: this.itemHeight + "px"
  222. };
  223. return this.options.map(function (option, index) {
  224. var _domProps;
  225. var text = _this3.getOptionText(option);
  226. var disabled = isOptionDisabled(option);
  227. var data = {
  228. style: optionStyle,
  229. attrs: {
  230. role: 'button',
  231. tabindex: disabled ? -1 : 0
  232. },
  233. class: [bem('item', {
  234. disabled: disabled,
  235. selected: index === _this3.currentIndex
  236. })],
  237. on: {
  238. click: function click() {
  239. _this3.onClickItem(index);
  240. }
  241. }
  242. };
  243. var childData = {
  244. class: 'van-ellipsis',
  245. domProps: (_domProps = {}, _domProps[_this3.allowHtml ? 'innerHTML' : 'textContent'] = text, _domProps)
  246. };
  247. return h("li", (0, _babelHelperVueJsxMergeProps.default)([{}, data]), [_this3.slots('option', option) || h("div", (0, _babelHelperVueJsxMergeProps.default)([{}, childData]))]);
  248. });
  249. }
  250. },
  251. render: function render() {
  252. var h = arguments[0];
  253. var wrapperStyle = {
  254. transform: "translate3d(0, " + (this.offset + this.baseOffset) + "px, 0)",
  255. transitionDuration: this.duration + "ms",
  256. transitionProperty: this.duration ? 'all' : 'none'
  257. };
  258. return h("div", {
  259. "class": [bem(), this.className]
  260. }, [h("ul", {
  261. "ref": "wrapper",
  262. "style": wrapperStyle,
  263. "class": bem('wrapper'),
  264. "on": {
  265. "transitionend": this.onTransitionEnd
  266. }
  267. }, [this.genOptions()])]);
  268. }
  269. });
  270. exports.default = _default2;