2013-07-01 15:56:32 +02:00
|
|
|
app = Sammy('#main', function (sam) {
|
|
|
|
|
|
|
|
sam.use('Mustache', 'ms');
|
|
|
|
|
|
|
|
var store = new Sammy.Store({name: 'storage', type: 'session'});
|
|
|
|
|
2013-07-01 19:32:11 +02:00
|
|
|
sam.helpers({
|
|
|
|
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);
|
|
|
|
},
|
|
|
|
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')));
|
|
|
|
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
|
|
|
},
|
|
|
|
view: function (view, data) {
|
|
|
|
//data = typeof data !== 'undefined' ? data : {};
|
|
|
|
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
|
|
|
|
|
|
|
sam.after(function () {
|
|
|
|
store.set('flash', {'info': [], 'fail': [], 'success': [] });
|
|
|
|
});
|
|
|
|
|
|
|
|
sam.before({except: {path: '#/login'}}, function (req) {
|
|
|
|
store.set('path', req.path);
|
|
|
|
if (!store.get('user')) {
|
|
|
|
req.redirect('#/login');
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
sam.get('#/', function (c) {
|
2013-07-01 19:32:11 +02:00
|
|
|
c.view('home');
|
2013-07-01 15:56:32 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
sam.get('#/users/:user', function (c) {
|
|
|
|
c.swap('');
|
2013-07-01 19:32:11 +02:00
|
|
|
c.api('/users/'+ c.params['user'], function(data) {
|
|
|
|
c.view('user_info', data);
|
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']);
|
|
|
|
store.set('user', c.params['user']);
|
|
|
|
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('#/');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
$(document).ready(function () {
|
|
|
|
app.run('#/');
|
|
|
|
});
|