-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
57 lines (47 loc) · 1.78 KB
/
index.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
function PlaceholderHider(itemClass, placeholderClass){
var errors = []
if(arguments.length !== 2)
throw new Error("Expected two arguments, received " + arguments.length);
for(var i in Array.prototype.slice.call(arguments)) {
if(document.querySelectorAll(arguments[i]).length === 0)
errors.push(arguments[i]);
}
if(errors.length > 0)
throw new Error("Unable to find elements for " + errors.join(', '));
this.itemClass = itemClass;
this.placeholderClass = placeholderClass;
// assuming all placeholders use same initial display
this.defaultDisplay = document.querySelectorAll(this.placeholderClass)[0].style.display;
}
// use to modify the gcm i.e. to change the numeric value that causes placeholders to disappear
PlaceholderHider.prototype.gcm = (function(value){
var initialValue = 4;
return function setter(val){
if(typeof arguments[0] === 'number') {
setter.gcm = Math.floor(arguments[0]);
this.hidePlaceholders();
}
if(!setter.gcm){
return initialValue;
}
else
return setter.gcm;
};
})();
// unhide placeholders then hide placeholders according to math on gcm
PlaceholderHider.prototype.hidePlaceholders = function hp() {
var i;
var items = document.querySelectorAll(this.itemClass);
var placeholders = document.querySelectorAll(this.placeholderClass);
var j;
for(j = 0; j < placeholders.length; j++)
placeholders[j].style.display = this.defaultDisplay;
var totalElements = items.length + placeholders.length;
var k = 0;
while(totalElements % this.gcm() !== 0) {
placeholders[k].style.display = 'none';
totalElements--;
k++;
}
};
module.exports = PlaceholderHider;