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

Commit

Permalink
initial import
Browse files Browse the repository at this point in the history
  • Loading branch information
steffans committed Apr 26, 2017
1 parent d6a15c4 commit 9281cca
Show file tree
Hide file tree
Showing 15 changed files with 3,617 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/node_modules
.DS_Store
2 changes: 2 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
build
docs
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2015-2017 steffans

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
30 changes: 29 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,29 @@
# vue-event-manager
# vue-event-manager [![Version](https://img.shields.io/npm/v/vue-event-manager.svg)](https://www.npmjs.com/package/vue-event-manager) [![License](https://img.shields.io/npm/l/vue-event-manager.svg)](https://www.npmjs.com/package/vue-event-manager) [![Downloads](https://img.shields.io/npm/dt/vue-event-manager.svg)](https://www.npmjs.com/package/vue-event-manager)

The plugin for [Vue.js](http://vuejs.org) provides a declarative way to bind events to a global event manager. It uses the Vue lifecycle to automatically bind and unbind all events.

## Features

- Supports event priorities and [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) based asynchronous events
- Supports latest Firefox, Chrome, Safari, Opera and IE9+
- Supports Vue 1.0 & Vue 2.0
- Compact size 2KB

## Installation
You can install it via [yarn](https://yarnpkg.com/) or [NPM](http://npmjs.org/).
```
$ yarn add vue-event-manager
$ npm install vue-event-manager
```

## Changelog

Details changes for each release are documented in the [release notes](https://github.com/pagekit/vue-event-manager/releases).

## Contribution

If you find a bug or want to contribute to the code or documentation, you can help by submitting an [issue](https://github.com/pagekit/vue-event-manager/issues) or a [pull request](https://github.com/pagekit/vue-event-manager/pulls).

## License

[MIT](http://opensource.org/licenses/MIT)
63 changes: 63 additions & 0 deletions build/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
var fs = require('fs');
var rollup = require('rollup');
var uglify = require('uglify-js');
var buble = require('rollup-plugin-buble');
var package = require('../package.json');
var banner =
"/*!\n" +
" * vue-event-manager v" + package.version + "\n" +
" * https://github.com/pagekit/vue-event-manager\n" +
" * Released under the MIT License.\n" +
" */\n";

rollup.rollup({
entry: 'src/index.js',
plugins: [buble()]
})
.then(function (bundle) {
return write('dist/vue-event-manager.js', bundle.generate({
format: 'umd',
banner: banner,
moduleName: 'VueEventManager'
}).code, bundle);
})
.then(function (bundle) {
return write('dist/vue-event-manager.min.js',
banner + '\n' + uglify.minify('dist/vue-event-manager.js').code,
bundle);
})
.then(function (bundle) {
return write('dist/vue-event-manager.es2015.js', bundle.generate({
format: 'es',
banner: banner
}).code, bundle);
})
.then(function (bundle) {
return write('dist/vue-event-manager.common.js', bundle.generate({
format: 'cjs',
banner: banner
}).code, bundle);
})
.catch(logError);

function write(dest, code, bundle) {
return new Promise(function (resolve, reject) {
fs.writeFile(dest, code, function (err) {
if (err) return reject(err);
console.log(blue(dest) + ' ' + getSize(code));
resolve(bundle);
});
});
}

function getSize(code) {
return (code.length / 1024).toFixed(2) + 'kb';
}

function logError(e) {
console.log(e);
}

function blue(str) {
return '\x1b[1m\x1b[34m' + str + '\x1b[39m\x1b[22m';
}
14 changes: 14 additions & 0 deletions build/release.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
var replace = require('replace-in-file');
var version = process.argv[2];

replace({
files: "package.json",
from: /("version"\s*:\s*")\d+\.\d+\.\d+("\s*,)/g,
to: "$1" + version + "$2"
});

replace({
files: "README.md",
from: /(\/|@)\d+\.\d+\.\d+/g,
to: "$1" + version
});
157 changes: 157 additions & 0 deletions dist/vue-event-manager.common.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
/*!
* vue-event-manager v1.0.0
* https://github.com/pagekit/vue-event-manager
* Released under the MIT License.
*/

'use strict';

/**
* Utility functions.
*/

var isArray = Array.isArray;

function isObject(obj) {
return obj !== null && typeof obj === 'object';
}

function forEach(collection, callback) {
Object.keys(collection || {}).forEach(function (key) {
callback.call(null, collection[key], key);
});
}

/**
* Event manager class.
*/

var EventManager = function EventManager() {
this.listeners = {};
};

EventManager.prototype.on = function on (event, callback, priority) {
var this$1 = this;
if ( priority === void 0 ) priority = 0;


var listeners = this.listeners[event] || [], index;

index = listeners.findIndex(function (listener) { return listener.priority < priority; });

if (~index) {
listeners.splice(index, 0, {callback: callback, priority: priority});
} else {
listeners.push({callback: callback, priority: priority});
}

this.listeners[event] = listeners;

return function () { return this$1.off(event, callback); };
};

EventManager.prototype.off = function off (event, callback) {

if (!callback) {
delete this.listeners[event];
}

var listeners = this.listeners[event], index;

if (listeners && callback) {

index = listeners.findIndex(function (listener) { return listener.callback === callback; });

if (~index) {
listeners.splice(index, 1);
}
}
};

EventManager.prototype.trigger = function trigger (event, params, asynch) {
if ( params === void 0 ) params = [];
if ( asynch === void 0 ) asynch = false;


if (!isArray(params)) {
params = [params];
}

return ((this.listeners[event] || []).concat()).reduce(function (result, listener) {

var callback = function (result) {

if (result === false) {
return result;
}

if (isArray(result)) {
params = result;
}

return listener.callback.apply(listener.callback, params);
};

if (asynch) {
return result.then(callback);
}

return callback(result);

}, asynch ? Promise.resolve() : undefined);
};

/**
* Install plugin.
*/

function plugin(Vue) {

if (plugin.installed) {
return;
}

var Events = new EventManager();

Vue.mixin({

init: function init() {
var this$1 = this;


var ref = this.$options;
var events = ref.events;
var _events = [];

if (events) {

forEach(events, function (listeners, event) {
forEach(isArray(listeners) ? listeners : [listeners], function (listener) {

var priority = 0;

if (isObject(listener)) {
priority = listener.priority;
listener = listener.handler;
}

_events.push(Events.on(event, listener.bind(this$1), priority));
});
});

this.$on('hook:beforeDestroy', function () { return _events.forEach(function (off) { return off(); }); });
}

}

});

Vue.prototype.$events = Events;
Vue.prototype.$trigger = Events.trigger.bind(Events);
}

if (typeof window !== 'undefined' && window.Vue) {
window.Vue.use(plugin);
}

module.exports = plugin;
Loading

0 comments on commit 9281cca

Please sign in to comment.