kaffee
Version:
Kaffee is a software project management tool similar to Maven and is written in Coffeescript. Kaffee allows you to compile, test, minify and many other tasks to make building your application simple and fun again.
329 lines (254 loc) • 8.22 kB
JavaScript
(function() {
var EventManager, LogEvent,
__slice = [].slice;
LogEvent = require('./logevent');
/*
The {@link EventManager} class manages events fired by Kaffee.
@author Fabian M. <mail.fabianm@gmail.com>
*/
EventManager = (function() {
/*
Constructs a new {@link EventManager} instance.
@since 0.3.0
@param name The name of this {@link EventManager}.
@param parent The parent {@link EventManager} instance.
@param attachment An attachment object.
*/
function EventManager(name, parent, attachment) {
this.name = name;
this.parent = parent;
this.attachment = attachment;
this.events = {};
}
/*
Sets the name of this {@link EventManager}.
@since 0.3.0
@param name The name to set.
*/
EventManager.prototype.setName = function(name) {
return this.name = name;
};
/*
Returns the name of this {@link EventManager}.
@since 0.3.0
@return The name of this {@link EventManager}.
*/
EventManager.prototype.getName = function() {
return this.name;
};
/*
Returns the attachment object of this {@link EventManager}.
@since 0.3.0
@return The attachment object of this {@link EventManager}.
*/
EventManager.prototype.getAttachment = function() {
return this.attachment;
};
/*
Sets the attachment object of this {@link EventManager}.
@since 0.3.0
@param attachment The attachment object to set.
*/
EventManager.prototype.setAttachment = function(attachment) {
return this.attachment = attachment;
};
/*
Returns a logger object that provides basic logging functionality.
@since 0.3.0
@return A logger object.
*/
EventManager.prototype.getLogger = function() {
return (function(manager) {
/*
The Level object defines a set of standard logging levels that can be used to control logging output.
@since 0.3.0
*/
this.Level = {
ERROR: {
name: 'error',
value: 3
},
WARN: {
name: 'warn',
value: 2
},
DEBUG: {
name: 'debug',
value: 1
},
INFO: {
name: 'info',
value: 0
}
};
/*
Logs a {@link LogEvent}.
@since 0.3.0
@param log The {@link LogEvent} to log.
*/
this.log = function(log) {
return manager.fire("" + (manager.getName()) + ":log", log);
};
/*
Logs a {@link Level#ERROR} message.
@since 0.3.0
@param message The message to log.
*/
this.error = function(message) {
var event;
event = new LogEvent(manager, this.Level.ERROR, message, null, arguments.callee);
this.log(event);
return manager.fire("" + (manager.getName()) + ":error", event);
};
/*
Logs a {@link Level#WARN} message.
@since 0.3.0
@param message The message to log.
*/
this.warn = function(message) {
var event;
event = new LogEvent(manager, this.Level.WARN, message, null, arguments.callee);
this.log(event);
return manager.fire("" + (manager.getName()) + ":warn", event);
};
/*
Logs a {@link Level#DEBUG} message.
@since 0.3.0
@param message The message to log.
*/
this.debug = function(message) {
var event;
event = new LogEvent(manager, this.Level.DEBUG, message, null, arguments.callee);
this.log(event);
return manager.fire("" + (manager.getName()) + ":debug", event);
};
/*
Logs a {@link Level#INFO} message.
@since 0.3.0
@param message The message to log.
*/
this.info = function(message) {
var event;
event = new LogEvent(manager, this.Level.INFO, message, null, arguments.callee);
this.log(event);
return manager.fire("" + (manager.getName()) + ":info", event);
};
return this;
})(this);
};
/*
Returns the parent {@link EventManager} of this {@link EventManager}.
@since 0.3.0
@return The parent {@link EventManager} of this {@link EventManager}.
*/
EventManager.prototype.getParent = function() {
return this.parent;
};
/*
Sets the parent {@link EventManager} of this {@link EventManager}.
@since 0.3.0
@param parent The parent {@link EventManager} to set.
*/
EventManager.prototype.setParent = function(parent) {
this.parent = parent;
};
/*
Returns the events of this {@link EventManager}.
@since 0.3.0
@return The events of this {@link EventManager}.
*/
EventManager.prototype.getEvents = function() {
return this.events;
};
/*
Returns the listeners of an event of this {@link EventManager}.
@
@since 0.3.0
@param name The name of the event.
@return The listeners of an event of this {@link EventManager}.
*/
EventManager.prototype.getListeners = function(name) {
var item, listeners, regex, result, _ref;
result = [];
regex = this.getRegex(name);
_ref = this.events;
for (item in _ref) {
listeners = _ref[item];
if (this.getRegex(item).test(name || this.regex.test(item))) {
result = result.concat(listeners);
}
}
return result;
};
/*
Returns the RegEx of a string.
@since 0.3.0
@param str The string.
@return The RegEx.
*/
EventManager.prototype.getRegex = function(str) {
return new RegExp("^" + str.replace(new RegExp("[.*+?|()\\[\\]{}\\\\]", "g"), "\\$&").replace("\\*", ".*").replace("\\?", ".") + "$");
};
/*
Adds a listener to this {@link EventManager}.
@since 0.3.0
@param listener The listener to add.
*/
EventManager.prototype.addListener = function(name, listener) {
var key, value, _i, _j, _len, _len1;
if (typeof name === 'object') {
for (value = _i = 0, _len = name.length; _i < _len; value = ++_i) {
key = name[value];
if (!this.events[key]) {
this.events[key] = [];
}
this.events[key] = this.events[key].concat(value);
}
}
if (typeof name === 'array' && typeof listener === 'function') {
for (_j = 0, _len1 = name.length; _j < _len1; _j++) {
key = name[_j];
if (!this.events[key]) {
this.events[key] = [];
}
this.events[key].push(listener);
}
}
if (typeof name === 'string' && typeof listener === 'function') {
if (!this.events[name]) {
this.events[name] = [];
}
return this.events[name].push(listener);
}
};
/*
@see EventManager#addListener(String, Function)
*/
EventManager.prototype.on = function(name, listener) {
return this.addListener(name, listener);
};
/*
Fires an event.
@since 0.3.0
@param name The name of the event to fire.
@param args The arguments.
*/
EventManager.prototype.fire = function() {
var args, listener, name, _i, _len, _ref;
name = arguments[0], args = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
if (!name) {
return false;
}
_ref = this.getListeners(name);
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
listener = _ref[_i];
listener.apply(null, args);
}
if (this.parent) {
return this.parent.fire.apply(this.parent, [name].concat(args));
}
};
return EventManager;
})();
module.exports = EventManager;
}).call(this);