2 * Set up the protection chaining interface (i.e. "unlock move permissions" checkbox)
3 * on the protection form
5 * @param String tableId Identifier of the table containing UI bits
6 * @param String labelText Text to use for the checkbox label
8 function protectInitialize( tableId
, labelText
) {
9 if( !( document
.createTextNode
&& document
.getElementById
&& document
.getElementsByTagName
) )
12 var box
= document
.getElementById( tableId
);
16 var tbody
= box
.getElementsByTagName( 'tbody' )[0];
17 var row
= document
.createElement( 'tr' );
18 tbody
.appendChild( row
);
20 row
.appendChild( document
.createElement( 'td' ) );
21 var col
= document
.createElement( 'td' );
22 row
.appendChild( col
);
24 var check
= document
.createElement( 'input' );
25 check
.id
= 'mwProtectUnchained';
26 check
.type
= 'checkbox';
27 col
.appendChild( check
);
28 addClickHandler( check
, protectChainUpdate
);
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
);
36 check
.checked
= !protectAllMatch();
37 protectEnable( check
.checked
);
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( !isCascadeableLevel(selected
) ) {
51 document
.getElementById( 'mwProtect-cascade' ).checked
= false;
52 document
.getElementById( 'mwProtect-cascade' ).disabled
= true;
57 document
.getElementById( 'mwProtect-cascade' ).disabled
= false;
61 function isCascadeableLevel( level
) {
62 for (var k
= 0; k
< wgCascadeableLevels
.length
; k
++) {
63 if ( wgCascadeableLevels
[k
] == level
) {
71 * When protection levels are locked together, update the rest
72 * when one action's level changes
74 * @param Element source Level selector that changed
76 function protectLevelsUpdate(source
) {
77 if( !protectUnchained() )
78 protectUpdateAll( source
.selectedIndex
);
83 * Update chain status and enable/disable various bits of the UI
84 * when the user changes the "unlock move permissions" checkbox
86 function protectChainUpdate() {
87 if( protectUnchained() ) {
88 protectEnable( true );
91 protectEnable( false );
97 * Are all actions protected at the same level?
101 function protectAllMatch() {
102 var values
= new Array();
103 protectForSelectors(function(set) {
104 values
[values
.length
] = set.selectedIndex
;
106 for (var i
= 1; i
< values
.length
; i
++) {
107 if (values
[i
] != values
[0]) {
115 * Is protection chaining on or off?
119 function protectUnchained() {
120 var unchain
= document
.getElementById( 'mwProtectUnchained' );
123 : true; // No control, so we need to let the user set both levels
127 * Find the highest-protected action and set all others to that level
129 function protectChain() {
131 protectForSelectors(function(set) {
132 if (set.selectedIndex
> maxIndex
) {
133 maxIndex
= set.selectedIndex
;
136 protectUpdateAll(maxIndex
);
140 * Protect all actions at the specified level
142 * @param int index Protection level
144 function protectUpdateAll(index
) {
145 protectForSelectors(function(set) {
146 if (set.selectedIndex
!= index
) {
147 set.selectedIndex
= index
;
153 * Apply a callback to each protection selector
155 * @param callable func Callback function
157 function protectForSelectors(func
) {
158 var selectors
= protectSelectors();
159 for (var i
= 0; i
< selectors
.length
; i
++) {
165 * Get a list of all protection selectors on the page
169 function protectSelectors() {
170 var all
= document
.getElementsByTagName("select");
171 var ours
= new Array();
172 for (var i
= 0; i
< all
.length
; i
++) {
174 if (set.id
.match(/^mwProtect-level-/)) {
175 ours
[ours
.length
] = set;
182 * Enable/disable protection selectors
184 * @param boolean val Enable?
186 function protectEnable(val
) {
189 protectForSelectors(function(set) {
194 set.style
.visible
= val
? "visible" : "hidden";