Projekt

Obecné

Profil

Stáhnout (9.59 KB) Statistiky
| Větev: | Revize:
1
var app = angular.module('pvpk', ['ngRoute', 'ngResource']);
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
                break;
46
            case 401:
47
                jQuery('#modalExpiredToken').modal('show');
48
                break;
49
            case 404:
50
                console.log('API ERROR 404');
51
                break;
52
            case 500:
53
                console.log('API ERROR 500');
54
                break;
55
            default:
56
        }
57
    };
58

    
59
    $scope.reloadApp = function () {
60
        $window.location.reload();
61
    }
62
});
63

    
64

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

    
67
    this.$onInit = function () {
68
        var fromTime = new Date();
69
        fromTime.setHours(7, 0, 0, 0);
70

    
71
        var toTime = new Date();
72
        toTime.setHours(16, 0, 0, 0);
73

    
74

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

    
77
        //DODELAT OMEZENI
78
        $scope.maxDate = toDate;
79
        var fromDate = new Date(toDate.getTime() - (30 * 24 * 60 * 60 * 1000));
80

    
81
        var params = $location.search();
82

    
83
        $scope.search = {
84
            location: params.location,
85
            fromDate: params.fromDate == null ? fromDate : new Date(parseInt(params.fromDate)),
86
            toDate: params.toDate == null ? toDate : new Date(parseInt(params.toDate)),
87
            fromTime: params.fromTime == null ? fromTime : new Date(parseInt(params.fromTime)),
88
            toTime: params.toTime == null ? toTime : new Date(parseInt(params.toTime)),
89
            direction: params.direction == null ? true : !!+params.direction
90
        };
91

    
92
        $scope.locations = [];
93
        $scope.showSearchLoading = false;
94

    
95
        if (params.location != null && params.location.length > 2) {
96
            $scope.searchLocations(false);
97
        }
98
    };
99

    
100

    
101
    $scope.searchLocations = function (saveToUrl) {
102
        if (!$scope.search.location || $scope.search.location.length <= 1) {
103
            $scope.locations = [];
104
            return;
105
        }
106

    
107
        $scope.showSearchLoading = true;
108

    
109
        if (saveToUrl)
110
            $location.search({
111
                location: $scope.search.location,
112
                // fromDate: $scope.search.fromDate.getTime(),
113
                // toDate: $scope.search.toDate.getTime(),
114
                // fromTime: $scope.search.fromTime.getTime(),
115
                // toTime: $scope.search.toTime.getTime(),
116
                direction: $scope.search.direction ? 1 : 0
117
            });
118

    
119
        Device.query({
120
            address: $scope.search.location,
121
            showDirection: $scope.search.direction ? 1 : 0
122
        }, function (data) {
123
            $scope.locations = data;
124
            $scope.showSearchLoading = false;
125

    
126
            var params = $location.search();
127
            if (!saveToUrl && jQuery.grep($scope.locations, function (e) {
128
                return e.id === params.deviceId;
129
            }).length > 0) {
130
                $scope.selectDevice(params.deviceId);
131
            }
132

    
133
        }, function (response) {
134
            $scope.showSearchLoading = false;
135
            console.log('Error api all Devices');
136
            $rootScope.handleErrorResponse(response);
137
        });
138
    };
139

    
140
    $scope.selectDevice = function (id) {
141
        var searchObject = $location.search();
142
        searchObject.deviceId = id;
143
        $location.search(searchObject);
144

    
145
        $rootScope.$emit('activeMarker', {id: id});
146
        $rootScope.$emit('infoLocation', {id: id});
147
    };
148

    
149
});
150

    
151

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

    
154
    this.$onInit = function () {
155
        $rootScope.selectDevice = null;
156
        $scope.vehicles = [];
157
        $scope.showInfoLoading = false;
158

    
159
        Vehicle.query(null, function (data) {
160
            $scope.vehicles = data;
161
        }, function (response) {
162
            $rootScope.graphShow = false;
163
            console.log('Error api all Vehicles');
164
            $rootScope.handleErrorResponse(response);
165
        });
166
    };
167

    
168
    $rootScope.$on('infoLocation', function (event, args) {
169
        $scope.showInfoLoading = true;
170

    
171
        Device.get({
172
            id: args.id
173
            // dateFrom: $scope.search.fromDate.getTime(),
174
            // dateTo: $scope.search.toDate.getTime(),
175
            // timeFrom: $scope.search.fromTime.getTime(),
176
            // timeTo: $scope.search.toTime.getTime(),
177
            // direction: $scope.search.direction ? 1 : 0
178
        }, function (data) {
179
            $rootScope.selectDevice = data;
180
            $scope.showInfoLoading = false;
181
        }, function (response) {
182
            $rootScope.selectDevice = null;
183
            $scope.showInfoLoading = false;
184
            console.log('Error api get Devices');
185
            $rootScope.handleErrorResponse(response);
186
        });
187

    
188
    });
189

    
190
    $scope.infoClose = function () {
191
        $rootScope.selectDevice = null;
192

    
193
        $rootScope.$emit('setDefaultMap', null);
194
    };
195
});
196

    
197

    
198
app.controller('mapController', function ($rootScope, $scope, config, Device) {
199

    
200
    this.$onInit = function () {
201

    
202
        $scope.map = new GMaps({
203
            div: '#map',
204
            zoomControl: true,
205
            mapTypeControl: false,
206
            scaleControl: false,
207
            streetViewControl: false,
208
            rotateControl: false,
209
            fullscreenControl: false,
210
            mapTypeId: 'roadmap',
211
            zoom: config.DEFAULT_ZOOM,
212
            lat: config.DEFAULT_POSITION.LAT,
213
            lng: config.DEFAULT_POSITION.LNG
214
        });
215

    
216
        Device.query({showDirection: 0}, function (data) {
217
            $scope.createMarkerNext(data, 0);
218
        }, function (response) {
219
            console.log('Error api all Devices');
220
            $rootScope.handleErrorResponse(response);
221
        });
222
    };
223

    
224
    $scope.createMarkerNext = function (data, i) {
225
        var lctn = data[i];
226

    
227
        GMaps.geocode({
228
            address: lctn.street + ', ' + lctn.town + ', Plzeňský kraj',
229
            callback: function (results, status) {
230
                if (status === 'OK') {
231
                    latlng = results[0].geometry.location;
232

    
233
                    var marker = $scope.map.addMarker({
234
                        lat: latlng.lat(),
235
                        lng: latlng.lng(),
236
                        title: lctn.name,
237
                        label: 'U',
238
                        click: function (e) {
239
                            $rootScope.$emit('infoLocation', {id: lctn.id});
240
                            //alert("asdfas");
241
                        },
242
                        infoWindow: {
243
                            content: '<h6 class="mb-1">' + lctn.name + '</h6>'
244
                            + '<address>' + lctn.street + ', ' + lctn.town + '</address>'
245
                        },
246
                        id: lctn.id
247
                    });
248

    
249
                } else if (status === 'ZERO_RESULTS') {
250
                    console.log('No results found address');
251
                }
252

    
253
                i++;
254
                if (i < data.length) {
255
                    setTimeout(function () {
256
                        $scope.createMarkerNext(data, i);
257
                    }, 900);
258
                }
259
            }
260
        });
261

    
262
    };
263

    
264
    $rootScope.$on('activeMarker', function (event, args) {
265
        var id = args.id;
266
        for (var i = 0, marker; marker = $scope.map.markers[i]; i++) {
267
            if (marker.id && marker.id === id && marker.infoWindow) {
268
                $scope.map.setCenter(marker.position.lat(), marker.position.lng());
269
                $scope.map.setZoom(12);
270
                marker.infoWindow.open($scope.map, marker);
271
                return;
272
            }
273
        }
274
    });
275

    
276
    $rootScope.$on('setDefaultMap', function (event, args) {
277
        $scope.map.setCenter(config.DEFAULT_POSITION.LAT, config.DEFAULT_POSITION.LNG);
278
        $scope.map.setZoom(config.DEFAULT_ZOOM);
279
        $scope.map.hideInfoWindows();
280
    });
281

    
282
});
283

    
284

    
285
app.factory("Device", function ($resource, config) {
286
    return $resource(config.API_URL + "/devices/:id", {id: '@id'}, {
287
        'get': {
288
            url: config.API_URL + '/devices/:id',
289
            method: 'GET',
290
            headers: {jwt: config.API_TOKEN}
291
        },
292
        'query': {
293
            url: config.API_URL + '/devices',
294
            method: 'GET',
295
            isArray: true,
296
            headers: {jwt: config.API_TOKEN}
297
        }
298
    });
299
});
300

    
301
app.factory("Vehicle", function ($resource, config) {
302
    return $resource(config.API_URL + "/vehicles", null, {
303
        'query': {
304
            url: config.API_URL + '/vehicles',
305
            method: 'GET',
306
            isArray: true,
307
            headers: {jwt: config.API_TOKEN}
308
        }
309
    });
310
});
(1-1/3)