first commit

This commit is contained in:
2018-08-01 16:00:05 +02:00
commit 944fd76206
3205 changed files with 356767 additions and 0 deletions
@@ -0,0 +1,24 @@
import $ from 'jquery';
import request from '../utils/request';
const switcher = $('input[type="radio"][name="channel-switch"]');
if (switcher) {
switcher.on('change', (event) => {
let radio = $(event.target);
let url = `${radio.parent('[data-url]').data('url')}`;
request(url, {
method: 'post',
body: {
task: 'gpmRelease',
release: radio.val()
}
},
(response) => {
if (response.reload) {
global.location.reload();
}
});
});
}
@@ -0,0 +1,26 @@
import $ from 'jquery';
import { Instance as gpm } from '../utils/gpm';
import { translations } from 'grav-config';
import toastr from '../utils/toastr';
// Check for updates trigger
$('[data-gpm-checkupdates]').on('click', function() {
let element = $(this);
element.find('i').addClass('fa-spin');
gpm.fetch((response) => {
element.find('i').removeClass('fa-spin');
let payload = response.payload;
if (!payload) { return; }
if (!payload.grav.isUpdatable && !payload.resources.total) {
toastr.success(translations.PLUGIN_ADMIN.EVERYTHING_UP_TO_DATE);
} else {
var grav = payload.grav.isUpdatable ? 'Grav v' + payload.grav.available : '';
var resources = payload.resources.total ? payload.resources.total + ' ' + translations.PLUGIN_ADMIN.UPDATES_ARE_AVAILABLE : '';
if (!resources) { grav += ' ' + translations.PLUGIN_ADMIN.IS_AVAILABLE_FOR_UPDATE; }
toastr.info(grav + (grav && resources ? ' ' + translations.PLUGIN_ADMIN.AND + ' ' : '') + resources);
}
}, true);
});
@@ -0,0 +1,70 @@
import $ from 'jquery';
import { config } from 'grav-config';
import request from '../utils/request';
const URI = `${config.base_url_relative}/ajax.json/task${config.param_sep}getnewsfeed`;
class Feed {
constructor() {
this.data = null;
}
fetch(refresh = false, callback = function() {}) {
request(URI, {
method: 'post',
body: { refresh }
}, (response) => {
this.data = response;
callback(response);
});
}
refresh(refresh = false) {
const feed = $('#news-feed .widget-content');
if (!feed.length) { return; }
let loader = feed.find('.widget-loader');
loader.find('div').remove();
loader.find('.fa-warning').removeClass('fa-warning').addClass('fa-refresh fa-spin');
loader.show();
feed.find('> ul').hide();
if (!this.data || this.data.error || refresh) {
this.fetch(refresh, this.updateContent.bind(this));
} else {
this.updateContent();
}
}
updateContent() {
const feed = $('#news-feed .widget-content');
if (!feed.length) { return; }
let loader = feed.find('.widget-loader').hide();
let content = feed.find('> ul').empty().show();
if (this.data.error || this.data.status === 'error') {
loader.show().find('div').remove();
loader.find('.fa-refresh').removeClass('fa-refresh fa-spin').addClass('fa-warning');
loader.append(`<div>${this.data.error ? this.data.error.message : this.data.message || 'Unable to download news feed'}</div>`);
return;
}
if (this.data && this.data.feed_data) {
this.data.feed_data.forEach((data) => {
content.append(data);
});
}
}
}
let feed = new Feed();
$(document).ready(() => feed.refresh());
$(document).on('click', '[data-refresh="feed"]', (event) => {
event.preventDefault();
feed.refresh(true);
});
export default feed;
@@ -0,0 +1,186 @@
import $ from 'jquery';
import unique from 'mout/array/unique';
import { config, translations } from 'grav-config';
import formatBytes from '../utils/formatbytes';
import { Instance as gpm } from '../utils/gpm';
import Notifications from './notifications';
import Feed from './feed';
import './check';
import './update';
import './channel-switcher';
export default class Updates {
constructor(payload = {}) {
this.setPayload(payload);
this.task = `task${config.param_sep}`;
}
setPayload(payload = {}) {
this.payload = payload;
return this;
}
fetch(force = false) {
gpm.fetch((response) => this.setPayload(response), force);
return this;
}
maintenance(mode = 'hide') {
let element = $('#updates [data-update-packages]');
element[mode === 'show' ? 'fadeIn' : 'fadeOut']();
if (mode === 'hide') {
$('.badges.with-updates').removeClass('with-updates').find('.badge.updates').remove();
}
return this;
}
grav() {
let payload = this.payload.grav;
if (payload && payload.isUpdatable) {
let task = this.task;
let bar = '';
if (!payload.isSymlink) {
bar += `<button data-maintenance-update="${config.base_url_relative}/update.json/${task}updategrav/admin-nonce${config.param_sep}${config.admin_nonce}" class="button button-small secondary" id="grav-update-button">${translations.PLUGIN_ADMIN.UPDATE_GRAV_NOW}</button>`;
} else {
bar += `<span class="hint--left" style="float: right;" data-hint="${translations.PLUGIN_ADMIN.GRAV_SYMBOLICALLY_LINKED}"><i class="fa fa-fw fa-link"></i></span>`;
}
bar += `
Grav <b>v${payload.available}</b> ${translations.PLUGIN_ADMIN.IS_NOW_AVAILABLE}! <span class="less">(${translations.PLUGIN_ADMIN.CURRENT} v${payload.version})</span>
`;
let element = $('[data-gpm-grav]').removeClass('hidden');
if (element.is(':empty')) {
element.hide();
}
element
.addClass('grav')
.html(`${bar}`)
.slideDown(150)
.parent('#messages').addClass('default-box-shadow');
}
$('#grav-update-button').on('click', function() {
$(this).html(`${translations.PLUGIN_ADMIN.UPDATING_PLEASE_WAIT} ${formatBytes(payload.assets['grav-update'].size)}..`);
});
return this;
}
resources() {
if (!this.payload || !this.payload.resources || !this.payload.resources.total) {
return this.maintenance('hide');
}
let is_current_package_latest = true;
let map = ['plugins', 'themes'];
let singles = ['plugin', 'theme'];
let { plugins, themes } = this.payload.resources;
if (!this.payload.resources.total) { return this; }
[plugins, themes].forEach(function(resources, index) {
if (!resources || Array.isArray(resources)) { return; }
let length = Object.keys(resources).length;
let type = map[index];
// sidebar
$(`#admin-menu a[href$="/${map[index]}"]`)
.find('.badges')
.addClass('with-updates')
.find('.badge.updates').text(length);
var type_translation = '';
// update all
if (type === 'plugins') {
type_translation = translations.PLUGIN_ADMIN.PLUGINS;
} else {
type_translation = translations.PLUGIN_ADMIN.THEMES;
}
let updateAll = $(`.grav-update.${type}`);
updateAll.css('display', 'block').html(`
<p>
<a href="#" class="button button-small secondary" data-remodal-target="update-packages" data-packages-slugs="${Object.keys(resources).join()}" data-${singles[index]}-action="start-packages-update">${translations.PLUGIN_ADMIN.UPDATE} ${translations.PLUGIN_ADMIN.ALL} ${type_translation}</a>
<i class="fa fa-bullhorn"></i>
${length} ${translations.PLUGIN_ADMIN.OF_YOUR} ${type} ${translations.PLUGIN_ADMIN.HAVE_AN_UPDATE_AVAILABLE}
</p>
`);
let existing_slugs = $('[data-update-packages]').attr('data-packages-slugs') || '';
if (existing_slugs) {
existing_slugs = existing_slugs.split(',');
} else {
existing_slugs = [];
}
let slugs = unique(existing_slugs.concat(Object.keys(resources))).join();
$('[data-update-packages]').attr('data-packages-slugs', `${slugs}`);
Object.keys(resources).forEach(function(item) {
// listing page
let container = $(`[data-gpm-${singles[index]}="${item}"]`);
let element = container.find('.gpm-name');
let url = element.find('a');
let content_wrapper = container.parents('.content-wrapper');
if (type === 'plugins' && !element.find('.badge.update').length) {
element.append(`<a class="plugin-update-button" href="${url.attr('href')}"><span class="badge update">${translations.PLUGIN_ADMIN.UPDATE_AVAILABLE}!</span></a>`);
content_wrapper.addClass('has-updates');
} else if (type === 'themes') {
element.append(`<div class="gpm-ribbon"><a href="${url.attr('href')}">${translations.PLUGIN_ADMIN.UPDATE.toUpperCase()}</a></div>`);
content_wrapper.addClass('has-updates');
}
// details page
if (container.length) {
let details = $(`.grav-update.${singles[index]}`);
if (details.length) {
let releaseType = resources[item].type === 'testing' ? '<span class="gpm-testing">test release</span>' : '';
details.html(`
<p>
<a href="#" class="button button-small secondary" data-remodal-target="update-packages" data-packages-slugs="${item}" data-${singles[index]}-action="start-package-installation">${translations.PLUGIN_ADMIN.UPDATE} ${singles[index].charAt(0).toUpperCase() + singles[index].substr(1).toLowerCase()}</a>
<i class="fa fa-bullhorn"></i>
<strong>v${resources[item].available}</strong> ${releaseType} ${translations.PLUGIN_ADMIN.OF_THIS} ${singles[index]} ${translations.PLUGIN_ADMIN.IS_NOW_AVAILABLE}!
</p>
`).css('display', 'block');
is_current_package_latest = false;
}
}
});
$('[data-update-packages]').removeClass('hidden');
});
$('.content-wrapper').addClass('updates-checked');
if (!is_current_package_latest) {
$('.warning-reinstall-not-latest-release').removeClass('hidden');
}
}
}
let Instance = new Updates();
export { Instance, Notifications, Feed };
// automatically refresh UI for updates (graph, sidebar, plugin/themes pages) after every fetch
gpm.on('fetched', (response, raw) => {
Instance.setPayload(response.payload || {});
Instance.grav().resources();
});
if (config.enable_auto_updates_check === '1') {
gpm.fetch();
}
@@ -0,0 +1,287 @@
import $ from 'jquery';
import { config } from 'grav-config';
import request from '../utils/request';
const canFetchNotifications = () => config.notifications;
class Notifications {
showNotificationInFeed(notification, index) {
let notifications = $('#notifications').removeClass('hidden');
let loader = notifications.find('.widget-loader').hide();
let content = notifications.find('.widget-content > ul').show();
loader.find('div').remove();
loader.find('.fa-warning').removeClass('fa-warning').addClass('fa-refresh fa-spin');
if (!notification.type) {
notification.type = 'note';
}
switch (notification.type) {
case 'note':
notification.intro_text = 'Note';
break;
case 'info':
notification.intro_text = 'Info';
break;
case 'warning':
notification.intro_text = 'Warning';
break;
}
let hidden = '';
if (index > 9) {
hidden = ' hidden ';
}
if (notification.link) {
const title = document.createElement('div');
title.innerHTML = notification.message;
content.append(`
<li class="single-notification ${hidden}">
<span class="badge alert ${notification.type}">${notification.intro_text}</span>
<a target="_blank" href="${notification.link}" title="${(title.textContent || title.innerText || '')}">${notification.message}</a>
</li>
`);
} else {
let clean_text = $('<p>' + notification.message + '</p>').text();
content.append(`
<li class="single-notification ${hidden}">
<span class="badge alert ${notification.type}">${notification.intro_text}</span>
<span title="${clean_text}">${notification.message}</span>
</li>
`);
}
}
addShowAllInFeed() {
$('#notifications ul').append(`
<li class="show-all" data-notification-action="show-all-notifications">Show all</li>
`);
}
showNotificationInTop(notification) {
let element;
if (notification.link) {
element = $(`<div class="single-notification ${notification.type} alert">
<a target="_blank" href="${notification.link}">${notification.message}</a>
${notification.closeButton}
</div>`);
} else {
element = $(`<div class="single-notification ${notification.type} alert">
${notification.message}
${notification.closeButton}
</div>`);
}
element.hide();
$('.top-notifications-container').removeClass('hidden').addClass('default-box-shadow').append(element);
element.slideDown(150);
}
showNotificationInDashboard(notification) {
let element;
if (notification.link) {
element = $(`<div class="single-notification alert ${notification.type}">
<a target="_blank" href="${notification.link}">${notification.message}</a>
${notification.closeButton}
</div>`);
} else {
element = $(`<div class="single-notification alert ${notification.type}">
${notification.message}
${notification.closeButton}
</div>`);
}
element.hide();
$('.dashboard-notifications-container').removeClass('hidden').append(element);
element.slideDown(150);
}
showNotificationInPlugins(notification) {
let element;
if (notification.link) {
element = $(`<div class="single-notification alert ${notification.type}">
<a target="_blank" href="${notification.link}">${notification.message}</a>
${notification.closeButton}
</div>`);
} else {
element = $(`<div class="single-notification alert ${notification.type}">
${notification.message} ${notification.closeButton}
</div>`);
}
element.hide();
$('.plugins-notifications-container').removeClass('hidden').append(element);
element.slideDown(150);
}
showNotificationInThemes(notification) {
let element;
if (notification.link) {
element = $(`<div class="single-notification alert ${notification.type}">
<a target="_blank" href="${notification.link}">${notification.message}</a>
${notification.closeButton}
</div>`);
} else {
element = $(`<div class="single-notification alert ${notification.type}">
${notification.message}
${notification.closeButton}
</div>`);
}
element.hide();
$('.themes-notifications-container').removeClass('hidden').append(element);
element.slideDown(150);
}
processLocation(location, notification, index = 0) {
switch (location) {
case 'feed':
this.showNotificationInFeed(notification, index);
break;
case 'top':
if (!notification.read) {
this.showNotificationInTop(notification);
}
break;
case 'dashboard':
if (!notification.read) {
this.showNotificationInDashboard(notification);
}
break;
case 'plugins':
if (!notification.read) {
this.showNotificationInPlugins(notification);
}
break;
case 'themes':
if (!notification.read) {
this.showNotificationInThemes(notification);
}
break;
}
}
// Grav.default.Notifications.fetch()
fetch({ locations = [], refresh = false } = {}) {
if (!canFetchNotifications()) {
return false;
}
let that = this;
let feed = $('#notifications');
let loader = feed.find('.widget-loader');
let content = feed.find('.widget-content > ul');
loader.find('div').remove();
loader.find('.fa-warning').removeClass('fa-warning').addClass('fa-refresh fa-spin');
loader.show();
content.hide();
let processNotifications = function processNotifications(response) {
let notifications = response.notifications;
$('#notifications').find('.widget-content > ul').empty();
if (notifications) {
let index = 0;
notifications.forEach(function(notification, i) {
notification.closeButton = `<a href="#" data-notification-action="hide-notification" data-notification-id="${notification.id}" class="close hide-notification"><i class="fa fa-close"></i></a>`;
if (notification.options && notification.options.indexOf('sticky') !== -1) {
notification.closeButton = '';
}
if (Array.isArray(notification.location)) {
notification.location.forEach(function(location) {
if (locations.length && locations.indexOf(location) === -1) {
return;
}
if (location === 'feed') {
that.processLocation(location, notification, index);
index++;
} else {
that.processLocation(location, notification);
}
});
} else {
if (locations.length && locations.indexOf(notification.location) === -1) {
return;
}
that.processLocation(notification.location, notification);
}
});
if (index > 10) {
that.addShowAllInFeed();
}
}
};
request(`${config.base_url_relative}/notifications.json/task${config.param_sep}getNotifications`, {
method: 'post'
}, (response) => {
if (response.need_update === true || refresh) {
$.get((config.local_notifications ? 'http://localhost' : 'https://getgrav.org') + '/notifications.json?' + Date.now()).then(function(response) {
request(`${config.base_url_relative}/notifications.json/task${config.param_sep}processNotifications`, {
method: 'post',
body: { 'notifications': JSON.stringify(response) }
}, (response) => {
if (response.show_immediately === true) {
processNotifications(response);
}
});
}).fail(function() {
let widget = $('#notifications .widget-content');
widget
.find('.widget-loader')
.find('div').remove();
widget
.find('.widget-loader')
.append('<div>Failed to retrieve notifications</div>')
.find('.fa-spin')
.removeClass('fa-spin fa-refresh').addClass('fa-warning');
});
}
processNotifications(response);
});
}
}
let notifications = new Notifications();
export default notifications;
if (canFetchNotifications()) {
notifications.fetch();
$(document).on('click', '[data-notification-action="hide-notification"]', (event) => {
let notification_id = $(event.target).parents('.hide-notification').data('notification-id');
let url = `${config.base_url_relative}/notifications.json/task${config.param_sep}hideNotification/notification_id${config.param_sep}${notification_id}`;
request(url, { method: 'post' }, () => {});
$(event.target).parents('.single-notification').hide();
});
$(document).on('click', '[data-notification-action="show-all-notifications"]', (event) => {
$('#notifications .show-all').hide();
$('#notifications .hidden').removeClass('hidden');
});
$(document).on('click', '[data-refresh="notifications"]', (event) => {
event.preventDefault();
notifications.fetch({ locations: ['feed'], refresh: true });
});
}
@@ -0,0 +1,19 @@
import $ from 'jquery';
import request from '../utils/request';
// Dashboard update and Grav update
$('body').on('click', '[data-maintenance-update]', function() {
let element = $(this);
let url = element.data('maintenanceUpdate');
element.attr('disabled', 'disabled').find('> .fa').removeClass('fa-cloud-download').addClass('fa-refresh fa-spin');
request(url, (response) => {
if (response.type === 'updategrav') {
$('[data-gpm-grav]').remove();
$('#footer .grav-version').html(response.version);
}
element.removeAttr('disabled').find('> .fa').removeClass('fa-refresh fa-spin').addClass('fa-cloud-download');
});
});