User:Joshua Scott/Scripts/pendingchanges.js

var pendch_http;

var pendch_enabled;

var pendch_num_pages;

var pendch_refresh;

var pendch_num_idle_req;

var pendch_curr_idle_req;

var pendch_diffonly;

var pendch_str_no_ajax = "There seems to be a problem using the pendingchanges script. Your browser is not supported.";

var pendch_str_box_title = "Pending changes";

var pendch_str_box_title_updating = "Pending changes (updating)";

var pendch_str_box_title_failed = "Pending changes (update failed)";

var pendch_str_enable = "Enable this box";

var pendch_str_disable = "Disable this box";

$(document).ready( pendch_init );

/* initalise */

function pendch_init() {

if (window.location.toString().search(/diffonly=1/) != -1) {

var currURL = window.location.toString();

var newURL=currURL.replace(/diffonly=1/,diffonly=0);

console.log(document.getElementById("difference").innerHTML);

if (document.getElementById("difference").innerHTML.search(/Show page below diff/) == -1) {

document.getElementById("difference").innerHTML += ' (Show page below diff)';

}

}

// allow user settings through

if (pendch_enabled == null) {

pendch_enabled = false;

}

if (pendch_num_pages == null) {

pendch_num_pages = 10;

}

if (pendch_refresh == null) {

pendch_refresh = 10;

}

if (pendch_num_idle_req == null) {

pendch_num_idle_req = 60;

}

if(pendch_diffonly == null) {

pendch_diffonly = 1;

}

// A few limits to be nice to the servers

if (pendch_num_pages > 50) {

pendch_num_pages = 50;

}

if (pendch_num_pages < 1) {

pendch_num_pages = 1;

}

if (pendch_refresh < 2) {

pendch_refresh = 2;

}

if (pendch_num_idle_req > 1000) {

pendch_num_idle_req = 1000;

}

if (pendch_num_idle_req < 5) {

pendch_num_idle_req = 5;

}

// get our cookie

if (document.cookie.length > 0) {

var c_start = document.cookie.indexOf("pendch_show_box=");

if (c_start != -1) {

c_start = c_start + 16;

var c_end = document.cookie.indexOf(";", c_start);

if (c_end == -1) {

c_end = document.cookie.length;

}

if (document.cookie.substring(c_start, c_end) == "yes") {

pendch_enabled = true;

} else {

pendch_enabled = false;

}

}

}

// Either make a request or show nothing

pendch_curr_idle_req = 0;

if (pendch_enabled == true) {

pendch_ajax_request();

} else {

pendch_draw_disabled_box();

}

}

/* init ajax */

function pendch_create_request() {

try {

pendch_http = new XMLHttpRequest();

} catch (e) {

try {

pendch_http = new ActiveXObject("Msxml2.XMLHTTP");

} catch (e) {

try {

pendch_http = new ActiveXObject("Microsoft.XMLHTTP");

} catch (e) {

return false;

}

}

}

pendch_http.onreadystatechange = function() {

if(pendch_http.readyState == 4) pendch_ajax_response();

}

return true;

}

/* make a request */

function pendch_ajax_request() {

// if we have done too many requests, disable the box

pendch_curr_idle_req++;

if (pendch_curr_idle_req > pendch_num_idle_req) {

pendch_disable_box();

}

// check we are enabled

if (pendch_enabled == false) return;

// firstly, inform the user

var cur_box = document.getElementById('p-pendingchanges');

if (cur_box != null) {

cur_box.firstChild.firstChild.data = pendch_str_box_title_updating;

}

if (pendch_create_request () == false) {

if (cur_box != null) {

cur_box.firstChild.firstChild.data = pendch_str_box_title_failed;

} else {

alert (pendch_str_no_ajax);

}

}

// Get the current time

var dateobj = new Date();

var now = Math.floor(dateobj.getTime() / 1000.0);

// Then make the request

pendch_http.open("GET", "/w/api.php?action=query&format=json&list=oldreviewedpages&ornamespace=0|4&orlimit=" + pendch_num_pages, true);

pendch_http.send(null);

}

function pendch_draw_disabled_box() {

if (mw.config.get('skin') == 'vector') {

pendch_draw_disabled_box_vector();

} else {

pendch_draw_disabled_box_monobook();

}

}

function pendch_ajax_response() {

if (mw.config.get('skin') == 'vector') {

pendch_ajax_response_vector();

} else {

pendch_ajax_response_monobook();

}

// and do it again in 5 secs

setTimeout("pendch_ajax_request()", pendch_refresh * 1000);

}

function pendch_disable_box() {

pendch_enabled = false;

pendch_draw_disabled_box();

document.cookie = "pendch_show_box=no; path=/";

}

function pendch_enable_box() {

pendch_enabled = true;

pendch_curr_idle_req = 0;

document.cookie = "pendch_show_box=yes; path=/";

pendch_ajax_request();

}

/* Draw disabled (monobook) */

function pendch_draw_disabled_box_monobook() {

// Container div

var link_div = document.createElement('div');

link_div.className = 'pBody';

var div = document.createElement('div');

div.setAttribute('id', 'p-pendingchanges');

div.className = 'portal';

var heading = document.createElement('h3');

heading.appendChild(document.createTextNode(pendch_str_box_title));

div.appendChild(heading);

div.appendChild(link_div);

// enable link

var p = document.createElement('p');

p.style.fontSize = 'x-small';

p.style.margin = '0px';

var a = document.createElement('a');

a.appendChild(document.createTextNode(pendch_str_enable));

a.onclick = pendch_enable_box;

p.appendChild(a);

link_div.appendChild(p);

// now replace the div

var old_div = document.getElementById('p-pendingchanges');

var side_col = document.getElementById('column-one');

if (old_div != null) {

side_col.replaceChild(div, old_div);

} else {

var node = document.getElementById('p-search');

if ( node && node.parentNode === side_col ) {

side_col.insertBefore(div, node);

}

}

}

/* Draw response (monobook) */

function pendch_ajax_response_monobook() {

var resp = JSON.parse(pendch_http.responseText);

var items = resp.query.oldreviewedpages;

// create the div that holds all the newpage links

var link_div = document.createElement('div');

link_div.className = 'pBody';

var list = document.createElement('ul');

link_div.appendChild(list);

// populate the list with 10 links.

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

var item = items[i];

var item_name = item.title;

var old_id = item.stable_revid;

item_name = item_name.replace(/&/, "%26");

var item_url = 'http://en.wikipedia.org/w/index.php?title=' + item_name + '&diff=cur&oldid=' + old_id + '&diffonly=' + pendch_diffonly;

a = document.createElement('a');

a.setAttribute('href', item_url);

a.appendChild(document.createTextNode(item_name));

var li = document.createElement('li');

li.appendChild(a);

list.appendChild(li);

}

// Container div

var div = document.createElement('div');

div.setAttribute('id', 'p-pendingchanges');

div.className = 'portal';

var heading = document.createElement('h3');

heading.appendChild(document.createTextNode(pendch_str_box_title));

div.appendChild(heading);

div.appendChild(link_div);

// disable link

var p = document.createElement('p');

p.style.fontSize = 'x-small';

p.style.margin = '0px';

p.style.textAlign = 'right';

a = document.createElement('a');

a.appendChild(document.createTextNode(pendch_str_disable));

a.onclick = pendch_disable_box;

p.appendChild(a);

link_div.appendChild(p);

// now replace the div

var old_div = document.getElementById('p-pendingchanges');

var side_col = document.getElementById('column-one');

if (old_div != null) {

side_col.replaceChild(div, old_div);

} else {

var node = document.getElementById('p-search');

side_col.insertBefore(div, node);

}

}

/* Draw disabled box (vector skin) */

function pendch_draw_disabled_box_vector() {

// Container div

var link_div = document.createElement('div');

link_div.className = 'body';

link_div.style.display = "block";

var div = document.createElement('div');

div.setAttribute('id', 'p-pendingchanges');

div.className = 'portal collapsed';

var heading = document.createElement('h3');

heading.appendChild(document.createTextNode(pendch_str_box_title));

div.appendChild(heading);

div.appendChild(link_div);

// enable link

var p = document.createElement('p');

p.style.fontSize = 'x-small';

p.style.margin = '0px';

var a = document.createElement('a');

a.appendChild(document.createTextNode(pendch_str_enable));

a.onclick = pendch_enable_box;

p.appendChild(a);

// add later

// now replace the div

var old_div = document.getElementById('p-pendingchanges');

var side_col = document.getElementById('mw-panel');

if (old_div != null) {

side_col.replaceChild(div, old_div);

} else {

var node = document.getElementById('p-interaction');

side_col.insertBefore(div, node);

}

if( typeof $ != 'undefined' ) {

$('#p-pendingchanges > h3').keydown( function( event ) {

if ( event.which == 13 /* Enter */ || event.which == 32 /* Space */ ) {

pendch_toggle( $(this) );

}

} )

.mousedown( function() {

pendch_toggle( $(this) );

$(this).blur();

return false;

} );

} else {

link_div.appendChild(p);

}

}

/* Draw response (vector skin) */

function pendch_ajax_response_vector() {

var resp = JSON.parse(pendch_http.responseText);

var items = resp.query.oldreviewedpages;

// create the div that holds all the newpage links

var link_div = document.createElement('div');

link_div.className = 'body';

link_div.style.display = "block";

var list = document.createElement('ul');

link_div.appendChild(list);

// populate the list with 10 links.

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

var item = items[i];

var item_name = item.title;

var old_id = item.stable_revid;

item_name = item_name.replace(/&/, "%26");

var item_url = 'http://en.wikipedia.org/w/index.php?title=' + item_name + '&diff=cur&oldid=' + old_id + '&diffonly=' + pendch_diffonly;

a = document.createElement('a');

a.setAttribute('href', item_url);

//a.style.fontSize = 'x-small';

a.appendChild(document.createTextNode(item_name));

var li = document.createElement('li');

li.appendChild(a);

list.appendChild(li);

}

// Container div

var div = document.createElement('div');

div.setAttribute('id', 'p-pendingchanges');

div.className = 'portal expanded';

var heading = document.createElement('h3');

heading.appendChild(document.createTextNode(pendch_str_box_title));

div.appendChild(heading);

div.appendChild(link_div);

// disable link

var p = document.createElement('p');

p.style.fontSize = 'x-small';

p.style.margin = '0px';

p.style.textAlign = 'left';

a = document.createElement('a');

a.appendChild(document.createTextNode(pendch_str_disable));

a.onclick = pendch_disable_box;

p.appendChild(a);

// add later

// now replace the div

var old_div = document.getElementById('p-pendingchanges');

var side_col = document.getElementById('mw-panel');

if (old_div != null) {

side_col.replaceChild(div, old_div);

} else {

var node = document.getElementById('p-interaction');

side_col.insertBefore(div, node);

}

if( typeof $ != 'undefined' ) {

$('#p-pendingchanges > h3').keydown( function( event ) {

if ( event.which == 13 /* Enter */ || event.which == 32 /* Space */ ) {

pendch_toggle( $(this) );

}

} )

.mousedown( function() {

pendch_toggle( $(this) );

$(this).blur();

return false;

} );

} else {

link_div.appendChild(p);

}

}

function pendch_toggle( $element ) {

$.cookie( 'vector-nav-' + $element.parent().attr( 'id' ), $element.parent().is( '.collapsed' ) );

if( $element.parent().is('.collapsed') ) {

pendch_enable_box();

} else {

pendch_disable_box();

}

$element

.parent()

.toggleClass( 'expanded' )

.toggleClass( 'collapsed' )

.find( 'div.body' )

.slideToggle( 'fast' );

}