a zip code crypto-currency system good for red ONLY

view-controller.js 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. import { EventEmitter, Output } from '@angular/core';
  2. import { isPresent } from '../util/util';
  3. import { STATE_DESTROYED, STATE_NEW } from './nav-util';
  4. import { NavParams } from './nav-params';
  5. /**
  6. * @name ViewController
  7. * @description
  8. * Access various features and information about the current view.
  9. * @usage
  10. * ```ts
  11. * import { Component } from '@angular/core';
  12. * import { ViewController } from 'ionic-angular';
  13. *
  14. * @Component({...})
  15. * export class MyPage{
  16. *
  17. * constructor(public viewCtrl: ViewController) {}
  18. *
  19. * }
  20. * ```
  21. */
  22. export class ViewController {
  23. constructor(component, data, rootCssClass = DEFAULT_CSS_CLASS) {
  24. this.component = component;
  25. this._isHidden = false;
  26. this._state = STATE_NEW;
  27. /**
  28. * Observable to be subscribed to when the current component will become active
  29. * @returns {Observable} Returns an observable
  30. */
  31. this.willEnter = new EventEmitter();
  32. /**
  33. * Observable to be subscribed to when the current component has become active
  34. * @returns {Observable} Returns an observable
  35. */
  36. this.didEnter = new EventEmitter();
  37. /**
  38. * Observable to be subscribed to when the current component will no longer be active
  39. * @returns {Observable} Returns an observable
  40. */
  41. this.willLeave = new EventEmitter();
  42. /**
  43. * Observable to be subscribed to when the current component is no long active
  44. * @returns {Observable} Returns an observable
  45. */
  46. this.didLeave = new EventEmitter();
  47. /**
  48. * Observable to be subscribed to when the current component has been destroyed
  49. * @returns {Observable} Returns an observable
  50. */
  51. this.willUnload = new EventEmitter();
  52. /**
  53. * @hidden
  54. */
  55. this.readReady = new EventEmitter();
  56. /**
  57. * @hidden
  58. */
  59. this.writeReady = new EventEmitter();
  60. /** @hidden */
  61. this.isOverlay = false;
  62. /** @hidden */
  63. this._emitter = new EventEmitter();
  64. // passed in data could be NavParams, but all we care about is its data object
  65. this.data = (data instanceof NavParams ? data.data : (isPresent(data) ? data : {}));
  66. this._cssClass = rootCssClass;
  67. this._ts = Date.now();
  68. window.addEventListener('orientationchange', this.handleOrientationChange.bind(this));
  69. }
  70. handleOrientationChange() {
  71. if (this.getContent()) {
  72. this.getContent().resize();
  73. }
  74. }
  75. /**
  76. * @hidden
  77. */
  78. init(componentRef) {
  79. (void 0) /* assert */;
  80. this._ts = Date.now();
  81. this._cmp = componentRef;
  82. this.instance = this.instance || componentRef.instance;
  83. this._detached = false;
  84. }
  85. _setNav(navCtrl) {
  86. this._nav = navCtrl;
  87. }
  88. _setInstance(instance) {
  89. this.instance = instance;
  90. }
  91. /**
  92. * @hidden
  93. */
  94. subscribe(generatorOrNext) {
  95. return this._emitter.subscribe(generatorOrNext);
  96. }
  97. /**
  98. * @hidden
  99. */
  100. emit(data) {
  101. this._emitter.emit(data);
  102. }
  103. /**
  104. * Called when the current viewController has be successfully dismissed
  105. */
  106. onDidDismiss(callback) {
  107. this._onDidDismiss = callback;
  108. }
  109. /**
  110. * Called when the current viewController will be dismissed
  111. */
  112. onWillDismiss(callback) {
  113. this._onWillDismiss = callback;
  114. }
  115. /**
  116. * Dismiss the current viewController
  117. * @param {any} [data] Data that you want to return when the viewController is dismissed.
  118. * @param {any} [role ]
  119. * @param {NavOptions} navOptions Options for the dismiss navigation.
  120. * @returns {any} data Returns the data passed in, if any.
  121. */
  122. dismiss(data, role, navOptions = {}) {
  123. if (!this._nav) {
  124. (void 0) /* assert */;
  125. return Promise.resolve(false);
  126. }
  127. if (this.isOverlay && !navOptions.minClickBlockDuration) {
  128. // This is a Modal being dismissed so we need
  129. // to add the minClickBlockDuration option
  130. // for UIWebView
  131. navOptions.minClickBlockDuration = 400;
  132. }
  133. this._dismissData = data;
  134. this._dismissRole = role;
  135. const options = Object.assign({}, this._leavingOpts, navOptions);
  136. return this._nav.removeView(this, options).then(() => data);
  137. }
  138. /**
  139. * @hidden
  140. */
  141. getNav() {
  142. return this._nav;
  143. }
  144. /**
  145. * @hidden
  146. */
  147. getTransitionName(_direction) {
  148. return this._nav && this._nav.config.get('pageTransition');
  149. }
  150. /**
  151. * @hidden
  152. */
  153. getNavParams() {
  154. return new NavParams(this.data);
  155. }
  156. /**
  157. * @hidden
  158. */
  159. setLeavingOpts(opts) {
  160. this._leavingOpts = opts;
  161. }
  162. /**
  163. * Check to see if you can go back in the navigation stack.
  164. * @returns {boolean} Returns if it's possible to go back from this Page.
  165. */
  166. enableBack() {
  167. // update if it's possible to go back from this nav item
  168. if (!this._nav) {
  169. return false;
  170. }
  171. // the previous view may exist, but if it's about to be destroyed
  172. // it shouldn't be able to go back to
  173. const previousItem = this._nav.getPrevious(this);
  174. return !!(previousItem);
  175. }
  176. /**
  177. * @hidden
  178. */
  179. get name() {
  180. return (this.component ? this.component.name : '');
  181. }
  182. /**
  183. * Get the index of the current component in the current navigation stack.
  184. * @returns {number} Returns the index of this page within its `NavController`.
  185. */
  186. get index() {
  187. return (this._nav ? this._nav.indexOf(this) : -1);
  188. }
  189. /**
  190. * @returns {boolean} Returns if this Page is the first in the stack of pages within its NavController.
  191. */
  192. isFirst() {
  193. return (this._nav ? this._nav.first() === this : false);
  194. }
  195. /**
  196. * @returns {boolean} Returns if this Page is the last in the stack of pages within its NavController.
  197. */
  198. isLast() {
  199. return (this._nav ? this._nav.last() === this : false);
  200. }
  201. /**
  202. * @hidden
  203. * DOM WRITE
  204. */
  205. _domShow(shouldShow, renderer) {
  206. // using hidden element attribute to display:none and not render views
  207. // _hidden value of '' means the hidden attribute will be added
  208. // _hidden value of null means the hidden attribute will be removed
  209. // doing checks to make sure we only update the DOM when actually needed
  210. // if it should render, then the hidden attribute should not be on the element
  211. if (this._cmp && shouldShow === this._isHidden) {
  212. this._isHidden = !shouldShow;
  213. let value = (shouldShow ? null : '');
  214. // ******** DOM WRITE ****************
  215. renderer.setElementAttribute(this.pageRef().nativeElement, 'hidden', value);
  216. }
  217. }
  218. /**
  219. * @hidden
  220. */
  221. getZIndex() {
  222. return this._zIndex;
  223. }
  224. /**
  225. * @hidden
  226. * DOM WRITE
  227. */
  228. _setZIndex(zIndex, renderer) {
  229. if (zIndex !== this._zIndex) {
  230. this._zIndex = zIndex;
  231. const pageRef = this.pageRef();
  232. if (pageRef) {
  233. // ******** DOM WRITE ****************
  234. renderer.setElementStyle(pageRef.nativeElement, 'z-index', zIndex);
  235. }
  236. }
  237. }
  238. /**
  239. * @returns {ElementRef} Returns the Page's ElementRef.
  240. */
  241. pageRef() {
  242. return this._cmp && this._cmp.location;
  243. }
  244. _setContent(directive) {
  245. this._cntDir = directive;
  246. }
  247. /**
  248. * @returns {component} Returns the Page's Content component reference.
  249. */
  250. getContent() {
  251. return this._cntDir;
  252. }
  253. _setContentRef(elementRef) {
  254. this._cntRef = elementRef;
  255. }
  256. /**
  257. * @returns {ElementRef} Returns the Content's ElementRef.
  258. */
  259. contentRef() {
  260. return this._cntRef;
  261. }
  262. _setIONContent(content) {
  263. this._setContent(content);
  264. this._ionCntDir = content;
  265. }
  266. /**
  267. * @hidden
  268. */
  269. getIONContent() {
  270. return this._ionCntDir;
  271. }
  272. _setIONContentRef(elementRef) {
  273. this._setContentRef(elementRef);
  274. this._ionCntRef = elementRef;
  275. }
  276. /**
  277. * @hidden
  278. */
  279. getIONContentRef() {
  280. return this._ionCntRef;
  281. }
  282. _setHeader(directive) {
  283. this._hdrDir = directive;
  284. }
  285. /**
  286. * @hidden
  287. */
  288. getHeader() {
  289. return this._hdrDir;
  290. }
  291. _setFooter(directive) {
  292. this._ftrDir = directive;
  293. }
  294. /**
  295. * @hidden
  296. */
  297. getFooter() {
  298. return this._ftrDir;
  299. }
  300. _setNavbar(directive) {
  301. this._nb = directive;
  302. }
  303. /**
  304. * @hidden
  305. */
  306. getNavbar() {
  307. return this._nb;
  308. }
  309. /**
  310. * Find out if the current component has a NavBar or not. Be sure
  311. * to wrap this in an `ionViewWillEnter` method in order to make sure
  312. * the view has rendered fully.
  313. * @returns {boolean} Returns a boolean if this Page has a navbar or not.
  314. */
  315. hasNavbar() {
  316. return !!this._nb;
  317. }
  318. /**
  319. * Change the title of the back-button. Be sure to call this
  320. * after `ionViewWillEnter` to make sure the DOM has been rendered.
  321. * @param {string} val Set the back button text.
  322. */
  323. setBackButtonText(val) {
  324. this._nb && this._nb.setBackButtonText(val);
  325. }
  326. /**
  327. * Set if the back button for the current view is visible or not. Be sure to call this
  328. * after `ionViewWillEnter` to make sure the DOM has been rendered.
  329. * @param {boolean} Set if this Page's back button should show or not.
  330. */
  331. showBackButton(shouldShow) {
  332. if (this._nb) {
  333. this._nb.hideBackButton = !shouldShow;
  334. }
  335. }
  336. _preLoad() {
  337. (void 0) /* assert */;
  338. this._lifecycle('PreLoad');
  339. }
  340. /**
  341. * @hidden
  342. * The view has loaded. This event only happens once per view will be created.
  343. * This event is fired before the component and his children have been initialized.
  344. */
  345. _willLoad() {
  346. (void 0) /* assert */;
  347. this._lifecycle('WillLoad');
  348. }
  349. /**
  350. * @hidden
  351. * The view has loaded. This event only happens once per view being
  352. * created. If a view leaves but is cached, then this will not
  353. * fire again on a subsequent viewing. This method is a good place
  354. * to put your setup code for the view; however, it is not the
  355. * recommended method to use when a view becomes active.
  356. */
  357. _didLoad() {
  358. (void 0) /* assert */;
  359. this._lifecycle('DidLoad');
  360. }
  361. /**
  362. * @hidden
  363. * The view is about to enter and become the active view.
  364. */
  365. _willEnter() {
  366. this.handleOrientationChange();
  367. (void 0) /* assert */;
  368. if (this._detached && this._cmp) {
  369. // ensure this has been re-attached to the change detector
  370. this._cmp.changeDetectorRef.reattach();
  371. this._detached = false;
  372. }
  373. this.willEnter.emit(null);
  374. this._lifecycle('WillEnter');
  375. }
  376. /**
  377. * @hidden
  378. * The view has fully entered and is now the active view. This
  379. * will fire, whether it was the first load or loaded from the cache.
  380. */
  381. _didEnter() {
  382. (void 0) /* assert */;
  383. this._nb && this._nb.didEnter();
  384. this.didEnter.emit(null);
  385. this._lifecycle('DidEnter');
  386. }
  387. /**
  388. * @hidden
  389. * The view is about to leave and no longer be the active view.
  390. */
  391. _willLeave(willUnload) {
  392. this.willLeave.emit(null);
  393. this._lifecycle('WillLeave');
  394. if (willUnload && this._onWillDismiss) {
  395. this._onWillDismiss(this._dismissData, this._dismissRole);
  396. this._onWillDismiss = null;
  397. }
  398. }
  399. /**
  400. * @hidden
  401. * The view has finished leaving and is no longer the active view. This
  402. * will fire, whether it is cached or unloaded.
  403. */
  404. _didLeave() {
  405. this.didLeave.emit(null);
  406. this._lifecycle('DidLeave');
  407. // when this is not the active page
  408. // we no longer need to detect changes
  409. if (!this._detached && this._cmp) {
  410. this._cmp.changeDetectorRef.detach();
  411. this._detached = true;
  412. }
  413. }
  414. /**
  415. * @hidden
  416. */
  417. _willUnload() {
  418. this.willUnload.emit(null);
  419. this._lifecycle('WillUnload');
  420. this._onDidDismiss && this._onDidDismiss(this._dismissData, this._dismissRole);
  421. this._onDidDismiss = null;
  422. this._dismissData = null;
  423. this._dismissRole = null;
  424. }
  425. /**
  426. * @hidden
  427. * DOM WRITE
  428. */
  429. _destroy(renderer) {
  430. (void 0) /* assert */;
  431. if (this._cmp) {
  432. if (renderer) {
  433. // ensure the element is cleaned up for when the view pool reuses this element
  434. // ******** DOM WRITE ****************
  435. var cmpEle = this._cmp.location.nativeElement;
  436. renderer.setElementAttribute(cmpEle, 'class', null);
  437. renderer.setElementAttribute(cmpEle, 'style', null);
  438. }
  439. window.removeEventListener('orientationchange', this.handleOrientationChange.bind(this));
  440. // completely destroy this component. boom.
  441. this._cmp.destroy();
  442. }
  443. this._nav = this._cmp = this.instance = this._cntDir = this._cntRef = this._leavingOpts = this._hdrDir = this._ftrDir = this._nb = this._onDidDismiss = this._onWillDismiss = null;
  444. this._state = STATE_DESTROYED;
  445. }
  446. /**
  447. * @hidden
  448. */
  449. _lifecycleTest(lifecycle) {
  450. const instance = this.instance;
  451. const methodName = 'ionViewCan' + lifecycle;
  452. if (instance && instance[methodName]) {
  453. try {
  454. var result = instance[methodName]();
  455. if (result instanceof Promise) {
  456. return result;
  457. }
  458. else {
  459. // Any value but explitic false, should be true
  460. return Promise.resolve(result !== false);
  461. }
  462. }
  463. catch (e) {
  464. return Promise.reject(`${this.name} ${methodName} error: ${e.message}`);
  465. }
  466. }
  467. return Promise.resolve(true);
  468. }
  469. /**
  470. * @hidden
  471. */
  472. _lifecycle(lifecycle) {
  473. const instance = this.instance;
  474. const methodName = 'ionView' + lifecycle;
  475. if (instance && instance[methodName]) {
  476. instance[methodName]();
  477. }
  478. }
  479. }
  480. ViewController.propDecorators = {
  481. '_emitter': [{ type: Output },],
  482. };
  483. export function isViewController(viewCtrl) {
  484. return !!(viewCtrl && viewCtrl._didLoad && viewCtrl._willUnload);
  485. }
  486. const DEFAULT_CSS_CLASS = 'ion-page';
  487. //# sourceMappingURL=view-controller.js.map