Partial revert of r45689 "bug 15470: Don't force-capitalize on Special:Upload. There...
[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 (!wgUploadAutoFill) {
116 return;
117 }
118 if (!document.getElementById) {
119 return;
120 }
121 var path = document.getElementById(id).value;
122 // Find trailing part
123 var slash = path.lastIndexOf('/');
124 var backslash = path.lastIndexOf('\\');
125 var fname;
126 if (slash == -1 && backslash == -1) {
127 fname = path;
128 } else if (slash > backslash) {
129 fname = path.substring(slash+1, 10000);
130 } else {
131 fname = path.substring(backslash+1, 10000);
132 }
133
134 // Capitalise first letter and replace spaces by underscores
135 fname = fname.charAt(0).toUpperCase().concat(fname.substring(1,10000)).replace(/ /g, '_');
136
137 // Output result
138 var destFile = document.getElementById('wpDestFile');
139 if (destFile) {
140 destFile.value = fname;
141 wgUploadWarningObj.checkNow(fname) ;
142 }
143 }
144
145 function toggleFilenameFiller() {
146 if(!document.getElementById) return;
147 var upfield = document.getElementById('wpUploadFile');
148 var destName = document.getElementById('wpDestFile').value;
149 if (destName=='' || destName==' ') {
150 wgUploadAutoFill = true;
151 } else {
152 wgUploadAutoFill = false;
153 }
154 }
155
156 var wgUploadLicenseObj = {
157
158 'responseCache' : { '' : '' },
159
160 'fetchPreview': function( license ) {
161 if( !wgAjaxLicensePreview || !sajax_init_object() ) return;
162 for (cached in this.responseCache) {
163 if (cached == license) {
164 this.showPreview( this.responseCache[license] );
165 return;
166 }
167 }
168 injectSpinner( document.getElementById( 'wpLicense' ), 'license' );
169 sajax_do_call( 'UploadForm::ajaxGetLicensePreview', [license],
170 function( result ) {
171 wgUploadLicenseObj.processResult( result, license );
172 }
173 );
174 },
175
176 'processResult' : function( result, license ) {
177 removeSpinner( 'license' );
178 this.showPreview( result.responseText );
179 this.responseCache[license] = result.responseText;
180 },
181
182 'showPreview' : function( preview ) {
183 var previewPanel = document.getElementById( 'mw-license-preview' );
184 if( previewPanel.innerHTML != preview )
185 previewPanel.innerHTML = preview;
186 }
187
188 }
189
190 addOnloadHook( licenseSelectorFixup );