ajax.js
174 lines
| 5.0 KiB
| application/javascript
|
JavascriptLexer
r4306 | // # Copyright (C) 2010-2020 RhodeCode GmbH | ||
r1 | // # | ||
// # This program is free software: you can redistribute it and/or modify | |||
// # it under the terms of the GNU Affero General Public License, version 3 | |||
// # (only), as published by the Free Software Foundation. | |||
// # | |||
// # This program is distributed in the hope that it will be useful, | |||
// # but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
// # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||
// # GNU General Public License for more details. | |||
// # | |||
// # You should have received a copy of the GNU Affero General Public License | |||
// # along with this program. If not, see <http://www.gnu.org/licenses/>. | |||
// # | |||
// # This program is dual-licensed. If you wish to learn more about the | |||
// # RhodeCode Enterprise Edition, including its added features, Support services, | |||
// # and proprietary license terms, please see https://rhodecode.com/licenses/ | |||
/** | |||
* turns objects into GET query string | |||
*/ | |||
var toQueryString = function(o) { | |||
r873 | if(typeof o === 'string') { | ||
return o; | |||
} | |||
r1 | if(typeof o !== 'object') { | ||
return false; | |||
} | |||
var _p, _qs = []; | |||
for(_p in o) { | |||
_qs.push(encodeURIComponent(_p) + '=' + encodeURIComponent(o[_p])); | |||
} | |||
return _qs.join('&'); | |||
}; | |||
/** | |||
* ajax call wrappers | |||
*/ | |||
r4310 | |||
var ajaxGET = function (url, success, failure) { | |||
var sUrl = url; | |||
var request = $.ajax({ | |||
url: sUrl, | |||
headers: {'X-PARTIAL-XHR': true} | |||
}) | |||
.done(function (data) { | |||
success(data); | |||
}) | |||
.fail(function (jqXHR, textStatus, errorThrown) { | |||
if (failure) { | |||
failure(jqXHR, textStatus, errorThrown); | |||
} else { | |||
var message = formatErrorMessage(jqXHR, textStatus, errorThrown); | |||
ajaxErrorSwal(message); | |||
} | |||
}); | |||
return request; | |||
}; | |||
var ajaxPOST = function (url, postData, success, failure) { | |||
var sUrl = url; | |||
var postData = toQueryString(postData); | |||
var request = $.ajax({ | |||
type: 'POST', | |||
url: sUrl, | |||
data: postData, | |||
headers: {'X-PARTIAL-XHR': true} | |||
}) | |||
.done(function (data) { | |||
success(data); | |||
}) | |||
.fail(function (jqXHR, textStatus, errorThrown) { | |||
if (failure) { | |||
failure(jqXHR, textStatus, errorThrown); | |||
} else { | |||
var message = formatErrorMessage(jqXHR, textStatus, errorThrown); | |||
ajaxErrorSwal(message); | |||
} | |||
}); | |||
return request; | |||
r1 | }; | ||
r4310 | |||
r4339 | |||
SwalNoAnimation = Swal.mixin({ | |||
confirmButtonColor: '#84a5d2', | |||
cancelButtonColor: '#e85e4d', | |||
showClass: { | |||
popup: 'swal2-noanimation', | |||
backdrop: 'swal2-noanimation' | |||
}, | |||
hideClass: { | |||
popup: '', | |||
backdrop: '' | |||
}, | |||
}) | |||
/* Example usage: | |||
* | |||
error: function(jqXHR, textStatus, errorThrown) { | |||
var prefix = "Error while fetching entries.\n" | |||
var message = formatErrorMessage(jqXHR, textStatus, errorThrown, prefix); | |||
ajaxErrorSwal(message); | |||
} | |||
* | |||
* */ | |||
r4310 | function formatErrorMessage(jqXHR, textStatus, errorThrown, prefix) { | ||
if(typeof prefix === "undefined") { | |||
prefix = '' | |||
} | |||
if (jqXHR.status === 0) { | |||
return (prefix + 'Not connected.\nPlease verify your network connection.'); | |||
} else if (jqXHR.status == 401) { | |||
return (prefix + 'Unauthorized access. [401]'); | |||
} else if (jqXHR.status == 404) { | |||
return (prefix + 'The requested page not found. [404]'); | |||
} else if (jqXHR.status == 500) { | |||
return (prefix + 'Internal Server Error [500].'); | |||
} else if (jqXHR.status == 503) { | |||
return (prefix + 'Service unavailable [503].'); | |||
} else if (errorThrown === 'parsererror') { | |||
return (prefix + 'Requested JSON parse failed.'); | |||
} else if (errorThrown === 'timeout') { | |||
return (prefix + 'Time out error.'); | |||
} else if (errorThrown === 'abort') { | |||
return (prefix + 'Ajax request aborted.'); | |||
r1140 | } else { | ||
r4310 | return (prefix + 'Uncaught Error.\n' + jqXHR.responseText); | ||
r1140 | } | ||
r4310 | } | ||
function ajaxErrorSwal(message) { | |||
r4339 | SwalNoAnimation.fire({ | ||
r4310 | icon: 'error', | ||
r4325 | title: _gettext('Ajax Request Error'), | ||
r4310 | html: '<span style="white-space: pre-line">{0}</span>'.format(message), | ||
showClass: { | |||
popup: 'swal2-noanimation', | |||
backdrop: 'swal2-noanimation' | |||
}, | |||
hideClass: { | |||
popup: '', | |||
backdrop: '' | |||
} | |||
}) | |||
r4313 | } | ||
/* | |||
* use in onclick attributes e.g | |||
* onclick="submitConfirm(event, this, _gettext('Confirm to delete '), _gettext('Confirm Delete'), 'what we delete')"> | |||
* */ | |||
function submitConfirm(event, self, question, confirmText, htmlText) { | |||
if (htmlText === "undefined") { | |||
htmlText = null; | |||
} | |||
if (confirmText === "undefined") { | |||
confirmText = _gettext('Delete') | |||
} | |||
event.preventDefault(); | |||
r4339 | SwalNoAnimation.fire({ | ||
r4313 | title: question, | ||
icon: 'warning', | |||
html: htmlText, | |||
r4339 | |||
r4313 | showCancelButton: true, | ||
r4339 | |||
r4313 | confirmButtonText: confirmText | ||
}).then(function(result) { | |||
if (result.value) { | |||
$(self).closest("form").submit(); | |||
} | |||
}) | |||
r4310 | } |