2015-11-24 08:36:36 +01:00
|
|
|
(function() {
|
|
|
|
|
|
|
|
var app = Sammy('#main', function (sam) {
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sammy Configuration
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Plugins
|
|
|
|
sam.use('Handlebars', 'ms');
|
|
|
|
|
|
|
|
Handlebars.registerHelper('ucwords', function(str) {
|
|
|
|
return (str + '').replace(/^([a-z\u00E0-\u00FC])|\s+([a-z\u00E0-\u00FC])/g, function ($1) {
|
|
|
|
return $1.toUpperCase();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
Handlebars.registerHelper('humanSize', function(bytes) {
|
|
|
|
var sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
|
|
|
|
if (bytes === 0) return 'n/a';
|
|
|
|
var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)));
|
|
|
|
return Math.round(bytes / Math.pow(1024, i), 2) + ' ' + sizes[[i]];
|
|
|
|
});
|
|
|
|
Handlebars.registerHelper('humanTime', function(time) {
|
|
|
|
return Math.round(time) + 's';
|
|
|
|
});
|
2017-06-12 16:57:52 +02:00
|
|
|
Handlebars.registerHelper('timestampToDate', function(timestamp) {
|
|
|
|
var date = new Date(timestamp * 1000);
|
|
|
|
return date.toLocaleString();
|
|
|
|
});
|
2015-11-24 08:36:36 +01:00
|
|
|
Handlebars.registerHelper('bitRate', function(bytes, time) {
|
|
|
|
var sizes = ['b', 'Kb', 'Mb', 'Gb', 'Tb'];
|
|
|
|
if (time === 0) return 'n/a';
|
|
|
|
var bps = bytes / time * 8;
|
|
|
|
var i = parseInt(Math.floor(Math.log(bps) / Math.log(1024)));
|
|
|
|
return Math.round(bps / Math.pow(1024, i), 2) + ' ' + sizes[[i]] + '/s';
|
|
|
|
});
|
|
|
|
|
|
|
|
Handlebars.registerHelper('t', function(y18n_key) {
|
|
|
|
var result = y18n.t(y18n_key, Array.prototype.slice.call(arguments, 1));
|
|
|
|
return new Handlebars.SafeString(result);
|
|
|
|
});
|
|
|
|
|
2018-01-12 08:11:27 +01:00
|
|
|
// Block helper to add a tooltip to any element
|
|
|
|
Handlebars.registerHelper('tooltip', function(tooltip, options) {
|
|
|
|
return new Handlebars.SafeString(
|
|
|
|
'<span data-toggle="tooltip" title="' + tooltip + '" data-placement="right">'
|
|
|
|
+ options.fn(this)
|
|
|
|
+ '</span>');
|
|
|
|
});
|
|
|
|
|
|
|
|
// Load tooltips on the page; needed if using tooltips
|
|
|
|
Handlebars.registerHelper('load_tooltips', function() {
|
|
|
|
return new Handlebars.SafeString(
|
|
|
|
'<script>'
|
|
|
|
+ '$(document).ready(function(){'
|
|
|
|
+ '$(\'[data-toggle="tooltip"]\').tooltip();'
|
|
|
|
+ '});'
|
|
|
|
+ '</script>');
|
|
|
|
});
|
2015-11-24 08:36:36 +01:00
|
|
|
|
|
|
|
// Look for supported type of storage to use
|
2016-02-04 12:06:34 +01:00
|
|
|
/**
|
|
|
|
* http://sammyjs.org/docs/api/0.7.4/all#Sammy.Store.LocalStorage
|
|
|
|
* LocalStorage is our favorite, as it allows multiple tabs
|
|
|
|
*/
|
2015-11-24 08:36:36 +01:00
|
|
|
var storageType;
|
2016-02-04 12:06:34 +01:00
|
|
|
if (Sammy.Store.isAvailable('local')) {
|
|
|
|
storageType = 'local';
|
|
|
|
} else if (Sammy.Store.isAvailable('session')) {
|
2015-11-24 08:36:36 +01:00
|
|
|
storageType = 'session';
|
|
|
|
} else if (Sammy.Store.isAvailable('cookie')) {
|
|
|
|
storageType = 'cookie';
|
|
|
|
} else {
|
|
|
|
storageType = 'memory';
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize storage
|
|
|
|
sam.store = new Sammy.Store({name: 'storage', type: storageType});
|
|
|
|
sam.loaded = false;
|
|
|
|
sam.isInstalledTry = 3;
|
|
|
|
|
|
|
|
|
2016-05-23 15:29:58 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Application bootstrap
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
sam.bind('run', function () {
|
|
|
|
// Store url
|
|
|
|
sam.store.set('url', window.location.hostname + '/yunohost/api');
|
|
|
|
|
2016-05-23 20:50:53 +02:00
|
|
|
// Get YunoHost version
|
2017-02-25 04:30:20 +01:00
|
|
|
if (sam.store.get('connected')) {
|
|
|
|
this.api('/version', function(versions) {
|
2018-01-08 04:11:01 +01:00
|
|
|
$('#yunohost-version').html(y18n.t('footer_version', [versions.yunohost.version, versions.yunohost.repo]));
|
2017-02-25 04:30:20 +01:00
|
|
|
});
|
|
|
|
}
|
2016-05-23 20:50:53 +02:00
|
|
|
|
2016-05-23 15:29:58 +02:00
|
|
|
// Flash messages
|
|
|
|
var flashMessage = $('#flashMessage');
|
|
|
|
$('#toggle-btn', flashMessage).click(function(e) {
|
|
|
|
flashMessage.toggleClass('open');
|
|
|
|
});
|
|
|
|
$('#clear-btn', flashMessage).click(function(e) {
|
|
|
|
flashMessage.removeClass('open').find('.messages').html('');
|
|
|
|
$('#slider').removeClass('with-flashMessage');
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2015-11-24 08:36:36 +01:00
|
|
|
/**
|
|
|
|
* Errors
|
|
|
|
*/
|
|
|
|
sam.notFound = function(){
|
|
|
|
// Redirect to home page on 404.
|
|
|
|
window.location = '#/';
|
|
|
|
};
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Translations
|
|
|
|
*/
|
|
|
|
$.getJSON('locales/en.json', function(data){
|
|
|
|
y18n.translations['en'] = data;
|
|
|
|
y18n.translateInlineHTML();
|
|
|
|
});
|
|
|
|
|
|
|
|
// User defined language
|
|
|
|
if (window.navigator && window.navigator.language) {
|
|
|
|
y18n.locale = window.navigator.language.substr(0, 2);
|
|
|
|
if (y18n.locale !== 'en') {
|
|
|
|
$.getJSON('locales/'+ y18n.locale +'.json', function(data){
|
|
|
|
y18n.translations[y18n.locale] = data;
|
|
|
|
y18n.translateInlineHTML();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Run the application
|
|
|
|
*/
|
|
|
|
$(document).ready(function () {
|
|
|
|
// Run Sammy.js application
|
|
|
|
app.run('#/');
|
|
|
|
});
|
|
|
|
|
2018-01-08 04:11:01 +01:00
|
|
|
})();
|