picker-column2.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. /*!
  2. * (C) Ionic http://ionicframework.com - MIT License
  3. */
  4. import { proxyCustomElement, HTMLElement, createEvent, h, Host } from '@stencil/core/internal/client';
  5. import { k as clamp } from './helpers.js';
  6. import { b as hapticSelectionChanged, h as hapticSelectionEnd, a as hapticSelectionStart } from './haptic.js';
  7. import { g as getClassMap } from './theme.js';
  8. import { b as getIonMode } from './ionic-global.js';
  9. const pickerColumnIosCss = ".picker-col{display:-ms-flexbox;display:flex;position:relative;-ms-flex:1;flex:1;-ms-flex-pack:center;justify-content:center;height:100%;-webkit-box-sizing:content-box;box-sizing:content-box;contain:content}.picker-opts{position:relative;-ms-flex:1;flex:1;max-width:100%}.picker-opt{top:0;display:block;position:absolute;width:100%;border:0;text-align:center;text-overflow:ellipsis;white-space:nowrap;contain:strict;overflow:hidden;will-change:transform}.picker-opt{inset-inline-start:0}.picker-opt.picker-opt-disabled{pointer-events:none}.picker-opt-disabled{opacity:0}.picker-opts-left{-ms-flex-pack:start;justify-content:flex-start}.picker-opts-right{-ms-flex-pack:end;justify-content:flex-end}.picker-opt:active,.picker-opt:focus{outline:none}.picker-prefix{position:relative;-ms-flex:1;flex:1;text-align:end;white-space:nowrap}.picker-suffix{position:relative;-ms-flex:1;flex:1;text-align:start;white-space:nowrap}.picker-col{-webkit-padding-start:4px;padding-inline-start:4px;-webkit-padding-end:4px;padding-inline-end:4px;padding-top:0;padding-bottom:0;-webkit-transform-style:preserve-3d;transform-style:preserve-3d}.picker-prefix,.picker-suffix,.picker-opts{top:77px;-webkit-transform-style:preserve-3d;transform-style:preserve-3d;color:inherit;font-size:20px;line-height:42px;pointer-events:none}.picker-opt{padding-left:0;padding-right:0;padding-top:0;padding-bottom:0;margin-left:0;margin-right:0;margin-top:0;margin-bottom:0;-webkit-transform-origin:center center;transform-origin:center center;height:46px;-webkit-transform-style:preserve-3d;transform-style:preserve-3d;-webkit-transition-timing-function:ease-out;transition-timing-function:ease-out;background:transparent;color:inherit;font-size:20px;line-height:42px;-webkit-backface-visibility:hidden;backface-visibility:hidden;pointer-events:auto}:host-context([dir=rtl]) .picker-opt{-webkit-transform-origin:calc(100% - center) center;transform-origin:calc(100% - center) center}[dir=rtl] .picker-opt{-webkit-transform-origin:calc(100% - center) center;transform-origin:calc(100% - center) center}@supports selector(:dir(rtl)){.picker-opt:dir(rtl){-webkit-transform-origin:calc(100% - center) center;transform-origin:calc(100% - center) center}}";
  10. const IonPickerLegacyColumnIosStyle0 = pickerColumnIosCss;
  11. const pickerColumnMdCss = ".picker-col{display:-ms-flexbox;display:flex;position:relative;-ms-flex:1;flex:1;-ms-flex-pack:center;justify-content:center;height:100%;-webkit-box-sizing:content-box;box-sizing:content-box;contain:content}.picker-opts{position:relative;-ms-flex:1;flex:1;max-width:100%}.picker-opt{top:0;display:block;position:absolute;width:100%;border:0;text-align:center;text-overflow:ellipsis;white-space:nowrap;contain:strict;overflow:hidden;will-change:transform}.picker-opt{inset-inline-start:0}.picker-opt.picker-opt-disabled{pointer-events:none}.picker-opt-disabled{opacity:0}.picker-opts-left{-ms-flex-pack:start;justify-content:flex-start}.picker-opts-right{-ms-flex-pack:end;justify-content:flex-end}.picker-opt:active,.picker-opt:focus{outline:none}.picker-prefix{position:relative;-ms-flex:1;flex:1;text-align:end;white-space:nowrap}.picker-suffix{position:relative;-ms-flex:1;flex:1;text-align:start;white-space:nowrap}.picker-col{-webkit-padding-start:8px;padding-inline-start:8px;-webkit-padding-end:8px;padding-inline-end:8px;padding-top:0;padding-bottom:0;-webkit-transform-style:preserve-3d;transform-style:preserve-3d}.picker-prefix,.picker-suffix,.picker-opts{top:77px;-webkit-transform-style:preserve-3d;transform-style:preserve-3d;color:inherit;font-size:22px;line-height:42px;pointer-events:none}.picker-opt{margin-left:0;margin-right:0;margin-top:0;margin-bottom:0;padding-left:0;padding-right:0;padding-top:0;padding-bottom:0;height:43px;-webkit-transition-timing-function:ease-out;transition-timing-function:ease-out;background:transparent;color:inherit;font-size:22px;line-height:42px;-webkit-backface-visibility:hidden;backface-visibility:hidden;pointer-events:auto}.picker-prefix,.picker-suffix,.picker-opt.picker-opt-selected{color:var(--ion-color-primary, #0054e9)}";
  12. const IonPickerLegacyColumnMdStyle0 = pickerColumnMdCss;
  13. const PickerColumnCmp = /*@__PURE__*/ proxyCustomElement(class PickerColumnCmp extends HTMLElement {
  14. constructor() {
  15. super();
  16. this.__registerHost();
  17. this.ionPickerColChange = createEvent(this, "ionPickerColChange", 7);
  18. this.optHeight = 0;
  19. this.rotateFactor = 0;
  20. this.scaleFactor = 1;
  21. this.velocity = 0;
  22. this.y = 0;
  23. this.noAnimate = true;
  24. // `colDidChange` is a flag that gets set when the column is changed
  25. // dynamically. When this flag is set, the column will refresh
  26. // after the component re-renders to incorporate the new column data.
  27. // This is necessary because `this.refresh` queries for the option elements,
  28. // so it needs to wait for the latest elements to be available in the DOM.
  29. // Ex: column is created with 3 options. User updates the column data
  30. // to have 5 options. The column will still think it only has 3 options.
  31. this.colDidChange = false;
  32. this.col = undefined;
  33. }
  34. colChanged() {
  35. this.colDidChange = true;
  36. }
  37. async connectedCallback() {
  38. let pickerRotateFactor = 0;
  39. let pickerScaleFactor = 0.81;
  40. const mode = getIonMode(this);
  41. if (mode === 'ios') {
  42. pickerRotateFactor = -0.46;
  43. pickerScaleFactor = 1;
  44. }
  45. this.rotateFactor = pickerRotateFactor;
  46. this.scaleFactor = pickerScaleFactor;
  47. this.gesture = (await import('./index3.js')).createGesture({
  48. el: this.el,
  49. gestureName: 'picker-swipe',
  50. gesturePriority: 100,
  51. threshold: 0,
  52. passive: false,
  53. onStart: (ev) => this.onStart(ev),
  54. onMove: (ev) => this.onMove(ev),
  55. onEnd: (ev) => this.onEnd(ev),
  56. });
  57. this.gesture.enable();
  58. // Options have not been initialized yet
  59. // Animation must be disabled through the `noAnimate` flag
  60. // Otherwise, the options will render
  61. // at the top of the column and transition down
  62. this.tmrId = setTimeout(() => {
  63. this.noAnimate = false;
  64. // After initialization, `refresh()` will be called
  65. // At this point, animation will be enabled. The options will
  66. // animate as they are being selected.
  67. this.refresh(true);
  68. }, 250);
  69. }
  70. componentDidLoad() {
  71. this.onDomChange();
  72. }
  73. componentDidUpdate() {
  74. // Options may have changed since last update.
  75. if (this.colDidChange) {
  76. // Animation must be disabled through the `onDomChange` parameter.
  77. // Otherwise, the recently added options will render
  78. // at the top of the column and transition down
  79. this.onDomChange(true, false);
  80. this.colDidChange = false;
  81. }
  82. }
  83. disconnectedCallback() {
  84. if (this.rafId !== undefined)
  85. cancelAnimationFrame(this.rafId);
  86. if (this.tmrId)
  87. clearTimeout(this.tmrId);
  88. if (this.gesture) {
  89. this.gesture.destroy();
  90. this.gesture = undefined;
  91. }
  92. }
  93. emitColChange() {
  94. this.ionPickerColChange.emit(this.col);
  95. }
  96. setSelected(selectedIndex, duration) {
  97. // if there is a selected index, then figure out it's y position
  98. // if there isn't a selected index, then just use the top y position
  99. const y = selectedIndex > -1 ? -(selectedIndex * this.optHeight) : 0;
  100. this.velocity = 0;
  101. // set what y position we're at
  102. if (this.rafId !== undefined)
  103. cancelAnimationFrame(this.rafId);
  104. this.update(y, duration, true);
  105. this.emitColChange();
  106. }
  107. update(y, duration, saveY) {
  108. if (!this.optsEl) {
  109. return;
  110. }
  111. // ensure we've got a good round number :)
  112. let translateY = 0;
  113. let translateZ = 0;
  114. const { col, rotateFactor } = this;
  115. const prevSelected = col.selectedIndex;
  116. const selectedIndex = (col.selectedIndex = this.indexForY(-y));
  117. const durationStr = duration === 0 ? '' : duration + 'ms';
  118. const scaleStr = `scale(${this.scaleFactor})`;
  119. const children = this.optsEl.children;
  120. for (let i = 0; i < children.length; i++) {
  121. const button = children[i];
  122. const opt = col.options[i];
  123. const optOffset = i * this.optHeight + y;
  124. let transform = '';
  125. if (rotateFactor !== 0) {
  126. const rotateX = optOffset * rotateFactor;
  127. if (Math.abs(rotateX) <= 90) {
  128. translateY = 0;
  129. translateZ = 90;
  130. transform = `rotateX(${rotateX}deg) `;
  131. }
  132. else {
  133. translateY = -9999;
  134. }
  135. }
  136. else {
  137. translateZ = 0;
  138. translateY = optOffset;
  139. }
  140. const selected = selectedIndex === i;
  141. transform += `translate3d(0px,${translateY}px,${translateZ}px) `;
  142. if (this.scaleFactor !== 1 && !selected) {
  143. transform += scaleStr;
  144. }
  145. // Update transition duration
  146. if (this.noAnimate) {
  147. opt.duration = 0;
  148. button.style.transitionDuration = '';
  149. }
  150. else if (duration !== opt.duration) {
  151. opt.duration = duration;
  152. button.style.transitionDuration = durationStr;
  153. }
  154. // Update transform
  155. if (transform !== opt.transform) {
  156. opt.transform = transform;
  157. }
  158. button.style.transform = transform;
  159. /**
  160. * Ensure that the select column
  161. * item has the selected class
  162. */
  163. opt.selected = selected;
  164. if (selected) {
  165. button.classList.add(PICKER_OPT_SELECTED);
  166. }
  167. else {
  168. button.classList.remove(PICKER_OPT_SELECTED);
  169. }
  170. }
  171. this.col.prevSelected = prevSelected;
  172. if (saveY) {
  173. this.y = y;
  174. }
  175. if (this.lastIndex !== selectedIndex) {
  176. // have not set a last index yet
  177. hapticSelectionChanged();
  178. this.lastIndex = selectedIndex;
  179. }
  180. }
  181. decelerate() {
  182. if (this.velocity !== 0) {
  183. // still decelerating
  184. this.velocity *= DECELERATION_FRICTION;
  185. // do not let it go slower than a velocity of 1
  186. this.velocity = this.velocity > 0 ? Math.max(this.velocity, 1) : Math.min(this.velocity, -1);
  187. let y = this.y + this.velocity;
  188. if (y > this.minY) {
  189. // whoops, it's trying to scroll up farther than the options we have!
  190. y = this.minY;
  191. this.velocity = 0;
  192. }
  193. else if (y < this.maxY) {
  194. // gahh, it's trying to scroll down farther than we can!
  195. y = this.maxY;
  196. this.velocity = 0;
  197. }
  198. this.update(y, 0, true);
  199. const notLockedIn = Math.round(y) % this.optHeight !== 0 || Math.abs(this.velocity) > 1;
  200. if (notLockedIn) {
  201. // isn't locked in yet, keep decelerating until it is
  202. this.rafId = requestAnimationFrame(() => this.decelerate());
  203. }
  204. else {
  205. this.velocity = 0;
  206. this.emitColChange();
  207. hapticSelectionEnd();
  208. }
  209. }
  210. else if (this.y % this.optHeight !== 0) {
  211. // needs to still get locked into a position so options line up
  212. const currentPos = Math.abs(this.y % this.optHeight);
  213. // create a velocity in the direction it needs to scroll
  214. this.velocity = currentPos > this.optHeight / 2 ? 1 : -1;
  215. this.decelerate();
  216. }
  217. }
  218. indexForY(y) {
  219. return Math.min(Math.max(Math.abs(Math.round(y / this.optHeight)), 0), this.col.options.length - 1);
  220. }
  221. onStart(detail) {
  222. // We have to prevent default in order to block scrolling under the picker
  223. // but we DO NOT have to stop propagation, since we still want
  224. // some "click" events to capture
  225. if (detail.event.cancelable) {
  226. detail.event.preventDefault();
  227. }
  228. detail.event.stopPropagation();
  229. hapticSelectionStart();
  230. // reset everything
  231. if (this.rafId !== undefined)
  232. cancelAnimationFrame(this.rafId);
  233. const options = this.col.options;
  234. let minY = options.length - 1;
  235. let maxY = 0;
  236. for (let i = 0; i < options.length; i++) {
  237. if (!options[i].disabled) {
  238. minY = Math.min(minY, i);
  239. maxY = Math.max(maxY, i);
  240. }
  241. }
  242. this.minY = -(minY * this.optHeight);
  243. this.maxY = -(maxY * this.optHeight);
  244. }
  245. onMove(detail) {
  246. if (detail.event.cancelable) {
  247. detail.event.preventDefault();
  248. }
  249. detail.event.stopPropagation();
  250. // update the scroll position relative to pointer start position
  251. let y = this.y + detail.deltaY;
  252. if (y > this.minY) {
  253. // scrolling up higher than scroll area
  254. y = Math.pow(y, 0.8);
  255. this.bounceFrom = y;
  256. }
  257. else if (y < this.maxY) {
  258. // scrolling down below scroll area
  259. y += Math.pow(this.maxY - y, 0.9);
  260. this.bounceFrom = y;
  261. }
  262. else {
  263. this.bounceFrom = 0;
  264. }
  265. this.update(y, 0, false);
  266. }
  267. onEnd(detail) {
  268. if (this.bounceFrom > 0) {
  269. // bounce back up
  270. this.update(this.minY, 100, true);
  271. this.emitColChange();
  272. return;
  273. }
  274. else if (this.bounceFrom < 0) {
  275. // bounce back down
  276. this.update(this.maxY, 100, true);
  277. this.emitColChange();
  278. return;
  279. }
  280. this.velocity = clamp(-MAX_PICKER_SPEED, detail.velocityY * 23, MAX_PICKER_SPEED);
  281. if (this.velocity === 0 && detail.deltaY === 0) {
  282. const opt = detail.event.target.closest('.picker-opt');
  283. if (opt === null || opt === void 0 ? void 0 : opt.hasAttribute('opt-index')) {
  284. this.setSelected(parseInt(opt.getAttribute('opt-index'), 10), TRANSITION_DURATION);
  285. }
  286. }
  287. else {
  288. this.y += detail.deltaY;
  289. if (Math.abs(detail.velocityY) < 0.05) {
  290. const isScrollingUp = detail.deltaY > 0;
  291. const optHeightFraction = (Math.abs(this.y) % this.optHeight) / this.optHeight;
  292. if (isScrollingUp && optHeightFraction > 0.5) {
  293. this.velocity = Math.abs(this.velocity) * -1;
  294. }
  295. else if (!isScrollingUp && optHeightFraction <= 0.5) {
  296. this.velocity = Math.abs(this.velocity);
  297. }
  298. }
  299. this.decelerate();
  300. }
  301. }
  302. refresh(forceRefresh, animated) {
  303. var _a;
  304. let min = this.col.options.length - 1;
  305. let max = 0;
  306. const options = this.col.options;
  307. for (let i = 0; i < options.length; i++) {
  308. if (!options[i].disabled) {
  309. min = Math.min(min, i);
  310. max = Math.max(max, i);
  311. }
  312. }
  313. /**
  314. * Only update selected value if column has a
  315. * velocity of 0. If it does not, then the
  316. * column is animating might land on
  317. * a value different than the value at
  318. * selectedIndex
  319. */
  320. if (this.velocity !== 0) {
  321. return;
  322. }
  323. const selectedIndex = clamp(min, (_a = this.col.selectedIndex) !== null && _a !== void 0 ? _a : 0, max);
  324. if (this.col.prevSelected !== selectedIndex || forceRefresh) {
  325. const y = selectedIndex * this.optHeight * -1;
  326. const duration = animated ? TRANSITION_DURATION : 0;
  327. this.velocity = 0;
  328. this.update(y, duration, true);
  329. }
  330. }
  331. onDomChange(forceRefresh, animated) {
  332. const colEl = this.optsEl;
  333. if (colEl) {
  334. // DOM READ
  335. // We perfom a DOM read over a rendered item, this needs to happen after the first render or after the the column has changed
  336. this.optHeight = colEl.firstElementChild ? colEl.firstElementChild.clientHeight : 0;
  337. }
  338. this.refresh(forceRefresh, animated);
  339. }
  340. render() {
  341. const col = this.col;
  342. const mode = getIonMode(this);
  343. return (h(Host, { key: '88a3c9397c9ac92dd814074c8ae6ecf8e3420a2c', class: Object.assign({ [mode]: true, 'picker-col': true, 'picker-opts-left': this.col.align === 'left', 'picker-opts-right': this.col.align === 'right' }, getClassMap(col.cssClass)), style: {
  344. 'max-width': this.col.columnWidth,
  345. } }, col.prefix && (h("div", { key: '4491a705d15337e6f45f3cf6fd21af5242474729', class: "picker-prefix", style: { width: col.prefixWidth } }, col.prefix)), h("div", { key: 'b0dd4b7a7a4c1edc4b73e7fb134ac85264072365', class: "picker-opts", style: { maxWidth: col.optionsWidth }, ref: (el) => (this.optsEl = el) }, col.options.map((o, index) => (h("button", { "aria-label": o.ariaLabel, class: { 'picker-opt': true, 'picker-opt-disabled': !!o.disabled }, "opt-index": index }, o.text)))), col.suffix && (h("div", { key: 'c16419ce6481d60fc3ba6b8d102a4edf0ede02aa', class: "picker-suffix", style: { width: col.suffixWidth } }, col.suffix))));
  346. }
  347. get el() { return this; }
  348. static get watchers() { return {
  349. "col": ["colChanged"]
  350. }; }
  351. static get style() { return {
  352. ios: IonPickerLegacyColumnIosStyle0,
  353. md: IonPickerLegacyColumnMdStyle0
  354. }; }
  355. }, [32, "ion-picker-legacy-column", {
  356. "col": [16]
  357. }, undefined, {
  358. "col": ["colChanged"]
  359. }]);
  360. const PICKER_OPT_SELECTED = 'picker-opt-selected';
  361. const DECELERATION_FRICTION = 0.97;
  362. const MAX_PICKER_SPEED = 90;
  363. const TRANSITION_DURATION = 150;
  364. function defineCustomElement() {
  365. if (typeof customElements === "undefined") {
  366. return;
  367. }
  368. const components = ["ion-picker-legacy-column"];
  369. components.forEach(tagName => { switch (tagName) {
  370. case "ion-picker-legacy-column":
  371. if (!customElements.get(tagName)) {
  372. customElements.define(tagName, PickerColumnCmp);
  373. }
  374. break;
  375. } });
  376. }
  377. export { PickerColumnCmp as P, defineCustomElement as d };