Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

add a minified version

  • Loading branch information...
commit 9ccd2f538a82374dc4e3e82b6da7e45f16b60a3b 1 parent 61b21c1
@tobowers authored
Showing with 37 additions and 0 deletions.
  1. +37 −0 event_handler_min.js
View
37 event_handler_min.js
@@ -0,0 +1,37 @@
+if(!("MBX"in window)){MBX={};}
+MBX.EventHandler=(function(){var stdEvents=["click","mouseout","mouseover","keypress","change"];var ieFocusBlurEvents=["focusin","focusout"];var nonBubblingBlurFocusEvents=["blur","focus"];var eventListeners={};var handleEvent=function(evt){var targetElement;if(Event&&evt){try{targetElement=Event.element(evt);}catch(e){}}
+if(targetElement){functionsFromElementAndEvent(targetElement,evt);}};stdEvents.each(function(evtType){eventListeners[evtType]=document.observe(evtType,handleEvent);});if(!Prototype.Browser.IE){nonBubblingBlurFocusEvents.each(function(evtType){eventListeners[evtType]=document.addEventListener(evtType,handleEvent,true);});}else{var handleIEFocusEvents=function(evt){var targetElement;if(Event&&evt){try{targetElement=evt.srcElement;}catch(e){}}
+if(targetElement){var eventType;switch(evt.type){case"focusin":eventType="focus";break;case"focusout":eventType="blur";break;}
+functionsFromElementAndEvent(targetElement,evt,{eventType:eventType});}};ieFocusBlurEvents.each(function(evtType){eventListeners[evtType]=document.observe(evtType,handleIEFocusEvents);});}
+var domReadyAlreadyFired=false;document.observe("dom:loaded",function(){domReadyAlreadyFired=true;MBX.EventHandler.fireCustom(document,"dom:loaded");});var subscriptions={ids:{},classes:{},rules:{},objects:{}};var subscriptionMarker=1;var callFunctions=function(functionsToCall,evt){while(functionsToCall.length>0){functionsToCall.pop()(evt);}};var deferFunctions=function(functionsToCall,evt){var func;while(functionsToCall.length>0){func=functionsToCall.pop().wrap(function(orig){orig(evt);});setTimeout(func,0);}};var callFunctionsFromRules=function(target,evtType,evt){if(!subscriptions.rules[evtType]){return;}
+var functionsToCall=[];var functionsToDefer=[];for(prop in subscriptions.rules[evtType]){if(subscriptions.rules[evtType].hasOwnProperty(prop)&&target.match(prop)){functionsToCall=functionsToCall.concat(subscriptions.rules[evtType][prop]);if(subscriptions.rules[evtType][prop].deferrable){functionsToDefer=functionsToDefer.concat(subscriptions.rules[evtType][prop].deferrable);}}}
+callFunctions(functionsToCall,evt);deferFunctions(functionsToDefer,evt);};var callFunctionsFromIdOrObject=function(specifierType,targetId,evtType,evt){var returnArray=[];var deferArray=[];var subscriptionTarget=subscriptions[specifierType][targetId];if(subscriptionTarget&&subscriptionTarget[evtType]){returnArray=returnArray.concat(subscriptionTarget[evtType]);if(subscriptionTarget[evtType].deferrable){deferArray=deferArray.concat(subscriptionTarget[evtType].deferrable);}}
+callFunctions(returnArray,evt);deferFunctions(deferArray,evt);};var callFunctionsFromClasses=function(targetClasses,evtType,evt){var functionsToCall=[];var functionsToDefer=[];var classObject;targetClasses=$A(targetClasses);for(var index=0,classLen=targetClasses.length;index<classLen;++index){classObject=subscriptions.classes[targetClasses[index]];if(classObject&&classObject[evtType]){functionsToCall=functionsToCall.concat(classObject[evtType]);if(classObject[evtType].deferrable){functionsToDefer.concat(classObject[evtType].deferrable);}}}
+callFunctions(functionsToCall,evt);deferFunctions(functionsToDefer,evt);};var functionsFromElementAndEvent=function(targetElement,evt,opts){if(!targetElement){return;}
+opts=opts||{};var evtType;if(opts.eventType){evtType=opts.eventType;}else{evtType=evt.type;}
+if(targetElement.__MotionboxEventHandlerMaker){callFunctionsFromIdOrObject("objects",targetElement.__MotionboxEventHandlerMaker,evtType,evt);if(!Object.isElement(targetElement)){return;}}
+if(targetElement.id){callFunctionsFromIdOrObject("ids",targetElement.id,evtType,evt);}
+if(targetElement.className){var targetClasses=Element.classNames(targetElement);callFunctionsFromClasses(targetClasses,evtType,evt);}
+callFunctionsFromRules(targetElement,evtType,evt);if(targetElement!=window&&targetElement!=document&&targetElement.parentNode){var upTreeNode=targetElement.parentNode;if(upTreeNode&&upTreeNode.tagName&&upTreeNode.tagName!="HTML"){functionsFromElementAndEvent($(upTreeNode),evt,opts);}}};var createIdClassorObjectSubscription=function(specifierType,specifier,evtTypes,funcs,opts){var subscriptionArray=[];if(!subscriptions[specifierType][specifier]){subscriptions[specifierType][specifier]={};}
+var specifierObject=subscriptions[specifierType][specifier];evtTypes.each(function(evtType){if(!specifierObject[evtType]){specifierObject[evtType]=[];}
+if(opts.defer&&!specifierObject[evtType].deferrable){specifierObject[evtType].deferrable=[];}
+funcs.each(function(func){if(opts.defer){specifierObject[evtType].deferrable.push(func);}else{specifierObject[evtType].push(func);}
+subscriptionArray.push({'specifierType':specifierType,'eventType':evtType,'func':func,'specifier':specifier});});});return subscriptionArray;};var createRulesSubscription=function(specifier,evtTypes,funcs,opts){var subscriptionArray=[];evtTypes.each(function(evtType){if(!subscriptions.rules[evtType]){subscriptions.rules[evtType]={};}
+var specifierObject=subscriptions.rules[evtType];if(!specifierObject[specifier]){specifierObject[specifier]=[];}
+if(opts.defer&&!specifierObject[specifier].deferrable){specifierObject[specifier].deferrable=[];}
+funcs.each(function(func){if(opts.defer){specifierObject[specifier].deferrable.push(func);}else{specifierObject[specifier].push(func);}
+subscriptionArray.push({'specifierType':'rules','eventType':evtType,'func':func,'specifier':specifier});});});return subscriptionArray;};var isId=function(specifierString){return/^\#[\w-]+$/.test(specifierString);};var isClass=function(specifierString){return/^\.[\w-]+$/.test(specifierString);};var isObject=function(specifier){return typeof specifier=="object";};var getSubscriptionMarker=function(obj){if(!obj.__MotionboxEventHandlerMaker){obj.__MotionboxEventHandlerMaker=subscriptionMarker++;}
+return obj.__MotionboxEventHandlerMaker;};var browserLikeEventExtender={preventDefault:function(){},stopPropagation:function(){},pageX:0,pageY:0,clientX:0,clientY:0};var CustomEvent=function(theTarget,evt,opts){this.type=evt;this.target=theTarget;this.srcElement=theTarget;this.eventName=evt;this.memo={};Object.extend(this,opts);for(prop in browserLikeEventExtender){if(browserLikeEventExtender.hasOwnProperty(prop)){if(!this[prop]){this[prop]=browserLikeEventExtender[prop];}}}
+if(Prototype.Browser.IE){Event.extend(this);}};if(Prototype.Browser.IE){var destroyObservers=function(){stdEvents.each(function(evtType){document.stopObserving(evtType,handleEvent);});};window.attachEvent('onbeforeunload',destroyObservers);}else{(function(){var methods=Object.keys(Event.Methods).inject({},function(m,name){m[name]=Event.Methods[name].methodize();return m;});CustomEvent.prototype=CustomEvent.prototype||document.createEvent("HTMLEvents").__proto__;Object.extend(CustomEvent.prototype,methods);})();}
+return{subscribe:function(specifiers,evtTypes,funcs,opts){if(!Object.isArray(specifiers)){specifiers=[specifiers];}
+if(!Object.isArray(evtTypes)){evtTypes=[evtTypes];}
+if(!Object.isArray(funcs)){funcs=[funcs];}
+opts=opts||{};var referenceArray=[];specifiers.each(function(specifier){var specifierType;if(isObject(specifier)){specifierType="objects";specifier=getSubscriptionMarker(specifier);}else{if(typeof specifier!="string"){throw new Error("specifier was neither an object nor a string");}
+if(isId(specifier)){specifier=specifier.sub(/#/,"");specifierType="ids";}
+if(isClass(specifier)){specifierType="classes";specifier=specifier.sub(/\./,"");}}
+if(specifierType){referenceArray=referenceArray.concat(createIdClassorObjectSubscription(specifierType,specifier,evtTypes,funcs,opts));}else{referenceArray=referenceArray.concat(createRulesSubscription(specifier,evtTypes,funcs,opts));}});return referenceArray;},unsubscribe:function(handlerObjects){var locator;handlerObjects.each(function(handlerObject){if(!(handlerObject.specifierType&&handlerObject.eventType&&handlerObject.specifier)||typeof handlerObject.func!='function'){throw new Error('bad unsubscribe object passed to EventHandler.unsubscribe');}
+if(handlerObject.specifierType!="rules"){locator=subscriptions[handlerObject.specifierType][handlerObject.specifier][handlerObject.eventType];for(var i=0,funcLen=locator.length;i<funcLen;++i){if(locator[i]==handlerObject.func){subscriptions[handlerObject.specifierType][handlerObject.specifier][handlerObject.eventType].splice(i,1);}}
+locator=subscriptions[handlerObject.specifierType][handlerObject.specifier][handlerObject.eventType].deferrable;if(locator){for(var i=0,funcLen=locator.length;i<funcLen;++i){if(locator[i]==handlerObject.func){subscriptions[handlerObject.specifierType][handlerObject.specifier][handlerObject.eventType].deferrable.splice(i,1);}}}}else{locator=subscriptions[handlerObject.specifierType][handlerObject.eventType][handlerObject.specifier];for(var i=0,funcLen=locator.length;i<funcLen;++i){if(locator[i]==handlerObject.func){subscriptions[handlerObject.specifierType][handlerObject.eventType][handlerObject.specifier].splice(i,1);}}
+locator=subscriptions[handlerObject.specifierType][handlerObject.eventType][handlerObject.specifier].deferrable;if(locator){for(var i=0,funcLen=locator.length;i<funcLen;++i){if(locator[i]==handlerObject.func){subscriptions[handlerObject.specifierType][handlerObject.eventType][handlerObject.specifier].deferrable.splice(i,1);}}}}});return true;},fireCustom:function(theTarget,evt,opts){if(theTarget){opts=opts||{};if(Object.isElement(theTarget)){var theEvent=new CustomEvent(theTarget,evt,opts);Event.extend(theEvent);handleEvent(theEvent);}else{callFunctionsFromIdOrObject("objects",getSubscriptionMarker(theTarget),evt,opts);}}},onDomReady:function(funcs,opts){opts=opts||{};if(typeof opts.defer=='undefined'){opts.defer=true;}
+if(!Object.isArray(funcs)){funcs=[funcs];}
+if(domReadyAlreadyFired){if(opts.defer){deferFunctions(funcs,"dom:loaded");}else{callFunctions(funcs,"dom:loaded");}}else{var subHandler=MBX.EventHandler.subscribe(document,"dom:loaded",funcs,{defer:opts.defer});return subHandler;}},dirSubscriptions:function(){console.dir(subscriptions);},dirEventListeners:function(){console.dir(eventListeners);},debugSubscriptions:function(){return subscriptions;}};})();
Please sign in to comment.
Something went wrong with that request. Please try again.