Merge "Allow extension of the Special:Upload form"
[lhc/web/wiklou.git] / resources / src / mediawiki.special / mediawiki.special.upload.js
index 0ddf7fe..5934278 100644 (file)
@@ -10,7 +10,7 @@
 /* global Uint8Array */
 
 ( function ( mw, $ ) {
-       var uploadWarning, uploadLicense,
+       var uploadWarning, uploadTemplatePreview,
                ajaxUploadDestCheck = mw.config.get( 'wgAjaxUploadDestCheck' ),
                $license = $( '#wpLicense' );
 
                }
        };
 
-       uploadLicense = {
+       window.wgUploadTemplatePreviewObj = uploadTemplatePreview = {
 
                responseCache: { '': '' },
 
-               fetchPreview: function ( license ) {
-                       var $spinnerLicense;
-                       if ( !mw.config.get( 'wgAjaxLicensePreview' ) ) {
-                               return;
-                       }
-                       if ( this.responseCache.hasOwnProperty( license ) ) {
-                               this.showPreview( this.responseCache[ license ] );
+               /**
+                * @param {jQuery} $element The element whose .val() will be previewed
+                * @param {jQuery} $previewContainer The container to display the preview in
+                */
+               getPreview: function ( $element, $previewContainer ) {
+                       var template = $element.val(),
+                               $spinner;
+
+                       if ( this.responseCache.hasOwnProperty( template ) ) {
+                               this.showPreview( this.responseCache[ template ], $previewContainer );
                                return;
                        }
 
-                       $spinnerLicense = $.createSpinner().insertAfter( '#wpLicense' );
+                       $spinner = $.createSpinner().insertAfter( $element );
 
                        ( new mw.Api() ).get( {
                                formatversion: 2,
                                action: 'parse',
-                               text: '{{' + license + '}}',
+                               text: '{{' + template + '}}',
                                title: $( '#wpDestFile' ).val() || 'File:Sample.jpg',
                                prop: 'text',
-                               pst: true
+                               pst: true,
+                               uselang: mw.config.get( 'wgUserLanguage' )
                        } ).done( function ( result ) {
-                               uploadLicense.processResult( result, license );
+                               uploadTemplatePreview.processResult( result, template, $previewContainer );
                        } ).always( function () {
-                               $spinnerLicense.remove();
+                               $spinner.remove();
                        } );
                },
 
-               processResult: function ( result, license ) {
-                       this.responseCache[ license ] = result.parse.text;
-                       this.showPreview( this.responseCache[ license ] );
+               processResult: function ( result, template, $previewContainer ) {
+                       this.responseCache[ template ] = result.parse.text;
+                       this.showPreview( this.responseCache[ template ], $previewContainer );
                },
 
-               showPreview: function ( preview ) {
-                       $( '#mw-license-preview' ).html( preview );
+               showPreview: function ( preview, $previewContainer ) {
+                       $previewContainer.html( preview );
                }
 
        };
                        // License selector check
                        $license.change( function () {
                                // We might show a preview
-                               uploadLicense.fetchPreview( $license.val() );
+                               uploadTemplatePreview.getPreview( $license, $( '#mw-license-preview' ) );
                        } );
 
                        // License selector table row
                                if (
                                        mw.config.get( 'wgCheckFileExtensions' ) &&
                                        mw.config.get( 'wgStrictFileExtensions' ) &&
-                                       mw.config.get( 'wgFileExtensions' ) &&
+                                       Array.isArray( mw.config.get( 'wgFileExtensions' ) ) &&
                                        $( this ).attr( 'id' ) !== 'wpUploadFileURL'
                                ) {
                                        if (
                                                fname.lastIndexOf( '.' ) === -1 ||
-                                               $.inArray(
-                                                       fname.slice( fname.lastIndexOf( '.' ) + 1 ).toLowerCase(),
-                                                       $.map( mw.config.get( 'wgFileExtensions' ), function ( element ) {
-                                                               return element.toLowerCase();
-                                                       } )
-                                               ) === -1
+                                               mw.config.get( 'wgFileExtensions' ).map( function ( element ) {
+                                                       return element.toLowerCase();
+                                               } ).indexOf( fname.slice( fname.lastIndexOf( '.' ) + 1 ).toLowerCase() ) === -1
                                        ) {
                                                // Not a valid extension
                                                // Clear the upload and set mw-upload-permitted to error
                function fileIsPreviewable( file ) {
                        var known = [ 'image/png', 'image/gif', 'image/jpeg', 'image/svg+xml' ],
                                tooHuge = 10 * 1024 * 1024;
-                       return ( $.inArray( file.type, known ) !== -1 ) && file.size > 0 && file.size < tooHuge;
+                       return ( known.indexOf( file.type ) !== -1 ) && file.size > 0 && file.size < tooHuge;
                }
 
                /**
                        allowCloseWindow.release();
                } );
        } );
+
+       // Add tabindex to mw-editTools
+       $( function () {
+               // Function to change tabindex for all links within mw-editTools
+               function setEditTabindex( $val ) {
+                       $( '.mw-editTools' ).find( 'a' ).each( function () {
+                               $( this ).attr( 'tabindex', $val );
+                       } );
+               }
+
+               // Change tabindex to 0 if user pressed spaced or enter while focused
+               $( '.mw-editTools' ).on( 'keypress', function ( e ) {
+                       // Don't continue if pressed key was not enter or spacebar
+                       if ( e.which !== 13 && e.which !== 32 ) {
+                               return;
+                       }
+
+                       // Change tabindex only when main div has focus
+                       if ( $( this ).is( ':focus' ) ) {
+                               $( this ).find( 'a' ).first().focus();
+                               setEditTabindex( '0' );
+                       }
+               } );
+
+               // Reset tabindex for elements when user focused out mw-editTools
+               $( '.mw-editTools' ).on( 'focusout', function ( e ) {
+                       // Don't continue if relatedTarget is within mw-editTools
+                       if ( e.relatedTarget !== null && $( e.relatedTarget ).closest( '.mw-editTools' ).length > 0 ) {
+                               return;
+                       }
+
+                       // Reset tabindex back to -1
+                       setEditTabindex( '-1' );
+               } );
+
+               // Set initial tabindex for mw-editTools to 0 and to -1 for all links
+               $( '.mw-editTools' ).attr( 'tabindex', '0' );
+               setEditTabindex( '-1' );
+       } );
 }( mediaWiki, jQuery ) );