/* Portal v1.1.1 | http://flowersinthesand.github.io/portal/ | (c) 2011-2014, Donghwan Kim | http://www.apache.org/licenses/LICENSE-2.0 */ (function(l,w){"function"===typeof define&&define.amd?define(function(){return w(l)}):"object"===typeof exports?(module.exports=w(function(){var l=require("jsdom").jsdom().createWindow();l.WebSocket=require("ws");l.EventSource=require("eventsource");return l}()),module.exports.support.corsable=!0):l.portal=w(l)})(this,function(l){function w(a){var c,b,g,d,h,f,e=[],k=function(k,p){p=p||[];b=!a||[k,p];g=!0;f=d||0;d=0;for(h=e.length;f',q.body.appendChild(b));f=b.firstChild.contentWindow;return{init:function(){f.callbacks=[c];f.fire=function(a){var b;for(b=0;bb._heartbeat&&(a(),m.one("close",c));k.connecting.lock();for(h=f=r=null;s.length;)m.send.apply(m, s.shift())},close:function(){d="closed";var a,c,e=k.close.order;for(a in k)c=k[a],c.order';return encodeURI(decodeURI(c.firstChild.href))},each:function(a,c){var b;for(b=0;ba?"0"+a:a}var e=/[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,d={"\b":"\\b","\t":"\\t","\n":"\\n","\f":"\\f","\r":"\\r",'"':'\\"', "\\":"\\\\"};return l.JSON&&l.JSON.stringify?l.JSON.stringify(a):function f(a,e){var d,g,s,n=e[a];s=typeof n;n&&"object"===typeof n&&"function"===typeof n.toJSON&&(n=n.toJSON(a),s=typeof n);switch(s){case "string":return c(n);case "number":return isFinite(n)?String(n):"null";case "boolean":return String(n);case "object":if(!n)return"null";switch(B.call(n)){case "[object Date]":return isFinite(n.valueOf())?'"'+n.getUTCFullYear()+"-"+b(n.getUTCMonth()+1)+"-"+b(n.getUTCDate())+"T"+b(n.getUTCHours())+ ":"+b(n.getUTCMinutes())+":"+b(n.getUTCSeconds())+'Z"':"null";case "[object Array]":g=n.length;s=[];for(d=0;d';d.firstChild.value=c;e.on(d.lastChild,"load",function(){q.body.removeChild(d);b()});q.body.appendChild(d);d.submit()};return{send:function(a){h.push(a);d||(d=!0,b())}}}, sse:function(a,c){var b,g=l.EventSource;if(g)return e.extend(u.httpbase(a,c),{open:function(){var d=a.data("url");b=new g(d,{withCredentials:c.credentials});b.onopen=function(b){a.data("event",b).fire("open")};b.onmessage=function(b){a.data("event",b)._fire(b.data)};b.onerror=function(c){b.close();a.data("event",c).fire("close","done")}},close:function(){b.close()}})},stream:function(a){a.data("candidates").unshift("streamxhr","streamxdr","streamiframe")},streamxhr:function(a,c){var b;if(!(e.browser.msie&& 10>+e.browser.version.split(".")[0]||c.crossDomain&&!e.corsable))return e.extend(u.httpbase(a,c),{open:function(){b=e.xhr();b.onreadystatechange=function(){if(3===b.readyState&&200===b.status){var c=a.data("index"),d=b.responseText.length;c?d>c&&a._fire(b.responseText.substring(c,d),!0):a.fire("open")._fire(b.responseText,!0);a.data("index",d)}else 4===b.readyState&&a.fire("close",200===b.status?"done":"error")};b.open("GET",a.data("url"));e.corsable&&(b.withCredentials=c.credentials);b.send(null)}, close:function(){b.abort()}})},streamiframe:function(a,c){var b,g,d=l.ActiveXObject;if(d&&!c.crossDomain){try{new d("htmlfile")}catch(h){return}return e.extend(u.httpbase(a,c),{open:function(){function c(a){var b;(function n(){b=setTimeout(function(){!1!==a()&&n()},1)})();return function(){clearTimeout(b)}}var e,k;b=new d("htmlfile");b.open();b.close();e=b.createElement("iframe");e.src=a.data("url");b.body.appendChild(e);k=e.contentDocument||e.contentWindow.document;g=c(function(){function b(){var a; a=d.cloneNode(!0);a.appendChild(k.createTextNode("."));a=a.innerText;return a.substring(0,a.length-1)}var d;if(k.firstChild){d=k.body.lastChild;if(!d)return a.fire("close","error"),!1;a.fire("open")._fire(b(),!0);d.innerText="";g=c(function(){var c=b();c&&(d.innerText="",a._fire(c,!0));if("complete"===k.readyState)return a.fire("close","done"),!1});return!1}})},close:function(){g();b.execCommand("Stop")}})}},streamxdr:function(a,c){var b,g=l.XDomainRequest;if(g&&c.xdrURL&&c.xdrURL.call(a,"t"))return e.extend(u.httpbase(a, c),{open:function(){var d=c.xdrURL.call(a,a.data("url"));a.data("url",d);b=new g;b.onprogress=function(){var c=a.data("index"),d=b.responseText.length;c?a._fire(b.responseText.substring(c,d),!0):a.fire("open")._fire(b.responseText,!0);a.data("index",d)};b.onerror=function(){a.fire("close","error")};b.onload=function(){a.fire("close","done")};b.open("GET",d);b.send()},close:function(){b.abort()}})},longpoll:function(a){a.data("candidates").unshift("longpollajax","longpollxdr","longpolljsonp")},longpollajax:function(a, c){var b,g,d=0;if(!c.crossDomain||e.corsable)return e.extend(u.httpbase(a,c),{open:function(){function h(){var f=a.buildURL(d?"poll":"open",{count:++d});a.data("url",f);b=e.xhr();b.onreadystatechange=function(){var c;g||4!==b.readyState||(200===b.status?(c=b.responseText)||1===d?(1===d&&a.fire("open"),c&&a._fire(c),g||h()):a.fire("close","done"):a.fire("close","error"))};b.open("GET",f);e.corsable&&(b.withCredentials=c.credentials);b.send(null)}h()},close:function(){g=!0;b.abort()}})},longpollxdr:function(a, c){var b,g,d=0,h=l.XDomainRequest;if(h&&c.xdrURL&&c.xdrURL.call(a,"t"))return e.extend(u.httpbase(a,c),{open:function(){function e(){var l=c.xdrURL.call(a,a.buildURL(d?"poll":"open",{count:++d}));a.data("url",l);b=new h;b.onload=function(){var c=b.responseText;c||1===d?(1===d&&a.fire("open"),c&&a._fire(c),g||e()):a.fire("close","done")};b.onerror=function(){a.fire("close","error")};b.open("GET",l);b.send()}e()},close:function(){g=!0;b.abort()}})},longpolljsonp:function(a,c){var b,g,d,h=0,f=F.pop()|| "socket_"+ ++z;return e.extend(u.httpbase(a,c),{open:function(){function c(){var e=a.buildURL(h?"poll":"open",{callback:f,count:++h}),l=q.head||q.getElementsByTagName("head")[0]||q.documentElement;a.data("url",e);b=q.createElement("script");b.async=!0;b.src=e;b.clean=function(){b.clean=b.src=b.onerror=b.onload=b.onreadystatechange=null;b.parentNode&&b.parentNode.removeChild(b)};b.onload=b.onreadystatechange=function(){if(!b.readyState||/loaded|complete/.test(b.readyState))b.clean(),g?(g=!1,d||c()): 1===h?(a.fire("open"),d||c()):a.fire("close","done")};b.onerror=function(){b.clean();a.fire("close","error")};l.insertBefore(b,l.firstChild)}l[f]=function(b){g=!0;1===h&&a.fire("open");a._fire(b)};a.one("close",function(){l[f]=function(){};F.push(f)});c()},close:function(){d=!0;b.clean&&b.clean()}})}};return v});