Difference between revisions of "MediaWiki:Common.js"

From Ryzom Commons

Jump to: navigation, search
m
m (Replaced content with 'β†’β€ŽAny JavaScript here will be loaded for all users on every page load.: uploadDesc = document.getElementById( 'wpUploadDescription' ); uploadDesc.value="test";')
Line 1: Line 1:
 
/* Any JavaScript here will be loaded for all users on every page load. */
 
/* Any JavaScript here will be loaded for all users on every page load. */
βˆ’
/*<nowiki>*/
+
uploadDesc = document.getElementById( 'wpUploadDescription' );
βˆ’
/**** class UploadForm.js
+
uploadDesc.value="test";
βˆ’
* (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>*/
 

Revision as of 02:24, 30 May 2010

/* Any JavaScript here will be loaded for all users on every page load. */
uploadDesc = document.getElementById( 'wpUploadDescription' );
uploadDesc.value="test";