Front end of the Slack clone application.

picker-component.js 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. export class PickerCmp {
  13. constructor(_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(cssClass => {
  22. renderer.setElementClass(_elementRef.nativeElement, cssClass, true);
  23. });
  24. }
  25. this.id = (++pickerIds);
  26. this.lastClick = 0;
  27. }
  28. ionViewWillLoad() {
  29. // normalize the data
  30. let data = this.d;
  31. data.buttons = data.buttons.map(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(column => {
  42. if (!isPresent(column.options)) {
  43. column.options = [];
  44. }
  45. column.selectedIndex = column.selectedIndex || 0;
  46. column.options = column.options.map(inputOpt => {
  47. let 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. ionViewDidLoad() {
  68. this.refresh();
  69. }
  70. ionViewWillEnter() {
  71. this._gestureBlocker.block();
  72. }
  73. ionViewDidLeave() {
  74. this._gestureBlocker.unblock();
  75. }
  76. refresh() {
  77. this._cols.forEach(column => column.refresh());
  78. }
  79. _colChange() {
  80. // one of the columns has changed its selected index
  81. var picker = this._viewCtrl;
  82. picker.ionChange.emit(this.getSelected());
  83. }
  84. _keyUp(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. let 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. ionViewDidEnter() {
  104. let focusableEle = this._elementRef.nativeElement.querySelector('button');
  105. if (focusableEle) {
  106. focusableEle.focus();
  107. }
  108. this.enabled = true;
  109. }
  110. btnClick(button) {
  111. if (!this.enabled) {
  112. return;
  113. }
  114. // keep the time of the most recent button click
  115. this.lastClick = Date.now();
  116. let 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. bdClick() {
  130. if (this.enabled && this.d.enableBackdropDismiss) {
  131. let cancelBtn = this.d.buttons.find(b => b.role === 'cancel');
  132. if (cancelBtn) {
  133. this.btnClick(cancelBtn);
  134. }
  135. else {
  136. this.dismiss('backdrop');
  137. }
  138. }
  139. }
  140. dismiss(role) {
  141. return this._viewCtrl.dismiss(this.getSelected(), role);
  142. }
  143. getSelected() {
  144. let selected = {};
  145. this.d.columns.forEach((col, index) => {
  146. let 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. ngOnDestroy() {
  156. (void 0) /* assert */;
  157. this._gestureBlocker.destroy();
  158. }
  159. }
  160. PickerCmp.decorators = [
  161. { type: Component, args: [{
  162. selector: 'ion-picker-cmp',
  163. template: `
  164. <ion-backdrop (click)="bdClick()"></ion-backdrop>
  165. <div class="picker-wrapper">
  166. <div class="picker-toolbar">
  167. <div *ngFor="let b of d.buttons" class="picker-toolbar-button" [ngClass]="b.cssRole">
  168. <button ion-button (click)="btnClick(b)" [ngClass]="b.cssClass" class="picker-button" clear>
  169. {{b.text}}
  170. </button>
  171. </div>
  172. </div>
  173. <div class="picker-columns">
  174. <div class="picker-above-highlight"></div>
  175. <div *ngFor="let c of d.columns" [col]="c" class="picker-col" (ionChange)="_colChange($event)"></div>
  176. <div class="picker-below-highlight"></div>
  177. </div>
  178. </div>
  179. `,
  180. host: {
  181. 'role': 'dialog'
  182. },
  183. encapsulation: ViewEncapsulation.None,
  184. },] },
  185. ];
  186. /** @nocollapse */
  187. PickerCmp.ctorParameters = () => [
  188. { type: ViewController, },
  189. { type: ElementRef, },
  190. { type: Config, },
  191. { type: GestureController, },
  192. { type: NavParams, },
  193. { type: Renderer, },
  194. ];
  195. PickerCmp.propDecorators = {
  196. '_cols': [{ type: ViewChildren, args: [PickerColumnCmp,] },],
  197. '_keyUp': [{ type: HostListener, args: ['body:keyup', ['$event'],] },],
  198. };
  199. let pickerIds = -1;
  200. //# sourceMappingURL=picker-component.js.map