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