blob: b2e1cc82e7411165984f24d217d6c6128c8c7ca3 [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 });
73 $log.debug('Headers with custom widths: ', cstmWidths);
74 }
Bri Prebilic Cole9ca0f9c2015-01-29 15:44:20 -080075
Bri Prebilic Colee568ead2015-05-01 09:51:28 -070076 function setTdWidths(elem) {
77 var tds = elem.selectAll('tr:not(.ignore-width)').selectAll('td');
78 _width(elem, width + 'px');
Thomas Vachuska0fa583c2015-03-30 23:07:41 -070079
Bri Prebilic Colee568ead2015-05-01 09:51:28 -070080 tds.each(function (d, i) {
81 var td = d3.select(this),
82 index = i.toString();
83 if (cstmWidths.hasOwnProperty(index)) {
84 _width(td, cstmWidths[index]);
85 }
86 });
87 }
Bri Prebilic Cole9ca0f9c2015-01-29 15:44:20 -080088
Bri Prebilic Colee568ead2015-05-01 09:51:28 -070089 function setHeight(body) {
90 var h = height - (mast.mastHeight() +
91 fs.noPxStyle(d3.select('.tabular-header'), 'height') +
92 fs.noPxStyle(thead, 'height') + pdg);
93 body.style('height', h + 'px');
94 }
Bri Prebilic Cole08d08d42015-04-01 14:37:02 -070095
Bri Prebilic Colee568ead2015-05-01 09:51:28 -070096 findCstmWidths();
97 setTdWidths(thead);
98 setTdWidths(tbody);
99 setHeight(tbodyDiv);
Bri Prebilic Cole9ca0f9c2015-01-29 15:44:20 -0800100
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700101 cstmWidths = {};
Bri Prebilic Cole9ca0f9c2015-01-29 15:44:20 -0800102 }
103
Bri Prebilic Cole3d4d01c2015-04-30 13:48:36 -0700104 // Functions for sorting table rows by header
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -0800105
Bri Prebilic Cole3d4d01c2015-04-30 13:48:36 -0700106 function updateSortDirection(thElem) {
107 sortIconAPI.sortNone(thElem.select('div'));
108 currCol.div = thElem.append('div');
Bri Prebilic Cole902cb042015-02-11 14:04:15 -0800109 currCol.colId = thElem.attr('colId');
110
111 if (currCol.colId === prevCol.colId) {
Bri Prebilic Cole3d4d01c2015-04-30 13:48:36 -0700112 (currCol.dir === desc) ? currCol.dir = asc : currCol.dir = desc;
113 prevCol.dir = currCol.dir;
Bri Prebilic Cole902cb042015-02-11 14:04:15 -0800114 } else {
Bri Prebilic Cole3d4d01c2015-04-30 13:48:36 -0700115 currCol.dir = asc;
116 prevCol.dir = none;
Bri Prebilic Cole902cb042015-02-11 14:04:15 -0800117 }
Bri Prebilic Cole3d4d01c2015-04-30 13:48:36 -0700118 (currCol.dir === asc) ?
119 sortIconAPI.sortAsc(currCol.div) : sortIconAPI.sortDesc(currCol.div);
Bri Prebilic Cole902cb042015-02-11 14:04:15 -0800120
Bri Prebilic Cole3d4d01c2015-04-30 13:48:36 -0700121 if (prevCol.colId && prevCol.dir === none) {
122 sortIconAPI.sortNone(prevCol.div);
Bri Prebilic Cole902cb042015-02-11 14:04:15 -0800123 }
124
125 prevCol.colId = currCol.colId;
Bri Prebilic Cole3d4d01c2015-04-30 13:48:36 -0700126 prevCol.div = currCol.div;
Bri Prebilic Cole902cb042015-02-11 14:04:15 -0800127 }
128
Bri Prebilic Cole19a32dd2015-03-26 18:00:03 -0700129 function sortRequestParams() {
130 return {
131 sortCol: currCol.colId,
Bri Prebilic Cole3d4d01c2015-04-30 13:48:36 -0700132 sortDir: currCol.dir
Bri Prebilic Cole19a32dd2015-03-26 18:00:03 -0700133 };
Bri Prebilic Cole902cb042015-02-11 14:04:15 -0800134 }
135
Bri Prebilic Colec2449762015-05-29 14:30:51 -0700136 function resetSort() {
Bri Prebilic Cole3d4d01c2015-04-30 13:48:36 -0700137 if (currCol.div) {
138 sortIconAPI.sortNone(currCol.div);
139 }
140 if (prevCol.div) {
141 sortIconAPI.sortNone(prevCol.div);
142 }
143 currCol = {};
144 prevCol = {};
145 }
146
Bri Prebilic Cole093739a2015-01-23 10:22:50 -0800147 angular.module('onosWidget')
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700148 .directive('onosFixedHeader', ['$log','$window',
149 'FnService', 'MastService',
150
151 function (_$log_, _$window_, _fs_, _mast_) {
Bri Prebilic Colee4d5c6c2015-02-02 15:52:45 -0800152 return function (scope, element) {
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700153 $log = _$log_;
Bri Prebilic Colee4d5c6c2015-02-02 15:52:45 -0800154 $window = _$window_;
155 fs = _fs_;
Bri Prebilic Cole264c5ec2015-04-07 10:22:26 -0700156 mast = _mast_;
157
Bri Prebilic Cole068814d2015-05-14 16:06:38 -0700158 var table = d3.select(element[0]),
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800159 canAdjust = false;
Bri Prebilic Cole9ca0f9c2015-01-29 15:44:20 -0800160
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 () {
167 var wsz = fs.windowSize(0, 30),
168 wWidth = wsz.width,
169 wHeight = wsz.height;
Bri Prebilic Cole9ca0f9c2015-01-29 15:44:20 -0800170
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700171 if (!scope.tableData.length) {
172 defaultSize(table, wWidth);
173 }
Simon Hunt0c2c4c52015-04-02 17:42:45 -0700174
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800175 scope.$on('LastElement', function () {
176 // only adjust the table once it's completely loaded
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700177 adjustTable(table, wWidth, wHeight);
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800178 canAdjust = true;
179 });
Bri Prebilic Coleb0e66be2015-02-10 10:53:15 -0800180
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800181 if (canAdjust) {
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700182 adjustTable(table, wWidth, wHeight);
Bri Prebilic Coleb0e66be2015-02-10 10:53:15 -0800183 }
Bri Prebilic Colee4d5c6c2015-02-02 15:52:45 -0800184 });
185 };
Bri Prebilic Cole1f93be22015-02-10 17:16:46 -0800186 }])
187
188 .directive('onosSortableHeader', ['$log', 'IconService',
189 function (_$log_, _is_) {
Bri Prebilic Cole902cb042015-02-11 14:04:15 -0800190 return {
191 scope: {
192 ctrlCallback: '&sortCallback'
193 },
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -0800194 link: function (scope, element) {
Bri Prebilic Cole902cb042015-02-11 14:04:15 -0800195 $log = _$log_;
196 is = _is_;
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700197 var header = d3.select(element[0]);
Bri Prebilic Cole3d4d01c2015-04-30 13:48:36 -0700198 sortIconAPI = is.sortIcons();
Bri Prebilic Cole1f93be22015-02-10 17:16:46 -0800199
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700200 // when a header is clicked, change its sort direction
Bri Prebilic Cole902cb042015-02-11 14:04:15 -0800201 // and get sorting order to send to the server.
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700202 header.selectAll('td').on('click', function () {
203 var col = d3.select(this);
Bri Prebilic Cole1f93be22015-02-10 17:16:46 -0800204
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700205 if (col.attr('sortable') === '') {
206 updateSortDirection(col);
Bri Prebilic Cole902cb042015-02-11 14:04:15 -0800207 scope.ctrlCallback({
Bri Prebilic Cole72eb6db2015-03-30 16:58:53 -0700208 requestParams: sortRequestParams()
209 });
Bri Prebilic Cole902cb042015-02-11 14:04:15 -0800210 }
211 });
212 }
213 };
Bri Prebilic Cole3d4d01c2015-04-30 13:48:36 -0700214 }])
215
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700216 .factory('TableService', ['IconService',
Bri Prebilic Cole3d4d01c2015-04-30 13:48:36 -0700217
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700218 function (is) {
Bri Prebilic Cole3d4d01c2015-04-30 13:48:36 -0700219 sortIconAPI = is.sortIcons();
220
221 return {
Bri Prebilic Colec2449762015-05-29 14:30:51 -0700222 resetSort: resetSort
Bri Prebilic Cole3d4d01c2015-04-30 13:48:36 -0700223 };
Bri Prebilic Colee4d5c6c2015-02-02 15:52:45 -0800224 }]);
Bri Prebilic Cole093739a2015-01-23 10:22:50 -0800225
226}());