a144e5ebfe5a9f97aeaa09b350bbbb327e448b73
[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 label = document.createElement('label');
22 label.setAttribute("for", "mwProtectUnchained");
23 label.appendChild(document.createTextNode(labelText));
24 col2.appendChild(label);
25
26 if (protectAllMatch()) {
27 check.checked = false;
28 protectEnable(false);
29 } else {
30 check.checked = true;
31 protectEnable(true);
32 }
33
34 return true;
35 }
36 return false;
37 }
38
39 function protectLevelsUpdate(source) {
40 if (!protectUnchained()) {
41 protectUpdateAll(source.selectedIndex);
42 }
43 }
44
45 function protectChainUpdate() {
46 if (protectUnchained()) {
47 protectEnable(true);
48 } else {
49 protectChain();
50 protectEnable(false);
51 }
52 }
53
54
55 function protectAllMatch() {
56 var values = new Array();
57 protectForSelectors(function(set) {
58 values[values.length] = set.selectedIndex;
59 });
60 for (var i = 1; i < values.length; i++) {
61 if (values[i] != values[0]) {
62 return false;
63 }
64 }
65 return true;
66 }
67
68 function protectUnchained() {
69 var unchain = document.getElementById("mwProtectUnchained");
70 if (!unchain) {
71 alert("This shouldn't happen");
72 return false;
73 }
74 return unchain.checked;
75 }
76
77 function protectChain() {
78 // Find the highest-protected action and bump them all to this level
79 var maxIndex = -1;
80 protectForSelectors(function(set) {
81 if (set.selectedIndex > maxIndex) {
82 maxIndex = set.selectedIndex;
83 }
84 });
85 protectUpdateAll(maxIndex);
86 }
87
88 function protectUpdateAll(index) {
89 protectForSelectors(function(set) {
90 if (set.selectedIndex != index) {
91 set.selectedIndex = index;
92 }
93 });
94 }
95
96 function protectForSelectors(func) {
97 var selectors = protectSelectors();
98 for (var i = 0; i < selectors.length; i++) {
99 func(selectors[i]);
100 }
101 }
102
103 function protectSelectors() {
104 var all = document.getElementsByTagName("select");
105 var ours = new Array();
106 for (var i = 0; i < all.length; i++) {
107 var set = all[i];
108 if (set.id.match(/^mwProtect-level-/)) {
109 ours[ours.length] = set;
110 }
111 }
112 return ours;
113 }
114
115 function protectEnable(val) {
116 // fixme
117 var first = true;
118 protectForSelectors(function(set) {
119 if (first) {
120 first = false;
121 } else {
122 set.disabled = !val;
123 set.style.visible = val ? "visible" : "hidden";
124 }
125 });
126 }