Document this file all over
[lhc/web/wiklou.git] / skins / common / protect.js
1 /**
2 * Set up the protection chaining interface (i.e. "unlock move permissions" checkbox)
3 * on the protection form
4 *
5 * @param String tableId Identifier of the table containing UI bits
6 * @param String labelText Text to use for the checkbox label
7 */
8 function protectInitialize( tableId, labelText ) {
9 if( !( document.createTextNode && document.getElementById && document.getElementsByTagName ) )
10 return false;
11
12 var box = document.getElementById( tableId );
13 if( !box )
14 return false;
15
16 var tbody = box.getElementsByTagName( 'tbody' )[0];
17 var row = document.createElement( 'tr' );
18 tbody.appendChild( row );
19
20 row.appendChild( document.createElement( 'td' ) );
21 var col = document.createElement( 'td' );
22 row.appendChild( col );
23
24 var check = document.createElement( 'input' );
25 check.id = 'mwProtectUnchained';
26 check.type = 'checkbox';
27 col.appendChild( check );
28 addClickHandler( check, protectChainUpdate );
29
30 col.appendChild( document.createTextNode( ' ' ) );
31 var label = document.createElement( 'label' );
32 label.setAttribute( 'for', 'mwProtectUnchained' );
33 label.appendChild( document.createTextNode( labelText ) );
34 col.appendChild( label );
35
36 check.checked = !protectAllMatch();
37 protectEnable( check.checked );
38
39 allowCascade();
40
41 return true;
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 /**
62 * When protection levels are locked together, update the rest
63 * when one action's level changes
64 *
65 * @param Element source Level selector that changed
66 */
67 function protectLevelsUpdate(source) {
68 if( !protectUnchained() )
69 protectUpdateAll( source.selectedIndex );
70 allowCascade();
71 }
72
73 /**
74 * Update chain status and enable/disable various bits of the UI
75 * when the user changes the "unlock move permissions" checkbox
76 */
77 function protectChainUpdate() {
78 if( protectUnchained() ) {
79 protectEnable( true );
80 } else {
81 protectChain();
82 protectEnable( false );
83 }
84 allowCascade();
85 }
86
87 /**
88 * Are all actions protected at the same level?
89 *
90 * @return boolean
91 */
92 function protectAllMatch() {
93 var values = new Array();
94 protectForSelectors(function(set) {
95 values[values.length] = set.selectedIndex;
96 });
97 for (var i = 1; i < values.length; i++) {
98 if (values[i] != values[0]) {
99 return false;
100 }
101 }
102 return true;
103 }
104
105 /**
106 * Is protection chaining on or off?
107 *
108 * @return bool
109 */
110 function protectUnchained() {
111 var unchain = document.getElementById( 'mwProtectUnchained' );
112 return unchain
113 ? unchain.checked
114 : true; // No control, so we need to let the user set both levels
115 }
116
117 /**
118 * Find the highest-protected action and set all others to that level
119 */
120 function protectChain() {
121 var maxIndex = -1;
122 protectForSelectors(function(set) {
123 if (set.selectedIndex > maxIndex) {
124 maxIndex = set.selectedIndex;
125 }
126 });
127 protectUpdateAll(maxIndex);
128 }
129
130 /**
131 * Protect all actions at the specified level
132 *
133 * @param int index Protection level
134 */
135 function protectUpdateAll(index) {
136 protectForSelectors(function(set) {
137 if (set.selectedIndex != index) {
138 set.selectedIndex = index;
139 }
140 });
141 }
142
143 /**
144 * Apply a callback to each protection selector
145 *
146 * @param callable func Callback function
147 */
148 function protectForSelectors(func) {
149 var selectors = protectSelectors();
150 for (var i = 0; i < selectors.length; i++) {
151 func(selectors[i]);
152 }
153 }
154
155 /**
156 * Get a list of all protection selectors on the page
157 *
158 * @return Array
159 */
160 function protectSelectors() {
161 var all = document.getElementsByTagName("select");
162 var ours = new Array();
163 for (var i = 0; i < all.length; i++) {
164 var set = all[i];
165 if (set.id.match(/^mwProtect-level-/)) {
166 ours[ours.length] = set;
167 }
168 }
169 return ours;
170 }
171
172 /**
173 * Enable/disable protection selectors
174 *
175 * @param boolean val Enable?
176 */
177 function protectEnable(val) {
178 // fixme
179 var first = true;
180 protectForSelectors(function(set) {
181 if (first) {
182 first = false;
183 } else {
184 set.disabled = !val;
185 set.style.visible = val ? "visible" : "hidden";
186 }
187 });
188 }