Richard Allred
2019-08-03 99e73490b45f1f76040216418afeec9ff9770e57
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
var app = angular.module('items', ['ngResource', 'ngGrid', 'ui.bootstrap']);
 
// Create a controller with name itemListController to bind to the grid section.
app.controller('itemsListController', function ($scope, $rootScope, itemService) {
    // Initialize required information: sorting, the first page to show and the grid options.
    $scope.sortInfo = {fields: ['id'], directions: ['asc']};
    $scope.items = {currentPage: 1};
 
    $scope.gridOptions = {
        data: 'items.list',
        useExternalSorting: true,
        sortInfo: $scope.sortInfo,
 
 
        columnDefs: [
            { field: 'id', displayName: 'Id' },
            { field: 'description', displayName: 'Description' },
            { field: 'done', displayName: 'Done' },
            { field: '', width: 30, cellTemplate: '<span class="glyphicon glyphicon-remove remove" ng-click="deleteRow(row)"></span>' }
        ],
 
        multiSelect: false,
        selectedItems: [],
        // Broadcasts an event when a row is selected, to signal the form that it needs to load the row data.
        afterSelectionChange: function (rowItem) {
            if (rowItem.selected) {
                $rootScope.$broadcast('itemselected', $scope.gridOptions.selectedItems[0].id);
            }
        }
    };
 
    // Refresh the grid, calling the appropriate rest method.
    $scope.refreshGrid = function () {
        var listitemsArgs = {
            page: $scope.items.currentPage,
            sortFields: $scope.sortInfo.fields[0],
            sortDirections: $scope.sortInfo.directions[0]
        };
 
        itemService.get(listitemsArgs, function (data) {
            $scope.items = data;
        })
    };
 
    // Broadcast an event when an element in the grid is deleted. No real deletion is perfomed at this point.
    $scope.deleteRow = function (row) {
        $rootScope.$broadcast('deleteitem', row.entity.id);
    };
 
    // Watch the sortInfo variable. If changes are detected than we need to refresh the grid.
    // This also works for the first page access, since we assign the initial sorting in the initialize section.
    $scope.$watch('sortInfo.fields[0]', function () {
        $scope.refreshGrid();
    }, true);
    $scope.$watch('sortInfo.directions[0]', function () {
        $scope.refreshGrid();
    }, true);
 
    // Do something when the grid is sorted.
    // The grid throws the ngGridEventSorted that gets picked up here and assigns the sortInfo to the scope.
    // This will allow to watch the sortInfo in the scope for changed and refresh the grid.
    $scope.$on('ngGridEventSorted', function (event, sortInfo) {
        $scope.sortInfo = sortInfo;
    });
 
    // Picks the event broadcasted when a item is saved or deleted to refresh the grid elements with the most
    // updated information.
    $scope.$on('refreshGrid', function () {
        $scope.refreshGrid();
    });
 
    // Picks the event broadcasted when the form is cleared to also clear the grid selection.
    $scope.$on('clear', function () {
        $scope.gridOptions.selectAll(false);
    });
});
 
// Create a controller with name itemsFormController to bind to the form section.
app.controller('itemsFormController', function ($scope, $rootScope, itemService) {
    // Clears the form. Either by clicking the 'Clear' button in the form, or when a successful save is performed.
    $scope.clearForm = function () {
        $scope.item = null;
        // Resets the form validation state.
        $scope.itemForm.$setPristine();
        // Broadcast the event to also clear the grid selection.
        $rootScope.$broadcast('clear');
    };
 
    // Calls the rest method to save a item.
    $scope.updateItem = function () {
        itemService.save($scope.item).$promise.then(
            function () {
                // Broadcast the event to refresh the grid.
                $rootScope.$broadcast('refreshGrid');
                // Broadcast the event to display a save message.
                $rootScope.$broadcast('itemsaved');
                // XXX Generates null error in browser ?!?
                $scope.clearForm();
            },
            function () {
                // Broadcast the event for a server error.
                $rootScope.$broadcast('error');
            });
    };
 
    // Picks up the event broadcasted when the item is selected from the grid and perform the item load by calling
    // the appropiate rest service.
    $scope.$on('itemselected', function (event, id) {
        $scope.item = itemService.get({id: id});
    });
 
    // Picks us the event broadcasted when the item is deleted from the grid and perform the actual item delete by
    // calling the appropiate rest service.
    $scope.$on('deleteitem', function (event, id) {
        itemService.delete({id: id}).$promise.then(
            function () {
                // Broadcast the event to refresh the grid.
                $rootScope.$broadcast('refreshGrid');
                // Broadcast the event to display a delete message.
                $rootScope.$broadcast('itemDeleted');
                $scope.clearForm();
            },
            function () {
                // Broadcast the event for a server error.
                $rootScope.$broadcast('error');
            });
    });
});
 
// Create a controller with name alertMessagesController to bind to the feedback messages section.
app.controller('alertMessagesController', function ($scope) {
    // Picks up the event to display a saved message.
    $scope.$on('itemSaved', function () {
        $scope.alerts = [
            { type: 'success', msg: 'Record saved successfully!' }
        ];
    });
 
    // Picks up the event to display a deleted message.
    $scope.$on('itemDeleted', function () {
        $scope.alerts = [
            { type: 'success', msg: 'Record deleted successfully!' }
        ];
    });
 
    // Picks up the event to display a server error message.
    $scope.$on('error', function () {
        $scope.alerts = [
            { type: 'danger', msg: 'There was a problem in the server!' }
        ];
    });
 
    $scope.closeAlert = function (index) {
        $scope.alerts.splice(index, 1);
    };
});
 
// Service that provides items operations
app.factory('itemService', function ($resource) {
    return $resource('http://_BACKEND_/todo/api/items/:id');
});
 
 
// Create a controller that handles host information
app.controller('hostController', function ($scope, hostService) {
        $scope.host = hostService.get();
});
 
//Service that provides host operations
app.factory('hostService', function ($resource) {
    return $resource('http://_BACKEND_/todo/api/host', null,
                {
                'get': { method:'GET' }
                });
});