a zip code crypto-currency system good for red ONLY

events.js 6.7KB

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