Jump to content

User:Ais523/contribcalendar.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
// Contributions calendar script ([[User:ais523/contribcalendar.js]]).

// <pre><nowiki>

// To use, install this script, and update the calendar by clicking the 'calendar' tab
// whilst viewing your userpage. It creates a user subpage for you (in 'Show Preview'
// mode, so you can choose not to save it if you don't like it). If you do this on
// someone else's userpage, it will create a calendar as a subpage for them (so don't
// do it there without permission). You can place the calendar on your userpage by
// typing {{User:ais523/ContribCalendar}} (use your username instead of mine!), or
// {{User:ais523/ContribCalendar|left}} if you want it left-aligned rather than
// right-aligned. Script idea suggested by [[User:Casmith 789]].

var ccwpajax;
// From [[WP:US]] mainpage (wpajax renamed to ccwpajax)
ccwpajax={
        download:function(bundle) {
                // mandatory: bundle.url
                // optional:  bundle.onSuccess (xmlhttprequest, bundle)
                // optional:  bundle.onFailure (xmlhttprequest, bundle)
                // optional:  bundle.otherStuff OK too, passed to onSuccess and onFailure
                
                var x = window.XMLHttpRequest ? new XMLHttpRequest()
                : window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP")
                : false;
                
                if (x) {
                        x.onreadystatechange=function() {
                                x.readyState==4 && ccwpajax.downloadComplete(x,bundle);
                        };
                        x.open("GET",bundle.url,true);
                        x.send(null); 
                }
                return x;
        },

        downloadComplete:function(x,bundle) {
                x.status==200 && ( bundle.onSuccess && bundle.onSuccess(x,bundle) || true )
                || ( bundle.onFailure && bundle.onFailure(x,bundle) || alert(x.statusText));
        }
};

// Example:


// function dlComplete(xmlreq, data) {
//      alert(data.message + xmlreq.responseText);
// }
//  ccwpajax.download({url:'http://en.wikipedia.org/w/index.php?title=Thresher&action=raw', 
//                   onSuccess: dlComplete, message: "Here's what we got:\n\n" });

// End of [[WP:US]] quote

function ccinterpolate(n,x,l,h,v)
{
  return Math.round(((v-n)/(x-n))*h+((x-v)/(x-n))*l);
}

function contribcolor(c)
{
  var r,g,b;
  //1 is #CCCCCC   (#CC is 204)
  //10 is #FF8080  (#FF is 255)
  //50 is #8080FF  (#80 is 128)
  //100 is #80FF80
  if(c<10) {r=ccinterpolate(1,10,204,255,c); g=ccinterpolate(1,10,204,128,c); b=g;}
  else if(c<50) {r=ccinterpolate(10,50,255,128,c); b=128; g=383-r;}
  else {g=ccinterpolate(50,100,255,128,c); r=128; b=383-g;}
  if(r>255) r=255; if(g>255) g=255; if(b>255) b=255;
  var nybbles=new Array('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F');
  return nybbles[Math.floor(r/16)]+nybbles[r%16]+
         nybbles[Math.floor(g/16)]+nybbles[g%16]+
         nybbles[Math.floor(b/16)]+nybbles[b%16];
}

function rendercalen(xmlreq,data)
{
  var a=xmlreq.responseText;
  a=a.split(/timestamp="/i);
  var i;
  var t=new Array();
  i=a.length;
  while(--i) //loop backwards, skip element 0
  {
    a[i]=a[i].substr(0,a[i].indexOf('T'));
    if(t[a[i]]==undefined) t[a[i]]=0;
    t[a[i]]++;
  }
  // This basically generates something similar to
  // [[Template:OctoberCalendar2006Source]], designed to be transcluded onto a userpage.
  a='{| class="toccolours" style="float:{{{1|right}}}; margin-left: 1em; '+
    'text-align:center;" cellpadding=2 cellspacing=0\n';
  a+='|- style="background:#CCCCFF;"\n'+
     '|colspan="7"|Contributions\n';
  a+='|- style="background:#CCCCFF;"\n'+
     '|width="14%"| Su\n'+
     '|width="14%"| Mo\n'+
     '|width="14%"| Tu\n'+
     '|width="14%"| We\n'+
     '|width="14%"| Th\n'+
     '|width="14%"| Fr\n'+
     '|width="14%"| Sa\n'+
     '|-\n';
  var curdate="";
  var curday=0;
  var flg=0;
  var d=new Date();
  var od=d;
  for(i in t)
  {
    d.setUTCFullYear(i.substr(0,4),new Number(i.substr(5,2))-1,i.substr(8,2));
    if(d-od>new Date(2006,1,8)-new Date(2006,1,1)) flg=7;
    od=d;
    while(curday!=d.getUTCDay()||flg)
    {
      curday++;
      a+='|&nbsp;\n';
      if(curday==7)
      {
        a+="|-\n";
        curday=0;
      }
      if(flg) flg--;
    }
    a+="|<span style='background-color:#"+contribcolor(t[i])+"'>"+i.substr(8,2)+
       "</span>\n";
    curday++;
    if(curday==7)
    {
      a+="|-\n";
      curday=0;
    }
  }
  a+='|- style="background:#CCCCFF"\n'+
     '|colspan="7"|Updated [[{{subst:CURRENTMONTHNAME}} {{subst:CURRENTDAY}}]]'+
     ' [[{{subst:CURRENTYEAR}}]]\n'+
     '|-\n';
  a+="|<span style='background-color:#"+contribcolor( 1)+"'>01</span>\n";
  a+="|<span style='background-color:#"+contribcolor( 5)+"'>05</span>\n";
  a+="|<span style='background-color:#"+contribcolor(10)+"'>10</span>\n";
  a+="|<span style='background-color:#"+contribcolor(25)+"'>25</span>\n";
  a+="|<span style='background-color:#"+contribcolor(50)+"'>50</span>\n";
  a+="|<span style='background-color:#"+contribcolor(75)+"'>75</span>\n";
  a+="|<span style='background-color:#"+contribcolor(99)+"'>99</span>\n";
  a+='|}';
  document.editform.wpTextbox1.value=a;
  document.editform.wpMinoredit.checked="checked";
  document.editform.wpSummary.value=
    "Updating contributions calendar using [[WP:US|user scripts]]";
  document.editform.wpPreview.click();
}

function contribcalendar()
{
  var wgpn=mw.config.get('wgPageName');
  if(wgpn.indexOf("/")!=-1) wgpn=wgpn.substr(0,wgpn.indexOf("/"));
  location.href="http://en.wikipedia.org/w/index.php?title="+wgpn+
    "/ContribCalendar&action=edit&rendercc="+wgpn;
}

// Contrib calendar update
$(function() {
   if(location.href.indexOf("&rendercc=")!=-1)
     ccwpajax.download({url:'http://en.wikipedia.org/w/query.php?what=usercontribs&'+
       'titles='+location.href.substr(location.href.indexOf("&rendercc=")+10)+
       '&uclimit=500&format=xml', onSuccess:rendercalen});
   else if(mw.config.get('wgNamespaceNumber')==2)
     mw.util.addPortletLink('p-cactions',"javascript:contribcalendar();","calendar","ca-calen","Contrib calendar","");
});

// </nowiki></pre>
//[[Category:Wikipedia scripts]]