blob: 285d21d00ca2824cc9e3900a0cd2f5fef706003d [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) {
Thomas Vachuska0fa583c2015-03-30 23:07:41 -070032 var tHeaders, tdElement, colWidth, numIcons, numNonIcons,
Bri Prebilic Colee4d5c6c2015-02-02 15:52:45 -080033 winWidth = fs.windowSize().width;
Bri Prebilic Cole9ca0f9c2015-01-29 15:44:20 -080034
35 tHeaders = t.selectAll('th');
Thomas Vachuska0fa583c2015-03-30 23:07:41 -070036 numIcons = 0;
37 numNonIcons = 0;
38
39 // FIXME: This should observe custom-set width from the HTML
Bri Prebilic Cole9ca0f9c2015-01-29 15:44:20 -080040
Bri Prebilic Colee4d5c6c2015-02-02 15:52:45 -080041 tHeaders.each(function(thElement, index) {
Bri Prebilic Cole9ca0f9c2015-01-29 15:44:20 -080042 thElement = d3.select(this);
Thomas Vachuska0fa583c2015-03-30 23:07:41 -070043 if (thElement.classed('table-icon')) {
44 numIcons = numIcons + 1;
45 } else {
46 numNonIcons = numNonIcons + 1;
47 }
48 });
Bri Prebilic Cole9ca0f9c2015-01-29 15:44:20 -080049
Thomas Vachuska0fa583c2015-03-30 23:07:41 -070050 colWidth = Math.floor((winWidth - (numIcons * tableIconTdSize)) / numNonIcons);
51
52 tHeaders.each(function(thElement, index) {
53 thElement = d3.select(this);
Bri Prebilic Cole9ca0f9c2015-01-29 15:44:20 -080054 tdElement = t.select('td:nth-of-type(' + (index + 1) + ')');
Bri Prebilic Cole9ca0f9c2015-01-29 15:44:20 -080055
Thomas Vachuska0fa583c2015-03-30 23:07:41 -070056 if (thElement.classed('table-icon')) {
Bri Prebilic Cole357f7fd2015-02-12 17:03:42 -080057 thElement.style('width', tableIconTdSize + 'px');
58 tdElement.style('width', tableIconTdSize + 'px');
59 } else {
Bri Prebilic Colee4d5c6c2015-02-02 15:52:45 -080060 thElement.style('width', colWidth + 'px');
61 tdElement.style('width', colWidth + 'px');
62 }
Bri Prebilic Cole9ca0f9c2015-01-29 15:44:20 -080063 });
64 }
65
Bri Prebilic Colee4d5c6c2015-02-02 15:52:45 -080066 function setTableHeight(thead, tbody) {
67 var winHeight = fs.windowSize().height;
68
Bri Prebilic Cole9ca0f9c2015-01-29 15:44:20 -080069 thead.style('display', 'block');
70 tbody.style({'display': 'block',
Bri Prebilic Colee4d5c6c2015-02-02 15:52:45 -080071 'height': ((winHeight - bottomMargin) + 'px'),
Bri Prebilic Cole9ca0f9c2015-01-29 15:44:20 -080072 'overflow': 'auto'
73 });
74 }
75
Bri Prebilic Cole1f93be22015-02-10 17:16:46 -080076 function fixTable(t, th, tb) {
Bri Prebilic Colee4d5c6c2015-02-02 15:52:45 -080077 setTableWidth(t);
Bri Prebilic Cole1f93be22015-02-10 17:16:46 -080078 setTableHeight(th, tb);
Bri Prebilic Cole9ca0f9c2015-01-29 15:44:20 -080079 }
80
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -080081 // Functions for sorting table rows by header and choosing appropriate icon
82
Bri Prebilic Cole902cb042015-02-11 14:04:15 -080083 function updateSortingIcons(thElem, api) {
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -080084 var div;
Bri Prebilic Cole902cb042015-02-11 14:04:15 -080085 currCol.colId = thElem.attr('colId');
86
87 if (currCol.colId === prevCol.colId) {
88 (currCol.icon === 'tableColSortDesc') ?
89 currCol.icon = 'tableColSortAsc' :
90 currCol.icon = 'tableColSortDesc';
91 prevCol.icon = currCol.icon;
92 } else {
93 currCol.icon = 'tableColSortAsc';
94 prevCol.icon = 'tableColSortNone';
95 }
96
97 div = thElem.select('div');
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -080098 api.sortNone(div);
Bri Prebilic Cole902cb042015-02-11 14:04:15 -080099 div = thElem.append('div');
100
101 if (currCol.icon === 'tableColSortAsc') {
102 api.sortAsc(div);
103 } else {
104 api.sortDesc(div);
105 }
106
107 if (prevCol.colId !== undefined &&
108 prevCol.icon === 'tableColSortNone') {
109 api.sortNone(prevCol.elem.select('div'));
110 }
111
112 prevCol.colId = currCol.colId;
113 prevCol.elem = thElem;
114 }
115
Bri Prebilic Cole19a32dd2015-03-26 18:00:03 -0700116 function sortRequestParams() {
117 return {
118 sortCol: currCol.colId,
119 sortDir: (currCol.icon === 'tableColSortAsc' ? 'asc' : 'desc')
120 };
Bri Prebilic Cole902cb042015-02-11 14:04:15 -0800121 }
122
Bri Prebilic Cole093739a2015-01-23 10:22:50 -0800123 angular.module('onosWidget')
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -0800124 .directive('onosFixedHeader', ['$window', 'FnService',
125 function (_$window_, _fs_) {
Bri Prebilic Colee4d5c6c2015-02-02 15:52:45 -0800126 return function (scope, element) {
127 $window = _$window_;
128 fs = _fs_;
129 var w = angular.element($window),
Bri Prebilic Coleb0e66be2015-02-10 10:53:15 -0800130 table = d3.select(element[0]),
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800131 thead = table.select('thead'),
132 tbody = table.select('tbody'),
133 canAdjust = false;
Bri Prebilic Cole9ca0f9c2015-01-29 15:44:20 -0800134
Bri Prebilic Colee4d5c6c2015-02-02 15:52:45 -0800135 scope.$watch(function () {
136 return {
137 h: window.innerHeight,
138 w: window.innerWidth
139 };
140 }, function (newVal) {
Bri Prebilic Colee4d5c6c2015-02-02 15:52:45 -0800141 scope.windowHeight = newVal.h;
142 scope.windowWidth = newVal.w;
Bri Prebilic Cole9ca0f9c2015-01-29 15:44:20 -0800143
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800144 scope.$on('LastElement', function () {
145 // only adjust the table once it's completely loaded
146 fixTable(table, thead, tbody);
147 canAdjust = true;
148 });
Bri Prebilic Coleb0e66be2015-02-10 10:53:15 -0800149
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800150 if (canAdjust) {
Bri Prebilic Coleb0e66be2015-02-10 10:53:15 -0800151 fixTable(table, thead, tbody);
152 }
Bri Prebilic Colee4d5c6c2015-02-02 15:52:45 -0800153 }, true);
Bri Prebilic Cole9ca0f9c2015-01-29 15:44:20 -0800154
Bri Prebilic Colee4d5c6c2015-02-02 15:52:45 -0800155 w.bind('onos-fixed-header', function () {
156 scope.$apply();
157 });
158 };
Bri Prebilic Cole1f93be22015-02-10 17:16:46 -0800159 }])
160
161 .directive('onosSortableHeader', ['$log', 'IconService',
162 function (_$log_, _is_) {
Bri Prebilic Cole902cb042015-02-11 14:04:15 -0800163 return {
164 scope: {
165 ctrlCallback: '&sortCallback'
166 },
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -0800167 link: function (scope, element) {
Bri Prebilic Cole902cb042015-02-11 14:04:15 -0800168 $log = _$log_;
169 is = _is_;
170 var table = d3.select(element[0]),
171 sortIconAPI = is.createSortIcon();
Bri Prebilic Cole1f93be22015-02-10 17:16:46 -0800172
Bri Prebilic Cole902cb042015-02-11 14:04:15 -0800173 // when a header is clicked, change its icon tag
174 // and get sorting order to send to the server.
175 table.selectAll('th').on('click', function () {
176 var thElem = d3.select(this);
Bri Prebilic Cole1f93be22015-02-10 17:16:46 -0800177
Bri Prebilic Cole902cb042015-02-11 14:04:15 -0800178 if (thElem.attr('sortable') === '') {
179 updateSortingIcons(thElem, sortIconAPI);
Bri Prebilic Cole902cb042015-02-11 14:04:15 -0800180 scope.ctrlCallback({
Bri Prebilic Cole72eb6db2015-03-30 16:58:53 -0700181 requestParams: sortRequestParams()
182 });
Bri Prebilic Cole902cb042015-02-11 14:04:15 -0800183 }
184 });
185 }
186 };
Bri Prebilic Colee4d5c6c2015-02-02 15:52:45 -0800187 }]);
Bri Prebilic Cole093739a2015-01-23 10:22:50 -0800188
189}());