r76109 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r76108‎ | r76109 | r76110 >
Date:16:16, 5 November 2010
Author:travis
Status:deferred
Tags:
Comment:
rewrite make it compatible with 1.16
Modified paths:
  • /trunk/extensions/MultiUpload/multiupload.js (modified) (history)

Diff [purge]

Index: trunk/extensions/MultiUpload/multiupload.js
@@ -1,27 +1,350 @@
 2+
 3+var current;
 4+
 5+function licenseSelectorCheck() {
 6+ var selector = document.getElementById( "wpLicense" );
 7+ var selection = selector.options[selector.selectedIndex].value;
 8+ if( selector.selectedIndex > 0 ) {
 9+ if( selection == "" ) {
 10+ // Option disabled, but browser is broken and doesn't respect this
 11+ selector.selectedIndex = 0;
 12+ }
 13+ }
 14+ // We might show a preview
 15+ wgUploadLicenseObj.fetchPreview( selection );
 16+}
 17+
 18+function wgUploadSetup() {
 19+ // Disable URL box if the URL copy upload source type is not selected
 20+ var e = document.getElementById( 'wpSourceTypeURL' );
 21+ if( e ) {
 22+ if( !e.checked ) {
 23+ var ein = document.getElementById( 'wpUploadFileURL' );
 24+ if(ein)
 25+ ein.setAttribute( 'disabled', 'disabled' );
 26+ }
 27+ }
 28+
 29+ // For MSIE/Mac: non-breaking spaces cause the <option> not to render.
 30+ // But for some reason, setting the text to itself works
 31+ var selector = document.getElementById("wpLicense");
 32+ if (selector) {
 33+ var ua = navigator.userAgent;
 34+ var isMacIe = (ua.indexOf("MSIE") != -1) && (ua.indexOf("Mac") != -1);
 35+ if (isMacIe) {
 36+ for (var i = 0; i < selector.options.length; i++) {
 37+ selector.options[i].text = selector.options[i].text;
 38+ }
 39+ }
 40+ }
 41+
 42+ // Toggle source type
 43+ var sourceTypeCheckboxes = document.getElementsByName( 'wpSourceType' );
 44+ for ( var i = 0; i < sourceTypeCheckboxes.length; i++ ) {
 45+ sourceTypeCheckboxes[i].onchange = toggleUploadInputs;
 46+ }
 47+
 48+ // AJAX wpDestFile warnings
 49+ if ( wgAjaxUploadDestCheck ) {
 50+ // Insert an event handler that fetches upload warnings when wpDestFile
 51+ // has been changed
 52+ for (i = 0; i < wgMaxUploadFiles; i++) {
 53+ document.getElementById( 'wpDestFile' + i ).onchange = function ( e ) {
 54+ wgUploadWarningObj.checkNow(this.value);
 55+ };
 56+ }
 57+ // Insert a row where the warnings will be displayed just below the
 58+ // wpDestFile row
 59+ var optionsTable = document.getElementById( 'mw-htmlform-description' ).tBodies[0];
 60+ var row = optionsTable.insertRow( 1 );
 61+ var td = document.createElement( 'td' );
 62+ td.id = 'wpDestFile-warning';
 63+ td.colSpan = 2;
 64+
 65+ row.appendChild( td );
 66+ }
 67+
 68+ if ( wgAjaxLicensePreview ) {
 69+ // License selector check
 70+ document.getElementById( 'wpLicense' ).onchange = licenseSelectorCheck;
 71+
 72+ // License selector table row
 73+ var wpLicense = document.getElementById( 'wpLicense' );
 74+ var wpLicenseRow = wpLicense.parentNode.parentNode;
 75+ var wpLicenseTbody = wpLicenseRow.parentNode;
 76+
 77+ var row = document.createElement( 'tr' );
 78+ var td = document.createElement( 'td' );
 79+ row.appendChild( td );
 80+ td = document.createElement( 'td' );
 81+ td.id = 'mw-license-preview';
 82+ row.appendChild( td );
 83+
 84+ wpLicenseTbody.insertBefore( row, wpLicenseRow.nextSibling );
 85+ }
 86+
 87+
 88+ // fillDestFile setup
 89+ for ( var i = 0; i < wgUploadSourceIds.length; i++ )
 90+ document.getElementById( wgUploadSourceIds[i] ).onchange = function (e) {
 91+ fillDestFilename( this.id );
 92+ };
 93+}
 94+
295 /**
3 - * JavaScript helper function for MultiUpload extension
 96+ * Iterate over all upload source fields and disable all except the selected one.
 97+ *
 98+ * @param enabledId The id of the selected radio button
 99+ * @return emptiness
4100 */
5 -function fillDestFilenameMulti( i ) {
6 - if( !document.getElementById )
 101+function toggleUploadInputs() {
 102+ // Iterate over all rows with UploadSourceField
 103+ var rows;
 104+ if ( document.getElementsByClassName ) {
 105+ rows = document.getElementsByClassName( 'mw-htmlform-field-UploadSourceField' );
 106+ } else {
 107+ // Older browsers don't support getElementsByClassName
 108+ rows = new Array();
 109+
 110+ var allRows = document.getElementsByTagName( 'tr' );
 111+ for ( var i = 0; i < allRows.length; i++ ) {
 112+ if ( allRows[i].className == 'mw-htmlform-field-UploadSourceField' )
 113+ rows.push( allRows[i] );
 114+ }
 115+ }
 116+
 117+ for ( var i = 0; i < rows.length; i++ ) {
 118+ var inputs = rows[i].getElementsByTagName( 'input' );
 119+
 120+ // Check if this row is selected
 121+ var isChecked = true; // Default true in case wpSourceType is not found
 122+ for ( var j = 0; j < inputs.length; j++ ) {
 123+ if ( inputs[j].name == 'wpSourceType' )
 124+ isChecked = inputs[j].checked;
 125+ }
 126+
 127+ // Disable all unselected rows
 128+ for ( var j = 0; j < inputs.length; j++ ) {
 129+ if ( inputs[j].type != 'radio')
 130+ inputs[j].disabled = !isChecked;
 131+ }
 132+ }
 133+}
 134+
 135+var wgUploadWarningObj = {
 136+ 'responseCache' : { '' : '&nbsp;' },
 137+ 'nameToCheck' : '',
 138+ 'typing': false,
 139+ 'delay': 500, // ms
 140+ 'timeoutID': false,
 141+
 142+ 'keypress': function () {
 143+ if ( !wgAjaxUploadDestCheck || !sajax_init_object() ) return;
 144+
 145+ // Find file to upload
 146+ var destFile = document.getElementById('wpDestFile' + current);
 147+ var warningElt = document.getElementById( 'wpDestFile-warning' );
 148+ if ( !destFile || !warningElt ) return ;
 149+
 150+ this.nameToCheck = destFile.value ;
 151+
 152+ // Clear timer
 153+ if ( this.timeoutID ) {
 154+ window.clearTimeout( this.timeoutID );
 155+ }
 156+ // Check response cache
 157+ for (cached in this.responseCache) {
 158+ if (this.nameToCheck == cached) {
 159+ this.setWarning(this.responseCache[this.nameToCheck]);
 160+ return;
 161+ }
 162+ }
 163+
 164+ this.timeoutID = window.setTimeout( 'wgUploadWarningObj.timeout()', this.delay );
 165+ },
 166+
 167+ 'checkNow': function (fname) {
 168+ if ( !wgAjaxUploadDestCheck || !sajax_init_object() ) return;
 169+ if ( this.timeoutID ) {
 170+ window.clearTimeout( this.timeoutID );
 171+ }
 172+ this.nameToCheck = fname;
 173+ this.timeout();
 174+ },
 175+
 176+ 'timeout' : function() {
 177+ if ( !wgAjaxUploadDestCheck || !sajax_init_object() ) return;
 178+ injectSpinner( document.getElementById( 'wpDestFile' + current ), 'destcheck' );
 179+
 180+ // Get variables into local scope so that they will be preserved for the
 181+ // anonymous callback. fileName is copied so that multiple overlapping
 182+ // ajax requests can be supported.
 183+ var obj = this;
 184+ var fileName = this.nameToCheck;
 185+ sajax_do_call( 'SpecialUpload::ajaxGetExistsWarning', [this.nameToCheck],
 186+ function (result) {
 187+ obj.processResult(result, fileName)
 188+ }
 189+ );
 190+ },
 191+
 192+ 'processResult' : function (result, fileName) {
 193+ removeSpinner( 'destcheck' );
 194+ this.setWarning(result.responseText);
 195+ this.responseCache[fileName] = result.responseText;
 196+ },
 197+
 198+ 'setWarning' : function (warning) {
 199+ var warningElt = document.getElementById( 'wpDestFile-warning' );
 200+ var ackElt = document.getElementsByName( 'wpDestFileWarningAck' );
 201+
 202+ this.setInnerHTML(warningElt, warning);
 203+
 204+ // Set a value in the form indicating that the warning is acknowledged and
 205+ // doesn't need to be redisplayed post-upload
 206+ if ( warning == '' || warning == '&nbsp;' ) {
 207+ ackElt[0].value = '';
 208+ } else {
 209+ ackElt[0].value = '1';
 210+ }
 211+
 212+ },
 213+ 'setInnerHTML' : function (element, text) {
 214+ // Check for no change to avoid flicker in IE 7
 215+ if (element.innerHTML != text) {
 216+ element.innerHTML = text;
 217+ }
 218+ }
 219+}
 220+
 221+function fillDestFilename(id) {
 222+ if (!wgUploadAutoFill) {
7223 return;
8 - var path = document.getElementById('wpUploadFile_' + i).value;
 224+ }
 225+ if (!document.getElementById) {
 226+ return;
 227+ }
 228+ // Remove any previously flagged errors
 229+ var e = document.getElementById( 'mw-upload-permitted' );
 230+ if( e ) e.className = '';
 231+
 232+ var e = document.getElementById( 'mw-upload-prohibited' );
 233+ if( e ) e.className = '';
 234+
 235+
 236+ var path = document.getElementById(id).value;
9237 // Find trailing part
10238 var slash = path.lastIndexOf('/');
11239 var backslash = path.lastIndexOf('\\');
12240 var fname;
13 - if( slash == -1 && backslash == -1 ) {
 241+ if (slash == -1 && backslash == -1) {
14242 fname = path;
15 - } else if( slash > backslash ) {
 243+ } else if (slash > backslash) {
16244 fname = path.substring(slash+1, 10000);
17245 } else {
18246 fname = path.substring(backslash+1, 10000);
19247 }
20248
 249+ // Clear the filename if it does not have a valid extension.
 250+ // URLs are less likely to have a useful extension, so don't include them in the
 251+ // extension check.
 252+ current= id.replace(/[^0-9]/g, "");
 253+ if( wgFileExtensions && id != 'wpUploadFileURL' ) {
 254+ var found = false;
 255+ if( fname.lastIndexOf( '.' ) != -1 ) {
 256+ var ext = fname.substr( fname.lastIndexOf( '.' ) + 1 );
 257+ for( var i = 0; i < wgFileExtensions.length; i++ ) {
 258+ if( wgFileExtensions[i].toLowerCase() == ext.toLowerCase() ) {
 259+ found = true;
 260+ break;
 261+ }
 262+ }
 263+ }
 264+ if( !found ) {
 265+ // Not a valid extension
 266+ // Clear the upload and set mw-upload-permitted to error
 267+ document.getElementById(id).value = '';
 268+ var e = document.getElementById( 'mw-upload-permitted' );
 269+ if( e ) e.className = 'error';
 270+
 271+ var e = document.getElementById( 'mw-upload-prohibited' );
 272+ if( e ) e.className = 'error';
 273+
 274+ // Clear wpDestFile as well
 275+ var e = document.getElementById( 'wpDestFile' + current)
 276+ if( e ) e.value = '';
 277+
 278+ return false;
 279+ }
 280+ }
 281+
21282 // Capitalise first letter and replace spaces by underscores
 283+ // FIXME: $wgCapitalizedNamespaces
22284 fname = fname.charAt(0).toUpperCase().concat(fname.substring(1,10000)).replace(/ /g, '_');
23285
24286 // Output result
25 - var destFile = document.getElementById('wpDestFile_' + i);
26 - if( destFile )
 287+ var destFile = document.getElementById('wpDestFile' + current);
 288+ if (destFile) {
27289 destFile.value = fname;
28 -}
\ No newline at end of file
 290+ wgUploadWarningObj.checkNow(fname) ;
 291+ }
 292+}
 293+
 294+function toggleFilenameFiller() {
 295+ if(!document.getElementById) return;
 296+ var upfield = document.getElementById('wpUploadFile');
 297+ var destName = document.getElementById('wpDestFile').value;
 298+ if (destName=='' || destName==' ') {
 299+ wgUploadAutoFill = true;
 300+ } else {
 301+ wgUploadAutoFill = false;
 302+ }
 303+}
 304+
 305+var wgUploadLicenseObj = {
 306+
 307+ 'responseCache' : { '' : '' },
 308+
 309+ 'fetchPreview': function( license ) {
 310+ if( !wgAjaxLicensePreview ) return;
 311+ for (cached in this.responseCache) {
 312+ if (cached == license) {
 313+ this.showPreview( this.responseCache[license] );
 314+ return;
 315+ }
 316+ }
 317+ injectSpinner( document.getElementById( 'wpLicense' ), 'license' );
 318+
 319+ var title = document.getElementById('wpDestFile' + current).value;
 320+ if ( !title ) title = 'File:Sample.jpg';
 321+
 322+ var url = wgScriptPath + '/api' + wgScriptExtension
 323+ + '?action=parse&text={{' + encodeURIComponent( license ) + '}}'
 324+ + '&title=' + encodeURIComponent( title )
 325+ + '&prop=text&pst&format=json';
 326+
 327+ var req = sajax_init_object();
 328+ req.onreadystatechange = function() {
 329+ if ( req.readyState == 4 && req.status == 200 )
 330+ wgUploadLicenseObj.processResult( eval( '(' + req.responseText + ')' ), license );
 331+ };
 332+ req.open( 'GET', url, true );
 333+ req.send( '' );
 334+ },
 335+
 336+ 'processResult' : function( result, license ) {
 337+ removeSpinner( 'license' );
 338+ this.responseCache[license] = result['parse']['text']['*'];
 339+ this.showPreview( this.responseCache[license] );
 340+
 341+ },
 342+
 343+ 'showPreview' : function( preview ) {
 344+ var previewPanel = document.getElementById( 'mw-license-preview' );
 345+ if( previewPanel.innerHTML != preview )
 346+ previewPanel.innerHTML = preview;
 347+ }
 348+
 349+}
 350+
 351+addOnloadHook( wgUploadSetup );

Follow-up revisions

RevisionCommit summaryAuthorDate
r76115MultiUpload: follow-up to r76108 and r76109 - coding style tweaks and bump ve...ashley16:42, 5 November 2010

Status & tagging log