-
Notifications
You must be signed in to change notification settings - Fork 69
/
main.js
153 lines (128 loc) · 4.84 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
(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';
});
Handlebars.registerHelper('timestampToDate', function(timestamp) {
var date = new Date(timestamp * 1000);
return date.toLocaleString();
});
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);
});
// 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>');
});
// Look for supported type of storage to use
/**
* http://sammyjs.org/docs/api/0.7.4/all#Sammy.Store.LocalStorage
* LocalStorage is our favorite, as it allows multiple tabs
*/
var storageType;
if (Sammy.Store.isAvailable('local')) {
storageType = 'local';
} else if (Sammy.Store.isAvailable('session')) {
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;
/**
* Application bootstrap
*
*/
sam.bind('run', function () {
// Store url
sam.store.set('url', window.location.hostname + '/yunohost/api');
// Get YunoHost version
if (sam.store.get('connected')) {
this.api('/version', function(versions) {
$('#yunohost-version').html(y18n.t('footer_version', [versions.yunohost.version, versions.yunohost.repo]));
});
}
// 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');
});
});
/**
* 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('#/');
});
})();