Graph database Analysis of the Steam Network
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

204 lines
5.4 KiB

  1. ;(function() {
  2. 'use strict';
  3. /**
  4. * Dispatcher constructor.
  5. *
  6. * @return {dispatcher} The new dispatcher instance.
  7. */
  8. var dispatcher = function() {
  9. Object.defineProperty(this, '_handlers', {
  10. value: {}
  11. });
  12. };
  13. /**
  14. * Will execute the handler everytime that the indicated event (or the
  15. * indicated events) will be triggered.
  16. *
  17. * @param {string} events The name of the event (or the events
  18. * separated by spaces).
  19. * @param {function(Object)} handler The handler to bind.
  20. * @return {dispatcher} Returns the instance itself.
  21. */
  22. dispatcher.prototype.bind = function(events, handler) {
  23. var i,
  24. l,
  25. event,
  26. eArray;
  27. if (
  28. arguments.length === 1 &&
  29. typeof arguments[0] === 'object'
  30. )
  31. for (events in arguments[0])
  32. this.bind(events, arguments[0][events]);
  33. else if (
  34. arguments.length === 2 &&
  35. typeof arguments[1] === 'function'
  36. ) {
  37. eArray = typeof events === 'string' ? events.split(' ') : events;
  38. for (i = 0, l = eArray.length; i !== l; i += 1) {
  39. event = eArray[i];
  40. // Check that event is not '':
  41. if (!event)
  42. continue;
  43. if (!this._handlers[event])
  44. this._handlers[event] = [];
  45. // Using an object instead of directly the handler will make possible
  46. // later to add flags
  47. this._handlers[event].push({
  48. handler: handler
  49. });
  50. }
  51. } else
  52. throw 'bind: Wrong arguments.';
  53. return this;
  54. };
  55. /**
  56. * Removes the handler from a specified event (or specified events).
  57. *
  58. * @param {?string} events The name of the event (or the events
  59. * separated by spaces). If undefined,
  60. * then all handlers are removed.
  61. * @param {?function(object)} handler The handler to unbind. If undefined,
  62. * each handler bound to the event or the
  63. * events will be removed.
  64. * @return {dispatcher} Returns the instance itself.
  65. */
  66. dispatcher.prototype.unbind = function(events, handler) {
  67. var i,
  68. n,
  69. j,
  70. m,
  71. k,
  72. a,
  73. event,
  74. eArray = typeof events === 'string' ? events.split(' ') : events;
  75. if (!arguments.length) {
  76. for (k in this._handlers)
  77. delete this._handlers[k];
  78. return this;
  79. }
  80. if (handler) {
  81. for (i = 0, n = eArray.length; i !== n; i += 1) {
  82. event = eArray[i];
  83. if (this._handlers[event]) {
  84. a = [];
  85. for (j = 0, m = this._handlers[event].length; j !== m; j += 1)
  86. if (this._handlers[event][j].handler !== handler)
  87. a.push(this._handlers[event][j]);
  88. this._handlers[event] = a;
  89. }
  90. if (this._handlers[event] && this._handlers[event].length === 0)
  91. delete this._handlers[event];
  92. }
  93. } else
  94. for (i = 0, n = eArray.length; i !== n; i += 1)
  95. delete this._handlers[eArray[i]];
  96. return this;
  97. };
  98. /**
  99. * Executes each handler bound to the event
  100. *
  101. * @param {string} events The name of the event (or the events separated
  102. * by spaces).
  103. * @param {?object} data The content of the event (optional).
  104. * @return {dispatcher} Returns the instance itself.
  105. */
  106. dispatcher.prototype.dispatchEvent = function(events, data) {
  107. var i,
  108. n,
  109. j,
  110. m,
  111. a,
  112. event,
  113. eventName,
  114. self = this,
  115. eArray = typeof events === 'string' ? events.split(' ') : events;
  116. data = data === undefined ? {} : data;
  117. for (i = 0, n = eArray.length; i !== n; i += 1) {
  118. eventName = eArray[i];
  119. if (this._handlers[eventName]) {
  120. event = self.getEvent(eventName, data);
  121. a = [];
  122. for (j = 0, m = this._handlers[eventName].length; j !== m; j += 1) {
  123. this._handlers[eventName][j].handler(event);
  124. if (!this._handlers[eventName][j].one)
  125. a.push(this._handlers[eventName][j]);
  126. }
  127. this._handlers[eventName] = a;
  128. }
  129. }
  130. return this;
  131. };
  132. /**
  133. * Return an event object.
  134. *
  135. * @param {string} events The name of the event.
  136. * @param {?object} data The content of the event (optional).
  137. * @return {object} Returns the instance itself.
  138. */
  139. dispatcher.prototype.getEvent = function(event, data) {
  140. return {
  141. type: event,
  142. data: data || {},
  143. target: this
  144. };
  145. };
  146. /**
  147. * A useful function to deal with inheritance. It will make the target
  148. * inherit the prototype of the class dispatcher as well as its constructor.
  149. *
  150. * @param {object} target The target.
  151. */
  152. dispatcher.extend = function(target, args) {
  153. var k;
  154. for (k in dispatcher.prototype)
  155. if (dispatcher.prototype.hasOwnProperty(k))
  156. target[k] = dispatcher.prototype[k];
  157. dispatcher.apply(target, args);
  158. };
  159. /**
  160. * EXPORT:
  161. * *******
  162. */
  163. if (typeof this.sigma !== 'undefined') {
  164. this.sigma.classes = this.sigma.classes || {};
  165. this.sigma.classes.dispatcher = dispatcher;
  166. } else if (typeof exports !== 'undefined') {
  167. if (typeof module !== 'undefined' && module.exports)
  168. exports = module.exports = dispatcher;
  169. exports.dispatcher = dispatcher;
  170. } else
  171. this.dispatcher = dispatcher;
  172. }).call(this);