-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblox.js
241 lines (188 loc) · 8.08 KB
/
blox.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
// Blox.JS:
// Every block needs its specified class and its unique id. Any id will work
// ===================================================================================
// TEMPLATES - TEMPLATES - TEMPLATES - TEMPLATES - TEMPLATES - TEMPLATES - TEMPLATES -
// ===================================================================================
function loadTemplates(blockType, templateFill, givenElId, imgUrl) {
switch (blockType) {
case "single":
var template =
"<div class=\"singleContent\" id=\"" + givenElId + "\">" +
"<div class=\"wsContainer\">" +
templateFill +
"</div>" +
"</div>";
break;
case "singleWallpaper":
var template =
"<div class=\"singleContent\" id=\"" + givenElId + "\" style=\"background-image: url('" + imgUrl + "'); background-size: cover;\">" +
"<div class=\"wsContainer\">" +
templateFill +
"</div>" +
"</div>";
break;
case "doubleWallpaper":
var template =
"<div class=\"doubleContent\" id=\"" + givenElId + "\" style=\"background-image: url('" + imgUrl + "'); background-size: cover;\">" +
"<div class=\"wsContainer\">" +
templateFill +
"</div>" +
"</div>";
break;
}
return template
}
function throwError(errorCode, info) {
switch (errorCode) {
case 001:
throw new Error("Blox.JS: The element \"" + info + "\"is required to have an Id.")
break;
case 002:
throw new Error("Blox.JS: Your double element is required to have a background element in it.");
break;
case 003:
throw new Error("I don't know how I got there, but something went wrong.");
break;
}
}
// ===================================================================================
// MAIN - MAIN - MAIN - MAIN - MAIN - MAIN - MAIN - MAIN - MAIN - MAIN - MAIN - MAIN -
// ===================================================================================
// ===================================================================================
// MAIN - MAIN - MAIN - MAIN - MAIN - MAIN - MAIN - MAIN - MAIN - MAIN - MAIN - MAIN -
// ===================================================================================
// ===================================================================================
// MAIN - MAIN - MAIN - MAIN - MAIN - MAIN - MAIN - MAIN - MAIN - MAIN - MAIN - MAIN -
// ===================================================================================
function drawBlock(blockType, contents) {
var blockDiv;
switch (blockType) {
case "single":
blockDiv = document.createElement("div");
blockDiv.setAttribute('class', "blok");
document.body.appendChild(blockDiv);
blockDiv.innerHTML = contents;
break;
case "double":
blockDiv = document.createElement("div");
blockDiv.setAttribute('class', "blok")
document.body.appendChild(blockDiv);
blockDiv.innerHTML = contents;
break;
}
}
// =========================================================================================
// CHECK SINGLE - CHECK SINGLE - CHECK SINGLE - CHECK SINGLE - CHECK SINGLE - CHECK SINGLE -
// =========================================================================================
var elements = document.getElementsByTagName('div');
// This function checks if a single block is wanted.
function checkSingle(element) {
// We already have the element, so now we need to check if it is a blocksingle.
if (element.className == "blockSingle") {
// 'element' is the current element.
// Dont understand completely how it works... but it works ok!
// element = blockSingleElements[0];
// get the elements id
var elementId = element.id
// If the element doesn't have an id, then throw an error.
if (elementId == "" || !elementId || elementId == null) {
throwError(001, element);
// break;
}
// Get the contains of the element:
innerElement = element.innerHTML;
// console.log(innerElement);
// Draw the new template with the contains of the element.
// Also set the new drawn block to have an id, so that we can find it latrer.
var drawBackground = false;
// remove the old element (by id, not by class name).
document.getElementById(elementId).remove();
// ==============================================================================
// BACKGROUND - BACKGROUND - BACKGROUND - BACKGROUND - BACKGROUND - BACKGROUND -
// Now, we want to check if there's an image that has the 'background' id.
// if it has that certain id, then we will set the style
var parentDiv = element;
var hasChild = parentDiv.querySelector("#background") != null;
// hasChild returns 'true' if there's a child present with the background id.
// If hasChild is true, then check which element has the background id.
var imageUrl
if (hasChild) {
var backgroundImage = parentDiv.querySelector("#background");
// console.log(backgroundImage);
// Now that we have found the image, we want to get its Url.
imageUrl = backgroundImage.src;
// console.log(imageUrl);
// We set drawbackground to true, and just fill in the template with an extra image in it.
drawBackground = true;
}
// Now to test if our boolean drawBackground is equal to true or false.
// Pretty self explanatory
switch (drawBackground) {
case true:
drawBlock("single", loadTemplates("singleWallpaper", innerElement, elementId, imageUrl));
// Now we remove the old image
var singleContent = document.getElementById(elementId)
var image = singleContent.querySelector("#background")
image.remove();
break;
case false:
drawBlock("single", loadTemplates("single", innerElement, elementId));
break;
}
}
}
// =========================================================================================
// CHECK DOUBLE - CHECK DOUBLE - CHECK DOUBLE - CHECK DOUBLE - CHECK DOUBLE - CHECK DOUBLE -
// =========================================================================================
function checkDouble(element) {
if (element.className == "blockDouble") {
// element = blockDoubleElements[0];
var elementId = element.id;
if (elementId == "" || !elementId || elementId == null) {
throwError(001, element);
// break;
}
innerElement = element.innerHTML;
console.log(innerElement);
// Drawbackground is set to true, because this block can only have an image
// as a background. Nothing else will be allowed.
var drawBackground = true;
// remove the old element (by id, not by class name).
document.getElementById(elementId).remove();
// ==============================================================================
// BACKGROUND - BACKGROUND - BACKGROUND - BACKGROUND - BACKGROUND - BACKGROUND -
var parentDiv = element;
var hasChild = parentDiv.querySelector("#background") != null;
// If haschild is false, then we will throw an error. This block can't have
// anything else but a background image.
if (hasChild == false) {
throwError(002);
// break;
}
// We dont need to check if hasChild is true, because we already checked if false.
var backgroundImage = parentDiv.querySelector("#background");
var imageUrl = backgroundImage.src;
switch (drawBackground) {
case true:
var contents = loadTemplates("doubleWallpaper", innerElement, elementId, imageUrl);
drawBlock("double", contents);
var doubleContent = document.getElementById(elementId);
var image = doubleContent.querySelector("#background");
image.remove();
break;
case false:
// It shouldn't get here...
throwError(003);
break;
}
}
}
// For every element, check if it is a double or single:
for (var i = 0; i < elements.length; i++) {
// console.log(elements[i]);
// This is always a zero, because when the element is transformed,
// then its first spot is lost and another element takes the zero.
var currentEl = elements[0];
checkSingle(currentEl);
checkDouble(currentEl);
}