Club Penguin Archives

The Club Penguin Wiki Network was abandoned by its tech staff. This is a recreation of the original site. Your original CPWN account will not work here. Any issues, please contact Sky.

User:Snowstormer/common.js

From Club Penguin Archives
Jump to navigation Jump to search

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
ajaxPages = [
      "Special:Contributions",
      "Special:Log",
      "Special:RecentChanges",
      "Special:Watchlist"
];
window.ajaxRefresh = 5000;

( function ( window, $, mw ) {
	'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 || 'http://www.prestonbaker.co.uk/loading.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' ],
		RecentChangesLocal,
		// don't load on these values of wgAction
		// @todo check if markpatrolled should be here
		disallowActions = [
			'delete',
			'edit',
			'protect',
			'revisiondelete'
		];


	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' ) );
	}


	/**
	 * Main function to start the Auto-refresh process
	 */
	function preloadAJAXRL() {
		// monobook
		var $appTo = $( '.firstHeading' ).length ? $( '.firstHeading' ) :
			// most oasis pages
			( $( '#WikiaPageHeader' ).length ? $( '#WikiaPageHeader' ) :
				// most oasis special pages
				( $( '#AdminDashboardHeader' ).length ? $( '#AdminDashboardHeader > h1' ) : false ) ),
			$checkbox = $( '<span id="ajaxRefresh"></span>' )
				.css( { 'font-size': 'xx-small', 'line-height': '100%', 'margin-left': '5px' } )
				.append(
					$( '<label id="ajaxToggleText" for="ajaxToggle"></label>' )
						.css( { 'border-bottom': '1px dotted', 'cursor': 'help' } )
						.attr( 'title', refreshHover ).text( refreshText + ':' ),
					$( '<input type="checkbox" id="ajaxToggle">' ).css( { 'margin-bottom': 0 } ),
					$( '<span id="ajaxLoadProgress"></span>' ).css( 'display', 'none' ).append(
						$( '<img>' ).css( { 'vertical-align': 'baseline', 'float': 'none', 'border': 0 } )
							.attr( 'src', ajaxIndicator ).attr( '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 ( event, xhr, settings ) {
			if ( location.href === settings.url ) {
				$throbber.show();
			}
		} ).ajaxComplete ( function ( event, xhr, settings ) {

			var $collapsibleElements = $( '#mw-content-text' ).find( '.mw-collapsible' ),
				ajCallAgain = window.ajaxCallAgain || [],
				i;

			if ( location.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 ) {
						RecentChangesLocal.init();
					}
				}
				for ( i = 0; i < ajCallAgain.length; i++ ) {
					ajCallAgain[i]();
				}
			}
		} );
		$( '#ajaxToggle' ).click( toggleAjaxReload );
		$( '#ajaxToggle' ).attr( 'checked', storage() );
		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 = $( '<div>' );

		$temp.load( location.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 (
			$.inArray( config.wgPageName, ajPages ) !== -1 && $( '#ajaxToggle' ).length === 0 &&
			$.inArray( config.wgAction, disallowActions ) === -1
		) {
			preloadAJAXRL();
		}
	} );

	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();
				}
			} );
		}
	};

}( this, jQuery, mediaWiki ) );