Difference between revisions of "MediaWiki:QIvoter.js"

From Wikimedia Commons, the free media repository
Jump to: navigation, search
("ReferenceError: Can't find variable: QInominator. Aborting"; JSHint validation: 0 errors. passed 100% (had a missing semi colon))
m (typeof is just ugly (string comparison... please))
Line 184: Line 184:
 
for( var i = 0; i < cookies.length; i++ )
 
for( var i = 0; i < cookies.length; i++ )
 
{
 
{
if( typeof QInominator !== 'undefined' && cookies[i].indexOf(QInominator.cookiename+'=') === 0 )
+
if( window.QInominator && cookies[i].indexOf(QInominator.cookiename+'=') === 0 )
 
{
 
{
 
QIVoter.storednom = decodeURIComponent( cookies[i].substr( (QIVoter.cookiename+'=').length ) );
 
QIVoter.storednom = decodeURIComponent( cookies[i].substr( (QIVoter.cookiename+'=').length ) );

Revision as of 17:00, 23 February 2011

var QIVoter = {
 // configuration defaults
 thumbSize : 100,
 
 // working variables and constants
 b1 : null,
 b2 : null,
 file : null,
 imfileRE : /^\s*([Ii]mage:|[Ff]ile:)([^\|]*)/,
 newn : /\{\{\/[Nn]omination\|/,
 nomi : /\{\{\/([Nn]omination|[Pp]romotion|[Dd]ecline)\|/,
 disc : /\{\{\/([Pp]romotion|[Dd]ecline)\|/,
 tb : null,
 report : null,
 rcache : {},
 cookiename : 'qinomtext',
 storednom : '',
 
 getPos : null,
 getPosSane : function()
 {
  return QIVoter.tb.selectionStart;
 },
 getPosInsane : function() 
 { 
  var tb = QIVoter.tb;
  if( document.selection )
  { 
   tb.focus(); 
   var r = document.selection.createRange(); 
   if (r === null) return 0; 
 
   var re = tb.createTextRange(), rc = re.duplicate(); 
   re.moveToBookmark(r.getBookmark()); 
   rc.setEndPoint('EndToStart', re); 
   return rc.text.length; 
  }  
  return 0; 
 },
 analyzeCallback : function( file, data )
 {
  QIVoter.rcache[encodeURIComponent(file)] = data;
  QIVoter.analyze(data);
 },
 analyze : function( data )
 {
  var flaws = '', report = '';
  if( data !== null )
  {
   var megapixel = Math.round( ( 100 * data.width * data.height ) / (1024*1024) ) / 100;
   var megabyte = Math.round( ( 100 * data.size ) / (1024*1024) ) / 100;
   if( megapixel < 2.0 ) flaws += '<b>Below minimum size requirement!</b><br/>';
 
   report = 'Size: ' + data.width + '×' + data.height + 'px = ' + megapixel + 'MP at ' + megabyte + 'MB<br/>' + flaws;
  }
  QIVoter.report.innerHTML = report;
 },
 cursorHandler : function()
 {
  // analyze current line
  var tb = QIVoter.tb;
  var cp = QIVoter.getPos();
  var a1 = tb.value.lastIndexOf('\n',cp-1);
  var a2 = tb.value.indexOf('\n',cp);
 
  // new line?
  if( a1 !== QIVoter.b1 || a2 !== QIVoter.b2 )
  {
   // first hide the old examine box
   $('#qivexaminebox').hide('fast');
 
   QIVoter.b1 = a1;
   QIVoter.b2 = a2;
   QIVoter.line = tb.value.substr(a1,a2-a1);
   if(QIVoter.imfileRE.test(QIVoter.line))
   {
    QIVoter.file = encodeURIComponent( QIVoter.imfileRE.exec(QIVoter.line)[2] );
    // request a report on the image
    if( QIVoter.file in QIVoter.rcache ) 
     QIVoter.analyze( QIVoter.rcache[QIVoter.file] );
    else
     $.getScript( 'http://toolserver.org/~dschwen/qivoter/analyze.php?f=' + QIVoter.file );
 
    // now update everything else
    $('#qivlink').attr( 'href', 'http://commons.wikimedia.org/wiki/File:' + QIVoter.file );
    $('#qivthumb').attr( 'src', 'http://commons.wikimedia.org/w/thumb.php?w=' + QIVoter.thumbSize + '&f=' + QIVoter.file );
    if(QIVoter.nomi.test(QIVoter.line))
    {
     if(QIVoter.newn.test(QIVoter.line))
     {
      $('#qivpromote').attr('disabled', false);
      $('#qivdecline').attr('disabled', false);
      $('#qivdiscuss').attr('disabled', true);
     }
     else
     {
      $('#qivpromote').attr('disabled', true);
      $('#qivdecline').attr('disabled', true);
      $('#qivdiscuss').attr('disabled', false);
     }
    }
    else
    {
     $('#qivpromote').attr('disabled', true);
     $('#qivdecline').attr('disabled', true);
     $('#qivdiscuss').attr('disabled', true);
    }
    $('#qivbox').show('fast');
   }
   else
    $('#qivbox').hide('fast');
  }
 },
 examine : function()
 {
  $('#qiviframe').attr('src','http://commons.wikimedia.org/w/index.php?title=Special%3AFilePath&file=' + QIVoter.file );
  $('#qivexaminebox').show('fast');
  return false;
 },
 tag : function( name, separator, caption, defaulttext )
 {
  var pnumRE = /\|[12]=\s*/g;
  var tendRE = /\|{0,1}\s*\}\}\s*$/;
  var tb = QIVoter.tb;
  var pre = tb.value.substr(0,QIVoter.b1);
  var post = tb.value.substr(QIVoter.b2);
  var boilerplate = prompt( caption, defaulttext );
 
  if( boilerplate === null ) return;
  boilerplate = separator + boilerplate;
 
  $('#qivexaminebox').hide('fast');
  $('#qivbox').hide('fast');
 
  // remove superfluous parameter numbers
  QIVoter.line = QIVoter.line.replace(pnumRE,'|');
 
  // tag
  if( QIVoter.nomi.test(QIVoter.line) && tendRE.test(QIVoter.line) )
    QIVoter.line = QIVoter.line.replace(tendRE, boilerplate + ' --~~'+'~~}}').replace(QIVoter.nomi,'{{/' +  name + '|');
 
  tb.value = pre + QIVoter.line + post;
  QIVoter.b1 = null;
  QIVoter.tb.focus();
 },
 promote : function() { QIVoter.tag('Promotion','|','Promote reason','Good quality.'); return false; },
 decline : function() { QIVoter.tag('Decline','|','Decline reason','Insufficient quality.'); return false; },
 discuss : function() { QIVoter.tag('Discuss','<br/>','Discuss reason','I disagree '); return false; },
 activate : function()
 {
  QIVoter.report = $('#qivreport')[0];
  $('#qivactivate').hide('fast');
  $('#qivexamine').click(QIVoter.examine);
  $('#qivpromote').click(QIVoter.promote);
  $('#qivdecline').click(QIVoter.decline);
  $('#qivdiscuss').click(QIVoter.discuss);
  $('#wpTextbox1').keyup(QIVoter.cursorHandler).click(QIVoter.cursorHandler);
 },
 insertNom : function()
 {
  var galleryRE = /<[Gg]allery[^>]*>/;
  if( galleryRE.test(QIVoter.tb.value) )
  {
   var galtag = galleryRE.exec(QIVoter.tb.value);
   QIVoter.tb.value = QIVoter.tb.value.replace( galleryRE, galtag + '\n' + QIVoter.storednom );
  }
  else
  {
   QIVoter.tb.value = '<gallery>\n' + QIVoter.storednom + '</gallery>';
  }
  $('#qivnom').hide('fast');
  document.cookie = QIVoter.cookiename + '=; path=/; expires=Thu, 01-Jan-1970 00:00:01 GMT';
 },
 install : function()
 {
  QIVoter.tb = $('#wpTextbox1')[0];
  if( typeof(QIVoter.tb.selectionStart) == 'number' )
   QIVoter.getPos = QIVoter.getPosSane;
  else
   QIVoter.getPos = QIVoter.getPosInsane;
 
  // get existing nomination cookie
  var cookies = document.cookie.split("; ");
  for( var i = 0; i < cookies.length; i++ )
  {
   if( window.QInominator && cookies[i].indexOf(QInominator.cookiename+'=') === 0 )
   {
    QIVoter.storednom = decodeURIComponent( cookies[i].substr( (QIVoter.cookiename+'=').length ) );
   }
  }
 
  $('#wpTextbox1').before('<div style="display:none" id="qivbox"><a id="qivlink"><img id="qivthumb" style="float:left"/>'
   + '</a> <input id="qivexamine" type="button" value="Examine"> <input id="qivpromote"  type="button" value="Promote">'
   + ' <input id="qivdecline" type="button" value="Decline"> <input id="qivdiscuss" type="button" value="Discuss">'
   + '<div id="qivreport" style="padding-left: 1em; line-height: 1"></div><br style="clear:both"/></div>'
   + '<div id="qivactivate" style="line-height: 1; background-color: red; color: white;'
   + ' padding: 0.5em">Click here to activate QIVoter helper!</div>'
   + '<div id="qivnom" style="line-height: 1; background-color: #008800; color: white;'
   + ' display:none; padding: 0.5em">Click here to insert your stored nomination!</div>'
   + '<div style="display:none;'
   + ' height: 400px" id="qivexaminebox"><iframe id="qiviframe" style="width: 100%; height: 400px; border: 0px">'
   + '</iframe><div>');
 
  if( QIVoter.storednom !== '' ) 
   $('#qivnom').show().click(QIVoter.insertNom);
 
  $('#qivactivate').click(QIVoter.activate);
 }
};
 
if( wgTitle == "Quality images candidates/candidate list" && wgAction == 'edit' ) 
 $(document).ready(QIVoter.install);