nextcloud/settings/js/apps.js

665 lines
21 KiB
JavaScript
Raw Normal View History

/* global Handlebars */
Handlebars.registerHelper('score', function() {
if(this.score) {
var score = Math.round( this.score / 10 );
2016-03-11 20:41:32 +03:00
var imageName = 'rating/s' + score + '.svg';
return new Handlebars.SafeString('<img src="' + OC.imagePath('core', imageName) + '">');
}
return new Handlebars.SafeString('');
});
Handlebars.registerHelper('level', function() {
if(typeof this.level !== 'undefined') {
if(this.level === 200) {
return new Handlebars.SafeString('<span class="official icon-checkmark">' + t('settings', 'Official') + '</span>');
} else if(this.level === 100) {
return new Handlebars.SafeString('<span class="approved">' + t('settings', 'Approved') + '</span>');
} else {
return new Handlebars.SafeString('<span class="experimental">' + t('settings', 'Experimental') + '</span>');
}
}
});
OC.Settings = OC.Settings || {};
OC.Settings.Apps = OC.Settings.Apps || {
setupGroupsSelect: function($elements) {
OC.Settings.setupGroupsSelect($elements, {
placeholder: t('core', 'All')
});
},
2014-01-17 17:05:39 +04:00
State: {
currentCategory: null,
apps: null
},
loadCategories: function() {
if (this._loadCategoriesCall) {
this._loadCategoriesCall.abort();
}
var categories = [
2015-10-20 13:02:08 +03:00
{displayName: t('settings', 'Enabled'), ident: 'enabled', id: '0'},
{displayName: t('settings', 'Not enabled'), ident: 'disabled', id: '1'}
];
var source = $("#categories-template").html();
var template = Handlebars.compile(source);
var html = template(categories);
$('#apps-categories').html(html);
2015-10-20 13:02:08 +03:00
OC.Settings.Apps.loadCategory($('#app-navigation').attr('data-category'));
this._loadCategoriesCall = $.ajax(OC.generateUrl('settings/apps/categories'), {
data:{},
type:'GET',
success:function (jsondata) {
var html = template(jsondata);
$('#apps-categories').html(html);
$('#app-category-' + OC.Settings.Apps.State.currentCategory).addClass('active');
},
complete: function() {
$('#app-navigation').removeClass('icon-loading');
}
});
2014-01-17 17:05:39 +04:00
},
2014-01-17 17:05:39 +04:00
loadCategory: function(categoryId) {
if (OC.Settings.Apps.State.currentCategory === categoryId) {
return;
}
if (this._loadCategoryCall) {
this._loadCategoryCall.abort();
}
$('#apps-list')
.addClass('icon-loading')
.removeClass('hidden')
.html('');
$('#apps-list-empty').addClass('hidden');
$('#app-category-' + OC.Settings.Apps.State.currentCategory).removeClass('active');
$('#app-category-' + categoryId).addClass('active');
OC.Settings.Apps.State.currentCategory = categoryId;
this._loadCategoryCall = $.ajax(OC.generateUrl('settings/apps/list?category={categoryId}&includeUpdateInfo=0', {
categoryId: categoryId
}), {
type:'GET',
success: function (apps) {
2015-07-02 18:15:14 +03:00
var appListWithIndex = _.indexBy(apps.apps, 'id');
OC.Settings.Apps.State.apps = appListWithIndex;
var appList = _.map(appListWithIndex, function(app) {
// default values for missing fields
return _.extend({level: 0}, app);
});
var source = $("#app-template").html();
var template = Handlebars.compile(source);
if (appList.length) {
appList.sort(function(a,b) {
var levelDiff = b.level - a.level;
if (levelDiff === 0) {
return OC.Util.naturalSortCompare(a.name, b.name);
}
return levelDiff;
});
var firstExperimental = false;
_.each(appList, function(app) {
if(app.level === 0 && firstExperimental === false) {
firstExperimental = true;
OC.Settings.Apps.renderApp(app, template, null, true);
} else {
OC.Settings.Apps.renderApp(app, template, null, false);
}
});
} else {
$('#apps-list').addClass('hidden');
$('#apps-list-empty').removeClass('hidden').find('h2').text(t('settings', 'No apps found for your version'));
}
2016-07-13 10:59:08 +03:00
$('.enable.needs-download').tooltip({
title: t('settings', 'The app will be downloaded from the app store'),
placement: 'bottom',
container: 'body'
});
2016-07-13 10:59:08 +03:00
$('.app-level .official').tooltip({
title: t('settings', 'Official apps are developed by and within the community. They offer central functionality and are ready for production use.'),
placement: 'bottom',
container: 'body'
});
$('.app-level .approved').tooltip({
title: t('settings', 'Approved apps are developed by trusted developers and have passed a cursory security check. They are actively maintained in an open code repository and their maintainers deem them to be stable for casual to normal use.'),
placement: 'bottom',
container: 'body'
});
$('.app-level .experimental').tooltip({
title: t('settings', 'This app is not checked for security issues and is new or known to be unstable. Install at your own risk.'),
placement: 'bottom',
container: 'body'
});
},
complete: function() {
2016-02-16 14:59:24 +03:00
var availableUpdates = 0;
$('#apps-list').removeClass('icon-loading');
$.ajax(OC.generateUrl('settings/apps/list?category={categoryId}&includeUpdateInfo=1', {
categoryId: categoryId
}), {
type: 'GET',
success: function (apps) {
_.each(apps.apps, function(app) {
if (app.update) {
var $update = $('#app-' + app.id + ' .update');
$update.removeClass('hidden');
$update.val(t('settings', 'Update to %s').replace(/%s/g, app.update));
2016-02-16 14:59:24 +03:00
availableUpdates++;
OC.Settings.Apps.State.apps[app.id].update = true;
}
2016-02-16 14:59:24 +03:00
});
if (availableUpdates > 0) {
OC.Notification.show(n('settings', 'You have %n app update pending', 'You have %n app updates pending', availableUpdates));
}
}
});
}
});
},
renderApp: function(app, template, selector, firstExperimental) {
if (!template) {
var source = $("#app-template").html();
template = Handlebars.compile(source);
}
if (typeof app === 'string') {
app = OC.Settings.Apps.State.apps[app];
}
app.firstExperimental = firstExperimental;
if (!app.preview) {
app.preview = OC.imagePath('core', 'default-app-icon');
app.previewAsIcon = true;
}
var html = template(app);
if (selector) {
selector.html(html);
2012-08-05 01:48:09 +04:00
} else {
$('#apps-list').append(html);
2012-08-05 01:48:09 +04:00
}
var page = $('#app-' + app.id);
// image loading kung-fu (IE doesn't properly scale SVGs, so disable app icons)
if (app.preview && !OC.Util.isIE()) {
var currentImage = new Image();
currentImage.src = app.preview;
2016-08-17 17:04:53 +03:00
currentImage.onload = function() {
page.find('.app-image')
.append(OC.Settings.Apps.imageUrl(app.preview, app.detailpage))
.fadeIn();
};
}
2016-08-11 10:29:57 +03:00
// set group select properly
if(OC.Settings.Apps.isType(app, 'filesystem') || OC.Settings.Apps.isType(app, 'prelogin') ||
OC.Settings.Apps.isType(app, 'authentication') || OC.Settings.Apps.isType(app, 'logging') ||
OC.Settings.Apps.isType(app, 'prevent_group_restriction')) {
page.find(".groups-enable").hide();
page.find(".groups-enable__checkbox").prop('checked', false);
} else {
2015-04-02 16:47:48 +03:00
page.find('#group_select').val((app.groups || []).join('|'));
if (app.active) {
if (app.groups.length) {
OC.Settings.Apps.setupGroupsSelect(page.find('#group_select'));
page.find(".groups-enable__checkbox").prop('checked', true);
} else {
page.find(".groups-enable__checkbox").prop('checked', false);
}
page.find(".groups-enable").show();
} else {
page.find(".groups-enable").hide();
}
}
2012-08-05 01:48:09 +04:00
},
2016-08-11 10:29:57 +03:00
/**
* Returns the image for apps listing
* url : the url of the image
* appfromstore: bool to check whether the app is fetched from store or not.
2016-08-11 10:29:57 +03:00
*/
imageUrl : function (url, appfromstore) {
2016-08-11 10:29:57 +03:00
var img = '<svg width="72" height="72" viewBox="0 0 72 72">';
2016-08-17 16:16:48 +03:00
if (appfromstore) {
img += '<image x="0" y="0" width="72" height="72" preserveAspectRatio="xMinYMin meet" xlink:href="' + url + '?v=' + oc_config.version + '" class="app-icon" /></svg>';
} else {
img += '<image x="0" y="0" width="72" height="72" preserveAspectRatio="xMinYMin meet" filter="url(#invertIcon)" xlink:href="' + url + '?v=' + oc_config.version + '" class="app-icon"></image></svg>';
}
2016-08-11 10:29:57 +03:00
return img;
},
isType: function(app, type){
return app.types && app.types.indexOf(type) !== -1;
},
/**
* Checks the server health.
*
* If the promise fails, the server is broken.
*
* @return {Promise} promise
*/
_checkServerHealth: function() {
return $.get(OC.generateUrl('apps/files'));
},
enableApp:function(appId, active, element, groups) {
var self = this;
OC.Settings.Apps.hideErrorMessage(appId);
groups = groups || [];
var appItem = $('div#app-'+appId+'');
element.val(t('settings','Please wait....'));
if(active && !groups.length) {
$.post(OC.filePath('settings','ajax','disableapp.php'),{appid:appId},function(result) {
2013-04-17 17:32:03 +04:00
if(!result || result.status !== 'success') {
if (result.data && result.data.message) {
OC.Settings.Apps.showErrorMessage(appId, result.data.message);
appItem.data('errormsg', result.data.message);
} else {
OC.Settings.Apps.showErrorMessage(appId, t('settings', 'Error while disabling app'));
appItem.data('errormsg', t('settings', 'Error while disabling app'));
}
element.val(t('settings','Disable'));
appItem.addClass('appwarning');
} else {
OC.Settings.Apps.rebuildNavigation();
appItem.data('active',false);
appItem.data('groups', '');
element.data('active',false);
appItem.removeClass('active');
2012-08-05 01:48:09 +04:00
element.val(t('settings','Enable'));
element.parent().find(".groups-enable").hide();
element.parent().find('#group_select').hide().val(null);
OC.Settings.Apps.State.apps[appId].active = false;
2012-08-05 01:48:09 +04:00
}
},'json');
} else {
// TODO: display message to admin to not refresh the page!
// TODO: lock UI to prevent further operations
$.post(OC.filePath('settings','ajax','enableapp.php'),{appid: appId, groups: groups},function(result) {
2013-04-17 17:32:03 +04:00
if(!result || result.status !== 'success') {
if (result.data && result.data.message) {
OC.Settings.Apps.showErrorMessage(appId, result.data.message);
appItem.data('errormsg', result.data.message);
} else {
OC.Settings.Apps.showErrorMessage(appId, t('settings', 'Error while enabling app'));
appItem.data('errormsg', t('settings', 'Error while disabling app'));
}
element.val(t('settings','Enable'));
appItem.addClass('appwarning');
} else {
self._checkServerHealth().done(function() {
if (result.data.update_required) {
OC.Settings.Apps.showReloadMessage();
setTimeout(function() {
location.reload();
}, 5000);
}
OC.Settings.Apps.rebuildNavigation();
appItem.data('active',true);
element.data('active',true);
appItem.addClass('active');
element.val(t('settings','Disable'));
var app = OC.Settings.Apps.State.apps[appId];
app.active = true;
if (OC.Settings.Apps.isType(app, 'filesystem') || OC.Settings.Apps.isType(app, 'prelogin') ||
OC.Settings.Apps.isType(app, 'authentication') || OC.Settings.Apps.isType(app, 'logging')) {
element.parent().find(".groups-enable").prop('checked', true);
element.parent().find(".groups-enable").hide();
element.parent().find('#group_select').hide().val(null);
} else {
element.parent().find("#groups-enable").show();
if (groups) {
appItem.data('groups', JSON.stringify(groups));
} else {
appItem.data('groups', '');
}
}
}).fail(function() {
// server borked, emergency disable app
$.post(OC.webroot + '/index.php/disableapp', {appid: appId}, function() {
OC.Settings.Apps.showErrorMessage(
appId,
t('settings', 'Error: this app cannot be enabled because it makes the server unstable')
);
appItem.data('errormsg', t('settings', 'Error while enabling app'));
element.val(t('settings','Enable'));
appItem.addClass('appwarning');
}).fail(function() {
OC.Settings.Apps.showErrorMessage(
appId,
t('settings', 'Error: could not disable broken app')
);
appItem.data('errormsg', t('settings', 'Error while disabling broken app'));
element.val(t('settings','Enable'));
});
});
2012-08-05 01:48:09 +04:00
}
},'json')
.fail(function() {
OC.Settings.Apps.showErrorMessage(appId, t('settings', 'Error while enabling app'));
appItem.data('errormsg', t('settings', 'Error while enabling app'));
appItem.data('active',false);
appItem.addClass('appwarning');
element.val(t('settings','Enable'));
});
2012-08-05 01:48:09 +04:00
}
},
updateApp:function(appId, element) {
var oldButtonText = element.val();
2013-02-01 03:51:00 +04:00
element.val(t('settings','Updating....'));
OC.Settings.Apps.hideErrorMessage(appId);
$.post(OC.filePath('settings','ajax','updateapp.php'),{appid:appId},function(result) {
2013-04-17 17:32:03 +04:00
if(!result || result.status !== 'success') {
if (result.data && result.data.message) {
OC.Settings.Apps.showErrorMessage(appId, result.data.message);
} else {
OC.Settings.Apps.showErrorMessage(appId, t('settings','Error while updating app'));
}
element.val(oldButtonText);
}
else {
element.val(t('settings','Updated'));
element.hide();
}
},'json');
},
uninstallApp:function(appId, element) {
OC.Settings.Apps.hideErrorMessage(appId);
element.val(t('settings','Uninstalling ....'));
$.post(OC.filePath('settings','ajax','uninstallapp.php'),{appid:appId},function(result) {
if(!result || result.status !== 'success') {
OC.Settings.Apps.showErrorMessage(appId, t('settings','Error while uninstalling app'));
element.val(t('settings','Uninstall'));
} else {
OC.Settings.Apps.rebuildNavigation();
element.parent().fadeOut(function() {
element.remove();
});
}
},'json');
},
rebuildNavigation: function() {
$.getJSON(OC.filePath('settings', 'ajax', 'navigationdetect.php')).done(function(response){
if(response.status === 'success'){
var idsToKeep = {};
var navEntries=response.nav_entries;
var container = $('#apps ul');
for(var i=0; i< navEntries.length; i++){
var entry = navEntries[i];
idsToKeep[entry.id] = true;
if(container.children('li[data-id="'+entry.id+'"]').length === 0){
var li=$('<li></li>');
li.attr('data-id', entry.id);
var img = '<svg width="32" height="32" viewBox="0 0 32 32">';
img += '<defs><filter id="invert"><feColorMatrix in="SourceGraphic" type="matrix" values="-1 0 0 0 1 0 -1 0 0 1 0 0 -1 0 1 0 0 0 1 0" /></filter></defs>';
img += '<image x="0" y="0" width="32" height="32" preserveAspectRatio="xMinYMin meet" filter="url(#invert)" xlink:href="' + entry.icon + '" class="app-icon" /></svg>';
2013-01-31 10:22:56 +04:00
var a=$('<a></a>').attr('href', entry.href);
2013-06-29 00:10:40 +04:00
var filename=$('<span></span>');
var loading = $('<div class="icon-loading-dark"></div>').css('display', 'none');
filename.text(entry.name);
a.prepend(filename);
a.prepend(loading);
a.prepend(img);
li.append(a);
// append the new app as last item in the list
2014-06-04 20:22:40 +04:00
// which is the "add apps" entry with the id
// #apps-management
$('#apps-management').before(li);
// scroll the app navigation down
// so the newly added app is seen
$('#navigation').animate({
scrollTop: $('#navigation').height()
}, 'slow');
// draw attention to the newly added app entry
// by flashing it twice
2014-06-04 20:22:40 +04:00
$('#header .menutoggle')
.animate({opacity: 0.5})
.animate({opacity: 1})
.animate({opacity: 0.5})
2014-06-04 20:22:40 +04:00
.animate({opacity: 1})
.animate({opacity: 0.75});
}
}
container.children('li[data-id]').each(function(index, el) {
if (!idsToKeep[$(el).data('id')]) {
$(el).remove();
}
});
}
});
},
showErrorMessage: function(appId, message) {
$('div#app-'+appId+' .warning')
.show()
.text(message);
},
hideErrorMessage: function(appId) {
$('div#app-'+appId+' .warning')
.hide()
.text('');
2015-01-18 16:52:19 +03:00
},
showReloadMessage: function() {
OC.dialogs.info(
t(
'settings',
2015-10-06 12:34:06 +03:00
'The app has been enabled but needs to be updated. You will be redirected to the update page in 5 seconds.'
),
t('settings','App update'),
function () {
window.location.reload();
},
true
);
},
2015-01-18 16:52:19 +03:00
filter: function(query) {
var $appList = $('#apps-list'),
$emptyList = $('#apps-list-empty');
$appList.removeClass('hidden');
$appList.find('.section').removeClass('hidden');
$emptyList.addClass('hidden');
if (query === '') {
return;
}
2015-01-18 16:52:19 +03:00
query = query.toLowerCase();
$appList.find('.section').addClass('hidden');
2015-01-18 16:52:19 +03:00
// App Name
2015-01-18 16:52:19 +03:00
var apps = _.filter(OC.Settings.Apps.State.apps, function (app) {
return app.name.toLowerCase().indexOf(query) !== -1;
});
2016-01-07 18:33:25 +03:00
// App ID
apps = apps.concat(_.filter(OC.Settings.Apps.State.apps, function (app) {
return app.id.toLowerCase().indexOf(query) !== -1;
}));
// App Description
2015-01-18 16:52:19 +03:00
apps = apps.concat(_.filter(OC.Settings.Apps.State.apps, function (app) {
return app.description.toLowerCase().indexOf(query) !== -1;
}));
// Author Name
apps = apps.concat(_.filter(OC.Settings.Apps.State.apps, function (app) {
return app.author.toLowerCase().indexOf(query) !== -1;
}));
// App status
if (t('settings', 'Official').toLowerCase().indexOf(query) !== -1) {
apps = apps.concat(_.filter(OC.Settings.Apps.State.apps, function (app) {
return app.level === 200;
}));
}
if (t('settings', 'Approved').toLowerCase().indexOf(query) !== -1) {
apps = apps.concat(_.filter(OC.Settings.Apps.State.apps, function (app) {
return app.level === 100;
}));
}
if (t('settings', 'Experimental').toLowerCase().indexOf(query) !== -1) {
apps = apps.concat(_.filter(OC.Settings.Apps.State.apps, function (app) {
return app.level !== 100 && app.level !== 200;
}));
}
2015-01-18 16:52:19 +03:00
apps = _.uniq(apps, function(app){return app.id;});
if (apps.length === 0) {
$appList.addClass('hidden');
$emptyList.removeClass('hidden');
2016-03-31 16:26:37 +03:00
$emptyList.removeClass('hidden').find('h2').text(t('settings', 'No apps found for {query}', {
query: query
}));
} else {
_.each(apps, function (app) {
$('#app-' + app.id).removeClass('hidden');
});
2015-01-18 16:52:19 +03:00
$('#searchresults').hide();
}
2015-02-23 17:29:25 +03:00
},
2015-10-26 19:05:31 +03:00
_onPopState: function(params) {
params = _.extend({
category: 'enabled'
}, params);
OC.Settings.Apps.loadCategory(params.category);
},
2015-02-23 17:29:25 +03:00
/**
* Initializes the apps list
*/
initialize: function($el) {
OC.Plugins.register('OCA.Search', OC.Settings.Apps.Search);
OC.Settings.Apps.loadCategories();
2015-10-26 19:05:31 +03:00
OC.Util.History.addOnPopStateHandler(_.bind(this._onPopState, this));
2015-02-23 17:29:25 +03:00
$(document).on('click', 'ul#apps-categories li', function () {
var categoryId = $(this).data('categoryId');
OC.Settings.Apps.loadCategory(categoryId);
2015-10-26 19:05:31 +03:00
OC.Util.History.pushState({
category: categoryId
});
2015-10-15 17:50:31 +03:00
$('#searchbox').val('');
2015-02-23 17:29:25 +03:00
});
$(document).on('click', '.app-description-toggle-show', function () {
$(this).addClass('hidden');
$(this).siblings('.app-description-toggle-hide').removeClass('hidden');
$(this).siblings('.app-description-container').slideDown();
});
$(document).on('click', '.app-description-toggle-hide', function () {
$(this).addClass('hidden');
$(this).siblings('.app-description-toggle-show').removeClass('hidden');
$(this).siblings('.app-description-container').slideUp();
});
2015-02-23 17:29:25 +03:00
$(document).on('click', '#apps-list input.enable', function () {
var appId = $(this).data('appid');
var element = $(this);
var active = $(this).data('active');
2015-02-23 17:29:25 +03:00
OC.Settings.Apps.enableApp(appId, active, element);
});
2015-02-23 17:29:25 +03:00
$(document).on('click', '#apps-list input.uninstall', function () {
var appId = $(this).data('appid');
var element = $(this);
2015-02-23 17:29:25 +03:00
OC.Settings.Apps.uninstallApp(appId, element);
});
2015-02-23 17:29:25 +03:00
$(document).on('click', '#apps-list input.update', function () {
var appId = $(this).data('appid');
var element = $(this);
2015-02-23 17:29:25 +03:00
OC.Settings.Apps.updateApp(appId, element);
});
2015-02-23 17:29:25 +03:00
$(document).on('change', '#group_select', function() {
var element = $(this).parent().find('input.enable');
var groups = $(this).val();
if (groups && groups !== '') {
groups = groups.split('|');
2015-02-23 17:29:25 +03:00
} else {
groups = [];
}
2015-02-23 17:29:25 +03:00
var appId = element.data('appid');
if (appId) {
OC.Settings.Apps.enableApp(appId, false, element, groups);
OC.Settings.Apps.State.apps[appId].groups = groups;
}
});
2015-10-31 19:02:30 +03:00
$(document).on('change', ".groups-enable__checkbox", function() {
var $select = $(this).closest('.section').find('#group_select');
2015-02-23 17:29:25 +03:00
$select.val('');
2015-02-23 17:29:25 +03:00
if (this.checked) {
OC.Settings.Apps.setupGroupsSelect($select);
} else {
$select.select2('destroy');
}
2015-02-23 17:29:25 +03:00
$select.change();
});
$(document).on('click', '#enable-experimental-apps', function () {
var state = $(this).prop('checked');
$.ajax(OC.generateUrl('settings/apps/experimental'), {
data: {state: state},
type: 'POST',
success:function () {
location.reload();
}
});
});
2015-02-23 17:29:25 +03:00
}
};
2015-02-23 17:29:25 +03:00
OC.Settings.Apps.Search = {
attach: function (search) {
search.setFilter('settings', OC.Settings.Apps.filter);
}
};
$(document).ready(function () {
// HACK: FIXME: use plugin approach
if (!window.TESTING) {
OC.Settings.Apps.initialize($('#apps-list'));
}
2011-08-10 14:20:43 +04:00
});