blob: 6b94dd28cc40df5dcaf9a101465875a2a8ec26f3 [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 Cole45069382015-04-14 15:21:38 -070028 pdg = 12,
Bri Prebilic Cole08d08d42015-04-01 14:37:02 -070029 colWidth = 'col-width',
30 tableIcon = 'table-icon';
31
32 // internal state
33 var currCol = {},
34 prevCol = {};
Bri Prebilic Cole093739a2015-01-23 10:22:50 -080035
Bri Prebilic Colee4d5c6c2015-02-02 15:52:45 -080036 // Functions for creating a fixed header on a table (Angular Directive)
Bri Prebilic Cole9ca0f9c2015-01-29 15:44:20 -080037
Bri Prebilic Cole08d08d42015-04-01 14:37:02 -070038 function setElemWidth(elem, size) {
39 elem.style('width', size + 'px')
40 }
Bri Prebilic Cole9ca0f9c2015-01-29 15:44:20 -080041
Bri Prebilic Cole08d08d42015-04-01 14:37:02 -070042 function setColWidth(th, td, size) {
43 setElemWidth(th, size);
44 setElemWidth(td, size);
45 }
Thomas Vachuska0fa583c2015-03-30 23:07:41 -070046
Bri Prebilic Cole08d08d42015-04-01 14:37:02 -070047 // count number of headers of
48 // - assigned width,
49 // - icon width,
50 // - and default width
51 // assumes assigned width is not given to icons
Bri Prebilic Cole264c5ec2015-04-07 10:22:26 -070052 // returns the width of all columns that are not icons
53 // or have an assigned width
Bri Prebilic Cole08d08d42015-04-01 14:37:02 -070054 function getDefaultWidth(headers) {
55 var winWidth = fs.windowSize().width,
56 iconCols = 0,
57 regCols = 0,
58 cstmColWidth = 0;
Bri Prebilic Cole9ca0f9c2015-01-29 15:44:20 -080059
Bri Prebilic Cole08d08d42015-04-01 14:37:02 -070060 headers.each(function (d, i) {
61 var thElement = d3.select(this),
62 cstmWidth = thElement.attr(colWidth);
63
64 if (cstmWidth) {
65 cstmColWidth += fs.noPx(cstmWidth);
66 } else if (thElement.classed(tableIcon)) {
67 iconCols += 1;
Thomas Vachuska0fa583c2015-03-30 23:07:41 -070068 } else {
Bri Prebilic Cole08d08d42015-04-01 14:37:02 -070069 regCols += 1;
Thomas Vachuska0fa583c2015-03-30 23:07:41 -070070 }
71 });
Bri Prebilic Cole9ca0f9c2015-01-29 15:44:20 -080072
Bri Prebilic Cole08d08d42015-04-01 14:37:02 -070073 return Math.floor((winWidth - cstmColWidth -
74 (iconCols * tableIconTdSize)) / regCols);
75 }
Thomas Vachuska0fa583c2015-03-30 23:07:41 -070076
Bri Prebilic Cole08d08d42015-04-01 14:37:02 -070077 function setTableWidth(t) {
78 var tHeaders = t.selectAll('th'),
79 defaultColWidth = getDefaultWidth(tHeaders);
Bri Prebilic Cole9ca0f9c2015-01-29 15:44:20 -080080
Bri Prebilic Cole08d08d42015-04-01 14:37:02 -070081 tHeaders.each(function (d, i) {
82 var thElement = d3.select(this),
Bri Prebilic Cole384e8dc2015-04-13 15:51:14 -070083 tr = t.select('tr:nth-of-type(2)'),
84 tdElement = tr.select('td:nth-of-type(' + (i + 1) + ')'),
Bri Prebilic Cole08d08d42015-04-01 14:37:02 -070085 custWidth = thElement.attr(colWidth);
86
87 if (custWidth) {
88 setColWidth(thElement, tdElement, fs.noPx(custWidth));
89 } else if (thElement.classed(tableIcon)) {
90 setColWidth(thElement, tdElement, tableIconTdSize);
Bri Prebilic Cole357f7fd2015-02-12 17:03:42 -080091 } else {
Bri Prebilic Cole08d08d42015-04-01 14:37:02 -070092 setColWidth(thElement, tdElement, defaultColWidth);
Bri Prebilic Colee4d5c6c2015-02-02 15:52:45 -080093 }
Bri Prebilic Cole9ca0f9c2015-01-29 15:44:20 -080094 });
95 }
96
Bri Prebilic Cole264c5ec2015-04-07 10:22:26 -070097 // get the size of the window and then subtract the extra space at the top
98 // to get the height of the table
Bri Prebilic Colee4d5c6c2015-02-02 15:52:45 -080099 function setTableHeight(thead, tbody) {
Bri Prebilic Cole45069382015-04-14 15:21:38 -0700100 var ttlHgt = fs.noPxStyle(d3.select('.tabular-header'), 'height'),
101 thHgt = fs.noPxStyle(thead, 'height'),
102 totalHgt = ttlHgt + thHgt + pdg,
103 tbleHgt = fs.windowSize(mast.mastHeight() + totalHgt).height;
Bri Prebilic Colee4d5c6c2015-02-02 15:52:45 -0800104
Bri Prebilic Cole9ca0f9c2015-01-29 15:44:20 -0800105 thead.style('display', 'block');
Bri Prebilic Cole384e8dc2015-04-13 15:51:14 -0700106 tbody.style({
107 display: 'block',
Bri Prebilic Cole45069382015-04-14 15:21:38 -0700108 height: tbleHgt + 'px',
Bri Prebilic Cole384e8dc2015-04-13 15:51:14 -0700109 overflow: 'auto'
Bri Prebilic Cole9ca0f9c2015-01-29 15:44:20 -0800110 });
111 }
112
Bri Prebilic Cole1f93be22015-02-10 17:16:46 -0800113 function fixTable(t, th, tb) {
Bri Prebilic Colee4d5c6c2015-02-02 15:52:45 -0800114 setTableWidth(t);
Bri Prebilic Cole1f93be22015-02-10 17:16:46 -0800115 setTableHeight(th, tb);
Bri Prebilic Cole9ca0f9c2015-01-29 15:44:20 -0800116 }
117
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -0800118 // Functions for sorting table rows by header and choosing appropriate icon
119
Bri Prebilic Cole902cb042015-02-11 14:04:15 -0800120 function updateSortingIcons(thElem, api) {
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -0800121 var div;
Bri Prebilic Cole902cb042015-02-11 14:04:15 -0800122 currCol.colId = thElem.attr('colId');
123
124 if (currCol.colId === prevCol.colId) {
Bri Prebilic Coleab582b82015-04-14 15:08:22 -0700125 (currCol.icon === 'downArrow') ?
126 currCol.icon = 'upArrow' :
127 currCol.icon = 'downArrow';
Bri Prebilic Cole902cb042015-02-11 14:04:15 -0800128 prevCol.icon = currCol.icon;
129 } else {
Bri Prebilic Coleab582b82015-04-14 15:08:22 -0700130 currCol.icon = 'upArrow';
Bri Prebilic Cole902cb042015-02-11 14:04:15 -0800131 prevCol.icon = 'tableColSortNone';
132 }
133
134 div = thElem.select('div');
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -0800135 api.sortNone(div);
Bri Prebilic Cole902cb042015-02-11 14:04:15 -0800136 div = thElem.append('div');
137
Bri Prebilic Coleab582b82015-04-14 15:08:22 -0700138 if (currCol.icon === 'upArrow') {
Bri Prebilic Cole902cb042015-02-11 14:04:15 -0800139 api.sortAsc(div);
140 } else {
141 api.sortDesc(div);
142 }
143
144 if (prevCol.colId !== undefined &&
145 prevCol.icon === 'tableColSortNone') {
146 api.sortNone(prevCol.elem.select('div'));
147 }
148
149 prevCol.colId = currCol.colId;
150 prevCol.elem = thElem;
151 }
152
Bri Prebilic Cole19a32dd2015-03-26 18:00:03 -0700153 function sortRequestParams() {
154 return {
155 sortCol: currCol.colId,
Bri Prebilic Coleab582b82015-04-14 15:08:22 -0700156 sortDir: (currCol.icon === 'upArrow' ? 'asc' : 'desc')
Bri Prebilic Cole19a32dd2015-03-26 18:00:03 -0700157 };
Bri Prebilic Cole902cb042015-02-11 14:04:15 -0800158 }
159
Bri Prebilic Cole093739a2015-01-23 10:22:50 -0800160 angular.module('onosWidget')
Bri Prebilic Cole264c5ec2015-04-07 10:22:26 -0700161 .directive('onosFixedHeader', ['$window', 'FnService', 'MastService',
162 function (_$window_, _fs_, _mast_) {
Bri Prebilic Colee4d5c6c2015-02-02 15:52:45 -0800163 return function (scope, element) {
164 $window = _$window_;
165 fs = _fs_;
Bri Prebilic Cole264c5ec2015-04-07 10:22:26 -0700166 mast = _mast_;
167
Bri Prebilic Colee4d5c6c2015-02-02 15:52:45 -0800168 var w = angular.element($window),
Bri Prebilic Coleb0e66be2015-02-10 10:53:15 -0800169 table = d3.select(element[0]),
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800170 thead = table.select('thead'),
171 tbody = table.select('tbody'),
172 canAdjust = false;
Bri Prebilic Cole9ca0f9c2015-01-29 15:44:20 -0800173
Bri Prebilic Colee4d5c6c2015-02-02 15:52:45 -0800174 scope.$watch(function () {
175 return {
176 h: window.innerHeight,
177 w: window.innerWidth
178 };
179 }, function (newVal) {
Simon Hunt0c2c4c52015-04-02 17:42:45 -0700180 var wsz = fs.windowSize(0, 30);
Bri Prebilic Colee4d5c6c2015-02-02 15:52:45 -0800181 scope.windowHeight = newVal.h;
182 scope.windowWidth = newVal.w;
Bri Prebilic Cole9ca0f9c2015-01-29 15:44:20 -0800183
Simon Hunt0c2c4c52015-04-02 17:42:45 -0700184 // default table size in case no data elements
185 table.style('width', wsz.width + 'px');
186
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800187 scope.$on('LastElement', function () {
188 // only adjust the table once it's completely loaded
189 fixTable(table, thead, tbody);
190 canAdjust = true;
191 });
Bri Prebilic Coleb0e66be2015-02-10 10:53:15 -0800192
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800193 if (canAdjust) {
Bri Prebilic Coleb0e66be2015-02-10 10:53:15 -0800194 fixTable(table, thead, tbody);
195 }
Bri Prebilic Colee4d5c6c2015-02-02 15:52:45 -0800196 }, true);
Bri Prebilic Cole9ca0f9c2015-01-29 15:44:20 -0800197
Bri Prebilic Colee4d5c6c2015-02-02 15:52:45 -0800198 w.bind('onos-fixed-header', function () {
199 scope.$apply();
200 });
201 };
Bri Prebilic Cole1f93be22015-02-10 17:16:46 -0800202 }])
203
204 .directive('onosSortableHeader', ['$log', 'IconService',
205 function (_$log_, _is_) {
Bri Prebilic Cole902cb042015-02-11 14:04:15 -0800206 return {
207 scope: {
208 ctrlCallback: '&sortCallback'
209 },
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -0800210 link: function (scope, element) {
Bri Prebilic Cole902cb042015-02-11 14:04:15 -0800211 $log = _$log_;
212 is = _is_;
213 var table = d3.select(element[0]),
214 sortIconAPI = is.createSortIcon();
Bri Prebilic Cole1f93be22015-02-10 17:16:46 -0800215
Bri Prebilic Cole902cb042015-02-11 14:04:15 -0800216 // when a header is clicked, change its icon tag
217 // and get sorting order to send to the server.
218 table.selectAll('th').on('click', function () {
219 var thElem = d3.select(this);
Bri Prebilic Cole1f93be22015-02-10 17:16:46 -0800220
Bri Prebilic Cole902cb042015-02-11 14:04:15 -0800221 if (thElem.attr('sortable') === '') {
222 updateSortingIcons(thElem, sortIconAPI);
Bri Prebilic Cole902cb042015-02-11 14:04:15 -0800223 scope.ctrlCallback({
Bri Prebilic Cole72eb6db2015-03-30 16:58:53 -0700224 requestParams: sortRequestParams()
225 });
Bri Prebilic Cole902cb042015-02-11 14:04:15 -0800226 }
227 });
228 }
229 };
Bri Prebilic Colee4d5c6c2015-02-02 15:52:45 -0800230 }]);
Bri Prebilic Cole093739a2015-01-23 10:22:50 -0800231
232}());