11/*!
2- * vue-event-manager v2.0.1
2+ * vue-event-manager v2.1.0
33 * https://github.com/pagekit/vue-event-manager
44 * Released under the MIT License.
55 */
1010 * Utility functions.
1111 */
1212
13+ var assign = Object . assign || _assign ;
14+
1315var isArray = Array . isArray ;
1416
15- function isObject ( obj ) {
16- return obj !== null && typeof obj === 'object' ;
17+ function isObject ( val ) {
18+ return val !== null && typeof val === 'object' ;
1719}
1820
19- function isUndefined ( obj ) {
20- return typeof obj === 'undefined' ;
21+ function isUndefined ( val ) {
22+ return typeof val === 'undefined' ;
2123}
2224
2325function 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+ ) ;
2729}
2830
2931function array ( array ) {
3032 if ( array === void 0 ) array = [ ] ;
3133
3234
3335 if ( ! array . findIndex ) {
34- array . findIndex = findIndex ;
36+ array . findIndex = _findIndex ;
3537 }
3638
3739 return array ;
3840}
3941
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+
4059/**
4160 * Array.findIndex() polyfill.
4261 */
43- function findIndex ( predicate ) {
62+ function _findIndex ( predicate ) {
4463
4564 if ( this == null ) {
4665 throw new TypeError ( '"this" is null or not defined' ) ;
@@ -75,6 +94,7 @@ function findIndex(predicate) {
7594 */
7695
7796var EventManager = function EventManager ( ) {
97+ this . log = null ;
7898 this . listeners = { } ;
7999} ;
80100
@@ -116,53 +136,58 @@ EventManager.prototype.off = function off (event, callback) {
116136} ;
117137
118138EventManager . prototype . trigger = function trigger ( event , params , asynch ) {
139+ if ( params === void 0 ) params = [ ] ;
119140 if ( asynch === void 0 ) asynch = false ;
120141
121142
122- var $event = new Event ( event , params ) ;
143+ var _event = new Event ( event , params ) ;
123144 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 ; } ;
125146 var reducer = function ( result , ref ) {
126147 var callback = ref . callback ;
127148
128149
129150 var next = function ( result ) {
130151
131152 if ( ! isUndefined ( result ) ) {
132- $event . result = result ;
153+ _event . result = result ;
133154 }
134155
135156 if ( result === false ) {
136- $event . stopPropagation ( ) ;
157+ _event . stopPropagation ( ) ;
137158 }
138159
139- if ( $event . isPropagationStopped ( ) ) {
140- return $event . result ;
160+ if ( _event . isPropagationStopped ( ) ) {
161+ return _event . result ;
141162 }
142163
143- return callback . apply ( callback , [ $event ] . concat ( $event . params ) ) ;
164+ return callback . apply ( callback , [ _event ] . concat ( _event . params ) ) ;
144165 } ;
145166
146167 return asynch ? result . then ( next , reject ) : next ( result ) ;
147168 } ;
148169
149- var listeners = ( this . listeners [ event ] || [ ] ) . concat ( ) ;
170+ var listeners = ( this . listeners [ _event . name ] || [ ] ) . concat ( ) ;
150171 var result = listeners . reduce ( reducer , asynch ? Promise . resolve ( ) : undefined ) ;
151172
173+ if ( this . log ) {
174+ this . log . call ( this , _event ) ;
175+ }
176+
152177 return asynch ? result . then ( resolve , reject ) : resolve ( result ) ;
153178} ;
154179
155- var Event = function Event ( name , params ) {
156- if ( params === void 0 ) params = [ ] ;
180+ var Event = function Event ( event , params ) {
157181
182+ if ( ! isObject ( event ) ) {
183+ event = { name : event } ;
184+ }
158185
159186 if ( ! isArray ( params ) ) {
160187 params = [ params ] ;
161188 }
162189
163- this . name = name ;
164- this . params = params ;
165- this . result = undefined ;
190+ assign ( this , event , { params : params , result : undefined } ) ;
166191} ;
167192
168193Event . prototype . stopPropagation = function stopPropagation ( ) {
@@ -179,18 +204,64 @@ Event.prototype.isPropagationStopped = function isPropagationStopped () {
179204
180205var Events = new EventManager ( ) ;
181206
182- Events . install = function ( Vue ) {
207+ Events . install = function ( Vue , options ) {
208+ if ( options === void 0 ) options = { } ;
209+
183210
184211 if ( this . installed ) {
185212 return ;
186213 }
187214
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 } ) ;
192234} ;
193235
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+
194265function initEvents ( ) {
195266 var this$1 = this ;
196267
0 commit comments