-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpartial.min.js
1 lines (1 loc) · 2.45 KB
/
partial.min.js
1
var partial=function(){function Init(){var e=document.getElementsByTagName(_tagName);0!=e.length&&LoadPartials(e)}function SetTagName(e="partial"){_tagName=e,Init()}var baseUrl="",_tagName="partial",getPage=function(e,t,a){var r=window.XMLHttpRequest?new XMLHttpRequest:new ActiveXObject("Microsoft.XMLHTTP");return r.open("GET",e),r.onreadystatechange=function(){if(r.readyState>3&&200==r.status){var e=t.getAttribute("noscript"),n=t.getAttribute("ajax");if(null!=n)return void HandleAjax(t,JSON.parse(r.responseText),a);if(t.innerHTML=r.responseText,null==e||null==e||"false"==e){for(var l=t.getElementsByTagName("script"),i=0;i<l.length;i++){var o=l[i].getAttribute("src");if(o)getScript(o);else var u=l[i].text;window.eval(u)}var s=t.getElementsByTagName(_tagName);0!=s.length&&LoadPartials(s)}void 0!==a&&a(t)}},r.setRequestHeader("X-Requested-With","XMLHttpRequest"),r.send(),r},HandleAjax=function(e,t,a){var r=e.querySelectorAll("template");if(0==r.length)throw"Need a template tag inside partial for supporting ajax";var n=r[0].innerHTML;r=Handlebars.compile(n);var l=e.querySelectorAll("view");0==l.length?(l=document.createElement("view"),e.appendChild(l)):l=l[0],l.innerHTML=r(t),void 0!==a&&a(l,t);var i=l.getElementsByTagName(_tagName);0!=i.length&&LoadPartials(i)},reload=function(e){var t=document.querySelectorAll(e)[0],a="",r=void 0;null==t&&null==t||(a=t.getAttribute("src"),r=t.getAttribute("onload")),LoadAfterDelay(r,t,a,0)},LoadPartials=function(e){for(var t=0;t<e.length;++t){var a=e[t],r=a.getAttribute("src");baseUrl=a.getAttribute("baseurl"),baseUrl&&""!==baseUrl&&(r=baseUrl+r);var n=a.getAttribute("onload"),l=a.getAttribute("loadafter"),i=a.getAttribute("autorefresh"),o=!1;null==l?l=0:(l=isNaN(l)?0:parseInt(l),o=!0),i=null==i?0:isNaN(i)?0:parseInt(i),i>0?(l=i,setInterval(LoadAfterDelay,l,n,a,r,l),o||LoadAfterDelay(n,a,r,0)):LoadAfterDelay(n,a,r,l)}},getScript=function(url){var xhr=window.XMLHttpRequest?new XMLHttpRequest:new ActiveXObject("Microsoft.XMLHTTP");return xhr.open("GET",url),xhr.onreadystatechange=function(){xhr.readyState>3&&200==xhr.status&&eval(xhr.responseText)},xhr.setRequestHeader("X-Requested-With","XMLHttpRequest"),xhr.send(),xhr},LoadAfterDelay=function(func,item,src,loadafter){var loadIf=null==item.getAttribute("loadIf")||eval(item.getAttribute("loadIf"));loadIf&&setTimeout(function(){func=eval(func),null==func||null==func?getPage(src,item):getPage(src,item,func)},loadafter)};return Init(),{getPage:getPage,reload:reload,SetTagName:SetTagName}}();