]> git.sur5r.net Git - bacula/bacula/blob - gui/baculum/protected/Web/JavaScript/misc.js
baculum: Enlarge interface height to 100%
[bacula/bacula] / gui / baculum / protected / Web / JavaScript / misc.js
1 var Units = {
2         formats: [
3                 {format: 'second', value: 1},
4                 {format: 'minute', value: 60},
5                 {format: 'hour', value: 60},
6                 {format: 'day', value: 24}
7         ],
8         units: ['K', 'M', 'G', 'T', 'P'],
9         get_decimal_size: function(size) {
10                 var dec_size;
11                 var size_unit = 'B';
12                 var units = Units.units.slice(0);
13
14                 if (size === null) {
15                         size = 0;
16                 }
17
18                 var size_pattern = new RegExp('^[\\d\\.]+(' + units.join('|') + ')?' + size_unit + '$');
19
20                 if (size_pattern.test(size.toString())) {
21                         // size is already formatted
22                         dec_size = size;
23                 } else {
24                         size = parseInt(size, 10);
25                         var unit;
26                         dec_size = size.toString() + ((size > 0 ) ? size_unit : '');
27                         while(size >= 1000) {
28                                 size /= 1000;
29                                 unit = units.shift(units);
30                         }
31                         if (unit) {
32                                 dec_size = (Math.floor(size * 10) / 10).toFixed(1);
33                                 dec_size += unit + size_unit;
34                         }
35                 }
36                 return dec_size;
37         },
38         format_time_period: function(time_seconds, format) {
39                 var reminder;
40                 var f;
41                 for (var i = 0; i < this.formats.length; i++) {
42                         if (this.formats[i].format != format) {
43                                 reminder = time_seconds % this.formats[i].value;
44                                 if (reminder === 0) {
45                                         time_seconds /= this.formats[i].value;
46                                         f = this.formats[i].format;
47                                         continue;
48                                 }
49                                 break;
50                         }
51                 }
52                 var ret = {value: time_seconds, format: f};
53                 return ret;
54         }
55 }
56
57 var Strings = {
58         limits: {
59                 label: 15
60         },
61         get_short_label: function(txt) {
62                 var short_txt = txt;
63                 var cut = ((this.limits.label - 2) / 2);
64                 if (txt.length > this.limits.label) {
65                         short_txt = txt.substr(0, cut) + '..' + txt.substr(-cut);
66                 }
67                 return short_txt;
68         }
69 }
70
71 var PieGraph  = {
72         pie_label_formatter: function (total, value) {
73                 var percents =  (100 * value / total).toFixed(1);
74                 if (percents >= 1) {
75                         percents = percents.toString() + '%';
76                 } else {
77                         percents = '';
78                 }
79                 return percents;
80         },
81         pie_track_formatter: function(e) {
82                 return e.series.label;
83         }
84 }
85
86 var Formatters = {
87         formatter: [
88                 {css_class: 'size', format_func: Units.get_decimal_size}
89         ],
90         set_formatters: function() {
91                 var elements, formatter, txt;
92                 for (var i = 0; i < this.formatter.length; i++) {
93                         elements = document.getElementsByClassName(this.formatter[i].css_class);
94                         formatter = this.formatter[i].format_func;
95                         for (var i = 0; i < elements.length; i++) {
96                                 txt = elements[i].firstChild;
97                                 if (txt && txt.nodeType === 3) {
98                                         txt.nodeValue = formatter(txt.nodeValue);
99                                 }
100                         }
101                 }
102         }
103 }
104
105 var Cookies = {
106         default_exipration_time: 31536000000, // 1 year in miliseconds
107         set_cookie: function(name, value, expiration) {
108                 var date = new Date();
109                 date.setTime(date.getTime() + this.default_exipration_time);
110                 var expires = 'expires=' + date.toUTCString();
111                 document.cookie = name + '=' + value + '; ' + expires;
112         },
113         get_cookie: function(name) {
114                 name += '=';
115                 var values = document.cookie.split(';');
116                 var cookie_val = null;
117                 var value;
118                 for (var i = 0; i < values.length; i++) {
119                         value = values[i];
120                         while (value.charAt(0) == ' ') {
121                                 value = value.substr(1);
122                         }
123                         if (value.indexOf(name) == 0) {
124                                 cookie_val = value.substring(name.length, value.length);
125                                 break;
126                         }
127                 }
128                 return cookie_val;
129         }
130 }
131
132 var Dashboard = {
133         stats: null,
134         txt: null,
135         pie: null,
136         noval: '-',
137         ids: {
138                 clients: {
139                         no: 'clients_no',
140                         most: 'clients_most',
141                         jobs: 'clients_jobs'
142                 },
143                 jobs: {
144                         to_view: 'jobs_to_view',
145                         most: 'jobs_most',
146                         most_count: 'jobs_most_count'
147                 },
148                 jobtotals: {
149                         total_bytes: 'jobs_total_bytes',
150                         total_files: 'jobs_total_files'
151                 },
152                 database: {
153                         type: 'database_type',
154                         size: 'database_size'
155                 },
156                 pools: {
157                         no: 'pools_no',
158                         most: 'pools_most',
159                         jobs: 'pools_jobs'
160                 },
161                 pie_summary: 'jobs_summary_graph'
162         },
163         dbtype: {
164                 pgsql: 'PostgreSQL',
165                 mysql: 'MySQL',
166                 sqlite: 'SQLite'
167         },
168         update_all: function(statistics, txt) {
169                 this.stats = statistics;
170                 this.txt = txt;
171                 this.update_pie_jobstatus();
172                 this.update_clients();
173                 this.update_job_access();
174                 this.update_jobs();
175                 this.update_jobtotals();
176                 this.update_database();
177                 this.update_pools();
178         },
179         update_clients: function() {
180                 var clients = this.stats.clients_occupancy;
181                 var most_occuped_client = this.noval;
182                 var occupancy = -1;
183                 for (client in clients) {
184                         if (occupancy < clients[client]) {
185                                 most_occuped_client = client;
186                                 occupancy = clients[client];
187                         }
188                 }
189
190                 if (occupancy === -1) {
191                         occupancy = 0;
192                 }
193
194                 document.getElementById(this.ids.clients.no).textContent = Object.keys(this.stats.clients).length;
195                 document.getElementById(this.ids.clients.most).setAttribute('title', most_occuped_client);
196                 document.getElementById(this.ids.clients.most).textContent = Strings.get_short_label(most_occuped_client);
197                 document.getElementById(this.ids.clients.jobs).textContent = occupancy;
198         },
199         update_job_access: function() {
200                 var jobs_combobox= document.getElementById(this.ids.jobs.to_view);
201                 jobs_combobox.innerHTML = '';
202                 var last_jobs = this.stats.jobs.slice(0, 100);
203                 for (var i = 0; i < last_jobs.length; i++) {
204                         var opt = document.createElement('OPTION');
205                         var txt = '[' + last_jobs[i].jobid + '] ' + last_jobs[i].name + ' (' + this.txt.level + ': ' + last_jobs[i].level + ' ' + this.txt.status + ': ' + last_jobs[i].jobstatus + ' ' + this.txt.starttime + ': ' + last_jobs[i].starttime + ')';
206                         var label = document.createTextNode(txt);
207                         opt.value = last_jobs[i].jobid;
208                         opt.appendChild(label);
209                         jobs_combobox.appendChild(opt);
210                 }
211         },
212         update_jobs: function() {
213                 var jobs = this.stats.jobs_occupancy;
214                 var most_occuped_job = this.noval;
215                 var occupancy = -1;
216                 for (job in jobs) {
217                         if (occupancy < jobs[job]) {
218                                 most_occuped_job = job;
219                                 occupancy = jobs[job];
220                         }
221                 }
222
223                 if (occupancy === -1) {
224                         occupancy = 0;
225                 }
226
227                 document.getElementById(this.ids.jobs.most).setAttribute('title',most_occuped_job);
228                 document.getElementById(this.ids.jobs.most).textContent = Strings.get_short_label(most_occuped_job);
229                 document.getElementById(this.ids.jobs.most_count).textContent = occupancy;
230         },
231         update_jobtotals: function() {
232                 document.getElementById(this.ids.jobtotals.total_bytes).textContent = Units.get_decimal_size(this.stats.jobtotals.bytes);
233                 document.getElementById(this.ids.jobtotals.total_files).textContent = this.stats.jobtotals.files || 0;
234         },
235         update_database: function() {
236                 if (this.stats.dbsize.dbsize) {
237                         document.getElementById(this.ids.database.type).textContent = this.dbtype[this.stats.dbsize.dbtype];
238                         document.getElementById(this.ids.database.size).textContent = Units.get_decimal_size(this.stats.dbsize.dbsize);
239                 }
240         },
241         update_pools: function() {
242                 var pools = this.stats.pools_occupancy;
243                 var most_occuped_pool = this.noval;
244                 var occupancy = -1;
245                 for (pool in pools) {
246                         if (occupancy < pools[pool]) {
247                                 most_occuped_pool = pool;
248                                 occupancy = pools[pool];
249                         }
250                 }
251
252                 if (occupancy === -1) {
253                         occupancy = 0;
254                 }
255
256                 document.getElementById(this.ids.pools.no).textContent = Object.keys(this.stats.pools).length;
257                 document.getElementById(this.ids.pools.most).setAttribute('title', most_occuped_pool);
258                 document.getElementById(this.ids.pools.most).textContent = Strings.get_short_label(most_occuped_pool);
259                 document.getElementById(this.ids.pools.jobs).textContent = occupancy;
260         },
261         update_pie_jobstatus: function() {
262                 if (PanelWindow.currentWindowId === 'dashboard') {
263                         if (this.pie != null) {
264                                 this.pie.pie.destroy();
265                         }
266                         this.pie = new GraphPieClass(this.stats.jobs_summary, this.ids.pie_summary);
267                 }
268         }
269 }
270
271 var Users = {
272         ids: {
273                 create_user: {
274                         add_user: 'add_user',
275                         add_user_btn: 'add_user_btn',
276                         newuser: 'newuser',
277                         newpwd: 'newpwd'
278                 },
279                 change_pwd: {
280                         rel_chpwd: 'chpwd',
281                         rel_chpwd_btn: 'chpwd_btn'
282                 },
283                 set_host: {
284                         rel_user_host: 'user_host_img'
285                 }
286         },
287         validators: {
288                 user_pattern: null
289         },
290         current_action: null,
291         init: function() {
292                 this.setEvents();
293         },
294         setEvents: function() {
295                 document.getElementById(this.ids.create_user.add_user_btn).addEventListener('click', function(e) {
296                         $('#' + this.ids.create_user.add_user).show();
297                         $('#' + this.ids.create_user.newuser).focus();
298                 }.bind(this));
299                 document.getElementById(this.ids.create_user.newuser).addEventListener('keydown', function(e) {
300                         var target = e.target || e.srcElement;
301                         if (e.keyCode == 13) {
302                                 $(target.parentNode.getElementsByTagName('A')[0]).click();
303                         } else if (e.keyCode == 27) {
304                                 this.cancelAddUser();
305                         }
306                         return false;
307                 }.bind(this));
308                 document.getElementById(this.ids.create_user.newpwd).addEventListener('keydown', function(e) {
309                         var target = e.target || e.srcElement;
310                         if (e.keyCode == 13) {
311                                 $(target.nextElementSibling).click();
312                         } else if (e.keyCode == 27) {
313                                 this.cancelAddUser();
314                         }
315                         return false;
316                 }.bind(this));
317         },
318         userValidator: function(user) {
319                 user = user.replace(/\s/g, '');
320                 if (user == '') {
321                         alert(this.txt.enter_login);
322                         return false;
323                 }
324                 var valid = this.validators.user_pattern.test(user);
325                 if (valid === false) {
326                         alert(this.txt.invalid_login);
327                         return false;
328                 }
329                 return true;
330         },
331         pwdValidator: function(pwd) {
332                 var valid = pwd.length > 4;
333                 if (valid === false) {
334                         alert(this.txt.invalid_pwd);
335                 }
336                 return valid;
337         },
338         addUser: function() {
339                 var user = document.getElementById(this.ids.create_user.newuser).value;
340                 var pwd = document.getElementById(this.ids.create_user.newpwd).value;
341                 if (this.userValidator(user) === false) {
342                         return false;
343                 }
344                 if (this.pwdValidator(pwd) === false) {
345                         return false;
346                 }
347
348                 $('#' + this.ids.create_user.add_user).hide();
349                 this.action_callback('newuser', user, pwd);
350                 return true;
351         },
352         rmUser: function(user) {
353                 this.action_callback('rmuser', user);
354         },
355         showChangePwd: function(el) {
356                 $('a[rel=\'' + this.ids.change_pwd.rel_chpwd_btn + '\']').show();
357                 $('#' + el).hide();
358                 $('span[rel=\'' + this.ids.change_pwd.rel_chpwd + '\']').hide();
359                 $(el.nextElementSibling).show();
360                 $(el.nextElementSibling).select('input')[0].focus();
361         },
362         changePwd: function(el, user) {
363                 var pwd = el.value;
364
365                 if (this.pwdValidator(pwd) === false) {
366                         return false;
367                 }
368
369                 $(el.parentNode).hide();
370                 $(el.parentNode.previousElementSibling).show();
371                 this.action_callback('chpwd', user, pwd);
372                 return true;
373         },
374         set_host: function(user, select) {
375                 select.nextElementSibling.style.visibility = 'visible';
376                 this.action_callback('set_host', user, select.value);
377         },
378         hide_loader: function() {
379                 if (this.current_action === 'set_host') {
380                         $('img[rel=\'' + this.ids.set_host.rel_user_host + '\']').css({visibility: 'hidden'});
381                 }
382
383         },
384         cancelAddUser: function() {
385                 $('#' + this.ids.create_user.add_user).hide();
386         },
387         cancelChangePwd: function(el) {
388                 $(el.parentNode).hide();
389                 $(el.parentNode.previousElementSibling).show();
390         }
391
392 };
393
394 function openElementOnCursor(e, element, offsetX, offsetY) {
395         if (!offsetX) {
396                 offsetX = 0;
397         }
398         if (!offsetY) {
399                 offsetY = 0;
400         }
401         var x = (e.clientX + offsetX).toString();
402         var y = (e.clientY + offsetY + window.pageYOffset).toString();
403         $('#' + element).css({
404                 position: 'absolute',
405                 left: x + 'px',
406                 top: y + 'px',
407                 zIndex: 1000
408         });
409         $('#' + element).show();
410 }
411
412
413 function get_random_string(allowed, len) {
414         var random_string = "";
415         for(var i = 0; i < len; i++) {
416                 random_string += allowed.charAt(Math.floor(Math.random() * allowed.length));
417         }
418         return random_string;
419 }