forked from gturri/hidden
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
executable file
·80 lines (69 loc) · 2.06 KB
/
script.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
/**
* script.js for Plugin hidden
*
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
* @author Guillaume Turri <guillaume.turri@gmail.com>
*
*/
/**
* @param elem The structure to display/hide
* @param forceHide FALSE to flip, TRUE to hide unconditionnaly
*/
function plugin_hidden_flip(elem, forceHide){
var parent = jQuery(elem.parentNode);
var onHiddenText = parent.children('div.hiddenOnHidden')[0].innerHTML;
var onVisibleText = parent.children('div.hiddenOnVisible')[0].innerHTML;
var head = parent.children('div.hiddenHead')[0];
var body = parent.children('div.hiddenBody')[0];
if (body.style.display !== "none" || forceHide){
jQuery(body).hide();
head.innerHTML = onHiddenText;
} else {
jQuery(body).show();
head.innerHTML = onVisibleText;
}
}
/**
* The plugin will hide text only if javascript is enabled (else, it wouldn't be possible to display it)
*/
function installPluginHiddenJS(){
jQuery(".hiddenActive").each(function(){
jQuery(this).click(function(){plugin_hidden_flip(this, false)});
});
jQuery(".hiddenSinceBeginning").each(function(){
plugin_hidden_flip(this, true);
});
jQuery(".hiddenSwitch").each(function(){
jQuery(this).click(function(){plugin_hidden_flipAll()});
});
}
/**
* Check if at least one element is currently hidden
*/
function plugin_hidden_atLeastOneHidden(){
var result = false;
jQuery(".hiddenActive").each(function(){
if ( plugin_hidden_isHidden(this) ){
result = true;
}
});
return result;
}
/**
* Check if a given element is hidden
*/
function plugin_hidden_isHidden(elem){
return jQuery(elem.parentNode).children('div.hiddenBody')[0].style.display === "none";
}
/**
* Swap the state of every element
*/
function plugin_hidden_flipAll(){
var weShallDisplay = plugin_hidden_atLeastOneHidden();
jQuery(".hiddenActive").each(function(){
if ( weShallDisplay == plugin_hidden_isHidden(this) ){
plugin_hidden_flip(this, false);
}
});
}
jQuery(function(){installPluginHiddenJS();});