Sang Anganggé:Gesram Dwijayana Ida Bagus/common.js

Cutetan:Sesampun kawedar, Ragané patut ngentasin tembolok peramban wéb antuk nyingakin uahan.

  • Firefox/Safari: Taan Shift ritatkala ngaklik Reload, utawi teken Ctrl-F5 utawi Ctrl-R (⌘-R ring Mac)
  • Google Chrome: Taan Ctrl-Shift-R (⌘-Shift-R ring Mac)
  • Internet Explorer: Taan Ctrl ritatkala ngaklik Refresh, utawi teken Ctrl-F5
  • Opera: Teken Ctrl-F5.
  • Opera: Ngranjing ka Ménu → Pengaturan (Opera → Pustaka ring Mac) Salanturnyané ring Privacy & security → Clear browsing data → Cached images and files.
//Dokumentation unter [[Benutzer:Schnark/js/letzteredit]] <nowiki>
/*global mediaWiki*/
(function ($, mw) {
"use strict";
var l10n = {
	en: {
		'january': 'January',
		'february': 'February',
		'march': 'March',
		'april': 'April',
		'may_long': 'May',
		'june': 'June',
		'july': 'July',
		'august': 'August',
		'september': 'September',
		'october': 'October',
		'november': 'November',
		'december': 'December',
		'schnark-letzteredit-last-edit': '(last action $1)',
		'schnark-letzteredit-just-now': 'just now',
		'schnark-letzteredit-minutes-ago': '$1 {{PLURAL:$1|minute|minutes}} ago',
		'schnark-letzteredit-hours-minutes-ago': '$1 {{PLURAL:$1|hour|hours}} $2 {{PLURAL:$2|minute|minutes}} ago',
		'schnark-letzteredit-today-at': 'today at $1:$2',
		'schnark-letzteredit-yesterday-at': 'yesterday at $1:$2',
		'schnark-letzteredit-days-ago': '$1 days ago',
		'schnark-letzteredit-on-day': 'on $2 $1, $3',
		'schnark-letzteredit-in-month': 'in $1 $2',
		'schnark-letzteredit-never': '(no action)',
		'schnark-letzteredit-error': '(error while trying to get the time of the last action)'
	},
	de: {
		'january': 'Januar',
		'february': 'Februar',
		'march': 'März',
		'april': 'April',
		'may_long': 'Mai',
		'june': 'Juni',
		'july': 'Juli',
		'august': 'August',
		'september': 'September',
		'october': 'Oktober',
		'november': 'November',
		'december': 'Dezember',
		'schnark-letzteredit-last-edit': '(zuletzt aktiv $1)',
		'schnark-letzteredit-just-now': 'gerade eben',
		'schnark-letzteredit-minutes-ago': 'vor {{PLURAL:$1|einer Minute|$1 Minuten}}',
		'schnark-letzteredit-hours-minutes-ago': 'vor $1 {{PLURAL:$1|Stunde|Stunden}} $2 {{PLURAL:$2|Minute|Minuten}}',
		'schnark-letzteredit-today-at': 'heute um $1:$2',
		'schnark-letzteredit-yesterday-at': 'gestern um $1:$2',
		'schnark-letzteredit-days-ago': 'vor $1 Tagen',
		'schnark-letzteredit-on-day': 'am $1. $2 $3',
		'schnark-letzteredit-in-month': 'im $1 $2',
		'schnark-letzteredit-never': '(noch nie aktiv)',
		'schnark-letzteredit-error': '(Fehler bei der Ermittlung der letzten Aktion)'
	},
	'de-at': {
		'january': 'Jänner'
	}
}, config = {
	fewHours: 6,
	fewDays: 10,
	manyDays: 100
};

function initL10N (l10n, keep) {
	var i, chain = mw.language.getFallbackLanguageChain();
	keep = $.grep(mw.messages.get(keep), function (val) {
		return val !== null;
	});
	for (i = chain.length - 1; i >= 0; i--) {
		if (chain[i] in l10n) {
			mw.messages.set(l10n[chain[i]]);
		}
	}
	mw.messages.set(keep);
}

function getDateFromTimestamp (ts) {
	if (!ts) {
		return false;
	}
	var d = new Date(ts);
	return isNaN(d.valueOf()) ? false : d;
}

function getMaxDate (d1, d2) {
	if (!d1) {
		return d2;
	}
	if (!d2) {
		return d1;
	}
	return d1.getTime() < d2.getTime() ? d2 : d1;
}

function getDayDiff (then, now) {
	var thenDay, nowDay;
	thenDay = new Date(then.getFullYear(), then.getMonth(), then.getDate());
	nowDay = new Date(now.getFullYear(), now.getMonth(), now.getDate());
	return Math.round((nowDay.getTime() - thenDay.getTime()) / (24 * 60 * 60 * 1000));
}

function pad (n) {
	return n < 10 ? '0' + String(n) : n;
}

function getMonth (date) {
	return mw.msg(['january', 'february', 'march', 'april', 'may_long', 'june',
		'july', 'august', 'september', 'october', 'november', 'december'][date.getMonth()]);
}

function getHoursMinutes (min) {
	if (min <= 0) {
		return mw.msg('schnark-letzteredit-just-now');
	}
	if (min < 60) {
		return mw.msg('schnark-letzteredit-minutes-ago', min);
	}
	return mw.msg('schnark-letzteredit-hours-minutes-ago', Math.floor(min / 60), min % 60);
}

function getTodayYesterday (today, date) {
	return mw.msg(today ? 'schnark-letzteredit-today-at' : 'schnark-letzteredit-yesterday-at',
		date.getHours(), pad(date.getMinutes()));
}

function getReadableTime (then, now) {
	var min, days;
	min = Math.round((now.getTime() - then.getTime()) / 60000);
	days = getDayDiff(then, now);
	if (min <= config.fewHours * 60) {
		return getHoursMinutes(min);
	}
	if (days <= 1) {
		return getTodayYesterday(days === 0, then);
	}
	if (days <= config.fewDays) {
		return mw.msg('schnark-letzteredit-days-ago', days);
	}
	if (days <= config.manyDays) {
		return mw.msg('schnark-letzteredit-on-day', then.getDate(), getMonth(then), then.getFullYear());
	}
	return mw.msg('schnark-letzteredit-in-month', getMonth(then), then.getFullYear());
}

function getDisplayFromDates (then, now) {
	if (!now) {
		return mw.msg('schnark-letzteredit-error');
	}
	if (!then) {
		return mw.msg('schnark-letzteredit-never');
	}
	return mw.msg('schnark-letzteredit-last-edit', getReadableTime(then, now));
}

function getDisplay (data) {
	var now = '', edit = '', log = '';
	if (data) {
		now = data.curtimestamp || '';
	}
	if (data && data.query && data.query.usercontribs && data.query.usercontribs[0]) {
		edit = data.query.usercontribs[0].timestamp || '';
	}
	if (data && data.query && data.query.logevents && data.query.logevents[0]) {
		log = data.query.logevents[0].timestamp || '';
	}
	now = getDateFromTimestamp(now);
	edit = getDateFromTimestamp(edit);
	log = getDateFromTimestamp(log);
	return getDisplayFromDates(getMaxDate(edit, log), now);
}

function show (str) {
	$(function () { //TODO mw.hook('wikipage.title').add(...)
		var $el = $('#firstHeading small.letzteredit');
		if (!$el.length) {
			$el = $('<small>')
				.addClass('letzteredit')
				.css({marginLeft: '1em', lineHeight: '1em'});
			$('#firstHeading').append($el);
		}
		$el.text(' ' + str);
	});
}

function processApi (data) {
	mw.loader.using('mediawiki.jqueryMsg').then(function () {
		show(getDisplay(data));
	});
}

function runQuery () {
	var user = mw.config.get('wgTitle').replace(/\/.*/, ''),
		data = {
			action: 'query', list: 'usercontribs|logevents',
			uclimit: 1, lelimit: 1,
			ucprop: 'timestamp', leprop: 'timestamp', curtimestamp: true,
			ucuser: user, leuser: user,
			format: 'json', formatversion: 2
		};
	$.getJSON(mw.util.wikiScript('api'), data).then(processApi);
}

function init () {
	mw.loader.load('mediawiki.jqueryMsg');
	initL10N(l10n, ['january', 'february', 'march', 'april', 'may_long',
		'june', 'july', 'august', 'september', 'october', 'november', 'december']);
	runQuery();
}

mw.hook('userjs.load-script.letzteredit').fire(config);
if (mw.config.get('wgNamespaceNumber') === 2 || mw.config.get('wgNamespaceNumber') === 3) {
	mw.loader.using(['mediawiki.util', 'mediawiki.language']).then(init);
}

})(jQuery, mediaWiki);
//</nowiki>