-
Notifications
You must be signed in to change notification settings - Fork 82
/
init.js
146 lines (131 loc) · 4.23 KB
/
init.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
/**
* Initialization and configuration of frontend
*
* @author Florian Ziegler <fz@f10-home.de>
* @author Justin Otherguy <justin@justinotherguy.org>
* @author Steffen Vogel <info@steffenvogel.de>
* @copyright Copyright (c) 2011, The volkszaehler.org project
* @package default
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*/
/*
* This file is part of volkzaehler.org
*
* volkzaehler.org is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* volkzaehler.org is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with volkszaehler.org. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* volkszaehler.org namespace
*
* holds all data, options and functions for the frontend
* we dont want to pollute the global namespace
*/
var vz = {
entities: [], // entity properties + data
middleware: [], // array of all known middlewares
wui: { // web user interface
dialogs: { },
timeout: null
},
capabilities: { // debugging and runtime information from middleware
definitions: {} // definitions of entities & properties
},
plot: { }, // flot instance
options: { } // options loaded from cookies in options.js
};
/**
* Executed on document loaded complete
* this is where it all starts...
*/
$(document).ready(function() {
// late binding
$(window).resize(function() {
vz.options.tuples = Math.round($('#flot').width() / 3);
if (vz && vz.plot) {
if (vz.plot.resize)
vz.plot.resize();
if (vz.plot.setupGrid)
vz.plot.setupGrid();
if (vz.plot.draw)
vz.plot.draw();
}
});
window.onerror = function(errorMsg, url, lineNumber) {
vz.wui.dialogs.error('Javascript Runtime Error', errorMsg);
};
// add timezone-js support
if (typeof timezoneJS != "undefined" && typeof timezoneJS.Date != "undefined") {
timezoneJS.timezone.zoneFileBasePath = "tz";
timezoneJS.timezone.defaultZoneFile = [];
timezoneJS.timezone.init({ async: false });
}
// initialize variables
vz.middleware.push({ // default middleware
url: vz.options.localMiddleware,
title: 'Local (default)',
public: [ ] // public entities
/* capabilities: { } */
});
// remote middleware(s)
vz.options.remoteMiddleware.forEach(function(middleware) {
vz.middleware.push({
url: middleware.url,
title: middleware.title,
public: [ ] // public entities
/* capabilities: { } */
});
});
// TODO make language/translation dependent (vz.options.language)
vz.options.plot.xaxis.monthNames = vz.options.monthNames;
vz.options.plot.xaxis.dayNames = vz.options.dayNames;
// clear cookies and localStorage cache
var params = $.getUrlParams();
if (params.hasOwnProperty('reset') && params.reset) {
$.setCookie('vz_entities', null);
try {
localStorage.removeItem('vz.capabilities');
}
catch (e) { }
}
// start loading cookies/url params
vz.entities.loadCookie(); // load uuids from cookie
vz.options.loadCookies(); // load options from cookie
// set x axis limits _after_ loading options cookie
vz.options.plot.xaxis.max = new Date().getTime();
vz.options.plot.xaxis.min = vz.options.plot.xaxis.max - vz.options.interval;
// parse additional url params (new uuid etc e.g. for permalink) after loading defaults
vz.parseUrlParams();
// initialize user interface
vz.wui.init();
vz.wui.initEvents();
// chaining ajax request with jquery deferred object
vz.capabilities.load().done(function() {
if (vz.capabilities.formats.contains('png')) {
$('#export option[value=png]').removeAttr('disabled');
}
vz.entities.loadDetails().done(function() {
if (vz.entities.length === 0) {
vz.wui.dialogs.init();
}
vz.entities.showTable();
vz.entities.loadData().done(vz.wui.drawPlot);
/*
// alternative approach to parallelize js and json loading
$.when.apply($, [
$.cachedScript('jquery.flot.min.js'),
vz.entities.loadData()
]).done(vz.wui.drawPlot);
*/
});
});
});