User:Saftzie/global.js

// __NOINDEX__

// define user.util module (window.user = window.user || {}).util = window.user.util || (function {	'use strict';

var self = {		// try to prevent double loading of scripts preview: function (page) {			user.page = user.page || {}; if (user.page[page] !== undefined) {				throw new Error('user.util.preview: double loading ' + page); }			else {				user.page[page] = true; }		},

// execute loop until cond returns true // then call then // time (optional) = # of msec per loop (default: 100) wait: function wait(cond, then, time) {			if ((typeof cond !== 'function') || (typeof then !== 'function')) {				throw new TypeError('user.util.wait: required wait parameters must be functions'); }			if (cond) {				then; }			else {				time = (typeof time === 'number') ? time : 100; setTimeout(wait, time, cond, then, time); }		}	};	return self; });

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

var wgCanonicalSpecialPageName = mw.config.get('wgCanonicalSpecialPageName'), wgNamespaceNumber = mw.config.get('wgNamespaceNumber'), wgSkin = mw.config.get('skin');

// stop execution of this page on diff and preview user.util.preview('Community:User:Saftzie/global.js');

// redirect Specal:Chat to Oasis (function 	{		var			query, t;

if ((wgCanonicalSpecialPageName === 'Chat') &&	(wgSkin !== 'oasis')) {			query = location.search.slice(1).split('&'); if ((query.length === 1) && (query[0].length === 0)) {			 query = []; }			t = query.filter(function (v)			{	 		return (v.search(/^useskin=/) === 0);			}); if ((t.length === 1) && (t[0] === 'useskin=oasis')) {		 	return; // error, prevent infinite loop }			query = query.filter(function (v)			{		 	return (v.search(/^useskin=/) !== 0);			}); query.push('useskin=oasis'); location.search = '?' + query.join('&'); }	});

// add classes to closed and highlighted thread items if (wgNamespaceNumber === 2000) {		$(function 		{			$('.ThreadList > .thread').each(function {				var li = $(this);

$.post(mw.util.wikiScript('wikia'),				{					controller: 'Forum',					method: 'brickHeader',					id: li.data('id'),					format: 'json'				}, function (data)				{					if (data.isClosed)					{						li.addClass('g-closed');					}					if (data.isNotifyeveryone)					{						li.addClass('g-highlight');					}				}); });		});	}

/* dynamically generated textarea currently not supported edits to Wikia article/blog comments a.article-comm-edit is a#comment -> #article-comm-textfield- edits to replies to Wikia article/blog comments a.article-comm-edit is a#comment -> #article-comm-textfield- edits to Wikia threads a.edit-message has ancestor li#1 -> #1 .body edits to replies to Wikia threads a.edit-message has ancestor li# -> # .body // insert tab for tab key event in edit areas // however, because of Event.timeStamp, //  it's broken in IE 8 and earlier //  and might be broken in Chrome 49 and later $(function 	{		var			wait = user.util.wait,			t = 0;

// insert a tab function tab(textarea) {			var s = textarea.selectionStart, t = textarea.scrollTop, v = textarea.value;

textarea.value = v.substr(0, s) + '\t' + v.substr(textarea.selectionEnd); textarea.scrollTop = t;			textarea.selectionStart = textarea.selectionEnd = s + 1; }

// attach keydown and keypress handlers to DOM elements function handle(select) {			$(select).keydown(function (e)			{				var					epoch = e.timeStamp;

if (e.keyCode === 9) {					e.preventDefault; if (typeof e.stopPropagation === 'function') { // required for replies to Wikia threads e.stopPropagation; }					if (epoch !== t)					{ tab(this); t = epoch; }				}			}).keypress(function (e) {				var epoch = e.timeStamp;

if (e.keyCode === 9) {					e.preventDefault; if (epoch !== t)					{ tab(this); t = epoch; }				}			});		}

// some things can take key handlers at document.ready handle(			'#wpTextbox1,'  + // articles, includes Wikia blogs			'#Wall .body,'   + // new Wikia threads			'#Wall .replyBody' // new replies to Wikia threads		);

// article comments need to wait longer if ($('#mw-data-after-content').length > 0) {			wait(function 			{				return $('#article-comm').length > 0;			}, function 			{				// new article comments				handle('#article-comm');				// new replies to article comments				$('button.article-comm-reply').click(function {					var id = $(this).closest('li')[0].id							.replace('comm', '#article-comm-reply-textfield');

wait(function 					{						return $(id).length > 0;					},	function 					{						handle(id);					}); });			});		}	}); }(mediaWiki, jQuery));