/* Any JavaScript here will be loaded for all users on every page load. */
 /*<nowiki>*/
/**** class UploadForm.js
 * (c) 2008 by Patrick Westerhoff [poke] and Johan Sköld [Galil]
 */

function UploadForm ()
{
  /** setting start **/
  var settingRows = {
    'Image use' : [
      { id:    'uploadGeneralImage',
        name:  'uploadImageUse',
        type:  'radio',
        label: 'General use',
        value: 'general',
        def:   true },
      { id:    'uploadUserImage',
        name:  'uploadImageUse',
        type:  'radio',
        label: 'User space',
        value: 'user' },
      { id:    'uploadGuildImage',
        name:  'uploadImageUse',
        type:  'radio',
        label: 'Guild space',
        value: 'guild' }
    ],
    'Licensing settings': [
      { id:    'uploadGFDL',
        name:  'uploadLicensing',
        type:  'radio',
        label: 'Normal image',
        value: 'gfdl',
        def:   true },
      { id:    'uploadArenanetImg',
        name:  'uploadLicensing',
        type:  'radio',
        label: 'ArenaNet owned image',
        value: 'arenanetimage' },
      { id:    'uploadScreenshot',
        name:  'uploadLicensing',
        type:  'radio',
        label: 'Guild Wars screenshot',
        value: 'screenshot' }
    ]
  }
  var rulesNaming = {
    'uploadUserImage':
    { expr:    new RegExp( '^[Uu]ser[-_ ]([^-_ ]+)[-_ ](.+?)' ),
      replace: 'User_' + ( wgUserName ? wgUserName.replace( ' ', '_' ) : '' ) + '_$1',
      message: 'Invalid user image filename. It has been modified to conform with our <a href="/wiki/Guild_Wars_Wiki:Image_use#User_page_images" title="Guild Wars Wiki:Image use">image use policy</a>. Please click "Upload file" again to upload.'
    },
    'uploadGuildImage':
    { expr:    new RegExp( '^[Gg]uild[-_ ]([^-_ ]+)[-_ ](.+?)' ),
      message: 'Invalid guild image filename. Please name it according to our <a href="/wiki/Guild_Wars_Wiki:Image_use#Guild_images" title="Guild Wars Wiki:Image use">image use policy</a>, such as: <code>Guild_<strong>Guild_Name</strong>_$1</code>.'
    }
  }
  var rulesContent = {
    'uploadGeneralImage':
    { expr:    new RegExp( '' ),
      tag:     ''
    },
    'uploadUserImage':
    { expr:    new RegExp( '(\\{\\{\\s*?(Template:)?[Uu]ser image\\s*?(\|.*?)?\\}\\}\\s*)', 'g' ),
      tag:     '{{user image|' + wgUserName + '}}\n'
    },
    'uploadGuildImage':
    { expr:    new RegExp( '(\\{\\{\\s*?(Template:)?[Gg]uild image\\s*?(\|.*?)?\\}\\}\\s*)', 'g' ),
      tag:     '{{guild image}}\n'
    },
    'uploadGFDL':
    { expr:    new RegExp( '' ),
      tag:     ''
    },
    'uploadArenanetImg':
    { expr:    new RegExp( '(\\{\\{\\s*?(Template:)?[Aa]renanet image\\s*?\\}\\}\\s*)', 'g' ),
      tag:     '{{arenanet image}}\n'
    },
    'uploadScreenshot':
    { expr:    new RegExp( '(\\{\\{\\s*?(Template:)?[Ss]creenshot\\s*?\\}\\}\\s*)', 'g' ),
      tag:     '{{screenshot}}\n'
    }
  }
  /** settings end **/
  
  var uploadForm;
  var uploadDesc;
  var uploadFilename;
  var uploadMessage = document.createElement( 'div' );
  var reTrim        = new RegExp( '^(\\s*)(.*?)(\\s*)$' );
  
  uploadMessage.id  = 'uploadMessage';
  uploadMessage.style.color     = '#CC0000';
  uploadMessage.style.fontStyle = 'italic';
  uploadMessage.style.marginTop = '0.3em';
  
  initialize();
  
  /** private void initialize () **/
  function initialize ()
  {
    if ( wgPageName != 'Special:Upload' || document.getElementById( 'mw-upload-form' ) == null )
      return;
    
    uploadForm     = document.getElementById( 'mw-upload-form' );
    uploadDesc     = document.getElementById( 'wpUploadDescription' );
    uploadFilename = document.getElementById( 'wpDestFile' );
    
    for ( var row in settingRows )
    {
      var tRow  = document.createElement( 'tr' );
      var tCel1 = document.createElement( 'td' );
      var tCel2 = document.createElement( 'td' );
      
      for ( var key in settingRows[row] )
      {
        configElement = settingRows[row][key];
        var input     = null;
        
        try
        {
          input = document.createElement( '<input name="' + configElement.name + '" />' );
        }
        catch ( e )
        {}
        
        if ( !input )
        {
          input      = document.createElement( 'input' );
          input.name = configElement.name;
        }
        
        input.type    = configElement.type;
        input.value   = configElement.value;
        input.id      = configElement.id;
        input.checked = configElement.def;
        
        var label     = document.createElement( 'label' );
        label.htmlFor = configElement.id;
        label.appendChild( document.createTextNode( configElement.label ) );
        
        if ( rulesContent[ configElement.id ] != null || configElement.type == 'radio' )
          input.onclick = applyInput;
        
        tCel2.appendChild( input );
        tCel2.appendChild( label );
        tCel2.appendChild( document.createTextNode( ' ' ) );
      }
      
      tCel1.style.textAlign = 'right';
      tCel1.appendChild( document.createTextNode( row + ':' ) );
      tRow.appendChild( tCel1 );
      tRow.appendChild( tCel2 );
      
      var position = uploadDesc.parentNode.parentNode;
      position.parentNode.insertBefore( tRow, position );
    }
    
    uploadDesc.onkeyup  = redrawInput;
    uploadForm.onsubmit = checkInput;
    
    if ( uploadFilename.value != '' )
    {
      for ( var inputId in rulesNaming )
      {
        if ( rulesContent[inputId] == null || uploadFilename.value.search( rulesNaming[ inputId ].expr ) < 0 )
          continue;
        
        uploadDesc.value = rulesContent[ inputId ].tag + uploadDesc.value;
        break;
      }
      redrawInput();
    }
  }
    
  /** private void applyInput () **/
    function applyInput ()
  {
    var inputGroup = document.getElementsByName( this.name );
    
    for ( var i = 0; i < inputGroup.length; i++ )
    {
      if ( inputGroup[i].nodeName.toLowerCase() != 'input' || rulesContent[ inputGroup[i].id ] == null )
        continue;
      
      var inputId   = inputGroup[i].id;
      var ruleMatch = uploadDesc.value.search( rulesContent[ inputId ].expr );
      
      if ( !inputGroup[i].checked )
        uploadDesc.value = uploadDesc.value.replace( rulesContent[ inputId ].expr, '' );
      else if ( ruleMatch < 0 )
        uploadDesc.value = rulesContent[ inputId ].tag + uploadDesc.value;
    }
  }
  
  /** private void redrawInput () **/
  function redrawInput ()
  {
    for ( var id in rulesContent )
    {
      var input = document.getElementById( id );
        
      if ( input.nodeName.toLowerCase() != 'input' )
        continue;
      
      input.checked = !( uploadDesc.value.search( rulesContent[id].expr ) < 0 );
    }
  }
  
  /** private void checkInput ( event ) **/
  function checkInput ( e )
  {
    if ( !e )
      var e = window.event;
      
    if ( document.getElementById( 'uploadMessage' ) == null )
      uploadFilename.parentNode.appendChild( uploadMessage );
    else
      uploadMessage.innerHTML = '';
    
    var messages = new Array();
    
    for ( var inputId in rulesNaming )
    {
      var input = document.getElementById( inputId );
        
      if ( input == null || input.nodeName.toLowerCase() != 'input' || !input.checked )
        continue;
      
      if ( uploadFilename.value.search( rulesNaming[ inputId ].expr ) < 0 )
      {
        messages.push( uploadFilename.value.replace( /^(.*?)$/, rulesNaming[ inputId ].message ) );
        
        if ( rulesNaming[ inputId ].replace != null )
          uploadFilename.value = uploadFilename.value.replace( /^(.*?)$/, rulesNaming[ inputId ].replace );
      }
    }
    
    for ( var inputId in rulesContent )
    {
      var input = document.getElementById( inputId );
      
      if ( input == null || input.nodeName.toLowerCase() != 'input' )
        continue;
      
      var ruleMatch  = uploadDesc.value.search( rulesContent[ inputId ].expr );
      
      if ( !input.checked )
        uploadDesc.value = uploadDesc.value.replace( rulesContent[ inputId ].expr, '' );
      else if ( ruleMatch < 0 )
        uploadDesc.value = rulesContent[ inputId ].tag + uploadDesc.value;
      else
      {
        ruleMatch = uploadDesc.value.match( rulesContent[ inputId ].expr );
        
        if ( ruleMatch.length > 1 )
        {
          uploadDesc.value = rulesContent[ inputId ].tag + uploadDesc.value.replace( rulesContent[ inputId ].expr, '' );
        }
      }
    }
    
    if ( messages.length > 0 )
    {
      uploadMessage.innerHTML = messages.join( '<br />' );
      
      if ( e.preventDefault )
        e.preventDefault();
      
      e.returnValue = false;  
    }
  }
}
UploadForm();
/*</nowiki>*/
Retrieved from "https://atys.wiki.ryzom.com/w/index.php?title=MediaWiki:Common.js&oldid=13938"