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] = []; } } // isolating the routine and its variables from the rest of the system (function($){	/*************************** 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 */	/* Routines that doesn't involve variables go here */	/* - Assign classes to cells for jQuery selection */	/* - Also add tooltips to the figure columns */	var $head = $("table.untergangers thead th");	for (var i in $head) {		if ( $head.eq(i-1).hasClass("index") ) {			$(".untergangers tbody tr td:nth-child("+i+")").addClass("index");		} else if ( $head.eq(i-1).hasClass("name") ) {			$(".untergangers tbody tr td:nth-child("+i+")").addClass("name");		} else if ( $head.eq(i-1).hasClass("gender") ) {			$(".untergangers tbody tr td:nth-child("+i+")").addClass("gender");		} else if ( $head.eq(i-1).hasClass("region") ) {			$(".untergangers tbody tr td:nth-child("+i+")").addClass("region"); } else if ( $head.eq(i-1).hasClass("country") ) { $(".untergangers tbody tr td:nth-child("+i+")").addClass("country"); } else if ( $head.eq(i-1).hasClass("vids") ) { $(".untergangers tbody tr td:nth-child("+i+")").addClass("vids").attr("title","Uploaded parodies"); } else if ( $head.eq(i-1).hasClass("views") ) { $(".untergangers tbody tr td:nth-child("+i+")").addClass("views").attr("title","Total views"); } else if ( $head.eq(i-1).hasClass("subs") ) { $(".untergangers tbody tr td:nth-child("+i+")").addClass("subs").attr("title","Subscriber count"); } else if ( $head.eq(i-1).hasClass("status") ) { $(".untergangers tbody tr td:nth-child("+i+")").addClass("status"); } else if ( $head.eq(i-1).hasClass("yt") ) { $(".untergangers tbody tr td:nth-child("+i+")").addClass("yt"); }	}	// GENDER COLUMN //$("table.untergangers thead th.name").after('G '); //$("table.untergangers tfoot th:nth-child(2)").after(' G '); //$("table.untergangers tbody td.name").after(' M '); $(".untergangers tr").each(function{		/* Add flag to country column */		var ctry = $("td.country",this).text.replace(/\s/g, '');		$("td.country",this).html(' '+ctry+' ');		/* ADDING YT LINK */			/* Checking if the last cell has less than 3 characters */		/* (they are absolutely not empty) */		if ($("td.yt a", this).length==0 ) { /* doesn't contain yt link */			/* Getting the unterganger's yt channel from the name column */			/* (for multi-word entries the first one MUST be the yt name) */			var channelname = $("td.name", this).text.split(" ")[0];			/* Check to see if Status column contain "Suicide" */			var statscol =$("td.status", this).text;			if (statscol.search("Suicide") == -1) {				/* adding the link to the YT column... */				$("td.yt", this).html('yt'); }		}		/* EXECUTED ONLY ON CHANNELS WITH YT LINKS */ if ($("td.yt a", this).length ) { var yturl = $("td.yt a", this).attr("href"); /* retrieve channelname from url if empty */ /* for AJAX purposes, channel ID is now supported */ if ( channelname == '' && yturl.search("/user/") !== -1 ) { var channelname = yturl.split("/user/")[1].split("/")[0].split("?")[0]; } else if ( channelname == '' && yturl.search("/channel/") !== -1 ) { var channelname = yturl.split("/channel/")[1].split("/")[0].split("?")[0]; }			/* retrieves playlist ID too */ if ( yturl.search("/playlist") !== -1 ) { /*console.log($("td.name").text);*/ var plid = $("td.yt a", this).attr("href").split("list=")[1].split("&")[0]; }		}		/* put channel name somewhere for the AJAX to work later on */ if (typeof channelname !== 'undefined') { /* make sure channelname is not undefined */ $("td.yt", this).attr('data-channelname',channelname); // $(this).attr("id",channelname); // I don't think this would be useful anywhere }		if (typeof plid !== 'undefined') { $("td.yt", this).attr('data-plid',plid); }		/* Add button to update stats using AJAX */ /* won't be adding support for playlists just yet */ if (typeof channelname !== 'undefined' /*|| typeof plid !== 'undefined'*/) { /* making sure there's something for the AJAX to look for */ $("td.name",this).append(' ');/* */ }	}); }(jQuery)); /********************** 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 ) { /* trying to catch an error in Chrome about num being undefined */ if (typeof num == 'undefined') return null; 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 */	var $row = $(this).parent.parent.parent;	handleRow($row); });

function handleRow(element) { console.log("handleRow function called"); /* 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 ( $('tr.yt["data-channelname"], tr.yt["data-plid"]', element).length !== 0 && element.hasClass("noAJAX") !== 'true' ) { /* retrive channelname from the invisible divs we put in earlier */ var channelname = element.find("tr.yt").attr("data-channelname"); var plid = element.find("tr.yt").attr("data-plid"); console.log("channelname: "+channelname+", plid: "+plid); if (typeof channelname !== 'undefined') { $.ajaxq('unterajax',{				type:"GET", dataType:"xml",				/* playlist: http://gdata.youtube.com/feeds/api/playlists/PLAYLIST_ID?v=2 */				url:"http://gdata.youtube.com/feeds/api/users/"+channelname+,				beforeSend: setUpdateThrobber(1),				success: parseAjax(ytdata),				complete: setUpdateThrobber(0)			}); } else if (typeof plid !== 'undefined') { $.ajaxq('unterajax',{				type:"GET", dataType:"xml",				/* playlist: http://gdata.youtube.com/feeds/api/playlists/PLAYLIST_ID?v=2 */				url:"http://gdata.youtube.com/feeds/api/playlists/"+plid+,				beforeSend: setUpdateThrobber(1),				success: parseAjax(ytdata),				complete: setUpdateThrobber(0)			}); }	} } function parseAjax(ytdata){ console.log("parsing ajax..."); 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 */ var vid = $(ytdata).find('gd\\:feedLink[rel$="#user.uploads"]').attr("countHint"); var view = $(ytdata).find("yt\\:statistics").attr("totalUploadViews"); var sub = $(ytdata).find("yt\\:statistics").attr("subscriberCount"); /* still need to figure out how to parse the date */ var last = $(ytdata).find("yt\\:statistics").attr("lastWebAccess"); var 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) ); } } function setUpdateThrobber(value) { if (value==0) { /* returns the 'update' text */ $("td.name .Update",element).removeClass("icon-throbber").addClass("icon-refresh-g"); } else { /* replace 'update' with a throbber */ $("td.name .Update",element).removeClass("icon-refresh-g").addClass("icon-throbber"); } } /* UNCOMMENT TO ENABLE ALL AJAX FUNCTIONS TO THE PUBLIC */ //$('#UntergangerAJAX').hide; //$('.Update').hide; /************************** PART FOUR EXPORT HTML **********************/ $('table.untergangers').after('Get table HTML Read on how to convert HTML to wikitext here.'); function ListHTML { var $ut = $('table.untergangers').clone; /* cleanup */ $('td.name .col2-float',$ut).remove; $('td.country',$ut).each(function{		$(this).html($(this).text);	}); $('thead th',$ut).removeClass('headerSort').removeAttr("title"); $('tbody td',$ut).removeAttr("title"); $('div.channelname',$ut).remove; thtml = ' '; if ($('#tableHTML').length !== 0){ $('#tableHTML').text(thtml); } else { $('table.untergangers').after(''+thtml+' '); $('#tableHTML').width('100%'); } }