-
Notifications
You must be signed in to change notification settings - Fork 1
/
eventify.js
132 lines (114 loc) · 3.93 KB
/
eventify.js
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
// eventify.js 1.0.0
// Eventify may be freely distributed under the MIT license.
// For all details and documentation:
// http://geraldchecka.github.io/eventify
(function(Eventify) {
// Cross environment Support
// -------------------------
// Setup Eventify appropriately for the environment.
// Support for AMD
if (typeof define === "function" && define.amd) {
define(function() {
return Eventify;
});
}
// Setup for libraries that expose 'exports'
else if (typeof module !== "undefined" && module.exports) {
module.exports = Eventify;
}
// Support for browser global
else {
window.Eventify = Eventify;
}
}(function() {
// Base Setup
// ----------
// **Base class** constructor
function Eventify() {
this.events = {};
}
// Wrapper method holds all core method definitions. It is later attached to prototype
// using a publicly available method called `mixin`.
var methodApi = {
// Expects an event name, callback and a context[*optional*]
// Event names can be aliased using space as a separator.
on: function(event, callback, context) {
// ***event.split*** with a regex fared good in all browsers except Chrome.
//
// Regex and String manipulation micro-benchmarks did well in browsers, but chrome.
// So leaving it to be a character string as it is a highly used browser.
// http://jsperf.com/regex-string-tests-1
//
var events = event.split(" "),
eLen = events.length,
aVoid = void 0;
if ((typeof event !== "string" || typeof callback !== "function") && events[0].length > 0) throw new TypeError("Invalid input arguments.");
for (var eIndex = 0; eIndex < eLen; eIndex++) {
this.events[events[eIndex]] = {
context: context || aVoid,
callback: callback
};
}
return this;
},
// Remove events and listeners from the parent object
off: function(event) {
if (this.isEvent(event)) delete this.events[event];
return this;
},
// Looks up appropriate event and calls up its listener.
trigger: function(event) {
var handler = this.events[event],
context = handler.context || this,
args = Array.prototype.slice.call(arguments);
args.splice(0,1);
if (this.isEvent(event)) handler.callback.apply(context, args);
return this;
},
// `isEvent` Is the passed-in value an Event?
isEvent: function(event) {
if (typeof event !== "string" || event.length < 1) return false;
if (!this.events[event]) return false;
return true;
}
};
// Utility functions
// -----------------
// `extend` Extends a given object with all the properties in passed-in object
var extend = Eventify.extend = function() {
var aLen = arguments.length,
target = arguments[0];
if (aLen < 2 || target === void 0) return {};
for (var aIndex = 1; aIndex < aLen; aIndex++) {
var source = arguments[aIndex],
keys = getKeys(source),
kLen = keys.length;
for (var kIndex = 0; kIndex < kLen; kIndex++) {
var key = keys[kIndex];
target[key] = source[key];
}
}
return target;
};
// `getKeys` Generate keys for the passed-in object
var getKeys = Eventify.getKeys = function(obj) {
var keys = [];
if (!isObject(obj)) return keys;
for (var key in obj) { keys.push(key); }
return keys;
};
// `isObject` Is the passed-in value an object?
var isObject = Eventify.isObject = function(obj) {
return typeof obj === 'object' && !!obj;
};
// `mixin`
// **Mixin** lets you add your own custom properties and functions to the Eventify object
var mixin = Eventify.mixin = function(obj) {
extend(Eventify.prototype, obj);
};
// Onboard all wrapper methods to Eventify
mixin(methodApi);
// Current **version** of the library
Eventify.version = "1.0.0";
return Eventify;
}()));