a zip code crypto-currency system good for red ONLY

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. import { EventEmitter, Injectable, NgZone } from '@angular/core';
  2. import { Config } from '../config/config';
  3. import { DomController } from './dom-controller';
  4. import { isTextInput } from '../util/dom';
  5. import { KEY_TAB } from './key';
  6. import { Platform } from './platform';
  7. /**
  8. * @name Keyboard
  9. * @description
  10. * The `Keyboard` class allows you to work with the keyboard events provided
  11. * by the Ionic keyboard plugin.
  12. *
  13. * @usage
  14. * ```ts
  15. * export class MyClass {
  16. *
  17. * constructor(public keyboard: Keyboard) { }
  18. *
  19. * }
  20. * ```
  21. */
  22. export class Keyboard {
  23. constructor(config, _plt, _zone, _dom) {
  24. this._plt = _plt;
  25. this._zone = _zone;
  26. this._dom = _dom;
  27. this.willShow = new EventEmitter();
  28. this.willHide = new EventEmitter();
  29. this.didShow = new EventEmitter();
  30. this.didHide = new EventEmitter();
  31. this.eventsAvailable = false;
  32. this.focusOutline(config.get('focusOutline'));
  33. const win = _plt.win();
  34. if (win.Ionic && win.Ionic.keyboardPlugin) {
  35. this.listenV2(win);
  36. }
  37. else {
  38. this.listenV1(win);
  39. }
  40. }
  41. listenV2(win) {
  42. const platform = this._plt;
  43. platform.registerListener(win, 'keyboardWillShow', (ev) => {
  44. this._zone.run(() => {
  45. this.willShow.emit(ev.keyboardHeight);
  46. });
  47. }, { zone: false, passive: true });
  48. platform.registerListener(win, 'keyboardWillHide', () => {
  49. this._zone.run(() => {
  50. this.willHide.emit();
  51. });
  52. }, { zone: false, passive: true });
  53. platform.registerListener(win, 'keyboardDidShow', (ev) => {
  54. this._zone.run(() => {
  55. this.didShow.emit(ev.keyboardHeight);
  56. });
  57. }, { zone: false, passive: true });
  58. platform.registerListener(win, 'keyboardDidHide', () => {
  59. this._zone.run(() => {
  60. this.didHide.emit();
  61. });
  62. }, { zone: false, passive: true });
  63. this.eventsAvailable = true;
  64. }
  65. listenV1(win) {
  66. const platform = this._plt;
  67. platform.registerListener(win, 'native.keyboardhide', () => {
  68. this.blurActiveInput(true);
  69. }, { zone: false, passive: true });
  70. platform.registerListener(win, 'native.keyboardshow', () => {
  71. this.blurActiveInput(false);
  72. }, { zone: false, passive: true });
  73. }
  74. blurActiveInput(shouldBlur) {
  75. const platform = this._plt;
  76. platform.cancelTimeout(this._tmr);
  77. if (shouldBlur) {
  78. this._tmr = platform.timeout(() => {
  79. // this custom cordova plugin event fires when the keyboard will hide
  80. // useful when the virtual keyboard is closed natively
  81. // https://github.com/ionic-team/ionic-plugin-keyboard
  82. if (this.isOpen()) {
  83. platform.focusOutActiveElement();
  84. }
  85. }, 80);
  86. }
  87. }
  88. /**
  89. * Check to see if the keyboard is open or not.
  90. *
  91. * ```ts
  92. * export class MyClass {
  93. * constructor(public keyboard: Keyboard) {
  94. *
  95. * }
  96. *
  97. * keyboardCheck() {
  98. * console.log('The keyboard is open:', this.keyboard.isOpen());
  99. * }
  100. * }
  101. * ```
  102. *
  103. * @return {boolean} returns a true or false value if the keyboard is open or not.
  104. */
  105. isOpen() {
  106. return this.hasFocusedTextInput();
  107. }
  108. /**
  109. * When the keyboard is closed, call any methods you want.
  110. *
  111. * ```ts
  112. * export class MyClass {
  113. * constructor(public keyboard: Keyboard) {
  114. * this.keyboard.onClose(this.closeCallback);
  115. * }
  116. * closeCallback() {
  117. * // call what ever functionality you want on keyboard close
  118. * console.log('Closing time');
  119. * }
  120. * }
  121. * ```
  122. *
  123. * @param {function} callback method you want to call when the keyboard has been closed.
  124. * @return {function} returns a callback that gets fired when the keyboard is closed.
  125. */
  126. onClose(callback, pollingInternval = KEYBOARD_CLOSE_POLLING, pollingChecksMax = KEYBOARD_POLLING_CHECKS_MAX) {
  127. (void 0) /* console.debug */;
  128. const self = this;
  129. let checks = 0;
  130. let promise = null;
  131. if (!callback) {
  132. // a callback wasn't provided, so let's return a promise instead
  133. promise = new Promise(resolve => { callback = resolve; });
  134. }
  135. function checkKeyboard() {
  136. (void 0) /* console.debug */;
  137. if (!self.isOpen() || checks > pollingChecksMax) {
  138. self._plt.timeout(function () {
  139. self._zone.run(function () {
  140. (void 0) /* console.debug */;
  141. callback();
  142. });
  143. }, 400);
  144. }
  145. else {
  146. self._plt.timeout(checkKeyboard, pollingInternval);
  147. }
  148. checks++;
  149. }
  150. self._plt.timeout(checkKeyboard, pollingInternval);
  151. return promise;
  152. }
  153. /**
  154. * Programmatically close the keyboard.
  155. */
  156. close() {
  157. this._dom.read(() => {
  158. if (this.isOpen()) {
  159. // only focus out when a text input has focus
  160. (void 0) /* console.debug */;
  161. this._dom.write(() => {
  162. this._plt.focusOutActiveElement();
  163. });
  164. }
  165. });
  166. }
  167. /**
  168. * @hidden
  169. */
  170. focusOutline(setting) {
  171. /* Focus Outline
  172. * --------------------------------------------------
  173. * By default, when a keydown event happens from a tab key, then
  174. * the 'focus-outline' css class is added to the body element
  175. * so focusable elements have an outline. On a mousedown or
  176. * touchstart event, then the 'focus-outline' css class is removed.
  177. *
  178. * Config default overrides:
  179. * focusOutline: true - Always add the focus-outline
  180. * focusOutline: false - Do not add the focus-outline
  181. */
  182. const self = this;
  183. const platform = self._plt;
  184. const doc = platform.doc();
  185. let isKeyInputEnabled = false;
  186. let unRegMouse;
  187. let unRegTouch;
  188. const evOpts = { passive: true, zone: false };
  189. function cssClass() {
  190. self._dom.write(() => {
  191. platform.doc().body.classList[isKeyInputEnabled ? 'add' : 'remove']('focus-outline');
  192. });
  193. }
  194. if (setting === true) {
  195. isKeyInputEnabled = true;
  196. return cssClass();
  197. }
  198. else if (setting === false) {
  199. return;
  200. }
  201. // default is to add the focus-outline when the tab key is used
  202. function keyDown(ev) {
  203. if (!isKeyInputEnabled && ev.keyCode === KEY_TAB) {
  204. isKeyInputEnabled = true;
  205. enableKeyInput();
  206. }
  207. }
  208. function pointerDown() {
  209. isKeyInputEnabled = false;
  210. enableKeyInput();
  211. }
  212. function enableKeyInput() {
  213. cssClass();
  214. unRegMouse && unRegMouse();
  215. unRegTouch && unRegTouch();
  216. if (isKeyInputEnabled) {
  217. // listen for when a mousedown or touchstart event happens
  218. unRegMouse = platform.registerListener(doc, 'mousedown', pointerDown, evOpts);
  219. unRegTouch = platform.registerListener(doc, 'touchstart', pointerDown, evOpts);
  220. }
  221. }
  222. // always listen for tab keydown events
  223. platform.registerListener(platform.doc(), 'keydown', keyDown, evOpts);
  224. }
  225. hasFocusedTextInput() {
  226. const activeEle = this._plt.getActiveElement();
  227. if (isTextInput(activeEle)) {
  228. return (activeEle.parentElement.querySelector(':focus') === activeEle);
  229. }
  230. return false;
  231. }
  232. /**
  233. * Set to true to hide the additional toolbar that is on top of the keyboard.
  234. * This toolbar features the Prev, Next, and Done buttons.
  235. * @param hidden
  236. */
  237. hideFormAccessoryBar(hidden) {
  238. const win = this._plt.win();
  239. if (win && win.Keyboard && win.Keyboard.hideFormAccessoryBar) {
  240. win.Keyboard.hideFormAccessoryBar(hidden);
  241. }
  242. }
  243. }
  244. Keyboard.decorators = [
  245. { type: Injectable },
  246. ];
  247. /** @nocollapse */
  248. Keyboard.ctorParameters = () => [
  249. { type: Config, },
  250. { type: Platform, },
  251. { type: NgZone, },
  252. { type: DomController, },
  253. ];
  254. const KEYBOARD_CLOSE_POLLING = 150;
  255. const KEYBOARD_POLLING_CHECKS_MAX = 100;
  256. //# sourceMappingURL=keyboard.js.map