GUI -- onos-sortable-header will get the controller to use the restService to up…
…date the table data. Query parameters were created based on colId and sort direction. Change-Id: I27bc6ebab9f8f1ba332b4480171b8afc3a4fd3c4
Showing
5 changed files
with
96 additions
and
71 deletions
... | @@ -21,8 +21,12 @@ | ... | @@ -21,8 +21,12 @@ |
21 | 'use strict'; | 21 | 'use strict'; |
22 | 22 | ||
23 | var $log, $window, fs, is, | 23 | var $log, $window, fs, is, |
24 | + div, | ||
25 | + currCol = {}, | ||
26 | + prevCol = {}, | ||
24 | bottomMargin = 200; | 27 | bottomMargin = 200; |
25 | 28 | ||
29 | + | ||
26 | // Render a plain d3 table by giving it the div, a config file, and data | 30 | // Render a plain d3 table by giving it the div, a config file, and data |
27 | 31 | ||
28 | function renderTable(div, config, data) { | 32 | function renderTable(div, config, data) { |
... | @@ -106,6 +110,50 @@ | ... | @@ -106,6 +110,50 @@ |
106 | setTableHeight(th, tb); | 110 | setTableHeight(th, tb); |
107 | } | 111 | } |
108 | 112 | ||
113 | + function updateSortingIcons(thElem, api) { | ||
114 | + currCol.colId = thElem.attr('colId'); | ||
115 | + | ||
116 | + if (currCol.colId === prevCol.colId) { | ||
117 | + (currCol.icon === 'tableColSortDesc') ? | ||
118 | + currCol.icon = 'tableColSortAsc' : | ||
119 | + currCol.icon = 'tableColSortDesc'; | ||
120 | + prevCol.icon = currCol.icon; | ||
121 | + } else { | ||
122 | + currCol.icon = 'tableColSortAsc'; | ||
123 | + prevCol.icon = 'tableColSortNone'; | ||
124 | + } | ||
125 | + | ||
126 | + div = thElem.select('div'); | ||
127 | + div.remove(); | ||
128 | + div = thElem.append('div'); | ||
129 | + | ||
130 | + if (currCol.icon === 'tableColSortAsc') { | ||
131 | + api.sortAsc(div); | ||
132 | + } else { | ||
133 | + api.sortDesc(div); | ||
134 | + } | ||
135 | + | ||
136 | + if (prevCol.colId !== undefined && | ||
137 | + prevCol.icon === 'tableColSortNone') { | ||
138 | + api.sortNone(prevCol.elem.select('div')); | ||
139 | + } | ||
140 | + | ||
141 | + prevCol.colId = currCol.colId; | ||
142 | + prevCol.elem = thElem; | ||
143 | + } | ||
144 | + | ||
145 | + function generateQueryParams() { | ||
146 | + var queryString = '?sortCol=' + currCol.colId + '&sortDir='; | ||
147 | + | ||
148 | + if(currCol.icon === 'tableColSortAsc') { | ||
149 | + queryString = queryString + 'asc'; | ||
150 | + } else { | ||
151 | + queryString = queryString + 'desc'; | ||
152 | + } | ||
153 | + | ||
154 | + return queryString; | ||
155 | + } | ||
156 | + | ||
109 | angular.module('onosWidget') | 157 | angular.module('onosWidget') |
110 | .factory('TableService', [function () { | 158 | .factory('TableService', [function () { |
111 | return { | 159 | return { |
... | @@ -158,66 +206,31 @@ | ... | @@ -158,66 +206,31 @@ |
158 | 206 | ||
159 | .directive('onosSortableHeader', ['$log', 'IconService', | 207 | .directive('onosSortableHeader', ['$log', 'IconService', |
160 | function (_$log_, _is_) { | 208 | function (_$log_, _is_) { |
161 | - return function (scope, element, attrs) { | 209 | + return { |
162 | - $log = _$log_; | 210 | + scope: { |
163 | - is = _is_; | 211 | + ctrlCallback: '&sortCallback' |
164 | - var table = d3.select(element[0]), | 212 | + }, |
165 | - currCol = {}, | 213 | + link: function (scope, element, attrs) { |
166 | - prevCol = {}, | 214 | + $log = _$log_; |
167 | - sortIconAPI = is.createSortIcon(); | 215 | + is = _is_; |
168 | - | 216 | + var table = d3.select(element[0]), |
169 | - // when a header is clicked, change its icon tag and get sorting | 217 | + sortIconAPI = is.createSortIcon(); |
170 | - // order to send to the server. | 218 | + |
171 | - table.selectAll('th').on('click', function () { | 219 | + // when a header is clicked, change its icon tag |
172 | - var thElem = d3.select(this), | 220 | + // and get sorting order to send to the server. |
173 | - div; | 221 | + table.selectAll('th').on('click', function () { |
174 | - | 222 | + var thElem = d3.select(this); |
175 | - currCol.colId = thElem.attr('colId'); | 223 | + |
176 | - | 224 | + if (thElem.attr('sortable') === '') { |
177 | - if (currCol.colId === prevCol.colId) { | 225 | + updateSortingIcons(thElem, sortIconAPI); |
178 | - (currCol.icon === 'tableColSortDesc') ? | 226 | + // call the ctrl's rest callback function |
179 | - currCol.icon = 'tableColSortAsc' : | 227 | + scope.ctrlCallback({ |
180 | - currCol.icon = 'tableColSortDesc'; | 228 | + urlSuffix: generateQueryParams() |
181 | - prevCol.icon = currCol.icon; | 229 | + }); |
182 | - } else { | 230 | + } |
183 | - currCol.icon = 'tableColSortAsc'; | 231 | + }); |
184 | - prevCol.icon = 'tableColSortNone'; | 232 | + } |
185 | - } | 233 | + }; |
186 | - | ||
187 | - $log.debug('currCol clicked: ' + currCol.colId + | ||
188 | - ', with sorting icon: ' + currCol.icon); | ||
189 | - $log.debug('prevCol clicked: ' + prevCol.colId + | ||
190 | - ', with its current sorting icon as ' + prevCol.icon); | ||
191 | - | ||
192 | - div = thElem.select('div'); | ||
193 | - div.remove(); | ||
194 | - | ||
195 | - div = thElem.append('div'); | ||
196 | - | ||
197 | - if (currCol.icon === 'tableColSortAsc') { | ||
198 | - sortIconAPI.sortAsc(div); | ||
199 | - } else { | ||
200 | - sortIconAPI.sortDesc(div); | ||
201 | - } | ||
202 | - | ||
203 | - if (prevCol.colId !== undefined && | ||
204 | - prevCol.icon === 'tableColSortNone') { | ||
205 | - sortIconAPI.sortNone(prevCol.elem.select('div')); | ||
206 | - } | ||
207 | - | ||
208 | - prevCol.colId = currCol.colId; | ||
209 | - prevCol.elem = thElem; | ||
210 | - | ||
211 | - }); | ||
212 | - | ||
213 | - // TODO: send the prev and currCol info to the server to use in sorting table | ||
214 | - | ||
215 | - // TODO: figure out timing of events: | ||
216 | - // updating the icon | ||
217 | - // sending the column sorting info to the server | ||
218 | - // refreshing the table so that the new rows will be sorted | ||
219 | - | ||
220 | - } | ||
221 | }]); | 234 | }]); |
222 | 235 | ||
223 | }()); | 236 | }()); | ... | ... |
... | @@ -5,7 +5,7 @@ | ... | @@ -5,7 +5,7 @@ |
5 | onos-fixed-header | 5 | onos-fixed-header |
6 | ng-style="setTableHW()" | 6 | ng-style="setTableHW()" |
7 | onos-sortable-header | 7 | onos-sortable-header |
8 | - sort-callback="ctrl.sortCallback()"> | 8 | + sort-callback="sortCallback(urlSuffix)"> |
9 | <thead> | 9 | <thead> |
10 | <tr> | 10 | <tr> |
11 | <th colId="available"></th> | 11 | <th colId="available"></th> | ... | ... |
... | @@ -22,8 +22,8 @@ | ... | @@ -22,8 +22,8 @@ |
22 | 'use strict'; | 22 | 'use strict'; |
23 | 23 | ||
24 | angular.module('ovDevice', []) | 24 | angular.module('ovDevice', []) |
25 | - .controller('OvDeviceCtrl', ['$log', '$location', 'RestService', | 25 | + .controller('OvDeviceCtrl', ['$log', '$scope', '$location', 'RestService', |
26 | - function ($log, $location, rs) { | 26 | + function ($log, $scope, $location, rs) { |
27 | var self = this; | 27 | var self = this; |
28 | self.deviceData = []; | 28 | self.deviceData = []; |
29 | 29 | ||
... | @@ -31,9 +31,17 @@ | ... | @@ -31,9 +31,17 @@ |
31 | var testCase = $location.search().test; | 31 | var testCase = $location.search().test; |
32 | var url = testCase ? 'test/' + testCase : 'device'; | 32 | var url = testCase ? 'test/' + testCase : 'device'; |
33 | 33 | ||
34 | - rs.get(url, function (data) { | 34 | + $scope.sortCallback = function (urlSuffix) { |
35 | - self.deviceData = data.devices; | 35 | + if (!urlSuffix) { |
36 | - }); | 36 | + urlSuffix = ''; |
37 | + } | ||
38 | + url = 'device' + urlSuffix; | ||
39 | + rs.get(url, function (data) { | ||
40 | + self.deviceData = data.devices; | ||
41 | + }); | ||
42 | + }; | ||
43 | + | ||
44 | + $scope.sortCallback(); | ||
37 | 45 | ||
38 | $log.log('OvDeviceCtrl has been created'); | 46 | $log.log('OvDeviceCtrl has been created'); |
39 | }]); | 47 | }]); | ... | ... |
... | @@ -64,7 +64,7 @@ describe('factory: fw/layer/flash.js', function () { | ... | @@ -64,7 +64,7 @@ describe('factory: fw/layer/flash.js', function () { |
64 | text = item.select('text'); | 64 | text = item.select('text'); |
65 | expect(text.size()).toEqual(1); | 65 | expect(text.size()).toEqual(1); |
66 | expect(text.text()).toEqual('foo'); | 66 | expect(text.text()).toEqual('foo'); |
67 | - }, 500); | 67 | + }, 2000); |
68 | }); | 68 | }); |
69 | 69 | ||
70 | // TODO: testing these time-sensitive behaviors is hard... | 70 | // TODO: testing these time-sensitive behaviors is hard... | ... | ... |
... | @@ -21,7 +21,7 @@ describe('Controller: OvDeviceCtrl', function () { | ... | @@ -21,7 +21,7 @@ describe('Controller: OvDeviceCtrl', function () { |
21 | // instantiate the Device module | 21 | // instantiate the Device module |
22 | beforeEach(module('ovDevice', 'onosRemote')); | 22 | beforeEach(module('ovDevice', 'onosRemote')); |
23 | 23 | ||
24 | - var $log, $controller, ctrl, $mockHttp; | 24 | + var $log, $scope, $controller, ctrl, $mockHttp; |
25 | 25 | ||
26 | var fakeData = { | 26 | var fakeData = { |
27 | "devices": [{ | 27 | "devices": [{ |
... | @@ -40,17 +40,21 @@ describe('Controller: OvDeviceCtrl', function () { | ... | @@ -40,17 +40,21 @@ describe('Controller: OvDeviceCtrl', function () { |
40 | }] | 40 | }] |
41 | }; | 41 | }; |
42 | 42 | ||
43 | - beforeEach(inject(function(_$log_, _$controller_, $httpBackend) { | 43 | + beforeEach(inject(function(_$log_, $rootScope, _$controller_, $httpBackend) { |
44 | $log = _$log_; | 44 | $log = _$log_; |
45 | + $scope = $rootScope.$new(); | ||
45 | $controller = _$controller_; | 46 | $controller = _$controller_; |
46 | $mockHttp = $httpBackend; | 47 | $mockHttp = $httpBackend; |
47 | 48 | ||
48 | $mockHttp.whenGET(/\/device$/).respond(fakeData); | 49 | $mockHttp.whenGET(/\/device$/).respond(fakeData); |
49 | })); | 50 | })); |
50 | 51 | ||
51 | - it('should be an empty array', function () { | 52 | + it('should be an empty array and then have device data', function () { |
52 | - ctrl = $controller('OvDeviceCtrl'); | 53 | + ctrl = $controller('OvDeviceCtrl', { |
54 | + $scope: $scope | ||
55 | + }); | ||
53 | expect(ctrl.deviceData).toEqual([]); | 56 | expect(ctrl.deviceData).toEqual([]); |
57 | + $scope.sortCallback(); | ||
54 | $mockHttp.flush(); | 58 | $mockHttp.flush(); |
55 | expect(ctrl.deviceData).toEqual(fakeData.devices); | 59 | expect(ctrl.deviceData).toEqual(fakeData.devices); |
56 | }); | 60 | }); | ... | ... |
-
Please register or login to post a comment