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>' ;
} ) ;
}
2013-09-22 21:31:51 +02:00
if ( level == 'fail' ) { alertClass = 'alert-danger' ; }
else if ( level == 'success' ) { alertClass = 'alert-success' ; }
else { alertClass = 'alert-info' ; }
$ ( '#flash' ) . removeClass ( ) . addClass ( 'alert ' + alertClass ) . 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 : { } ;
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 ) ;
}
if ( uri == '/postinstall' ) {
$ ( '#popup-title' ) . text ( 'Installing' ) ;
$ ( '#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>' ) ;
$ ( '#popup' ) . modal ( 'show' ) ;
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 ) {
2013-09-24 16:54:33 +02:00
console . log ( xhr ) ;
if ( xhr . status == 401 ) {
c . flash ( 'fail' , 'Wrong password' ) ;
} 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 ) {
2013-11-21 10:54:33 +01:00
$ ( '#popup-body' ) . html ( '<p>YunoHost has been successfully installed, please go to <a href="https://' + window . location . hostname + '/ynhadmin" target="_blank"><strong>https://' + window . location . hostname + '/ynhadmin</strong></a>.</p>' ) ;
} else {
$ ( '#popup' ) . modal ( 'hide' ) ;
2013-10-31 20:37:43 +01:00
}
}
2013-11-21 10:54:33 +01:00
c . flash ( 'fail' , 'An error occured at post-installation.' ) ;
2013-09-24 16:54:33 +02: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 ) {
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
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-10-30 21:27:36 +01:00
if ( ! store . get ( 'connected' ) && window . localStorage . getItem ( "isConnected" ) != 'true' ) {
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 ) {
c . view ( 'home' ) ;
2013-07-01 15:56:32 +02:00
} ) ;
sam . get ( '#/login' , function ( c ) {
2013-09-24 17:38:38 +02:00
$ ( '#logout-button' ) . hide ( ) ;
2013-09-24 16:54:33 +02:00
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-10-06 15:05:36 +02:00
url : 'https://' + domain + ':6767/api' ,
2013-09-30 18:47:38 +02:00
timeout : 3000
} )
. success ( function ( ) {
2013-10-06 15:05:36 +02:00
$ . getJSON ( 'https://' + domain + ':6767/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-10-06 15:05:36 +02:00
store . set ( 'url' , 'https://' + c . params [ 'domain' ] + ':6767' ) ;
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 ) {
if ( data . apiVersion == '0.1' ) {
c . api ( '/users' , function ( data ) {
store . set ( 'connected' , true ) ;
2013-10-30 21:27:36 +01:00
window . localStorage . setItem ( "isConnected" , "true" ) ;
2013-09-25 21:49:12 +02:00
$ ( '#logout-button' ) . fadeIn ( ) ;
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-09-25 21:49:12 +02:00
c . flash ( 'fail' , 'Non-compatible API (0.1 required)' ) ;
c . redirect ( '#/login' ) ;
2013-07-01 15:56:32 +02:00
}
} ) ;
} ) ;
2013-07-02 12:35:31 +02:00
2013-09-24 17:38:38 +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-10-06 15:05:36 +02:00
store . set ( 'url' , 'https://' + window . location . hostname + ':6767' ) ;
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" ) ;
}
} ) ;
2013-09-24 16:54:33 +02:00
/ * *
* Users
*
* /
2013-09-22 21:31:51 +02:00
sam . get ( '#/users' , function ( c ) {
2013-09-24 16:54:33 +02:00
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 ) {
2013-09-24 16:54:33 +02:00
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' ] ) {
2013-09-24 17:38:38 +02:00
c . params [ 'mail' ] = c . params [ 'email' ] + c . params [ 'domain' ] ;
2013-09-24 16:54:33 +02:00
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 ) {
2013-09-24 16:54:33 +02:00
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 ) {
2013-09-24 16:54:33 +02:00
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 {
2013-09-24 16:54:33 +02:00
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' ] + ' ?' ) ) {
2013-09-24 16:54:33 +02:00
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' ] ) ;
}
} ) ;
2013-09-24 16:54:33 +02:00
/ * *
* 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 ) {
c . api ( '/app/list' , function ( data ) { // http://api.yunohost.org/#!/app/app_list_get_8
// Keep only installed apps
data2 = { 'Apps' : [ ] , 'Installed' : true }
$ . each ( data [ 'Apps' ] , function ( k , v ) {
if ( v [ 'Installed' ] !== 'No' ) data2 [ 'Apps' ] . push ( v ) ;
} ) ;
c . view ( 'app_list' , data2 ) ;
} ) ;
} ) ;
sam . get ( '#/apps/install' , function ( c ) {
c . api ( '/app/list' , function ( data ) { // http://api.yunohost.org/#!/app/app_list_get_8
// Keep only uninstalled apps
data2 = { 'Apps' : [ ] }
$ . each ( data [ 'Apps' ] , function ( k , v ) {
if ( v [ 'Installed' ] !== 'Yes' ) data2 [ 'Apps' ] . push ( v ) ;
} ) ;
c . view ( 'app_list' , data2 ) ;
} ) ;
} ) ;
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-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
} ) ;