blob: 1b999ae1f91c98780e915a1eeb89deb61c4fb1fe [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 Cole1f93be22015-02-10 17:16:46 -080023 var $log, $window, fs, is,
Bri Prebilic Cole902cb042015-02-11 14:04:15 -080024 currCol = {},
25 prevCol = {},
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -080026 tableIconTdSize = 33,
Bri Prebilic Colee4d5c6c2015-02-02 15:52:45 -080027 bottomMargin = 200;
Bri Prebilic Cole093739a2015-01-23 10:22:50 -080028
Bri Prebilic Colee4d5c6c2015-02-02 15:52:45 -080029 // Functions for creating a fixed header on a table (Angular Directive)
Bri Prebilic Cole9ca0f9c2015-01-29 15:44:20 -080030
Bri Prebilic Colee4d5c6c2015-02-02 15:52:45 -080031 function setTableWidth(t) {
32 var tHeaders, tdElement, colWidth, numCols,
33 winWidth = fs.windowSize().width;
Bri Prebilic Cole9ca0f9c2015-01-29 15:44:20 -080034
35 tHeaders = t.selectAll('th');
Bri Prebilic Colee4d5c6c2015-02-02 15:52:45 -080036 numCols = tHeaders[0].length;
37 colWidth = Math.floor(winWidth/numCols);
Bri Prebilic Cole9ca0f9c2015-01-29 15:44:20 -080038
Bri Prebilic Colee4d5c6c2015-02-02 15:52:45 -080039 tHeaders.each(function(thElement, index) {
Bri Prebilic Cole9ca0f9c2015-01-29 15:44:20 -080040 thElement = d3.select(this);
41
42 tdElement = t.select('td:nth-of-type(' + (index + 1) + ')');
Bri Prebilic Cole9ca0f9c2015-01-29 15:44:20 -080043
Bri Prebilic Cole357f7fd2015-02-12 17:03:42 -080044 if (tdElement.classed('table-icon')) {
45 thElement.style('width', tableIconTdSize + 'px');
46 tdElement.style('width', tableIconTdSize + 'px');
47 } else {
Bri Prebilic Colee4d5c6c2015-02-02 15:52:45 -080048 thElement.style('width', colWidth + 'px');
49 tdElement.style('width', colWidth + 'px');
50 }
Bri Prebilic Cole9ca0f9c2015-01-29 15:44:20 -080051 });
52 }
53
Bri Prebilic Colee4d5c6c2015-02-02 15:52:45 -080054 function setTableHeight(thead, tbody) {
55 var winHeight = fs.windowSize().height;
56
Bri Prebilic Cole9ca0f9c2015-01-29 15:44:20 -080057 thead.style('display', 'block');
58 tbody.style({'display': 'block',
Bri Prebilic Colee4d5c6c2015-02-02 15:52:45 -080059 'height': ((winHeight - bottomMargin) + 'px'),
Bri Prebilic Cole9ca0f9c2015-01-29 15:44:20 -080060 'overflow': 'auto'
61 });
62 }
63
Bri Prebilic Cole1f93be22015-02-10 17:16:46 -080064 function fixTable(t, th, tb) {
Bri Prebilic Colee4d5c6c2015-02-02 15:52:45 -080065 setTableWidth(t);
Bri Prebilic Cole1f93be22015-02-10 17:16:46 -080066 setTableHeight(th, tb);
Bri Prebilic Cole9ca0f9c2015-01-29 15:44:20 -080067 }
68
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -080069 // Functions for sorting table rows by header and choosing appropriate icon
70
Bri Prebilic Cole902cb042015-02-11 14:04:15 -080071 function updateSortingIcons(thElem, api) {
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -080072 var div;
Bri Prebilic Cole902cb042015-02-11 14:04:15 -080073 currCol.colId = thElem.attr('colId');
74
75 if (currCol.colId === prevCol.colId) {
76 (currCol.icon === 'tableColSortDesc') ?
77 currCol.icon = 'tableColSortAsc' :
78 currCol.icon = 'tableColSortDesc';
79 prevCol.icon = currCol.icon;
80 } else {
81 currCol.icon = 'tableColSortAsc';
82 prevCol.icon = 'tableColSortNone';
83 }
84
85 div = thElem.select('div');
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -080086 api.sortNone(div);
Bri Prebilic Cole902cb042015-02-11 14:04:15 -080087 div = thElem.append('div');
88
89 if (currCol.icon === 'tableColSortAsc') {
90 api.sortAsc(div);
91 } else {
92 api.sortDesc(div);
93 }
94
95 if (prevCol.colId !== undefined &&
96 prevCol.icon === 'tableColSortNone') {
97 api.sortNone(prevCol.elem.select('div'));
98 }
99
100 prevCol.colId = currCol.colId;
101 prevCol.elem = thElem;
102 }
103
104 function generateQueryParams() {
105 var queryString = '?sortCol=' + currCol.colId + '&sortDir=';
106
107 if(currCol.icon === 'tableColSortAsc') {
108 queryString = queryString + 'asc';
109 } else {
110 queryString = queryString + 'desc';
111 }
112
113 return queryString;
114 }
115
Bri Prebilic Cole093739a2015-01-23 10:22:50 -0800116 angular.module('onosWidget')
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -0800117 .directive('onosFixedHeader', ['$window', 'FnService',
118 function (_$window_, _fs_) {
Bri Prebilic Colee4d5c6c2015-02-02 15:52:45 -0800119 return function (scope, element) {
120 $window = _$window_;
121 fs = _fs_;
122 var w = angular.element($window),
Bri Prebilic Coleb0e66be2015-02-10 10:53:15 -0800123 table = d3.select(element[0]),
124 shouldResize = false;
Bri Prebilic Cole9ca0f9c2015-01-29 15:44:20 -0800125
Bri Prebilic Colee4d5c6c2015-02-02 15:52:45 -0800126 scope.$watch(function () {
127 return {
128 h: window.innerHeight,
129 w: window.innerWidth
130 };
131 }, function (newVal) {
132 var thead = table.select('thead'),
133 tbody = table.select('tbody');
Bri Prebilic Cole9ca0f9c2015-01-29 15:44:20 -0800134
Bri Prebilic Colee4d5c6c2015-02-02 15:52:45 -0800135 scope.windowHeight = newVal.h;
136 scope.windowWidth = newVal.w;
Bri Prebilic Cole9ca0f9c2015-01-29 15:44:20 -0800137
Bri Prebilic Colee4d5c6c2015-02-02 15:52:45 -0800138 scope.setTableHW = function () {
Bri Prebilic Coleb0e66be2015-02-10 10:53:15 -0800139 scope.$on('LastElement', function (event) {
Bri Prebilic Cole1f93be22015-02-10 17:16:46 -0800140 // only adjust the table once it's completely loaded
Bri Prebilic Colee4d5c6c2015-02-02 15:52:45 -0800141 fixTable(table, thead, tbody);
Bri Prebilic Coleb0e66be2015-02-10 10:53:15 -0800142 shouldResize = true;
143 });
Bri Prebilic Colee4d5c6c2015-02-02 15:52:45 -0800144 };
Bri Prebilic Coleb0e66be2015-02-10 10:53:15 -0800145
146 if (shouldResize) {
147 fixTable(table, thead, tbody);
148 }
149
Bri Prebilic Colee4d5c6c2015-02-02 15:52:45 -0800150 }, true);
Bri Prebilic Cole9ca0f9c2015-01-29 15:44:20 -0800151
Bri Prebilic Colee4d5c6c2015-02-02 15:52:45 -0800152 w.bind('onos-fixed-header', function () {
153 scope.$apply();
154 });
155 };
156
Bri Prebilic Cole1f93be22015-02-10 17:16:46 -0800157 }])
158
159 .directive('onosSortableHeader', ['$log', 'IconService',
160 function (_$log_, _is_) {
Bri Prebilic Cole902cb042015-02-11 14:04:15 -0800161 return {
162 scope: {
163 ctrlCallback: '&sortCallback'
164 },
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -0800165 link: function (scope, element) {
Bri Prebilic Cole902cb042015-02-11 14:04:15 -0800166 $log = _$log_;
167 is = _is_;
168 var table = d3.select(element[0]),
169 sortIconAPI = is.createSortIcon();
Bri Prebilic Cole1f93be22015-02-10 17:16:46 -0800170
Bri Prebilic Cole902cb042015-02-11 14:04:15 -0800171 // when a header is clicked, change its icon tag
172 // and get sorting order to send to the server.
173 table.selectAll('th').on('click', function () {
174 var thElem = d3.select(this);
Bri Prebilic Cole1f93be22015-02-10 17:16:46 -0800175
Bri Prebilic Cole902cb042015-02-11 14:04:15 -0800176 if (thElem.attr('sortable') === '') {
177 updateSortingIcons(thElem, sortIconAPI);
Bri Prebilic Cole902cb042015-02-11 14:04:15 -0800178 scope.ctrlCallback({
179 urlSuffix: generateQueryParams()
180 });
181 }
182 });
183 }
184 };
Bri Prebilic Colee4d5c6c2015-02-02 15:52:45 -0800185 }]);
Bri Prebilic Cole093739a2015-01-23 10:22:50 -0800186
187}());