Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 0b27c108

Přidáno uživatelem Jan Kohlíček před asi 7 roky(ů)

refs #6777: Komunikace s REST API, přispůsobení REST API, vyhledávání lokalit, možnost schovat graf, ošetření expirace tokenu, přidání loading screenu, generování tokenu do stránky

Zobrazit rozdíly:

frontend/app.js
1
var app = angular.module('pvpk', ['ngRoute', 'ngResource']);
1 2

  
2
//var app = angular.module('PVPK', []);
3

  
4

  
5
// app.controller('myController', function($scope, $http, $window) {
6
//     $scope.render = function() {
7
//         var url = 'http://www.recipepuppy.com/api/';
8
//
9
//         var c = $window.angular.callbacks.counter.toString(36);
10
//         $window['angularcallbacks' + c] = function (data) {
11
//             $window.angular.callbacks['_' + c](data);
12
//             delete $window['angularcallbacks' + c];
13
//         };
14
//
15
//         $http.jsonp('http://www.recipepuppy.com/api/?i=onions,garlic&q=omelet&p=1&callback=JSON_CALLBACK').then(function(response) {
16
//             $scope.records = response.data.results;
17
//         });
18
//     };
19
//
20
//     $scope.render();
21
// });
3
app.constant('config', {
4
    APP_NAME: 'PVPK',
5
    APP_VERSION: 1.0,
6
    API_URL: API_URL,
7
    API_TOKEN: API_TOKEN
8
});
9

  
10

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

  
32
app.config(['$routeProvider', '$locationProvider', function ($routeProvider, $locationProvider) {
33

  
34
}]);
35

  
36

  
37
app.controller('mainController', function ($rootScope, $scope, $http, $window) {
38

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

  
43
    $rootScope.handleErrorResponse = function (response) {
44
        switch (response.status) {
45
            case 400:
46
                console.log('API ERROR 400');
47
                break;
48
            case 401:
49
                jQuery('#modalExpiredToken').modal('show');
50
                break;
51
            case 404:
52
                console.log('API ERROR 404');
53
                break;
54
            case 500:
55
                console.log('API ERROR 500');
56
                break;
57
            default:
58
        }
59
    };
60

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

  
66

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

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

  
73
        var toTime = new Date();
74
        toTime.setHours(16, 0, 0, 0);
75

  
76
        var toDate = new Date();
77
        var fromDate = new Date(toDate.getTime() - (30 * 24 * 60 * 60 * 1000));
78

  
79
        var params = $location.search();
80

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

  
90
        $scope.locations = [];
91
        $scope.showLocationsLoading = false;
92

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

  
98

  
99
    $scope.searchLocations = function (saveToUrl) {
100
        $scope.showLocationsLoading = true;
101

  
102
        if (saveToUrl)
103
            $location.search({
104
                location: $scope.search.location,
105
                fromDate: $scope.search.fromDate.getTime(),
106
                toDate: $scope.search.toDate.getTime(),
107
                fromTime: $scope.search.fromTime.getTime(),
108
                toTime: $scope.search.toTime.getTime(),
109
                direction: $scope.search.direction ? 1 : 0
110
            });
111

  
112
        Device({jwt: config.API_TOKEN}).query({
113
            address: $scope.search.location,
114
            showDirection: $scope.search.direction ? 1 : 0
115
        }, function (data) {
116
            $scope.locations = data;
117
            $scope.showLocationsLoading = false;
118

  
119
            var params = $location.search();
120
            if(!saveToUrl && jQuery.grep($scope.locations, function(e){ return e.id === params.deviceId; }).length>0){
121
                $scope.selectDevice(params.deviceId);
122
            }
123

  
124
        }, function (response) {
125
            $scope.showLocationsLoading = false;
126
            console.log('Error api all Devices');
127
            $rootScope.handleErrorResponse(response);
128
        });
129
    };
130

  
131
    $scope.selectDevice = function (id) {
132
        $scope.deviceId = id;
133

  
134
        var searchObject = $location.search();
135
        searchObject.deviceId = id;
136
        $location.search(searchObject);
137

  
138
        Device({jwt: config.API_TOKEN}).get({
139
            id: id
140
            // dateFrom: $scope.search.fromDate.getTime(),
141
            // dateTo: $scope.search.toDate.getTime(),
142
            // timeFrom: $scope.search.fromTime.getTime(),
143
            // timeTo: $scope.search.toTime.getTime(),
144
            // direction: $scope.search.direction ? 1 : 0
145
        }, function (data) {
146
            $rootScope.$emit('renderGraph', data);
147
        }, function (response) {
148
            console.log('Error api get Devices');
149
            $rootScope.handleErrorResponse(response);
150
        });
151
    };
152

  
153
});
154

  
155

  
156
app.controller('graphController', function ($rootScope, $scope, config, Vehicle) {
157

  
158
    this.$onInit = function () {
159
        $rootScope.graphShow = false;
160
        $scope.vehicles = [];
161
    };
162

  
163
    $rootScope.$on("renderGraph", function (event, args) {
164
        $rootScope.graphShow = true;
165

  
166
        Vehicle({jwt: config.API_TOKEN}).query(null, function (data) {
167
            $scope.vehicles = data;
168

  
169
        }, function (response) {
170
            $rootScope.graphShow = false;
171
            console.log('Error api all Vehicles');
172
            $rootScope.handleErrorResponse(response);
173
        });
174
    });
175

  
176
});
177

  
178
app.controller('mapController', function ($scope) {
179

  
180
    this.$onInit = function () {
181

  
182
    };
183

  
184
});
185

  
186

  
187
app.factory("Device", function ($resource, config) {
188
    return function (headers) {
189
        return $resource(config.API_URL + "/devices/:id", {id: '@id'}, {
190
            'get': {
191
                url: config.API_URL + '/devices/:id',
192
                method: 'GET',
193
                headers: headers || {}
194
            },
195
            'query': {
196
                url: config.API_URL + '/devices',
197
                method: 'GET',
198
                isArray: true,
199
                headers: headers || {}
200
            }
201
        });
202
    };
203
});
204

  
205
app.factory("Vehicle", function ($resource, config) {
206
    return function (headers) {
207
        return $resource(config.API_URL + "/vehicles", null, {
208
            'query': {
209
                url: config.API_URL + '/vehicles',
210
                method: 'GET',
211
                isArray: true,
212
                headers: headers || {}
213
            }
214
        });
215
    };
216
});

Také k dispozici: Unified diff