User:M art in/Common.js

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

if (mw.config.get('wgCanonicalSpecialPageName') !== 'Search') { return; }

mw.loader.using(['ext.relatedArticles.readMore'], function {

var $searchResultEls = $('.mw-search-results li');

var DeferredWidget = function (config) { var reqResponse = []; var pub = {}; var template = ''; var hasData = $.Deferred; var articleTitle = config.articleTitle || ''; var apiEndpoint = config.apiEndpoint || '';

var getTemplate = function { return template };

var getData = function { $.ajax({					url: apiEndpoint,					format: 'json'				}) .complete(function (response) {						reqResponse = response;						hasData.resolve;					}) return hasData; };

hasData.done(function {				var filteredReqResponse = filterApiResponse(reqResponse);				template = compileTemplate(filteredReqResponse);			});

var filterApiResponse = config.filterApiResponse || function (reqResponse) { return reqResponse; };

var outerTemplate = config.outerTemplate || function (innerTemplate) { return " " + encodeURIComponent(innerTemplate) + " "; };

var innerTemplate = config.innerTemplate || function (data) { return JSON.stringify(data); }

var compileTemplate = function (data) { if (data.length === 0) { return 'no data available'; } else { return outerTemplate(						innerTemplate(data)					); }			};

return { hasData: hasData, getData: getData, getTemplate: getTemplate, }		};

var RelatedCategoriesWidget = function (articleTitle) { var config = { apiEndpoint: "/w/api.php?action=query&format=json&prop=categories&titles=" + articleTitle + "&clprop=hidden", filterApiResponse: function (reqResponse) { var filteredResp = []; if (!reqResponse.responseJSON.query) { return filteredResp; }					for (var page in reqResponse.responseJSON.query.pages) { filteredResp = reqResponse.responseJSON.query.pages[page].categories || categories; }					return filteredResp.filter(function (el, i) {						return el.hidden === undefined;					}); },				outerTemplate: function (innerTemplate) { return "" + " Categories " + " " +								innerTemplate + " " +						" ";				},				innerTemplate: function (data) { var template = ''; for (var i = 0; i < data.length; i++) { var cat = data[i]; var catTitle = cat.title; var humanTitle = catTitle.replace(/.*:/, ''); template += "" + humanTitle + ""; }					return template; }			}			return DeferredWidget.call(this, config); }

var RelatedPagesWidget = function (articleTitle) { var config = { apiEndpoint: "/w/api.php?action=query&format=json&formatversion=2&prop=pageimages|pageterms&piprop=thumbnail&pithumbsize=160&wbptterms=description&pilimit=3&generator=search&gsrsearch=morelike:" + articleTitle + "&gsrnamespace=0&gsrlimit=3&gsrqiprofile=classic_noboostlinks&uselang=content&smaxage=86400&maxage=86400", filterApiResponse: function (reqResponse) { return reqResponse.responseJSON.query.pages; },				outerTemplate: function (innerTemplate) { return '' + 'Related pages ' + '' + innerTemplate + '' + ' ';				},				innerTemplate: function (data) { var template = "";

for (var i = 0; i < data.length; i++) { var article = data[i], thumbSrc = (article.thumbnail) ? article.thumbnail.source : '', description = (article.terms) ? article.terms.description : '';

template += '' + ' ' + '' + ' ' +									' ' +										'' + article.title + '' + ' ' +									'' + description + ' ' +								' ' +							''; }

return template; }			}			return DeferredWidget.call(this, config); }

window.ExploreSimilarButton = function ($appendedEl, $parentEl, resultTitle) { var encodedResultTitle = encodeURIComponent(resultTitle); var $button = $(' &bull; ' +				'<a href="#' + encodedResultTitle + '" style="color:grey;">' +				'explore similar' +				'</a>' +				'<small style="font-size: 0.7em" class="explore-similar-icon">&#9660; ' +				' ');

var buttonContentStyle = "position: absolute;background-color: white;box-shadow: rgba(0,0,0,0.5) 0px 2px 4px -1px;padding: 1rem;width: 100%;max-width: 38rem;border-radius: 2px;left: 0;top: 100%;z-index: 1;"; var $buttonContent = $('<div class="explore-similar-content" id="' + encodedResultTitle + '" style="' + buttonContentStyle + '"> ');

var relatedCategoriesWidget = new RelatedCategoriesWidget(resultTitle); var relatedPagesWidget = new RelatedPagesWidget(resultTitle);

$button.data('resultTitle', resultTitle);

$button.one('mouseenter', function {

$button.after($buttonContent); relatedCategoriesWidget.getData.done(function {					$buttonContent.append(relatedCategoriesWidget.getTemplate);				})

relatedPagesWidget.getData.done(function {					$buttonContent.prepend(relatedPagesWidget.getTemplate);				}) $buttonContent.show; });

$button.on('mouseenter', function {				$parentEl.parents('li').addClass('active')				$buttonContent.show;			})

$parentEl.parents('li').on('mouseleave', function {				$parentEl.parents('li').removeClass('active')				$buttonContent.hide;

});

$button.on('click', function (e) {				$parentEl.parents('li').toggleClass('active')				$buttonContent.toggle;			});

return $button;

}

$searchResultEls.each(function (index, el) {			var $el = $(el);			var $parentEl = $el.children('.mw-search-result-data');			var resultTitle = $el.find('.mw-search-result-heading a').attr('title');			var exploreButton = new ExploreSimilarButton($el, $parentEl, resultTitle);			$el.children('.mw-search-result-data').append(exploreButton);		});

});

$("<style type='text/css'>" +	".mw-search-results {" +		"max-width: 40rem;" +	"}" +	".mw-search-results > li {" +		"padding: 1rem;"+		"padding-bottom: 0;" +		"position: relative;" +		"max-width: 40rem;" +		"left: -1rem;" +		"top: -1rem;" +	"}" +	".mw-search-results li.active {"+ 	 	"box-shadow: rgba(0,0,0,0.5) 0px 1px 4px -1px;" +		 "z-index: 2;" +	"} "	).appendTo("head");

})(mediaWiki, jQuery);