a zip code crypto-currency system good for red ONLY

alert-component.js 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. import { Component, ElementRef, HostListener, Renderer, ViewEncapsulation } from '@angular/core';
  2. import { Config } from '../../config/config';
  3. import { NON_TEXT_INPUT_REGEX } from '../../util/dom';
  4. import { BLOCK_ALL, GestureController } from '../../gestures/gesture-controller';
  5. import { isPresent } from '../../util/util';
  6. import { KEY_ENTER, KEY_ESCAPE } from '../../platform/key';
  7. import { NavParams } from '../../navigation/nav-params';
  8. import { Platform } from '../../platform/platform';
  9. import { ViewController } from '../../navigation/view-controller';
  10. /**
  11. * @hidden
  12. */
  13. export class AlertCmp {
  14. constructor(_viewCtrl, _elementRef, config, gestureCtrl, params, _renderer, _plt) {
  15. this._viewCtrl = _viewCtrl;
  16. this._elementRef = _elementRef;
  17. this._renderer = _renderer;
  18. this._plt = _plt;
  19. // gesture blocker is used to disable gestures dynamically
  20. this.gestureBlocker = gestureCtrl.createBlocker(BLOCK_ALL);
  21. this.d = params.data;
  22. this.mode = this.d.mode || config.get('mode');
  23. this.keyboardResizes = config.getBoolean('keyboardResizes', false);
  24. _renderer.setElementClass(_elementRef.nativeElement, `alert-${this.mode}`, true);
  25. if (this.d.cssClass) {
  26. this.d.cssClass.split(' ').forEach(cssClass => {
  27. // Make sure the class isn't whitespace, otherwise it throws exceptions
  28. if (cssClass.trim() !== '')
  29. _renderer.setElementClass(_elementRef.nativeElement, cssClass, true);
  30. });
  31. }
  32. this.id = (++alertIds);
  33. this.descId = '';
  34. this.hdrId = 'alert-hdr-' + this.id;
  35. this.subHdrId = 'alert-subhdr-' + this.id;
  36. this.msgId = 'alert-msg-' + this.id;
  37. this.activeId = '';
  38. this.lastClick = 0;
  39. if (this.d.message) {
  40. this.descId = this.msgId;
  41. }
  42. else if (this.d.subTitle) {
  43. this.descId = this.subHdrId;
  44. }
  45. if (!this.d.message) {
  46. this.d.message = '';
  47. }
  48. }
  49. ionViewDidLoad() {
  50. // normalize the data
  51. const data = this.d;
  52. data.buttons = data.buttons.map(button => {
  53. if (typeof button === 'string') {
  54. return { text: button };
  55. }
  56. return button;
  57. });
  58. data.inputs = data.inputs.map((input, index) => {
  59. let r = {
  60. type: input.type || 'text',
  61. name: isPresent(input.name) ? input.name : index + '',
  62. placeholder: isPresent(input.placeholder) ? input.placeholder : '',
  63. value: isPresent(input.value) ? input.value : '',
  64. label: input.label,
  65. checked: !!input.checked,
  66. disabled: !!input.disabled,
  67. id: isPresent(input.id) ? input.id : `alert-input-${this.id}-${index}`,
  68. handler: isPresent(input.handler) ? input.handler : null,
  69. min: isPresent(input.min) ? input.min : null,
  70. max: isPresent(input.max) ? input.max : null
  71. };
  72. return r;
  73. });
  74. // An alert can be created with several different inputs. Radios,
  75. // checkboxes and inputs are all accepted, but they cannot be mixed.
  76. const inputTypes = [];
  77. data.inputs.forEach(input => {
  78. if (inputTypes.indexOf(input.type) < 0) {
  79. inputTypes.push(input.type);
  80. }
  81. });
  82. if (inputTypes.length > 1 && (inputTypes.indexOf('checkbox') > -1 || inputTypes.indexOf('radio') > -1)) {
  83. console.warn(`Alert cannot mix input types: ${(inputTypes.join('/'))}. Please see alert docs for more info.`);
  84. }
  85. this.inputType = inputTypes.length ? inputTypes[0] : null;
  86. const checkedInput = this.d.inputs.find(input => input.checked);
  87. if (checkedInput) {
  88. this.activeId = checkedInput.id;
  89. }
  90. const hasTextInput = (this.d.inputs.length && this.d.inputs.some(i => !(NON_TEXT_INPUT_REGEX.test(i.type))));
  91. if (!this.keyboardResizes && hasTextInput && this._plt.is('mobile')) {
  92. // this alert has a text input and it's on a mobile device so we should align
  93. // the alert up high because we need to leave space for the virtual keboard
  94. // this also helps prevent the layout getting all messed up from
  95. // the browser trying to scroll the input into a safe area
  96. this._renderer.setElementClass(this._elementRef.nativeElement, 'alert-top', true);
  97. }
  98. }
  99. ionViewWillEnter() {
  100. this.gestureBlocker.block();
  101. }
  102. ionViewDidLeave() {
  103. this.gestureBlocker.unblock();
  104. }
  105. ionViewDidEnter() {
  106. // set focus on the first input or button in the alert
  107. // note that this does not always work and bring up the keyboard on
  108. // devices since the focus command must come from the user's touch event
  109. // and ionViewDidEnter is not in the same callstack as the touch event :(
  110. const focusableEle = this._elementRef.nativeElement.querySelector('input,button');
  111. if (focusableEle) {
  112. setTimeout(() => focusableEle.focus());
  113. }
  114. this.enabled = true;
  115. }
  116. keyUp(ev) {
  117. if (this.enabled && this._viewCtrl.isLast()) {
  118. if (ev.keyCode === KEY_ENTER) {
  119. if (this.lastClick + 1000 < Date.now()) {
  120. // do not fire this click if there recently was already a click
  121. // this can happen when the button has focus and used the enter
  122. // key to click the button. However, both the click handler and
  123. // this keyup event will fire, so only allow one of them to go.
  124. (void 0) /* console.debug */;
  125. let button = this.d.buttons[this.d.buttons.length - 1];
  126. this.btnClick(button);
  127. }
  128. }
  129. else if (ev.keyCode === KEY_ESCAPE) {
  130. (void 0) /* console.debug */;
  131. this.bdClick();
  132. }
  133. }
  134. }
  135. btnClick(button) {
  136. if (!this.enabled) {
  137. return;
  138. }
  139. // keep the time of the most recent button click
  140. this.lastClick = Date.now();
  141. let shouldDismiss = true;
  142. if (button.handler) {
  143. // a handler has been provided, execute it
  144. // pass the handler the values from the inputs
  145. if (button.handler(this.getValues()) === false) {
  146. // if the return value of the handler is false then do not dismiss
  147. shouldDismiss = false;
  148. }
  149. }
  150. if (shouldDismiss) {
  151. this.dismiss(button.role);
  152. }
  153. }
  154. rbClick(checkedInput) {
  155. if (this.enabled) {
  156. this.d.inputs.forEach(input => {
  157. input.checked = (checkedInput === input);
  158. });
  159. this.activeId = checkedInput.id;
  160. if (checkedInput.handler) {
  161. checkedInput.handler(checkedInput);
  162. }
  163. }
  164. }
  165. cbClick(checkedInput) {
  166. if (this.enabled) {
  167. checkedInput.checked = !checkedInput.checked;
  168. if (checkedInput.handler) {
  169. checkedInput.handler(checkedInput);
  170. }
  171. }
  172. }
  173. bdClick() {
  174. if (this.enabled && this.d.enableBackdropDismiss) {
  175. var cancelBtn = this.d.buttons.find(b => b.role === 'cancel');
  176. if (cancelBtn) {
  177. this.btnClick(cancelBtn);
  178. }
  179. else {
  180. this.dismiss('backdrop');
  181. }
  182. }
  183. }
  184. dismiss(role) {
  185. const opts = {
  186. minClickBlockDuration: 400
  187. };
  188. return this._viewCtrl.dismiss(this.getValues(), role, opts);
  189. }
  190. getValues() {
  191. if (this.inputType === 'radio') {
  192. // this is an alert with radio buttons (single value select)
  193. // return the one value which is checked, otherwise undefined
  194. const checkedInput = this.d.inputs.find(i => i.checked);
  195. return checkedInput ? checkedInput.value : undefined;
  196. }
  197. if (this.inputType === 'checkbox') {
  198. // this is an alert with checkboxes (multiple value select)
  199. // return an array of all the checked values
  200. return this.d.inputs.filter(i => i.checked).map(i => i.value);
  201. }
  202. if (this.d.inputs.length === 0) {
  203. // this is an alert without any options/inputs at all
  204. return undefined;
  205. }
  206. // this is an alert with text inputs
  207. // return an object of all the values with the input name as the key
  208. const values = {};
  209. this.d.inputs.forEach(i => {
  210. values[i.name] = i.value;
  211. });
  212. return values;
  213. }
  214. ngOnDestroy() {
  215. (void 0) /* assert */;
  216. this.gestureBlocker.destroy();
  217. }
  218. }
  219. AlertCmp.decorators = [
  220. { type: Component, args: [{
  221. selector: 'ion-alert',
  222. template: '<ion-backdrop (click)="bdClick()" [class.backdrop-no-tappable]="!d.enableBackdropDismiss"></ion-backdrop>' +
  223. '<div class="alert-wrapper">' +
  224. '<div class="alert-head">' +
  225. '<h2 id="{{hdrId}}" class="alert-title" *ngIf="d.title" [innerHTML]="d.title"></h2>' +
  226. '<h3 id="{{subHdrId}}" class="alert-sub-title" *ngIf="d.subTitle" [innerHTML]="d.subTitle"></h3>' +
  227. '</div>' +
  228. '<div id="{{msgId}}" class="alert-message" [innerHTML]="d.message"></div>' +
  229. '<div *ngIf="d.inputs.length" [ngSwitch]="inputType">' +
  230. '<ng-template ngSwitchCase="radio">' +
  231. '<div class="alert-radio-group" role="radiogroup" [attr.aria-labelledby]="hdrId" [attr.aria-activedescendant]="activeId">' +
  232. '<button ion-button="alert-radio-button" *ngFor="let i of d.inputs" (click)="rbClick(i)" [attr.aria-checked]="i.checked" [disabled]="i.disabled" [attr.id]="i.id" class="alert-tappable alert-radio" role="radio">' +
  233. '<div class="alert-radio-icon"><div class="alert-radio-inner"></div></div>' +
  234. '<div class="alert-radio-label">' +
  235. '{{i.label}}' +
  236. '</div>' +
  237. '</button>' +
  238. '</div>' +
  239. '</ng-template>' +
  240. '<ng-template ngSwitchCase="checkbox">' +
  241. '<div class="alert-checkbox-group">' +
  242. '<button ion-button="alert-checkbox-button" *ngFor="let i of d.inputs" (click)="cbClick(i)" [attr.aria-checked]="i.checked" [attr.id]="i.id" [disabled]="i.disabled" class="alert-tappable alert-checkbox" role="checkbox">' +
  243. '<div class="alert-checkbox-icon"><div class="alert-checkbox-inner"></div></div>' +
  244. '<div class="alert-checkbox-label">' +
  245. '{{i.label}}' +
  246. '</div>' +
  247. '</button>' +
  248. '</div>' +
  249. '</ng-template>' +
  250. '<ng-template ngSwitchDefault>' +
  251. '<div class="alert-input-group">' +
  252. '<div *ngFor="let i of d.inputs" class="alert-input-wrapper">' +
  253. '<input [placeholder]="i.placeholder" [(ngModel)]="i.value" [type]="i.type" dir="auto" [min]="i.min" [max]="i.max" [attr.id]="i.id" class="alert-input">' +
  254. '</div>' +
  255. '</div>' +
  256. '</ng-template>' +
  257. '</div>' +
  258. '<div class="alert-button-group" [ngClass]="{\'alert-button-group-vertical\':d.buttons.length>2}">' +
  259. '<button ion-button="alert-button" *ngFor="let b of d.buttons" (click)="btnClick(b)" [ngClass]="b.cssClass">' +
  260. '{{b.text}}' +
  261. '</button>' +
  262. '</div>' +
  263. '</div>',
  264. host: {
  265. 'role': 'dialog',
  266. '[attr.aria-labelledby]': 'hdrId',
  267. '[attr.aria-describedby]': 'descId'
  268. },
  269. encapsulation: ViewEncapsulation.None,
  270. },] },
  271. ];
  272. /** @nocollapse */
  273. AlertCmp.ctorParameters = () => [
  274. { type: ViewController, },
  275. { type: ElementRef, },
  276. { type: Config, },
  277. { type: GestureController, },
  278. { type: NavParams, },
  279. { type: Renderer, },
  280. { type: Platform, },
  281. ];
  282. AlertCmp.propDecorators = {
  283. 'keyUp': [{ type: HostListener, args: ['body:keyup', ['$event'],] },],
  284. };
  285. let alertIds = -1;
  286. //# sourceMappingURL=alert-component.js.map