Commit 4dd4c594 authored by Julien Cigar's avatar Julien Cigar
Browse files

added Yeti.Tools.Dispatcher

parent 25b74821
......@@ -12,7 +12,7 @@
return Array.prototype.slice.call(arguments).join('/').replace(/\/{2,}/, '/');
}
/* Shortcut to document.getElementById */
/* Wrapper for document.getElementById */
Yeti.Element = function(src) {
if (typeof(src) === 'string') {
......@@ -186,7 +186,7 @@
return typeof(JSON) !== 'undefined' ?
JSON.parse(data) :
(function(src) {
// See RFC 4627
// Taken from RFC 4627 (http://tools.ietf.org/html/rfc4627)
var json = !(/[^,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]/.test(src.replace(/"(\\.|[^"\\])*"/g, '')));
return json ? eval('(' + json + ')') : null;
})(data);
......@@ -433,13 +433,14 @@
*/
Yeti.DOM.getWindowSize = function() {
return typeof(window.innerHeight == 'number') ? {
return
typeof(window.innerHeight == 'number') ? {
height : window.innerHeight,
width : window.innerWidth
} : document.body && document.body.clientHeight ? {
height : document.body.clientHeight,
width : document.body.clientWidth
} : document.documentElement &&
} : document.documentElement &&
document.documentElement.clientHeight ? {
height : document.documentElement.clientHeight,
width : document.documentElement.clientWidth
......@@ -500,4 +501,63 @@
this.qs.push(encodeURIComponent(key) + '=' + encodeURIComponent(value));
}
/* Yeti.Tools.Dispatcher
* A simple subscribe/publish dispatcher
*/
Yeti.Tools.Dispatcher = function(scope) {
this.default_scope = scope || this;
this.callbacks = {};
}
Yeti.Tools.Dispatcher.prototype.add = function(evt, callback, scope) {
if (!this.callbacks[evt]) {
this.callbacks[evt] = [];
}
this.callbacks[evt].push({
'evt' : evt,
'callback' : callback,
'scope' : scope || this.default_scope
});
}
Yeti.Tools.Dispatcher.prototype.remove = function(params) {
var _checks = ['evt', 'callback', 'scope'];
for (var i=0, _len=this.callbacks.length; i<_len; i++) {
var cb = this.callbacks[i],
cond = undefined
;
for (var i in _checks) {
var check = _checks[i];
if (params[check] !== undefined) {
if (cond === undefined) {
cond = params[check] === cb[check] ? true : false;
} else {
cond &= params[check] === cb[check] ? true : false;
}
}
}
if (cond) {
delete this.callback[i];
}
}
}
Yeti.Tools.Dispatcher.prototype.fire = function(evt, params) {
var cbs = this.callbacks[evt];
if (!cbs) {
return false;
}
for (var i=0, _len=cbs.length; i<_len; i++) {
cbs[i].callback.call(cbs[i].scope, params, arguments);
}
}
})(window);
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment