Misplaced Pages

User:Lupin/popups.js: Difference between revisions

Article snapshot taken from Wikipedia with creative commons attribution-sharealike license. Give it a read and then ask your questions in the chat. We can research this topic together.
< User:Lupin Browse history interactively← Previous editNext edit →Content deleted Content added
Revision as of 05:04, 3 August 2005 view sourceLupin (talk | contribs)19,513 edits image loading improved - can load thumbs, and also commons images← Previous edit Revision as of 16:32, 3 August 2005 view source Lupin (talk | contribs)19,513 edits try to make sourcable from the commonsNext edit →
Line 3: Line 3:
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////


document.write('<script type="text/javascript" src="/search/?title=User:Lupin/md5.js&action=raw&ctype=text/javascript&dontcountme=s"></script>'); document.write('<script type="text/javascript" src="http://en.wikipedia.org/search/?title=User:Lupin/md5.js&action=raw&ctype=text/javascript&dontcountme=s"></script>');


document.write('<script type="text/javascript" src="/search/?title=User:Lupin/overlib.js&action=raw&ctype=text/javascript&dontcountme=s"></script>'); document.write('<script type="text/javascript" src="http://en.wikipedia.org/search/?title=User:Lupin/overlib.js&action=raw&ctype=text/javascript&dontcountme=s"></script>');


// we're not set up for interwiki stuff yet - only affect en links // we're not set up for interwiki stuff yet - only affect en links

Revision as of 16:32, 3 August 2005

////////////////////////////////////////////////////////////////////
// Popup stuff
////////////////////////////////////////////////////////////////////

document.write('<script type="text/javascript" src="http://en.wikipedia.org/search/?title=User:Lupin/md5.js&action=raw&ctype=text/javascript&dontcountme=s"></script>');

document.write('<script type="text/javascript" src="http://en.wikipedia.org/search/?title=User:Lupin/overlib.js&action=raw&ctype=text/javascript&dontcountme=s"></script>');

// we're not set up for interwiki stuff yet - only affect en links

var re=/*:\/\/en\.wikipedia\.org\/w(iki\/|\/index\.php\?title=)(*)/
var exceptions=/((title=|\/)Special:|section=)/
var contributions=/(title=|\/)Special:Contributions&target=(.*)/
var talk=/Talk:/i
var titlebase='http://en.wikipedia.org/search/?title=';

function wikiLink(article, action, text) {
  hint = myDecodeURI(article + '&action=' + action);
  return titledWikiLink(article, action, text, hint);
}

function titledWikiLink(article, action, text, title) {
  var base = titlebase +  article;
  var url=base;
  // no need to add action&view, and this confuses anchors
  if (action != 'view') url = base + '&action=' + action;

  var hint;
  if (title == null || title == '')
    hint = ''
  else 
    hint = 'title="' + title + '"'; 

  return '<a href="' + url + '"' + hint + '>' + text + '</a>';
}

function specialLink(article, specialpage, text) {
  var base = titlebase +  'Special:'+specialpage;
  var url = base + '&target=' + article;
  var hint = myDecodeURI(specialpage+':'+article) ;
  return '<a href="' + url + '" title="' + hint + '">' + text + '</a>';
}

function talkPage(article) {
  if (article.indexOf('Talk:') > -1 || article.indexOf('talk:') > -1 ) 
    return null;

  var i=article.indexOf(':');
  if (i == -1) return 'Talk:'+article;
  else return article.substring(0,i)+'_talk:' + article.substring(i+1);
}

function articleFromTalkPage(talkpage) {
  var i=talkpage.indexOf('Talk:');
  var j=talkpage.indexOf('_talk:');
  if ( i == -1 && j  == -1 ) 
    return null;
  if ( i > -1 ) return talkpage.substring(i+5);
  return talkpage.split('_talk:').join(':');
}

function userName(article) {
  var i=article.indexOf('User');
  var j=article.indexOf(':');
  if  (i != 0 || j < -1) return null;
  var k=article.indexOf('/');
  if (k==-1) return article.substring(j+1);
  else return article.substring(j+1,k);
}

function isInNamespace(article, namespace) {
  var i=article.indexOf(namespace+':');
  var j=article.indexOf(namespace+'_talk:');
  if  (i == -1 && j == -1) return false;
  return true;
}

function stripNamespace(article) {
  // this isn't very sophisticated 
  // it just removes everything up to the final :
  var list = article.split(':');
  return list;
}

function contribsLink(article, text) {
  return specialLink(userName(article), 'Contributions', text);
}

function imagePathComponent(article) {
  if (isInNamespace(article, 'Image')) {
    var stripped=stripNamespace(article);
    var forhash=myDecodeURI(stripped).split(' ').join('_');
    var hash=hex_md5(forhash);
    var pathcpt=hash.substring(0,1) + '/' + hash.substring(0,2) + '/';
    return pathcpt;
  }
  else return null;
}

function imageURL(article, wiki) {
  var imgurl=null;
  if (isInNamespace(article, 'Image')) {
    var pathcpt = imagePathComponent(article);
    var stripped=stripNamespace(article);
    var imgurl="http://upload.wikimedia.org/wikipedia/" + wiki + '/'
            + pathcpt + stripped;
  }
  return imgurl;
}

function imageThumbURL(article, wiki, width) {
  //  
  // eg http://upload.wikimedia.org/wikipedia/en/thumb/6/61/ 
  //           Rubiks_cube_solved.jpg/120px-Rubiks_cube_solved.jpg

  var imgurl=null;
  if (isInNamespace(article, 'Image')) {
    var pathcpt = imagePathComponent(article);
    var stripped=stripNamespace(article);
    var imgurl="http://upload.wikimedia.org/wikipedia/" + wiki + "/thumb/"
            + pathcpt + stripped + '/' + width +"px-" + stripped;
  }
  return imgurl;
}
   
var imageSources=new Array ();

imageSources.push(
                  {wiki: 'en',      thumb: true,  width: 60},
                  {wiki: 'en',      thumb: true,  width: 180},
                  {wiki: 'en',      thumb: true,  width: 120},
                  {wiki: 'en',      thumb: false, width: 0},
                  {wiki: 'commons', thumb: true,  width: 60},
                  {wiki: 'commons', thumb: true,  width: 180},
                  {wiki: 'commons', thumb: true,  width: 120},
                  {wiki: 'commons', thumb: false, width: 0}
                 );

//imageSources.push(
//                  {wiki: 'en',      thumb: true,  width: 120},
//                  {wiki: 'en',      thumb: false, width: 0}
//                 );


var imageArray=new Array();
var popupImageTimer=null;

function loadImages(article) {
 
  if(! isInNamespace(article, 'Image') ) {
    return;
  } 

  for (var i=0; i<imageSources.length; ++i) {

    var url;

    if (imageSources.thumb) 
      url=imageThumbURL(article, imageSources.wiki, imageSources.width);
    else 
      url=imageURL(article, imageSources.wiki);
    
    imageArray=new Image();
    imageArray.src=url;

  }
  if (popupImageTimer != null) {
    clearInterval(popupImageTimer);
    counter=0;
  }
  popupImageTimer=setInterval("checkImages()", 250);
  return;
}

function isImageOk(img)
{   
    // IE test
    if (!img.complete)
        return false;

    // gecko test
    if (typeof img.naturalWidth != "undefined" && img.naturalWidth == 0)
        return false;

    // No other way of checking: assume it's ok.
    return true;
}

var counter=0;
var checkImagesTimer=null;
var loopcounter=0;

function checkImages() {

  if (checkImagesTimer!=null) {
    clearInterval(checkImagesTimer);
    checkImagesTimer=null;
    if (loopcounter > 10); {loopcounter=0; return;}
    loopcounter++;
  } else counter++;

  // document.title=counter + ',' + loopcounter;
  var popupImageStatus=
           document.getElementById("popupImageStatus"+popupImageId);
  if (popupImageStatus != null) {
    var status =  ( counter % 2 ) ? '*' : '.' ;
    popupImageStatus.innerHTML=status;
  }
  


  if (counter > 100) {counter = 0; clearInterval(popupImageTimer);}

  var popupImage=null;
  popupImage=document.getElementById("popupImage"+popupImageId);
  if (popupImage == null) {
    // this doesn't seem to happen any more in practise for some reason
    // still, I'll leave it in
    checkImagesTimer=setInterval("checkImages()",333);
    return;
  }
      
  // get the first image to successfully load
  // and put it in the popupImage
  for(var i = 0; i < imageArray.length; i++) {
    if(isImageOk(imageArray)) {

      clearInterval(popupImageTimer);

      popupImage.src=imageArray.src;
      if (popupImageStatus != null) popupImageStatus.innerHTML = '';
      // reset evil nonconstant globals
      var l=imageArray.length;
      imageArray=null; imageArray=new Array();
      popupImageTimer=null;

      // document.title='got '+hex_md5(popupImage.src)+' after '+counter+' tics';
      counter=0;
      loopcounter=0;
      return popupImage.src; 
    }
  }
}

var popupImageId=0;

function imageHTML(article) {
  var imgurl=''; // imageURL(article, 'en');
  var ret='';

  if (imgurl != null) {
    popupImageId++;
    ret += '<img src="' + imgurl + 
           '" width=60 align="right" valign="top" + id="popupImage' 
           + popupImageId + '"></img>';
  }
  return ret;
}

var kateBase='http://kohl.wikimedia.org/~kate/cgi-bin/count_edits' 
             + '?dbname=enwiki&user='

function kateLink(article, text) {
  return '<a href="' + kateBase + userName(article) + '" title="'
          + text + '">' + text + '</a>';
}


var decodeExtras = new Array ();
decodeExtras = {from: '%2C', to: ',' };
decodeExtras = {from: '_',   to: ' ' };
decodeExtras = {from: '%26',   to: '&' };


function myDecodeURI (str) {
  var ret=decodeURI(str);
  for (var i=0; i<decodeExtras.length; ++i) {
    var from=decodeExtras.from;
    var to=decodeExtras.to;
    ret=ret.split(from).join(to);
  }
  return ret;
}

// defaults
var dpopupDelay=1.5;
var dpopupFgColor='#CCCCFF';
var dpopupBgColor='#333399';

var popupDelay;
var popupFgColor;
var popupBgColor;

var dremoveTitles=true;
var  removeTitles;

function removeAnchor(article) {
  // is there a #? if not, we're done
  var i=article.indexOf('#');
  if (i == -1) return article;

  // head#tail
  var head = article.substring(0,i);
  var tail = article.substring(i+1);

  return head;
}

function mouseOverWikiLink() {
  // FIXME: should not generate the HTML until the delay has elapsed,
  //        and then popup immediately. Can be a CPU hog otherwise.

  var a=this;
  var h=a.href;

  var contribs=contributions.exec(h);
  if (contribs != null) {
    article='User:'+contribs;
  } else {
    var m=re.exec(h);
    article=m;
  }
  var hint=a.originalTitle;
  if (hint == '' || hint == null)
    hint = myDecodeURI(article);

  var html='';
  
  html +=imageHTML(article);

  html+='<b>';
  html+=titledWikiLink(article, 'view', myDecodeURI(article),hint);
  html+='</b>';

  html+='<span id="popupImageStatus'+popupImageId+'"></span>';

  // Get rid of anchor now
  article=removeAnchor(article);

  if (userName(article) != null) {
    html += ' ' + contribsLink(article, 'contribs');
    html += ' ' + kateLink(article, 'count');
  }

  html += '<br>' + wikiLink(article, 'edit', 'edit');
  html += ' ' + wikiLink(article, 'history', 'history');
  html += ' (' + wikiLink(article, 'unwatch', 'un') + ')';
  html += wikiLink(article, 'watch', 'watch');
  var t=talkPage(article);
  if (t != null) html += ' ' + wikiLink(t, 'view', 'talk') + 
                         ' ' + wikiLink(t, 'edit', 'editTalk') +
                         ' ' + wikiLink(t, 'edit&section=new', 'newTopic');
  var ta=articleFromTalkPage(article);
  if (ta != null) html +=' ' + 
                         wikiLink(article, 'edit&section=new', 'newTopic') +
                         ' ' + wikiLink(ta, 'view', 'article') + 
                         ' ' + wikiLink(ta, 'edit', 'editArticle');
                         
  html += '<br>' + specialLink(article, 'Whatlinkshere', 'whatLinksHere');
  html += ' ' + specialLink(article, 'Recentchangeslinked', 'relatedChanges');

  if (popupDelay==null) popupDelay=dpopupDelay;
  if (popupFgColor==null) popupFgColor=dpopupFgColor;
  if (popupBgColor==null) popupBgColor=dpopupBgColor;

  overlib(html, STICKY, MOUSEOFF, WRAP, CELLPAD, 5, 
	  OFFSETX, 2, OFFSETY, 2, DELAY, popupDelay*1000,
          FGCOLOR, popupFgColor, BGCOLOR, popupBgColor);

  loadImages(article);
}

function setupTooltips() {
  var anchors=document.getElementsByTagName('A');
  // alert(anchors.length + 'anchors');
  var s='';
  
  if (removeTitles==null) removeTitles=dremoveTitles;

  for (var i=0; i<anchors.length; ++i)
  {
    var a=anchors;
    var h=a.href;
    var contribs=contributions.exec(h);
    var exc=exceptions.exec(h);
    var m=re.exec(h);
    if (contribs != null || (exc == null && m != null) ) {
      a.onmouseover=mouseOverWikiLink;
      a.onmouseout=function () {  if ( o3_showingsticky == 0 ) cClick(); }
      if (removeTitles) {
        a.originalTitle=a.title;
        a.title='';
      }
    }
  }
}

////////////////////////////////////////////////////////////////////
// Run things
////////////////////////////////////////////////////////////////////

if (window.addEventListener)
  window.addEventListener("load",setupTooltips,false);
else if (window.attachEvent) 
  window.attachEvent("onload",setupTooltips);
else {
  window._old_ABCD_onload = window.onload;
  window.onload = function() {
    window._old_ABCD_onload();
    setupTooltips();
  }
}