added options reset and local storage to remember them forever
Costa Tsaousis (ktsaou) committed
Dec 23, 2015 at 00:10 UTC
71150dcb03076b6eced761cd9e9cbd924ea15026
3 files changed
+158
-18
LICENSE.md
+6
@@ -67,6 +67,12 @@ connectivity is not available.
67
[MIT License](https://github.com/marcj/css-element-queries/blob/master/LICENSE)
68
69
70
+- [AVL C Library](http://freecode.com/projects/avl)
71
+
72
+ Copyright 2000, Daniel Nagy, Budapest University of Technology and Economics
73
+ Released under GNU General Public License (GPL) version 2
74
+
75
+
76
- [morris.js](http://morrisjs.github.io/morris.js/)
77
78
Copyright 2013, Olly Smith
web/dashboard.js
+118
-1
@@ -207,7 +207,9 @@
207
line: 1.0,
208
area: 0.2,
209
stacked: 0.8
210
- }
210
+ },
211
+
212
+ set_option_callback: function() { ; }
213
},
214
215
debug: {
@@ -224,6 +226,112 @@
226
}
227
}
228
229
+
230
+ // ----------------------------------------------------------------------------------------------------------------
231
+ // local storage options
232
+
233
+ NETDATA.objectToLocalStorage = function(obj, prefix) {
234
+ if(typeof Storage !== "undefined" && typeof localStorage === 'object') {
235
+ for(var i in obj) {
236
+ if(typeof obj[i] === 'object') {
237
+ //console.log('object ' + prefix + '.' + i.toString());
238
+ NETDATA.objectToLocalStorage(obj[i], prefix + '.' + i.toString());
239
+ continue;
240
+ }
241
+
242
+ var value = localStorage.getItem(prefix + '.' + i.toString());
243
+ if(value === null) {
244
+ switch(typeof obj[i]) {
245
+ case 'boolean':
246
+ case 'number':
247
+ //console.log('write ' + prefix + '.' + i.toString() + ' = ' + obj[i].toString());
248
+ localStorage.setItem(prefix + '.' + i.toString(), obj[i].toString())
249
+ break;
250
+ }
251
+ continue;
252
+ }
253
+
254
+ switch(typeof obj[i]) {
255
+ case 'boolean':
256
+ if(value === 'true' || value === 'false') {
257
+ obj[i] = (value === 'false')?false:true;
258
+ //console.log('read ' + prefix + '.' + i.toString() + ' = ' + obj[i].toString());
259
+ }
260
+ //else console.log('ignoring ' + i.toString());
261
+ break;
262
+
263
+ case 'number':
264
+ var n = parseInt(value);
265
+ if(isNaN(n) === false) {
266
+ obj[i] = n;
267
+ // console.log('read ' + prefix + '.' + i.toString() + ' = ' + obj[i].toString());
268
+ }
269
+ // else console.log('ignoring ' + i.toString());
270
+ break;
271
+ }
272
+ }
273
+ }
274
+ }
275
+
276
+ NETDATA.setOption = function(key, value) {
277
+ var ret = false;
278
+
279
+ switch(typeof NETDATA.options.current[key]) {
280
+ case 'boolean':
281
+ if(value) NETDATA.options.current[key] = true;
282
+ else NETDATA.options.current[key] = false;
283
+ localStorage.setItem('options.' + key.toString(), NETDATA.options.current[key].toString());
284
+ ret = true;
285
+ break;
286
+
287
+ case 'number':
288
+ if(typeof value === 'number') {
289
+ NETDATA.options.current[key] = value;
290
+ localStorage.setItem('options.' + key.toString(), NETDATA.options.current[key].toString());
291
+ ret = true;
292
+ }
293
+ //else
294
+ // console.log('invalid number given for option key ' + key.toString);
295
+ break;
296
+
297
+ case 'function':
298
+ if(typeof value === 'function') {
299
+ NETDATA.options.current[key] = value;
300
+ ret = true;
301
+ }
302
+ //else
303
+ // console.log('invalid function given for option key ' + key.toString);
304
+ break;
305
+
306
+ default:
307
+ // console.log('cannot find option key ' + key.toString());
308
+ break;
309
+ }
310
+
311
+ if(ret === true)
312
+ NETDATA.options.current.set_option_callback();
313
+
314
+ return ret;
315
+ }
316
+
317
+ NETDATA.getOption = function(key) {
318
+ return NETDATA.options.current[key];
319
+ }
320
+
321
+ NETDATA.resetOptions = function() {
322
+ // console.log('will reset all');
323
+ for(var i in NETDATA.options.defaults) {
324
+ if(i.toString() === 'set_option_callback') continue;
325
+
326
+ if(NETDATA.options.current[i] !== NETDATA.options.defaults[i]) {
327
+ // console.log('reseting ' + i.toString() + ' to ' + NETDATA.options.defaults[i].toString());
328
+ NETDATA.setOption(i, NETDATA.options.defaults[i]);
329
+ }
330
+ }
331
+ }
332
+
333
+ // ----------------------------------------------------------------------------------------------------------------
334
+
335
if(NETDATA.options.debug.main_loop === true)
336
console.log('welcome to NETDATA');
337
@@ -3709,6 +3817,15 @@
3817
NETDATA.loadRequiredJs(0, function() {
3818
ElementQueries.init();
3819
3820
+ // keep a copy of the default settings
3821
+ NETDATA.options.defaults = $.extend({}, NETDATA.options.current);
3822
+
3823
+ // read settings from local storage
3824
+ NETDATA.objectToLocalStorage(NETDATA.options.current, 'options');
3825
+
3826
+ // always start with this option enabled.
3827
+ NETDATA.setOption('stop_updates_when_focus_is_lost', true);
3828
+
3829
if(typeof netdataDontStart === 'undefined' || !netdataDontStart) {
3830
if(NETDATA.options.debug.main_loop === true)
3831
console.log('starting chart refresh thread');
web/index.html
+34
-17
@@ -418,6 +418,12 @@
418
<div class="modal-body">
419
<form id="optionsForm" method="get" class="form-horizontal">
420
<div class="form-group">
421
+ <center>
422
+ <small style="color: #BBBBBB;">Settings take effect immediately and are saved permanently to browser local storage (except the refresh on focus / always option).
423
+ <br/>
424
+ To reset all options to their defaults, click <a href="#" onclick="NETDATA.resetOptions(); return false;">here</a>.</small>
425
+ </center>
426
+
427
<table>
428
<tr class="option-row">
429
<td class="option-control"><input id="stop_updates_when_focus_is_lost" type="checkbox" checked data-toggle="toggle" data-offstyle="danger" data-onstyle="success" data-on="On Focus" data-off="Always" data-width="110px"></td>
@@ -989,33 +995,44 @@ function prepareScreen(data) {
995
996
document.getElementById('footer').style.display = 'block';
997
992
- // default options
993
- $('#eliminate_zero_dimensions').bootstrapToggle(NETDATA.options.current.eliminate_zero_dimensions?'on':'off');
994
- $('#destroy_on_hide').bootstrapToggle(NETDATA.options.current.destroy_on_hide?'on':'off');
995
- $('#parallel_refresher').bootstrapToggle(NETDATA.options.current.parallel_refresher?'on':'off');
996
- $('#concurrent_refreshes').bootstrapToggle(NETDATA.options.current.concurrent_refreshes?'on':'off');
997
- $('#sync_selection').bootstrapToggle(NETDATA.options.current.sync_selection?'on':'off');
998
- $('#sync_pan_and_zoom').bootstrapToggle(NETDATA.options.current.sync_pan_and_zoom?'on':'off');
999
- $('#stop_updates_when_focus_is_lost').bootstrapToggle(NETDATA.options.current.stop_updates_when_focus_is_lost?'on':'off');
998
999
var update_options_visibility = function() {
1002
- if(NETDATA.options.current.parallel_refresher === false) {
1000
+ // console.log('update_options_visibility');
1001
+
1002
+ var sync_option = function(option) {
1003
+ var self = $('#' + option);
1004
+
1005
+ if(self.prop('checked') !== NETDATA.getOption(option)) {
1006
+ // console.log('switching ' + option.toString());
1007
+ self.bootstrapToggle(NETDATA.getOption(option)?'on':'off');
1008
+ }
1009
+ }
1010
+
1011
+ sync_option('eliminate_zero_dimensions');
1012
+ sync_option('destroy_on_hide');
1013
+ sync_option('parallel_refresher');
1014
+ sync_option('concurrent_refreshes');
1015
+ sync_option('sync_selection');
1016
+ sync_option('sync_pan_and_zoom');
1017
+ sync_option('stop_updates_when_focus_is_lost');
1018
+
1019
+ if(NETDATA.getOption('parallel_refresher') === false) {
1020
$('#concurrent_refreshes_row').hide();
1021
}
1022
else {
1023
$('#concurrent_refreshes_row').show();
1024
}
1025
};
1009
- update_options_visibility();
1026
+ NETDATA.setOption('set_option_callback', update_options_visibility);
1027
1028
// handle options changes
1012
- $('#eliminate_zero_dimensions').change(function() { NETDATA.options.current.eliminate_zero_dimensions = $(this).prop('checked'); update_options_visibility(); });
1013
- $('#destroy_on_hide').change(function() { NETDATA.options.current.destroy_on_hide = $(this).prop('checked'); update_options_visibility(); });
1014
- $('#parallel_refresher').change(function() { NETDATA.options.current.parallel_refresher = $(this).prop('checked'); update_options_visibility(); });
1015
- $('#concurrent_refreshes').change(function() { NETDATA.options.current.concurrent_refreshes = $(this).prop('checked'); update_options_visibility(); });
1016
- $('#sync_selection').change(function() { NETDATA.options.current.sync_selection = $(this).prop('checked'); update_options_visibility(); });
1017
- $('#sync_pan_and_zoom').change(function() { NETDATA.options.current.sync_pan_and_zoom = $(this).prop('checked'); update_options_visibility(); });
1018
- $('#stop_updates_when_focus_is_lost').change(function() { NETDATA.options.current.stop_updates_when_focus_is_lost = $(this).prop('checked'); update_options_visibility(); });
1029
+ $('#eliminate_zero_dimensions').change(function() { NETDATA.setOption('eliminate_zero_dimensions', $(this).prop('checked')); });
1030
+ $('#destroy_on_hide').change(function() { NETDATA.setOption('destroy_on_hide', $(this).prop('checked')); });
1031
+ $('#parallel_refresher').change(function() { NETDATA.setOption('parallel_refresher', $(this).prop('checked')); });
1032
+ $('#concurrent_refreshes').change(function() { NETDATA.setOption('concurrent_refreshes', $(this).prop('checked')); });
1033
+ $('#sync_selection').change(function() { NETDATA.setOption('sync_selection', $(this).prop('checked')); });
1034
+ $('#sync_pan_and_zoom').change(function() { NETDATA.setOption('sync_pan_and_zoom', $(this).prop('checked')); });
1035
+ $('#stop_updates_when_focus_is_lost').change(function() { NETDATA.setOption('stop_updates_when_focus_is_lost', $(this).prop('checked')); });
1036
1037
if(document.location.hostname === demo_hostname)
1038
setTimeout(function() {