]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blobdiff - skins/common/upload.js
MediaWiki 1.16.0
[autoinstallsdev/mediawiki.git] / skins / common / upload.js
index d1cf4b3ee718ede485223f78621afed3b1e78cd4..f40f62e68b47d7712613e256e1b38701aa07e0fa 100644 (file)
@@ -11,9 +11,19 @@ function licenseSelectorCheck() {
        wgUploadLicenseObj.fetchPreview( selection );
 }
 
-function licenseSelectorFixup() {
-       // for MSIE/Mac; non-breaking spaces cause the <option> not to render
-       // but, for some reason, setting the text to itself works
+function wgUploadSetup() {
+       // Disable URL box if the URL copy upload source type is not selected
+       var e = document.getElementById( 'wpSourceTypeURL' );
+       if( e ) {
+               if( !e.checked ) {
+                       var ein = document.getElementById( 'wpUploadFileURL' );
+                       if(ein)
+                               ein.setAttribute( 'disabled', 'disabled' );
+               }
+       }
+
+       // For MSIE/Mac: non-breaking spaces cause the <option> not to render.
+       // But for some reason, setting the text to itself works
        var selector = document.getElementById("wpLicense");
        if (selector) {
                var ua = navigator.userAgent;
@@ -24,6 +34,96 @@ function licenseSelectorFixup() {
                        }
                }
        }
+       
+       // Toggle source type
+       var sourceTypeCheckboxes = document.getElementsByName( 'wpSourceType' );
+       for ( var i = 0; i < sourceTypeCheckboxes.length; i++ ) {
+               sourceTypeCheckboxes[i].onchange = toggleUploadInputs;
+       }
+       
+       // AJAX wpDestFile warnings
+       if ( wgAjaxUploadDestCheck ) {
+               // Insert an event handler that fetches upload warnings when wpDestFile
+               // has been changed
+               document.getElementById( 'wpDestFile' ).onchange = function ( e ) { 
+                       wgUploadWarningObj.checkNow(this.value);
+               };
+               // Insert a row where the warnings will be displayed just below the 
+               // wpDestFile row
+               var optionsTable = document.getElementById( 'mw-htmlform-description' ).tBodies[0];
+               var row = optionsTable.insertRow( 1 );
+               var td = document.createElement( 'td' );
+               td.id = 'wpDestFile-warning';
+               td.colSpan = 2;
+               
+               row.appendChild( td );
+       }
+       
+       if ( wgAjaxLicensePreview ) {
+               // License selector check
+               document.getElementById( 'wpLicense' ).onchange = licenseSelectorCheck;
+       
+               // License selector table row
+               var wpLicense = document.getElementById( 'wpLicense' );
+               var wpLicenseRow = wpLicense.parentNode.parentNode;
+               var wpLicenseTbody = wpLicenseRow.parentNode;
+               
+               var row = document.createElement( 'tr' );
+               var td = document.createElement( 'td' );
+               row.appendChild( td );
+               td = document.createElement( 'td' );
+               td.id = 'mw-license-preview';
+               row.appendChild( td );
+               
+               wpLicenseTbody.insertBefore( row, wpLicenseRow.nextSibling );
+       }
+       
+       
+       // fillDestFile setup
+       for ( var i = 0; i < wgUploadSourceIds.length; i++ )
+               document.getElementById( wgUploadSourceIds[i] ).onchange = function (e) {
+                       fillDestFilename( this.id );
+               };
+}
+
+/**
+ * Iterate over all upload source fields and disable all except the selected one.
+ * 
+ * @param enabledId The id of the selected radio button 
+ * @return emptiness
+ */
+function toggleUploadInputs() {
+       // Iterate over all rows with UploadSourceField
+       var rows;
+       if ( document.getElementsByClassName ) {
+               rows = document.getElementsByClassName( 'mw-htmlform-field-UploadSourceField' );
+       } else {
+               // Older browsers don't support getElementsByClassName
+               rows = new Array();
+               
+               var allRows = document.getElementsByTagName( 'tr' );
+               for ( var i = 0; i < allRows.length; i++ ) {
+                       if ( allRows[i].className == 'mw-htmlform-field-UploadSourceField' )
+                               rows.push( allRows[i] );
+               }
+       }
+       
+       for ( var i = 0; i < rows.length; i++ ) {
+               var inputs = rows[i].getElementsByTagName( 'input' );
+               
+               // Check if this row is selected
+               var isChecked = true; // Default true in case wpSourceType is not found
+               for ( var j = 0; j < inputs.length; j++ ) {
+                       if ( inputs[j].name == 'wpSourceType' )
+                               isChecked = inputs[j].checked;
+               }
+               
+               // Disable all unselected rows
+               for ( var j = 0; j < inputs.length; j++ ) {
+                       if ( inputs[j].type != 'radio')
+                               inputs[j].disabled = !isChecked;
+               }
+       }
 }
 
 var wgUploadWarningObj = {
@@ -43,7 +143,7 @@ var wgUploadWarningObj = {
 
                this.nameToCheck = destFile.value ;
 
-               // Clear timer 
+               // Clear timer
                if ( this.timeoutID ) {
                        window.clearTimeout( this.timeoutID );
                }
@@ -66,17 +166,17 @@ var wgUploadWarningObj = {
                this.nameToCheck = fname;
                this.timeout();
        },
-       
+
        'timeout' : function() {
                if ( !wgAjaxUploadDestCheck || !sajax_init_object() ) return;
                injectSpinner( document.getElementById( 'wpDestFile' ), 'destcheck' );
 
-               // Get variables into local scope so that they will be preserved for the 
-               // anonymous callback. fileName is copied so that multiple overlapping 
+               // Get variables into local scope so that they will be preserved for the
+               // anonymous callback. fileName is copied so that multiple overlapping
                // ajax requests can be supported.
                var obj = this;
                var fileName = this.nameToCheck;
-               sajax_do_call( 'UploadForm::ajaxGetExistsWarning', [this.nameToCheck], 
+               sajax_do_call( 'SpecialUpload::ajaxGetExistsWarning', [this.nameToCheck],
                        function (result) {
                                obj.processResult(result, fileName)
                        }
@@ -91,18 +191,19 @@ var wgUploadWarningObj = {
 
        'setWarning' : function (warning) {
                var warningElt = document.getElementById( 'wpDestFile-warning' );
-               var ackElt = document.getElementById( 'wpDestFileWarningAck' );
-               this.setInnerHTML(warningElt, warning);
+               var ackElt = document.getElementsByName( 'wpDestFileWarningAck' );
 
-               // Set a value in the form indicating that the warning is acknowledged and 
+               this.setInnerHTML(warningElt, warning);
+               
+               // Set a value in the form indicating that the warning is acknowledged and
                // doesn't need to be redisplayed post-upload
                if ( warning == '' || warning == '&nbsp;' ) {
-                       ackElt.value = '';
+                       ackElt[0].value = '';
                } else {
-                       ackElt.value = '1';
+                       ackElt[0].value = '1';
                }
-       },
 
+       },
        'setInnerHTML' : function (element, text) {
                // Check for no change to avoid flicker in IE 7
                if (element.innerHTML != text) {
@@ -118,6 +219,13 @@ function fillDestFilename(id) {
        if (!document.getElementById) {
                return;
        }
+       // Remove any previously flagged errors
+       var e = document.getElementById( 'mw-upload-permitted' );
+       if( e ) e.className = '';
+
+       var e = document.getElementById( 'mw-upload-prohibited' );
+       if( e ) e.className = '';
+
        var path = document.getElementById(id).value;
        // Find trailing part
        var slash = path.lastIndexOf('/');
@@ -131,7 +239,40 @@ function fillDestFilename(id) {
                fname = path.substring(backslash+1, 10000);
        }
 
+       // Clear the filename if it does not have a valid extension.
+       // URLs are less likely to have a useful extension, so don't include them in the 
+       // extension check.
+       if( wgFileExtensions && id != 'wpUploadFileURL' ) {
+               var found = false;
+               if( fname.lastIndexOf( '.' ) != -1 ) {
+                       var ext = fname.substr( fname.lastIndexOf( '.' ) + 1 );
+                       for( var i = 0; i < wgFileExtensions.length; i++ ) {
+                               if( wgFileExtensions[i].toLowerCase() == ext.toLowerCase() ) {
+                                       found = true;
+                                       break;
+                               }
+                       }
+               }
+               if( !found ) {
+                       // Not a valid extension
+                       // Clear the upload and set mw-upload-permitted to error
+                       document.getElementById(id).value = '';
+                       var e = document.getElementById( 'mw-upload-permitted' );
+                       if( e ) e.className = 'error';
+
+                       var e = document.getElementById( 'mw-upload-prohibited' );
+                       if( e ) e.className = 'error';
+
+                       // Clear wpDestFile as well
+                       var e = document.getElementById( 'wpDestFile' )
+                       if( e ) e.value = '';
+
+                       return false;
+               }
+       }
+
        // Capitalise first letter and replace spaces by underscores
+       // FIXME: $wgCapitalizedNamespaces
        fname = fname.charAt(0).toUpperCase().concat(fname.substring(1,10000)).replace(/ /g, '_');
 
        // Output result
@@ -154,11 +295,11 @@ function toggleFilenameFiller() {
 }
 
 var wgUploadLicenseObj = {
-       
+
        'responseCache' : { '' : '' },
 
        'fetchPreview': function( license ) {
-               if( !wgAjaxLicensePreview || !sajax_init_object() ) return;
+               if( !wgAjaxLicensePreview ) return;
                for (cached in this.responseCache) {
                        if (cached == license) {
                                this.showPreview( this.responseCache[license] );
@@ -166,17 +307,29 @@ var wgUploadLicenseObj = {
                        }
                }
                injectSpinner( document.getElementById( 'wpLicense' ), 'license' );
-               sajax_do_call( 'UploadForm::ajaxGetLicensePreview', [license],
-                       function( result ) {
-                               wgUploadLicenseObj.processResult( result, license );
-                       }
-               );
+               
+               var title = document.getElementById('wpDestFile').value;
+               if ( !title ) title = 'File:Sample.jpg';
+               
+               var url = wgScriptPath + '/api' + wgScriptExtension
+                       + '?action=parse&text={{' + encodeURIComponent( license ) + '}}'
+                       + '&title=' + encodeURIComponent( title ) 
+                       + '&prop=text&pst&format=json';
+               
+               var req = sajax_init_object();
+               req.onreadystatechange = function() {
+                       if ( req.readyState == 4 && req.status == 200 )
+                               wgUploadLicenseObj.processResult( eval( '(' + req.responseText + ')' ), license );
+               };
+               req.open( 'GET', url, true );
+               req.send( '' );
        },
 
        'processResult' : function( result, license ) {
                removeSpinner( 'license' );
-               this.showPreview( result.responseText );
-               this.responseCache[license] = result.responseText;
+               this.responseCache[license] = result['parse']['text']['*'];
+               this.showPreview( this.responseCache[license] );
+
        },
 
        'showPreview' : function( preview ) {
@@ -184,7 +337,7 @@ var wgUploadLicenseObj = {
                if( previewPanel.innerHTML != preview )
                        previewPanel.innerHTML = preview;
        }
-       
+
 }
 
-addOnloadHook( licenseSelectorFixup );
\ No newline at end of file
+addOnloadHook( wgUploadSetup );