Add spacing in the protection form.
[lhc/web/wiklou.git] / skins / common / protect.js
1 function protectInitialize(tableId, labelText) {
2 if (document.createTextNode) {
3 var box = document.getElementById(tableId);
4 if (!box)
5 return false;
6
7 var tbody = box.getElementsByTagName('tbody')[0];
8 var row = document.createElement('tr');
9 tbody.appendChild(row);
10
11 row.appendChild(document.createElement('td'));
12 var col2 = document.createElement('td');
13 row.appendChild(col2);
14
15 var check = document.createElement('input');
16 check.id = "mwProtectUnchained";
17 check.type = "checkbox";
18 check.onclick = protectChainUpdate;
19 col2.appendChild(check);
20
21 var space = document.createTextNode(" ");
22 col2.appendChild(space);
23
24 var label = document.createElement('label');
25 label.setAttribute("for", "mwProtectUnchained");
26 label.appendChild(document.createTextNode(labelText));
27 col2.appendChild(label);
28
29 if (protectAllMatch()) {
30 check.checked = false;
31 protectEnable(false);
32 } else {
33 check.checked = true;
34 protectEnable(true);
35 }
36
37 return true;
38 }
39 return false;
40 }
41
42 function protectLevelsUpdate(source) {
43 if (!protectUnchained()) {
44 protectUpdateAll(source.selectedIndex);
45 }
46 }
47
48 function protectChainUpdate() {
49 if (protectUnchained()) {
50 protectEnable(true);
51 } else {
52 protectChain();
53 protectEnable(false);
54 }
55 }
56
57
58 function protectAllMatch() {
59 var values = new Array();
60 protectForSelectors(function(set) {
61 values[values.length] = set.selectedIndex;
62 });
63 for (var i = 1; i < values.length; i++) {
64 if (values[i] != values[0]) {
65 return false;
66 }
67 }
68 return true;
69 }
70
71 function protectUnchained() {
72 var unchain = document.getElementById("mwProtectUnchained");
73 if (!unchain) {
74 alert("This shouldn't happen");
75 return false;
76 }
77 return unchain.checked;
78 }
79
80 function protectChain() {
81 // Find the highest-protected action and bump them all to this level
82 var maxIndex = -1;
83 protectForSelectors(function(set) {
84 if (set.selectedIndex > maxIndex) {
85 maxIndex = set.selectedIndex;
86 }
87 });
88 protectUpdateAll(maxIndex);
89 }
90
91 function protectUpdateAll(index) {
92 protectForSelectors(function(set) {
93 if (set.selectedIndex != index) {
94 set.selectedIndex = index;
95 }
96 });
97 }
98
99 function protectForSelectors(func) {
100 var selectors = protectSelectors();
101 for (var i = 0; i < selectors.length; i++) {
102 func(selectors[i]);
103 }
104 }
105
106 function protectSelectors() {
107 var all = document.getElementsByTagName("select");
108 var ours = new Array();
109 for (var i = 0; i < all.length; i++) {
110 var set = all[i];
111 if (set.id.match(/^mwProtect-level-/)) {
112 ours[ours.length] = set;
113 }
114 }
115 return ours;
116 }
117
118 function protectEnable(val) {
119 // fixme
120 var first = true;
121 protectForSelectors(function(set) {
122 if (first) {
123 first = false;
124 } else {
125 set.disabled = !val;
126 set.style.visible = val ? "visible" : "hidden";
127 }
128 });
129 }