MediaWiki:UntergangerList.js

/************************* PART ONE - JQUERY AJAX QUEUE ************************** */

/* * jQuery AjaxQ - AJAX request queueing for jQuery * * Version: 0.0.1 * Date: July 22, 2008 * * Copyright (c) 2008 Oleg Podolsky (oleg.podolsky@gmail.com) * Licensed under the MIT (MIT-LICENSE.txt) license. * * http://plugins.jquery.com/project/ajaxq * http://code.google.com/p/jquery-ajaxq/ */

jQuery.ajaxq = function (queue, options) {   /* Initialize storage for request queues if it's not initialized yet */ if (typeof document.ajaxq == "undefined") document.ajaxq = {q:{}, r:null};

/* Initialize current queue if it's not initialized yet */ if (typeof document.ajaxq.q[queue] == "undefined") document.ajaxq.q[queue] = []; if (typeof options != "undefined") /* Request settings are given, enqueue the new request */ {       /* Copy the original options, because options.complete is going to be overridden */

var optionsCopy = {}; for (var o in options) optionsCopy[o] = options[o]; options = optionsCopy; /* Override the original callback */

var originalCompleteCallback = options.complete;

options.complete = function (request, status) {           /* Dequeue the current request */ document.ajaxq.q[queue].shift ; document.ajaxq.r = null; /* Run the original callback */ if (originalCompleteCallback) originalCompleteCallback (request, status);

/* Run the next request from the queue */ if (document.ajaxq.q[queue].length > 0) document.ajaxq.r = jQuery.ajax (document.ajaxq.q[queue][0]); };

/* Enqueue the request */ document.ajaxq.q[queue].push (options);

/* Also, if no request is currently running, start it */ if (document.ajaxq.q[queue].length == 1) document.ajaxq.r = jQuery.ajax (options); }   else /* No request settings are given, stop current request and clear the queue */ {       if (document.ajaxq.r)        { document.ajaxq.r.abort ; document.ajaxq.r = null; }

document.ajaxq.q[queue] = []; } }

/*************************** PART TWO - TABLE ADJUSTMENTS ***********************/ /* Because everyone wants to add their YT channel link in the List of Untergangers, */ /* This block of code will do it for all unfilled ones */

/* Codes that doesn't involve variables go here */ /* - Assign classes to cells for jQuery selection */ /* - Also add tooltips to the figure columns */ $(".untergangers tbody tr td:nth-child(1)").addClass("index"); $(".untergangers tbody tr td:nth-child(2)").addClass("name"); $(".untergangers tbody tr td:nth-child(3)").addClass("region"); $(".untergangers tbody tr td:nth-child(4)").addClass("country"); $(".untergangers tbody tr td:nth-child(5)").addClass("vids").attr("title","Uploaded parodies"); $(".untergangers tbody tr td:nth-child(6)").addClass("views").attr("title","Total views"); $(".untergangers tbody tr td:nth-child(7)").addClass("subs").attr("title","Subscriber count"); $(".untergangers tbody tr td:nth-child(8)").addClass("status"); $(".untergangers tbody tr td:nth-child(9)").addClass("yt");

$(".untergangers tr").each(function(idx){   /* Add flag to country column */    ctry = $("td.country",this).text;    $("td.country",this).html(' '+ctry+' ');    /* Checking if the last cell has less than 3 characters */    /* (they are absolutely not empty) */    if ($("td.yt", this).text.length < 3 ) {        /* Getting the unterganger's yt channel from the name column */        /* (for multi-word entries the first one MUST be the yt name) */        channelname = $("td.name", this).text.split(" ")[0];        /* Check to see if Status column contain "Suicide" */        statscol =$("td.status", this).text;        if (statscol.search("Suicide") == -1) {            /* adding the link to the YT column... */            $("td.yt", this).html('yt'); }   }    /* now that we added yt links on non-suicidal channels.. */   if ($("td.yt", this).text.length >= 2 ) { /* retrieve channelname from url if empty */ if (channelname == '') channelname = $("td.yt a", this).attr("href").split("/user/")[1]; /* check if channel is undefined (Das Reich's case) */ /* then place an invisible div holding channelname */ if (typeof channelname !== 'undefined') { $("td.yt", this).append(''+channelname+' '); /* Add button to update stats using AJAX */ $("td.name",this).append(' [ update ] '); }   }   });

/********************** PART THREE - AJAX CODES *************************/

$("table.untergangers").before(' Update list (AJAX) Please wait while the list is being updated. If it does not finish, try refreshing the page. '); $("#UntergangerAJAX").click(function {	/* make this a one time deal */	$('button#UntergangerAJAX').hide;	$('#UntergangerLoading').show;   /* hope the queueing mechanism works */	$('table.untergangers tbody tr').each(function{ handleRow($(this)); }); });

/* from stackoverflow */ function commafy( num ) { var str = num.toString.split('.'); if (str[0].length >= 4) { str[0] = str[0].replace(/(\d)(?=(\d{3})+$)/g, '$1,'); }   if (str[1] && str[1].length >= 4) { str[1] = str[1].replace(/(\d{3})/g, '$1 '); }   return str.join('.'); }

$(".untergangers tr td.name .Update").click(function{   /* please note the number of levels of parent nodes the is */    $row = $(this).parent.parent.parent;    handleRow($row); });

function handleRow(element) { /* do not handle rows with noAJAX class. theoretically this can be used to mark suicidals and verraters */ /* Also, it's cruicial to check in advance whether there's a yt link in its column */ if ( $("td.yt a",element).length !== 0 && element.hasClass("noAJAX") !== 'true' ) { channelname = element.find("td.yt a").attr("href").split("/user/")[1].split("/")[0]; /* console.log(channelname); */ if (typeof channelname !== 'undefined') { $.ajaxq('unterajax',{               type:"GET",                url:"http://gdata.youtube.com/feeds/api/users/"+channelname,                dataType:"xml",                beforeSend: function {                    /* replace 'update' with a throbber */                    $("td.name .Update",element).html('');               },                success: function(ytdata) {                    if (ytdata == 'User not found') {                        /* this is more to indicate that the channel does not exist anymore (suicidal) */                        $("td.vids, td.views, td.subs",element).text("-");                    } else {                        /* retrieve stats */                        vid = $(ytdata).find('gd\\:feedLink[rel$="#user.uploads"]').attr("countHint");                        view = $(ytdata).find("yt\\:statistics").attr("totalUploadViews");                        sub = $(ytdata).find("yt\\:statistics").attr("subscriberCount");                        /* still need to figure out how to parse the date */                        last = $(ytdata).find("yt\\:statistics").attr("lastWebAccess");                        gender = $(ytdata).find("yt\\:gender").text;                        console.log(channelname+": vid:"+vid+" view:"+view+" sub:"+sub+" g:"+gender); /* note that some channels do not specify their gender, so the string is empty */ if ($("td.name span.gender",element).length == 0 ) { $("td.name",element).append('' + gender.toUpperCase + ' '); } else { $("td.name span.gender",element).addClass(gender).text(gender.toUpperCase); }                       /* do not update if text is "N/A" or "?" (to mark channels with a majority of non-parody videos) */ if ( $.trim( $("td.vids",element).text ) !== "?" ) $("td.vids",element).text( commafy(vid) ); if ( $.trim( $("td.views",element).text ) !== "N/A" ) $("td.views",element).text( commafy(view) ); if ( $.trim( $("td.subs",element).text ) !== "N/A" ) $("td.subs",element).text( commafy(sub) ); }               },                complete: function { /* returns the 'update' text */ $("td.name .Update",element).text('update'); }           });        }    } }