blob: adae2d02df579ee30d87bf92a3d1f6bb15949a7f [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 = {},
38 sortIconAPI;
Bri Prebilic Cole093739a2015-01-23 10:22:50 -080039
Bri Prebilic Colee568ead2015-05-01 09:51:28 -070040 // Functions for creating a scrolling table body with fixed table header
Bri Prebilic Cole9ca0f9c2015-01-29 15:44:20 -080041
Bri Prebilic Colee568ead2015-05-01 09:51:28 -070042 function _width(elem, width) {
43 elem.style('width', width);
Bri Prebilic Cole08d08d42015-04-01 14:37:02 -070044 }
Bri Prebilic Cole9ca0f9c2015-01-29 15:44:20 -080045
Bri Prebilic Colee568ead2015-05-01 09:51:28 -070046 function defaultSize(table, width) {
47 var thead = table.select('.table-header').select('table'),
48 tbody = table.select('.table-body').select('table'),
49 wpx = width + 'px';
50 _width(thead, wpx);
51 _width(tbody, wpx);
Bri Prebilic Cole08d08d42015-04-01 14:37:02 -070052 }
Thomas Vachuska0fa583c2015-03-30 23:07:41 -070053
Bri Prebilic Colee568ead2015-05-01 09:51:28 -070054 function adjustTable(table, width, height) {
55 var thead = table.select('.table-header').select('table'),
56 tbodyDiv = table.select('.table-body'),
57 tbody = tbodyDiv.select('table'),
58 cstmWidths = {};
Bri Prebilic Cole9ca0f9c2015-01-29 15:44:20 -080059
Bri Prebilic Colee568ead2015-05-01 09:51:28 -070060 function findCstmWidths() {
61 var headers = thead.selectAll('td');
Bri Prebilic Cole08d08d42015-04-01 14:37:02 -070062
Bri Prebilic Colee568ead2015-05-01 09:51:28 -070063 headers.each(function (d, i) {
64 var h = d3.select(this),
65 index = i.toString();
66 if (h.classed(tableIcon)) {
67 cstmWidths[index] = tableIconTdSize + 'px';
68 }
69 if (h.attr(colWidth)) {
70 cstmWidths[index] = h.attr(colWidth);
71 }
72 });
Simon Hunt4deb0e82015-06-10 16:18:25 -070073 if (fs.debugOn('widget')) {
74 $log.debug('Headers with custom widths: ', cstmWidths);
75 }
Bri Prebilic Colee568ead2015-05-01 09:51:28 -070076 }
Bri Prebilic Cole9ca0f9c2015-01-29 15:44:20 -080077
Bri Prebilic Colee568ead2015-05-01 09:51:28 -070078 function setTdWidths(elem) {
79 var tds = elem.selectAll('tr:not(.ignore-width)').selectAll('td');
80 _width(elem, width + 'px');
Thomas Vachuska0fa583c2015-03-30 23:07:41 -070081
Bri Prebilic Colee568ead2015-05-01 09:51:28 -070082 tds.each(function (d, i) {
83 var td = d3.select(this),
84 index = i.toString();
85 if (cstmWidths.hasOwnProperty(index)) {
86 _width(td, cstmWidths[index]);
87 }
88 });
89 }
Bri Prebilic Cole9ca0f9c2015-01-29 15:44:20 -080090
Bri Prebilic Colee568ead2015-05-01 09:51:28 -070091 function setHeight(body) {
92 var h = height - (mast.mastHeight() +
93 fs.noPxStyle(d3.select('.tabular-header'), 'height') +
94 fs.noPxStyle(thead, 'height') + pdg);
95 body.style('height', h + 'px');
96 }
Bri Prebilic Cole08d08d42015-04-01 14:37:02 -070097
Bri Prebilic Colee568ead2015-05-01 09:51:28 -070098 findCstmWidths();
99 setTdWidths(thead);
100 setTdWidths(tbody);
101 setHeight(tbodyDiv);
Bri Prebilic Cole9ca0f9c2015-01-29 15:44:20 -0800102
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700103 cstmWidths = {};
Bri Prebilic Cole9ca0f9c2015-01-29 15:44:20 -0800104 }
105
Bri Prebilic Cole3d4d01c2015-04-30 13:48:36 -0700106 // Functions for sorting table rows by header
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -0800107
Bri Prebilic Cole3d4d01c2015-04-30 13:48:36 -0700108 function updateSortDirection(thElem) {
109 sortIconAPI.sortNone(thElem.select('div'));
110 currCol.div = thElem.append('div');
Bri Prebilic Cole902cb042015-02-11 14:04:15 -0800111 currCol.colId = thElem.attr('colId');
112
113 if (currCol.colId === prevCol.colId) {
Bri Prebilic Cole3d4d01c2015-04-30 13:48:36 -0700114 (currCol.dir === desc) ? currCol.dir = asc : currCol.dir = desc;
115 prevCol.dir = currCol.dir;
Bri Prebilic Cole902cb042015-02-11 14:04:15 -0800116 } else {
Bri Prebilic Cole3d4d01c2015-04-30 13:48:36 -0700117 currCol.dir = asc;
118 prevCol.dir = none;
Bri Prebilic Cole902cb042015-02-11 14:04:15 -0800119 }
Bri Prebilic Cole3d4d01c2015-04-30 13:48:36 -0700120 (currCol.dir === asc) ?
121 sortIconAPI.sortAsc(currCol.div) : sortIconAPI.sortDesc(currCol.div);
Bri Prebilic Cole902cb042015-02-11 14:04:15 -0800122
Bri Prebilic Cole3d4d01c2015-04-30 13:48:36 -0700123 if (prevCol.colId && prevCol.dir === none) {
124 sortIconAPI.sortNone(prevCol.div);
Bri Prebilic Cole902cb042015-02-11 14:04:15 -0800125 }
126
127 prevCol.colId = currCol.colId;
Bri Prebilic Cole3d4d01c2015-04-30 13:48:36 -0700128 prevCol.div = currCol.div;
Bri Prebilic Cole902cb042015-02-11 14:04:15 -0800129 }
130
Bri Prebilic Cole19a32dd2015-03-26 18:00:03 -0700131 function sortRequestParams() {
132 return {
133 sortCol: currCol.colId,
Bri Prebilic Cole3d4d01c2015-04-30 13:48:36 -0700134 sortDir: currCol.dir
Bri Prebilic Cole19a32dd2015-03-26 18:00:03 -0700135 };
Bri Prebilic Cole902cb042015-02-11 14:04:15 -0800136 }
137
Bri Prebilic Colec2449762015-05-29 14:30:51 -0700138 function resetSort() {
Bri Prebilic Cole3d4d01c2015-04-30 13:48:36 -0700139 if (currCol.div) {
140 sortIconAPI.sortNone(currCol.div);
141 }
142 if (prevCol.div) {
143 sortIconAPI.sortNone(prevCol.div);
144 }
145 currCol = {};
146 prevCol = {};
147 }
148
Bri Prebilic Cole093739a2015-01-23 10:22:50 -0800149 angular.module('onosWidget')
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700150 .directive('onosFixedHeader', ['$log','$window',
151 'FnService', 'MastService',
152
153 function (_$log_, _$window_, _fs_, _mast_) {
Bri Prebilic Colee4d5c6c2015-02-02 15:52:45 -0800154 return function (scope, element) {
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700155 $log = _$log_;
Bri Prebilic Colee4d5c6c2015-02-02 15:52:45 -0800156 $window = _$window_;
157 fs = _fs_;
Bri Prebilic Cole264c5ec2015-04-07 10:22:26 -0700158 mast = _mast_;
159
Bri Prebilic Cole068814d2015-05-14 16:06:38 -0700160 var table = d3.select(element[0]),
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800161 canAdjust = false;
Bri Prebilic Cole9ca0f9c2015-01-29 15:44:20 -0800162
Bri Prebilic Cole068814d2015-05-14 16:06:38 -0700163 scope.$watchCollection(function () {
Bri Prebilic Colee4d5c6c2015-02-02 15:52:45 -0800164 return {
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700165 h: $window.innerHeight,
166 w: $window.innerWidth
Bri Prebilic Colee4d5c6c2015-02-02 15:52:45 -0800167 };
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700168 }, function () {
169 var wsz = fs.windowSize(0, 30),
170 wWidth = wsz.width,
171 wHeight = wsz.height;
Bri Prebilic Cole9ca0f9c2015-01-29 15:44:20 -0800172
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700173 if (!scope.tableData.length) {
174 defaultSize(table, wWidth);
175 }
Simon Hunt0c2c4c52015-04-02 17:42:45 -0700176
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800177 scope.$on('LastElement', function () {
178 // only adjust the table once it's completely loaded
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700179 adjustTable(table, wWidth, wHeight);
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800180 canAdjust = true;
181 });
Bri Prebilic Coleb0e66be2015-02-10 10:53:15 -0800182
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800183 if (canAdjust) {
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700184 adjustTable(table, wWidth, wHeight);
Bri Prebilic Coleb0e66be2015-02-10 10:53:15 -0800185 }
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}());