a zip code crypto-currency system good for red ONLY

picker-column.js 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. import { Component, ElementRef, EventEmitter, Input, NgZone, Output, ViewChild } from '@angular/core';
  2. import { clamp } from '../../util/util';
  3. import { Config } from '../../config/config';
  4. import { DomController } from '../../platform/dom-controller';
  5. import { Haptic } from '../../tap-click/haptic';
  6. import { DECELERATION_FRICTION, FRAME_MS, MAX_PICKER_SPEED, PICKER_OPT_SELECTED } from './picker-options';
  7. import { Platform } from '../../platform/platform';
  8. import { pointerCoord } from '../../util/dom';
  9. import { UIEventManager } from '../../gestures/ui-event-manager';
  10. /**
  11. * @hidden
  12. */
  13. export class PickerColumnCmp {
  14. constructor(config, _plt, elementRef, _zone, _haptic, plt, domCtrl) {
  15. this._plt = _plt;
  16. this.elementRef = elementRef;
  17. this._zone = _zone;
  18. this._haptic = _haptic;
  19. this.y = 0;
  20. this.pos = [];
  21. this.startY = null;
  22. this.ionChange = new EventEmitter();
  23. this.events = new UIEventManager(plt);
  24. this.rotateFactor = config.getNumber('pickerRotateFactor', 0);
  25. this.scaleFactor = config.getNumber('pickerScaleFactor', 1);
  26. this.decelerateFunc = this.decelerate.bind(this);
  27. this.debouncer = domCtrl.debouncer();
  28. }
  29. ngAfterViewInit() {
  30. // get the scrollable element within the column
  31. let colEle = this.colEle.nativeElement;
  32. this.colHeight = colEle.clientHeight;
  33. // get the height of one option
  34. this.optHeight = (colEle.firstElementChild ? colEle.firstElementChild.clientHeight : 0);
  35. // Listening for pointer events
  36. this.events.pointerEvents({
  37. element: this.elementRef.nativeElement,
  38. pointerDown: this.pointerStart.bind(this),
  39. pointerMove: this.pointerMove.bind(this),
  40. pointerUp: this.pointerEnd.bind(this),
  41. capture: true,
  42. zone: false
  43. });
  44. }
  45. ngOnDestroy() {
  46. this._plt.cancelRaf(this.rafId);
  47. this.events.destroy();
  48. }
  49. pointerStart(ev) {
  50. (void 0) /* console.debug */;
  51. this._haptic.gestureSelectionStart();
  52. // We have to prevent default in order to block scrolling under the picker
  53. // but we DO NOT have to stop propagation, since we still want
  54. // some "click" events to capture
  55. ev.preventDefault();
  56. // cancel any previous raf's that haven't fired yet
  57. this._plt.cancelRaf(this.rafId);
  58. // remember where the pointer started from`
  59. this.startY = pointerCoord(ev).y;
  60. // reset everything
  61. this.velocity = 0;
  62. this.pos.length = 0;
  63. this.pos.push(this.startY, Date.now());
  64. let options = this.col.options;
  65. let minY = (options.length - 1);
  66. let maxY = 0;
  67. for (var i = 0; i < options.length; i++) {
  68. if (!options[i].disabled) {
  69. minY = Math.min(minY, i);
  70. maxY = Math.max(maxY, i);
  71. }
  72. }
  73. this.minY = (minY * this.optHeight * -1);
  74. this.maxY = (maxY * this.optHeight * -1);
  75. return true;
  76. }
  77. pointerMove(ev) {
  78. ev.preventDefault();
  79. ev.stopPropagation();
  80. let currentY = pointerCoord(ev).y;
  81. this.pos.push(currentY, Date.now());
  82. this.debouncer.write(() => {
  83. if (this.startY === null) {
  84. return;
  85. }
  86. // update the scroll position relative to pointer start position
  87. let y = this.y + (currentY - this.startY);
  88. if (y > this.minY) {
  89. // scrolling up higher than scroll area
  90. y = Math.pow(y, 0.8);
  91. this.bounceFrom = y;
  92. }
  93. else if (y < this.maxY) {
  94. // scrolling down below scroll area
  95. y += Math.pow(this.maxY - y, 0.9);
  96. this.bounceFrom = y;
  97. }
  98. else {
  99. this.bounceFrom = 0;
  100. }
  101. this.update(y, 0, false, false);
  102. let currentIndex = Math.max(Math.abs(Math.round(y / this.optHeight)), 0);
  103. if (currentIndex !== this.lastTempIndex) {
  104. // Trigger a haptic event for physical feedback that the index has changed
  105. this._haptic.gestureSelectionChanged();
  106. this.lastTempIndex = currentIndex;
  107. }
  108. });
  109. }
  110. pointerEnd(ev) {
  111. ev.preventDefault();
  112. this.debouncer.cancel();
  113. if (this.startY === null) {
  114. return;
  115. }
  116. (void 0) /* console.debug */;
  117. this.velocity = 0;
  118. if (this.bounceFrom > 0) {
  119. // bounce back up
  120. this.update(this.minY, 100, true, true);
  121. return;
  122. }
  123. else if (this.bounceFrom < 0) {
  124. // bounce back down
  125. this.update(this.maxY, 100, true, true);
  126. return;
  127. }
  128. let endY = pointerCoord(ev).y;
  129. this.pos.push(endY, Date.now());
  130. let endPos = (this.pos.length - 1);
  131. let startPos = endPos;
  132. let timeRange = (Date.now() - 100);
  133. // move pointer to position measured 100ms ago
  134. for (var i = endPos; i > 0 && this.pos[i] > timeRange; i -= 2) {
  135. startPos = i;
  136. }
  137. if (startPos !== endPos) {
  138. // compute relative movement between these two points
  139. var timeOffset = (this.pos[endPos] - this.pos[startPos]);
  140. var movedTop = (this.pos[startPos - 1] - this.pos[endPos - 1]);
  141. // based on XXms compute the movement to apply for each render step
  142. var velocity = ((movedTop / timeOffset) * FRAME_MS);
  143. this.velocity = clamp(-MAX_PICKER_SPEED, velocity, MAX_PICKER_SPEED);
  144. }
  145. if (Math.abs(endY - this.startY) > 3) {
  146. var y = this.y + (endY - this.startY);
  147. this.update(y, 0, true, true);
  148. }
  149. this.startY = null;
  150. this.decelerate();
  151. }
  152. decelerate() {
  153. let y = 0;
  154. if (isNaN(this.y) || !this.optHeight) {
  155. // fallback in case numbers get outta wack
  156. this.update(y, 0, true, true);
  157. this._haptic.gestureSelectionEnd();
  158. }
  159. else if (Math.abs(this.velocity) > 0) {
  160. // still decelerating
  161. this.velocity *= DECELERATION_FRICTION;
  162. // do not let it go slower than a velocity of 1
  163. this.velocity = (this.velocity > 0)
  164. ? Math.max(this.velocity, 1)
  165. : Math.min(this.velocity, -1);
  166. y = Math.round(this.y - this.velocity);
  167. if (y > this.minY) {
  168. // whoops, it's trying to scroll up farther than the options we have!
  169. y = this.minY;
  170. this.velocity = 0;
  171. }
  172. else if (y < this.maxY) {
  173. // gahh, it's trying to scroll down farther than we can!
  174. y = this.maxY;
  175. this.velocity = 0;
  176. }
  177. var notLockedIn = (y % this.optHeight !== 0 || Math.abs(this.velocity) > 1);
  178. this.update(y, 0, true, !notLockedIn);
  179. if (notLockedIn) {
  180. // isn't locked in yet, keep decelerating until it is
  181. this.rafId = this._plt.raf(this.decelerateFunc);
  182. }
  183. }
  184. else if (this.y % this.optHeight !== 0) {
  185. // needs to still get locked into a position so options line up
  186. var currentPos = Math.abs(this.y % this.optHeight);
  187. // create a velocity in the direction it needs to scroll
  188. this.velocity = (currentPos > (this.optHeight / 2) ? 1 : -1);
  189. this._haptic.gestureSelectionEnd();
  190. this.decelerate();
  191. }
  192. let currentIndex = Math.max(Math.abs(Math.round(y / this.optHeight)), 0);
  193. if (currentIndex !== this.lastTempIndex) {
  194. // Trigger a haptic event for physical feedback that the index has changed
  195. this._haptic.gestureSelectionChanged();
  196. }
  197. this.lastTempIndex = currentIndex;
  198. }
  199. optClick(ev, index) {
  200. if (!this.velocity) {
  201. ev.preventDefault();
  202. ev.stopPropagation();
  203. this.setSelected(index, 150);
  204. }
  205. }
  206. setSelected(selectedIndex, duration) {
  207. // if there is a selected index, then figure out it's y position
  208. // if there isn't a selected index, then just use the top y position
  209. let y = (selectedIndex > -1) ? ((selectedIndex * this.optHeight) * -1) : 0;
  210. this._plt.cancelRaf(this.rafId);
  211. this.velocity = 0;
  212. // so what y position we're at
  213. this.update(y, duration, true, true);
  214. }
  215. update(y, duration, saveY, emitChange) {
  216. // ensure we've got a good round number :)
  217. y = Math.round(y);
  218. let i;
  219. let button;
  220. let opt;
  221. let optOffset;
  222. let visible;
  223. let translateX;
  224. let translateY;
  225. let translateZ;
  226. let rotateX;
  227. let transform;
  228. let selected;
  229. const parent = this.colEle.nativeElement;
  230. const children = parent.children;
  231. const length = children.length;
  232. const selectedIndex = this.col.selectedIndex = Math.min(Math.max(Math.round(-y / this.optHeight), 0), length - 1);
  233. const durationStr = (duration === 0) ? null : duration + 'ms';
  234. const scaleStr = `scale(${this.scaleFactor})`;
  235. for (i = 0; i < length; i++) {
  236. button = children[i];
  237. opt = this.col.options[i];
  238. optOffset = (i * this.optHeight) + y;
  239. visible = true;
  240. transform = '';
  241. if (this.rotateFactor !== 0) {
  242. rotateX = optOffset * this.rotateFactor;
  243. if (Math.abs(rotateX) > 90) {
  244. visible = false;
  245. }
  246. else {
  247. translateX = 0;
  248. translateY = 0;
  249. translateZ = 90;
  250. transform = `rotateX(${rotateX}deg) `;
  251. }
  252. }
  253. else {
  254. translateX = 0;
  255. translateZ = 0;
  256. translateY = optOffset;
  257. if (Math.abs(translateY) > 170) {
  258. visible = false;
  259. }
  260. }
  261. selected = selectedIndex === i;
  262. if (visible) {
  263. transform += `translate3d(0px,${translateY}px,${translateZ}px) `;
  264. if (this.scaleFactor !== 1 && !selected) {
  265. transform += scaleStr;
  266. }
  267. }
  268. else {
  269. transform = 'translate3d(-9999px,0px,0px)';
  270. }
  271. // Update transition duration
  272. if (duration !== opt._dur) {
  273. opt._dur = duration;
  274. button.style[this._plt.Css.transitionDuration] = durationStr;
  275. }
  276. // Update transform
  277. if (transform !== opt._trans) {
  278. opt._trans = transform;
  279. button.style[this._plt.Css.transform] = transform;
  280. }
  281. // Update selected item
  282. if (selected !== opt._selected) {
  283. opt._selected = selected;
  284. if (selected) {
  285. button.classList.add(PICKER_OPT_SELECTED);
  286. }
  287. else {
  288. button.classList.remove(PICKER_OPT_SELECTED);
  289. }
  290. }
  291. }
  292. this.col.prevSelected = selectedIndex;
  293. if (saveY) {
  294. this.y = y;
  295. }
  296. if (emitChange) {
  297. if (this.lastIndex === undefined) {
  298. // have not set a last index yet
  299. this.lastIndex = this.col.selectedIndex;
  300. }
  301. else if (this.lastIndex !== this.col.selectedIndex) {
  302. // new selected index has changed from the last index
  303. // update the lastIndex and emit that it has changed
  304. this.lastIndex = this.col.selectedIndex;
  305. var ionChange = this.ionChange;
  306. if (ionChange.observers.length > 0) {
  307. this._zone.run(ionChange.emit.bind(ionChange, this.col.options[this.col.selectedIndex]));
  308. }
  309. }
  310. }
  311. }
  312. refresh() {
  313. let min = this.col.options.length - 1;
  314. let max = 0;
  315. const options = this.col.options;
  316. for (var i = 0; i < options.length; i++) {
  317. if (!options[i].disabled) {
  318. min = Math.min(min, i);
  319. max = Math.max(max, i);
  320. }
  321. }
  322. const selectedIndex = clamp(min, this.col.selectedIndex, max);
  323. if (this.col.prevSelected !== selectedIndex) {
  324. var y = (selectedIndex * this.optHeight) * -1;
  325. this._plt.cancelRaf(this.rafId);
  326. this.velocity = 0;
  327. this.update(y, 150, true, false);
  328. }
  329. }
  330. }
  331. PickerColumnCmp.decorators = [
  332. { type: Component, args: [{
  333. selector: '.picker-col',
  334. template: '<div *ngIf="col.prefix" class="picker-prefix" [style.width]="col.prefixWidth">{{col.prefix}}</div>' +
  335. '<div class="picker-opts" #colEle [style.max-width]="col.optionsWidth">' +
  336. '<button *ngFor="let o of col.options; let i=index"' +
  337. '[class.picker-opt-disabled]="o.disabled" ' +
  338. 'class="picker-opt" disable-activated (click)="optClick($event, i)">' +
  339. '{{o.text}}' +
  340. '</button>' +
  341. '</div>' +
  342. '<div *ngIf="col.suffix" class="picker-suffix" [style.width]="col.suffixWidth">{{col.suffix}}</div>',
  343. host: {
  344. '[style.max-width]': 'col.columnWidth',
  345. '[class.picker-opts-left]': 'col.align=="left"',
  346. '[class.picker-opts-right]': 'col.align=="right"',
  347. }
  348. },] },
  349. ];
  350. /** @nocollapse */
  351. PickerColumnCmp.ctorParameters = () => [
  352. { type: Config, },
  353. { type: Platform, },
  354. { type: ElementRef, },
  355. { type: NgZone, },
  356. { type: Haptic, },
  357. { type: Platform, },
  358. { type: DomController, },
  359. ];
  360. PickerColumnCmp.propDecorators = {
  361. 'colEle': [{ type: ViewChild, args: ['colEle',] },],
  362. 'col': [{ type: Input },],
  363. 'ionChange': [{ type: Output },],
  364. };
  365. //# sourceMappingURL=picker-column.js.map