1
|
|
|
var Include = function (error, success) { |
2
|
|
|
// |
3
|
|
|
this.cfg = {}; |
4
|
|
|
// this.cfg.target = target; |
5
|
|
|
|
6
|
|
|
var include = this; |
|
|
|
|
7
|
|
|
|
8
|
|
|
this.addScriptToHead = function (src) { |
9
|
|
|
var head = document.getElementsByTagName('head')[0]; |
10
|
|
|
var script = document.createElement('script'); |
11
|
|
|
script.type = 'text/javascript'; |
12
|
|
|
script.src = src; |
13
|
|
|
head.appendChild(script); |
14
|
|
|
}; |
15
|
|
|
|
16
|
|
|
this.addScriptToHeadDelayed = function (src) { |
17
|
|
|
var delay = 100; |
18
|
|
|
setTimeout(function () { |
19
|
|
|
addScriptToHead(src) |
20
|
|
|
}, |
21
|
|
|
delay); |
22
|
|
|
}; |
23
|
|
|
|
24
|
|
|
this.addStyleStringToHead = function (string) { |
25
|
|
|
var head = document.getElementsByTagName('head')[0]; |
26
|
|
|
var linkElement = this.document.createElement('link'); |
27
|
|
|
linkElement.setAttribute('rel', 'stylesheet'); |
28
|
|
|
linkElement.setAttribute('type', 'text/css'); |
29
|
|
|
linkElement.setAttribute('href', 'data:text/css;charset=UTF-8,' + encodeURIComponent(string)); |
30
|
|
|
head.appendChild(linkElement); |
31
|
|
|
}; |
32
|
|
|
|
33
|
|
|
// |
34
|
|
|
// function addStyleStringToHeadDelayed(string) { |
35
|
|
|
// var delay = 50; |
36
|
|
|
// document.addEventListener("DOMContentLoaded", function () { |
37
|
|
|
// setTimeout(function () { |
38
|
|
|
// addStyleStringToHead(string) |
39
|
|
|
// }, |
40
|
|
|
// delay) |
41
|
|
|
// }); |
42
|
|
|
// } |
43
|
|
|
|
44
|
|
|
|
45
|
|
|
this.addStyleToHead = function (src) { |
46
|
|
|
var head = document.getElementsByTagName('head')[0]; |
47
|
|
|
var link = document.createElement('link'); |
48
|
|
|
link.rel = 'stylesheet'; |
49
|
|
|
link.type = 'text/css'; |
50
|
|
|
link.href = src; |
51
|
|
|
head.appendChild(link); |
52
|
|
|
|
53
|
|
|
// var linkElement = this.document.createElement('link'); |
54
|
|
|
// linkElement.setAttribute('rel', 'stylesheet'); |
55
|
|
|
// linkElement.setAttribute('type', 'text/css'); |
56
|
|
|
// linkElement.setAttribute('href', src); |
57
|
|
|
// head.appendChild(linkElement); |
58
|
|
|
}; |
59
|
|
|
|
60
|
|
|
this.addStyleToHeadDelayed = function (src) { |
61
|
|
|
addStyleToHead(src); |
62
|
|
|
// |
63
|
|
|
// var delay = 1; |
64
|
|
|
// setTimeout(function () { |
65
|
|
|
// addStyleToHead(src); |
66
|
|
|
// }, |
67
|
|
|
// delay); |
68
|
|
|
}; |
69
|
|
|
|
70
|
|
|
// <a href="/path/to/image.jpg" download="FileName.jpg"> |
71
|
|
|
this.includeImgA = function (url, fileName) { |
72
|
|
|
var xhr = new XMLHttpRequest(); |
73
|
|
|
xhr.open("GET", url, true); |
74
|
|
|
xhr.responseType = "blob"; |
75
|
|
|
xhr.onload = function () { |
76
|
|
|
var urlCreator = window.URL || window.webkitURL; |
77
|
|
|
var imageUrl = urlCreator.createObjectURL(this.response); |
78
|
|
|
var tag = document.createElement('a'); |
79
|
|
|
tag.href = imageUrl; |
80
|
|
|
tag.download = fileName; |
81
|
|
|
document.body.appendChild(tag); |
82
|
|
|
tag.click(); |
83
|
|
|
document.body.removeChild(tag); |
84
|
|
|
} |
85
|
|
|
xhr.send(); |
86
|
|
|
}; |
87
|
|
|
|
88
|
|
|
this.includeImg = function (url, selector) { |
89
|
|
|
console.log('apiunit / image / ', url); |
|
|
|
|
90
|
|
|
var el = new E(selector); |
|
|
|
|
91
|
|
|
var elmnt = el.first(); |
92
|
|
|
|
93
|
|
|
let img = new Image; |
|
|
|
|
94
|
|
|
img.onload = function () { |
95
|
|
|
console.log("Including IMG:", url); |
|
|
|
|
96
|
|
|
elmnt.appendChild(img); |
97
|
|
|
}; |
98
|
|
|
img.src = url; // erst nach dem Event Listener! |
99
|
|
|
|
100
|
|
|
// var image = document.images[0]; |
101
|
|
|
// var downloadingImage = new Image(); |
102
|
|
|
// downloadingImage.onload = function () { |
103
|
|
|
// image.src = this.src; |
104
|
|
|
// }; |
105
|
|
|
// downloadingImage.src = url; |
106
|
|
|
}; |
107
|
|
|
|
108
|
|
|
this.includeHtml = function (file, selector, error, success) { |
109
|
|
|
var xhttp; |
110
|
|
|
|
111
|
|
|
var el = new E(selector); |
|
|
|
|
112
|
|
|
|
113
|
|
|
console.log('includeHtml el', el); |
|
|
|
|
114
|
|
|
|
115
|
|
|
var elmnt = el.first(); |
116
|
|
|
console.log('includeHtml elmnt', elmnt); |
117
|
|
|
|
118
|
|
|
|
119
|
|
|
if (typeof success !== 'function') { |
120
|
|
|
success = function () { |
121
|
|
|
console.log('success', "included"); |
|
|
|
|
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
if (typeof error !== 'function') { |
126
|
|
|
error = function () { |
127
|
|
|
console.log('error', "Page not found."); |
|
|
|
|
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
console.log('file', file); |
131
|
|
|
|
132
|
|
|
if (file) { |
133
|
|
|
/* Make an HTTP request using the attribute value as the file name: */ |
134
|
|
|
xhttp = new XMLHttpRequest(); |
135
|
|
|
xhttp.onreadystatechange = function () { |
136
|
|
|
console.log('el_id', selector); |
|
|
|
|
137
|
|
|
|
138
|
|
|
if (this.readyState == 4) { |
139
|
|
|
if (this.status == 200) { |
140
|
|
|
// console.log('elmnt', elmnt); |
141
|
|
|
// console.log('responseText', this.responseText); |
142
|
|
|
// elmnt.innerHTML = this.responseText; |
143
|
|
|
// elmnt.appendChild(this.responseText); |
144
|
|
|
// elmnt.insertAdjacentHTML('beforeend', this.responseText); |
145
|
|
|
// var e = document.createElement('div'); |
146
|
|
|
// e.innerHTML = this.responseText; |
147
|
|
|
// while(e.firstChild) { |
148
|
|
|
// elmnt.appendChild(e); |
149
|
|
|
// } |
150
|
|
|
|
151
|
|
|
// elmnt.insertAdjacentHTML('afterend', this.responseText); |
152
|
|
|
elmnt.insertAdjacentHTML('beforeend', this.responseText); |
153
|
|
|
|
154
|
|
|
success(this); |
|
|
|
|
155
|
|
|
} |
156
|
|
|
if (this.status == 404) { |
157
|
|
|
elmnt.innerHTML = "Page not found."; |
158
|
|
|
error(this); |
|
|
|
|
159
|
|
|
} |
160
|
|
|
/* Remove the attribute, and call this function once more: */ |
161
|
|
|
// includeHtml(file, success, error); |
162
|
|
|
} |
163
|
|
|
}; |
164
|
|
|
xhttp.open("GET", file, true); |
165
|
|
|
xhttp.send(); |
166
|
|
|
/* Exit the function: */ |
167
|
|
|
} |
168
|
|
|
return this; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
}; |
172
|
|
|
|