MediaWiki:FallbackTimer.js: различия между версиями
Перейти к навигации
(Новая страница: «qwe») |
Нет описания правки |
||
(не показано 9 промежуточных версий этого же участника) | |||
Строка 1: | Строка 1: | ||
function plural_ru(ms, n) { | |||
var m; | |||
if (n % 10 == 1 && n % 100 !== 11) m = ms[0]; | |||
else if ((n % 10 >= 2 && n % 10 <= 4 && (n % 10) % 1 == 0) && (n % 100 < 12 || n % 100 > 14)) m = ms[1]; | |||
else if (n % 10 == 0 || (n % 10 >= 5 && n % 10 <= 9 && (n % 10) % 1 == 0) || (n % 100 >= 11 && n % 100 <= 14 && (n % 100) % 1 == 0)) m = ms[2]; | |||
else m = ms[3] || ''; | |||
return m ? m.replace('{n}', n) : null; | |||
} | |||
!(function (fbTimer, jQuery) { | |||
"use strict"; | |||
var regex = /^(\d{4})-(0\d|1[012])-([012]\d|3[01])(?: ([01]\d|2[0-3]):([0-5]\d):([0-5]\d))?$/; | |||
var time_labels = { | |||
month: ['{n} месяц', '{n} месяца', '{n} месяцев'], | |||
week: ['{n} неделя', '{n} недели', '{n} недель'], | |||
day: ['{n} день', '{n} дня', '{n} дней'], | |||
hour: ['{n} час', '{n} часа', '{n} часов'], | |||
minute: ['{n} минута', '{n} минуты', '{n} минут'] | |||
}; | |||
var delay = { | |||
month: 0, | |||
week: 604800, | |||
day: 86400, | |||
hour: 3600, | |||
minutes: 60 | |||
}; | |||
jQuery(function () { | |||
console.log('timer.js', 'initialization...'); | |||
jQuery('.fallback-timer[data-end]').each(function () { | |||
var timer = jQuery(this); | |||
var end = timer.data('end'); | |||
var prefix = timer.data('prefix') || 'Осталось'; | |||
var postfix = timer.data('postfix'); | |||
var finish_label = timer.data('complete') || 'Отчет завершен'; | |||
if (end == void 0) return console.warn('[Timer.js]', 'For correct work required "data-end" field with end date in format "2000-01-01"'); | |||
var _pd = regex.exec(end); | |||
if (_pd == void 0) return console.warn('[Timer.js]', 'Fallback timer data-end field contains unexpected value'); | |||
var date = new Date(_pd[1], _pd[2] - 1, _pd[3], (_pd[4] === void 0) ? 0 : _pd[4], (_pd[5] === void 0) ? 0 : _pd[5], (_pd[6] === void 0) ? 0 : _pd[6]); | |||
var diff = Math.floor((date.getTime() - (new Date()).getTime()) / 1000); | |||
if (diff <= 0) return timer.text(finish_label); | |||
var marge = { | |||
month: 0, | |||
week: 0, | |||
day: 0, | |||
hour: 0, | |||
minute: 0 | |||
}; | |||
marge.week = Math.floor(diff / delay.week); | |||
diff = diff % delay.week; | |||
marge.day = Math.floor(diff / delay.day); | |||
diff = diff % delay.day; | |||
marge.hour = Math.floor(diff / delay.hour); | |||
diff = diff % delay.hour; | |||
var messages = []; | |||
(marge.week != 0) && (messages.push(plural_ru(time_labels.week, marge.week))); | |||
(marge.day != 0) && (messages.push(plural_ru(time_labels.day, marge.day))); | |||
(marge.hour != 0) && (messages.push(plural_ru(time_labels.hour, marge.hour))); | |||
var result = []; | |||
for (var p in messages) { | |||
if (messages.hasOwnProperty(p) && messages[p]) result.push(messages[p]); | |||
} | |||
timer.text(((prefix) ? prefix + ' ' : '') + messages.join(', ') + ((postfix) ? postfix : '')); | |||
}); | |||
}); | |||
})((window.WGFallbackTimer) || (window.WGFallbackTimer = {}), jQuery); |
Текущая версия от 14:57, 15 февраля 2015
function plural_ru(ms, n) { var m; if (n % 10 == 1 && n % 100 !== 11) m = ms[0]; else if ((n % 10 >= 2 && n % 10 <= 4 && (n % 10) % 1 == 0) && (n % 100 < 12 || n % 100 > 14)) m = ms[1]; else if (n % 10 == 0 || (n % 10 >= 5 && n % 10 <= 9 && (n % 10) % 1 == 0) || (n % 100 >= 11 && n % 100 <= 14 && (n % 100) % 1 == 0)) m = ms[2]; else m = ms[3] || ''; return m ? m.replace('{n}', n) : null; } !(function (fbTimer, jQuery) { "use strict"; var regex = /^(\d{4})-(0\d|1[012])-([012]\d|3[01])(?: ([01]\d|2[0-3]):([0-5]\d):([0-5]\d))?$/; var time_labels = { month: ['{n} месяц', '{n} месяца', '{n} месяцев'], week: ['{n} неделя', '{n} недели', '{n} недель'], day: ['{n} день', '{n} дня', '{n} дней'], hour: ['{n} час', '{n} часа', '{n} часов'], minute: ['{n} минута', '{n} минуты', '{n} минут'] }; var delay = { month: 0, week: 604800, day: 86400, hour: 3600, minutes: 60 }; jQuery(function () { console.log('timer.js', 'initialization...'); jQuery('.fallback-timer[data-end]').each(function () { var timer = jQuery(this); var end = timer.data('end'); var prefix = timer.data('prefix') || 'Осталось'; var postfix = timer.data('postfix'); var finish_label = timer.data('complete') || 'Отчет завершен'; if (end == void 0) return console.warn('[Timer.js]', 'For correct work required "data-end" field with end date in format "2000-01-01"'); var _pd = regex.exec(end); if (_pd == void 0) return console.warn('[Timer.js]', 'Fallback timer data-end field contains unexpected value'); var date = new Date(_pd[1], _pd[2] - 1, _pd[3], (_pd[4] === void 0) ? 0 : _pd[4], (_pd[5] === void 0) ? 0 : _pd[5], (_pd[6] === void 0) ? 0 : _pd[6]); var diff = Math.floor((date.getTime() - (new Date()).getTime()) / 1000); if (diff <= 0) return timer.text(finish_label); var marge = { month: 0, week: 0, day: 0, hour: 0, minute: 0 }; marge.week = Math.floor(diff / delay.week); diff = diff % delay.week; marge.day = Math.floor(diff / delay.day); diff = diff % delay.day; marge.hour = Math.floor(diff / delay.hour); diff = diff % delay.hour; var messages = []; (marge.week != 0) && (messages.push(plural_ru(time_labels.week, marge.week))); (marge.day != 0) && (messages.push(plural_ru(time_labels.day, marge.day))); (marge.hour != 0) && (messages.push(plural_ru(time_labels.hour, marge.hour))); var result = []; for (var p in messages) { if (messages.hasOwnProperty(p) && messages[p]) result.push(messages[p]); } timer.text(((prefix) ? prefix + ' ' : '') + messages.join(', ') + ((postfix) ? postfix : '')); }); }); })((window.WGFallbackTimer) || (window.WGFallbackTimer = {}), jQuery);