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

/*  What doesn't work? 1. Monobook edits to threads, the handler is detached after the first edit 2. Oasis new article comments and edits to article comments, nothing happens 3. Oasis edits to global.js and skin.js do not run global.js (Wikia problem) // 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		); // edits to Wikia threads and replies $('a.edit-message').click(function 		{			var				s = '#' + $(this).closest('li.message')[0].id + ' .body';			wait(function {				return $(s).length > 0; },	function {				handle(s); });		});

// 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 s = $(this).closest('li')[0].id							.replace('comm', '#article-comm-reply-textfield');

wait(function 					{						return $(s).length > 0;					},	function 					{						handle(s);					}); });				// edits to Wikia article comments and replies				$('a.article-comm-edit').click(function {					var s = this.id							.replace('comment', '#article-comm-textfield-'); wait(function 					{						return $(s).length > 0;					},	function 					{						handle(s);					}); });			});		}	}); }(mediaWiki, jQuery));