Skip to content

Instantly share code, notes, and snippets.

@pmros pmros/index.js
Created Mar 25, 2016

Embed
What would you like to do?
requirebin sketch
var event = require('value-event/event')
var listener = function (data) {
console.log('data', data)
}
var elem = document.getElementById('foo')
elem.querySelector('div.name')
.addEventListener('click', event(listener, {
clicked: true
}))
elem.querySelector('input.name')
.addEventListener('keypress', event(listener, {
changed: true
}))
setTimeout(function(){require=function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s}({1:[function(require,module,exports){var Delegator=require("dom-delegator");module.exports=BaseEvent;function BaseEvent(lambda){return EventHandler;function EventHandler(fn,data,opts){var handler={fn:fn,data:data!==undefined?data:{},opts:opts||{},handleEvent:handleEvent};if(fn&&fn.type==="dom-delegator-handle"){return Delegator.transformHandle(fn,handleLambda.bind(handler))}return handler}function handleLambda(ev,broadcast){if(this.opts.startPropagation&&ev.startPropagation){ev.startPropagation()}return lambda.call(this,ev,broadcast)}function handleEvent(ev){var self=this;if(self.opts.startPropagation&&ev.startPropagation){ev.startPropagation()}lambda.call(self,ev,broadcast);function broadcast(value){if(typeof self.fn==="function"){self.fn(value)}else{self.fn.write(value)}}}}},{"dom-delegator":4}],2:[function(require,module,exports){var EvStore=require("ev-store");module.exports=addEvent;function addEvent(target,type,handler){var events=EvStore(target);var event=events[type];if(!event){events[type]=handler}else if(Array.isArray(event)){if(event.indexOf(handler)===-1){event.push(handler)}}else if(event!==handler){events[type]=[event,handler]}}},{"ev-store":6}],3:[function(require,module,exports){var globalDocument=require("global/document");var EvStore=require("ev-store");var createStore=require("weakmap-shim/create-store");var addEvent=require("./add-event.js");var removeEvent=require("./remove-event.js");var ProxyEvent=require("./proxy-event.js");var HANDLER_STORE=createStore();module.exports=DOMDelegator;function DOMDelegator(document){if(!(this instanceof DOMDelegator)){return new DOMDelegator(document)}document=document||globalDocument;this.target=document.documentElement;this.events={};this.rawEventListeners={};this.globalListeners={}}DOMDelegator.prototype.addEventListener=addEvent;DOMDelegator.prototype.removeEventListener=removeEvent;DOMDelegator.allocateHandle=function allocateHandle(func){var handle=new Handle;HANDLER_STORE(handle).func=func;return handle};DOMDelegator.transformHandle=function transformHandle(handle,broadcast){var func=HANDLER_STORE(handle).func;return this.allocateHandle(function(ev){broadcast(ev,func)})};DOMDelegator.prototype.addGlobalEventListener=function addGlobalEventListener(eventName,fn){var listeners=this.globalListeners[eventName]||[];if(listeners.indexOf(fn)===-1){listeners.push(fn)}this.globalListeners[eventName]=listeners};DOMDelegator.prototype.removeGlobalEventListener=function removeGlobalEventListener(eventName,fn){var listeners=this.globalListeners[eventName]||[];var index=listeners.indexOf(fn);if(index!==-1){listeners.splice(index,1)}};DOMDelegator.prototype.listenTo=function listenTo(eventName){if(!(eventName in this.events)){this.events[eventName]=0}this.events[eventName]++;if(this.events[eventName]!==1){return}var listener=this.rawEventListeners[eventName];if(!listener){listener=this.rawEventListeners[eventName]=createHandler(eventName,this)}this.target.addEventListener(eventName,listener,true)};DOMDelegator.prototype.unlistenTo=function unlistenTo(eventName){if(!(eventName in this.events)){this.events[eventName]=0}if(this.events[eventName]===0){throw new Error("already unlistened to event.")}this.events[eventName]--;if(this.events[eventName]!==0){return}var listener=this.rawEventListeners[eventName];if(!listener){throw new Error("dom-delegator#unlistenTo: cannot "+"unlisten to "+eventName)}this.target.removeEventListener(eventName,listener,true)};function createHandler(eventName,delegator){var globalListeners=delegator.globalListeners;var delegatorTarget=delegator.target;return handler;function handler(ev){var globalHandlers=globalListeners[eventName]||[];if(globalHandlers.length>0){var globalEvent=new ProxyEvent(ev);globalEvent.currentTarget=delegatorTarget;callListeners(globalHandlers,globalEvent)}findAndInvokeListeners(ev.target,ev,eventName)}}function findAndInvokeListeners(elem,ev,eventName){var listener=getListener(elem,eventName);if(listener&&listener.handlers.length>0){var listenerEvent=new ProxyEvent(ev);listenerEvent.currentTarget=listener.currentTarget;callListeners(listener.handlers,listenerEvent);if(listenerEvent._bubbles){var nextTarget=listener.currentTarget.parentNode;findAndInvokeListeners(nextTarget,ev,eventName)}}}function getListener(target,type){if(target===null||typeof target==="undefined"){return null}var events=EvStore(target);var handler=events[type];var allHandler=events.event;if(!handler&&!allHandler){return getListener(target.parentNode,type)}var handlers=[].concat(handler||[],allHandler||[]);return new Listener(target,handlers)}function callListeners(handlers,ev){handlers.forEach(function(handler){if(typeof handler==="function"){handler(ev)}else if(typeof handler.handleEvent==="function"){handler.handleEvent(ev)}else if(handler.type==="dom-delegator-handle"){HANDLER_STORE(handler).func(ev)}else{throw new Error("dom-delegator: unknown handler "+"found: "+JSON.stringify(handlers))}})}function Listener(target,handlers){this.currentTarget=target;this.handlers=handlers}function Handle(){this.type="dom-delegator-handle"}},{"./add-event.js":2,"./proxy-event.js":13,"./remove-event.js":14,"ev-store":6,"global/document":15,"weakmap-shim/create-store":11}],4:[function(require,module,exports){var Individual=require("individual");var cuid=require("cuid");var globalDocument=require("global/document");var DOMDelegator=require("./dom-delegator.js");var versionKey="13";var cacheKey="__DOM_DELEGATOR_CACHE@"+versionKey;var cacheTokenKey="__DOM_DELEGATOR_CACHE_TOKEN@"+versionKey;var delegatorCache=Individual(cacheKey,{delegators:{}});var commonEvents=["blur","change","click","contextmenu","dblclick","error","focus","focusin","focusout","input","keydown","keypress","keyup","load","mousedown","mouseup","resize","select","submit","touchcancel","touchend","touchstart","unload"];module.exports=Delegator;function Delegator(opts){opts=opts||{};var document=opts.document||globalDocument;var cacheKey=document[cacheTokenKey];if(!cacheKey){cacheKey=document[cacheTokenKey]=cuid()}var delegator=delegatorCache.delegators[cacheKey];if(!delegator){delegator=delegatorCache.delegators[cacheKey]=new DOMDelegator(document)}if(opts.defaultEvents!==false){for(var i=0;i<commonEvents.length;i++){delegator.listenTo(commonEvents[i])}}return delegator}Delegator.allocateHandle=DOMDelegator.allocateHandle;Delegator.transformHandle=DOMDelegator.transformHandle},{"./dom-delegator.js":3,cuid:5,"global/document":15,individual:9}],5:[function(require,module,exports){(function(app){"use strict";var namespace="cuid",c=0,blockSize=4,base=36,discreteValues=Math.pow(base,blockSize),pad=function pad(num,size){var s="000000000"+num;return s.substr(s.length-size)},randomBlock=function randomBlock(){return pad((Math.random()*discreteValues<<0).toString(base),blockSize)},safeCounter=function(){c=c<discreteValues?c:0;c++;return c-1},api=function cuid(){var letter="c",timestamp=(new Date).getTime().toString(base),counter,fingerprint=api.fingerprint(),random=randomBlock()+randomBlock();counter=pad(safeCounter().toString(base),blockSize);return letter+timestamp+counter+fingerprint+random};api.slug=function slug(){var date=(new Date).getTime().toString(36),counter,print=api.fingerprint().slice(0,1)+api.fingerprint().slice(-1),random=randomBlock().slice(-2);counter=safeCounter().toString(36).slice(-4);return date.slice(-2)+counter+print+random};api.globalCount=function globalCount(){var cache=function calc(){var i,count=0;for(i in window){count++}return count}();api.globalCount=function(){return cache};return cache};api.fingerprint=function browserPrint(){return pad((navigator.mimeTypes.length+navigator.userAgent.length).toString(36)+api.globalCount().toString(36),4)};if(app.register){app.register(namespace,api)}else if(typeof module!=="undefined"){module.exports=api}else{app[namespace]=api}})(this.applitude||this)},{}],6:[function(require,module,exports){"use strict";var OneVersionConstraint=require("individual/one-version");var MY_VERSION="7";OneVersionConstraint("ev-store",MY_VERSION);var hashKey="__EV_STORE_KEY@"+MY_VERSION;module.exports=EvStore;function EvStore(elem){var hash=elem[hashKey];if(!hash){hash=elem[hashKey]={}}return hash}},{"individual/one-version":8}],7:[function(require,module,exports){(function(global){"use strict";var root=typeof window!=="undefined"?window:typeof global!=="undefined"?global:{};module.exports=Individual;function Individual(key,value){if(key in root){return root[key]}root[key]=value;return value}}).call(this,typeof global!=="undefined"?global:typeof self!=="undefined"?self:typeof window!=="undefined"?window:{})},{}],8:[function(require,module,exports){"use strict";var Individual=require("./index.js");module.exports=OneVersion;function OneVersion(moduleName,version,defaultValue){var key="__INDIVIDUAL_ONE_VERSION_"+moduleName;var enforceKey=key+"_ENFORCE_SINGLETON";var versionValue=Individual(enforceKey,version);if(versionValue!==version){throw new Error("Can only have one copy of "+moduleName+".\n"+"You already have version "+versionValue+" installed.\n"+"This means you cannot install version "+version)}return Individual(key,defaultValue)}},{"./index.js":7}],9:[function(require,module,exports){(function(global){var root=typeof window!=="undefined"?window:typeof global!=="undefined"?global:{};module.exports=Individual;function Individual(key,value){if(root[key]){return root[key]}Object.defineProperty(root,key,{value:value,configurable:true});return value}}).call(this,typeof global!=="undefined"?global:typeof self!=="undefined"?self:typeof window!=="undefined"?window:{})},{}],10:[function(require,module,exports){if(typeof Object.create==="function"){module.exports=function inherits(ctor,superCtor){ctor.super_=superCtor;ctor.prototype=Object.create(superCtor.prototype,{constructor:{value:ctor,enumerable:false,writable:true,configurable:true}})}}else{module.exports=function inherits(ctor,superCtor){ctor.super_=superCtor;var TempCtor=function(){};TempCtor.prototype=superCtor.prototype;ctor.prototype=new TempCtor;ctor.prototype.constructor=ctor}}},{}],11:[function(require,module,exports){var hiddenStore=require("./hidden-store.js");module.exports=createStore;function createStore(){var key={};return function(obj){if((typeof obj!=="object"||obj===null)&&typeof obj!=="function"){throw new Error("Weakmap-shim: Key must be object")}var store=obj.valueOf(key);return store&&store.identity===key?store:hiddenStore(obj,key)}}},{"./hidden-store.js":12}],12:[function(require,module,exports){module.exports=hiddenStore;function hiddenStore(obj,key){var store={identity:key};var valueOf=obj.valueOf;Object.defineProperty(obj,"valueOf",{value:function(value){return value!==key?valueOf.apply(this,arguments):store},writable:true});return store}},{}],13:[function(require,module,exports){var inherits=require("inherits");var ALL_PROPS=["altKey","bubbles","cancelable","ctrlKey","eventPhase","metaKey","relatedTarget","shiftKey","target","timeStamp","type","view","which"];var KEY_PROPS=["char","charCode","key","keyCode"];var MOUSE_PROPS=["button","buttons","clientX","clientY","layerX","layerY","offsetX","offsetY","pageX","pageY","screenX","screenY","toElement"];var rkeyEvent=/^key|input/;var rmouseEvent=/^(?:mouse|pointer|contextmenu)|click/;module.exports=ProxyEvent;function ProxyEvent(ev){if(!(this instanceof ProxyEvent)){return new ProxyEvent(ev)}if(rkeyEvent.test(ev.type)){return new KeyEvent(ev)}else if(rmouseEvent.test(ev.type)){return new MouseEvent(ev)}for(var i=0;i<ALL_PROPS.length;i++){var propKey=ALL_PROPS[i];this[propKey]=ev[propKey]}this._rawEvent=ev;this._bubbles=false}ProxyEvent.prototype.preventDefault=function(){this._rawEvent.preventDefault()};ProxyEvent.prototype.startPropagation=function(){this._bubbles=true};function MouseEvent(ev){for(var i=0;i<ALL_PROPS.length;i++){var propKey=ALL_PROPS[i];this[propKey]=ev[propKey]}for(var j=0;j<MOUSE_PROPS.length;j++){var mousePropKey=MOUSE_PROPS[j];this[mousePropKey]=ev[mousePropKey]}this._rawEvent=ev}inherits(MouseEvent,ProxyEvent);function KeyEvent(ev){for(var i=0;i<ALL_PROPS.length;i++){var propKey=ALL_PROPS[i];this[propKey]=ev[propKey]}for(var j=0;j<KEY_PROPS.length;j++){var keyPropKey=KEY_PROPS[j];this[keyPropKey]=ev[keyPropKey]}this._rawEvent=ev}inherits(KeyEvent,ProxyEvent)},{inherits:10}],14:[function(require,module,exports){var EvStore=require("ev-store");module.exports=removeEvent;function removeEvent(target,type,handler){var events=EvStore(target);var event=events[type];if(!event){return}else if(Array.isArray(event)){var index=event.indexOf(handler);if(index!==-1){event.splice(index,1)}}else if(event===handler){events[type]=null}}},{"ev-store":6}],15:[function(require,module,exports){(function(global){var topLevel=typeof global!=="undefined"?global:typeof window!=="undefined"?window:{};var minDoc=require("min-document");if(typeof document!=="undefined"){module.exports=document}else{var doccy=topLevel["__GLOBAL_DOCUMENT_CACHE@4"];if(!doccy){doccy=topLevel["__GLOBAL_DOCUMENT_CACHE@4"]=minDoc}module.exports=doccy}}).call(this,typeof global!=="undefined"?global:typeof self!=="undefined"?self:typeof window!=="undefined"?window:{})},{"min-document":16}],16:[function(require,module,exports){},{}],"value-event/event":[function(require,module,exports){var BaseEvent=require("./base-event.js");module.exports=BaseEvent(eventLambda);function eventLambda(ev,broadcast){broadcast(this.data)}},{"./base-event.js":1}]},{},[]);var event=require("value-event/event");var listener=function(data){console.log("data",data)};var elem=document.getElementById("foo");elem.querySelector("div.name").addEventListener("click",event(listener,{clicked:true}));elem.querySelector("input.name").addEventListener("keypress",event(listener,{changed:true}))},0);
{
"name": "requirebin-sketch",
"version": "1.0.0",
"dependencies": {
"value-event": "5.1.0"
}
}
<div id='foo'>
<div class='name'>Bob Steve</div>
<input class='name' value='Bob Steve'></input>
</div>
<!-- contents of this file will be placed inside the <head> -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.