auto-events
Version:
Small Vanilla JS utility for binding events between DOMNode and a simple controller.
1 lines • 8.06 kB
JavaScript
;var classCallCheck=function e(r,n){if(!(r instanceof n)){throw new TypeError("Cannot call a class as a function")}};var createClass=function(){function e(e,r){for(var n=0;n<r.length;n++){var t=r[n];t.enumerable=t.enumerable||false;t.configurable=true;if("value"in t)t.writable=true;Object.defineProperty(e,t.key,t)}}return function(r,n,t){if(n)e(r.prototype,n);if(t)e(r,t);return r}}();var Parser=function(){function e(r,n){classCallCheck(this,e);this.rules=r;this.defaultRule=n}createClass(e,[{key:"getRule",value:function e(r){var n=this.rules[r];if(!n)throw new Error("elenpi parser : rules not found : "+r);return n}},{key:"parse",value:function r(n){var t=arguments.length>1&&arguments[1]!==undefined?arguments[1]:null;var i=arguments.length>2&&arguments[2]!==undefined?arguments[2]:{};var u=arguments.length>3&&arguments[3]!==undefined?arguments[3]:{};u.parser=this;u.string=n;u.lastPosition=u.lastPosition||0;t=t||this.getRule(this.defaultRule);e.exec(t,i,u);if(!u.error&&u.string.length){u.error=true;u.errorMessage="string wasn't parsed entierly"}if(u.error){var s=n.length-u.string.length,o=getPositionInFile(n,s);throw new Error("Parsing failed : "+(u.errorMessage||"no rules matched")+" : (line:"+o.line+" , col:"+o.col+") near : "+n.substring(Math.max(0,s-10),s+50))}return i}}],[{key:"exec",value:function e(r,n,t){if(typeof r==="string")r=t.parser.getRule(r);var i=r._queue;for(var u=0,s=i.length;u<s;++u){i[u](t,n,t.lastPosition);if(t.error)break}}}]);return e}();function getPositionInFile(e,r){var n=e.split(/\r|\n/),t=n.length;var i=0,u=0,s=void 0,o=void 0;while(i<t){s=n[i];o=s.length;if(r<=u+o)break;u+=o;i++}return{line:i+1,col:r-u}}var defaultSpaceRegExp=/^\s+/;var exec=Parser.exec;var Rule=function(){function e(){classCallCheck(this,e);this._queue=[];this.__elenpi__=true}createClass(e,[{key:"done",value:function e(r){this._queue.push(r);return this}},{key:"use",value:function e(r){return this.done(function(e,n){if(typeof r==="string")r=e.parser.getRule(r);exec(r,n,e)})}},{key:"terminal",value:function e(r,n){return this.done(function(e,t,i){if(!e.string.length){e.error=true;return}var u=r.exec(e.string);if(u){e.string=e.string.substring(u[0].length);e.lastPosition+=u[0].length;if(n){if(typeof n==="string")t[n]=u[0];else n(e,t,u,i,e.lastPosition)}}else e.error=true})}},{key:"char",value:function e(r){return this.done(function(e){if(!e.string.length||e.string[0]!==r)e.error=true;else{e.string=e.string.substring(1);e.lastPosition+=1}})}},{key:"xOrMore",value:function e(r){var n=typeof r==="string"||r.__elenpi__?{rule:r}:r;n.minimum=n.minimum||0;n.maximum=n.maximum||Infinity;return this.done(function(e,r){if(n.minimum&&!e.string.length){e.error=true;return}var t=n.rule,i=n.pushTo,u=typeof i==="string",s=n.as,o=n.separator;var a=0,l=void 0,f=void 0,c=void 0;while(e.string.length&&a<n.maximum){f=s?s(e,r):i?{}:r;l=e.string.length;exec(t,f,e);c=e.string.length;if(e.error){if(l===c)e.error=false;break}a++;if(!f.skip&&i)if(u){r[i]=r[i]||[];r[i].push(f)}else i(e,r,f);if(o&&c){l=c;exec(o,f,e);if(e.error){if(l===e.string.length)e.error=false;break}}}if(!e.error&&a<n.minimum){e.error=true;e.errorMessage="missing xOrMore item : "+t}})}},{key:"zeroOrMore",value:function e(r){return this.xOrMore(r)}},{key:"oneOrMore",value:function e(r){if(typeof r==="string"||r.__elenpi__)r={rule:r,minimum:1};else r.minimum=1;return this.xOrMore(r)}},{key:"oneOf",value:function e(r){var n=typeof r==="string"||r.__elenpi__?{rules:[].slice.call(arguments)}:r;return this.done(function(e,r){if(!n.optional&&!e.string.length){e.error=true;return}var t=n.rules.length,i=e.string.length;var u=0,s=void 0,o=void 0;while(u<t){s=n.rules[u++];o=n.as?n.as(e,r):n.set?{}:r;exec(s,o,e);if(e.error){if(e.string.length===i){e.error=false;continue}}else setDescriptor(r,o,n.set,e);return}if(!n.optional)e.error=true})}},{key:"maybeOneOf",value:function e(r){var n=typeof r==="string"||r.__elenpi__?{rules:[].slice.call(arguments)}:r;n.optional=true;return this.oneOf(n)}},{key:"one",value:function e(r){var n=typeof r==="string"||r&&r.__elenpi__?{rule:r}:r;return this.done(function(e,r){if(!n.optional&&!e.string.length){e.error=true;return}var t=n.as?n.as(e,r):n.set?{}:r,i=e.string.length;exec(n.rule,t,e);if(!e.error)setDescriptor(r,t,n.set,e);else if(n.optional&&e.string.length===i)e.error=false})}},{key:"maybeOne",value:function e(r){var n=typeof r==="string"||r&&r.__elenpi__?{rule:r}:r;n.optional=true;return this.one(n)}},{key:"skip",value:function e(){return this.done(function(e,r){r.skip=true})}},{key:"space",value:function e(){var r=arguments.length>0&&arguments[0]!==undefined?arguments[0]:false;return this.done(function(e){if(!e.string.length){if(r)e.error=true;return}var n=(e.parser.rules.space||defaultSpaceRegExp).exec(e.string);if(n)e.string=e.string.substring(n[0].length);else if(r)e.error=true})}},{key:"end",value:function e(){return this.done(function(e){if(e.string.length)e.error=true})}},{key:"error",value:function e(r){return this.done(function(e){e.error=true;e.errorMessage=r})}}]);return e}();function setDescriptor(e,r,n,t){if(!r.skip&&n)if(typeof n==="string")e[n]=r;else n(t,e,r)}var r$1={};Object.getOwnPropertyNames(Rule.prototype).forEach(function(e){if(typeof Rule.prototype[e]==="function")r$1[e]=function(){var r=new Rule;return r[e].apply(r,arguments)}});Rule.initializer=r$1;var elenpi$1={Rule:Rule,Parser:Parser};var dist=Object.freeze({default:elenpi$1});var require$$0=dist&&elenpi$1||dist;var elenpi=require$$0;elenpi=elenpi.default||elenpi;var r=elenpi.Rule.initializer;var rules={doublestring:r.terminal(/^"((?:[^"\\]|\\.)*)"/,function(e,r,n){return r.arguments.push(n[1])}),singlestring:r.terminal(/^'((?:[^'\\]|\\.)*)'/,function(e,r,n){return r.arguments.push(n[1])}),templatestring:r.terminal(/^`([^`]*)`/,function(e,r,n){return r.arguments.push(n[1])}),float:r.terminal(/^[0-9]*\.[0-9]+/,function(e,r,n){return r.arguments.push(parseFloat(n[0],10))}),integer:r.terminal(/^[0-9]+/,function(e,r,n){return r.arguments.push(parseInt(n[0],10))}),bool:r.terminal(/^(true|false)/,function(e,r,n){return r.arguments.push(n[1]==="true"?true:false)}),null:r.terminal(/^null/,function(e,r){return r.arguments.push(null)}),undefined:r.terminal(/^undefined/,function(e,r){return r.arguments.push(undefined)}),NaN:r.terminal(/^NaN/,function(e,r){return r.arguments.push(NaN)}),Infinity:r.terminal(/^Infinity/,function(e,r){return r.arguments.push(Infinity)}),call:r.terminal(/^\s*([\w-_]+)\s*/,function(e,r,n){r.method=n[1];r.arguments=[]}).maybeOne(r.terminal(/^\(\s*/).zeroOrMore({rule:r.oneOf("singlestring","doublestring","templatestring","null","undefined","NaN","Infinity","float","integer","bool"),separator:r.terminal(/^\s*,\s*/)}).terminal(/^\s*\)\s*/))};var src=new elenpi.Parser(rules,"call");var toConsumableArray=function(e){if(Array.isArray(e)){for(var r=0,n=Array(e.length);r<e.length;r++)n[r]=e[r];return n}else{return Array.from(e)}};var debug=require("debug");var eventAttributeMatcher=/^data-ev-(\w+)/i;var autoEvents={devMode:false};function isAllowedMethod(e,r){if(!r[e])throw new Error("method not found on component : ",e);return true}function parseCallAndCheck(e,r,n){r=src.parse(e);isAllowedMethod(r.method,n);return r}function checkAttribute(e,r,n){var t=eventAttributeMatcher.exec(n.name);if(t){var i=t[1];var u=void 0;debug("bind event (%s) with : %s : on : ",i,n.value,r);if(!u&&autoEvents.devMode)u=parseCallAndCheck(n.value,u,r);e.addEventListener(i,function(e){if(!u&&!autoEvents.devMode)u=parseCallAndCheck(n.value,u,r);debug("binded event : %s ! : ",i,u.method,u.arguments);r[u.method].apply(r,[e].concat(toConsumableArray(u.arguments)))})}}function bindEvents(e,r){if(typeof e==="string")e=document.querySelector(e);[].slice.call(e.attributes).forEach(function(n){return checkAttribute(e,r,n)})}autoEvents.bindEvents=bindEvents;autoEvents.install=function(){var e=arguments.length>0&&arguments[0]!==undefined?arguments[0]:window.$;e.fn.autoEvents=function e(r){this.get().forEach(function(e){return bindEvents(e,r)});return this}};module.exports=autoEvents;