UNPKG

intercooler

Version:

Making AJAX as easy as anchor tags

2 lines 13.6 kB
/*! intercooler 0.0.1-prealpha-1 2014-02-17 */ "use strict";var Intercooler=Intercooler||function(){function fp(a){return CryptoJS.SHA1(a.html()).toString()}function levelPrefix(a){return a==_DEBUG?"IC DEBUG: ":a==_INFO?"IC INFO: ":a==_WARN?"IC WARN: ":a==_ERROR?"IC ERROR: ":"IC UNKNOWN: "}function log(a,b){var c=b||_INFO,d=_loggingLevel||_ERROR;_logger&&c>=d&&(null==_loggingGrep||_loggingGrep.test(a))&&_logger.log(levelPrefix(c),a)}function uuid(){var a=(new Date).getTime(),b="xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,function(b){var c=(a+16*Math.random())%16|0;return a=Math.floor(a/16),("x"==b?c:7&c|8).toString(16)});return b}function icSelectorFor(a){return"[ic-id='"+a.attr("ic-id")+"']"}function parseInterval(a){return log("POLL: Parsing interval string "+a,_DEBUG),"null"==a||"false"==a||""==a?null:a.lastIndexOf("ms")==a.length-2?parseInt(a.substr(0,a.length-2)):a.lastIndexOf("s")==a.length-1?1e3*parseInt(a.substr(0,a.length-1)):1e3}function getTarget(a){return a.attr("ic-target")?$(a.attr("ic-target")):a}function processHeaders(elt,xhr){if(xhr.getResponseHeader("X-ic-refresh")){var pathsToRefresh=xhr.getResponseHeader("X-ic-refresh").split(",");log("IC HEADER: refreshing "+pathsToRefresh,_DEBUG),$.each(pathsToRefresh,function(a,b){refreshDependencies(b.replace(/ /g,""),elt)})}else if(xhr.getResponseHeader("X-ic-script"))log("IC HEADER: evaling "+xhr.getResponseHeader("X-ic-script"),_DEBUG),eval(xhr.getResponseHeader("X-ic-script"));else if(xhr.getResponseHeader("X-ic-redirect"))log("IC HEADER: redirecting to "+xhr.getResponseHeader("X-ic-redirect"),_DEBUG),window.location=xhr.getResponseHeader("X-ic-redirect");else if(xhr.getResponseHeader("X-ic-open"))log("IC HEADER: opening "+xhr.getResponseHeader("X-ic-open"),_DEBUG),window.open(xhr.getResponseHeader("X-ic-open"));else if(xhr.getResponseHeader("X-ic-remove")&&(log("IC HEADER REMOVE COMMAND"),elt)){var target=getTarget(elt);log("IC REMOVING: "+target.html(),_DEBUG),"none"==target.attr("ic-transition")?target.remove():target.fadeOut("fast",function(){target.remove()})}return!0}function handleTestResponse(a,b,c){var d={};c&&c.headers&&(d=c.headers);var e="";c&&("string"==typeof c||c instanceof String?e=c:("string"==typeof c.body||c.body instanceof String)&&(e=c.body)),processHeaders(a,{getResponseHeader:function(a){return d[a]}}),b(e,"",a)}function handleRemoteRequest(a,b,c,d,e){"PUT"==b&&(d+="&_method=PUT"),"DELETE"==b&&(d+="&_method=DELETE");for(var f=0,g=_urlHandlers.length;g>f;f++){var h=_urlHandlers[f],i=null;if(null==h.url||new RegExp(h.url.replace(/\*/g,".*").replace(/\//g,"\\/")).test(c))return"GET"==b&&h.get&&(h.get&&(i=h.get(c,parseParams(d))),handleTestResponse(a,e,i)),"POST"==b&&(h.post&&(i=h.post(c,parseParams(d))),handleTestResponse(a,e,i)),"PUT"==b&&(h.put&&(i=h.put(c,parseParams(d))),handleTestResponse(a,e,i)),"DELETE"==b&&(h.delete&&(i=h.delete(c,parseParams(d))),handleTestResponse(a,e,i)),void 0}_remote.ajax({type:b,url:c,data:d,dataType:"text",success:function(b,c,d){processHeaders(a,d)&&e(b,c,a,d)},error:function(a,b,c){log("An error occurred: "+c,_ERROR)}})}function parseParams(a){var b,c=/([^&=]+)=?([^&]*)/g,d=function(a){return decodeURIComponent(a.replace(/\+/g," "))},e={};if(a)for("?"==a.substr(0,1)&&(a=a.substr(1));b=c.exec(a);){var f=d(b[1]),g=d(b[2]);void 0!==e[f]?($.isArray(e[f])||(e[f]=[e[f]]),e[f].push(g)):e[f]=g}return e}function processInclude(str){if(0==str.indexOf("$"))return eval(str).serialize();if(str.indexOf(":")){var name=str.split(":")[0],val=str.split(":")[1],result=eval(val);if(result)return encodeURIComponent(name)+"="+encodeURIComponent(result.toString())}return""}function processIncludes(a){for(var b="",c=a.split(","),d=0,e=c.length;e>d;d++)b+="&"+processInclude(c[d]);return b}function getParametersForElement(a){var b=getTarget(a),c="ic-request=true";return c+=a.closest("form").length>0?"&"+a.closest("form").serialize():"&"+a.serialize(),b.attr("id")&&(c+="&ic-element-id="+b.attr("id")),b.attr("name")&&(c+="&ic-element-name="+b.attr("name")),b.attr("ic-id")&&(c+="&ic-id="+b.attr("ic-id")),b.attr("ic-id")&&(c+="&ic-id="+b.attr("ic-id")),b.attr("ic-last-refresh")&&(c+="&ic-last-refresh="+b.attr("ic-last-refresh")),b.attr("ic-fingerprint")&&(c+="&ic-fingerprint="+b.attr("ic-fingerprint")),a.attr("ic-include")&&(c+=processIncludes(a.attr("ic-include"))),log("PARAMS: Returning parameters "+c+" for "+a),c}function maybeSetIntercoolerInfo(a){var b=getTarget(a);if(!b.data("ic-id")){var c=fp(a),d=uuid(),e=(new Date).getTime();b.attr("ic-id",d),b.attr("ic-last-refresh",e),b.attr("ic-fingerprint",c)}}function withAttrs(a,b){$.each(a,function(a,c){b(c)})}function processSources(a){withAttrs(_SRC_ATTRS,function(b){$(a).is("["+b+"]")&&maybeSetIntercoolerInfo($(a)),$(a).find("["+b+"]").each(function(){maybeSetIntercoolerInfo($(this))})})}function startPolling(a){var b=parseInterval(a.attr("ic-poll"));if(null!=b){var c=icSelectorFor(a);log("POLL: Starting poll for element "+c,_DEBUG);var d=setInterval(function(){var a=$(c);0==a.length?(log("POLL: Clearing poll for element "+c,_DEBUG),clearTimeout(d)):updateElement(a)},b)}}function processPolling(a){$(a).is("[ic-poll]")&&(maybeSetIntercoolerInfo($(a)),startPolling(a)),$(a).find("[ic-poll]").each(function(){maybeSetIntercoolerInfo($(this)),startPolling($(this))})}function isDependent(a,b){return a&&b&&(0==b.indexOf(a)||0==a.indexOf(b))}function refreshDependencies(a,b){withAttrs(_SRC_ATTRS,function(c){$("["+c+"]").each(function(){isDependent(a,$(this).attr(c))?(null==b||$(b)[0]!=$(this)[0])&&updateElement($(this)):(isDependent(a,$(this).attr("ic-deps"))||"*"==$(this).attr("ic-deps"))&&(null==b||$(b)[0]!=$(this)[0])&&updateElement($(this))})})}function verbFor(a){return"ic-post-to"==a?"POST":"ic-put-to"==a?"PUT":"ic-delete-from"==a?"DELETE":"POST"}function initButtonDestination(a,b){var c=$(a).attr(b);$(a).click(function(d){d.preventDefault(),handleRemoteRequest(a,verbFor(b),c,getParametersForElement(a),function(b){processICResponse(b,a),refreshDependencies(c)})})}function initInputDestination(a,b){var c=$(a).attr(b);$(a).change(function(){handleRemoteRequest(a,verbFor(b),c,getParametersForElement(a),function(b){processICResponse(b,a),refreshDependencies(c)})})}function initDestination(a,b){$(a).is("button, a, div, span")?initButtonDestination(a,b):$(a).is("input, select")&&initInputDestination(a,b)}function processDestinations(a){withAttrs(_DEST_ATTRS,function(b){$(a).is("["+b+"]")&&(maybeSetIntercoolerInfo($(a)),initDestination(a,b)),$(a).find("["+b+"]").each(function(){maybeSetIntercoolerInfo($(this)),initDestination($(this),b)})})}function processNodes(a){processSources(a),processPolling(a),processDestinations(a)}function processICResponse(a,b){if(a&&""!=a){log("IC RESPONSE: Received: "+a,_DEBUG);var c=$(a),d=getTarget(b);maybeSetIntercoolerInfo(c),c.attr("ic-fingerprint")!=d.attr("ic-fingerprint")?"none"==d.attr("ic-transition")?(d.replaceWith(c),processNodes(c),log("IC RESPONSE: Replacing "+d.html()+" with "+c.html(),_DEBUG)):d.fadeOut("fast",function(){c.hide(),d.replaceWith(c),log("IC RESPONSE: Replacing "+d.html()+" with "+c.html(),_DEBUG),processNodes(c),c.fadeIn("slow")}):c.remove()}}function updateElement(a){var b=a;if(b.attr("ic-src"))handleRemoteRequest(a,"GET",b.attr("ic-src"),getParametersForElement(b),function(a){processICResponse(a,b)});else if(b.attr("ic-text-src"))handleRemoteRequest(a,"GET",b.attr("ic-text-src"),getParametersForElement(b),function(a){a!=b.text()&&b.fadeOut("fast",function(){b.text(a),b.fadeIn("fast")})});else if(b.attr("ic-prepend-from"))handleRemoteRequest(a,"GET",b.attr("ic-prepend-from"),getParametersForElement(b),function(a){var c=$(a);if(c.is("tr")?c.children().hide():c.hide(),b.prepend(c),log("elt is "),log(b),c.is("tr")?c.children().slideDown():c.slideDown(),processNodes(c),b.attr("ic-limit-children")){var d=parseInt(b.attr("ic-limit-children"));b.children().length>d&&b.children().slice(d,b.children().length).remove()}});else if(b.attr("ic-append-from"))handleRemoteRequest(a,"GET",b.attr("ic-append-from"),getParametersForElement(b),function(a){var c=$(a);if(c.hide(),b.append(c),c.is("tr")?c.children().slideDown():c.slideDown(),processNodes(c),b.attr("ic-limit-children")){var d=parseInt(b.attr("ic-limit-children"));b.children().length>d&&b.children().slice(0,b.children().length-d).remove()}});else if(b.attr("ic-style-src")){var c=b.attr("ic-style-src").split(":");handleRemoteRequest(a,"GET",c[1],getParametersForElement(b),function(a){b.css(c[0],a)})}else if(b.attr("ic-attr-src")){var d=b.attr("ic-attr-src").split(":");handleRemoteRequest(a,"GET",d[1],getParametersForElement(b),function(a){b.attr(d[0],a)})}}var _DEBUG=1,_INFO=2,_WARN=3,_ERROR=4,_SRC_ATTRS=["ic-src","ic-style-src","ic-attr-src","ic-prepend-from","ic-append-from","ic-text-src"],_DEST_ATTRS=["ic-post-to","ic-put-to","ic-delete-from"],_remote=$,_urlHandlers=[],_logger=window.console,_loggingLevel=null,_loggingGrep=null,CryptoJS=CryptoJS||function(a,b){var c={},d=c.lib={},e=function(){},f=d.Base={extend:function(a){e.prototype=this;var b=new e;return a&&b.mixIn(a),b.hasOwnProperty("init")||(b.init=function(){b.$super.init.apply(this,arguments)}),b.init.prototype=b,b.$super=this,b},create:function(){var a=this.extend();return a.init.apply(a,arguments),a},init:function(){},mixIn:function(a){for(var b in a)a.hasOwnProperty(b)&&(this[b]=a[b]);a.hasOwnProperty("toString")&&(this.toString=a.toString)},clone:function(){return this.init.prototype.extend(this)}},g=d.WordArray=f.extend({init:function(a,c){a=this.words=a||[],this.sigBytes=c!=b?c:4*a.length},toString:function(a){return(a||i).stringify(this)},concat:function(a){var b=this.words,c=a.words,d=this.sigBytes;if(a=a.sigBytes,this.clamp(),d%4)for(var e=0;a>e;e++)b[d+e>>>2]|=(c[e>>>2]>>>24-8*(e%4)&255)<<24-8*((d+e)%4);else if(65535<c.length)for(e=0;a>e;e+=4)b[d+e>>>2]=c[e>>>2];else b.push.apply(b,c);return this.sigBytes+=a,this},clamp:function(){var b=this.words,c=this.sigBytes;b[c>>>2]&=4294967295<<32-8*(c%4),b.length=a.ceil(c/4)},clone:function(){var a=f.clone.call(this);return a.words=this.words.slice(0),a},random:function(b){for(var c=[],d=0;b>d;d+=4)c.push(4294967296*a.random()|0);return new g.init(c,b)}}),h=c.enc={},i=h.Hex={stringify:function(a){var b=a.words;a=a.sigBytes;for(var c=[],d=0;a>d;d++){var e=b[d>>>2]>>>24-8*(d%4)&255;c.push((e>>>4).toString(16)),c.push((15&e).toString(16))}return c.join("")},parse:function(a){for(var b=a.length,c=[],d=0;b>d;d+=2)c[d>>>3]|=parseInt(a.substr(d,2),16)<<24-4*(d%8);return new g.init(c,b/2)}},j=h.Latin1={stringify:function(a){var b=a.words;a=a.sigBytes;for(var c=[],d=0;a>d;d++)c.push(String.fromCharCode(b[d>>>2]>>>24-8*(d%4)&255));return c.join("")},parse:function(a){for(var b=a.length,c=[],d=0;b>d;d++)c[d>>>2]|=(255&a.charCodeAt(d))<<24-8*(d%4);return new g.init(c,b)}},k=h.Utf8={stringify:function(a){try{return decodeURIComponent(escape(j.stringify(a)))}catch(b){throw Error("Malformed UTF-8 data")}},parse:function(a){return j.parse(unescape(encodeURIComponent(a)))}},l=d.BufferedBlockAlgorithm=f.extend({reset:function(){this._data=new g.init,this._nDataBytes=0},_append:function(a){"string"==typeof a&&(a=k.parse(a)),this._data.concat(a),this._nDataBytes+=a.sigBytes},_process:function(b){var c=this._data,d=c.words,e=c.sigBytes,f=this.blockSize,h=e/(4*f),h=b?a.ceil(h):a.max((0|h)-this._minBufferSize,0);if(b=h*f,e=a.min(4*b,e),b){for(var i=0;b>i;i+=f)this._doProcessBlock(d,i);i=d.splice(0,b),c.sigBytes-=e}return new g.init(i,e)},clone:function(){var a=f.clone.call(this);return a._data=this._data.clone(),a},_minBufferSize:0});d.Hasher=l.extend({cfg:f.extend(),init:function(a){this.cfg=this.cfg.extend(a),this.reset()},reset:function(){l.reset.call(this),this._doReset()},update:function(a){return this._append(a),this._process(),this},finalize:function(a){return a&&this._append(a),this._doFinalize()},blockSize:16,_createHelper:function(a){return function(b,c){return new a.init(c).finalize(b)}},_createHmacHelper:function(a){return function(b,c){return new m.HMAC.init(a,c).finalize(b)}}});var m=c.algo={};return c}(Math);return function(){var a=CryptoJS,b=a.lib,c=b.WordArray,d=b.Hasher,e=[],b=a.algo.SHA1=d.extend({_doReset:function(){this._hash=new c.init([1732584193,4023233417,2562383102,271733878,3285377520])},_doProcessBlock:function(a,b){for(var c=this._hash.words,d=c[0],f=c[1],g=c[2],h=c[3],i=c[4],j=0;80>j;j++){if(16>j)e[j]=0|a[b+j];else{var k=e[j-3]^e[j-8]^e[j-14]^e[j-16];e[j]=k<<1|k>>>31}k=(d<<5|d>>>27)+i+e[j],k=20>j?k+((f&g|~f&h)+1518500249):40>j?k+((f^g^h)+1859775393):60>j?k+((f&g|f&h|g&h)-1894007588):k+((f^g^h)-899497514),i=h,h=g,g=f<<30|f>>>2,f=d,d=k}c[0]=c[0]+d|0,c[1]=c[1]+f|0,c[2]=c[2]+g|0,c[3]=c[3]+h|0,c[4]=c[4]+i|0},_doFinalize:function(){var a=this._data,b=a.words,c=8*this._nDataBytes,d=8*a.sigBytes;return b[d>>>5]|=128<<24-d%32,b[(d+64>>>9<<4)+14]=Math.floor(c/4294967296),b[(d+64>>>9<<4)+15]=c,a.sigBytes=4*b.length,this._process(),this._hash},clone:function(){var a=d.clone.call(this);return a._hash=this._hash.clone(),a}});a.SHA1=d._createHelper(b),a.HmacSHA1=d._createHmacHelper(b)}(),$(function(){processNodes("body")}),{refresh:function(a){return updateElement(a),Intercooler},addURLHandler:function(a){if(!a.url)throw"Handlers must include a URL pattern";return _urlHandlers.push(a),Intercooler},setRemote:function(a){return _remote=a,Intercooler},setLogger:function(a,b,c){return _logger=a,b&&(_loggingLevel=b),c&&(_loggingGrep=c),Intercooler},log:function(a,b){return log(a,b),Intercooler},setLogLevel:function(a){return _loggingLevel=a,Intercooler},logLevels:{DEBUG:_DEBUG,INFO:_INFO,WARNING:_WARN,ERROR:_ERROR}}}();