* (bug 10732) Protection chaining checkbox broken - this appears to have regressed...
[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.addEventListener( 'click', protectChainUpdate, false );
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 allowCascade();
38
39 return true;
40 }
41 return false;
42 }
43
44 function allowCascade() {
45 var lists = protectSelectors();
46 for( var i = 0; i < lists.length; i++ ) {
47 if( lists[i].selectedIndex > -1 ) {
48 var items = lists[i].getElementsByTagName( 'option' );
49 var selected = items[ lists[i].selectedIndex ].value;
50 if( wgCascadeableLevels.indexOf( selected ) == -1 ) {
51 document.getElementById( 'mwProtect-cascade' ).checked = false;
52 document.getElementById( 'mwProtect-cascade' ).disabled = true;
53 return false;
54 }
55 }
56 }
57 document.getElementById( 'mwProtect-cascade' ).disabled = false;
58 return true;
59 }
60
61 function protectLevelsUpdate(source) {
62 if (!protectUnchained()) {
63 protectUpdateAll(source.selectedIndex);
64 }
65 allowCascade();
66 }
67
68 function protectChainUpdate() {
69 if (protectUnchained()) {
70 protectEnable(true);
71 } else {
72 protectChain();
73 protectEnable(false);
74 }
75 allowCascade();
76 }
77
78
79 function protectAllMatch() {
80 var values = new Array();
81 protectForSelectors(function(set) {
82 values[values.length] = set.selectedIndex;
83 });
84 for (var i = 1; i < values.length; i++) {
85 if (values[i] != values[0]) {
86 return false;
87 }
88 }
89 return true;
90 }
91
92 function protectUnchained() {
93 var unchain = document.getElementById("mwProtectUnchained");
94 if (!unchain) {
95 alert("This shouldn't happen");
96 return false;
97 }
98 return unchain.checked;
99 }
100
101 function protectChain() {
102 // Find the highest-protected action and bump them all to this level
103 var maxIndex = -1;
104 protectForSelectors(function(set) {
105 if (set.selectedIndex > maxIndex) {
106 maxIndex = set.selectedIndex;
107 }
108 });
109 protectUpdateAll(maxIndex);
110 }
111
112 function protectUpdateAll(index) {
113 protectForSelectors(function(set) {
114 if (set.selectedIndex != index) {
115 set.selectedIndex = index;
116 }
117 });
118 }
119
120 function protectForSelectors(func) {
121 var selectors = protectSelectors();
122 for (var i = 0; i < selectors.length; i++) {
123 func(selectors[i]);
124 }
125 }
126
127 function protectSelectors() {
128 var all = document.getElementsByTagName("select");
129 var ours = new Array();
130 for (var i = 0; i < all.length; i++) {
131 var set = all[i];
132 if (set.id.match(/^mwProtect-level-/)) {
133 ours[ours.length] = set;
134 }
135 }
136 return ours;
137 }
138
139 function protectEnable(val) {
140 // fixme
141 var first = true;
142 protectForSelectors(function(set) {
143 if (first) {
144 first = false;
145 } else {
146 set.disabled = !val;
147 set.style.visible = val ? "visible" : "hidden";
148 }
149 });
150 }