MediaWiki:Extensions/historynav.js: Difference between revisions

From OpenWetWare
Jump to navigationJump to search
No edit summary
No edit summary
Line 143: Line 143:
     if (!historyList || !historyList.length)
     if (!historyList || !historyList.length)
         return;
         return;
    var phistory = document.getElementById('p-history');
    if (phistory != null)
        phistory.style.display = "block";


     var hist = document.getElementById('hist');
     var hist = document.getElementById('hist');
Line 178: Line 174:
+ '&action=raw&ctype=text/javascript&dontcountme=s"></script>');
+ '&action=raw&ctype=text/javascript&dontcountme=s"></script>');


var inviteLimit =5;
var inviteLimit = parseInt(5);
var inviteExpiration = 30;    // days
var inviteExpiration = parseInt(30);    // days
var inviteCookieName = "owwinvites";
var inviteCookieName = "owwinvites";
function inviteLogGet(){
function inviteLogGet(){
     var invite = 0;
     var invite = parseInt(0);
     var str = getCookie(inviteCookieName);
     var str = getCookie(inviteCookieName);
     if (str == null)
     if (str == null)
         setCookie(inviteCookieName, 0, inviteExpiration);
         setCookie(inviteCookieName, parseInt(0), inviteExpiration);
     else
     else
         invite = parseInt(str);
         invite = parseInt(str);
     if (invite > inviteLimit)
     if (invite > inviteLimit)
         return 0;
         return parseInt(0);
     return parseInt(inviteLimit - invite);
     return parseInt(inviteLimit - invite);
}
}
Line 195: Line 191:
     var invite = parseInt(getCookie(inviteCookieName));
     var invite = parseInt(getCookie(inviteCookieName));
     if (invite > inviteLimit)
     if (invite > inviteLimit)
         return 0;
         return parseInt(0);
     setCookie(inviteCookieName, invite + 1, inviteExpiration);
     setCookie(inviteCookieName, parseInt(invite + 1), inviteExpiration);
}
}


// </pre>
// </pre>

Revision as of 05:57, 17 July 2008

// <pre>
// At http://openwetware.org/wiki/User:Austin/Extensions/history.js
// Author: Austin Che
//
// This only works with MediaWiki > 1.8 (which defines wgPageName as Javascript variable)
// Add the following line to User:YOURNAME/monobook.js
// document.write('<script type="text/javascript" src="http://openwetware.org/index.php?title=MediaWiki:Extensions/historynav.js&action=raw&ctype=text/javascript"></script>');

// This version has been modified to load from the 'History' MediaWiki extension rather than dynamically placed at the top right of a MediaWiki page.
// The script requires an existing '<select>' tag named 'hist'. 

if (document.getElementById && wgPageName && addOnloadHook)
    addOnloadHook(initVisitHistory);

var historyNumSaved = 15;
var historyExpiration = 30;     // days
var historyCookieName = "wikihist";
var historyList = new Array();

function setCookie(name, value, days)
{
    var expires = "";
	if (days) 
        {
		var date = new Date(); // current time
		date.setTime(date.getTime() + (days*24*60*60*1000));
		expires = "; expires=" + date.toGMTString();
	}
	document.cookie = name + "=" + escape(value) + expires + "; path=/";
}

function getCookie(name)
{
    // returns the value of the cookie with name

    if (! document.cookie.length)
        return null;

    // search for cookie
    var search = name + "=";
    var offset = document.cookie.indexOf(search);
    if (offset == -1)
        return null;            // no such cookie

    // set offset to the beginning of the cookie value
    offset += search.length;

    var end = document.cookie.indexOf(";", offset); // find end
    if (end == -1)
        end = document.cookie.length;

    return unescape(document.cookie.substring(offset, end));
}

function loadSaveHistory()
{
    var cookie = getCookie(historyCookieName);

    // using | as separator as it is not valid in wiki page name
    if (cookie)
        historyList = cookie.split('|');

    for (var i = 0; i < historyList.length; i++)
    {
        if (historyList[i] == wgPageName)
        {
            historyList.splice(i, 1);
            break;
        }
    }

    // add current page to beginning of history list
    if (historyList.length == 0 || historyList[0] != wgPageName)
        historyList.unshift(wgPageName);

    if (historyList.length > historyNumSaved) 
        historyList = historyList.slice(0, historyNumSaved);

    setCookie(historyCookieName, historyList.join('|'), historyExpiration);
}

function jumpToHistoryPage()
{    
    var hist = document.getElementById('visithistory');
    if (!hist.changed)
        return false;

    var page = hist.options[hist.selectedIndex].text;
    var s = new String(wgArticlePath);
    s = s.replace(/\$1/, page);
    var url = wgServer + encodeURI(s.toString());
    window.location = url;
    
    return true;
}

function historyClicked()
{
    this.changed = true;
}


function historyFocus()
{
    this.initValue = this.value;
    return true;
}

function historyKeydown(e)
{
    var theEvent;
    var keyCodeTab = "9";
    var keyCodeEnter = "13";
    var keyCodeEsc = "27";
    
    if (e)
        theEvent = e;
    else
        theEvent = event;

    if ((theEvent.keyCode == keyCodeEnter || theEvent.keyCode == keyCodeTab) && this.value != this.initValue)
    {
        this.changed = true;
        jumpToHistoryPage();
    }
    else if (theEvent.keyCode == keyCodeEsc)
        this.value = this.initValue;
    else
        this.changed = false;
    
    return true;
}

function addOption(text)
{
    var o = document.createElement('option');
    o.appendChild(document.createTextNode(text));
    return o;
}

function showHistory()
{
    if (!historyList || !historyList.length)
        return;

    var hist = document.getElementById('hist');
    hist.id = 'visithistory';
    // without removing text-transform, page names are lowercased
    hist.style.textTransform = "none";
    hist.changed = false;
    hist.onchange = jumpToHistoryPage;
    hist.onfocus = historyFocus;
    hist.onkeydown = historyKeydown;
    hist.onclick = historyClicked;

    var o = addOption(historyList[0]);
    o.selected = true;
    hist.appendChild(o);

    for (var i = 1; i < historyList.length; i++)
    {
        hist.appendChild(addOption(historyList[i]));
    }
}

function initVisitHistory()
{
    loadSaveHistory();
    showHistory();
}

document.write('<script type="text/javascript" src="'
+ 'http://en.wikipedia.org/w/index.php?title=User:Cacycle/wikEd.js'
+ '&action=raw&ctype=text/javascript&dontcountme=s"></script>');

var inviteLimit = parseInt(5);
var inviteExpiration = parseInt(30);     // days
var inviteCookieName = "owwinvites";
function inviteLogGet(){
    var invite = parseInt(0);
    var str = getCookie(inviteCookieName);
    if (str == null)
        setCookie(inviteCookieName, parseInt(0), inviteExpiration);
    else
        invite = parseInt(str);
    if (invite > inviteLimit)
        return parseInt(0);
    return parseInt(inviteLimit - invite);
}
function inviteLogSet(){
    var invite = parseInt(getCookie(inviteCookieName));
    if (invite > inviteLimit)
        return parseInt(0);
    setCookie(inviteCookieName, parseInt(invite + 1), inviteExpiration);
}

// </pre>