-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.js
More file actions
300 lines (272 loc) · 7.08 KB
/
index.js
File metadata and controls
300 lines (272 loc) · 7.08 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/*!
* dual-emitter <https://github.com/tunnckoCore/dual-emitter>
*
* Copyright (c) 2015 Charlike Mike Reagent <@tunnckoCore> (http://www.tunnckocore.tk)
* Released under the MIT license.
*/
/* jshint asi:true */
'use strict'
var util = require('util')
/**
* Expose `DualEmitter`
*/
module.exports = DualEmitter
/**
* > Create a new instance of `DualEmitter`.
*
* **Example**
*
* ```js
* var DualEmitter = require('dual-emitter')
* var emitter = new DualEmitter()
* ```
*
* @param {Object} `[events]` Initialize with default events.
* @api public
*/
function DualEmitter (events) {
if (!(this instanceof DualEmitter)) {
return new DualEmitter(events)
}
this._events = events && typeof events === 'object' ? events : {}
}
/**
* > Add/bind event listener to custom or DOM event.
* Notice that `this` in event handler function vary - it can be the DOM element
* or DualEmitter instance.
*
* **Example**
*
* ```js
* function handler (a, b) {
* console.log('hi', a, b) //=> hi 123 bar
* }
*
* function onclick (evt) {
* console.log(evt, 'clicked')
* }
*
* var element = document.body.querySelector('a.link')
*
* emitter.on('custom', handler).emit('custom', 123, 'bar')
* emitter.on('click', onclick, element).off('click', onclick, element)
* ```
*
* @param {String} `<name>` event name
* @param {Function} `<fn>` event handler
* @param {Object} `[el]` optional DOM element
* @return {DualEmitter} DualEmitter for chaining
* @api public
*/
DualEmitter.prototype.on = function on (name, fn, el) {
if (typeof name !== 'string') {
throw new TypeError('DualEmitter#on expect `name` be string')
}
if (typeof fn !== 'function') {
throw new TypeError('DualEmitter#on expect `fn` be function')
}
this._events[name] = this._hasOwn(this._events, name) ? this._events[name] : []
this._events[name].push(fn)
if (el && this._isDom(el)) {
fn.outerHTML = el.outerHTML
this._element = el
el.addEventListener ? el.addEventListener(name, fn, false) : el.attachEvent('on' + name, fn)
}
return this
}
/**
* > Remove/unbind event listener of custom or DOM event.
*
* **Example**
*
* ```js
* var element = document.body.querySelector('a.link')
* emitter.off('custom', handler)
* emitter.off('click', onclick, element)
* ```
*
* @param {String} `<name>` event name
* @param {Function} `<fn>` event handler
* @param {Object} `[el]` optional DOM element
* @return {DualEmitter} DualEmitter for chaining
* @api public
*/
DualEmitter.prototype.off = function off (name, fn, el) {
if (typeof name !== 'string') {
throw new TypeError('DualEmitter#off expect `name` be string')
}
if (typeof fn !== 'function') {
throw new TypeError('DualEmitter#off expect `fn` be function')
}
if (!this._hasOwn(this._events, name)) {return this}
this._events[name].splice(this._events[name].indexOf(fn), 1)
if (el && this._isDom(el)) {
el.removeEventListener ? el.removeEventListener(name, fn, false) : el.detachEvent('on' + name, fn)
}
return this
}
/**
* > Add one-time event listener to custom or DOM event.
* Notice that `this` in event handler function vary - it can be the DOM element
* or DualEmitter instance.
*
* **Example**
*
* ```js
* emitter
* .once('custom', function () {
* console.log('executed one time')
* })
* .emit('custom')
* .emit('custom')
*
* var element = document.body.querySelector('a.link')
* emitter.once('click', function () {
* console.log('listen for click event only once')
* }, element)
* ```
*
* @param {String} `<name>` event name
* @param {Function} `<fn>` event handler
* @param {Object} `[el]` optional DOM element
* @return {DualEmitter} DualEmitter for chaining
* @api public
*/
DualEmitter.prototype.once = function once (name, fn, el) {
var self = this
function handler () {
self.off(name, handler, el)
return fn.apply(el, arguments)
}
return this.on(name, handler, el)
}
/**
* > Emit/execute some type of event listener.
* You also can emit DOM events if last argument
* is the DOM element that have attached event listener.
*
* **Example**
*
* ```js
* var i = 0
*
* emitter
* .on('custom', function () {
* console.log('i ==', i++, arguments)
* })
* .emit('custom')
* .emit('custom', 123)
* .emit('custom', 'foo', 'bar', 'baz')
* .emit('custom', [1, 2, 3], 4, 5)
*
* // or even emit DOM events, but you should
* // give the element as last argument to `.emit` method
* var element = document.body.querySelector('a.link')
* var clicks = 0
*
* emitter
* .on('click', function (a) {
* console.log(a, 'clicked', clicks++)
* console.log(this.textContent) // content of <a> tag
* }, element)
* .emit('click', 123, element)
* .emit('click', element)
* .emit('click', foo, element)
* ```
*
* @param {String} `<name>` event name
* @param {Mixed} `[args...]` context to pass to event listeners
* @param {Object} `[el]` optional DOM element
* @return {DualEmitter} DualEmitter for chaining
* @api public
*/
DualEmitter.prototype.emit = function emit (name) {
if (!this._hasOwn(this._events, name)) {return this}
var args = Array.prototype.slice.call(arguments, 1)
var el = args[args.length - 1]
var isdom = this._isDom(el)
el = isdom ? el : this
args = isdom ? args.slice(0, -1) : args
for (var i = 0; i < this._events[name].length; i++) {
var fn = this._events[name][i]
if (isdom && fn.outerHTML !== el.outerHTML) {
continue
}
fn.apply(el, args)
}
return this
}
/**
* > Check that given `val` is DOM element. Used internally.
*
* **Example**
*
* ```js
* var element = document.body.querySelector('a.link')
*
* emitter._isDom(element) //=> true
* emitter._isDom({a: 'b'}) //=> false
* ```
*
* @param {Mixed} `val`
* @return {Boolean}
* @api public
*/
DualEmitter.prototype._isDom = function isDom (val) {
val = Object.prototype.toString.call(val).slice(8, -1)
return /(?:HTML)?(?:.*)Element/.test(val)
}
/**
* > Check that `key` exists in the given `obj`.
*
* **Example**
*
* ```js
* var obj = {a: 'b'}
*
* emitter._hasOwn(obj, 'a') //=> true
* emitter._hasOwn(obj, 'foo') //=> false
* ```
*
* @param {Object} `obj`
* @param {String} `key`
* @return {Boolean}
* @api public
*/
DualEmitter.prototype._hasOwn = function hasOwn (obj, key) {
return Object.prototype.hasOwnProperty.call(obj, key)
}
/**
* Static method for inheriting both the prototype and
* static methods of the `DualEmitter` class.
*
* ```js
* function MyApp(options) {
* DualEmitter.call(this)
* }
* DualEmitter.extend(MyApp)
*
*
* // Optionally pass another object to extend onto `MyApp`
* function MyApp(options) {
* DualEmitter.call(this)
* Foo.call(this, options)
* }
* DualEmitter.extend(MyApp, Foo.prototype)
* ```
*
* @param {Function} `Ctor` The constructor to extend.
* @api public
*/
DualEmitter.extend = function (Ctor, proto) {
util.inherits(Ctor, DualEmitter)
for (var key in DualEmitter) {
Ctor[key] = DualEmitter[key]
}
if (typeof proto === 'object') {
var obj = Object.create(proto)
for (var k in obj) {
Ctor.prototype[k] = obj[k]
}
}
}