Jump to content

User:Gary/user groups.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
/*
	USER GROUPS SCRIPT


	NORMAL GROUPS (18 total, as of February 2, 2011):
		(all), accountcreator, sysop (administrator), autoreviewer (auto patrolled), bot, bureaucrat, checkuser, confirmed, abusefilter (edit filter manager), founder, ipblock-exempt, import, oversight, researcher, reviewer, rollbacker, steward, transwiki	
	
	SPECIAL GROUPS (3)
		(anonymous), autoconfirmed, user
	
	EXAMPLE USAGE (all nouns are in singular form):
		<div class="for-sysop-only"></div>
	
	AND
		<div class="not-for-sysop"></div>
	
	
	NOTES
	
		- (all) has no associated class.
		- The associated class for (anonymous) uses "anonymous".
		- classes can be wrapped around each other. The one with the highest priority is the innermost class.
	
	TODO
	
		- 
*/

if (typeof(unsafeWindow) != 'undefined')
{
	var console = unsafeWindow.console;
	mw = unsafeWindow.mw;
}

// Settings ('X' is replaced with the group)
var hideText = 'not-for-X';
var showText = 'for-X-only';
var hiddenStyle = 'display: none;';
var hiddenContentId = 'hidden-content-';
var userGroupContentClass = 'user-group-content';

mw.util.addCSS('.hidden-content-link { display: inline-block; font-family: monospace; font-size: 0.9em; font-weight: bold; vertical-align: top; /*width: 15px;*/ }\
a.hidden-content-link:hover { text-decoration: none; }');

// normal user groups (17 total); (all) group is missing (-1)
var allUserGroups = { 'accountcreator': true, 'sysop': true, 'autoreviewer': true, 'bot': true, 'bureaucrat': true, 'checkuser': true, 'confirmed': true, 'abusefilter': true, 'founder': true, 'ipblock-exempt': true, 'import': true, 'oversight': true, 'researcher': true, 'reviewer': true, 'rollbacker': true, 'steward': true, 'transwiki': true, };

// do (anonymous) first; they have NO groups
if (!mw.config.get('wgUserGroups'))
{
	mw.util.addCSS('.' + hideText.replace('X', 'anonymous') + ' { ' + hiddenStyle + ' }');
}
else
{
	for (var group in allUserGroups) createGroupCss(group);

	// special user groups; (anonymous) is missing, will do last
	var specialUserGroups = { 'autoconfirmed': true, 'user': true, };
	for (var group in specialUserGroups) createGroupCss(group);
	
	// (anonymous) users
	mw.util.addCSS('.' + showText.replace('X', 'anonymous') + ' { ' + hiddenStyle + ' }');
}

// Add links to expand hidden content.
// Find all occurrences and append (...), which is clickable.
function expandHiddenContent()
{
	// Find all hidden content and add a class to them for other uses
	allUserGroups['anonymous'] = true;
	for (var group in allUserGroups)
	{
		$('.' + hideText.replace('X', group)).addClass(userGroupContentClass);
		$('.' + showText.replace('X', group)).addClass(userGroupContentClass);
	}
	
	$('.' + userGroupContentClass).each(function(i)
	{
		var hidden = $(this);
		var groupInfo = findUserGroup(hidden.attr('class').split(' '));
		if (!groupInfo) return true;
		
		// content is shown
		if (hidden.css('display') != 'none')
		{
			hidden.attr('title', 'This content is ' + (groupInfo[1] ? 'shown only to' : 'hidden only from') + ' members of the \'' + groupInfo[0] + '\' user group.');
		}
		// content is hidden
		else
		{
			hidden.attr('id', hiddenContentId + i);
			var expand = $('<a class="hidden-content-link" href="javascript:toggleHiddenContent(' + i + ')" title="This content is ' + (groupInfo[1] ? 'shown only to' : 'hidden only from') + ' members of the \'' + groupInfo[0] + '\' user group.">(+)</a>');
			var newNode = $('<div></div>');
			hidden.after(newNode);
			newNode.append(expand).append(' ').append(hidden);
		}
	});
}

function findUserGroup(arrayOfClasses)
{
	for (var group in allUserGroups)
	{
		if ($.inArray(hideText.replace('X', group), arrayOfClasses) != -1) return [group, 0];
		if ($.inArray(showText.replace('X', group), arrayOfClasses) != -1) return [group, 1];
	}
	return false;
}

toggleHiddenContent = function(i)
{
	var id = $('#' + hiddenContentId + i);
	var text = id.prev().contents().first()[0];
	var display = id.css('display');
	
	if (display == 'none')
	{
		id.css('display', 'inline');
		text.nodeValue = '(-)';
	}
	else
	{
		id.css('display', 'none');
		text.nodeValue = '(+)';
	}
}

$(expandHiddenContent);

function createGroupCss(group)
{
	if (userHasGroup(group)) mw.util.addCSS('.' + hideText.replace('X', group) + ' { ' + hiddenStyle + ' }'); // 'not-for-X'
	else mw.util.addCSS('.' + showText.replace('X', group) + ' { ' + hiddenStyle + ' }'); // 'for-X-only'	
}

function userHasGroup(group)
{
	for (var i = 0; i < mw.config.get('wgUserGroups').length; i++)
	{
		if (group == mw.config.get('wgUserGroups')[i])
			return true;
	}

	return false;
}

if (typeof(unsafeWindow) != 'undefined')
{
	unsafeWindow.toggleHiddenContent = toggleHiddenContent;
}