blob: 9bdf1c1c013ab6b745efede1402376b8ade13b9a [file] [log] [blame]
Bri Prebilic Cole093739a2015-01-23 10:22:50 -08001/*
2 * Copyright 2015 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/*
18 ONOS GUI -- Widget -- Table Service
19 */
20(function () {
21 'use strict';
22
Bri Prebilic Cole08d08d42015-04-01 14:37:02 -070023 // injected refs
Bri Prebilic Cole264c5ec2015-04-07 10:22:26 -070024 var $log, $window, fs, mast, is;
Bri Prebilic Cole08d08d42015-04-01 14:37:02 -070025
26 // constants
27 var tableIconTdSize = 33,
Bri Prebilic Cole9a5e0062015-05-11 17:20:57 -070028 pdg = 22,
Bri Prebilic Cole08d08d42015-04-01 14:37:02 -070029 colWidth = 'col-width',
Bri Prebilic Cole3d4d01c2015-04-30 13:48:36 -070030 tableIcon = 'table-icon',
31 asc = 'asc',
32 desc = 'desc',
33 none = 'none';
Bri Prebilic Cole08d08d42015-04-01 14:37:02 -070034
35 // internal state
36 var currCol = {},
Bri Prebilic Cole3d4d01c2015-04-30 13:48:36 -070037 prevCol = {},
Bri Prebilic Coleb3a6afe2015-06-24 14:10:41 -070038 cstmWidths = {},
Bri Prebilic Cole3d4d01c2015-04-30 13:48:36 -070039 sortIconAPI;
Bri Prebilic Cole093739a2015-01-23 10:22:50 -080040
Bri Prebilic Coleb3a6afe2015-06-24 14:10:41 -070041 // Functions for resizing a tabular view to the window
Bri Prebilic Cole9ca0f9c2015-01-29 15:44:20 -080042
Bri Prebilic Colee568ead2015-05-01 09:51:28 -070043 function _width(elem, width) {
44 elem.style('width', width);
Bri Prebilic Cole08d08d42015-04-01 14:37:02 -070045 }
Bri Prebilic Cole9ca0f9c2015-01-29 15:44:20 -080046
Bri Prebilic Coleb3a6afe2015-06-24 14:10:41 -070047 function findCstmWidths(table) {
48 var headers = table.select('.table-header').selectAll('td');
49
50 headers.each(function (d, i) {
51 var h = d3.select(this),
52 index = i.toString();
53 if (h.classed(tableIcon)) {
54 cstmWidths[index] = tableIconTdSize + 'px';
55 }
56 if (h.attr(colWidth)) {
57 cstmWidths[index] = h.attr(colWidth);
58 }
59 });
60 if (fs.debugOn('widget')) {
61 $log.debug('Headers with custom widths: ', cstmWidths);
62 }
Bri Prebilic Cole08d08d42015-04-01 14:37:02 -070063 }
Thomas Vachuska0fa583c2015-03-30 23:07:41 -070064
Bri Prebilic Coleb3a6afe2015-06-24 14:10:41 -070065 function setTdWidths(elem, width) {
66 var tds = elem.select('tr:first-child').selectAll('td');
67 _width(elem, width + 'px');
Bri Prebilic Cole9ca0f9c2015-01-29 15:44:20 -080068
Bri Prebilic Coleb3a6afe2015-06-24 14:10:41 -070069 tds.each(function (d, i) {
70 var td = d3.select(this),
71 index = i.toString();
72 if (cstmWidths.hasOwnProperty(index)) {
73 _width(td, cstmWidths[index]);
Simon Hunt4deb0e82015-06-10 16:18:25 -070074 }
Bri Prebilic Coleb3a6afe2015-06-24 14:10:41 -070075 });
76 }
77
78 function setHeight(thead, body, height) {
79 var h = height - (mast.mastHeight() +
80 fs.noPxStyle(d3.select('.tabular-header'), 'height') +
81 fs.noPxStyle(thead, 'height') + pdg);
82 body.style('height', h + 'px');
83 }
84
85 function adjustTable(haveItems, tableElems, width, height) {
86 if (haveItems) {
87 setTdWidths(tableElems.thead, width);
88 setTdWidths(tableElems.tbody, width);
89 setHeight(tableElems.thead, tableElems.table.select('.table-body'), height);
90 } else {
91 setTdWidths(tableElems.thead, width);
92 _width(tableElems.tbody, width + 'px');
Bri Prebilic Colee568ead2015-05-01 09:51:28 -070093 }
Bri Prebilic Cole9ca0f9c2015-01-29 15:44:20 -080094 }
95
Bri Prebilic Cole3d4d01c2015-04-30 13:48:36 -070096 // Functions for sorting table rows by header
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -080097
Bri Prebilic Cole3d4d01c2015-04-30 13:48:36 -070098 function updateSortDirection(thElem) {
99 sortIconAPI.sortNone(thElem.select('div'));
100 currCol.div = thElem.append('div');
Bri Prebilic Cole902cb042015-02-11 14:04:15 -0800101 currCol.colId = thElem.attr('colId');
102
103 if (currCol.colId === prevCol.colId) {
Bri Prebilic Cole3d4d01c2015-04-30 13:48:36 -0700104 (currCol.dir === desc) ? currCol.dir = asc : currCol.dir = desc;
105 prevCol.dir = currCol.dir;
Bri Prebilic Cole902cb042015-02-11 14:04:15 -0800106 } else {
Bri Prebilic Cole3d4d01c2015-04-30 13:48:36 -0700107 currCol.dir = asc;
108 prevCol.dir = none;
Bri Prebilic Cole902cb042015-02-11 14:04:15 -0800109 }
Bri Prebilic Cole3d4d01c2015-04-30 13:48:36 -0700110 (currCol.dir === asc) ?
111 sortIconAPI.sortAsc(currCol.div) : sortIconAPI.sortDesc(currCol.div);
Bri Prebilic Cole902cb042015-02-11 14:04:15 -0800112
Bri Prebilic Cole3d4d01c2015-04-30 13:48:36 -0700113 if (prevCol.colId && prevCol.dir === none) {
114 sortIconAPI.sortNone(prevCol.div);
Bri Prebilic Cole902cb042015-02-11 14:04:15 -0800115 }
116
117 prevCol.colId = currCol.colId;
Bri Prebilic Cole3d4d01c2015-04-30 13:48:36 -0700118 prevCol.div = currCol.div;
Bri Prebilic Cole902cb042015-02-11 14:04:15 -0800119 }
120
Bri Prebilic Cole19a32dd2015-03-26 18:00:03 -0700121 function sortRequestParams() {
122 return {
123 sortCol: currCol.colId,
Bri Prebilic Cole3d4d01c2015-04-30 13:48:36 -0700124 sortDir: currCol.dir
Bri Prebilic Cole19a32dd2015-03-26 18:00:03 -0700125 };
Bri Prebilic Cole902cb042015-02-11 14:04:15 -0800126 }
127
Bri Prebilic Colec2449762015-05-29 14:30:51 -0700128 function resetSort() {
Bri Prebilic Cole3d4d01c2015-04-30 13:48:36 -0700129 if (currCol.div) {
130 sortIconAPI.sortNone(currCol.div);
131 }
132 if (prevCol.div) {
133 sortIconAPI.sortNone(prevCol.div);
134 }
135 currCol = {};
136 prevCol = {};
137 }
138
Bri Prebilic Cole093739a2015-01-23 10:22:50 -0800139 angular.module('onosWidget')
Bri Prebilic Coleb3a6afe2015-06-24 14:10:41 -0700140 .directive('onosTableResize', ['$log','$window',
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700141 'FnService', 'MastService',
142
143 function (_$log_, _$window_, _fs_, _mast_) {
Bri Prebilic Colee4d5c6c2015-02-02 15:52:45 -0800144 return function (scope, element) {
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700145 $log = _$log_;
Bri Prebilic Colee4d5c6c2015-02-02 15:52:45 -0800146 $window = _$window_;
147 fs = _fs_;
Bri Prebilic Cole264c5ec2015-04-07 10:22:26 -0700148 mast = _mast_;
149
Bri Prebilic Cole068814d2015-05-14 16:06:38 -0700150 var table = d3.select(element[0]),
Bri Prebilic Coleb3a6afe2015-06-24 14:10:41 -0700151 tableElems = {
152 table: table,
153 thead: table.select('.table-header').select('table'),
154 tbody: table.select('.table-body').select('table')
155 },
156 wsz;
Bri Prebilic Cole9ca0f9c2015-01-29 15:44:20 -0800157
Bri Prebilic Coleb3a6afe2015-06-24 14:10:41 -0700158 findCstmWidths(table);
159
160 // adjust table on window resize
Bri Prebilic Cole068814d2015-05-14 16:06:38 -0700161 scope.$watchCollection(function () {
Bri Prebilic Colee4d5c6c2015-02-02 15:52:45 -0800162 return {
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700163 h: $window.innerHeight,
164 w: $window.innerWidth
Bri Prebilic Colee4d5c6c2015-02-02 15:52:45 -0800165 };
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700166 }, function () {
Bri Prebilic Coleb3a6afe2015-06-24 14:10:41 -0700167 wsz = fs.windowSize(0, 30);
168 adjustTable(
169 scope.tableData.length,
170 tableElems,
171 wsz.width, wsz.height
172 );
173 });
Bri Prebilic Cole9ca0f9c2015-01-29 15:44:20 -0800174
Bri Prebilic Coleb3a6afe2015-06-24 14:10:41 -0700175 // adjust table when data changes
176 scope.$watchCollection('tableData', function () {
177 adjustTable(
178 scope.tableData.length,
179 tableElems,
180 wsz.width, wsz.height
181 );
182 });
Simon Hunt0c2c4c52015-04-02 17:42:45 -0700183
Bri Prebilic Coleb3a6afe2015-06-24 14:10:41 -0700184 scope.$on('$destroy', function () {
185 cstmWidths = {};
Bri Prebilic Colee4d5c6c2015-02-02 15:52:45 -0800186 });
187 };
Bri Prebilic Cole1f93be22015-02-10 17:16:46 -0800188 }])
189
190 .directive('onosSortableHeader', ['$log', 'IconService',
191 function (_$log_, _is_) {
Bri Prebilic Cole902cb042015-02-11 14:04:15 -0800192 return {
193 scope: {
Bri Prebilic Colebfab9c72015-06-01 14:33:18 -0700194 sortCallback: '&',
195 sortParams: '='
Bri Prebilic Cole902cb042015-02-11 14:04:15 -0800196 },
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -0800197 link: function (scope, element) {
Bri Prebilic Cole902cb042015-02-11 14:04:15 -0800198 $log = _$log_;
199 is = _is_;
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700200 var header = d3.select(element[0]);
Bri Prebilic Cole3d4d01c2015-04-30 13:48:36 -0700201 sortIconAPI = is.sortIcons();
Bri Prebilic Cole1f93be22015-02-10 17:16:46 -0800202
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700203 // when a header is clicked, change its sort direction
Bri Prebilic Cole902cb042015-02-11 14:04:15 -0800204 // and get sorting order to send to the server.
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700205 header.selectAll('td').on('click', function () {
206 var col = d3.select(this);
Bri Prebilic Cole1f93be22015-02-10 17:16:46 -0800207
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700208 if (col.attr('sortable') === '') {
209 updateSortDirection(col);
Bri Prebilic Colebfab9c72015-06-01 14:33:18 -0700210 scope.$apply(function () {
211 scope.sortParams = sortRequestParams();
212 });
213 scope.sortCallback({
214 requestParams: scope.sortParams
Bri Prebilic Cole72eb6db2015-03-30 16:58:53 -0700215 });
Bri Prebilic Cole902cb042015-02-11 14:04:15 -0800216 }
217 });
218 }
219 };
Bri Prebilic Cole3d4d01c2015-04-30 13:48:36 -0700220 }])
221
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700222 .factory('TableService', ['IconService',
Bri Prebilic Cole3d4d01c2015-04-30 13:48:36 -0700223
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700224 function (is) {
Bri Prebilic Cole3d4d01c2015-04-30 13:48:36 -0700225 sortIconAPI = is.sortIcons();
226
227 return {
Bri Prebilic Colec2449762015-05-29 14:30:51 -0700228 resetSort: resetSort
Bri Prebilic Cole3d4d01c2015-04-30 13:48:36 -0700229 };
Bri Prebilic Colee4d5c6c2015-02-02 15:52:45 -0800230 }]);
Bri Prebilic Cole093739a2015-01-23 10:22:50 -0800231
232}());