-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathskeletize.js
251 lines (163 loc) · 5.03 KB
/
skeletize.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
242
243
244
245
246
247
248
249
250
251
/**
* This is a very simple skeleton screen generator for AJAX loading purposes.
* The general logic is that through data-attributes, you can trigger an element
* to enter a "skeletized" state during requests. Then you can destroy that state
* once the request is finished.
*
* For now this plugin will only create a skeleton screen out of the first level
* children of the specified container. The only requirements out of the box is
* that the element passed to the plugin have a `position:relative` set.
*
* @author Justin Kaczmar <kaczmar.justin@gmail.com>
*/
var Skeletize = function ()
{
/**
* @param { boolean } on_parent Specify whether to build the skeleton over the target element or its children.
*/
this.on_parent = false;
/**
* @param { HTMLElement } target The element(s) on which each operation will be done.
*/
this.target = null;
/**
* @param { string } text The text (optional) to insert into the skeleton container as a message.
*/
this.text = '';
/**
* @param { string } type The type of loading to create (skeleton or spinner).
*/
this.type = 'skeleton';
/**
* @param { string } type Show/hide the flashing gray background.
*/
this.show_background = true;
// Do the onLoad function.
//this.onLoad();
}
Skeletize.prototype.defaults = {};
/**
* Initialize and place the skeleton screen over the speicified element.
*
* @return {[type]} [description]
*/
Skeletize.prototype.create = function ()
{
this.target.forEach((element) => {
this.text = element.dataset.skeletizeText;
this.buildOnParent(element);
});
}
Skeletize.prototype.buildOnParent = function (el, on_load = false)
{
let new_skeleton_part = this.createSkeletonPart(el, on_load);
el.appendChild(new_skeleton_part);
}
Skeletize.prototype.createSkeletonPart = function (element, on_load = false)
{
// Create the element and give it out custom class.
let new_skeleton_part = document.createElement('DIV');
new_skeleton_part.classList.add('skeletize-part');
if (this.type === 'spinner')
new_skeleton_part.classList.add('spinner');
if (this.show_background)
new_skeleton_part.classList.add('skeletize-bg');
if (typeof this.text != "undefined")
new_skeleton_part.innerHTML = this.text;
// By default always set the width and height of the skeleton container.
new_skeleton_part.style.width = element.offsetWidth + 'px';
if (this.on_parent || on_load)
{
new_skeleton_part.style.top = '0px';
new_skeleton_part.style.left = '0px';
new_skeleton_part.style.height = '100%';
}
else
{
new_skeleton_part.style.top = element.offsetTop + 'px';
new_skeleton_part.style.left = element.offsetLeft + 'px';
new_skeleton_part.style.height = element.offsetHeight + 'px';
}
return new_skeleton_part;
}
Skeletize.prototype.onParent = function (on_parent = false)
{
this.on_parent = on_parent;
return this;
}
Skeletize.prototype.type = function (type = 'skeleton')
{
this.type = type;
return this;
}
Skeletize.prototype.showBackground = function (show_background = true)
{
this.show_background = show_background;
return this;
}
Skeletize.prototype.setTarget = function (element)
{
if (element[0] == '#')
this.target = [document.getElementById(element.substr(1))];
else if (element[0] == '.')
this.target = Array.from(document.getElementsByClassName(element.substr(1)));
else if (
typeof HTMLElement === "object" ? element instanceof HTMLElement : //DOM2
element && typeof element === "object" && element !== null && element.nodeType === 1 && typeof element.nodeName==="string"
)
this.target = [element];
else
throw "SKELETIZE.JS ERROR: A valid class or ID is required.";
return this;
}
Skeletize.prototype.clear = function ()
{
this.target.forEach((element) => {
element.classList.remove('skeletize-on-load');
});
return this;
}
Skeletize.prototype.getLength = function ()
{
return this.target.length;
}
Skeletize.prototype.destroy = function ()
{
this.target.forEach((element) => {
// This will also help with legacy stuff that already exists elsewhere.
if (element.classList.contains('skeletize-on-load')) {
element.classList.remove('skeletize-on-load', 'skeletize-bg');
}
Array.from(element.querySelectorAll('.skeletize-part')).forEach((el) => {
el.remove();
});
});
}
/**
* This function could be used in the future to actually accept a data
* attribute to create your own custom skeletized layouts.
*
* @todo Perhaps integrate this.
*
* @param {[type]} str [description]
* @return {[type]} [description]
*/
Skeletize.prototype.parse = function (str)
{
let template = '<div class="skeletize-container">';
if (str.indexOf('header') >= 0) {
template += '<div class="skeletize-header"></div>';
}
if (str.indexOf('body') >= 0) {
template += '<div class="skeletize-body"></div>';
}
template += '</div>';
return template;
}
/**
* Remove skeletize-on-load classes from whole document.
*
* @return void
Skeletize.prototype.onLoad = function () {
$('.skeletize-on-load').removeClass('skeletize-on-load');
}*/