-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodel.js
175 lines (150 loc) · 4.01 KB
/
model.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
* Kapselt den Zugriff auf die Immatrikulationsdaten und regelt die
* Kommuninkation mit dem Server.
*/
var App = App || {};
App.model = {
location : 2,
init : function() {
radio('filter.submit').subscribe(this.submitListener);
},
submitListener : function() {
App.model.fetch();
},
/*
* Enthaelt immer den aktuellen Datensatz fuer Highcharts
*/
data : {},
/*
* Enthaelt immer den aktuellen Datensatz fuer GoogleMaps
*/
data_gmaps : [],
/*
* Enthaelt immer den aktuellen Datensatz fuer GoogleGlobe
*/
data_globe : [],
/*
* Bereitet die Filter aus dem Formular fuer das Erstellen des
* Suchobjektes auf.
*/
prepareParameters : function(filter){
var parameters = {};
$.each(filter, function(index, value){
if ( !( value === null || value === undefined || value === '')) {
if (value instanceof Array) {
if ( (index != 'gender' || value.length != 2)) {
parameters[index] = value.join(', ');
}
} else {
parameters[index] = value;
}
}
});
var year = App.slider.getValue();
if (year !== 'All') {
if (year % 1 == 0.5) {
parameters.semester_of_matriculation = Math.floor(year)*10 + 1
} else {
parameters.semester_of_matriculation = Math.floor(year)*10 + 2
}
}
console.log(parameters);
return parameters;
},
/*
* Fuehrt eine neue Suche aus indem ein POST-Request
* zur Rails Anwendung geschickt wird
*/
post : function(filter) {
var parameters = this.prepareParameters(filter);
//Macht das Postrequest
$.ajax({
type : "POST",
url: "/searches.json" ,
async : false,
data : {
search : parameters
},
success : function(data, textStatus, request){
var response = request.getResponseHeader('Location');
var patt = /([0-9]+$)/;
App.model.location = patt.exec(response)[0];
}
});
},
//@todo hier weiter machen
limitateSeries : function(data){
if(data.categories.length > 20){
var newSeries = {
series : [],
categories : []
}
var summe = 0;
newSeries.categories = data.categories.slice(0,18);
newSeries.categories.push('Sonstige');
for(var e = 0; e < data.series.length; e++){
newSeries.series[e] = {
name : data.series[e].name,
data : data.series[e].data.slice(0,18)
};
}
for(var k = 0; k < data.series.length; k++){
for(var j = 19; j < data.series[0].data.length; j++){
summe += data.series[k].data[j];
}
newSeries.series[k].data.push(summe);
summe = 0;
}
return newSeries;
}
else{
return data;
}
},
/*
* Laedt einen neuen Datensatz mit den aktuellen Filtern vom Server.
*/
fetch : function() {
radio('model.fetch').broadcast();
this.post(App.filter.getFilter());
var url = 'searches/'+App.model.location+'.json?representation=highcharts';
var url_gmaps = 'searches/'+App.model.location+'.json?representation=maps';
var url_globe = 'searches/'+App.model.location+'.json?representation=globe';
$.getJSON(url, function(data) {
App.model.data = App.model.limitateSeries(data.data);
//App.model.data = data.data;
radio('model.hc.fetched').broadcast();
//App.model.limitateSeries();
}).fail(function() {
App.showAlert({
type: 'danger',
heading: 'Verbindungsfehler!',
message: 'Die Verbindung zum Server ist fehlgeschlagen!'
});
});
$.getJSON(url_gmaps, function(data) {
App.model.data_gmaps = data.data_gmaps;
radio('model.gmaps.fetched').broadcast();
}).fail(function() {
App.showAlert({
type: 'danger',
heading: 'Verbindungsfehler!',
message: 'Die Verbindung zum Server ist fehlgeschlagen!'
});
}).always(function() {
radio('model.fetched').broadcast();
});
$.getJSON(url_globe, function(data) {
App.model.data_globe = data.data;
radio('model.globe.fetched').broadcast();
}).fail(function() {
App.showAlert({
type: 'danger',
heading: 'Verbindungsfehler!',
message: 'Die Verbindung zum Server ist fehlgeschlagen!'
});
}).always(function() {
radio('model.fetched').broadcast();
});
}
};