미디어위키:Gadget-RecentChangesSidebar.js

/* RecentChanges SideBar- @author ykhwong
*/
/*jshint esversion: 6 */
$(function () {
	var langCode = mw.config.get( 'wgContentLanguage' );
	var timeoutIds = ;
	var options = {};
	var messages = {};
	var preMarginRight = $("#mw-content-text").css("margin-right");
	var preMinHeight = $("#mw-content-text").css("min-height");
	const sidebarWidth = 190;
	const minHeight = 400;
	const refreshRate = 10;
	const isVector = /vector/.test( mw.config.get("skin") );
	const isMinerva = /minerva/.test( mw.config.get("skin") );
	const isLegacyVector = ( $(".skin-vector-legacy").length > 0 );
	const pageViewsURI = '//wikimedia.org/api/rest_v1/metrics/pageviews/top/' + langCode + '.wikipedia.org/all-access';
	const recentChangesURI = '/wiki/Special:RecentChanges?hidecategorization=1&hideWikibase=1&limit=15&days=7&urlversion=2';
	const recentChangesWithWdURI = '/wiki/Special:RecentChanges?hidecategorization=1&hideWikibase=0&limit=15&days=7&urlversion=2';
	var msgGrp = {
		'sidebar_title' : {
			'en' : 'Recent changes sidebar',
			'ko' : '최근 바뀜 사이드바'
		},
		'sidebar_desc' : {
			'en' : 'Toggle recent changes sidebar',
			'ko' : '최근 바뀜 사이드바를 토글합니다'
		},
		'more' : {
			'en' : 'More',
			'ko' : '더 보기'
		},
		'top_view' : {
			'en' : 'Top-view',
			'ko' : '많이 본 문서'
		},
		'inc_wd' : {
			'en' : 'Inc. WD',
			'ko' : 'WD 포함'
		}
	};
	var rcText = "";
	var rcSidebarStyle = {
		'position': 'absolute',
		'background-color': 'white',
		'color': 'black',
		'width': sidebarWidth + 'px',
		'top': '0px',
		'right': '0px',
		'padding': '3px',
		'border': 'solid 1px #c8ccd1',
		'border-radius': '5px 5px 0 0',
		'font-size': $('#mw-content-text').css('font-size'),
		'line-height': $('#mw-content-text').css('line-height')
	};
	var rcSidebarMobileStyle = {
		'position': 'relative',
		'color': 'black',
		'top': '0px',
		'right': '0px',
		'padding': '3px',
		'overflow-x': 'hidden !important'
	};
	var rcSidebarTabStyle = {
		'position': 'relative',
		'background-color': 'white',
		'width': (sidebarWidth - 4) + 'px',
		'padding': '5px',
		'margin': '-4px 0px 0px -4px',
		'border': 'solid 1px #c8ccd1',
		'border-bottom': 'solid 2px #3F89F1',
		'border-radius': '5px 5px 0 0'
	};
	var rcSidebarTabMobileStyle = {
		'position': 'relative',
		'background-color': 'white',
		'padding-top': '5px',
		'padding-bottom': '5px',
		'margin': '0px',
		'border-bottom': 'solid 1px #c8ccd1',
		'overflow-x': 'hidden !important'
	};
	var pgViewSidebarSTyle = {
		'border-top': '2px solid #3F89F1',
		'padding': '4px',
		'font-size': 'smaller',
		'margin-top': '5px'
	};
	$.fn.isInViewport = function() {
		if ( $(this).length === 0 ) {
			return false;
		}
		var elementTop = $(this).offset().top;
		var elementBottom = elementTop + $(this).outerHeight();
		var viewportTop = $(window).scrollTop();
		var viewportBottom = viewportTop + $(window).height();
		return elementBottom > viewportTop && elementTop  0 ) {
			return false;
		}
		if ( window.rc_sidebar !== undefined ) {
			if ( window.rc_sidebar.config !== undefined ) {
				options = window.rc_sidebar.config;
			}
			if ( window.rc_sidebar.messages !== undefined ) {
				messages = window.rc_sidebar.messages;
			}
		}
		
		if ( options.enabled !== undefined ) {
			if ( ! options.enabled ) {
				return false;
			}
		}
		if ( isMinerva ) {
			$(".footer-content").append();
			$("#rcSidebar").css(rcSidebarMobileStyle);
			isInited = true;
		} else if ( isVector ) {
			$("#mw-content-text").append();
			$("#rcSidebar").css(rcSidebarStyle);
			isInited = true;
		}
		return isInited;
	}
	function procVector() {
		if ( !isVector ) {
			return false;
		}
		if ( isLegacyVector ) {
			if ($(".mw-indicators").children().length > 0) {
				$("#rcSidebar").css("top", "43px");
			}
			$("#mw-content-text").css("margin-right", (sidebarWidth + 30) + "px");
			$('#catlinks').css('margin-right', (sidebarWidth + 30) + 'px');
			if (mw.config.get("wgCanonicalSpecialPageName") === "Search") {
				$("#rcSidebar").css("margin-right", (-1 * sidebarWidth - 30) + "px");
			} else if (/^(AbuseLog|AbuseFilter|Contributions)$/.test(mw.config.get("wgCanonicalSpecialPageName"))) {
				$("#rcSidebar").css("top", "30px");
			}
			$("#mw-content-text").css("min-height", minHeight + "px");
		} else {
			var greyWidth = ( $("body").width() - ( $(".mw-page-container").width() + parseFloat($(".mw-page-container").css("padding-right")) + parseFloat($(".mw-page-container").css("padding-left")) ) ) / 2;
			var ns = mw.config.get( 'wgNamespaceNumber' );
			if ( ns === 14 
ns === -1 
mw.config.get("wgAction") === "history" 
mw.config.get("wgAction") === "edit" 
mw.config.get("wgDiffOldId") !== null ) {
				if (greyWidth - 30  0) {
						$("#rcSidebar").css("top", "43px");
					}
				} else {
					$("#rcSidebar").css("margin-right", -1 * (sidebarWidth + 90) + "px");
					$("#mw-content-text").css({
						'margin-right': 0,
						'min-height': 0
					});
					$('#catlinks').css('margin-right', 0);
					if ($(".mw-indicators").children().length > 0) {
						$("#rcSidebar").css("top", 0);
					}
				}
			} else {
				if ($(".vector-feature-limited-width-enabled").length === 0 
$(".mw-page-container").width() * 100 / $("body").width() > 90 ) {
					if (mw.config.get("wgCanonicalSpecialPageName") === "Search") {
						$("#rcSidebar").css("margin-right", -1 * (sidebarWidth + 30) + "px");
					} else {
						$("#rcSidebar").css("margin-right", 0);
					}
					$("#mw-content-text").css({
						'margin-right': (sidebarWidth + 30) + 'px',
						'min-height': minHeight + 'px'
					});
					$('#catlinks').css('margin-right', (sidebarWidth + 30) + 'px');
					if ($(".mw-indicators").children().length > 0) {
						$("#rcSidebar").css("top", "43px");
					}
				} else {
					$("#rcSidebar").css("margin-right", -1 * (sidebarWidth + 30) + "px");
					$("#mw-content-text").css({
						'margin-right': 0,
						'min-height': 0
					});
					$('#catlinks').css('margin-right', 0);
					if ($(".mw-indicators").children().length > 0) {
						$("#rcSidebar").css("top", 0);
					}
				}
			}
		}
	}
	function getMsg(msgCode) {
		var result = "";
		if ( messagesmsgCode !== undefined ) {
			return messagesmsgCode;
		}
		if (!/\S/.test(langCode)) {
			langCode = "en";
		}
		if (msgGrpmsgCode) {
			if (msgGrpmsgCodelangCode) {
				result = msgGrpmsgCodelangCode;
			} else {
				result = msgGrpmsgCode.en;
			}
		}
		return result;
	}
	function addRcText() {
		$("#rcSidebar").html(
			 + rcText +
			 +
			 +
			getMsg('more') +
			
		);
	}
	function getPageViews(info, date) {
		var retData;
		var cnt = 0;
		retData =  + getMsg('top_view') + ' (' + date.year + '-' + date.month + '-' + date.day + ')';
		retData += ;
		var ignore = false;
		localStorage'mw-recentchanges-sidebar-pageviews' = ;
		$.each( info.items0.articles, function( i, item ) {
			ignore = false;
			var nsgrp = mw.config.get('wgFormattedNamespaces');
			for ( var i2 = 1; i2 ' +
			item.article.replace(/_/g, " ") + "";
			if ( cnt === 10 ) {
				return false;
			}
		});
		retData += ;
		localStorage'mw-recentchanges-sidebar-pageviews' += retData;
		localStorage'mw-recentchanges-sidebar-pageviews-year' = date.year;
		localStorage'mw-recentchanges-sidebar-pageviews-month' = date.month;
		localStorage'mw-recentchanges-sidebar-pageviews-day' = date.day;
		return retData;
	}
	function clearAllTimeout() {
		for (var i = 0; i  0 ) {
			for ( var i = 0; i  span > a:first")0).attr('href') 
$(elem.find(".extiw")0).attr('href');
				var info;
				if ( ! /&diff=/.test(diffLink) ) {
					diffLink = '#';
				}
				if ( !targetPage 

/\S/.test(targetPage) ) {
					targetPage = elem.find(".mw-changeslist-line-inner").data("target-page");
					if ( !targetPage 

/\S/.test(targetPage) ) {
						targetPage = ;
					}
				}
				info = 
					 +
					 + targetPage +  +
					 +
					 + changedDate + "" + ;
				localStorage'mw-recentchanges-sidebar' += info;
				$("#rcSidebar").append(info);
			});
			$("#rcSidebar").append(' ' + getMsg('inc_wd') + );
			if ( localStorage'mw-recentchanges-sidebar-incWD' === 'enabled' ) {
				$("#rcSidebar-incWD").prop('checked', true);
			} else {
				$("#rcSidebar-incWD").prop('checked', false);
			}
			$("#rcSidebar-incWD").change(function() {
				if (!this.checked) {
					localStorage'mw-recentchanges-sidebar-incWD' = 'disabled';
				} else {
					localStorage'mw-recentchanges-sidebar-incWD' = 'enabled';
				}
				timeoutIds.push(setTimeout(function() { autoRefresh(); }, 1000));
			});
			// Add page views
			if ( options.enabled !== undefined ) {
				if ( ! options.top_view ) {
					return;
				}
			}
			if ( localStorage'mw-recentchanges-sidebar-pageviews-year'  

**undefined ) {
				if ( localStorage'mw-recentchanges-sidebar-pageviews-month'  

**undefined ) {
					if ( localStorage'mw-recentchanges-sidebar-pageviews-year'  

**undefined ) {
						$("#rcSidebar").append(localStorage'mw-recentchanges-sidebar-pageviews');
						$("#pgViewSidebar").css(pgViewSidebarSTyle);
						if ( isMinerva ) {
							$("#pgViewSidebar li").css("margin-left", "20px");
						}
						if ( svrYear.toString() === localStorage'mw-recentchanges-sidebar-pageviews-year' &&
						     svrMonth.toString() === localStorage'mw-recentchanges-sidebar-pageviews-month' &&
						     svrDay.toString() === localStorage'mw-recentchanges-sidebar-pageviews-day'
						) {
							usePageViewsCache = true;
						}
					}
				}
			}
			if (usePageViewsCache) {
				timeoutIds.push(setTimeout(function() { autoRefresh(); }, refreshRate * 1000));
			} else {
				$.getJSON(pageViewsURI + '/' + svrYear + '/' + ("0" + svrMonth).slice(-2) + '/' + ("0" + svrDay).slice(-2)).done(function (data) {
					$("#pgViewSidebar").remove();
					$("#rcSidebar").append(getPageViews(data, { month: svrMonth, day: svrDay, year: svrYear }));
					$("#pgViewSidebar").css(pgViewSidebarSTyle);
					if ( isMinerva ) {
						$("#pgViewSidebar li").css("margin-left", "20px");
					}
					timeoutIds.push(setTimeout(function() { autoRefresh(); }, refreshRate * 1000));
				}).fail(function(){
					prevDate = getPrevDate(svrYear, svrMonth, svrDay);
					svrYear = prevDate.year;
					svrMonth = prevDate.month;
					svrDay = prevDate.day;
					$.getJSON(pageViewsURI + '/' + svrYear + '/' + ("0" + svrMonth).slice(-2) + '/' + ("0" + svrDay).slice(-2)).done(function (data) {
						$("#pgViewSidebar").remove();
						$("#rcSidebar").append(getPageViews(data, { month: svrMonth, day: svrDay, year: svrYear }));
						$("#pgViewSidebar").css(pgViewSidebarSTyle);
						if ( isMinerva ) {
							$("#pgViewSidebar li").css("margin-left", "20px");
						}
						timeoutIds.push(setTimeout(function() { autoRefresh(); }, refreshRate * 1000));
					}).fail(function() {
						// Failed to get pageviews
						timeoutIds.push(setTimeout(function() { autoRefresh(); }, refreshRate * 1000));
					});
				});
			}
		});
	}
	function showSidebar() {
		localStorage'mw-recentchanges-sidebar-state' = 'show';
		$("#mw-content-text").css({
			"margin-right": preMarginRight,
			"min-height": preMinHeight
		});
		$('#catlinks').css('margin-right', preMarginRight);
		$("#rcSidebar").show();
		$(window).trigger("resize");
	}
	function hideSidebar() {
		localStorage'mw-recentchanges-sidebar-state' = 'hidden';
		$("#mw-content-text").css({
			"margin-right": 0,
			"min-height": 0
		});
		$('#catlinks').css('margin-right', 0);
		$("#rcSidebar").hide();
	}
	function initSidebar() {
		$(window).trigger("resize");
		if (localStorage'mw-recentchanges-sidebar-state' === 'hidden') {
			hideSidebar();
		} else {
			showSidebar();
		}
	}
	function toggleSidebar() {
		if (localStorage'mw-recentchanges-sidebar-state' !== 'hidden') {
			hideSidebar();
		} else {
			showSidebar();
		}
	}
	function loadCache() {
		if ( localStorage'mw-recentchanges-sidebar-incWD' === undefined ) {
			localStorage'mw-recentchanges-sidebar-incWD' = 'enabled';
		}
		if ( localStorage'mw-recentchanges-sidebar'  

**undefined ) {
			if (localStorage'mw-recentchanges-sidebar-tab1' !== undefined) {
				rcText = localStorage'mw-recentchanges-sidebar-tab1';
				addRcText();
				$("#rcSidebar").append(localStorage'mw-recentchanges-sidebar');
				$("#rcSidebar").append(' ' + getMsg('inc_wd') + );
				if ( localStorage'mw-recentchanges-sidebar-incWD' === 'enabled' ) {
					$("#rcSidebar-incWD").prop('checked', true);
				}
				$("#rcSidebar-incWD").attr("disabled", "disabled").off('click');
			}
		}
		$(".rcSidebarTab").css( isMinerva ? rcSidebarTabMobileStyle : rcSidebarTabStyle );
		$(".mw-parser-output").css("word-wrap", "break-word");
		if ( localStorage'mw-recentchanges-sidebar-pageviews'  

**undefined ) {
			$("#rcSidebar").append(localStorage'mw-recentchanges-sidebar-pageviews');
			$("#pgViewSidebar").css(pgViewSidebarSTyle);
			if ( isMinerva ) {
				$("#pgViewSidebar li").css("margin-left", "20px");
			}
		}
	}
	function main() {
		if ( !init() ) {
			return;
		}
		procVector();
		loadCache();
		autoRefresh();
		if ( ! isVector ) {
			return;
		}
		preMarginRight = $("#mw-content-text").css("margin-right");
		preMinHeight = $("#mw-content-text").css("min-height");
		$(window).resize(function() {
			if (isVector && !isLegacyVector) {
				if (localStorage'mw-recentchanges-sidebar-state' !== 'hidden') {
					procVector();
				}
			}
			if ( $("#rcSidebar").isInViewport() ) {
				preMarginRight = $("#mw-content-text").css("margin-right");
				preMinHeight = $("#mw-content-text").css("min-height");
			}
			if (
				$(".vector-column-end").length > 0 &&
				$(".vector-column-end").has("#vector-page-tools").length > 0 &&
				$(".vector-page-tools-landmark #vector-page-tools-pinned-container").length > 0
			) {
				$('#vector-page-tools').append($("#rcSidebar"));
				$("#rcSidebar").css({
					'position': 'auto',
					'top': 'auto',
					'right': 'auto',
					'width': '190px'
				});
				$("#mw-content-text").css({
					'margin-right': 'auto'
				});
				$('#vector-page-tools').css('width', '230px')
				if ( $(".vector-pinnable-header-unpin-button, .vector-pinnable-header-pin-button").attr('data-event-name') === 'pinnable-header.vector-main-menu.pin' ) {
					$('#rcSidebar').css('position', 'static');
				} else if ( $(".vector-pinnable-header-unpin-button, .vector-pinnable-header-pin-button").attr('data-event-name') === 'pinnable-header.vector-main-menu.unpin' ) {
					$('#rcSidebar').css('position', 'absolute');
				}
			} else {
				if ( isMinerva ) {
					$(".footer-content").append($("#rcSidebar"));
					$("#rcSidebar").css(rcSidebarMobileStyle);
				} else if ( isVector ) {
					$("#mw-content-text").append($("#rcSidebar"));
					$("#rcSidebar").css(rcSidebarStyle);
				}
			}
		});
		$(".vector-pinnable-header-unpin-button, .vector-pinnable-header-pin-button").click(function() {
			if ( $(this).attr('data-event-name') === 'pinnable-header.vector-page-tools.unpin' 
			$(this).attr('data-event-name') === 'pinnable-header.vector-page-tools.pin'
			) {
				setTimeout(function() { $(window).trigger('resize'); }, 150);
			}
		});
		$(document).on('click', '.vector-limited-width-toggle', function () {
			$(window).trigger("resize");
		});
		initSidebar();
		$toggle = $(  )
			.attr( 'id', 'ca-recentchanges' )
			.attr( 'class', 'vector-tab-noicon mw-list-item' );
		$toggle.find( 'a' )
			.attr( 'title', getMsg('sidebar_desc') ); // Toggle recent changes sidebar
		$toggle.find( 'span' )
			.text( getMsg('sidebar_title') ) // Recent changes sidebar
			.click( toggleSidebar );
		if ( $( '#ca-nstab-special' ).length > 0 ) {
			$( '#ca-nstab-special' ).parent().append( $toggle );
		} else {
			$( '#p-views ul' ).append( $toggle );
		}
	}
	main();
}());