PickerColumn.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. exports.__esModule = true;
  4. exports.default = exports.MOMENTUM_LIMIT_DISTANCE = exports.MOMENTUM_LIMIT_TIME = 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. exports.MOMENTUM_LIMIT_TIME = MOMENTUM_LIMIT_TIME;
  16. var MOMENTUM_LIMIT_DISTANCE = 15;
  17. exports.MOMENTUM_LIMIT_DISTANCE = MOMENTUM_LIMIT_DISTANCE;
  18. var _createNamespace = (0, _utils.createNamespace)('picker-column'),
  19. createComponent = _createNamespace[0],
  20. bem = _createNamespace[1];
  21. function getElementTranslateY(element) {
  22. var style = window.getComputedStyle(element);
  23. var transform = style.transform || style.webkitTransform;
  24. var translateY = transform.slice(7, transform.length - 1).split(', ')[5];
  25. return Number(translateY);
  26. }
  27. function isOptionDisabled(option) {
  28. return (0, _utils.isObject)(option) && option.disabled;
  29. } // use standard WheelEvent:
  30. // https://developer.mozilla.org/en-US/docs/Web/API/WheelEvent
  31. var supportMousewheel = _utils.inBrowser && 'onwheel' in window;
  32. var mousewheelTimer = null;
  33. var _default2 = createComponent({
  34. mixins: [_touch.TouchMixin],
  35. props: {
  36. valueKey: String,
  37. readonly: Boolean,
  38. allowHtml: Boolean,
  39. className: String,
  40. itemHeight: Number,
  41. defaultIndex: Number,
  42. swipeDuration: [Number, String],
  43. visibleItemCount: [Number, String],
  44. initialOptions: {
  45. type: Array,
  46. default: function _default() {
  47. return [];
  48. }
  49. }
  50. },
  51. data: function data() {
  52. return {
  53. offset: 0,
  54. duration: 0,
  55. options: (0, _deepClone.deepClone)(this.initialOptions),
  56. currentIndex: this.defaultIndex
  57. };
  58. },
  59. created: function created() {
  60. if (this.$parent.children) {
  61. this.$parent.children.push(this);
  62. }
  63. this.setIndex(this.currentIndex);
  64. },
  65. mounted: function mounted() {
  66. this.bindTouchEvent(this.$el);
  67. if (supportMousewheel) {
  68. (0, _event.on)(this.$el, 'wheel', this.onMouseWheel, false);
  69. }
  70. },
  71. destroyed: function destroyed() {
  72. var children = this.$parent.children;
  73. if (children) {
  74. children.splice(children.indexOf(this), 1);
  75. }
  76. if (supportMousewheel) {
  77. (0, _event.off)(this.$el, 'wheel');
  78. }
  79. },
  80. watch: {
  81. initialOptions: 'setOptions',
  82. defaultIndex: function defaultIndex(val) {
  83. this.setIndex(val);
  84. }
  85. },
  86. computed: {
  87. count: function count() {
  88. return this.options.length;
  89. },
  90. baseOffset: function baseOffset() {
  91. return this.itemHeight * (this.visibleItemCount - 1) / 2;
  92. }
  93. },
  94. methods: {
  95. setOptions: function setOptions(options) {
  96. if (JSON.stringify(options) !== JSON.stringify(this.options)) {
  97. this.options = (0, _deepClone.deepClone)(options);
  98. this.setIndex(this.defaultIndex);
  99. }
  100. },
  101. onTouchStart: function onTouchStart(event) {
  102. if (this.readonly) {
  103. return;
  104. }
  105. this.touchStart(event);
  106. if (this.moving) {
  107. var translateY = getElementTranslateY(this.$refs.wrapper);
  108. this.offset = Math.min(0, translateY - this.baseOffset);
  109. this.startOffset = this.offset;
  110. } else {
  111. this.startOffset = this.offset;
  112. }
  113. this.duration = 0;
  114. this.transitionEndTrigger = null;
  115. this.touchStartTime = Date.now();
  116. this.momentumOffset = this.startOffset;
  117. },
  118. onTouchMove: function onTouchMove(event) {
  119. if (this.readonly) {
  120. return;
  121. }
  122. this.touchMove(event);
  123. if (this.direction === 'vertical') {
  124. this.moving = true;
  125. (0, _event.preventDefault)(event, true);
  126. }
  127. this.offset = (0, _number.range)(this.startOffset + this.deltaY, -(this.count * this.itemHeight), this.itemHeight);
  128. var now = Date.now();
  129. if (now - this.touchStartTime > MOMENTUM_LIMIT_TIME) {
  130. this.touchStartTime = now;
  131. this.momentumOffset = this.offset;
  132. }
  133. },
  134. onTouchEnd: function onTouchEnd() {
  135. var _this = this;
  136. if (this.readonly) {
  137. return;
  138. }
  139. var distance = this.offset - this.momentumOffset;
  140. var duration = Date.now() - this.touchStartTime;
  141. var allowMomentum = duration < MOMENTUM_LIMIT_TIME && Math.abs(distance) > MOMENTUM_LIMIT_DISTANCE;
  142. if (allowMomentum) {
  143. this.momentum(distance, duration);
  144. return;
  145. }
  146. var index = this.getIndexByOffset(this.offset);
  147. this.duration = DEFAULT_DURATION;
  148. this.setIndex(index, true); // compatible with desktop scenario
  149. // use setTimeout to skip the click event Emitted after touchstart
  150. setTimeout(function () {
  151. _this.moving = false;
  152. }, 0);
  153. },
  154. onMouseWheel: function onMouseWheel(event) {
  155. var _this2 = this;
  156. if (this.readonly) {
  157. return;
  158. }
  159. (0, _event.preventDefault)(event, true); // simply combine touchstart and touchmove
  160. var translateY = getElementTranslateY(this.$refs.wrapper);
  161. this.startOffset = Math.min(0, translateY - this.baseOffset);
  162. this.momentumOffset = this.startOffset;
  163. this.transitionEndTrigger = null; // directly use deltaY, see https://caniuse.com/?search=deltaY
  164. // use deltaY to detect direction for not special setting device
  165. // https://developer.mozilla.org/en-US/docs/Web/API/Element/wheel_event
  166. var deltaY = event.deltaY;
  167. if (this.startOffset === 0 && deltaY < 0) {
  168. return;
  169. } // Calculate the offset based on itemHeight
  170. var itemOffset = this.itemHeight * (deltaY > 0 ? -1 : 1);
  171. this.offset = (0, _number.range)(this.startOffset + itemOffset, -(this.count * this.itemHeight), this.itemHeight);
  172. if (mousewheelTimer) {
  173. clearTimeout(mousewheelTimer);
  174. }
  175. mousewheelTimer = setTimeout(function () {
  176. _this2.onTouchEnd();
  177. _this2.touchStartTime = 0;
  178. }, MOMENTUM_LIMIT_TIME);
  179. },
  180. onTransitionEnd: function onTransitionEnd() {
  181. this.stopMomentum();
  182. },
  183. onClickItem: function onClickItem(index) {
  184. if (this.moving || this.readonly) {
  185. return;
  186. }
  187. this.transitionEndTrigger = null;
  188. this.duration = DEFAULT_DURATION;
  189. this.setIndex(index, true);
  190. },
  191. adjustIndex: function adjustIndex(index) {
  192. index = (0, _number.range)(index, 0, this.count);
  193. for (var i = index; i < this.count; i++) {
  194. if (!isOptionDisabled(this.options[i])) return i;
  195. }
  196. for (var _i = index - 1; _i >= 0; _i--) {
  197. if (!isOptionDisabled(this.options[_i])) return _i;
  198. }
  199. },
  200. getOptionText: function getOptionText(option) {
  201. if ((0, _utils.isObject)(option) && this.valueKey in option) {
  202. return option[this.valueKey];
  203. }
  204. return option;
  205. },
  206. setIndex: function setIndex(index, emitChange) {
  207. var _this3 = this;
  208. index = this.adjustIndex(index) || 0;
  209. var offset = -index * this.itemHeight;
  210. var trigger = function trigger() {
  211. if (index !== _this3.currentIndex) {
  212. _this3.currentIndex = index;
  213. if (emitChange) {
  214. _this3.$emit('change', index);
  215. }
  216. }
  217. }; // trigger the change event after transitionend when moving
  218. if (this.moving && offset !== this.offset) {
  219. this.transitionEndTrigger = trigger;
  220. } else {
  221. trigger();
  222. }
  223. this.offset = offset;
  224. },
  225. setValue: function setValue(value) {
  226. var options = this.options;
  227. for (var i = 0; i < options.length; i++) {
  228. if (this.getOptionText(options[i]) === value) {
  229. return this.setIndex(i);
  230. }
  231. }
  232. },
  233. getValue: function getValue() {
  234. return this.options[this.currentIndex];
  235. },
  236. getIndexByOffset: function getIndexByOffset(offset) {
  237. return (0, _number.range)(Math.round(-offset / this.itemHeight), 0, this.count - 1);
  238. },
  239. momentum: function momentum(distance, duration) {
  240. var speed = Math.abs(distance / duration);
  241. distance = this.offset + speed / 0.003 * (distance < 0 ? -1 : 1);
  242. var index = this.getIndexByOffset(distance);
  243. this.duration = +this.swipeDuration;
  244. this.setIndex(index, true);
  245. },
  246. stopMomentum: function stopMomentum() {
  247. this.moving = false;
  248. this.duration = 0;
  249. if (this.transitionEndTrigger) {
  250. this.transitionEndTrigger();
  251. this.transitionEndTrigger = null;
  252. }
  253. },
  254. genOptions: function genOptions() {
  255. var _this4 = this;
  256. var h = this.$createElement;
  257. var optionStyle = {
  258. height: this.itemHeight + "px"
  259. };
  260. return this.options.map(function (option, index) {
  261. var _domProps;
  262. var text = _this4.getOptionText(option);
  263. var disabled = isOptionDisabled(option);
  264. var data = {
  265. style: optionStyle,
  266. attrs: {
  267. role: 'button',
  268. tabindex: disabled ? -1 : 0
  269. },
  270. class: [bem('item', {
  271. disabled: disabled,
  272. selected: index === _this4.currentIndex
  273. })],
  274. on: {
  275. click: function click() {
  276. _this4.onClickItem(index);
  277. }
  278. }
  279. };
  280. var childData = {
  281. class: 'van-ellipsis',
  282. domProps: (_domProps = {}, _domProps[_this4.allowHtml ? 'innerHTML' : 'textContent'] = text, _domProps)
  283. };
  284. return h("li", (0, _babelHelperVueJsxMergeProps.default)([{}, data]), [_this4.slots('option', option) || h("div", (0, _babelHelperVueJsxMergeProps.default)([{}, childData]))]);
  285. });
  286. }
  287. },
  288. render: function render() {
  289. var h = arguments[0];
  290. var wrapperStyle = {
  291. transform: "translate3d(0, " + (this.offset + this.baseOffset) + "px, 0)",
  292. transitionDuration: this.duration + "ms",
  293. transitionProperty: this.duration ? 'all' : 'none'
  294. };
  295. return h("div", {
  296. "class": [bem(), this.className]
  297. }, [h("ul", {
  298. "ref": "wrapper",
  299. "style": wrapperStyle,
  300. "class": bem('wrapper'),
  301. "on": {
  302. "transitionend": this.onTransitionEnd
  303. }
  304. }, [this.genOptions()])]);
  305. }
  306. });
  307. exports.default = _default2;