MediaWiki:Gadget-AjaxRC.js: Difference between revisions

From WikiIndex
Jump to navigation Jump to search
m (1 revision)
 
(updating page code)
 
Line 1: Line 1:
/* <nowiki> */
/*
 
  * ADVANCED AJAX AUTO-REFRESHING ARTICLES
/**
  * Adds a check box at the top of the Recent Changes that auto-refreshes every 30 seconds.  
  * Script for automatically refreshing "recent changes" and "watchlist" via AJAX
  * Script by User:pcj (http://www.wowpedia.org)
* Bug fixes by User:Grunny (http://dev.wikia.com) and User:Porter21 (http://www.falloutwiki.com)
* Source: http://dev.wikia.com/wiki/AjaxRC
  */
  */
 
( function ( $, mw, window ) {
var ajaxPages = new Array("Special:RecentChanges", "Special:Watchlist");
    'use strict';
var ajaxTimer;
var ajaxRefresh = 60000;
var ajaxIndicator = window.ajaxIndicator || 'http://images2.wikia.nocookie.net/dev/images/8/82/Facebook_throbber.gif',
var refreshText = 'Auto-refresh';
ajaxTimer,
var refreshHover = 'Enable auto-refreshing page loads';
refreshText = typeof window.AjaxRCRefreshText === 'string' ? window.AjaxRCRefreshText : 'AJAX',
var doRefresh = true;
refreshHover = typeof window.AjaxRCRefreshHoverText === 'string' ? window.AjaxRCRefreshHoverText : 'Enable auto-refreshing page loads',
var ajaxBC = '#bodyContent';
ajRefresh = window.ajaxRefresh || 30000,
 
ajCallAgain = window.ajaxCallAgain || [],
/**
ajPages = window.ajaxPages || [ 'Special:RecentChanges' ];
* Sets the cookie
   
* @param c_name string Name of the cookie
   
* @param value string 'on' or 'off'
function storage( setTo ) {
  * @param expiredays integer Expiry time of the cookie in days
if ( localStorage.getItem( 'refresh' ) === null ) {
  */
localStorage.setItem( 'refresh', true );
function setCookie(c_name, value, expiredays) {
}
  var exdate = new Date();
if ( setTo === false ) {
  exdate.setDate(exdate.getDate() + expiredays);
localStorage.setItem( 'refresh', false );
  document.cookie = c_name + "=" + escape(value) + ((expiredays === null) ? "" : ";expires=" + exdate.toGMTString());
} else if ( setTo === true ) {
}
localStorage.setItem( 'refresh', true );
 
}
/**
return JSON.parse( localStorage.getItem( 'refresh' ) );
* Gets the cookie
}
* @param c_name string Cookie name
* @return The cookie name or empty string
*/
/**
function getCookie(c_name) {
* Main function to start the Auto-refresh process
  if (document.cookie.length > 0) {
*/
      var c_start = document.cookie.indexOf(c_name + "=");
function preloadAJAXRL() {
      if (c_start !== -1) {
var appTo = ( $( '#WikiaPageHeader' ).length ) ? $( '#WikiaPageHeader' ) : ( $( '#AdminDashboardHeader' ).length ? $( '#AdminDashboardHeader > h1' ) : $( '.firstHeading' ) );
        c_start = c_start + c_name.length + 1;
appTo.append( '&nbsp;<span style="font-size: xx-small; line-height: 100%;" id="ajaxRefresh"><span style="border-bottom: 1px dotted; cursor: help;" id="ajaxToggleText" title="' + refreshHover + '">' + refreshText + ':</span><input type="checkbox" style="margin-bottom: 0;" id="ajaxToggle"><span style="display: none;" id="ajaxLoadProgress"><img src="' + ajaxIndicator + '" style="vertical-align: baseline; float: none;" border="0" alt="Refreshing page" /></span></span>' );
        var c_end = document.cookie.indexOf(";", c_start);
$( '#ajaxLoadProgress' ).ajaxSend( function ( event, xhr, settings ) {
        if (c_end === -1) {
if ( location.href === settings.url ) {
            c_end = document.cookie.length;
$( this ).show();
        }
}
        return unescape(document.cookie.substring(c_start, c_end));
} ).ajaxComplete ( function ( event, xhr, settings ) {
      }  
var $collapsibleElements = $( '#mw-content-text' ).find( '.mw-collapsible' );
  }
if ( location.href === settings.url ) {
  return "";
$( this ).hide();
}
for ( var i = 0; i < ajCallAgain.length; i++ ) {
 
ajCallAgain[i]();
/**
}
* Main function to start the Auto-refresh process
if ( $collapsibleElements.length ) {
*/
$collapsibleElements.makeCollapsible();
function preloadAJAXRL() {
}
  var ajaxRLCookie = (getCookie("ajaxload-" + mw.config.get('wgPageName')) == "on") ? true : false;
if ( mw.config.get( 'wgNamespaceNumber' ) === -1 && mw.config.get( 'wgCanonicalSpecialPageName' ) === 'Recentchanges' ) {
 
mw.special.recentchanges.init();
  $('.firstHeading').append('&#160;<span style="font-size: x-small; line-height: 100%;" id="ajaxRefresh"><span id="ajaxToggleText" class="va-tooltip" title="' + refreshHover + '">' + refreshText + ':</span><input type="checkbox" style="vertical-align: bottom;" id="ajaxToggle"></span><span style="display: none; padding-left: 3px;" id="ajaxLoadProgress">' + vaultConfig.loadIndicator + '</span>');
if ( $( '.mw-recentchanges-table' ).find( '.WikiaDropdown' ).length ) {
 
RecentChangesLocal.init();
  $('#ajaxLoadProgress').ajaxSend(function(event, xhr, settings) {
}
      if (location.href == settings.url) {
}
        $(this).show();
if ( mw.config.get( 'wgNamespaceNumber' ) === -1 && mw.config.get( 'wgCanonicalSpecialPageName' ) === 'WikiActivity' ) {
      }
window.WikiActivity.init();
  } ).ajaxComplete(function(event, xhr, settings) {
}
      if (location.href == settings.url) {
}
        $(this).hide();
} );
 
$( '#ajaxToggle' ).click( toggleAjaxReload );
        // Re-run certain functions
$( '#ajaxToggle' ).attr( 'checked', storage());
        if ($(ajaxBC + ' .mw-collapsible').length) {
if ( storage() === true ) {
            $(ajaxBC + ' .mw-collapsible').makeCollapsible();
loadPageData();
        }
}
 
}
        if (mw.config.get("wgNamespaceNumber") == -1  
            && mw.config.get("wgCanonicalSpecialPageName") == "Recentchanges") {
/**
            mw.special.recentchanges.init();
* Turn refresh on and off by toggling the checkbox
        }
*/
 
function toggleAjaxReload() {
        if (typeof messagingImprovements !== 'undefined') {
if ( $( '#ajaxToggle' ).prop( 'checked' ) === true ) {
            messagingImprovements();
storage( true );
        }
loadPageData();
    }
} else {
  } );
storage( false );
 
clearTimeout( ajaxTimer );
  $('#ajaxToggle').click(toggleAjaxReload).attr('checked', ajaxRLCookie);
}
 
}
  if (getCookie("ajaxload-" + mw.config.get('wgPageName')) == "on") {
      loadPageData();
/**
  }
* Does the actual refresh
}
*/
 
function loadPageData() {
/**
var cC = '#mw-content-text';
* Turn refresh on and off by toggling the checkbox
$( cC ).load( location.href + " " + cC + " > *", function () {
*/
if ( storage() ) {
function toggleAjaxReload() {
ajaxTimer = setTimeout( loadPageData, ajRefresh );
  if ($('#ajaxToggle').prop('checked') === true) {
}
      setCookie("ajaxload-" + mw.config.get('wgPageName'), "on", 30);
} );
      doRefresh = true;
}
      loadPageData();
  } else {
/**
      setCookie("ajaxload-" + mw.config.get('wgPageName'), "off", 30);
* Load the script on specific pages
      doRefresh = false;
* Should we make it load only on view or just not on edit....
      clearTimeout(ajaxTimer);
*/
  }
$( function () {
}
if ( $.inArray( mw.config.get( 'wgPageName' ), ajPages ) !== -1 && $( '#ajaxToggle' ).length === 0 && mw.config.get( 'wgAction' ) !== 'edit' ) {
 
preloadAJAXRL();
/**
}
* Does the actual refresh
} );
*/
function loadPageData() {
/**
  $(ajaxBC).load(location.href + " " + ajaxBC + " > *", function (data) {
* Temp Hack: copy the RC filter JS since it can't be accessed
      if (doRefresh) {
*/
        ajaxTimer = setTimeout(loadPageData, ajaxRefresh);
var RecentChangesLocal = {
      }
init: function() {
  });
this.$table = $('.mw-recentchanges-table');
}
this.$dropdown = this.$table.find('.WikiaDropdown');
 
this.$submit = this.$table.find('input[type="submit"]');
jQuery(function($) {
this.$submit.on('click.RecentChangesDropdown', $.proxy(this.saveFilters, this));
  for (var x in ajaxPages) {
this.$submit.removeAttr('disabled'); //FF clean
      if (mw.config.get('wgPageName') == ajaxPages[x] && $("#ajaxToggle").length == 0) {
        preloadAJAXRL();
this.dropdown = new Wikia.MultiSelectDropdown(this.$dropdown);
      }
this.dropdown.on('change', $.proxy(this.onChange, this));
  }
});
},
 
/* </nowiki> */
saveFilters: function(event) {
var self = this;
event.preventDefault();
self.dropdown.disable();
self.$submit.attr('disabled', 'disabled');
if(self.dropdown.getSelectedValues().length == 0) {
self.dropdown.doSelectAll(true);
}
$.nirvana.sendRequest({
controller: 'RecentChangesController',
method: 'saveFilters',
data: {
filters: self.dropdown.getSelectedValues()
},
type: 'POST',
format: 'json',
callback: function(data) {
window.location.reload();
}
});
}
};
}( jQuery, mediaWiki, this ) );

Latest revision as of 21:26, 30 July 2013

/*
 * ADVANCED AJAX AUTO-REFRESHING ARTICLES
 * Adds a check box at the top of the Recent Changes that auto-refreshes every 30 seconds. 
 */
( function ( $, mw, window ) {
    'use strict';
 
	var	ajaxIndicator = window.ajaxIndicator || 'http://images2.wikia.nocookie.net/dev/images/8/82/Facebook_throbber.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 || 30000,
		ajCallAgain = window.ajaxCallAgain || [],
		ajPages = window.ajaxPages || [ 'Special:RecentChanges' ];
 
 
	function storage( setTo ) {
		if ( localStorage.getItem( 'refresh' ) === null ) {
			localStorage.setItem( 'refresh', true );
		}
		if ( setTo === false ) {
			localStorage.setItem( 'refresh', false );
		} else if ( setTo === true ) {
			localStorage.setItem( 'refresh', true );
		}
		return JSON.parse( localStorage.getItem( 'refresh' ) );
	}
 
 
	/**
	 * Main function to start the Auto-refresh process
	 */
	function preloadAJAXRL() {
		var	appTo = ( $( '#WikiaPageHeader' ).length ) ? $( '#WikiaPageHeader' ) : ( $( '#AdminDashboardHeader' ).length ? $( '#AdminDashboardHeader > h1' ) : $( '.firstHeading' ) );
		appTo.append( '&nbsp;<span style="font-size: xx-small; line-height: 100%;" id="ajaxRefresh"><span style="border-bottom: 1px dotted; cursor: help;" id="ajaxToggleText" title="' + refreshHover + '">' + refreshText + ':</span><input type="checkbox" style="margin-bottom: 0;" id="ajaxToggle"><span style="display: none;" id="ajaxLoadProgress"><img src="' + ajaxIndicator + '" style="vertical-align: baseline; float: none;" border="0" alt="Refreshing page" /></span></span>' );
		$( '#ajaxLoadProgress' ).ajaxSend( function ( event, xhr, settings ) {
			if ( location.href === settings.url ) {
				$( this ).show();
			}
		} ).ajaxComplete ( function ( event, xhr, settings ) {
			var	$collapsibleElements = $( '#mw-content-text' ).find( '.mw-collapsible' );
			if ( location.href === settings.url ) {
				$( this ).hide();
				for ( var i = 0; i < ajCallAgain.length; i++ ) {
					ajCallAgain[i]();
				}
				if ( $collapsibleElements.length ) {
					$collapsibleElements.makeCollapsible();
				}
				if ( mw.config.get( 'wgNamespaceNumber' ) === -1 && mw.config.get( 'wgCanonicalSpecialPageName' ) === 'Recentchanges' ) {
					mw.special.recentchanges.init();
					if ( $( '.mw-recentchanges-table' ).find( '.WikiaDropdown' ).length ) {
						RecentChangesLocal.init();
					}
				}
				if ( mw.config.get( 'wgNamespaceNumber' ) === -1 && mw.config.get( 'wgCanonicalSpecialPageName' ) === 'WikiActivity' ) {
					window.WikiActivity.init();
				}
			}
		} );
		$( '#ajaxToggle' ).click( toggleAjaxReload );
		$( '#ajaxToggle' ).attr( 'checked', storage());
		if ( storage() === true ) {
			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 cC = '#mw-content-text';
		$( cC ).load( location.href + " " + cC + " > *", function () {
			if ( storage() ) {
				ajaxTimer = setTimeout( loadPageData, ajRefresh );
			}
		} );
	}
 
	/**
	 * Load the script on specific pages
	 * Should we make it load only on view or just not on edit....
	 */
	$( function () {
		if ( $.inArray( mw.config.get( 'wgPageName' ), ajPages ) !== -1 && $( '#ajaxToggle' ).length === 0 && mw.config.get( 'wgAction' ) !== 'edit' ) {
			preloadAJAXRL();
		}
	} );
 
	/**
	 * Temp Hack: copy the RC filter JS since it can't be accessed
	 */
	var RecentChangesLocal = {
		init: function() {
			this.$table = $('.mw-recentchanges-table');
			this.$dropdown = this.$table.find('.WikiaDropdown');
			this.$submit = this.$table.find('input[type="submit"]');
			this.$submit.on('click.RecentChangesDropdown', $.proxy(this.saveFilters, this));
			this.$submit.removeAttr('disabled'); //FF clean
 
			this.dropdown = new Wikia.MultiSelectDropdown(this.$dropdown);
			this.dropdown.on('change', $.proxy(this.onChange, this));
 
		},
 
		saveFilters: function(event) {
			var self = this;
 
			event.preventDefault();
 
			self.dropdown.disable();
			self.$submit.attr('disabled', 'disabled');
 
			if(self.dropdown.getSelectedValues().length == 0) {
				self.dropdown.doSelectAll(true);
			}
 
			$.nirvana.sendRequest({
				controller: 'RecentChangesController',
				method: 'saveFilters',
				data: {
					filters: self.dropdown.getSelectedValues()
				},
				type: 'POST',
				format: 'json',
				callback: function(data) {
					window.location.reload();
				}
			});
		}
	};
}( jQuery, mediaWiki, this ) );