liberapay.com/js/10-base.js

153 lines
5.1 KiB
JavaScript
Raw Normal View History

Liberapay.init = function() {
2015-06-04 10:49:46 +02:00
Liberapay.forms.jsSubmit();
var success_re = /([?&])success=[^&]*/;
if (success_re.test(location.search)) {
history.replaceState(null, null,
location.pathname+
location.search.replace(success_re, '$1').replace(/[\?&]$/, '')+
location.hash
);
}
$('.notification .close').click(function(){ $(this).parent().fadeOut() });
2018-01-09 18:47:12 +01:00
Liberapay.auto_tail_log();
2018-01-09 18:31:36 +01:00
Liberapay.charts.init();
Liberapay.lookup.init();
Liberapay.s3_uploader_init();
2018-07-23 11:15:16 +02:00
Liberapay.stripe_init();
2016-02-28 23:21:47 +01:00
$('div[href]').css('cursor', 'pointer').on('click auxclick', function(event) {
if (event.target.tagName == 'A') {
// Ignore clicks on links
return;
}
if (event.button == 2) {
// Ignore right clicks
return;
}
event.preventDefault();
event.stopPropagation();
var url = this.getAttribute('href');
if (event.type == 'click' && event.ctrlKey ||
event.type == 'auxclick' && event.button == 1) {
window.open(url);
} else {
location.href = url;
}
2016-02-28 23:21:47 +01:00
});
$('.dropdown.dropdown-hover').removeClass('dropdown-hover');
2016-03-01 16:32:23 +01:00
$('.dropdown-toggle-form').click(function() {
var $this = $(this);
setTimeout(function() {
$this.siblings('.dropdown-menu').find('input').eq(0).focus();
}, 10);
});
var grid_float_breakpoint = 768;
$('.navbar-nav > li > .dropdown-toggle').click(function(e) {
if ($('html').width() < grid_float_breakpoint) {
$('.navbar-collapse').collapse('hide');
}
});
2020-06-30 10:21:33 +02:00
$('input[data-required-if-checked]').each(function() {
var $this = $(this);
var $requirer = $($this.attr('data-required-if-checked'));
$this.parents('form').find('input').on('change', function() {
$this.prop('required', $requirer.prop('checked'));
});
$requirer.trigger('change');
});
2016-03-14 10:53:02 +01:00
$('[data-toggle="tooltip"]').tooltip();
2017-04-11 09:38:03 +02:00
$('.radio input:not([type="radio"]), .radio-group input:not([type="radio"])').on('click change', function(event) {
if (event.type == 'click' && event.clientX == 0 && event.clientY == 0) {
return // This click event seems to be fake
} else if (event.type != 'click' && this.value == '') {
return // Don't act on non-click events when the <input> is empty
}
$(this).parents('label').children('input[type="radio"]').prop('checked', true).trigger('change');
2017-06-02 10:12:50 +02:00
});
$('.radio-group .list-group-item > label').on('click', function(event) {
if (event.clientX == 0 && event.clientY == 0) {
return // This click event seems to be fake
}
$(this).children('input[type="radio"]').prop('checked', true).trigger('change');
2017-04-11 09:38:03 +02:00
});
$('[data-toggle="enable"], [data-toggle="disable"]').each(function() {
var enable = this.getAttribute('data-toggle') == 'enable';
var $target = $(this.getAttribute('data-target'));
var $control = $(this);
(this.tagName == 'OPTION' ? $control.parent() : $control).on('change', function() {
var disable = enable ^ ($control.prop('checked') || $control.prop('selected'));
$target.prop('disabled', disable);
$target.find('input[type="checkbox"]').each(function() {
var $subelement = $(this);
if (disable) {
$subelement.data('was-checked', $subelement.prop('checked'));
$subelement.prop('checked', false);
} else {
$subelement.prop('checked', $subelement.data('was-checked'));
}
$subelement.prop('disabled', disable);
});
});
});
$('[data-email]').one('mouseover click', function () {
$(this).attr('href', 'mailto:'+$(this).data('email'));
});
$('[data-email-reveal]').one('click', function () {
$(this).html($(this).data('email-reveal'));
});
$('button[data-action="reload"]').on('click', function() {
location.reload();
});
};
$(function(){
try {
Liberapay.init();
} catch (exc) {
Liberapay.error(exc);
}
});
2018-01-09 11:04:04 +01:00
Liberapay.error = function(exc) {
console.error(exc);
var msg = "An error occurred (" + exc + ").\n" +
"Please contact support@liberapay.com if the problem persists.";
Liberapay.notification(msg, 'error', -1);
}
Liberapay.get_object_by_name = function(name) {
return name.split('.').reduce(function(o, k) {return o[k]}, window);
}
Liberapay.jsonml = function(jsonml) {
2015-05-31 11:34:06 +02:00
var node = document.createElement(jsonml[0]);
2015-05-31 11:34:06 +02:00
jQuery.each(jsonml, function(j, v) {
if (j === 0 || typeof v === 'undefined') return;
switch (v.constructor) {
case Object:
for (var p in v)
node.setAttribute(p, v[p]);
break;
2015-05-31 11:34:06 +02:00
case Array: node.appendChild(Liberapay.jsonml(v)); break;
default: node.appendChild(document.createTextNode(v.toString())); break;
}
});
return node;
};