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.js
176 lines (148 loc) · 4.92 KB
/
include.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*!
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 = false;
var useHandlebars = false;
var elements = [];
var requests = [];
var getElements = function() {
return document.getElementsByTagName("include");
};
var findElements = function() {
elements = getElements();
requests = [];
if (elements.length > 0) {
loadElements();
} else {
Include.onLoad();
}
};
var loadElements = function() {
var i;
for (i = 0; elements.length > i; i += 1) {
appendRequest(elements[i], i);
}
};
var appendRequest = function(element, i) {
requests[i] = {
client : new XMLHttpRequest(),
isLoaded : false
};
requests[i].client.open("GET", element.getAttribute("src"));
requests[i].client.json = element.getAttribute("data-json-src");
requests[i].client.ext = getExtension(element.getAttribute("src"));
requests[i].client.element = element;
requests[i].client.id = i;
requests[i].client.onloadend = function(e) {
prepareHtmlLoaded(e);
};
requests[i].client.send();
};
var prepareHtmlLoaded = function(e) {
var jsonSrc, jsonRequest;
jsonSrc = e.currentTarget.json;
if (jsonSrc !== null) {
jsonRequest = new XMLHttpRequest();
jsonRequest.open("GET", jsonSrc);
jsonRequest.onloadend = function(jsonEvent) {
createHtmlLoaded(e, jsonEvent.currentTarget.responseText);
};
jsonRequest.send();
} else {
createHtmlLoaded(e);
}
};
var createHtmlLoaded = function(e, textContent) {
var element, json, htmlString, extension;
element = e.currentTarget.element;
extension = e.currentTarget.ext;
json = e.currentTarget.json;
if (typeof textContent === "undefined") {
textContent = element.textContent.trim();
}
if (extension === "html" || extension === "htm") {
htmlString = getCompiledHTML(e.currentTarget.responseText, textContent);
} else if (extension === "md" || extension === "markdown") {
htmlString = getCompiledMarkdown(e.currentTarget.responseText);
}
element.insertAdjacentHTML("beforeBegin", htmlString);
element.parentNode.removeChild(element);
requests[e.currentTarget.id].isLoaded = true;
if (checkRequestsStatus()) {
findElements();
}
};
var checkRequestsStatus = function() {
var i;
for (i = 0; requests.length > i; i += 1) {
if (!requests[i].isLoaded) {
return false;
}
}
return true;
};
var getExtension = function(filename) {
return filename.substr(filename.lastIndexOf(".") + 1).toLowerCase();
};
var getCompiledHTML = function(htmlString, textContent) {
var template;
if (textContent.length > 0 && useHandlebars) {
template = Handlebars.compile(htmlString);
return template(getJSON(textContent));
} else {
return htmlString;
}
};
var getCompiledMarkdown = function(markdownString) {
if (useMarkdown) {
return marked(markdownString);
} else {
return markdownString;
}
};
var getJSON = function(textContent) {
try {
var jsonData = eval("(" + textContent + ")");
if (jsonData.constructor === {}.constructor) {
return jsonData;
} else {
console.log("Warning: the constructor is not an Object. Empty object will be returned.");
return {};
}
} catch (e) {
console.log("Warning: syntax error encountered. Empty object will be returned.");
return {};
}
};
return {
init : function() {
if (typeof Handlebars !== "undefined") {
useHandlebars = true;
//console.log("Notice: found Handlebars js library");
} else {
useHandlebars = false;
//console.log("Warning: Handlebars is NOT loaded and will be not used to render HTML include elements");
}
if (typeof marked !== "undefined") {
useMarkdown = true;
} else {
useMarkdown = false;
}
findElements();
},
onLoad : function() {
//console.log("Notice: all incliude elements are loaded");
}
};
}());
document.addEventListener("DOMContentLoaded", function(){
"use strict";
Include.init();
});