Comments and whitespace fixes only.
[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 wgUploadSetup() {
15 // Disable URL box if the URL copy upload source type is not selected
16 var e = document.getElementById( 'wpSourceTypeURL' );
17 if( e ) {
18 if( !e.checked ) {
19 var ein = document.getElementById( 'wpUploadFileURL' );
20 if(ein)
21 ein.setAttribute( 'disabled', 'disabled' );
22 }
23 }
24
25 // For MSIE/Mac: non-breaking spaces cause the <option> not to render.
26 // But for some reason, setting the text to itself works
27 var selector = document.getElementById("wpLicense");
28 if (selector) {
29 var ua = navigator.userAgent;
30 var isMacIe = (ua.indexOf("MSIE") != -1) && (ua.indexOf("Mac") != -1);
31 if (isMacIe) {
32 for (var i = 0; i < selector.options.length; i++) {
33 selector.options[i].text = selector.options[i].text;
34 }
35 }
36 }
37 }
38
39 var wgUploadWarningObj = {
40 'responseCache' : { '' : '&nbsp;' },
41 'nameToCheck' : '',
42 'typing': false,
43 'delay': 500, // ms
44 'timeoutID': false,
45
46 'keypress': function () {
47 if ( !wgAjaxUploadDestCheck || !sajax_init_object() ) return;
48
49 // Find file to upload
50 var destFile = document.getElementById('wpDestFile');
51 var warningElt = document.getElementById( 'wpDestFile-warning' );
52 if ( !destFile || !warningElt ) return ;
53
54 this.nameToCheck = destFile.value ;
55
56 // Clear timer
57 if ( this.timeoutID ) {
58 window.clearTimeout( this.timeoutID );
59 }
60 // Check response cache
61 for (cached in this.responseCache) {
62 if (this.nameToCheck == cached) {
63 this.setWarning(this.responseCache[this.nameToCheck]);
64 return;
65 }
66 }
67
68 this.timeoutID = window.setTimeout( 'wgUploadWarningObj.timeout()', this.delay );
69 },
70
71 'checkNow': function (fname) {
72 if ( !wgAjaxUploadDestCheck || !sajax_init_object() ) return;
73 if ( this.timeoutID ) {
74 window.clearTimeout( this.timeoutID );
75 }
76 this.nameToCheck = fname;
77 this.timeout();
78 },
79
80 'timeout' : function() {
81 if ( !wgAjaxUploadDestCheck || !sajax_init_object() ) return;
82 injectSpinner( document.getElementById( 'wpDestFile' ), 'destcheck' );
83
84 // Get variables into local scope so that they will be preserved for the
85 // anonymous callback. fileName is copied so that multiple overlapping
86 // ajax requests can be supported.
87 var obj = this;
88 var fileName = this.nameToCheck;
89 sajax_do_call( 'UploadForm::ajaxGetExistsWarning', [this.nameToCheck],
90 function (result) {
91 obj.processResult(result, fileName)
92 }
93 );
94 },
95
96 'processResult' : function (result, fileName) {
97 removeSpinner( 'destcheck' );
98 this.setWarning(result.responseText);
99 this.responseCache[fileName] = result.responseText;
100 },
101
102 'setWarning' : function (warning) {
103 var warningElt = document.getElementById( 'wpDestFile-warning' );
104 var ackElt = document.getElementById( 'wpDestFileWarningAck' );
105 this.setInnerHTML(warningElt, warning);
106
107 // Set a value in the form indicating that the warning is acknowledged and
108 // doesn't need to be redisplayed post-upload
109 if ( warning == '' || warning == '&nbsp;' ) {
110 ackElt.value = '';
111 } else {
112 ackElt.value = '1';
113 }
114 },
115 'setInnerHTML' : function (element, text) {
116 // Check for no change to avoid flicker in IE 7
117 if (element.innerHTML != text) {
118 element.innerHTML = text;
119 }
120 }
121 }
122
123 function fillDestFilename(id) {
124 if (!wgUploadAutoFill) {
125 return;
126 }
127 if (!document.getElementById) {
128 return;
129 }
130 // Remove any previously flagged errors
131 var e = document.getElementById( 'mw-upload-permitted' );
132 if( e ) e.className = '';
133
134 var e = document.getElementById( 'mw-upload-prohibited' );
135 if( e ) e.className = '';
136
137 var path = document.getElementById(id).value;
138 // Find trailing part
139 var slash = path.lastIndexOf('/');
140 var backslash = path.lastIndexOf('\\');
141 var fname;
142 if (slash == -1 && backslash == -1) {
143 fname = path;
144 } else if (slash > backslash) {
145 fname = path.substring(slash+1, 10000);
146 } else {
147 fname = path.substring(backslash+1, 10000);
148 }
149
150 // Clear the filename if it does not have a valid extension.
151 // URLs are less likely to have a useful extension, so don't include them in the
152 // extension check.
153 if( wgFileExtensions && id != 'wpUploadFileURL' ) {
154 var found = false;
155 if( fname.lastIndexOf( '.' ) != -1 ) {
156 var ext = fname.substr( fname.lastIndexOf( '.' ) + 1 );
157 for( var i = 0; i < wgFileExtensions.length; i++ ) {
158 if( wgFileExtensions[i].toLowerCase() == ext.toLowerCase() ) {
159 found = true;
160 break;
161 }
162 }
163 }
164 if( !found ) {
165 // Not a valid extension
166 // Clear the upload and set mw-upload-permitted to error
167 document.getElementById(id).value = '';
168 var e = document.getElementById( 'mw-upload-permitted' );
169 if( e ) e.className = 'error';
170
171 var e = document.getElementById( 'mw-upload-prohibited' );
172 if( e ) e.className = 'error';
173
174 // Clear wpDestFile as well
175 var e = document.getElementById( 'wpDestFile' )
176 if( e ) e.value = '';
177
178 return false;
179 }
180 }
181
182 // Capitalise first letter and replace spaces by underscores
183 fname = fname.charAt(0).toUpperCase().concat(fname.substring(1,10000)).replace(/ /g, '_');
184
185 // Output result
186 var destFile = document.getElementById('wpDestFile');
187 if (destFile) {
188 destFile.value = fname;
189 wgUploadWarningObj.checkNow(fname) ;
190 }
191 }
192
193 function toggleFilenameFiller() {
194 if(!document.getElementById) return;
195 var upfield = document.getElementById('wpUploadFile');
196 var destName = document.getElementById('wpDestFile').value;
197 if (destName=='' || destName==' ') {
198 wgUploadAutoFill = true;
199 } else {
200 wgUploadAutoFill = false;
201 }
202 }
203
204 var wgUploadLicenseObj = {
205
206 'responseCache' : { '' : '' },
207
208 'fetchPreview': function( license ) {
209 if( !wgAjaxLicensePreview || !sajax_init_object() ) return;
210 for (cached in this.responseCache) {
211 if (cached == license) {
212 this.showPreview( this.responseCache[license] );
213 return;
214 }
215 }
216 injectSpinner( document.getElementById( 'wpLicense' ), 'license' );
217 sajax_do_call( 'UploadForm::ajaxGetLicensePreview', [license],
218 function( result ) {
219 wgUploadLicenseObj.processResult( result, license );
220 }
221 );
222 },
223
224 'processResult' : function( result, license ) {
225 removeSpinner( 'license' );
226 this.showPreview( result.responseText );
227 this.responseCache[license] = result.responseText;
228 },
229
230 'showPreview' : function( preview ) {
231 var previewPanel = document.getElementById( 'mw-license-preview' );
232 if( previewPanel.innerHTML != preview )
233 previewPanel.innerHTML = preview;
234 }
235
236 }
237
238 addOnloadHook( wgUploadSetup );