1 line
12 KiB
JavaScript
1 line
12 KiB
JavaScript
"use strict";var r=[];window.JSON||(r=["./json"]),define(r,function(){function serialize(t,e,o){var r;if(Array.isArray(e))e.forEach(function(e,i){r=t?t+"["+(Array.isArray(e)?i:"")+"]":i,"object"==typeof e?serialize(r,e,o):o(r,e)});else for(var i in e)r=t?t+"["+i+"]":i,"object"==typeof e[i]?serialize(r,e[i],o):o(r,e[i])}var _request=function(t,e){this.transport=!0,e=(e+"").trim().toUpperCase(),this.xhr=Xhr(),this.pool={url:(t+"").trim(),type:e||"GET",form:"",headers:{},uuid:Math.random().toString(16).substr(2)}},_requestp=_request.prototype,toS=Object.prototype.toString,win=window,doc=win.document,encode=encodeURIComponent,decode=decodeURIComponent,noop=function(t){if(t)throw new Error(t+"")},rlocalProtocol=/^(?:about|app|app-storage|.+-extension|file|res|widget):$/,isLocal=!1;try{isLocal=rlocalProtocol.test(location.protocol)}catch(e){}var rheaders=/^(.*?):[ \t]*([^\r\n]*)\r?$/gm;!win.FormData,String.prototype.trim&&(String.prototype.trim=function(){return this.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g,"")}),Array.prototype.forEach||(Array.prototype.forEach=function(t,e){var o,r;if(null===this)throw new TypeError("forEach is not a function of null");var i=Object(this),s=i.length>>>0;if("function"!=typeof t)throw new TypeError("callback is not a function");for(arguments.length>1&&(o=e),r=0;s>r;){var a;r in i&&(a=i[r],t.call(o,a,r,i)),r++}}),Array.isArray||(Array.isArray=function(t){return"[object Array]"===toS.call(t)});var IE=function(){if(window.VBArray){var t=document.documentMode;return t?t:window.XMLHttpRequest?7:6}return 0}();win.Xhr=function(){var obj=["XMLHttpRequest","ActiveXObject('MSXML2.XMLHTTP.6.0')","ActiveXObject('MSXML2.XMLHTTP.3.0')","ActiveXObject('MSXML2.XMLHTTP')","ActiveXObject('Microsoft.XMLHTTP')"];obj[0]=8>IE&&0!==IE&&isLocal?"!":obj[0];for(var i=0,a;a=obj[i++];)try{if(eval("new "+a))return new Function("return new "+a)}catch(e){}}();var supportCors="withCredentials"in Xhr(),Format=function(){this.tagHooks=new function(){this.option=doc.createElement("select"),this.thead=doc.createElement("table"),this.td=doc.createElement("tr"),this.area=doc.createElement("map"),this.tr=doc.createElement("tbody"),this.col=doc.createElement("colgroup"),this.legend=doc.createElement("fieldset"),this._default=doc.createElement("div"),this.g=doc.createElementNS("http://www.w3.org/2000/svg","svg"),this.optgroup=this.option,this.tbody=this.tfoot=this.colgroup=this.caption=this.thead,this.th=this.td};var t=this;"circle,defs,ellipse,image,line,path,polygon,polyline,rect,symbol,text,use".replace(/,/g,function(e){t.tagHooks[e]=t.tagHooks.g}),this.rtagName=/<([\w:]+)/,this.rxhtml=/<(?!area|br|col|embed|hr|img|input|link|meta|param)(([\w:]+)[^>]*)\/>/gi,this.scriptTypes={"text/javascript":1,"text/ecmascript":1,"application/ecmascript":1,"application/javascript":1},this.rhtml=/<|&#?\w+;/};Format.prototype={parseJS:function(code){if(code=(code+"").trim())if(1===code.indexOf("use strict")){var script=doc.createElement("script");script.text=code,doc.head.appendChild(script).parentNode.removeChild(script)}else eval(code)},parseXML:function(t,e,o){try{var r=doc.documentMode;win.DOMParser&&(!r||r>8)?(o=new DOMParser,e=o.parseFromString(t,"text/xml")):(e=new ActiveXObject("Microsoft.XMLDOM"),e.async="false",e.loadXML(t))}catch(i){e=void 0}return e&&e.documentElement&&!e.getElementsByTagName("parsererror").length||console.error("Invalid XML: "+t),e},parseHTML:function(t){var e=doc.createDocumentFragment().cloneNode(!1);if("string"!=typeof t)return e;if(!this.rhtml.test(t))return e.appendChild(document.createTextNode(t)),e;t=t.replace(this.rxhtml,"<$1></$2>").trim();var o=(this.rtagName.exec(t)||["",""])[1].toLowerCase(),r=this.tagHooks[o]||this.tagHooks._default,i=null;r.innerHTML=t;var s=r.getElementsByTagName("script");if(s.length)for(var a,n=0;a=s[n++];)if(this.scriptTypes[a.type]){var p=doc.createElement("script").cloneNode(!1);a.attributes.forEach(function(t){p.setAttribute(t.name,t.value)}),p.text=a.text,a.parentNode.replaceChild(p,a)}for(;i=r.firstChild;)e.appendChild(i);return e},param:function(t){if(!t||"string"==typeof t||"number"==typeof t)return t;var e=[],o=function(t,o){/native code/.test(o)||(o="function"==typeof o?o():o,o="[object File]"!==toS.call(o)?encode(o):o,e.push(encode(t)+"="+o))};return"object"==typeof t&&serialize("",t,o),e.join("&")},parseForm:function(t){for(var e,o={},r=0;e=t.elements[r++];)switch(e.type){case"select-one":case"select-multiple":if(e.name.length&&!e.disabled)for(var i,s=0;i=e.options[s++];)i.selected&&(o[e.name]=i.value||i.text);break;case"file":e.name.length&&!e.disabled&&(o[e.name]=e.files[0]);break;case void 0:case"submit":case"reset":case"button":break;case"radio":case"checkbox":if(!e.checked)break;default:e.name.length&&!e.disabled&&(o[e.name]=e.value)}return o},merge:function(t,e){if("object"!=typeof t||"object"!=typeof e)throw new TypeError("argument must be an object");if(Object.assign)return Object.assign(t,e);for(var o in e)t[o]=e[o];return t}};var F=new Format,requestConvert={text:function(t){return t},xml:function(t,e){return void 0!==e?e:F.parseXML(t)},html:function(t){return F.parseHTML(t)},json:function(t){return JSON.parse(t)},script:function(t){return F.parseJS(t)},jsonp:function(){return window[this.jsonpCallback]}},requestExtend={formData:function(){if(win.FormData){if(this.pool.form){console.log(this.pool.form.elements);var t=F.parseForm(this.pool.form);console.log(t),F.merge(this.pool.data,t)}var e=new FormData;for(var o in this.pool.data){var r=this.pool.data[o];Array.isArray(r)?r.forEach(function(t){e.append(o+"[]",t)}):e.append(o,this.pool.data[o])}return e}this.transport=this.mkIframe(this.pool.uuid),this.pool.form=this.mkForm(this.pool.form),this.pool.field=[];for(var o in this.pool.data){var i=this.pool.data[o];if(Array.isArray(i))for(var s,a=0;s=i[a++];){var r=doc.createElement("input");r.type="hidden",r.name=o+"[]",r.value=s,this.pool.field.push(r),this.pool.form.appendChild(r)}else{var r=doc.createElement("input");r.type="hidden",r.name=o,r.value=i,this.pool.field.push(r),this.pool.form.appendChild(r)}}},mkIframe:function(t){var e=F.parseHTML('<iframe id="'+t+'" name="'+t+'" style="position: '+(6===IE?"absolute":"fixed")+';top: -9999px;left: 0"></iframe>').firstChild;return(doc.body||doc.documentElement).insertBefore(e,null)},mkForm:function(t){return t||(t=doc.createElement("form")),t.target=this.pool.uuid,t.action=this.pool.url,t.method="POST",t.enctype="multipart/form-data",t},dispatch:function(t,e){var o=this;if(this.transport){clearTimeout(o.timeoutID),4===e&&(t.status=e);var r=t.status>=200&&t.status<300||304===t.status,i={response:{url:t.responseURL||t.URL,headers:{"content-type":""}},request:{url:t.responseURL||t.URL,headers:o.pool.headers},status:t.status,statusText:t.statusText||"OK",text:"",body:"",error:null};if("object"!=typeof o.transport&&(delete o.transport,delete o.pool),!e){var s=t.getAllResponseHeaders();s=s.split("\n"),s.forEach(function(t){t=t.trim(),t&&(t=t.split(":"),i.response.headers[t.shift().toLowerCase()]=t.join(":").trim())})}if(r)if(204===e)i.statusText="no content";else if(304===e)i.statusText="not modified";else{var a=i.response.headers["content-type"].match(/json|xml|script|html/i)||["text"];a=a[0].toLowerCase();var n=t.responseText||"",p=t.responseXML||"";try{i.text=n||p,i.body=requestConvert[a](n,p)}catch(l){r=!1,i.error=l,i.statusText="parse error"}}else if(e){if(i.status=200,t.body){i.text=t.body.innerHTML,i.body=i.text;var c=t.body.firstChild;c&&"PRE"===c.nodeName.toUpperCase()&&(i.text=c.innerHTML,i.body=requestConvert.json(i.text))}else i.text=i.body=t;if(o.transport.parentNode.removeChild(o.transport),o.pool.field&&o.pool.field.length){o.pool.form.target="",o.pool.form.action="";for(var h,u=0;h=o.pool.field[u++];)o.pool.form.removeChild(h)}}else i.status=i.status||504,i.statusText=i.statusText||"Connected timeout",i.error=F.merge(new Error(i.statusText),{status:i.status});o.callback(i.error,i),delete o.transport,delete o.pool,delete o.xhr}}};_requestp.type=function(t){return"form-data"===this.pool.formType?this:(this.pool.formType=t||"form","form"===t||"GET"===this.pool.type?this.set("content-type","application/x-www-form-urlencoded; charset=UTF-8"):this.set("content-type","application/json; charset=UTF-8"),this)},_requestp.set=function(t,e){if(this.transport){if("object"==typeof t)for(var o in t)o=o.toLowerCase(),this.pool.headers[o]=t[o];else{if("string"!=typeof t)throw new Error("arguments must be string/object, but ["+typeof t+"] given");if(arguments.length<2)throw new Error("2 arguments required");t=t.toLowerCase(),void 0===e?delete this.pool.headers[t]:this.pool.headers[t]=e}return this}},_requestp.send=function(t,e){if(this.transport){if("object"==typeof t){if(this.pool.data&&"string"==typeof this.pool.data)throw new Error("param can not be string and object at the same time");this.pool.data||(this.pool.data={}),F.merge(this.pool.data,t)}else{if("string"!=typeof t)throw new Error("argument of send must be string/object, but ["+typeof t+"] given");if(1===arguments.length){if(this.pool.data)throw new Error("invalid param in function send");this.pool.data=t}else{if(this.pool.data&&"string"==typeof this.pool.data)throw new Error("param can not be string and object at the same time");this.pool.data||(this.pool.data={}),this.pool.data[t]=e}}return this}},_requestp.field=function(t,e){if(this.transport){if(this.pool.formType="form-data",(!this.pool.data||this.pool.data&&"object"!=typeof this.pool.data)&&(this.pool.data={}),1===arguments.length&&"object"==typeof t)F.merge(this.pool.data,t);else{if(2!==arguments.length)throw new TypeError("argument must be an object, but "+typeof t+" given");this.pool.data[t]=e}return this}},_requestp.cache=function(t){return this.transport?("GET"===this.pool.type&&(this.pool.cache=!!t),this):void 0},_requestp.abort=function(){return delete this.transport,this.pool.form||this.xhr.abort(),this},_requestp.timeout=function(t){return"number"!=typeof t||1>t?this:(this.pool.timeout=t,this)},_requestp.form=function(t){return"object"==typeof t&&"FORM"===t.nodeName&&(this.pool.type="POST",this.pool.form=t),this};var originAnchor=doc.createElement("a");return originAnchor.href=location.href,_requestp.end=function(t){var e=this;if(this.transport){if(!this.pool.url)throw new Error("Invalid request url");if(F.merge(this,requestExtend),this.callback=t||noop,this.pool.url=this.pool.url.replace(/#.*$/,"").replace(/^\/\//,location.protocol+"//"),this.pool.param=F.param(this.pool.data),"boolean"!=typeof this.pool.crossDomain){var o=doc.createElement("a");try{o.href=this.pool.url;var r="1"[0]?o.href:o.getAttribute("href",4);o.href=r,this.pool.crossDomain=originAnchor.protocol!==o.protocol||originAnchor.host!==o.host}catch(i){this.pool.crossDomain=!0}}if(this.pool.formType||this.type("form"),this.pool.hasContent="GET"!==this.pool.type,this.pool.hasContent?"form-data"===this.pool.formType?(delete this.pool.headers["content-type"],this.pool.param=this.formData()):"form"!==this.pool.formType&&(this.pool.param=JSON.stringify(this.pool.data)):(this.pool.param&&(this.pool.url+=(/\?/.test(this.pool.url)?"&":"?")+this.pool.param),this.pool.cache===!1&&(this.pool.url+=(/\?/.test(this.pool.url)?"&":"?")+"_="+Math.random())),this.transport!==!0)this.transport.onload=function(){e.dispatch(this.contentWindow.document,4)},this.pool.form.submit();else{this.xhr.onreadystatechange=function(){4===this.readyState&&e.dispatch(this)},this.xhr.open(this.pool.type,this.pool.url,!0);for(var s in this.pool.headers)this.pool.headers[s]&&this.xhr.setRequestHeader(s,this.pool.headers[s]);this.xhr.send(this.pool.param)}this.pool.timeout&&this.pool.timeout>0&&(this.timeoutID=setTimeout(function(){e.abort()},this.pool.timeout))}},win.request||(win.request={get:function(t){if(!t)throw new Error("argument url is required");return new _request(t,"GET")},post:function(t){if(!t)throw new Error("argument url is required");return new _request(t,"POST")},version:"1.0.0",release:"request full version/1.0.0"}),request}); |
JavaScript
95.2%
CSS
4.8%