yunohost-admin/js/app.js

189 lines
5.3 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
2013-07-01 15:56:32 +02:00
sam.use('Mustache', 'ms');
2013-07-02 12:35:31 +02:00
// Initialize storage
2013-07-01 15:56:32 +02:00
var store = new Sammy.Store({name: 'storage', type: 'session'});
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
// 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) {
html += '<div class="'+ lvl +'">'+ msg +'</div>';
});
}
$('#flash').html(html);
},
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) {
method = typeof method !== 'undefined' ? method : 'GET';
data = typeof data !== 'undefined' ? data : {};
auth = "Basic "+ btoa(store.get('user') +':'+ atob(store.get('password')));
2013-07-02 12:35:31 +02:00
this.swap('<img src="img/ajax-loader.gif" />');
2013-07-01 19:32:11 +02:00
jQuery.ajax({
url: store.get('url') + uri,
type: method,
crossdomain: true,
data: data,
dataType: 'json',
beforeSend: function(req) {
req.setRequestHeader('Authorization', auth);
}
})
.done(function(data) {
console.log(data);
result = data;
})
.fail(function() {
alert('fail');
result = false;
})
.always(function() {
callback(result);
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);
function leSwap() {
$('#slideBack').hide().html('');
$('#slideTo').hide().html('');
rendered.swap(function() {
$('.slide').on('click', function() {
if ($(this).hasClass('back')) {
store.set('slide', 'back');
} else {
store.set('slide', 'to');
}
});
});
}
2013-09-21 20:59:47 +02:00
blockSize = $('#slider').width();
2013-09-21 19:46:26 +02:00
if (store.get('slide') == 'back') {
2013-09-21 20:59:47 +02:00
$('#slideBack').show().css('display', 'inline-block').css('margin-left', '-'+ blockSize +'px');
2013-09-21 19:46:26 +02:00
rendered.appendTo($('#slideBack'));
2013-09-21 20:59:47 +02:00
$('#main').animate({marginLeft: blockSize +'px'}, 500);
2013-09-21 19:46:26 +02:00
$('#slideBack').animate({marginLeft: '0'}, 500, function() {
$('#main').html($('#slideBack').html());
$('#main').css('margin-left', '0');
leSwap();
});
} else if (store.get('slide') == 'to') {
$('#slideTo').show().css('display', 'inline-block');
rendered.appendTo($('#slideTo'));
2013-09-21 20:59:47 +02:00
$('#main').animate({marginLeft: '-'+ blockSize +'px'}, 500, function() {
2013-09-21 19:46:26 +02:00
$('#main').html($('#slideTo').html());
$('#main').css('margin-left', '0');
leSwap();
});
} else {
leSwap();
}
//this.render('views/'+ view +'.ms', data).swap();
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-07-01 15:56:32 +02:00
sam.before({except: {path: '#/login'}}, function (req) {
2013-07-02 12:35:31 +02:00
// Store path for further redirections
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-07-02 12:35:31 +02:00
if (!store.get('password')) {
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')) {
$('#flash').html('');
}
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-09-21 19:46:26 +02:00
// Clear sliding preference
store.clear('slide');
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) {
c.view('home');
2013-07-01 15:56:32 +02:00
});
sam.get('#/login', function (c) {
2013-07-01 19:32:11 +02:00
c.view('login');
2013-07-01 15:56:32 +02:00
});
sam.post('#/login', function (c) {
store.set('url', c.params['url']);
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-07-01 19:32:11 +02:00
c.api('/users', function(data) {
2013-07-01 15:56:32 +02:00
if (data.error) {
2013-07-01 19:32:11 +02:00
c.flash('fail', 'Error: '+ data.error);
2013-07-01 15:56:32 +02:00
} else {
2013-07-01 19:32:11 +02:00
c.flash('success', 'Connected :)');
2013-07-01 15:56:32 +02:00
}
if (store.get('path')) {
c.redirect(store.get('path'));
} else {
c.redirect('#/');
}
});
});
2013-07-02 12:35:31 +02:00
sam.get('#/users/:user', function (c) {
c.swap('');
c.api('/users/'+ c.params['user'], function(data) {
c.view('user_info', data);
});
});
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('#/');
});