This repository was archived by the owner on Jul 20, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinclude.min.js
9 lines (9 loc) · 2.48 KB
/
include.min.js
1
2
3
4
5
6
7
8
9
/*!
GitHub: https://github.com/vitto/includejs
License: MIT Licence
Version: 1.2.2
Date: 2015-10-19
Author: Vittorio Vittori
Website: http://vit.to
*/
var Include=function(){"use strict";var useMarkdown=!1,useHandlebars=!1,elements=[],requests=[],getElements=function(){return document.getElementsByTagName("include")},findElements=function(){elements=getElements(),requests=[],elements.length>0?loadElements():Include.onLoad()},loadElements=function(){var e;for(e=0;elements.length>e;e+=1)appendRequest(elements[e],e)},appendRequest=function(e,t){requests[t]={client:new XMLHttpRequest,isLoaded:!1},requests[t].client.open("GET",e.getAttribute("src")),requests[t].client.json=e.getAttribute("data-json-src"),requests[t].client.ext=getExtension(e.getAttribute("src")),requests[t].client.element=e,requests[t].client.id=t,requests[t].client.onloadend=function(e){prepareHtmlLoaded(e)},requests[t].client.send()},prepareHtmlLoaded=function(e){var t,n;t=e.currentTarget.json,null!==t?(n=new XMLHttpRequest,n.open("GET",t),n.onloadend=function(t){createHtmlLoaded(e,t.currentTarget.responseText)},n.send()):createHtmlLoaded(e)},createHtmlLoaded=function(e,t){var n,r,s,o;n=e.currentTarget.element,o=e.currentTarget.ext,r=e.currentTarget.json,"undefined"==typeof t&&(t=n.textContent.trim()),"html"===o||"htm"===o?s=getCompiledHTML(e.currentTarget.responseText,t):("md"===o||"markdown"===o)&&(s=getCompiledMarkdown(e.currentTarget.responseText)),n.insertAdjacentHTML("beforeBegin",s),n.parentNode.removeChild(n),requests[e.currentTarget.id].isLoaded=!0,checkRequestsStatus()&&findElements()},checkRequestsStatus=function(){var e;for(e=0;requests.length>e;e+=1)if(!requests[e].isLoaded)return!1;return!0},getExtension=function(e){return e.substr(e.lastIndexOf(".")+1).toLowerCase()},getCompiledHTML=function(e,t){var n;return t.length>0&&useHandlebars?(n=Handlebars.compile(e))(getJSON(t)):e},getCompiledMarkdown=function(e){return useMarkdown?marked(e):e},getJSON=function(textContent){try{var jsonData=eval("("+textContent+")");return jsonData.constructor==={}.constructor?jsonData:(console.log("Warning: the constructor is not an Object. Empty object will be returned."),{})}catch(e){return console.log("Warning: syntax error encountered. Empty object will be returned."),{}}};return{init:function(){useHandlebars="undefined"!=typeof Handlebars?!0:!1,useMarkdown="undefined"!=typeof marked?!0:!1,findElements()},onLoad:function(){}}}();document.addEventListener("DOMContentLoaded",function(){"use strict";Include.init()});