jQueryize the toggle upload source type code
[lhc/web/wiklou.git] / skins / common / block.js
1 // @TODO: find some better JS file for this
2 // Note: borrows from IP.php
3 window.isIPv4Address = function( address, allowBlock ) {
4 var block = allowBlock ? '(?:\\/(?:3[0-2]|[12]?\\d))?' : '';
5 var RE_IP_BYTE = '(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|0?[0-9]?[0-9])';
6 var RE_IP_ADD = '(?:' + RE_IP_BYTE + '\\.){3}' + RE_IP_BYTE;
7 return address.search( new RegExp( '^' + RE_IP_ADD + block + '$' ) ) != -1;
8 };
9
10 // @TODO: find some better JS file for this
11 // Note: borrows from IP.php
12 window.isIPv6Address = function( address, allowBlock ) {
13 var block = allowBlock ? '(?:\\/(?:12[0-8]|1[01][0-9]|[1-9]?\\d))?' : '';
14 var RE_IPV6_ADD =
15 '(?:' + // starts with "::" (including "::")
16 ':(?::|(?::' + '[0-9A-Fa-f]{1,4}' + '){1,7})' +
17 '|' + // ends with "::" (except "::")
18 '[0-9A-Fa-f]{1,4}' + '(?::' + '[0-9A-Fa-f]{1,4}' + '){0,6}::' +
19 '|' + // contains no "::"
20 '[0-9A-Fa-f]{1,4}' + '(?::' + '[0-9A-Fa-f]{1,4}' + '){7}' +
21 ')';
22 if ( address.search( new RegExp( '^' + RE_IPV6_ADD + block + '$' ) ) != -1 ) {
23 return true;
24 }
25 var RE_IPV6_ADD = // contains one "::" in the middle (single '::' check below)
26 '[0-9A-Fa-f]{1,4}' + '(?:::?' + '[0-9A-Fa-f]{1,4}' + '){1,6}';
27 return address.search( new RegExp( '^' + RE_IPV6_ADD + block + '$' ) ) != -1
28 && address.search( /::/ ) != -1 && address.search( /::.*::/ ) == -1;
29 };
30
31 window.considerChangingExpiryFocus = function() {
32 if ( !document.getElementById ) {
33 return;
34 }
35 var drop = document.getElementById( 'wpBlockExpiry' );
36 if ( !drop ) {
37 return;
38 }
39 var field = document.getElementById( 'wpBlockOther' );
40 if ( !field ) {
41 return;
42 }
43 var opt = drop.value;
44 if ( opt == 'other' ) {
45 field.style.display = '';
46 } else {
47 field.style.display = 'none';
48 }
49 };
50
51 window.updateBlockOptions = function() {
52 if ( !document.getElementById ) {
53 return;
54 }
55
56 var target = document.getElementById( 'mw-bi-target' );
57 if ( !target ) {
58 return;
59 }
60
61 var addy = target.value.replace( /(^\s*|\s*$)/, '' ); // trim
62 var isEmpty = (addy == "");
63
64 var isIp = isIPv4Address( addy, true ) || isIPv6Address( addy, true );
65 var isIpRange = isIp && addy.match(/\/\d+$/);
66
67 var anonymousRow = document.getElementById( 'wpAnonOnlyRow' );
68 if( anonymousRow ) {
69 anonymousRow.style.display = ( !isIp && !isEmpty ) ? 'none' : '';
70 }
71
72 var autoblockRow = document.getElementById( 'wpEnableAutoblockRow' );
73 if( autoblockRow ) {
74 autoblockRow.style.display = isIp && !isEmpty ? 'none' : '';
75 }
76
77 var hideuserRow = document.getElementById( 'wpEnableHideUser' );
78 if( hideuserRow ) {
79 hideuserRow.style.display = isIp && !isEmpty ? 'none' : '';
80 }
81
82 var watchuserRow = document.getElementById( 'wpEnableWatchUser' );
83 if( watchuserRow ) {
84 watchuserRow.style.display = isIpRange && !isEmpty ? 'none' : '';
85 }
86 };
87
88 addOnloadHook( updateBlockOptions );
89 addOnloadHook( considerChangingExpiryFocus );