gpm functionality in admin

This commit is contained in:
Gert
2015-04-13 21:37:12 +02:00
parent 6cb3749c56
commit cb77f0f848
8 changed files with 385 additions and 111 deletions

View File

@@ -87,114 +87,137 @@ $(function () {
});
});
ajaxRequest({
dataType: "JSON",
url: window.location.href,
method: "POST",
data: {
task: 'GPM',
action: 'getUpdates'
},
success: function (response) {
if (!response.success) {
throw new Error(response.message);
}
// Cache Clear
$('[data-maintenance-update]').on('click', function(e) {
var grav = response.payload.grav,
installed = response.payload.installed,
resources = response.payload.resources;
$(this).attr('disabled','disabled').find('> .fa').removeClass('fa-cloud-download').addClass('fa-refresh fa-spin');
var url = $(this).data('maintenanceUpdate');
//console.log(grav, resources);
// grav updatable
if (grav.isUpdatable) {
var icon = '<i class="fa fa-bullhorn"></i> ';
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>';
content = jQuery.substitute(content, {available: grav.available, version: grav.version});
$('[data-gpm-grav]').addClass('grav').html('<p>' + icon + content + button + '</p>');
}
// dashboard
if ($('.updates-chart').length) {
var missing = (resources.total + (grav.isUpdatable ? 1 : 0)) * 100 / (installed + (grav.isUpdatable ? 1 : 0)),
updated = 100 - missing;
UpdatesChart.update({series: [updated, missing]});
}
if (resources.total > 0) {
var length,
icon = '<i class="fa fa-bullhorn"></i>',
content = '{updates} of your {type} have an <strong>update available</strong>',
button = '<button class="button button-small secondary">Update {Type}</button>',
plugins = $('.grav-update.plugins'),
themes = $('.grav-update.themes'),
sidebar = {plugins: $('#admin-menu a[href$="/plugins"]'), themes: $('#admin-menu a[href$="/themes"]')};
// sidebar
if (sidebar.plugins.length || sidebar.themes.length) {
var length, badges;
if (sidebar.plugins.length && (length = Object.keys(resources.plugins).length)) {
badges = sidebar.plugins.find('.badges');
badges.addClass('with-updates');
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);
}
}
// 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>');
ajaxRequest({
dataType: "json",
url: url,
success: function(result, status) {
if (result.status == 'success') {
toastr.success(result.message);
} else {
toastr.error(result.message);
}
}
}
}).complete(function() {
GPMRefresh();
$('[data-maintenance-update]').removeAttr('disabled').find('> .fa').removeClass('fa-refresh fa-spin').addClass('fa-cloud-download');
});
});
var GPMRefresh = function () {
ajaxRequest({
dataType: "JSON",
url: window.location.href,
method: "POST",
data: {
task: 'GPM',
action: 'getUpdates'
},
success: function (response) {
if (!response.success) {
throw new Error(response.message);
}
var grav = response.payload.grav,
installed = response.payload.installed,
resources = response.payload.resources;
// grav updatable
if (grav.isUpdatable) {
var icon = '<i class="fa fa-bullhorn"></i> ';
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>';
content = jQuery.substitute(content, {available: grav.available, version: grav.version});
$('[data-gpm-grav]').addClass('grav').html('<p>' + icon + content + button + '</p>');
}
// dashboard
if ($('.updates-chart').length) {
var missing = (resources.total + (grav.isUpdatable ? 1 : 0)) * 100 / (installed + (grav.isUpdatable ? 1 : 0)),
updated = 100 - missing;
UpdatesChart.update({series: [updated, missing]});
}
if (resources.total > 0) {
var length,
icon = '<i class="fa fa-bullhorn"></i>',
content = '{updates} of your {type} have an <strong>update available</strong>',
button = '<a href="{location}/task:update" class="button button-small secondary">Update {Type}</a>',
plugins = $('.grav-update.plugins'),
themes = $('.grav-update.themes'),
sidebar = {plugins: $('#admin-menu a[href$="/plugins"]'), themes: $('#admin-menu a[href$="/themes"]')};
// sidebar
if (sidebar.plugins.length || sidebar.themes.length) {
var length, badges;
if (sidebar.plugins.length && (length = Object.keys(resources.plugins).length)) {
badges = sidebar.plugins.find('.badges');
badges.addClass('with-updates');
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);
}
}
// 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', location: GravAdmin.config.base_url_relative + '/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', location: GravAdmin.config.base_url_relative + '/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, location: GravAdmin.config.base_url_relative + '/' + type + 's/' + slug});
$(details).html('<p>' + icon + content + button + '</p>');
}
}
}
});
};
GPMRefresh();
});