* (bug 13234) Do not show grey category box for pages with only hidden categories
[lhc/web/wiklou.git] / skins / common / upload.js
1 function licenseSelectorCheck() {
2 var selector = document.getElementById( "wpLicense" );
3 var selection = selector.options[selector.selectedIndex].value;
4 if( selector.selectedIndex > 0 ) {
5 if( selection == "" ) {
6 // Option disabled, but browser is broken and doesn't respect this
7 selector.selectedIndex = 0;
8 }
9 }
10 // We might show a preview
11 wgUploadLicenseObj.fetchPreview( selection );
12 }
13
14 function licenseSelectorFixup() {
15 // for MSIE/Mac; non-breaking spaces cause the <option> not to render
16 // but, for some reason, setting the text to itself works
17 var selector = document.getElementById("wpLicense");
18 if (selector) {
19 var ua = navigator.userAgent;
20 var isMacIe = (ua.indexOf("MSIE") != -1) && (ua.indexOf("Mac") != -1);
21 if (isMacIe) {
22 for (var i = 0; i < selector.options.length; i++) {
23 selector.options[i].text = selector.options[i].text;
24 }
25 }
26 }
27 }
28
29 var wgUploadWarningObj = {
30 'responseCache' : { '' : '&nbsp;' },
31 'nameToCheck' : '',
32 'typing': false,
33 'delay': 500, // ms
34 'timeoutID': false,
35
36 'keypress': function () {
37 if ( !wgAjaxUploadDestCheck || !sajax_init_object() ) return;
38
39 // Find file to upload
40 var destFile = document.getElementById('wpDestFile');
41 var warningElt = document.getElementById( 'wpDestFile-warning' );
42 if ( !destFile || !warningElt ) return ;
43
44 this.nameToCheck = destFile.value ;
45
46 // Clear timer
47 if ( this.timeoutID ) {
48 window.clearTimeout( this.timeoutID );
49 }
50 // Check response cache
51 for (cached in this.responseCache) {
52 if (this.nameToCheck == cached) {
53 this.setWarning(this.responseCache[this.nameToCheck]);
54 return;
55 }
56 }
57
58 this.timeoutID = window.setTimeout( 'wgUploadWarningObj.timeout()', this.delay );
59 },
60
61 'checkNow': function (fname) {
62 if ( !wgAjaxUploadDestCheck || !sajax_init_object() ) return;
63 if ( this.timeoutID ) {
64 window.clearTimeout( this.timeoutID );
65 }
66 this.nameToCheck = fname;
67 this.timeout();
68 },
69
70 'timeout' : function() {
71 if ( !wgAjaxUploadDestCheck || !sajax_init_object() ) return;
72 injectSpinner( document.getElementById( 'wpDestFile' ), 'destcheck' );
73
74 // Get variables into local scope so that they will be preserved for the
75 // anonymous callback. fileName is copied so that multiple overlapping
76 // ajax requests can be supported.
77 var obj = this;
78 var fileName = this.nameToCheck;
79 sajax_do_call( 'UploadForm::ajaxGetExistsWarning', [this.nameToCheck],
80 function (result) {
81 obj.processResult(result, fileName)
82 }
83 );
84 },
85
86 'processResult' : function (result, fileName) {
87 removeSpinner( 'destcheck' );
88 this.setWarning(result.responseText);
89 this.responseCache[fileName] = result.responseText;
90 },
91
92 'setWarning' : function (warning) {
93 var warningElt = document.getElementById( 'wpDestFile-warning' );
94 var ackElt = document.getElementById( 'wpDestFileWarningAck' );
95 this.setInnerHTML(warningElt, warning);
96
97 // Set a value in the form indicating that the warning is acknowledged and
98 // doesn't need to be redisplayed post-upload
99 if ( warning == '' || warning == '&nbsp' ) {
100 ackElt.value = '';
101 } else {
102 ackElt.value = '1';
103 }
104 },
105
106 'setInnerHTML' : function (element, text) {
107 // Check for no change to avoid flicker in IE 7
108 if (element.innerHTML != text) {
109 element.innerHTML = text;
110 }
111 }
112 }
113
114 function fillDestFilename(id) {
115 if (!document.getElementById) {
116 return;
117 }
118 var path = document.getElementById(id).value;
119 // Find trailing part
120 var slash = path.lastIndexOf('/');
121 var backslash = path.lastIndexOf('\\');
122 var fname;
123 if (slash == -1 && backslash == -1) {
124 fname = path;
125 } else if (slash > backslash) {
126 fname = path.substring(slash+1, 10000);
127 } else {
128 fname = path.substring(backslash+1, 10000);
129 }
130
131 // Capitalise first letter and replace spaces by underscores
132 fname = fname.charAt(0).toUpperCase().concat(fname.substring(1,10000)).replace(/ /g, '_');
133
134 // Output result
135 var destFile = document.getElementById('wpDestFile');
136 if (destFile) {
137 destFile.value = fname;
138 wgUploadWarningObj.checkNow(fname) ;
139 }
140 }
141
142 var wgUploadLicenseObj = {
143
144 'responseCache' : { '' : '' },
145
146 'fetchPreview': function( license ) {
147 if( !wgAjaxLicensePreview || !sajax_init_object() ) return;
148 for (cached in this.responseCache) {
149 if (cached == license) {
150 this.showPreview( this.responseCache[license] );
151 return;
152 }
153 }
154 injectSpinner( document.getElementById( 'wpLicense' ), 'license' );
155 sajax_do_call( 'UploadForm::ajaxGetLicensePreview', [license],
156 function( result ) {
157 wgUploadLicenseObj.processResult( result, license );
158 }
159 );
160 },
161
162 'processResult' : function( result, license ) {
163 removeSpinner( 'license' );
164 this.showPreview( result.responseText );
165 this.responseCache[license] = result.responseText;
166 },
167
168 'showPreview' : function( preview ) {
169 var previewPanel = document.getElementById( 'mw-license-preview' );
170 if( previewPanel.innerHTML != preview )
171 previewPanel.innerHTML = preview;
172 }
173
174 }
175
176 addOnloadHook( licenseSelectorFixup );