Projekt

Obecné

Profil

Stáhnout (11.1 KB) Statistiky
| Větev: | Revize:
1
var app = angular.module('pvpk', ['ngRoute', 'ngResource', 'ngSanitize']);
2

    
3
app.constant('config', {
4
    APP_NAME: 'PVPK',
5
    APP_VERSION: 1.0,
6
    API_URL: API_URL,
7
    API_TOKEN: API_TOKEN,
8
    DEFAULT_POSITION: {LAT: 49.53, LNG: 13.3},
9
    DEFAULT_ZOOM: 10
10
});
11

    
12
//PRIPRAVA PRO REFAKTORING
13
// app.config(function($stateProvider, $locationProvider) {
14
//     // $stateProvider
15
//     // .state('report',{
16
//     //     views: {
17
//     //         'search': {
18
//     //             templateUrl: 'report-filters.html',
19
//     //             controller: searchController
20
//     //         },
21
//     //         'graph': {
22
//     //             templateUrl: 'report-table.html',
23
//     //             controller: graphController
24
//     //         },
25
//     //         'map': {
26
//     //             templateUrl: 'report-graph.html',
27
//     //             controller: mapController
28
//     //         }
29
//     //     }
30
//     // });
31
//    $locationProvider.html5Mode(true);
32
// });
33

    
34

    
35
app.controller('mainController', function ($rootScope, $scope, $http, $window) {
36

    
37
    this.$onInit = function () {
38
        $scope.showLoadingScreen = false;
39
    };
40

    
41
    $rootScope.handleErrorResponse = function (response) {
42
        switch (response.status) {
43
            case 400:
44
                console.log('API ERROR 400');
45
                $scope.modalError = {
46
                    title: 'Neplatný požadavek',
47
                    body: 'Požadavek nemůže být vyřízen, poněvadž byl syntakticky nesprávně zapsán.',
48
                    button: 'OK'
49
                };
50
                jQuery('#modalError').modal('show');
51
                break;
52
            case 401:
53
                $scope.modalError = {
54
                    title: 'Platnost webové aplikace vypršela',
55
                    body: 'Pro obnovení platnosti stačí stisknout tlačítko <strong>Obnovit</strong>.',
56
                    button: 'Obnovit',
57
                    clickButton: $scope.reloadApp
58
                };
59
                jQuery('#modalError').modal({backdrop: 'static', keyboard: false});
60
                break;
61
            case 404:
62
                console.log('API ERROR 404');
63
                $scope.modalError = {title: 'Nenalezen', body: 'Záznam nebyl nalezen.', button: 'OK'};
64
                jQuery('#modalError').modal('show');
65
                break;
66
            case 500:
67
                console.log('API ERROR 500');
68
                $scope.modalError = {title: 'Chyba', body: 'Chyba serveru. Zopakujte akci později.', button: 'OK'};
69
                jQuery('#modalError').modal('show');
70
                break;
71
            case -1:
72
                console.log('API NOT CONNECTED');
73
                $scope.modalError = {
74
                    title: 'Připojení k internetu',
75
                    body: 'Nejste připojeni k internetu. Zkontrolujte připojení.',
76
                    button: 'OK'
77
                };
78
                jQuery('#modalError').modal('show');
79
                break;
80
            default:
81
                console.log('API UNKNOWN ERROR');
82
                $scope.modalError = {title: 'Neočekávaná chyba', body: 'Nastala neočekávaná chyba.', button: 'OK'};
83
                jQuery('#modalError').modal('show');
84
                break;
85
        }
86
    };
87

    
88
    $scope.reloadApp = function () {
89
        $window.location.reload();
90
    }
91
});
92

    
93

    
94
app.controller('searchController', function ($rootScope, $scope, $location, config, Device) {
95

    
96
    this.$onInit = function () {
97
        var fromTime = new Date();
98
        fromTime.setHours(7, 0, 0, 0);
99

    
100
        var toTime = new Date();
101
        toTime.setHours(16, 0, 0, 0);
102

    
103

    
104
        var toDate = new Date(new Date().getTime() - (1 * 24 * 60 * 60 * 1000));
105

    
106
        //DODELAT OMEZENI
107
        $scope.maxDate = toDate;
108
        var fromDate = new Date(toDate.getTime() - (30 * 24 * 60 * 60 * 1000));
109

    
110
        var params = $location.search();
111

    
112
        $scope.search = {
113
            location: params.location,
114
            fromDate: params.fromDate == null ? fromDate : new Date(parseInt(params.fromDate)),
115
            toDate: params.toDate == null ? toDate : new Date(parseInt(params.toDate)),
116
            fromTime: params.fromTime == null ? fromTime : new Date(parseInt(params.fromTime)),
117
            toTime: params.toTime == null ? toTime : new Date(parseInt(params.toTime)),
118
            direction: params.direction == null ? true : !!+params.direction
119
        };
120

    
121
        $scope.locations = [];
122
        $scope.showSearchLoading = false;
123

    
124
        if (params.location != null && params.location.length > 2) {
125
            $scope.searchLocations(false);
126
        }
127
    };
128

    
129

    
130
    $scope.searchLocations = function (saveToUrl) {
131
        if (!$scope.search.location || $scope.search.location.length <= 1) {
132
            $scope.locations = [];
133
            return;
134
        }
135

    
136
        $scope.showSearchLoading = true;
137

    
138
        if (saveToUrl)
139
            $location.search({
140
                location: $scope.search.location,
141
                // fromDate: $scope.search.fromDate.getTime(),
142
                // toDate: $scope.search.toDate.getTime(),
143
                // fromTime: $scope.search.fromTime.getTime(),
144
                // toTime: $scope.search.toTime.getTime(),
145
                direction: $scope.search.direction ? 1 : 0
146
            });
147

    
148
        Device.query({
149
            address: $scope.search.location,
150
            showDirection: $scope.search.direction ? 1 : 0
151
        }, function (data) {
152
            $scope.locations = data;
153
            $scope.showSearchLoading = false;
154

    
155
            var params = $location.search();
156
            if (!saveToUrl && jQuery.grep($scope.locations, function (e) {
157
                return e.id === params.deviceId;
158
            }).length > 0) {
159
                $scope.selectDevice(params.deviceId);
160
            }
161

    
162
        }, function (response) {
163
            $scope.showSearchLoading = false;
164
            console.log('Error api all Devices');
165
            $rootScope.handleErrorResponse(response);
166
        });
167
    };
168

    
169
    $scope.selectDevice = function (id) {
170
        var searchObject = $location.search();
171
        searchObject.deviceId = id;
172
        $location.search(searchObject);
173

    
174
        $rootScope.$emit('activeMarker', {id: id});
175
        $rootScope.$emit('infoLocation', {id: id});
176
    };
177

    
178
});
179

    
180

    
181
app.controller('infoController', function ($rootScope, $scope, config, Device, Vehicle) {
182

    
183
    this.$onInit = function () {
184
        $rootScope.selectDevice = null;
185
        $scope.vehicles = [];
186
        $scope.showInfoLoading = false;
187

    
188
        Vehicle.query(null, function (data) {
189
            $scope.vehicles = data;
190
        }, function (response) {
191
            $rootScope.graphShow = false;
192
            console.log('Error api all Vehicles');
193
            $rootScope.handleErrorResponse(response);
194
        });
195
    };
196

    
197
    $rootScope.$on('infoLocation', function (event, args) {
198
        $scope.showInfoLoading = true;
199

    
200
        Device.get({
201
            id: args.id
202
            // dateFrom: $scope.search.fromDate.getTime(),
203
            // dateTo: $scope.search.toDate.getTime(),
204
            // timeFrom: $scope.search.fromTime.getTime(),
205
            // timeTo: $scope.search.toTime.getTime(),
206
            // direction: $scope.search.direction ? 1 : 0
207
        }, function (data) {
208
            $rootScope.selectDevice = data;
209
            $scope.showInfoLoading = false;
210
        }, function (response) {
211
            $rootScope.selectDevice = null;
212
            $scope.showInfoLoading = false;
213
            console.log('Error api get Devices');
214
            $rootScope.handleErrorResponse(response);
215
        });
216

    
217
    });
218

    
219
    $scope.infoClose = function () {
220
        $rootScope.selectDevice = null;
221

    
222
        $rootScope.$emit('setDefaultMap', null);
223
    };
224
});
225

    
226

    
227
app.controller('mapController', function ($rootScope, $scope, config, Device) {
228

    
229
    this.$onInit = function () {
230

    
231
        $scope.map = new GMaps({
232
            div: '#map',
233
            zoomControl: true,
234
            mapTypeControl: false,
235
            scaleControl: false,
236
            streetViewControl: false,
237
            rotateControl: false,
238
            fullscreenControl: false,
239
            mapTypeId: 'roadmap',
240
            zoom: config.DEFAULT_ZOOM,
241
            lat: config.DEFAULT_POSITION.LAT,
242
            lng: config.DEFAULT_POSITION.LNG
243
        });
244

    
245
        Device.query({showDirection: 0}, function (data) {
246
            $scope.createMarkerNext(data, 0);
247
        }, function (response) {
248
            console.log('Error api all Devices');
249
            $rootScope.handleErrorResponse(response);
250
        });
251
    };
252

    
253
    $scope.createMarkerNext = function (data, i) {
254
        var lctn = data[i];
255

    
256
        GMaps.geocode({
257
            address: lctn.street + ', ' + lctn.town + ', Plzeňský kraj',
258
            callback: function (results, status) {
259
                if (status === 'OK') {
260
                    latlng = results[0].geometry.location;
261

    
262
                    var marker = $scope.map.addMarker({
263
                        lat: latlng.lat(),
264
                        lng: latlng.lng(),
265
                        title: lctn.name,
266
                        click: function (e) {
267
                            $rootScope.$emit('infoLocation', {id: lctn.id});
268
                            //alert("asdfas");
269
                        },
270
                        infoWindow: {
271
                            content: '<h6 class="mb-1">' + lctn.name + '</h6>'
272
                            + '<address>' + lctn.street + ', ' + lctn.town + '</address>'
273
                        },
274
                        id: lctn.id
275
                    });
276

    
277
                } else if (status === 'ZERO_RESULTS') {
278
                    console.log('No results found address');
279
                }
280

    
281
                i++;
282
                if (i < data.length) {
283
                    setTimeout(function () {
284
                        $scope.createMarkerNext(data, i);
285
                    }, 900);
286
                }
287
            }
288
        });
289

    
290
    };
291

    
292
    $rootScope.$on('activeMarker', function (event, args) {
293
        var id = args.id;
294
        for (var i = 0, marker; marker = $scope.map.markers[i]; i++) {
295
            if (marker.id && marker.id === id && marker.infoWindow) {
296
                $scope.map.setCenter(marker.position.lat(), marker.position.lng());
297
                $scope.map.setZoom(12);
298
                marker.infoWindow.open($scope.map, marker);
299
                return;
300
            }
301
        }
302
    });
303

    
304
    $rootScope.$on('setDefaultMap', function (event, args) {
305
        $scope.map.setCenter(config.DEFAULT_POSITION.LAT, config.DEFAULT_POSITION.LNG);
306
        $scope.map.setZoom(config.DEFAULT_ZOOM);
307
        $scope.map.hideInfoWindows();
308
    });
309

    
310
});
311

    
312

    
313
app.factory("Device", function ($resource, config) {
314
    return $resource(config.API_URL + "/devices/:id", {id: '@id'}, {
315
        'get': {
316
            url: config.API_URL + '/devices/:id',
317
            method: 'GET',
318
            headers: {jwt: config.API_TOKEN}
319
        },
320
        'query': {
321
            url: config.API_URL + '/devices',
322
            method: 'GET',
323
            isArray: true,
324
            headers: {jwt: config.API_TOKEN}
325
        }
326
    });
327
});
328

    
329
app.factory("Vehicle", function ($resource, config) {
330
    return $resource(config.API_URL + "/vehicles", null, {
331
        'query': {
332
            url: config.API_URL + '/vehicles',
333
            method: 'GET',
334
            isArray: true,
335
            headers: {jwt: config.API_TOKEN}
336
        }
337
    });
338
});
(1-1/3)