User:Warpshard/common.js

$(function{

if( mw.config.get("skin") !== "fandomdesktop" ){ return; } if("ontouchstart" in window){ // only when this is not registered in common.js	return; }

//this is much faster than mw.loadMessage var l10n = (function{	var data = {		en: {			watchlist: 'My Watchlist', // in my account droplist			view: 'View', // in right tabs			more: 'More', // in right tabs			share: 'Share', // in left tabs			pagetools: 'Page Tools', // in sidebar, section heading			whatlinkshere: 'What links here',			'cargo-pagevalues': 'Page values', //in sidebar, under "Page Tools"			'recentchangeslinked': 'Related changes', //in sidebar, under "Page Tools"			'log': 'Logs',			searchText: 'Search '+mw.config.get('wgSiteName'), //placeholder for search box			searchGo: 'Go', //text for search button in search box 			searchGoTitle: 'Go to a page with this exact name if it exists' //hover text for search button		},		zh: {			watchlist: '我的监视列表',			view: '查看',			more: '更多',			share: '分享',			pagetools: '页面工具', 			whatlinkshere: '链入页面',			'cargo-pagevalues': '页面值',			'recentchangeslinked': '相关更改',			'log': '日志',			searchText: '搜索 '+mw.config.get('wgSiteName'), searchGo: '搜索', searchGoTitle: '若存在标题完全匹配的页面，则直接前往该页面' }			};	var lang = mw.config.get("wgUserLanguage"); return function (key) { // (null==undefined) is true,(null===undefined) is false return (data[lang] && data[lang][key] != null) ? data[lang][key] : data.en[key]; }; });

//unused, remove $('.fandom-sticky-header').remove;

(function{	var $netbar = $('.global-navigation__links');	//move gp logo to top bar	$('.fandom-community-header__top-container').appendTo($netbar);	//move wiki tools to aside, theme switch to netbar	var $box = $('.fandom-community-header__local-navigation .extra-large-navigation > :first-child .wds-list');	$('.wiki-tools').first		.find('a').removeClass('wds-button')		.appendTo($box).wrap('')		.filter('.wiki-tools__theme-switch')		.appendTo($netbar);	var $recent = $box.find('a[data-tracking="recent-changes"]');	$(' ', {text: $recent.attr('title').split('[')[0]}).appendTo($recent);	$box.find('a[data-tracking="explore-main-page"]').parent.after( $recent.parent, $box.find('a[data-tracking="explore-random"]').parent ); });

(function{	var $list = $('#global-navigation-user-signout').parent;	$('.wds-avatar').append($('', {href: $list.find('a[data-tracking-label="account.profile"]').attr('href')}));	var actions = mw.config.get('wgWikiaPageActions') || [];	for(var i in actions){		var info = actions[i];       if(info.id === 'special:watchlist'){            $('').append($('', {text: l10n('watchlist'), href: info.href})).insertBefore($list.find('[data-tracking-label="account.talk"]').parent);            break;        }    } });

//index wiki nav sections:(for collapsed/expanded status memory) $(".fandom-community-header__local-navigation .extra-large-navigation > .wds-dropdown").each(function(index, div){	div.dataset.index = index;	if($.cookie('hydra-nav-'+index) === "y"){		$(div).addClass('collapsed').find('.wds-dropdown__content').css('display', 'none');	} });

//build page tools on aside (faster than move from right rail after it loaded.) (function{	var tools = mw.config.get("wgRailModuleParams");	if(tools){		tools = tools.toolbox;	}	else{		tools = mw.config.get("wgWikiaBarSkinData").navUrls;	}	delete tools.mainpage;	delete tools.upload;	delete tools.specialpages;	var $box = $( ' '+		''+ ' '+l10n('pagetools')+' '+ ' '+ ' '+		''+ ''+ ' '+		' ')		.appendTo($('.fandom-community-header__local-navigation .extra-large-navigation'))		.find('ul.wds-list');	$.each(tools, function(key, item){ if(!item){ return; }		$('</a>', {			href: item.href,			text: item.text || l10n(key) || item.msg,			id: item.id || 't-'+key,			rel: item.rel		}).appendTo($box).wrap('</li>'); });	if($box.children.length === 0){		$box.closest('div.wds-dropdown').remove;	}	else{		if($.cookie('hydra-nav-pagetools') === "y"){			$box.closest('.wds-dropdown').addClass('collapsed')				.find('.wds-dropdown__content').css('display', 'none');		}		$box		.prepend( $box.find('#t-whatlinkshere').parent, $box.find('#t-recentchangeslinked').parent )		.append( $box.find('#t-info').parent, $box.find('#t-cargopagevalueslink').parent );	} });

//move language interwiki links to aside: (function{	var $languages = $('.page-footer__languages');	if(!$languages.length){ return; }	$languages.removeClass.addClass('wds-dropdown page-footer__languages').attr('data-index', 'languages')		.appendTo($('.fandom-community-header__local-navigation .extra-large-navigation'));	var $header = $languages.find('.wds-collapsible-panel__header');	$('<div class="wds-tabs__tab-label wds-dropdown__toggle first-level-item">'+ ' '+$header.text+' </a>'+ '<svg class="wds-icon wds-icon-tiny wds-dropdown__toggle-chevron"><use xlink:href="#wds-icons-dropdown-tiny"> '+ ' ').prependTo($languages);	$header.remove;	$languages.find('.wds-collapsible-panel__content')		.removeClass.addClass('wds-dropdown__content wds-is-not-scrollable')		.find('a').wrap('</li>');	$languages.find('.wds-dropdown__content li')		.wrapAll('<ul class="wds-list wds-is-linked"></ul>');	if($.cookie('hydra-nav-languages') === "y"){		$languages.addClass('collapsed')			.find('.wds-dropdown__content').css('display', 'none');	} });

//click to toggle expand/collapse for local nav dropdown lists: $(".fandom-community-header__local-navigation").on("click", '.wds-dropdown__toggle.first-level-item', function(event){	event.stopPropagation;	var $item = $(this).closest('.wds-dropdown');	$item.toggleClass('collapsed').find('.wds-dropdown__content').slideToggle('fast');	$.cookie('hydra-nav-' + $item[0].dataset.index, $item.hasClass('collapsed')?"y":"n", {expires: 365, path: '/'}); });

//remove empty  from aside nav. $(".fandom-community-header__local-navigation li:empty").remove;

//build topbar. var $topbar = (function{	var $topbar = $('.page-side-tools__wrapper');	if(!$topbar.length){ return; }

var $left = $('<ul class="left-tabs"></ul>'); var $right = $(		'<ul class="right-tabs">'+		'<li class="wds-dropdown">'+		'<div class="wds-dropdown__toggle"> ' + l10n('more') + ' <svg class="wds-icon wds-icon-tiny wds-dropdown__toggle-chevron"><use xlink:href="#wds-icons-dropdown-tiny"> '+		'<div class="wds-dropdown__content"><ul class="wds-list wds-is-linked"></ul> '+		'</li>'+		'</ul>'); $topbar.empty.append($left, $right); var $more = $right.find('.wds-list'); var $cactions = $('#p-cactions');

var actions = mw.config.get('wgWikiaBarSkinData'); actions = actions && actions.contentActions; (function{		if(!actions){ return; }		//build left/right tabs:		delete actions['cargo-purge']; //there is actions.purge already		var $target = $left;		$.each(actions, function(key, item){ if(key.substring(0,8) === 'varlang-'){ return; } //skip language variants if(key === 'share'){ $('<li class="wds-dropdown" id="ca-share">'+					'<div class="wds-dropdown__toggle">'+					' ' + l10n('share') + ' '+					'<svg class="wds-icon wds-icon-tiny wds-dropdown__toggle-chevron"><use xlink:href="#wds-icons-dropdown-tiny"> '+					' '+					'<div class="wds-dropdown__content">' + item.html + ' '+					'</li>').appendTo($left); return; }			if(key === 'edit' || key === 'viewsource'){ $target = $right; }else if(!item.primary && key !== 'history'){ $target = $more; }			var $z = $cactions.find('#'+item.id); if($z.length){ $z.closest('li').appendTo($target); //reuse the item from the dropdown list }			else{ //build a new one var $a = $('</a>', {					accesskey: item.accesskey,					id: item.id,					class: (item.class||) + (item.exists===false?' new':),					href: item.href,					text: item.text,				}); if(item.data){ $.each(item.data, function(index) {						$a.attr('data-'+index, item.data[index]);					}); }				$('</li>',{class: $a.attr('class')}).append($a).appendTo($target); }			//workaround for watch/unwatch: take out watch/unwatch from more menu, and set id/class on li element to make mediawiki.page.watch.ajax work properly. var $watch = $more.find('a.mw-watchlink'); $watch.closest('li').addClass('mw-watchlink').attr('id', $watch.attr('id')).appendTo($right); $watch.attr('id', null); });	});	//Is there anything left in "old" dropdown list? $cactions.find('ul li').appendTo($more); //removed old p-views area. $('#p-views').remove; $more.attr('id', 'p-cactions'); $right.attr('id', 'p-views');

//need "view" for right tabs? (function{		if(!(actions.edit || actions.viewsource)){			return;		}		var $view = $left.find('li.selected').clone.attr('id', 'ca-view').prependTo($right);		$view.find('a').text(l10n('view'));		if($right.find('li.selected').length > 1){			$view.removeClass('selected');		}	});

//language variants to left tabs (for some non-English languages such as Chinese) (function{		var $variants = $('.page-header__top .page-header__variants .wds-dropdown');		if($variants.length === 0){ return; }		$('<li class="wds-dropdown" id="p-variants"></li>').append($variants.children).appendTo($left)		.find('.wds-dropdown__toggle').children.first.wrap(' ');		//move share to the end		$left.find('#ca-share').appendTo($left);	});

//merge my tools bar to top right bar: (function{		var $mytools = $('#WikiaBar ul.tools');		$mytools.find('li.overflow').each(function(index, li){ var $li = $(li); var $a = $li.find('a'); var id = $a.attr('id') || 'ca-'+$a.attr('data-name'); if(id){ if($right.find('#'+id).length){ return; }				$a.attr('id', id); }			$li.appendTo($li.parent.hasClass('wds-list') ? $more : $right); });		if($more.children.length){			$more.closest('.wds-dropdown').appendTo($right); //move more menu to the end			mw.loader.using("ext.fandom.wikiaBar.js").then(function{ $more.append($mytools.find('li.tools-customize')); });		}		else{			$more.closest('.wds-dropdown').remove;		}	}); return $topbar; }); //end toolbar

//Search form: (function{	/** taked from Joritochip's HydraRevived (https://dev.fandom.com/wiki/HydraRevived) **/	var sitename = mw.config.get('wgSiteName');	$(' ', {action: mw.config.get('wgScript'), id: 'searchform'}).appendTo($topbar).append( $(' ', {id: 'simpleSearch'}).append(			$(' ', { type: 'search', name: 'search', placeholder: l10n('searchText'), title: l10n('searchText'), id: 'searchInput', tabindex: '1', accesskey: 'f', autocomplete: 'off' }),			$(' ', {				type: 'hidden', value: 'Special:Search', name: 'title' }),			$(' ', {				type: 'submit', name: 'go', value: l10n('searchGo'), title: l10n('searchGoTitle'), id: 'searchButton', class: 'searchButton' })		)	);	/* autocomplete	 * (based on mediawiki.searchSuggest: * https://gerrit.wikimedia.org/r/plugins/gitiles/mediawiki/core/+/refs/heads/master/resources/src/mediawiki.searchSuggest/searchSuggest.js)	 */	var searchNS = $.map(mw.config.get('wgFormattedNamespaces'), function(nsName, nsID) { if (nsID >= 0 && mw.user.options.get('searchNs' + nsID)) { return Number(nsID); }	});	mw.searchSuggest = {		request: function(api, query, response, maxRows, namespace) {			return api.get({ formatversion: 2, action: 'opensearch', search: query, namespace: namespace || searchNS, limit: maxRows, suggest: !0 }).done(function(data, jqXHR) { response(data[1], {					type: jqXHR.getResponseHeader('X-OpenSearch-Type'),					searchId: jqXHR.getResponseHeader('X-Search-ID'),					query: query				}); });		}	};

var api, searchboxesSelectors, $searchRegion = $('#simpleSearch, #searchInput').first, $searchInput = $('#searchInput'), previousSearchText = $searchInput.val;

function getFormData(context) { var $form, baseHref, linkParams; if (!context.formData) { $form = context.config.$region.closest('form'); baseHref = $form.attr('action'); baseHref += baseHref.indexOf('?') > -1 ? '&' : '?';			linkParams = $form. serializeObject; context.formData = { textParam: context.data.$textbox.attr('name'), linkParams: linkParams, baseHref: baseHref };		}		return context.formData; }

function onBeforeUpdate { var searchText = this.val; if (searchText && searchText !== previousSearchText) { mw.track('mediawiki.searchSuggest', {				action: 'session-start'			}); }		previousSearchText = searchText; }

function getInputLocation(context) { return context.config.$region.closest('form').find('[data-search-loc]').data('search-loc') || 'header'; }

function onAfterUpdate(metadata) { var context = this.data('suggestionsContext'); mw.track('mediawiki.searchSuggest', {			action: 'impression-results',			numberOfResults: context.config.suggestions.length,			resultSetType: metadata.type || 'unknown',			searchId: metadata.searchId || null,			query: metadata.query,			inputLocation: getInputLocation(context)		}); }

function renderFunction(text, context) { var formData = getFormData(context), textboxConfig = context.data.$textbox.data('mw-searchsuggest') || {}; formData.linkParams[formData.textParam] = text; mw.track(			'mediawiki.searchSuggest', {				action: 'render-one',				formData: formData,				index: context.config.suggestions.indexOf(text)			}); this.text(text); if (textboxConfig.wrapAsLink !== false) { this.wrap($('').attr('href', formData.baseHref + $.param(formData.linkParams)).attr('title', text).addClass('mw-searchSuggest-link')); }	}

function selectFunction($input, source) { var context = $input.data('suggestionsContext'), text = $input.val; if (source !== 'keyboard') { mw.track('mediawiki.searchSuggest', {				action: 'click-result',				numberOfResults: context.config.suggestions.length,				index: context.config.suggestions.indexOf(text)			}); }		return true; }

function specialRenderFunction(query, context) { var $el = this, formData = getFormData(context); formData.linkParams[formData.textParam] = query; mw.track('mediawiki.searchSuggest', {			action: 'render-one',			formData: formData,			index: context.config.suggestions.indexOf(query)		}); if ($el.children.length === 0) { $el.append($(' ').addClass('special-label').text(mw.msg('searchsuggest-containing')), $(' ').addClass( 'special-query').text(query)).show; } else { $el.find('.special-query').text(query); }		if ($el.parent.hasClass('mw-searchSuggest-link')) { $el.parent.attr('href', formData.baseHref + $.param(formData.linkParams) + '&fulltext=1'); } else { $el.wrap($('').attr('href', formData.baseHref + $.param(formData.linkParams) + '&fulltext=1').addClass('mw-searchSuggest-link')); }	}	searchboxesSelectors = ['#searchInput', '.mw-searchInput']; $(searchboxesSelectors.join(', ')).suggestions({		fetch: function(query, response, maxRows) {			var node = this[0];			api = api || new mw.Api;			$.data(node, 'request', mw.searchSuggest.request(api, query, response, maxRows));		},		cancel: function {			var node = this[0],				request = $.data(node, 'request');			if (request) {				request.abort;				$.removeData(node, 'request');			}		},		result: {			render: renderFunction,			select: function {				return true;			}		},		update: {			before: onBeforeUpdate,			after: onAfterUpdate		},		cache: !0,		highlightInput: !0	}).on('paste cut drop', function {		$(this).trigger('keypress');	}).each(function {		var $this = $(this);		$this.data( 'suggestions-context').data.$container.css('fontSize', $this.css('fontSize'));	}); if ($searchRegion.length === 0) { return; }	$searchInput.suggestions({		update: {			before: onBeforeUpdate,			after: onAfterUpdate		},		result: {			render: renderFunction,			select: selectFunction		},		special: {			render: specialRenderFunction,			select: function($input, source) {				var context = $input.data('suggestionsContext'),					text = $input.val;				if (source === 'mouse') {					mw.track('mediawiki.searchSuggest', { action: 'click-result', numberOfResults: context.config.suggestions.length, index: context.config.suggestions.indexOf(text) });				} else {					$input.closest('form').append($(' ').prop({						type: 'hidden',						value: 1					}).attr('name', 'fulltext'));				}				return true;			}		},		$region: $searchRegion	}); $searchInput.closest('form').on('submit', function {		var context = $searchInput.data('suggestionsContext');		mw.track('mediawiki.searchSuggest', { action: 'submit-form', numberOfResults: context.config.suggestions.length, $form: context.config.$region.closest('form'), inputLocation: getInputLocation(context), index: context.config.suggestions.indexOf(context.data.$textbox.val) });	}).find('.mw-fallbackSearchButton').remove; });

//expand? (function{	$body = $('body');	$(' '+ '<svg class="wds-icon wds-icon-small"><use href="#wds-icons-zoom-out-small"> '+ '<svg class="wds-icon wds-icon-small"><use href="#wds-icons-zoom-in-small"> '+ ' ')	.prependTo($topbar)	.on('click', function{ $body.toggleClass('hydralize-content-expanded'); //$.cookie('hydralize-content-expanded', $body.hasClass('hydralize-content-expanded')?"y":"n", {expires: 365, path: '/'}); });	//$body.toggleClass('hydralize-content-expanded', $.cookie('hydralize-content-expanded') === "y"); });

//add a flag class to body element for other scripts. $('body').addClass('hydralized');

}); //end $