/* global Handlebars */
Handlebars.registerHelper('score', function() {
if(this.score) {
var score = Math.round( this.score / 10 );
var imageName = 'rating/s' + score + '.svg';
return new Handlebars.SafeString('');
}
return new Handlebars.SafeString('');
});
Handlebars.registerHelper('level', function() {
if(typeof this.level !== 'undefined') {
if(this.level === 200) {
return new Handlebars.SafeString('' + t('settings', 'Official') + '');
} else if(this.level === 100) {
return new Handlebars.SafeString('' + t('settings', 'Approved') + '');
} else {
return new Handlebars.SafeString('' + t('settings', 'Experimental') + '');
}
}
});
OC.Settings = OC.Settings || {};
OC.Settings.Apps = OC.Settings.Apps || {
setupGroupsSelect: function($elements) {
OC.Settings.setupGroupsSelect($elements, {
placeholder: t('core', 'All')
});
},
State: {
currentCategory: null,
apps: null
},
loadCategories: function() {
if (this._loadCategoriesCall) {
this._loadCategoriesCall.abort();
}
var categories = [
{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);
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');
}
});
},
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) {
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'));
}
$('.enable.needs-download').tipsy({fallback: t('settings', 'The app will be downloaded from the app store')});
$('.app-level .official').tipsy({fallback: t('settings', 'Official apps are developed by and within the community. They offer central functionality and are ready for production use.')});
$('.app-level .approved').tipsy({fallback: 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.')});
$('.app-level .experimental').tipsy({fallback: t('settings', 'This app is not checked for security issues and is new or known to be unstable. Install at your own risk.')});
},
complete: function() {
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));
availableUpdates++;
OC.Settings.Apps.State.apps[app.id].update = true;
}
});
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);
} else {
$('#apps-list').append(html);
}
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;
currentImage.onload = function() {
page.find('.app-image')
.append(this)
.fadeIn();
};
}
// 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 {
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();
}
}
},
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) {
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');
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;
}
},'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) {
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'));
});
});
}
},'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'));
});
}
},
updateApp:function(appId, element) {
var oldButtonText = element.val();
element.val(t('settings','Updating....'));
OC.Settings.Apps.hideErrorMessage(appId);
$.post(OC.filePath('settings','ajax','updateapp.php'),{appid:appId},function(result) {
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=$('