Last active
August 29, 2015 14:18
-
-
Save ikouchiha47/7e02c41a355245e0be15 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// getEvents() | |
// Keep a track of the events list for each element | |
// getListeners(event) | |
// Keep a track of the listener list for each event | |
/* ===================Handle adding of event=================== */ | |
// addEventsListeners(element, event, listener or array_of_listeners) | |
// If a new event with a listener is added | |
// Make the first item in list to be the DOM element | |
// Check if the event is already in list or create it | |
// Add the listener to the list corresponding to the event | |
// Else | |
// Add the event to the event list and the listener to a new listener list for the event | |
// EndIf | |
/* ===================Handle firing of event=================== */ | |
// emitEvent(element, event, options) | |
// options = { timeIntervalBetweenEachListener (defaults to 500ms), numberOfListeners (defaults to all) } | |
// For an event check if the event exists in list on the element | |
// If event exists | |
// Check if the numberOfListeners are provided else call all the listeners | |
// Else | |
// Throw Error | |
// Endif | |
/* ===================Remove events=================== */ | |
// removeEventsListeners(event) | |
// Get the List of Events | |
// If event is specified | |
// remove the event from the list of events | |
// Else | |
// remove all events | |
// Endif | |
;var vcr = (function() { | |
function classOf(whatever) { | |
return Object.prototype.toString.call(whatever).slice(8, -1) | |
} | |
function $(sel) { | |
var dom = document.querySelectorAll(sel); | |
if(classOf(sel) == "String") { | |
return dom.length ==1 ? dom[0] : [].slice.call(dom) | |
} | |
else if(sel instanceof HTMLElement) { | |
return sel | |
} | |
else { | |
throw new Error("Feature not supported!!") | |
} | |
} | |
return { | |
getEvents: function() { | |
return this.events || (this.events = {}) | |
}, | |
getListeners: function(element, event) { | |
element = element || "body"; | |
this.events[element] = this.events[element] || {} | |
return this.events[element][event] || (this.events[element][event] = []) | |
}, | |
addEventsListeners: function(element, event, listeners) { | |
var events = this.getEvents() | |
, eventListeners = this.getListeners(element, event) | |
; | |
if(classOf(listeners) === 'Function') { | |
eventListeners.push(listeners); | |
} else if(Array.isArray(listeners)) { | |
for(var i = 0, len = eventListeners.length; i < len; i++) { | |
this.addEventsListeners(element, event, eventListeners[i]) | |
} | |
} else { | |
throw new Error("Need function or array of functions") | |
} | |
}, | |
emitEvent: function(element, event, options) { | |
var timeOut = options && options.timeOut || 500 | |
, limit = options && options.limit || 0 | |
, eventListeners = this.getListeners(element, event) | |
, element = $(element) | |
; | |
for(var i = 0, len = eventListeners.length - limit; len > 0 && i < len; i++) { | |
var evt = new Event(event) | |
element.addEventListener(event, eventListeners[i]); | |
element.dispatchEvent(evt); | |
} | |
} | |
} | |
}()); | |
vcr.addEventsListeners("body", "click", function(e) { console.log(1) }); | |
vcr.emitEvent("body", "click"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment