forked from tariqbuilds/linux-dash
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinuxDash.js
More file actions
526 lines (446 loc) · 13.5 KB
/
Copy pathlinuxDash.js
File metadata and controls
526 lines (446 loc) · 13.5 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
var linuxDash = angular.module('linuxDash', ['ngRoute']);
////////////////// Routing /////////////////////////////
linuxDash.config(['$routeProvider',
function($routeProvider) {
$routeProvider.
when('/basic-info', {
templateUrl: 'templates/sections/basic-info.html',
}).
when('/system-status', {
templateUrl: 'templates/sections/system-status.html',
}).
when('/network', {
templateUrl: 'templates/sections/network.html',
}).
when('/accounts', {
templateUrl: 'templates/sections/accounts.html',
}).
when('/apps', {
templateUrl: 'templates/sections/applications.html',
}).
otherwise({
redirectTo: '/basic-info'
});
}]);
////////////////// Global Application //////////////////
linuxDash.controller('body', function ($scope, server, $route, $location) {
});
/**
* Gets data from server and runs callbacks on response.data.data
*/
linuxDash.service('server',[ '$http', function ($http) {
this.get = function (moduleName, callback) {
return $http.get('module.php?module=' + moduleName).then(function (response) {
return callback(response.data.data);
});
};
}]);
/**
* Sidebar for SPA
*/
linuxDash.directive('navBar',function ($location) {
return {
restrict: 'E',
templateUrl: 'templates/app/navbar.html',
link: function (scope) {
scope.items = [
'basic-info',
'system-status',
'network',
'accounts',
'apps'
];
scope.getNavItemName = function (url) {
return url.replace('-', ' ');
};
scope.isActive = function(route) {
return '/' + route === $location.path();
};
}
};
});
////////////////// UI Element Directives //////////////////
/**
* Shows loader
*/
linuxDash.directive('loader', function() {
return {
restrict: 'E',
scope: {
width: '@'
},
template: '<div class="loader"></div>'
};
});
/**
* Top Bar for widget
*/
linuxDash.directive('topBar', function() {
return {
restrict: 'E',
scope: {
heading: '=',
refresh: '&',
lastUpdated: '='
},
templateUrl: 'templates/app/ui-elements/top-bar.html',
link: function(scope, element, attrs){
var $refreshBtn = element.find('refresh-btn').eq(0);
if (typeof attrs.noRefreshBtn !== 'undefined') {
$refreshBtn.remove();
}
}
};
});
/**
* Shows refresh button and calls
* provided expression on-click
*/
linuxDash.directive('refreshBtn', function() {
return {
restrict: 'E',
scope: {
refresh: '&'
},
template: '<button ng-click="refresh()">↺</button>'
};
});
/**
* Message shown when no data is found from server
*/
linuxDash.directive('noData', function() {
return {
restrict: 'E',
template: 'No Data'
};
});
/**
* Displays last updated timestamp for widget
*/
linuxDash.directive('lastUpdate', function() {
return {
restrict: 'E',
scope: {
timestamp: '='
},
templateUrl: 'templates/app/ui-elements/last-update.html'
};
});
////////////////// Plugin Directives //////////////////
///
/**
* Fetches and displays static data
*/
linuxDash.directive('staticDataPlugin', ['$timeout', 'server', function($timeout, server) {
return {
restrict: 'E',
scope: {
heading: '@',
staticData: '='
},
templateUrl: 'templates/app/static-data-plugin.html',
link: function (scope, element) {
scope.getData = function () {
// for each peice of static data to be shown
scope.staticData.forEach(function (staticObj) {
// get the resp from the server and assign it to obj.data
server.get(staticObj.module, function (serverResponseData) {
staticObj.info = serverResponseData;
});
});
scope.lastGet = new Date().getTime();
};
scope.getData();
}
};
}]);
/**
* Fetches and displays table data
*/
linuxDash.directive('tableData', [ 'server', function(server) {
return {
restrict: 'E',
scope: {
heading: '@',
moduleName: '@'
},
templateUrl: 'templates/app/table-data-plugin.html',
link: function (scope, element) {
scope.sortByColumn = null;
// set the column to sort by
scope.setSortColumn = function (column) {
// if the column is already being sorted
// reverse the order
if (column === scope.sortByColumn) {
scope.sortByColumn = '-' + column;
}
else {
scope.sortByColumn = column;
}
};
scope.getData = function () {
delete scope.tableRows;
server.get(scope.moduleName, function (serverResponseData) {
if (serverResponseData.length > 0) {
scope.tableHeaders = Object.keys(serverResponseData[0]);
}
scope.tableRows = serverResponseData;
scope.lastGet = new Date().getTime();
if(serverResponseData.length < 1) {
scope.emptyResult = true;
}
});
};
scope.getData();
}
};
}]);
/**
* Fetches and displays table data
*/
linuxDash.directive('keyValueList', ['server', function(server) {
return {
restrict: 'E',
scope: {
heading: '@',
moduleName: '@',
},
templateUrl: 'templates/app/key-value-list-plugin.html',
link: function (scope, element) {
scope.getData = function () {
delete scope.tableRows;
server.get(scope.moduleName, function (serverResponseData) {
scope.tableRows = serverResponseData;
scope.lastGet = new Date().getTime();
if(serverResponseData.length < 1) {
scope.emptyResult = true;
}
});
};
scope.getData();
}
};
}]);
/**
* Fetches and displays data as line chart at a certain refresh rate
*/
linuxDash.directive('lineChartPlugin', ['$interval', '$compile', 'server', function($interval, $compile, server) {
return {
restrict: 'E',
scope: {
heading: '@',
moduleName: '@',
refreshRate: '=',
maxValue: '=',
minValue: '=',
getDisplayValue: '=',
metrics: '='
},
templateUrl: 'templates/app/line-chart-plugin.html',
link: function (scope, element) {
// smoothieJS - Create new chart
var chart = new SmoothieChart({
borderVisible:false,
sharpLines:true,
grid: {
fillStyle:'#ffffff',
strokeStyle:'rgba(232,230,230,0.93)',
sharpLines:true,
millisPerLine:3000,
borderVisible:false
},
labels:{
fontSize:11,
precision:0,
fillStyle:'#0f0e0e'
},
maxValue: parseInt(scope.maxValue),
minValue: parseInt(scope.minValue),
horizontalLines: [{ value: 5, color: '#eff', lineWidth: 1 }]
});
// smoothieJS - set up canvas element for chart
canvas = element.find('canvas')[0],
series = new TimeSeries();
chart.addTimeSeries(series, { strokeStyle: 'rgba(0, 255, 0, 1)', fillStyle: 'rgba(0, 255, 0, 0.2)', lineWidth: 2 });
chart.streamTo(canvas, 1000);
// update data on chart
scope.getData = function () {
server.get(scope.moduleName, function (serverResponseData) {
scope.lastGet = new Date().getTime();
// update chart with this response
series.append(scope.lastGet, scope.getDisplayValue(serverResponseData));
// update the metrics for this chart
scope.metrics.forEach(function (metricObj) {
metricObj.data = metricObj.generate(serverResponseData) ;
});
});
};
// set the directive-provided interval
// at which to run the chart update
$interval(scope.getData, scope.refreshRate);
}
};
}]);
/**
* Fetches and displays data as line chart at a certain refresh rate
*
*/
linuxDash.directive('multiLineChartPlugin', ['$interval', '$compile', 'server', function($interval, $compile, server) {
return {
restrict: 'E',
scope: {
heading: '@',
moduleName: '@',
refreshRate: '=',
maxValue: '=',
minValue: '=',
getDisplayValue: '=',
metrics: '='
},
templateUrl: 'templates/app/multi-line-chart-plugin.html',
link: function (scope, element) {
// smoothieJS - Create new chart
var chart = new SmoothieChart({
borderVisible:false,
sharpLines:true,
grid: {
fillStyle:'#ffffff',
strokeStyle:'rgba(232,230,230,0.93)',
sharpLines:true,
borderVisible:false
},
labels:{
fontSize:12,
precision:0,
fillStyle:'#0f0e0e'
},
maxValue: parseInt(scope.maxValue),
minValue: parseInt(scope.minValue),
horizontalLines: [{ value: 1, color: '#ecc', lineWidth: 1 }]
});
var seriesOptions = [
{ strokeStyle: 'rgba(255, 0, 0, 1)', lineWidth: 2 },
{ strokeStyle: 'rgba(0, 255, 0, 1)', lineWidth: 2 },
{ strokeStyle: 'rgba(0, 0, 255, 1)', lineWidth: 2 },
{ strokeStyle: 'rgba(255, 255, 0, 1)', lineWidth: 1 }
];
// smoothieJS - set up canvas element for chart
var canvas = element.find('canvas')[0];
var seriesArray = [];
// get the data once to set up # of lines on chart
server.get(scope.moduleName, function (serverResponseData) {
serverResponseData.forEach(function (obj, key) {
seriesArray[key] = new TimeSeries();
chart.addTimeSeries(seriesArray[key], seriesOptions[key]);
scope.metrics[key].color = seriesOptions[key].strokeStyle;
});
});
chart.streamTo(canvas, 1000);
// update data on chart
scope.getData = function () {
server.get(scope.moduleName, function (serverResponseData) {
scope.lastGet = new Date().getTime();
// update chart with current response
serverResponseData.forEach(function (item, key) {
seriesArray[key].append(scope.lastGet, scope.getDisplayValue(serverResponseData[key]));
});
// update the metrics for this chart
scope.metrics.forEach(function (metricObj) {
metricObj.data = metricObj.generate(serverResponseData) ;
});
});
};
$interval(scope.getData, scope.refreshRate);
}
};
}]);
/**
* Base plugin structure
*/
linuxDash.directive('plugin', function() {
return {
restrict: 'E',
transclude: true,
// scope: {
// heading: '@',
// lastUpdated: '=',
// onRefresh: '&',
// },
templateUrl: 'templates/app/base-plugin.html'
}
});
/**
* Progress bar element
*/
linuxDash.directive('progressBarPlugin',function() {
return {
restrict: 'E',
scope: {
width: '@',
moduleName: '@',
name: '@',
value: '@',
max: '@'
},
templateUrl: 'templates/app/progress-bar-plugin.html'
};
});
/**
* Theme switcher
*/
linuxDash.directive('themeSwitcher',['$location', function($location) {
return {
restrict: 'E',
templateUrl: 'templates/app/theme-switcher.html',
link: function (scope) {
// alternate themes available
scope.themes = [
{
name: 'winter',
},
{
name: 'summer',
},
{
name: 'spring',
},
{
name: 'fall',
},
];
scope.themeSwitcherOpen = false;
scope.switchTheme = function (theme) {
if(theme.selected) {
scope.setDefaultTheme();
return;
}
scope.removeExistingThemes();
theme.selected = true;
document.getElementsByTagName('html')[0].className = theme.name;
localStorage.setItem('theme', theme.name);
};
scope.toggleThemeSwitcher = function () {
scope.themeSwitcherOpen = !scope.themeSwitcherOpen;
};
scope.removeExistingThemes = function () {
scope.themes.forEach(function (item) {
item.selected = false;
});
};
scope.setDefaultTheme = function () {
scope.removeExistingThemes();
document.getElementsByTagName('html')[0].className = '';
localStorage.setItem('theme', null);
};
// on load, check if theme was set in localStorage
if(localStorage.getItem('theme')) {
scope.themes.forEach(function (theme) {
if(theme.name === localStorage.getItem('theme'))
{
scope.switchTheme(theme);
}
});
}
}
};
}]);