1
0
mirror of https://github.com/vichan-devel/vichan.git synced 2024-12-04 20:08:02 +01:00
vichan/templates/main.js

446 lines
14 KiB
JavaScript
Raw Normal View History

{% verbatim %}
2013-07-31 10:28:26 +02:00
/* gettext-compatible _ function, example of usage:
*
* > // Loading pl_PL.json here (containing polish translation strings generated by tools/i18n_compile.php)
* > alert(_("Hello!"));
* Witaj!
*/
function _(s) {
return (typeof l10n != 'undefined' && typeof l10n[s] != 'undefined') ? l10n[s] : s;
}
/* printf-like formatting function, example of usage:
*
* > alert(fmt("There are {0} birds on {1} trees", [3,4]));
* There are 3 birds on 4 trees
* > // Loading pl_PL.json here (containing polish translation strings generated by tools/locale_compile.php)
* > alert(fmt(_("{0} users"), [3]));
* 3 uzytkownikow
*/
2024-08-04 15:32:12 +02:00
function fmt(s, a) {
2013-07-31 10:28:26 +02:00
return s.replace(/\{([0-9]+)\}/g, function(x) { return a[x[1]]; });
}
2024-08-04 15:32:12 +02:00
function until(timestamp) {
let difference = timestamp - Date.now() / 1000 | 0;
switch (true) {
case (difference < 60):
return "" + difference + ' ' + _('second(s)');
case (difference < 3600): // 60 * 60 = 3600
return "" + Math.round(difference / 60) + ' ' + _('minute(s)');
case (difference < 86400): // 60 * 60 * 24 = 86400
return "" + Math.round(difference / 3600) + ' ' + _('hour(s)');
case (difference < 604800): // 60 * 60 * 24 * 7 = 604800
return "" + Math.round(difference / 86400) + ' ' + _('day(s)');
case (difference < 31536000): // 60 * 60 * 24 * 365 = 31536000
return "" + Math.round(difference / 604800) + ' ' + _('week(s)');
default:
return "" + Math.round(difference / 31536000) + ' ' + _('year(s)');
}
}
2024-08-04 15:32:12 +02:00
function ago(timestamp) {
let difference = (Date.now() / 1000 | 0) - timestamp;
switch (true) {
case (difference < 60):
return "" + difference + ' ' + _('second(s)');
case (difference < 3600): /// 60 * 60 = 3600
return "" + Math.round(difference/(60)) + ' ' + _('minute(s)');
case (difference < 86400): // 60 * 60 * 24 = 86400
return "" + Math.round(difference/(3600)) + ' ' + _('hour(s)');
case (difference < 604800): // 60 * 60 * 24 * 7 = 604800
return "" + Math.round(difference/(86400)) + ' ' + _('day(s)');
case (difference < 31536000): // 60 * 60 * 24 * 365 = 31536000
return "" + Math.round(difference/(604800)) + ' ' + _('week(s)');
default:
return "" + Math.round(difference/(31536000)) + ' ' + _('year(s)');
}
}
var datelocale =
2024-08-04 15:32:12 +02:00
{ days: [_('Sunday'), _('Monday'), _('Tuesday'), _('Wednesday'), _('Thursday'), _('Friday'), _('Saturday')]
, shortDays: [_("Sun"), _("Mon"), _("Tue"), _("Wed"), _("Thu"), _("Fri"), _("Sat")]
, months: [_('January'), _('February'), _('March'), _('April'), _('May'), _('June'), _('July'), _('August'), _('September'), _('October'), _('November'), _('December')]
, shortMonths: [_('Jan'), _('Feb'), _('Mar'), _('Apr'), _('May'), _('Jun'), _('Jul'), _('Aug'), _('Sep'), _('Oct'), _('Nov'), _('Dec')]
, AM: _('AM')
, PM: _('PM')
, am: _('am')
, pm: _('pm')
};
function alert(a, do_confirm, confirm_ok_action, confirm_cancel_action) {
2024-09-19 22:51:01 +02:00
let handler, div, bg, closebtn, okbtn;
2024-08-04 15:32:12 +02:00
let close = function() {
handler.fadeOut(400, function() { handler.remove(); });
return false;
};
2024-08-04 15:32:12 +02:00
handler = $("<div id='alert_handler'></div>").hide().appendTo('body');
2024-08-04 15:32:12 +02:00
bg = $("<div id='alert_background'></div>").appendTo(handler);
2024-08-04 15:32:12 +02:00
div = $("<div id='alert_div'></div>").appendTo(handler);
closebtn = $("<a id='alert_close' href='javascript:void(0)'><i class='fa fa-times'></i></div>")
.appendTo(div);
2024-08-04 15:32:12 +02:00
$("<div id='alert_message'></div>").html(a).appendTo(div);
2024-08-04 15:32:12 +02:00
okbtn = $("<button class='button alert_button'>"+_("OK")+"</button>").appendTo(div);
2024-08-04 15:32:12 +02:00
if (do_confirm) {
confirm_ok_action = (typeof confirm_ok_action !== "function") ? function(){} : confirm_ok_action;
confirm_cancel_action = (typeof confirm_cancel_action !== "function") ? function(){} : confirm_cancel_action;
okbtn.click(confirm_ok_action);
$("<button class='button alert_button'>"+_("Cancel")+"</button>").click(confirm_cancel_action).click(close).appendTo(div);
bg.click(confirm_cancel_action);
okbtn.click(confirm_cancel_action);
closebtn.click(confirm_cancel_action);
}
2024-08-04 15:32:12 +02:00
bg.click(close);
okbtn.click(close);
closebtn.click(close);
2024-08-04 15:32:12 +02:00
handler.fadeIn(400);
}
2013-07-31 10:28:26 +02:00
var saved = {};
var selectedstyle = '{% endverbatim %}{{ config.default_stylesheet.0|addslashes }}{% verbatim %}';
2013-07-31 10:28:26 +02:00
var styles = {
{% endverbatim %}
{% for stylesheet in stylesheets %}{% verbatim %}'{% endverbatim %}{{ stylesheet.name|addslashes }}{% verbatim %}' : '{% endverbatim %}{{ stylesheet.uri|addslashes }}{% verbatim %}',
{% endverbatim %}{% endfor %}{% verbatim %}
2013-07-31 10:28:26 +02:00
};
2016-05-05 14:38:24 +02:00
if (typeof board_name === 'undefined') {
var board_name = false;
}
2013-07-31 10:28:26 +02:00
function changeStyle(styleName, link) {
{% endverbatim %}
{% if config.stylesheets_board %}{% verbatim %}
if (board_name) {
stylesheet_choices[board_name] = styleName;
localStorage.board_stylesheets = JSON.stringify(stylesheet_choices);
}
{% endverbatim %}{% else %}
localStorage.stylesheet = styleName;
{% endif %}
{% verbatim %}
2024-04-15 23:33:12 +02:00
2013-07-31 10:28:26 +02:00
if (!document.getElementById('stylesheet')) {
2024-09-19 22:51:01 +02:00
let s = document.createElement('link');
2013-07-31 10:28:26 +02:00
s.rel = 'stylesheet';
s.type = 'text/css';
s.id = 'stylesheet';
2024-09-19 22:51:01 +02:00
let x = document.getElementsByTagName('head')[0];
2013-07-31 10:28:26 +02:00
x.appendChild(s);
}
2024-04-15 23:33:12 +02:00
2013-07-31 10:28:26 +02:00
document.getElementById('stylesheet').href = styles[styleName];
selectedstyle = styleName;
2024-04-15 23:33:12 +02:00
2013-07-31 10:28:26 +02:00
if (document.getElementsByClassName('styles').length != 0) {
2024-09-19 22:51:01 +02:00
let styleLinks = document.getElementsByClassName('styles')[0].childNodes;
for (let i = 0; i < styleLinks.length; i++) {
2013-07-31 10:28:26 +02:00
styleLinks[i].className = '';
}
}
2024-04-15 23:33:12 +02:00
2013-07-31 10:28:26 +02:00
if (link) {
link.className = 'selected';
}
2024-04-15 23:33:12 +02:00
2024-08-04 15:32:12 +02:00
if (typeof $ != 'undefined') {
2013-09-15 02:29:35 +02:00
$(window).trigger('stylesheet', styleName);
2024-08-04 15:32:12 +02:00
}
2013-07-31 10:28:26 +02:00
}
{% endverbatim %}
{% if config.stylesheets_board %}
{% verbatim %}
2024-04-15 23:33:12 +02:00
if (!localStorage.board_stylesheets) {
localStorage.board_stylesheets = '{}';
}
2024-04-15 23:33:12 +02:00
var stylesheet_choices = JSON.parse(localStorage.board_stylesheets);
if (board_name && stylesheet_choices[board_name]) {
2024-09-19 22:51:01 +02:00
for (let styleName in styles) {
if (styleName == stylesheet_choices[board_name]) {
changeStyle(styleName);
break;
}
}
}
{% endverbatim%}
{% else %}
{% verbatim %}
2013-07-31 10:28:26 +02:00
if (localStorage.stylesheet) {
2024-09-19 22:51:01 +02:00
for (let styleName in styles) {
2013-07-31 10:28:26 +02:00
if (styleName == localStorage.stylesheet) {
changeStyle(styleName);
break;
}
}
}
{% endverbatim %}
{% endif %}
{% verbatim %}
2013-07-31 10:28:26 +02:00
function initStyleChooser() {
2024-09-19 22:51:01 +02:00
let newElement = document.createElement('div');
2013-07-31 10:28:26 +02:00
newElement.className = 'styles';
2024-04-15 23:33:12 +02:00
2013-07-31 10:28:26 +02:00
for (styleName in styles) {
2024-09-19 22:51:01 +02:00
let style = document.createElement('a');
2013-07-31 10:28:26 +02:00
style.innerHTML = '[' + styleName + ']';
style.onclick = function() {
changeStyle(this.innerHTML.substring(1, this.innerHTML.length - 1), this);
};
if (styleName == selectedstyle) {
style.className = 'selected';
}
style.href = 'javascript:void(0);';
newElement.appendChild(style);
2024-04-15 23:33:12 +02:00
}
2013-07-31 10:28:26 +02:00
document.getElementsByTagName('body')[0].insertBefore(newElement, document.getElementsByTagName('body')[0].lastChild.nextSibling);
}
2024-08-04 15:07:56 +02:00
function getCookie(cookie_name) {
let results = document.cookie.match('(^|;) ?' + cookie_name + '=([^;]*)(;|$)');
if (results) {
return unescape(results[2]);
} else {
2013-07-31 10:28:26 +02:00
return null;
2024-08-04 15:07:56 +02:00
}
2013-07-31 10:28:26 +02:00
}
2024-08-25 22:01:06 +02:00
{% endverbatim %}
{% if config.captcha.dynamic %}
2024-05-25 00:32:44 +02:00
function is_dynamic_captcha_enabled() {
let cookie = get_cookie('require-captcha');
return cookie === '1';
}
function get_captcha_pub_key() {
2024-08-25 22:01:06 +02:00
{% if config.captcha.provider == 'recaptcha' %}
return "{{ config.captcha.recaptcha.sitekey }}";
2024-05-25 00:32:44 +02:00
{% else %}
return null;
{% endif %}
}
function init_dynamic_captcha() {
if (!is_dynamic_captcha_enabled()) {
let pub_key = get_captcha_pub_key();
if (!pub_key) {
console.error("Missing public captcha key!");
return;
}
let captcha_hook = document.getElementById('captcha');
captcha_hook.style = "";
}
}
{% endif %}
2024-08-25 22:01:06 +02:00
{% verbatim %}
2024-05-25 00:32:44 +02:00
2013-07-31 10:28:26 +02:00
function highlightReply(id) {
if (typeof window.event != "undefined" && event.which == 2) {
// don't highlight on middle click
return true;
}
2024-04-15 23:33:12 +02:00
2024-08-04 15:32:12 +02:00
let divs = document.getElementsByTagName('div');
2024-09-19 22:51:01 +02:00
for (let i = 0; i < divs.length; i++) {
2024-08-04 15:32:12 +02:00
if (divs[i].className.indexOf('post') != -1) {
2013-07-31 10:28:26 +02:00
divs[i].className = divs[i].className.replace(/highlighted/, '');
2024-08-04 15:32:12 +02:00
}
2013-07-31 10:28:26 +02:00
}
if (id) {
2024-08-04 15:32:12 +02:00
let post = document.getElementById('reply_' + id);
if (post) {
2013-07-31 10:28:26 +02:00
post.className += ' highlighted';
2024-08-04 15:32:12 +02:00
}
window.location.hash = id;
2013-07-31 10:28:26 +02:00
}
return true;
2013-07-31 10:28:26 +02:00
}
function generatePassword() {
2024-08-04 15:32:12 +02:00
let pass = '';
let chars = '{% endverbatim %}{{ config.genpassword_chars }}{% verbatim %}';
for (let i = 0; i < 8; i++) {
let rnd = Math.floor(Math.random() * chars.length);
2013-07-31 10:28:26 +02:00
pass += chars.substring(rnd, rnd + 1);
}
return pass;
}
2024-08-04 15:13:05 +02:00
function doPost(form) {
2013-07-31 10:28:26 +02:00
if (form.elements['name']) {
localStorage.name = form.elements['name'].value.replace(/( |^)## .+$/, '');
}
if (form.elements['password']) {
localStorage.password = form.elements['password'].value;
}
2013-07-31 10:28:26 +02:00
if (form.elements['email'] && form.elements['email'].value != 'sage') {
localStorage.email = form.elements['email'].value;
}
2024-04-15 23:33:12 +02:00
2013-07-31 10:28:26 +02:00
saved[document.location] = form.elements['body'].value;
sessionStorage.body = JSON.stringify(saved);
2024-04-15 23:33:12 +02:00
2015-04-01 19:37:06 +02:00
return form.elements['body'].value != "" || (form.elements['file'] && form.elements['file'].value != "") || (form.elements.file_url && form.elements['file_url'].value != "");
2013-07-31 10:28:26 +02:00
}
function citeReply(id, with_link) {
2024-08-04 15:32:12 +02:00
let textarea = document.getElementById('body');
if (!textarea) {
return false;
}
2024-04-15 23:33:12 +02:00
2013-07-31 10:28:26 +02:00
if (document.selection) {
// IE
2013-09-15 19:51:36 +02:00
textarea.focus();
2024-08-04 15:32:12 +02:00
let sel = document.selection.createRange();
2013-07-31 10:28:26 +02:00
sel.text = '>>' + id + '\n';
2013-09-15 19:51:36 +02:00
} else if (textarea.selectionStart || textarea.selectionStart == '0') {
2024-08-04 15:32:12 +02:00
let start = textarea.selectionStart;
let end = textarea.selectionEnd;
2013-09-15 19:51:36 +02:00
textarea.value = textarea.value.substring(0, start) + '>>' + id + '\n' + textarea.value.substring(end, textarea.value.length);
2024-04-15 23:33:12 +02:00
2013-09-15 19:51:36 +02:00
textarea.selectionStart += ('>>' + id).length + 1;
textarea.selectionEnd = textarea.selectionStart;
2013-07-31 10:28:26 +02:00
} else {
// ???
2013-09-15 19:51:36 +02:00
textarea.value += '>>' + id + '\n';
2013-07-31 10:28:26 +02:00
}
2013-09-15 02:29:35 +02:00
if (typeof $ != 'undefined') {
2024-08-04 15:32:12 +02:00
let select = document.getSelection().toString();
2014-09-30 07:45:02 +02:00
if (select) {
2024-08-04 15:32:12 +02:00
let body = $('#reply_' + id + ', #op_' + id).find('div.body'); // TODO: support for OPs
let index = body.text().indexOf(select.replace('\n', '')); // for some reason this only works like this
2014-09-30 07:45:02 +02:00
if (index > -1) {
textarea.value += '>' + select + '\n';
}
}
$(window).trigger('cite', [id, with_link]);
2013-09-15 19:51:36 +02:00
$(textarea).change();
2013-07-31 10:28:26 +02:00
}
return false;
2013-07-31 10:28:26 +02:00
}
function rememberStuff() {
if (document.forms.post) {
if (document.forms.post.password) {
2024-08-04 15:32:12 +02:00
if (!localStorage.password) {
2013-07-31 10:28:26 +02:00
localStorage.password = generatePassword();
2024-08-04 15:32:12 +02:00
}
2013-07-31 10:28:26 +02:00
document.forms.post.password.value = localStorage.password;
}
2024-04-15 23:33:12 +02:00
2024-08-04 15:32:12 +02:00
if (localStorage.name && document.forms.post.elements['name']) {
2013-07-31 10:28:26 +02:00
document.forms.post.elements['name'].value = localStorage.name;
2024-08-04 15:32:12 +02:00
}
if (localStorage.email && document.forms.post.elements['email']) {
2013-07-31 10:28:26 +02:00
document.forms.post.elements['email'].value = localStorage.email;
2024-08-04 15:32:12 +02:00
}
2024-04-15 23:33:12 +02:00
2024-08-04 15:32:12 +02:00
if (window.location.hash.indexOf('q') == 1) {
citeReply(window.location.hash.substring(2), true);
2024-08-04 15:32:12 +02:00
}
2024-04-15 23:33:12 +02:00
2013-07-31 10:28:26 +02:00
if (sessionStorage.body) {
2024-08-04 15:32:12 +02:00
let saved = JSON.parse(sessionStorage.body);
2024-08-04 15:07:56 +02:00
if (getCookie('{% endverbatim %}{{ config.cookies.js }}{% verbatim %}')) {
2013-07-31 10:28:26 +02:00
// Remove successful posts
2024-08-04 15:32:12 +02:00
let successful = JSON.parse(getCookie('{% endverbatim %}{{ config.cookies.js }}{% verbatim %}'));
for (let url in successful) {
2013-07-31 10:28:26 +02:00
saved[url] = null;
}
sessionStorage.body = JSON.stringify(saved);
2024-04-15 23:33:12 +02:00
document.cookie = '{% endverbatim %}{{ config.cookies.js }}{% verbatim %}={};expires=0;path=/;';
2013-07-31 10:28:26 +02:00
}
if (saved[document.location]) {
document.forms.post.body.value = saved[document.location];
}
}
2024-04-15 23:33:12 +02:00
2013-07-31 10:28:26 +02:00
if (localStorage.body) {
document.forms.post.body.value = localStorage.body;
localStorage.body = '';
}
}
}
var script_settings = function(script_name) {
this.script_name = script_name;
this.get = function(var_name, default_val) {
if (typeof tb_settings == 'undefined' ||
typeof tb_settings[this.script_name] == 'undefined' ||
2024-08-04 15:32:12 +02:00
typeof tb_settings[this.script_name][var_name] == 'undefined') {
return default_val;
2024-08-04 15:32:12 +02:00
}
return tb_settings[this.script_name][var_name];
}
};
2013-07-31 10:28:26 +02:00
function init() {
initStyleChooser();
2024-04-15 23:33:12 +02:00
{% endverbatim %}
{% if config.allow_delete %}
2013-07-31 10:28:26 +02:00
if (document.forms.postcontrols) {
document.forms.postcontrols.password.value = localStorage.password;
}
{% endif %}
{% verbatim %}
2024-04-15 23:33:12 +02:00
2013-07-31 10:28:26 +02:00
if (window.location.hash.indexOf('q') != 1 && window.location.hash.substring(1))
highlightReply(window.location.hash.substring(1));
}
var RecaptchaOptions = {
theme : 'clean'
};
onready_callbacks = [];
2024-08-05 18:32:11 +02:00
function onReady(fnc) {
2013-07-31 10:28:26 +02:00
onready_callbacks.push(fnc);
}
function ready() {
2024-08-04 15:32:12 +02:00
for (let i = 0; i < onready_callbacks.length; i++) {
2013-07-31 10:28:26 +02:00
onready_callbacks[i]();
}
}
{% endverbatim %}
2013-08-17 20:00:26 +02:00
var post_date = "{{ config.post_date }}";
2014-04-27 15:48:47 +02:00
var max_images = {{ config.max_images }};
2024-08-05 18:32:11 +02:00
onReady(init);
{% if config.google_analytics %}{% verbatim %}
2013-07-31 10:28:26 +02:00
2024-09-19 22:51:01 +02:00
var _gaq = _gaq || [];_gaq.push(['_setAccount', '{% endverbatim %}{{ config.google_analytics }}{% verbatim %}']);{% endverbatim %}{% if config.google_analytics_domain %}{% verbatim %}_gaq.push(['_setDomainName', '{% endverbatim %}{{ config.google_analytics_domain }}{% verbatim %}']){% endverbatim %}{% endif %}{% if not config.google_analytics_domain %}{% verbatim %}_gaq.push(['_setDomainName', 'none']){% endverbatim %}{% endif %}{% verbatim %};_gaq.push(['_trackPageview']);(function() {var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;ga.src = ('https:' == document.location.protocol ? 'https://' : 'http://') + 'stats.g.doubleclick.net/dc.js';let s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);})();{% endverbatim %}{% endif %}
2013-07-31 10:28:26 +02:00
{% if config.statcounter_project and config.statcounter_security %}
var sc = document.createElement('script');
sc.type = 'text/javascript';
sc.innerHTML = 'var sc_project={{ config.statcounter_project }};var sc_invisible=1;var sc_security="{{ config.statcounter_security }}";var scJsHost=(("https:" == document.location.protocol) ? "https://secure." : "http://www.");document.write("<sc"+"ript type=text/javascript src="+scJsHost+"statcounter.com/counter/counter.js></"+"script>");';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(sc, s);
{% endif %}