Prettify & document

This commit is contained in:
Kload 2013-07-02 12:35:31 +02:00
parent 8847faaa6e
commit f7832aefe8
7 changed files with 853 additions and 34 deletions

786
css/bootstrap.min.css vendored Normal file

File diff suppressed because one or more lines are too long

BIN
img/ajax-loader.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

View file

@ -4,15 +4,14 @@
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="cache-control" content="no-cache" />
<title>YunoHost admin</title>
<link href="css/bootstrap.min.css" rel="stylesheet" media="screen">
</head>
<body>
<h1>YunoHost</h1>
<body style="padding: 20px">
<h1><a class="logo" href="#/">YunoHost</a></h1>
<br />
<div id="flash">
</div>
<div id="content">
<a href="#/">Greet</a>
</div>
<br />
<div id="main">
<a href="#/login">Grset</a>
</div>
@ -23,7 +22,6 @@
<script type="text/javascript" src="js/vendor/sammy.mustache.js"></script>
<script type="text/javascript" src="js/vendor/sammy.json.js"></script>
<script type="text/javascript" src="js/vendor/sammy.storage.js"></script>
<script type="text/javascript" src="js/vendor/sammy.flash.js"></script>
<script type="text/javascript" src="js/app.js"></script>
</body>

View file

@ -1,10 +1,23 @@
app = Sammy('#main', function (sam) {
/**
* Sammy Configuration
*
*/
// Plugins
sam.use('Mustache', 'ms');
// Initialize storage
var store = new Sammy.Store({name: 'storage', type: 'session'});
/**
* Helpers
*
*/
sam.helpers({
// Flash helper to diplay instant notifications
flash: function (level, message) {
flashs = store.get('flash');
if (!flashs) { flashs = {'info': [], 'fail': [], 'success': [] } }
@ -19,10 +32,13 @@ app = Sammy('#main', function (sam) {
}
$('#flash').html(html);
},
// API connection helper
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')));
this.swap('<img src="img/ajax-loader.gif" />');
jQuery.ajax({
url: store.get('url') + uri,
type: method,
@ -45,42 +61,60 @@ app = Sammy('#main', function (sam) {
callback(result);
});
},
// Render view (cross-browser)
view: function (view, data) {
//data = typeof data !== 'undefined' ? data : {};
this.render('views/'+ view +'.ms', data).swap();
}
});
sam.after(function () {
store.set('flash', {'info': [], 'fail': [], 'success': [] });
});
/**
* Filters
*
*/
sam.before({except: {path: '#/login'}}, function (req) {
// Store path for further redirections
store.set('path', req.path);
if (!store.get('user')) {
// Redirect to login page if no credentials are stored
if (!store.get('password')) {
req.redirect('#/login');
return false;
}
// Clear flash display
if (!store.get('flash')) {
$('#flash').html('');
}
});
sam.after(function () {
// Clear flash notifications
store.clear('flash');
});
/**
* Routes
*
* Note: var "c" is Sammy's route context
* @doc http://sammyjs.org/docs/api/#Sammy.EventContext
*
*/
sam.get('#/', function (c) {
c.view('home');
});
sam.get('#/users/:user', function (c) {
c.swap('');
c.api('/users/'+ c.params['user'], function(data) {
c.view('user_info', data);
});
});
sam.get('#/login', function (c) {
c.view('login');
});
sam.post('#/login', function (c) {
store.set('url', c.params['url']);
store.set('user', c.params['user']);
store.set('user', 'admin');
store.set('password', btoa(c.params['password']));
c.api('/users', function(data) {
if (data.error) {
@ -95,9 +129,20 @@ app = Sammy('#main', function (sam) {
}
});
});
sam.get('#/users/:user', function (c) {
c.swap('');
c.api('/users/'+ c.params['user'], function(data) {
c.view('user_info', data);
});
});
});
/**
* Run the app
*
*/
$(document).ready(function () {
app.run('#/');
});

View file

@ -1,15 +1,5 @@
<form action="/index.html#/login" id="form" method="post">
<p>
<label>Url:</label>
<input type="text" name="url" />
</p>
<p>
<label>Login:</label>
<input type="text" name="user" />
</p>
<p>
<label>Password:</label>
<input type="password" name="password" />
</p>
<input id="submit" type="submit" value="Gogogo" />
<form action="#/login" id="form" method="post">
<input type="text" name="url" placeholder="YunoHost API URL" /><br />
<input type="password" name="password" placeholder="Password" /><br />
<input id="submit" type="submit" value="Login" class="btn btn-primary"/>
</form>