File size: 3,334 Bytes
b3787d8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
'use strict';

/*
 global window,
 document,
 app
 */
 
(function() {
  // check if the app itself is defined at all
  if (! app) {
    return console.error('[modules/events] app.js has not been loaded yet');
  }
  
  // create the module object
  var eventModule = {
    events: {}
  };
  
  
  /**
   * on function.
   * adds a callback to an event stack
   *
   * @param {string} type        the event to subscribe to
   * @param {function} callback  the event callback to subscribe
   *
   * @return void
   */
  eventModule.on = function(type, callback) {
    // if there is no callback registered for this event yet, create an array for this event
    if (! this.events.hasOwnProperty(type)) {
      this.events[ type ] = [];
    }

    // push the event callback to the handler stack
    this.events[ type ].push(callback);
  };
  
  
  /**
   * off function.
   * removes a callback from an event stack
   *
   * @param {string} type        the event to unsubscribe from
   * @param {function} callback  the event callback to unsubscribe
   *
   * @return {boolean}           whether the callback was removed or not
   */
  eventModule.off = function(type, callback) {
    // if the event does not exist, return false
    if (! type in this.events) {
      return false;
    }
    
    // if no callback to unsubscribe was given, remove all callbacks
    if (typeof callback === 'undefined') {
      return delete this.events[ type ];
    }

    // iterate over handlers, remove the callback in question
    this.events[ type ] = this.events[ type ].filter(
      function(item) {
        if (item !== callback) {
          return item;
        }
      }
    );

    return true;
  };
  
  
  /**
   * emit function.
   * calls all registered callbacks for an event
   * 
   * @param {string} type       the event to trigger callbacks for
   * @param {*}      [data]     optional data to hand over to the callbacks
   * @param {*}      [context]  optional context to hand over to the callbacks,
   *                            defaults to window
   * 
   * @return {boolean}          whether the event has been fired
   */
  eventModule.emit = function(type, data, context) {
    // use an empty object if no data given
    data = data || {};
    
    // use window as the context if no context given
    context = context || window;

    // the event does not exist.
    if (! this.events.hasOwnProperty(type)) {
      return false;
    }

    // iterate over all callbacks, run them as expected
    for (var i = 0; i < this.events[ type ].length; i++) {
      this.events[ type ][ i ].call(context, type, data);
    }

    return true;
  };


  /**
   * getEvents function.
   * returns all registered events.
   *
   * @return {Array}  the list of events
   */
  eventModule.getEvents = function () {
    var events = [];

    for (var event in this.events) {
      if (! this.events.hasOwnProperty(event)) continue;

      events.push(event);
    }

    return events;
  };
  
  // register the module in the app
  app.registerModule('events', eventModule, function() {

    // register event endpoints
    app.mountModuleEndpoint('on', 'events', 'on');
    app.mountModuleEndpoint('off', 'events', 'off');
    app.mountModuleEndpoint('emit', 'events', 'emit');
  });
  
  // return the module
  return eventModule;
})();