Skip to content

Commit

Permalink
Merge pull request #2391 from liberapay/js-submit
Browse files Browse the repository at this point in the history
  • Loading branch information
Changaco authored Jul 2, 2024
2 parents 96e5822 + 317ce83 commit 81993cc
Show file tree
Hide file tree
Showing 38 changed files with 370 additions and 475 deletions.
14 changes: 8 additions & 6 deletions error.spt
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,13 @@ user_agent = request.headers.get(b'User-Agent')
% endif
% endblock
[----------------------------------------] application/json via json_dump
{ "error_code": code
, "error_message_short": msg
, "error_message_long": err
, "error_id": sentry_ident
, "error_location": error_location
}
{
"error_code": code,
"error_id": sentry_ident,
"error_location": error_location,
"error_message_long": err,
"error_message_short": msg,
"html_template": getattr(response, 'html_template', None),
}
[----------------------------------------] text/plain
{{err or msg}}
63 changes: 11 additions & 52 deletions js/10-base.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,4 @@
Liberapay.getCookie = function(key) {
var o = new RegExp("(?:^|; ?)" + escape(key) + "=([^;]+)").exec(document.cookie);
if (!o) return null;
var value = o[1];
if (value.charAt(0) === '"') value = value.slice(1, -1);
return unescape(value);
}

Liberapay.init = function() {
// https://docs.djangoproject.com/en/dev/ref/contrib/csrf/#ajax
jQuery.ajaxSetup({
beforeSend: function(xhr, settings) {
var safeMethod = (/^(GET|HEAD|OPTIONS|TRACE)$/.test(settings.type));
if (!safeMethod && !settings.crossDomain) {
// We have to avoid httponly on the csrf_token cookie because of this.
xhr.setRequestHeader("X-CSRF-TOKEN", Liberapay.getCookie('csrf_token'));
}
}
});

Liberapay.forms.jsSubmit();

var success_re = /([?&])success=[^&]*/;
Expand All @@ -32,7 +13,6 @@ Liberapay.init = function() {

Liberapay.auto_tail_log();
Liberapay.charts.init();
Liberapay.identity_docs_init();
Liberapay.lookup.init();
Liberapay.s3_uploader_init();
Liberapay.stripe_init();
Expand Down Expand Up @@ -130,31 +110,27 @@ Liberapay.init = function() {

$(function(){ Liberapay.init(); });

Liberapay.error = function(jqXHR, textStatus, errorThrown) {
var msg = null;
if (jqXHR.responseText > "") {
try {
msg = JSON.parse(jqXHR.responseText).error_message_long;
} catch(exc) {}
}
if (typeof msg != "string" || msg.length == 0) {
msg = "An error occurred (" + (errorThrown || textStatus || jqXHR.status) + ").\n" +
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.wrap = function(f) {
return function() {
return async function() {
try {
return f.apply(this, arguments);
} catch (e) {
console.log(e);
Liberapay.notification(e, 'error', -1);
return await f.apply(this, arguments);
} catch (exc) {
Liberapay.error(exc);
}
}
};

Liberapay.get_object_by_name = function(name) {
return name.split('.').reduce(function(o, k) {return o[k]}, window);
}

Liberapay.jsonml = function(jsonml) {
var node = document.createElement(jsonml[0]);

Expand All @@ -175,20 +151,3 @@ Liberapay.jsonml = function(jsonml) {

return node;
};

(function($) {
return $.fn.center = function(position) {
return this.each(function() {
var e = $(this);
var pos = e.css('position');
if (pos != 'absolute' && pos != 'fixed' || position && pos != position) {
e.css('position', position || 'absolute');
}
e.css({
left: '50%',
top: '50%',
margin: '-' + (e.innerHeight() / 2) + 'px 0 0 -' + (e.innerWidth() / 2) + 'px'
});
});
};
})(jQuery);
16 changes: 9 additions & 7 deletions js/charts.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,21 @@ Liberapay.charts.init = function() {
}

Liberapay.charts.load = function(url, $container) {
jQuery.get(url, function(series) {
$(function() {
Liberapay.charts.make(series, $container);
});
}).fail(Liberapay.error);
fetch(url).then(function(response) {
response.json().then(function(series) {
$(function() {
Liberapay.charts.make(series, $container);
});
}).catch(Liberapay.error);
}).catch(Liberapay.error);
}

Liberapay.charts.make = function(series, $container) {
if (series.length) {
$('.chart-wrapper').show();
} else {
if (!!$container.data('msg-empty')) {
$container.append($('<span>').text(' '+$container.data('msg-empty')));
if ($container.attr('data-msg-empty')) {
$container.append($('<span>').text(' '+$container.attr('data-msg-empty')));
}
return;
}
Expand Down
Loading

0 comments on commit 81993cc

Please sign in to comment.