Skip to content
This repository was archived by the owner on Dec 1, 2023. It is now read-only.

Commit fa669af

Browse files
author
Steffan
committed
add version, update deps
1 parent 0e5ac32 commit fa669af

File tree

6 files changed

+217
-139
lines changed

6 files changed

+217
-139
lines changed

build/build.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const fs = require('fs');
44
const rollup = require('rollup');
55
const uglify = require('uglify-js');
66
const buble = require('rollup-plugin-buble');
7+
const replace = require('rollup-plugin-replace');
78
const {version} = require('../package.json');
89
const banner =
910
'/*!\n' +
@@ -14,7 +15,7 @@ const banner =
1415

1516
rollup.rollup({
1617
input: 'src/index.js',
17-
plugins: [buble()]
18+
plugins: [buble(), replace({__VERSION__: version})]
1819
})
1920
.then(bundle =>
2021
bundle.generate({

package.json

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,22 @@
3434
"@storybook/vue": "^3.4.10",
3535
"buble": "^0.19.3",
3636
"buble-loader": "^0.5.1",
37-
"eslint": "^5.3.0",
37+
"eslint": "^5.4.0",
3838
"jasmine": "^3.1.0",
39-
"jasmine-core": "^3.1.0",
39+
"jasmine-core": "^3.2.1",
4040
"karma": "^2.0.0",
4141
"karma-chrome-launcher": "^2.2.0",
4242
"karma-firefox-launcher": "^1.1.0",
4343
"karma-jasmine": "^1.1.1",
4444
"karma-safari-launcher": "^1.0.0",
4545
"karma-webpack": "^2.0.9",
46-
"replace-in-file": "^3.1.0",
46+
"replace-in-file": "^3.4.2",
4747
"rollup": "^0.64.1",
4848
"rollup-plugin-buble": "^0.19.2",
49-
"uglify-js": "^3.3.9",
49+
"rollup-plugin-replace": "^2.0.0",
50+
"uglify-js": "^3.4.7",
5051
"vue": "^2.5.17",
51-
"vue-loader": "^15.3.0",
52+
"vue-loader": "^15.4.0",
5253
"vue-template-compiler": "^2.5.17",
5354
"webpack": "^3.10.0"
5455
}

src/index.js

Lines changed: 3 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -2,102 +2,10 @@
22
* Install plugin.
33
*/
44

5-
import EventManager from './EventManager';
6-
import {assign, forEach, isArray, isObject} from './util';
7-
8-
const Events = new EventManager();
9-
10-
Events.install = function (Vue, options = {}) {
11-
12-
if (this.installed) {
13-
return;
14-
}
15-
16-
// add global instance/methods
17-
Vue.prototype.$events = Vue.events = assign(Events, options);
18-
Vue.prototype.$trigger = function (event, params = [], asynch = false) {
19-
20-
if (!isObject(event)) {
21-
event = {name: event, origin: this};
22-
}
23-
24-
return Events.trigger(event, params, asynch);
25-
};
26-
27-
// add merge strategy for "events"
28-
Vue.config.optionMergeStrategies.events = mergeEvents;
29-
30-
// add mixin to parse "events" from component options
31-
Vue.mixin({beforeCreate: initEvents});
32-
};
33-
34-
function mergeEvents(parentVal, childVal) {
35-
36-
if (!childVal) {
37-
return parentVal;
38-
}
39-
40-
if (!parentVal) {
41-
return childVal;
42-
}
43-
44-
const events = assign({}, parentVal);
45-
46-
for (const event in childVal) {
47-
48-
let parent = events[event];
49-
const child = childVal[event];
50-
51-
if (parent && !isArray(parent)) {
52-
parent = [parent];
53-
}
54-
55-
events[event] = parent
56-
? parent.concat(child)
57-
: isArray(child) ? child : [child];
58-
}
59-
60-
return events;
61-
}
62-
63-
function initEvents() {
64-
65-
const _events = [];
66-
const {events} = this.$options;
67-
68-
if (events) {
69-
70-
forEach(events, (listeners, event) => {
71-
forEach(isArray(listeners) ? listeners : [listeners], listener => {
72-
73-
let priority = 0;
74-
75-
if (isObject(listener)) {
76-
priority = listener.priority;
77-
listener = listener.handler;
78-
}
79-
80-
_events.push(Events.on(event, bindListener(listener, this), priority));
81-
});
82-
});
83-
84-
this.$on('hook:beforeDestroy', () => _events.forEach(off => off()));
85-
}
86-
}
87-
88-
function bindListener(fn, vm) {
89-
90-
if (typeof fn === 'string') {
91-
return function () {
92-
return vm[fn].apply(vm, arguments);
93-
};
94-
}
95-
96-
return fn.bind(vm);
97-
}
5+
import Plugin from './plugin';
986

997
if (typeof window !== 'undefined' && window.Vue) {
100-
window.Vue.use(Events);
8+
window.Vue.use(Plugin);
1019
}
10210

103-
export default Events;
11+
export default Plugin;

src/plugin.js

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/**
2+
* Plugin class.
3+
*/
4+
5+
import EventManager from './EventManager';
6+
import Util, {log, assign, forEach, isArray, isObject} from './util';
7+
8+
const Events = new EventManager();
9+
10+
export default {
11+
12+
version: '__VERSION__',
13+
14+
install(Vue, options = {}) {
15+
16+
if (this.installed) {
17+
return;
18+
}
19+
20+
Util(Vue); log(this.version);
21+
22+
// add global instance/methods
23+
Vue.prototype.$events = Vue.events = assign(Events, options);
24+
Vue.prototype.$trigger = function (event, params = [], asynch = false) {
25+
26+
if (!isObject(event)) {
27+
event = {name: event, origin: this};
28+
}
29+
30+
return Events.trigger(event, params, asynch);
31+
};
32+
33+
// add merge strategy for "events"
34+
Vue.config.optionMergeStrategies.events = mergeEvents;
35+
36+
// add mixin to parse "events" from component options
37+
Vue.mixin({beforeCreate: initEvents});
38+
}
39+
40+
};
41+
42+
export function mergeEvents(parentVal, childVal) {
43+
44+
if (!childVal) {
45+
return parentVal;
46+
}
47+
48+
if (!parentVal) {
49+
return childVal;
50+
}
51+
52+
const events = assign({}, parentVal);
53+
54+
for (const event in childVal) {
55+
56+
let parent = events[event];
57+
const child = childVal[event];
58+
59+
if (parent && !isArray(parent)) {
60+
parent = [parent];
61+
}
62+
63+
events[event] = parent
64+
? parent.concat(child)
65+
: isArray(child) ? child : [child];
66+
}
67+
68+
return events;
69+
}
70+
71+
export function initEvents() {
72+
73+
const _events = [];
74+
const {events} = this.$options;
75+
76+
if (events) {
77+
78+
forEach(events, (listeners, event) => {
79+
forEach(isArray(listeners) ? listeners : [listeners], listener => {
80+
81+
let priority = 0;
82+
83+
if (isObject(listener)) {
84+
priority = listener.priority;
85+
listener = listener.handler;
86+
}
87+
88+
_events.push(this.$events.on(event, bindListener(listener, this), priority));
89+
});
90+
});
91+
92+
this.$on('hook:beforeDestroy', () => _events.forEach(off => off()));
93+
}
94+
}
95+
96+
export function bindListener(fn, vm) {
97+
98+
if (typeof fn === 'string') {
99+
return function () {
100+
return vm[fn].apply(vm, arguments);
101+
};
102+
}
103+
104+
return fn.bind(vm);
105+
}

src/util.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,26 @@
22
* Utility functions.
33
*/
44

5+
let _config = {};
6+
57
export const assign = Object.assign || _assign;
68

79
export const isArray = Array.isArray;
810

11+
export default function ({config}) {
12+
_config = config;
13+
}
14+
15+
export function log(message, color = '#41B883') {
16+
if (typeof console !== 'undefined' && _config.devtools) {
17+
console.log(`%c vue-event-manager %c ${message} `, 'color: #fff; background: #35495E; padding: 1px; border-radius: 3px 0 0 3px;', `color: #fff; background: ${color}; padding: 1px; border-radius: 0 3px 3px 0;`);
18+
}
19+
}
20+
21+
export function warn(message, color = '#DB6B00') {
22+
log(message, color);
23+
}
24+
925
export function isObject(val) {
1026
return val !== null && typeof val === 'object';
1127
}

0 commit comments

Comments
 (0)