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