UI for Zipcoin Blue

picker-component.js 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. import { Component, ElementRef, HostListener, Renderer, ViewChildren, ViewEncapsulation } from '@angular/core';
  2. import { isNumber, isPresent, isString } from '../../util/util';
  3. import { Config } from '../../config/config';
  4. import { BLOCK_ALL, GestureController, } from '../../gestures/gesture-controller';
  5. import { KEY_ENTER, KEY_ESCAPE } from '../../platform/key';
  6. import { NavParams } from '../../navigation/nav-params';
  7. import { ViewController } from '../../navigation/view-controller';
  8. import { PickerColumnCmp } from './picker-column';
  9. /**
  10. * @hidden
  11. */
  12. var PickerCmp = (function () {
  13. function PickerCmp(_viewCtrl, _elementRef, config, gestureCtrl, params, renderer) {
  14. this._viewCtrl = _viewCtrl;
  15. this._elementRef = _elementRef;
  16. this._gestureBlocker = gestureCtrl.createBlocker(BLOCK_ALL);
  17. this.d = params.data;
  18. this.mode = config.get('mode');
  19. renderer.setElementClass(_elementRef.nativeElement, "picker-" + this.mode, true);
  20. if (this.d.cssClass) {
  21. this.d.cssClass.split(' ').forEach(function (cssClass) {
  22. renderer.setElementClass(_elementRef.nativeElement, cssClass, true);
  23. });
  24. }
  25. this.id = (++pickerIds);
  26. this.lastClick = 0;
  27. }
  28. PickerCmp.prototype.ionViewWillLoad = function () {
  29. // normalize the data
  30. var data = this.d;
  31. data.buttons = data.buttons.map(function (button) {
  32. if (isString(button)) {
  33. return { text: button };
  34. }
  35. if (button.role) {
  36. button.cssRole = "picker-toolbar-" + button.role;
  37. }
  38. return button;
  39. });
  40. // clean up dat data
  41. data.columns = data.columns.map(function (column) {
  42. if (!isPresent(column.options)) {
  43. column.options = [];
  44. }
  45. column.selectedIndex = column.selectedIndex || 0;
  46. column.options = column.options.map(function (inputOpt) {
  47. var opt = {
  48. text: '',
  49. value: '',
  50. disabled: inputOpt.disabled,
  51. };
  52. if (isPresent(inputOpt)) {
  53. if (isString(inputOpt) || isNumber(inputOpt)) {
  54. opt.text = inputOpt.toString();
  55. opt.value = inputOpt;
  56. }
  57. else {
  58. opt.text = isPresent(inputOpt.text) ? inputOpt.text : inputOpt.value;
  59. opt.value = isPresent(inputOpt.value) ? inputOpt.value : inputOpt.text;
  60. }
  61. }
  62. return opt;
  63. });
  64. return column;
  65. });
  66. };
  67. PickerCmp.prototype.ionViewDidLoad = function () {
  68. this.refresh();
  69. };
  70. PickerCmp.prototype.ionViewWillEnter = function () {
  71. this._gestureBlocker.block();
  72. };
  73. PickerCmp.prototype.ionViewDidLeave = function () {
  74. this._gestureBlocker.unblock();
  75. };
  76. PickerCmp.prototype.refresh = function () {
  77. this._cols.forEach(function (column) { return column.refresh(); });
  78. };
  79. PickerCmp.prototype._colChange = function () {
  80. // one of the columns has changed its selected index
  81. var picker = this._viewCtrl;
  82. picker.ionChange.emit(this.getSelected());
  83. };
  84. PickerCmp.prototype._keyUp = function (ev) {
  85. if (this.enabled && this._viewCtrl.isLast()) {
  86. if (ev.keyCode === KEY_ENTER) {
  87. if (this.lastClick + 1000 < Date.now()) {
  88. // do not fire this click if there recently was already a click
  89. // this can happen when the button has focus and used the enter
  90. // key to click the button. However, both the click handler and
  91. // this keyup event will fire, so only allow one of them to go.
  92. (void 0) /* console.debug */;
  93. var button = this.d.buttons[this.d.buttons.length - 1];
  94. this.btnClick(button);
  95. }
  96. }
  97. else if (ev.keyCode === KEY_ESCAPE) {
  98. (void 0) /* console.debug */;
  99. this.bdClick();
  100. }
  101. }
  102. };
  103. PickerCmp.prototype.ionViewDidEnter = function () {
  104. var focusableEle = this._elementRef.nativeElement.querySelector('button');
  105. if (focusableEle) {
  106. focusableEle.focus();
  107. }
  108. this.enabled = true;
  109. };
  110. PickerCmp.prototype.btnClick = function (button) {
  111. if (!this.enabled) {
  112. return;
  113. }
  114. // keep the time of the most recent button click
  115. this.lastClick = Date.now();
  116. var shouldDismiss = true;
  117. if (button.handler) {
  118. // a handler has been provided, execute it
  119. // pass the handler the values from the inputs
  120. if (button.handler(this.getSelected()) === false) {
  121. // if the return value of the handler is false then do not dismiss
  122. shouldDismiss = false;
  123. }
  124. }
  125. if (shouldDismiss) {
  126. this.dismiss(button.role);
  127. }
  128. };
  129. PickerCmp.prototype.bdClick = function () {
  130. if (this.enabled && this.d.enableBackdropDismiss) {
  131. var cancelBtn = this.d.buttons.find(function (b) { return b.role === 'cancel'; });
  132. if (cancelBtn) {
  133. this.btnClick(cancelBtn);
  134. }
  135. else {
  136. this.dismiss('backdrop');
  137. }
  138. }
  139. };
  140. PickerCmp.prototype.dismiss = function (role) {
  141. return this._viewCtrl.dismiss(this.getSelected(), role);
  142. };
  143. PickerCmp.prototype.getSelected = function () {
  144. var selected = {};
  145. this.d.columns.forEach(function (col, index) {
  146. var selectedColumn = col.options[col.selectedIndex];
  147. selected[col.name] = {
  148. text: selectedColumn ? selectedColumn.text : null,
  149. value: selectedColumn ? selectedColumn.value : null,
  150. columnIndex: index,
  151. };
  152. });
  153. return selected;
  154. };
  155. PickerCmp.prototype.ngOnDestroy = function () {
  156. (void 0) /* assert */;
  157. this._gestureBlocker.destroy();
  158. };
  159. PickerCmp.decorators = [
  160. { type: Component, args: [{
  161. selector: 'ion-picker-cmp',
  162. template: "\n <ion-backdrop (click)=\"bdClick()\"></ion-backdrop>\n <div class=\"picker-wrapper\">\n <div class=\"picker-toolbar\">\n <div *ngFor=\"let b of d.buttons\" class=\"picker-toolbar-button\" [ngClass]=\"b.cssRole\">\n <button ion-button (click)=\"btnClick(b)\" [ngClass]=\"b.cssClass\" class=\"picker-button\" clear>\n {{b.text}}\n </button>\n </div>\n </div>\n <div class=\"picker-columns\">\n <div class=\"picker-above-highlight\"></div>\n <div *ngFor=\"let c of d.columns\" [col]=\"c\" class=\"picker-col\" (ionChange)=\"_colChange($event)\"></div>\n <div class=\"picker-below-highlight\"></div>\n </div>\n </div>\n ",
  163. host: {
  164. 'role': 'dialog'
  165. },
  166. encapsulation: ViewEncapsulation.None,
  167. },] },
  168. ];
  169. /** @nocollapse */
  170. PickerCmp.ctorParameters = function () { return [
  171. { type: ViewController, },
  172. { type: ElementRef, },
  173. { type: Config, },
  174. { type: GestureController, },
  175. { type: NavParams, },
  176. { type: Renderer, },
  177. ]; };
  178. PickerCmp.propDecorators = {
  179. '_cols': [{ type: ViewChildren, args: [PickerColumnCmp,] },],
  180. '_keyUp': [{ type: HostListener, args: ['body:keyup', ['$event'],] },],
  181. };
  182. return PickerCmp;
  183. }());
  184. export { PickerCmp };
  185. var pickerIds = -1;
  186. //# sourceMappingURL=picker-component.js.map