yunohost-admin/js/app.js

587 lines
20 KiB
JavaScript
Raw Normal View History

2013-07-01 15:56:32 +02:00
app = Sammy('#main', function (sam) {
2013-07-02 12:35:31 +02:00
/**
* Sammy Configuration
*
*/
// Plugins
sam.use('Handlebars', 'ms');
2013-07-01 15:56:32 +02:00
2013-12-15 17:14:42 +01:00
// Look for supported type of storage to use
var storageType;
if (Sammy.Store.isAvailable('session')) {
storageType = 'session';
} else if (Sammy.Store.isAvailable('cookie')) {
storageType = 'cookie';
} else {
storageType = 'memory';
}
2013-07-02 12:35:31 +02:00
// Initialize storage
2013-12-15 17:14:42 +01:00
var store = new Sammy.Store({name: 'storage', type: storageType});
2013-11-23 13:02:55 +01:00
var loaded = false;
2013-07-01 15:56:32 +02:00
2013-07-02 12:35:31 +02:00
/**
* Helpers
*
*/
2013-07-01 19:32:11 +02:00
sam.helpers({
2013-07-02 12:35:31 +02:00
2013-11-22 15:31:47 +01:00
// Serialize an object
serialize : function(obj) {
var str = [];
for(var p in obj)
if (obj.hasOwnProperty(p)) {
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
}
return str.join("&");
},
2013-07-02 12:35:31 +02:00
// Flash helper to diplay instant notifications
2013-07-01 19:32:11 +02:00
flash: function (level, message) {
flashs = store.get('flash');
if (!flashs) { flashs = {'info': [], 'fail': [], 'success': [] } }
flashs[level].push(message);
store.set('flash', flashs);
2013-07-01 15:56:32 +02:00
2013-07-01 19:32:11 +02:00
html = '';
for(lvl in flashs) {
flashs[lvl].forEach( function(msg) {
if (lvl == 'fail') { alertClass = 'alert-danger'; }
else { alertClass = 'alert-'+ lvl; }
html += '<div class="alert '+ alertClass +'">'+ msg +'</div>';
2013-07-01 19:32:11 +02:00
});
}
$('#flash').html(html).fadeIn();
2013-07-01 19:32:11 +02:00
},
2013-07-02 12:35:31 +02:00
// API connection helper
2013-07-01 19:32:11 +02:00
api: function (uri, callback, method, data) {
2013-09-23 21:18:51 +02:00
c = this;
2013-07-01 19:32:11 +02:00
method = typeof method !== 'undefined' ? method : 'GET';
data = typeof data !== 'undefined' ? data : {};
var args = data;
2013-07-01 19:32:11 +02:00
auth = "Basic "+ btoa(store.get('user') +':'+ atob(store.get('password')));
2013-10-31 20:37:43 +01:00
if (uri == '/postinstall') {
var installing = false;
2013-11-21 10:54:33 +01:00
setInterval(function () {
2013-10-31 20:37:43 +01:00
installing = true;
2013-11-21 10:54:33 +01:00
}, 6000);
2013-11-23 13:02:55 +01:00
2013-11-21 10:54:33 +01:00
$('#popup-title').text('Installing');
2013-11-23 13:02:55 +01:00
$('#popup-body').html('<p>YunoHost is being installed on <strong>'+ data.domain +'</strong>. It may take a few minutes ...</p><br><div class="text-center"><img src="img/ajax-loader.gif"></div><br>');
2013-11-21 10:54:33 +01:00
$('#popup').modal('show');
2013-11-23 13:02:55 +01:00
} else {
loaded = false;
if ($('div.loader-content').length == 0) {
setInterval(function () {
if (!loaded && $('div.loader-content').length == 0) {
$('#main').append('<div class="loader-content"><img src="img/ajax-loader.gif"></div>');
}
}, 500);
}
2013-10-31 20:37:43 +01:00
}
2013-07-01 19:32:11 +02:00
jQuery.ajax({
url: store.get('url') + uri,
type: method,
crossdomain: true,
data: data,
2013-09-23 12:31:35 +02:00
traditional: true,
2013-07-01 19:32:11 +02:00
dataType: 'json',
beforeSend: function(req) {
req.setRequestHeader('Authorization', auth);
}
})
2013-09-23 21:18:51 +02:00
.success(function(data) {
data = typeof data !== 'undefined' ? data : {};
if (typeof data.win !== 'undefined') {
$.each(data.win, function(k, v) {
c.flash('success', v);
});
}
callback(data);
2013-07-01 19:32:11 +02:00
})
2013-09-23 21:18:51 +02:00
.fail(function(xhr) {
console.log(xhr);
if (xhr.status == 401) {
c.flash('fail', 'Wrong password');
2013-11-23 13:31:30 +01:00
} else if (typeof xhr.responseJSON !== 'undefined') {
c.flash('fail', xhr.responseJSON.error);
} else {
2013-10-31 20:37:43 +01:00
if (uri == '/postinstall') {
if (installing) {
if (args.domain.match(/\.nohost\.me$/) || args.domain.match(/\.noho\.st$/)) {
2013-11-23 13:02:55 +01:00
$('#popup-title').text('Installed');
2013-11-23 15:18:37 +01:00
$('#popup-body p').text('YunoHost has been successfully installed, we\'ll wait for DNS to be propagated. It will take 3 minutes ...');
2013-11-23 13:02:55 +01:00
interval = 180000;
} else {
interval = 5000;
}
2013-11-21 14:56:46 +01:00
setInterval(function () {
$('#popup-title').text('Installation complete');
$('#popup-body').html('<p>YunoHost has been successfully installed, please go to <a href="https://'+ args.domain +'/ynhadmin" target="_blank"><strong>https://'+ args.domain +'/ynhadmin</strong></a>.</p><br><p><small><a href="https://doc.yunohost.org/#/dns" target="_blank">Not working ?</a></small></p>');
}, interval);
2013-11-21 10:54:33 +01:00
} else {
$('#popup').modal('hide');
c.flash('fail', 'An error occured, try again');
2013-10-31 20:37:43 +01:00
}
} else {
c.flash('fail', 'Server error');
2013-10-31 20:37:43 +01:00
}
}
2013-09-23 21:18:51 +02:00
store.clear('slide');
2013-09-24 22:54:48 +02:00
c.redirect(store.get('path-1'));
2013-07-01 19:32:11 +02:00
})
2013-09-23 21:18:51 +02:00
.done(function(data) {
2013-11-23 13:02:55 +01:00
loaded = true;
$('div.loader-content').remove();
2013-09-23 21:18:51 +02:00
console.log(data);
2013-07-01 15:56:32 +02:00
});
2013-07-01 19:32:11 +02:00
},
2013-07-02 12:35:31 +02:00
// Render view (cross-browser)
2013-07-01 19:32:11 +02:00
view: function (view, data) {
2013-09-21 19:46:26 +02:00
rendered = this.render('views/'+ view +'.ms', data);
2013-09-25 21:49:12 +02:00
enableSlide = true; // Change to false to disable animation
2013-11-23 13:02:55 +01:00
loaded = true;
$('div.loader-content').remove();
2013-09-25 21:49:12 +02:00
if (enableSlide) {
function leSwap() {
rendered.swap(function() {
$('.slide').on('click', function() {
$(this).addClass('active');
if ($(this).hasClass('back')) {
store.set('slide', 'back');
} else {
store.set('slide', 'to');
}
});
2013-09-21 19:46:26 +02:00
});
2013-09-25 21:49:12 +02:00
}
2013-09-21 20:59:47 +02:00
2013-09-25 21:49:12 +02:00
blockSize = $('#slider').width();
// Slide back effect
if (store.get('slide') == 'back') {
store.clear('slide');
$('#slideBack').css('display', 'none');
$('#slider-container').removeClass('move').css('margin-left', '-'+ blockSize +'px');
$('#slideTo').show().html($('#main').html());
leSwap();
$('#slider-container').addClass('move').css('margin-left', '0px');
// Slide to effect
} else if (store.get('slide') == 'to') {
store.clear('slide');
$('#slideTo').css('display', 'none');
$('#slider-container').removeClass('move').css('margin-left', '-'+ blockSize +'px');
$('#slider-container').removeClass('move').css('margin-left', '0px');
$('#slideBack').show().html($('#main').html());
leSwap();
$('#slider-container').addClass('move').css('margin-left', '-'+ blockSize +'px');
2013-09-25 02:33:39 +02:00
2013-09-25 21:49:12 +02:00
} else {
leSwap();
}
2013-09-21 19:46:26 +02:00
} else {
2013-09-25 21:49:12 +02:00
rendered.swap();
2013-09-21 19:46:26 +02:00
}
2013-07-01 15:56:32 +02:00
}
2013-07-01 19:32:11 +02:00
});
2013-07-01 15:56:32 +02:00
2013-07-02 12:35:31 +02:00
/**
* Filters
*
*/
2013-10-01 12:40:59 +02:00
sam.before({except: {path: ['#/login', '#/postinstall']}}, function (req) {
2013-07-02 12:35:31 +02:00
// Store path for further redirections
2013-09-23 21:18:51 +02:00
store.set('path-1', store.get('path'));
2013-07-01 15:56:32 +02:00
store.set('path', req.path);
2013-07-02 12:35:31 +02:00
2013-09-21 12:10:54 +02:00
// Redirect to login page if no credentials stored
2013-12-19 02:02:32 +01:00
if (!store.get('connected')) {
2013-07-01 15:56:32 +02:00
req.redirect('#/login');
return false;
}
2013-07-02 12:35:31 +02:00
// Clear flash display
if (!store.get('flash')) {
2013-09-22 21:31:51 +02:00
$('#flash').fadeOut(function() { $('#flash').html(''); });
2013-07-02 12:35:31 +02:00
}
2013-07-01 15:56:32 +02:00
});
2013-07-02 12:35:31 +02:00
sam.after(function () {
// Clear flash notifications
store.clear('flash');
2013-07-01 15:56:32 +02:00
});
2013-07-02 12:35:31 +02:00
/**
* Routes
*
* Note: var "c" is Sammy's route context
* @doc http://sammyjs.org/docs/api/#Sammy.EventContext
*
*/
sam.get('#/', function (c) {
2013-12-19 02:18:08 +01:00
// Show development note
c.flash('info', '<b>You are using a development version.</b><br />' +
'Please note that you can use the <a href="https://doc.yunohost.org/#/moulinette" target="_blank">moulinette</a> if you want to access to more YunoHost\'s features.');
2014-02-11 10:45:13 +01:00
// Available sections
data = {links: [
{name: "Users", path: '#/users'},
{name: "Domains", path: '#/domains'},
{name: "Applications", path: '#/apps'},
{name: "Services", path: '#/services'},
]};
c.view('home', data);
2013-07-01 15:56:32 +02:00
});
sam.get('#/login', function (c) {
$('.logout-button').hide();
store.set('path-1', '#/login');
2013-09-25 21:49:12 +02:00
// Check if te client is hosted on a yunohost node
domain = window.location.hostname
2013-09-30 18:47:38 +02:00
$.ajax({
2013-11-21 13:49:46 +01:00
url: 'https://'+ domain +'/ynhapi/api',
2013-09-30 18:47:38 +02:00
timeout: 3000
})
.success(function() {
2013-11-21 13:49:46 +01:00
$.getJSON('https://'+ domain +'/ynhapi/installed', function(data) {
2013-09-25 21:49:12 +02:00
if (!data.installed) {
2013-10-01 12:40:59 +02:00
c.redirect('#/postinstall');
2013-09-25 21:49:12 +02:00
} else {
c.view('login', { 'domain': domain });
}
});
})
.fail(function() {
c.view('login');
});
2013-07-01 15:56:32 +02:00
});
sam.post('#/login', function (c) {
2013-11-21 13:49:46 +01:00
store.set('url', 'https://'+ c.params['domain'] +'/ynhapi');
2013-07-02 12:35:31 +02:00
store.set('user', 'admin');
2013-07-01 15:56:32 +02:00
store.set('password', btoa(c.params['password']));
2013-09-25 21:49:12 +02:00
c.api('/api', function(data) {
2013-12-19 02:18:08 +01:00
if (data.apiVersion) {
2013-09-25 21:49:12 +02:00
c.api('/users', function(data) {
store.set('connected', true);
$('.logout-button').fadeIn();
2013-09-25 21:49:12 +02:00
c.flash('success', 'Logged in');
if (store.get('path')) {
c.redirect(store.get('path'));
} else {
c.redirect('#/');
}
});
2013-07-01 15:56:32 +02:00
} else {
2013-12-19 02:18:08 +01:00
c.flash('fail', 'Non-compatible API');
2013-09-25 21:49:12 +02:00
c.redirect('#/login');
2013-07-01 15:56:32 +02:00
}
});
});
2013-07-02 12:35:31 +02:00
sam.get('#/logout', function (c) {
store.clear('url');
store.clear('user');
store.clear('password');
store.clear('connected');
store.set('path', '#/');
c.flash('success', 'Logged out');
c.redirect('#/login');
});
2013-10-01 12:40:59 +02:00
sam.get('#/postinstall', function(c) {
c.view('postinstall', {'DDomains': ['.nohost.me', '.noho.st']});
});
sam.post('#/postinstall', function (c) {
if (c.params['password'] == c.params['confirmation']) {
if (c.params['domain'] == '') {
if (c.params['ddomain'] == '') {
c.flash('fail', "You should indicate a domain");
store.clear('slide');
2013-10-01 12:45:56 +02:00
c.redirect('#/postinstall');
} else {
params = { 'domain': c.params['ddomain'] + c.params['ddomain-ext'] }
2013-10-01 12:40:59 +02:00
}
} else {
2013-10-01 12:45:56 +02:00
params = { 'domain': c.params['domain'] }
2013-10-01 12:40:59 +02:00
}
params['password'] = c.params['password']
2013-11-21 13:49:46 +01:00
store.set('url', 'https://'+ window.location.hostname +'/ynhapi');
2013-10-01 12:40:59 +02:00
store.set('user', 'admin');
store.set('password', btoa('yunohost'));
c.api('/postinstall', function(data) { // http://api.yunohost.org/#!/tools/tools_postinstall_post_0
c.redirect('#/');
}, 'POST', params);
} else {
c.flash('fail', "Passwords don't match");
}
});
/**
* Users
*
*/
2013-09-22 21:31:51 +02:00
sam.get('#/users', function (c) {
c.api('/users', function(data) { // http://api.yunohost.org/#!/user/user_list_get_3
2013-09-22 21:31:51 +02:00
c.view('user_list', data);
});
});
2013-09-23 13:31:57 +02:00
sam.get('#/users/create', function (c) {
c.api('/domains', function(data) { // http://api.yunohost.org/#!/domain/domain_list_get_2
2013-09-23 13:31:57 +02:00
c.view('user_create', data);
});
});
sam.post('#/users', function (c) {
if (c.params['password'] == c.params['confirmation']) {
c.params['mail'] = c.params['email'] + c.params['domain'];
c.api('/users', function(data) { // http://api.yunohost.org/#!/user/user_create_post_2
2013-09-23 13:31:57 +02:00
c.redirect('#/users');
}, 'POST', c.params.toHash());
} else {
c.flash('fail', "Passwords don't match");
store.clear('slide');
//c.redirect('#/users/create');
}
});
2013-07-02 12:35:31 +02:00
sam.get('#/users/:user', function (c) {
c.api('/users/'+ c.params['user'], function(data) { // http://api.yunohost.org/#!/user/user_info_get_0
2013-07-02 12:35:31 +02:00
c.view('user_info', data);
});
});
2013-09-23 00:40:14 +02:00
sam.get('#/users/:user/edit', function (c) {
c.api('/users/'+ c.params['user'], function(data) { // http://api.yunohost.org/#!/user/user_info_get_0
2013-09-23 00:40:14 +02:00
c.view('user_edit', data);
});
});
2013-09-23 12:31:35 +02:00
sam.put('#/users/:user', function (c) {
params = {}
$.each(c.params.toHash(), function(key, value) {
2013-09-23 12:43:11 +02:00
if (value !== '' && value !== 'user') { params[key] = value; }
2013-09-23 12:31:35 +02:00
});
if ($.isEmptyObject(params)) {
c.flash('fail', 'You should modify something');
store.clear('slide');
c.redirect('#/users/'+ c.params['user'] + '/edit');
} else {
c.api('/users/'+ c.params['user'], function(data) { // http://api.yunohost.org/#!/user/user_update_put_1
2013-09-23 12:31:35 +02:00
c.redirect('#/users/'+ c.params['user']);
}, 'PUT', params);
}
});
2013-09-23 13:31:57 +02:00
2013-09-23 12:43:11 +02:00
sam.get('#/users/:user/delete', function (c) {
if (confirm('Are you sure you want to delete '+ c.params['user'] +' ?')) {
c.api('/users/'+ c.params['user'], function(data) { // http://api.yunohost.org/#!/user/user_delete_delete_4
2013-09-23 12:43:11 +02:00
c.redirect('#/users');
}, 'DELETE');
} else {
store.clear('slide');
c.redirect('#/users/'+ c.params['user']);
}
});
/**
* Domains
*
*/
sam.get('#/domains', function (c) {
c.api('/domains', function(data) { // http://api.yunohost.org/#!/domain/domain_list_get_2
c.view('domain_list', data);
});
});
sam.get('#/domains/add', function (c) {
c.view('domain_add', {'DDomains': ['.nohost.me', '.noho.st']});
});
sam.post('#/domains', function (c) {
if (c.params['domain'] == '') {
if (c.params['ddomain'] == '') {
c.flash('fail', "You should indicate a domain");
store.clear('slide');
c.redirect('#/domains/add');
}
params = { 'domains': c.params['ddomain'] + c.params['ddomain-ext'] }
} else {
params = { 'domains': c.params['domain'] }
}
c.api('/domains', function(data) { // http://api.yunohost.org/#!/domain/domain_add_post_1
c.redirect('#/domains');
}, 'POST', params);
});
2013-09-24 22:54:48 +02:00
/**
* Apps
*
*/
sam.get('#/apps', function (c) {
2013-11-22 12:16:52 +01:00
c.api('/apps', function(data) { // http://api.yunohost.org/#!/app/app_list_get_8
2013-09-24 22:54:48 +02:00
// Keep only installed apps
data2 = { 'Apps': [], 'Installed': true }
$.each(data['Apps'], function(k, v) {
2013-11-22 12:16:52 +01:00
if (v['Installed']) data2['Apps'].push(v);
2013-09-24 22:54:48 +02:00
});
c.view('app_list', data2);
});
});
sam.get('#/apps/install', function (c) {
2013-11-22 12:16:52 +01:00
c.api('/apps', function(data) { // http://api.yunohost.org/#!/app/app_list_get_8
c.api('/apps?raw=true', function(dataraw) { // http://api.yunohost.org/#!/app/app_list_get_8
2013-09-24 22:54:48 +02:00
// Keep only uninstalled apps
data2 = { 'Apps': [] }
$.each(data['Apps'], function(k, v) {
2013-11-22 12:16:52 +01:00
if (dataraw[v['ID']].manifest.multi_instance) v['Installed'] = false;
2013-11-22 15:46:50 +01:00
if (!v['Installed'] && !v['ID'].match(/__[0-9]{1,5}$/)) data2['Apps'].push(v);
2013-09-24 22:54:48 +02:00
});
c.view('app_list', data2);
});
2013-11-22 12:16:52 +01:00
});
});
2013-11-26 18:22:12 +01:00
sam.get('#/apps/refresh', function (c) {
c.api('/app/lists', function(data) { // http://api.yunohost.org/#!/app/app_fetchlist_put_5
c.redirect(store.get('path'));
}, 'PUT');
});
2013-11-22 12:16:52 +01:00
sam.get('#/apps/:app', function (c) {
2013-11-22 15:31:47 +01:00
c.api('/app/'+c.params['app']+'?raw=true', function(data) { // http://api.yunohost.org/#!/app/app_info_get_9
2013-11-22 12:16:52 +01:00
c.view('app_info', data);
});
});
sam.get('#/apps/install/:app', function (c) {
c.api('/apps?raw=true', function(data) { // http://api.yunohost.org/#!/app/app_list_get_8
c.view('app_install', data[c.params['app']]);
});
2013-09-24 22:54:48 +02:00
});
2013-11-22 15:31:47 +01:00
sam.post('#/apps', function(c) {
params = { 'label': c.params['label'], 'app': c.params['app'] }
delete c.params['label'];
delete c.params['app'];
params['args'] = c.serialize(c.params.toHash());
c.api('/app', function() { // http://api.yunohost.org/#!/app/app_install_post_2
c.redirect('#/apps');
}, 'POST', params);
});
2013-11-22 16:01:02 +01:00
sam.get('#/apps/:app/uninstall', function (c) {
if (confirm('Are you sure you want to uninstall '+ c.params['app'] +' ?')) {
2013-11-22 17:56:51 +01:00
c.api('/app/'+ c.params['app'], function() { // http://api.yunohost.org/#!/app/app_remove_delete_4
2013-11-22 16:01:02 +01:00
c.redirect('#/apps');
2013-11-22 17:56:51 +01:00
}, 'DELETE');
2013-11-22 16:01:02 +01:00
} else {
store.clear('slide');
c.redirect('#/apps/'+ c.params['app']);
}
});
2014-01-29 18:32:22 +01:00
/**
* Services
*
*/
// All services status
sam.get('#/services', function (c) {
c.api('/service/status', function(data) { // ?
data2 = { 'services': [] }
$.each(data, function(k, v) {
v.name = k;
v.is_loaded = (v.loaded=='enabled') ? true : false;
v.is_running = (v.status=='running') ? true : false;
data2.services.push(v);
});
c.view('service_list', data2);
});
});
// Status & actions for a service
sam.get('#/services/:service', function (c) {
params = { 'names': c.params['service'] }
c.api('/service/status', function(data) { // ?
data2 = { 'service': data }
data2.service.name = c.params['service'];
data2.service.is_loaded = (data.loaded=='enabled') ? true : false;
data2.service.is_running = (data.status=='running') ? true : false;
store.clear('slide');
c.view('service_info', data2);
}, 'GET', params);
});
// Service log
sam.get('#/services/:service/log', function (c) {
params = { 'name': c.params['service'], 'number': 50 }
c.api('/service/log', function(data) { // ?
data2 = { 'logs': [], 'name': c.params['service'] }
$.each(data, function(k, v) {
data2.logs.push({filename: k, filecontent: v.join('\n')});
2014-01-29 18:32:22 +01:00
});
store.clear('slide');
c.view('service_log', data2);
}, 'GET', params);
});
// Enable/Disable & Start/Stop service
sam.get('#/services/:service/:action', function (c) {
if (confirm('Are you sure you want to '+ c.params['action'] +' '+ c.params['service'] +' ?')) {
params = { 'names': c.params['service'] }
c.api('/service/'+ c.params['action'], function(data) {
store.clear('slide');
c.redirect('#/services/'+ c.params['service']);
}, 'GET', params);
} else {
store.clear('slide');
c.redirect('#/services/'+ c.params['service']);
}
});
2013-07-01 15:56:32 +02:00
});
2013-07-02 12:35:31 +02:00
/**
* Run the app
*
*/
2013-07-01 15:56:32 +02:00
$(document).ready(function () {
app.run('#/');
2013-09-25 21:49:12 +02:00
// Fixes for sliding effect
2013-09-25 11:34:40 +02:00
$('#slider-container').width(2*$('#slider').width() +'px');
2013-09-24 19:02:12 +02:00
$(window).resize(function() {
2013-09-25 12:01:01 +02:00
$('#slideBack').css('display', 'none');
$('#slideTo').css('display', 'none');
$('#slider-container').width(2*$('#slider').width() +'px').removeClass('move').css('margin-left', '0px');
2013-09-24 19:02:12 +02:00
});
2013-07-01 15:56:32 +02:00
});