User:NDKilla/common.js

/* * Ajax auto-refreshing articles * * Original by pcj of Wowpedia * * Maintenance, cleanup, style and bug fixes by: * - Grunny       (http://c.wikia.com/wiki/User:Grunny) * - Kangaroopower (http://c.wikia.com/wiki/User:Kangaroopower) * - Cqm          (http://c.wikia.com/wiki/User:Cqm) */

/* jshint bitwise:true, camelcase:true, curly:true, eqeqeq:true, latedef:true, maxdepth:3, maxlen:120, newcap:true, noarg:true, noempty:true, nonew:true, onevar:true, plusplus:false, quotmark:single, undef:true, unused:true, strict:true, trailing:true, asi:false, boss:false, debug:false, eqnull:false, evil:false, expr:false, lastsemic:false, loopfunc:false, moz:false, proto:false, scripturl:false, browser:true, devel:false, jquery:true

/*global mediaWiki:true, Wikia:true */

(function (window, $, mw, Wikia) {	'use strict';

var config = mw.config.get([			'stylepath',			'wgAction',			'wgCanonicalSpecialPageName',			'wgPageName'		]), // use common file as it's very likely to be already cached by user // used in oasis sidebar loading, preview modal, etc.		ajaxIndicator = window.ajaxIndicator || config.stylepath + '/common/images/ajax.gif', ajaxTimer, refreshText = typeof window.AjaxRCRefreshText === 'string' ? window.AjaxRCRefreshText : 'AJAX', refreshHover = typeof window.AjaxRCRefreshHoverText === 'string' ? window.AjaxRCRefreshHoverText : 'Enable auto-refreshing page loads', ajRefresh = window.ajaxRefresh || 60000, ajPages = window.ajaxPages || ['Special:RecentChanges'], // don't load on these values of wgAction // @todo check if markpatrolled should be here disallowActions = [ 'delete', 'edit', 'protect', 'revisiondelete' ],		// if there's a hash on the end of the url, jquery strips it		// not sure why, have a look at the comments in the source if you really want to know // however, location.href keeps the hash in a url // so the callbacks for ajaxsend and ajaxcomplete won't fire // just by comparing settings.url to location.href href = location.href.replace(/#[\S]*/, '');

function storage(setTo) { if (localStorage.getItem('AjaxRC-refresh') === null) { localStorage.setItem('AjaxRC-refresh', true); }

if (setTo === false) { localStorage.setItem('AjaxRC-refresh', false); } else if (setTo === true) { localStorage.setItem('AjaxRC-refresh', true); }

return JSON.parse(localStorage.getItem('AjaxRC-refresh')); }

/**	 * Get the element to add the ajaxrc checkbox, etc. to	 */ function getAppTo { var $ret;

// monobook $ret = $('.firstHeading');

if ($ret.length) { return $ret; }

// most oasis pages $ret = $('#WikiaPageHeader');

if ($ret.length) { return $ret; }

// most oasis special pages $ret = $('#AdminDashboardHeader');

if ($ret.length) { return $ret.children('h1'); }

return false; }

/**	 * Main function to start the Auto-refresh process */	function preloadAJAXRL { var $appTo = getAppTo, $checkbox = $(' ') .attr('id', 'ajaxRefresh') .css({					'font-size': 'xx-small',					'line-height': '100%',					'margin-left': '5px'				}) .append(					$(' ')						.attr({ id: 'ajaxToggleText', // for for RL to comply with es3 rules 'for': 'ajaxToggle', title: refreshHover })						.css({ 'border-bottom': '1px dotted', cursor: 'help' })						.text( refreshText + ':' ),					$(' ')						.attr({ id: 'ajaxToggle', type: 'checkbox' })						.css('margin-bottom', '0'),					$(' ')						.attr('id', 'ajaxLoadProgress')						// I think this is for a firefox bug (because .hide didn't do it properly)						.css('display', 'none')						.append( $(' ')								.css({									'vertical-align': 'baseline',									'float': 'none',									border: 0								}) .attr({									src: ajaxIndicator,									alt: 'Refreshing page'								}) )				),			$throbber;

// fallback for pages with profile masthead if ($appTo === false) { $('#WikiaArticle').prepend($checkbox); } else { $appTo.append($checkbox); }

$throbber = $appTo.find('#ajaxLoadProgress');

$(document).ajaxSend(function (_, _2, settings) {			if ( href === settings.url ) {				$throbber.show;			}		}).ajaxComplete(function (_, _2, settings) {			var $collapsibleElements = $( '#mw-content-text' ).find( '.mw-collapsible' ),				ajCallAgain = window.ajaxCallAgain || [],				i;

if (href === settings.url) { $throbber.hide;

if ($collapsibleElements.length) { $collapsibleElements.makeCollapsible; }

if (config.wgCanonicalSpecialPageName === 'Recentchanges') { mw.special.recentchanges.init;

if ($('.mw-recentchanges-table').find('.WikiaDropdown').length) { Wikia.RecentChanges.init; }				}

if (config.wgCanonicalSpecialPageName === 'WikiActivity') { window.WikiActivity.init; }

for (i = 0; i < ajCallAgain.length; i++) { ajCallAgain[i]; }			}		});

$( '#ajaxToggle' ) .attr('checked', storage) .click(toggleAjaxReload);

if (storage) { loadPageData; }	}

/**	 * Turn refresh on and off by toggling the checkbox */	function toggleAjaxReload { if ($('#ajaxToggle').prop('checked') === true) { storage(true); loadPageData; } else { storage(false); clearTimeout(ajaxTimer); }	}

/**	 * Does the actual refresh */	function loadPageData { var $temp = $(' ');

$temp.load(href + ' #mw-content-text', function {			var $newContent = $temp.children('#mw-content-text');

if ($newContent.length) { $('#mw-content-text').replaceWith($newContent); }

ajaxTimer = setTimeout(loadPageData, ajRefresh); });

$temp.remove; }

/**	 * Load the script on specific pages * and only on certain values for wgAction (see disallowActions above) */	$(function {		if ( ajPages.indexOf(config.wgPageName) !== -1 && $('#ajaxToggle').length === 0 && disallowActions.indexOf(config.wgAction) === -1 ) {			preloadAJAXRL;		}	});

}(this, jQuery, mediaWiki, Wikia));