a zip code crypto-currency system good for red ONLY

events.js 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. import { ScrollView } from './scroll-view';
  2. /**
  3. * @name Events
  4. * @description
  5. * Events is a publish-subscribe style event system for sending and responding to application-level
  6. * events across your app.
  7. *
  8. * @usage
  9. * ```ts
  10. * import { Events } from 'ionic-angular';
  11. *
  12. * // first page (publish an event when a user is created)
  13. * constructor(public events: Events) { }
  14. *
  15. * createUser(user) {
  16. * console.log('User created!')
  17. * this.events.publish('user:created', user, Date.now());
  18. * }
  19. *
  20. *
  21. * // second page (listen for the user created event after function is called)
  22. * constructor(public events: Events) {
  23. * events.subscribe('user:created', (user, time) => {
  24. * // user and time are the same arguments passed in `events.publish(user, time)`
  25. * console.log('Welcome', user, 'at', time);
  26. * });
  27. * }
  28. *
  29. * ```
  30. * @demo /docs/demos/src/events/
  31. */
  32. export class Events {
  33. constructor() {
  34. this._channels = [];
  35. }
  36. /**
  37. * Subscribe to an event topic. Events that get posted to that topic will trigger the provided handler.
  38. *
  39. * @param {string} topic the topic to subscribe to
  40. * @param {function} handler the event handler
  41. */
  42. subscribe(topic, ...handlers) {
  43. if (!this._channels[topic]) {
  44. this._channels[topic] = [];
  45. }
  46. handlers.forEach((handler) => {
  47. this._channels[topic].push(handler);
  48. });
  49. }
  50. /**
  51. * Unsubscribe from the given topic. Your handler will no longer receive events published to this topic.
  52. *
  53. * @param {string} topic the topic to unsubscribe from
  54. * @param {function} handler the event handler
  55. *
  56. * @return true if a handler was removed
  57. */
  58. unsubscribe(topic, handler = null) {
  59. let t = this._channels[topic];
  60. if (!t) {
  61. // Wasn't found, wasn't removed
  62. return false;
  63. }
  64. if (!handler) {
  65. // Remove all handlers for this topic
  66. delete this._channels[topic];
  67. return true;
  68. }
  69. // We need to find and remove a specific handler
  70. let i = t.indexOf(handler);
  71. if (i < 0) {
  72. // Wasn't found, wasn't removed
  73. return false;
  74. }
  75. t.splice(i, 1);
  76. // If the channel is empty now, remove it from the channel map
  77. if (!t.length) {
  78. delete this._channels[topic];
  79. }
  80. return true;
  81. }
  82. /**
  83. * Publish an event to the given topic.
  84. *
  85. * @param {string} topic the topic to publish to
  86. * @param {any} eventData the data to send as the event
  87. */
  88. publish(topic, ...args) {
  89. var t = this._channels[topic];
  90. if (!t) {
  91. return null;
  92. }
  93. let responses = [];
  94. t.forEach((handler) => {
  95. responses.push(handler(...args));
  96. });
  97. return responses;
  98. }
  99. }
  100. /**
  101. * @hidden
  102. */
  103. export function setupEvents(plt, dom) {
  104. const events = new Events();
  105. const win = plt.win();
  106. const doc = plt.doc();
  107. // start listening for resizes XXms after the app starts
  108. plt.timeout(() => {
  109. win.addEventListener('online', (ev) => {
  110. events.publish('app:online', ev);
  111. }, false);
  112. win.addEventListener('offline', (ev) => {
  113. events.publish('app:offline', ev);
  114. }, false);
  115. win.addEventListener('orientationchange', (ev) => {
  116. events.publish('app:rotated', ev);
  117. });
  118. // When that status taps, we respond
  119. win.addEventListener('statusTap', () => {
  120. // TODO: Make this more better
  121. let el = doc.elementFromPoint(plt.width() / 2, plt.height() / 2);
  122. if (!el) {
  123. return;
  124. }
  125. let contentEle = el.closest('.scroll-content');
  126. if (contentEle) {
  127. var style = contentEle.style;
  128. var scroll = new ScrollView(null, plt, dom);
  129. scroll._el = contentEle;
  130. // We need to stop scrolling if it's happening and scroll up
  131. style['WebkitBackfaceVisibility'] = 'hidden';
  132. style['WebkitTransform'] = 'translate3d(0,0,0)';
  133. dom.write(function () {
  134. style.overflow = 'hidden';
  135. function finish() {
  136. style.overflow = '';
  137. style['WebkitBackfaceVisibility'] = '';
  138. style['WebkitTransform'] = '';
  139. }
  140. let didScrollTimeout = plt.timeout(() => {
  141. finish();
  142. }, 400);
  143. scroll.scrollTo(0, 0, 300).then(() => {
  144. plt.cancelTimeout(didScrollTimeout);
  145. finish();
  146. });
  147. });
  148. }
  149. });
  150. }, 2000);
  151. return events;
  152. }
  153. /**
  154. * @hidden
  155. */
  156. export function setupProvideEvents(plt, dom) {
  157. return function () {
  158. return setupEvents(plt, dom);
  159. };
  160. }
  161. //# sourceMappingURL=events.js.map