1
1
/*!
2
- * vue-event-manager v2.0.1
2
+ * vue-event-manager v2.1.0
3
3
* https://github.com/pagekit/vue-event-manager
4
4
* Released under the MIT License.
5
5
*/
10
10
* Utility functions.
11
11
*/
12
12
13
+ var assign = Object . assign || _assign ;
14
+
13
15
var isArray = Array . isArray ;
14
16
15
- function isObject ( obj ) {
16
- return obj !== null && typeof obj === 'object' ;
17
+ function isObject ( val ) {
18
+ return val !== null && typeof val === 'object' ;
17
19
}
18
20
19
- function isUndefined ( obj ) {
20
- return typeof obj === 'undefined' ;
21
+ function isUndefined ( val ) {
22
+ return typeof val === 'undefined' ;
21
23
}
22
24
23
25
function forEach ( collection , callback ) {
24
- Object . keys ( collection || { } ) . forEach ( function ( key ) {
25
- callback . call ( null , collection [ key ] , key ) ;
26
- } ) ;
26
+ Object . keys ( collection || { } ) . forEach (
27
+ function ( key ) { return callback . call ( null , collection [ key ] , key ) ; }
28
+ ) ;
27
29
}
28
30
29
31
function array ( array ) {
30
32
if ( array === void 0 ) array = [ ] ;
31
33
32
34
33
35
if ( ! array . findIndex ) {
34
- array . findIndex = findIndex ;
36
+ array . findIndex = _findIndex ;
35
37
}
36
38
37
39
return array ;
38
40
}
39
41
42
+ /**
43
+ * Object.assign() polyfill.
44
+ */
45
+ function _assign ( target ) {
46
+ var sources = [ ] , len = arguments . length - 1 ;
47
+ while ( len -- > 0 ) sources [ len ] = arguments [ len + 1 ] ;
48
+
49
+
50
+ sources . forEach ( function ( source ) {
51
+ Object . keys ( source || { } ) . forEach (
52
+ function ( key ) { return target [ key ] = source [ key ] ; }
53
+ ) ;
54
+ } ) ;
55
+
56
+ return target ;
57
+ }
58
+
40
59
/**
41
60
* Array.findIndex() polyfill.
42
61
*/
43
- function findIndex ( predicate ) {
62
+ function _findIndex ( predicate ) {
44
63
45
64
if ( this == null ) {
46
65
throw new TypeError ( '"this" is null or not defined' ) ;
@@ -75,6 +94,7 @@ function findIndex(predicate) {
75
94
*/
76
95
77
96
var EventManager = function EventManager ( ) {
97
+ this . log = null ;
78
98
this . listeners = { } ;
79
99
} ;
80
100
@@ -116,53 +136,58 @@ EventManager.prototype.off = function off (event, callback) {
116
136
} ;
117
137
118
138
EventManager . prototype . trigger = function trigger ( event , params , asynch ) {
139
+ if ( params === void 0 ) params = [ ] ;
119
140
if ( asynch === void 0 ) asynch = false ;
120
141
121
142
122
- var $event = new Event ( event , params ) ;
143
+ var _event = new Event ( event , params ) ;
123
144
var reject = function ( result ) { return Promise . reject ( result ) ; } ;
124
- var resolve = function ( result ) { return ! isUndefined ( result ) ? result : $event . result ; } ;
145
+ var resolve = function ( result ) { return ! isUndefined ( result ) ? result : _event . result ; } ;
125
146
var reducer = function ( result , ref ) {
126
147
var callback = ref . callback ;
127
148
128
149
129
150
var next = function ( result ) {
130
151
131
152
if ( ! isUndefined ( result ) ) {
132
- $event . result = result ;
153
+ _event . result = result ;
133
154
}
134
155
135
156
if ( result === false ) {
136
- $event . stopPropagation ( ) ;
157
+ _event . stopPropagation ( ) ;
137
158
}
138
159
139
- if ( $event . isPropagationStopped ( ) ) {
140
- return $event . result ;
160
+ if ( _event . isPropagationStopped ( ) ) {
161
+ return _event . result ;
141
162
}
142
163
143
- return callback . apply ( callback , [ $event ] . concat ( $event . params ) ) ;
164
+ return callback . apply ( callback , [ _event ] . concat ( _event . params ) ) ;
144
165
} ;
145
166
146
167
return asynch ? result . then ( next , reject ) : next ( result ) ;
147
168
} ;
148
169
149
- var listeners = ( this . listeners [ event ] || [ ] ) . concat ( ) ;
170
+ var listeners = ( this . listeners [ _event . name ] || [ ] ) . concat ( ) ;
150
171
var result = listeners . reduce ( reducer , asynch ? Promise . resolve ( ) : undefined ) ;
151
172
173
+ if ( this . log ) {
174
+ this . log . call ( this , _event ) ;
175
+ }
176
+
152
177
return asynch ? result . then ( resolve , reject ) : resolve ( result ) ;
153
178
} ;
154
179
155
- var Event = function Event ( name , params ) {
156
- if ( params === void 0 ) params = [ ] ;
180
+ var Event = function Event ( event , params ) {
157
181
182
+ if ( ! isObject ( event ) ) {
183
+ event = { name : event } ;
184
+ }
158
185
159
186
if ( ! isArray ( params ) ) {
160
187
params = [ params ] ;
161
188
}
162
189
163
- this . name = name ;
164
- this . params = params ;
165
- this . result = undefined ;
190
+ assign ( this , event , { params : params , result : undefined } ) ;
166
191
} ;
167
192
168
193
Event . prototype . stopPropagation = function stopPropagation ( ) {
@@ -179,18 +204,64 @@ Event.prototype.isPropagationStopped = function isPropagationStopped () {
179
204
180
205
var Events = new EventManager ( ) ;
181
206
182
- Events . install = function ( Vue ) {
207
+ Events . install = function ( Vue , options ) {
208
+ if ( options === void 0 ) options = { } ;
209
+
183
210
184
211
if ( this . installed ) {
185
212
return ;
186
213
}
187
214
188
- Vue . events = this ;
189
- Vue . prototype . $events = this ;
190
- Vue . prototype . $trigger = this . trigger . bind ( this ) ;
191
- Vue . mixin ( Number ( Vue . version [ 0 ] ) < 2 ? { init : initEvents } : { beforeCreate : initEvents } ) ;
215
+ // add global instance/methods
216
+ Vue . prototype . $events = Vue . events = assign ( Events , options ) ;
217
+ Vue . prototype . $trigger = function ( event , params , asynch ) {
218
+ if ( params === void 0 ) params = [ ] ;
219
+ if ( asynch === void 0 ) asynch = false ;
220
+
221
+
222
+ if ( ! isObject ( event ) ) {
223
+ event = { name : event , origin : this } ;
224
+ }
225
+
226
+ return Events . trigger ( event , params , asynch ) ;
227
+ } ;
228
+
229
+ // add merge strategy for "events"
230
+ Vue . config . optionMergeStrategies . events = mergeEvents ;
231
+
232
+ // add mixin to parse "events" from component options
233
+ Vue . mixin ( { beforeCreate : initEvents } ) ;
192
234
} ;
193
235
236
+ function mergeEvents ( parentVal , childVal ) {
237
+
238
+ if ( ! childVal ) {
239
+ return parentVal ;
240
+ }
241
+
242
+ if ( ! parentVal ) {
243
+ return childVal ;
244
+ }
245
+
246
+ var events = assign ( { } , parentVal ) ;
247
+
248
+ for ( var event in childVal ) {
249
+
250
+ var parent = events [ event ] ;
251
+ var child = childVal [ event ] ;
252
+
253
+ if ( parent && ! isArray ( parent ) ) {
254
+ parent = [ parent ] ;
255
+ }
256
+
257
+ events [ event ] = parent
258
+ ? parent . concat ( child )
259
+ : isArray ( child ) ? child : [ child ] ;
260
+ }
261
+
262
+ return events ;
263
+ }
264
+
194
265
function initEvents ( ) {
195
266
var this$1 = this ;
196
267
0 commit comments