redirect to login on unauthorized ajax calls [fixes #29]

This commit is contained in:
Gert
2015-04-04 22:34:34 +02:00
parent 2ec9946a70
commit b06211b641
3 changed files with 133 additions and 101 deletions

View File

@@ -49,127 +49,152 @@ $(function () {
}); });
} }
var ajaxRequest = function (url, settings) {
settings = typeof settings === 'undefined' ? typeof url === 'string' ? {} : url : settings;
settings.url = typeof settings.url === 'undefined' && typeof url === 'string' ? url : settings.url;
var successHandler = typeof settings.success !== 'undefined' ? typeof settings.success === 'function' ? [ settings.success ] : settings.success : [];
successHandler.unshift(ajaxRequest.logoutHandler);
settings.success = successHandler;
return $.ajax(settings);
};
ajaxRequest.logoutHandler = function (response, status, xhr) {
if (response.status && (response.status === "unauthorized" || response.status === "forbidden")) {
document.location.href = GravAdmin.config.base_url_relative;
throw "Logged out";
}
};
// Cache Clear // Cache Clear
$('[data-clear-cache]').on('click', function(e) { $('[data-clear-cache]').on('click', function(e) {
$(this).attr('disabled','disabled').find('> .fa').removeClass('fa-trash').addClass('fa-refresh fa-spin'); $(this).attr('disabled','disabled').find('> .fa').removeClass('fa-trash').addClass('fa-refresh fa-spin');
var url = $(this).data('clearCache'); var url = $(this).data('clearCache');
var jqxhr = $.getJSON(url, function(result, status) {
if (result.status == 'success') { ajaxRequest({
toastr.success(result.message); dataType: "json",
} else { url: url,
toastr.error(result.message); success: function(result, status) {
if (result.status == 'success') {
toastr.success(result.message);
} else {
toastr.error(result.message);
}
} }
}); }).complete(function() {
jqxhr.complete(function() {
$('[data-clear-cache]').removeAttr('disabled').find('> .fa').removeClass('fa-refresh fa-spin').addClass('fa-trash'); $('[data-clear-cache]').removeAttr('disabled').find('> .fa').removeClass('fa-refresh fa-spin').addClass('fa-trash');
}); });
}); });
// GPM ajaxRequest({
$.post(window.location.href, { dataType: "JSON",
task: 'GPM', url: window.location.href,
action: 'getUpdates' method: "POST",
}, function (response) { data: {
if (!response.success) { task: 'GPM',
throw new Error(response.message); action: 'getUpdates'
} },
success: function (response) {
if (!response.success) {
throw new Error(response.message);
}
var grav = response.payload.grav, var grav = response.payload.grav,
installed = response.payload.installed, installed = response.payload.installed,
resources = response.payload.resources; resources = response.payload.resources;
//console.log(grav, resources); //console.log(grav, resources);
// grav updatable // grav updatable
if (grav.isUpdatable) { if (grav.isUpdatable) {
var icon = '<i class="fa fa-bullhorn"></i> '; var icon = '<i class="fa fa-bullhorn"></i> ';
content = 'Grav <b>v{available}</b> is now available! <span class="less">(Current: v{version})</span> ', content = 'Grav <b>v{available}</b> is now available! <span class="less">(Current: v{version})</span> ',
button = '<button class="button button-small secondary" data-gpm-update="grav">Update Grav Now</button>'; button = '<button class="button button-small secondary" data-gpm-update="grav">Update Grav Now</button>';
content = jQuery.substitute(content, {available: grav.available, version: grav.version}); content = jQuery.substitute(content, {available: grav.available, version: grav.version});
$('[data-gpm-grav]').addClass('grav').html('<p>' + icon + content + button + '</p>'); $('[data-gpm-grav]').addClass('grav').html('<p>' + icon + content + button + '</p>');
} }
// dashboard // dashboard
if ($('.updates-chart').length) { if ($('.updates-chart').length) {
var missing = (resources.total + (grav.isUpdatable ? 1 : 0)) * 100 / (installed + (grav.isUpdatable ? 1 : 0)), var missing = (resources.total + (grav.isUpdatable ? 1 : 0)) * 100 / (installed + (grav.isUpdatable ? 1 : 0)),
updated = 100 - missing; updated = 100 - missing;
UpdatesChart.update({series: [updated, missing]}); UpdatesChart.update({series: [updated, missing]});
} }
if (resources.total > 0) { if (resources.total > 0) {
var length, var length,
icon = '<i class="fa fa-bullhorn"></i>', icon = '<i class="fa fa-bullhorn"></i>',
content = '{updates} of your {type} have an <strong>update available</strong>', content = '{updates} of your {type} have an <strong>update available</strong>',
button = '<button class="button button-small secondary">Update {Type}</button>', button = '<button class="button button-small secondary">Update {Type}</button>',
plugins = $('.grav-update.plugins'), plugins = $('.grav-update.plugins'),
themes = $('.grav-update.themes'), themes = $('.grav-update.themes'),
sidebar = {plugins: $('#admin-menu a[href$="/plugins"]'), themes: $('#admin-menu a[href$="/themes"]')}; sidebar = {plugins: $('#admin-menu a[href$="/plugins"]'), themes: $('#admin-menu a[href$="/themes"]')};
// sidebar // sidebar
if (sidebar.plugins.length || sidebar.themes.length) { if (sidebar.plugins.length || sidebar.themes.length) {
var length, badges; var length, badges;
if (sidebar.plugins.length && (length = Object.keys(resources.plugins).length)) { if (sidebar.plugins.length && (length = Object.keys(resources.plugins).length)) {
badges = sidebar.plugins.find('.badges'); badges = sidebar.plugins.find('.badges');
badges.addClass('with-updates'); badges.addClass('with-updates');
badges.find('.badge.updates').text(length); badges.find('.badge.updates').text(length);
}
if (sidebar.themes.length && (length = Object.keys(resources.themes).length)) {
badges = sidebar.themes.find('.badges');
badges.addClass('with-updates');
badges.find('.badge.updates').text(length);
}
} }
if (sidebar.themes.length && (length = Object.keys(resources.themes).length)) { // list page
badges = sidebar.themes.find('.badges'); if (plugins[0] && (length = Object.keys(resources.plugins).length)) {
badges.addClass('with-updates'); content = jQuery.substitute(content, {updates: length, type: 'plugins'});
badges.find('.badge.updates').text(length); button = jQuery.substitute(button, {Type: 'All Plugins'});
plugins.html('<p>' + icon + content + button + '</p>');
var plugin, url;
$.each(resources.plugins, function (key, value) {
plugin = $('[data-gpm-plugin="' + key + '"] .gpm-name');
url = plugin.find('a');
plugin.append('<a href="' + url.attr('href') + '"><span class="badge update">Update available!</span></a>');
});
}
if (themes[0] && (length = Object.keys(resources.themes).length)) {
content = jQuery.substitute(content, {updates: length, type: 'themes'});
button = jQuery.substitute(button, {Type: 'All Themes'});
themes.html('<p>' + icon + content + button + '</p>');
var theme, url;
$.each(resources.themes, function (key, value) {
theme = $('[data-gpm-theme="' + key + '"]');
url = theme.find('.gpm-name a');
theme.append('<div class="gpm-ribbon"><a href="' + url.attr('href') + '">UPDATE</a></div>');
});
}
// details page
var type = 'plugin',
details = $('.grav-update.plugin')[0];
if (!details) {
details = $('.grav-update.theme')[0];
type = 'theme';
}
if (details){
var slug = $('[data-gpm-' + type + ']').data('gpm-' + type),
Type = type.charAt(0).toUpperCase() + type.substring(1);
content = '<strong>v{available}</strong> of this ' + type + ' is now available!';
content = jQuery.substitute(content, {available: resources[type + 's'][slug].available});
button = jQuery.substitute(button, {Type: Type});
$(details).html('<p>' + icon + content + button + '</p>');
} }
} }
// list page
if (plugins[0] && (length = Object.keys(resources.plugins).length)) {
content = jQuery.substitute(content, {updates: length, type: 'plugins'});
button = jQuery.substitute(button, {Type: 'All Plugins'});
plugins.html('<p>' + icon + content + button + '</p>');
var plugin, url;
$.each(resources.plugins, function (key, value) {
plugin = $('[data-gpm-plugin="' + key + '"] .gpm-name');
url = plugin.find('a');
plugin.append('<a href="' + url.attr('href') + '"><span class="badge update">Update available!</span></a>');
});
}
if (themes[0] && (length = Object.keys(resources.themes).length)) {
content = jQuery.substitute(content, {updates: length, type: 'themes'});
button = jQuery.substitute(button, {Type: 'All Themes'});
themes.html('<p>' + icon + content + button + '</p>');
var theme, url;
$.each(resources.themes, function (key, value) {
theme = $('[data-gpm-theme="' + key + '"]');
url = theme.find('.gpm-name a');
theme.append('<div class="gpm-ribbon"><a href="' + url.attr('href') + '">UPDATE</a></div>');
});
}
// details page
var type = 'plugin',
details = $('.grav-update.plugin')[0];
if (!details) {
details = $('.grav-update.theme')[0];
type = 'theme';
}
if (details){
var slug = $('[data-gpm-' + type + ']').data('gpm-' + type),
Type = type.charAt(0).toUpperCase() + type.substring(1);
content = '<strong>v{available}</strong> of this ' + type + ' is now available!';
content = jQuery.substitute(content, {available: resources[type + 's'][slug].available});
button = jQuery.substitute(button, {Type: Type});
$(details).html('<p>' + icon + content + button + '</p>');
}
} }
});
}, 'json');
}); });

View File

@@ -29,6 +29,7 @@
{{ assets.css() }} {{ assets.css() }}
{% endblock %} {% endblock %}
{% include 'partials/javascript-config.html.twig' %}
{% block javascripts %} {% block javascripts %}
{% do assets.add('jquery',101) %} {% do assets.add('jquery',101) %}
{% do assets.addJs(theme_url~'/js/modernizr.custom.71422.js') %} {% do assets.addJs(theme_url~'/js/modernizr.custom.71422.js') %}

View File

@@ -0,0 +1,6 @@
<script type="text/javascript">
window.GravAdmin = window.GravAdmin || {};
window.GravAdmin.config = {
base_url_relative: '{{ base_url_relative }}'
};
</script>