here it is ... the upload-api, script-server, js2 (javascript phase2) branch merge...
[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 source type if not checked:
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 //check for the wgFileExtensions and clear if not a valid fname extension
150
151 //urls are less likely to have a usefull extension don't include them in the extention check
152 if( wgFileExtensions && id != 'wpUploadFileURL' ){
153 var found = false;
154 if( fname.lastIndexOf('.')!=-1 ){
155 var ext = fname.substr( fname.lastIndexOf('.')+1 );
156 for(var i=0; i < wgFileExtensions.length; i++){
157 if( wgFileExtensions[i].toLowerCase() == ext.toLowerCase() )
158 found = true;
159 }
160 }
161 if(!found){
162 //clear the upload set mw-upload-permitted to error
163 document.getElementById(id).value = '';
164 var e = document.getElementById('mw-upload-permitted');
165 if(e) e. className = 'error';
166
167 var e = document.getElementById('mw-upload-prohibited');
168 if(e) e.className = 'error';
169
170 //clear the wpDestFile as well:
171 var e = document.getElementById('wpDestFile')
172 if(e) e.value = '';
173
174 //return false
175 return false;
176 }
177 }
178
179 // Capitalise first letter and replace spaces by underscores
180 fname = fname.charAt(0).toUpperCase().concat(fname.substring(1,10000)).replace(/ /g, '_');
181
182 // Output result
183 var destFile = document.getElementById('wpDestFile');
184 if (destFile) {
185 destFile.value = fname;
186 wgUploadWarningObj.checkNow(fname) ;
187 }
188 }
189
190 function toggleFilenameFiller() {
191 if(!document.getElementById) return;
192 var upfield = document.getElementById('wpUploadFile');
193 var destName = document.getElementById('wpDestFile').value;
194 if (destName=='' || destName==' ') {
195 wgUploadAutoFill = true;
196 } else {
197 wgUploadAutoFill = false;
198 }
199 }
200
201 var wgUploadLicenseObj = {
202
203 'responseCache' : { '' : '' },
204
205 'fetchPreview': function( license ) {
206 if( !wgAjaxLicensePreview || !sajax_init_object() ) return;
207 for (cached in this.responseCache) {
208 if (cached == license) {
209 this.showPreview( this.responseCache[license] );
210 return;
211 }
212 }
213 injectSpinner( document.getElementById( 'wpLicense' ), 'license' );
214 sajax_do_call( 'UploadForm::ajaxGetLicensePreview', [license],
215 function( result ) {
216 wgUploadLicenseObj.processResult( result, license );
217 }
218 );
219 },
220
221 'processResult' : function( result, license ) {
222 removeSpinner( 'license' );
223 this.showPreview( result.responseText );
224 this.responseCache[license] = result.responseText;
225 },
226
227 'showPreview' : function( preview ) {
228 var previewPanel = document.getElementById( 'mw-license-preview' );
229 if( previewPanel.innerHTML != preview )
230 previewPanel.innerHTML = preview;
231 }
232
233 }
234
235 addOnloadHook( wgUploadSetup );