blob: 2fc3277a5f27029f5be9cf26062059c88a61f7ba [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;
Bri Prebilic Cole7c445752015-02-17 14:49:46 -080037 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
Bri Prebilic Cole19a32dd2015-03-26 18:00:03 -0700104 function sortRequestParams() {
105 return {
106 sortCol: currCol.colId,
107 sortDir: (currCol.icon === 'tableColSortAsc' ? 'asc' : 'desc')
108 };
Bri Prebilic Cole902cb042015-02-11 14:04:15 -0800109 }
110
Bri Prebilic Cole093739a2015-01-23 10:22:50 -0800111 angular.module('onosWidget')
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -0800112 .directive('onosFixedHeader', ['$window', 'FnService',
113 function (_$window_, _fs_) {
Bri Prebilic Colee4d5c6c2015-02-02 15:52:45 -0800114 return function (scope, element) {
115 $window = _$window_;
116 fs = _fs_;
117 var w = angular.element($window),
Bri Prebilic Coleb0e66be2015-02-10 10:53:15 -0800118 table = d3.select(element[0]),
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800119 thead = table.select('thead'),
120 tbody = table.select('tbody'),
121 canAdjust = false;
Bri Prebilic Cole9ca0f9c2015-01-29 15:44:20 -0800122
Bri Prebilic Colee4d5c6c2015-02-02 15:52:45 -0800123 scope.$watch(function () {
124 return {
125 h: window.innerHeight,
126 w: window.innerWidth
127 };
128 }, function (newVal) {
Bri Prebilic Colee4d5c6c2015-02-02 15:52:45 -0800129 scope.windowHeight = newVal.h;
130 scope.windowWidth = newVal.w;
Bri Prebilic Cole9ca0f9c2015-01-29 15:44:20 -0800131
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800132 scope.$on('LastElement', function () {
133 // only adjust the table once it's completely loaded
134 fixTable(table, thead, tbody);
135 canAdjust = true;
136 });
Bri Prebilic Coleb0e66be2015-02-10 10:53:15 -0800137
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800138 if (canAdjust) {
Bri Prebilic Coleb0e66be2015-02-10 10:53:15 -0800139 fixTable(table, thead, tbody);
140 }
Bri Prebilic Colee4d5c6c2015-02-02 15:52:45 -0800141 }, true);
Bri Prebilic Cole9ca0f9c2015-01-29 15:44:20 -0800142
Bri Prebilic Colee4d5c6c2015-02-02 15:52:45 -0800143 w.bind('onos-fixed-header', function () {
144 scope.$apply();
145 });
146 };
Bri Prebilic Cole1f93be22015-02-10 17:16:46 -0800147 }])
148
149 .directive('onosSortableHeader', ['$log', 'IconService',
150 function (_$log_, _is_) {
Bri Prebilic Cole902cb042015-02-11 14:04:15 -0800151 return {
152 scope: {
153 ctrlCallback: '&sortCallback'
154 },
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -0800155 link: function (scope, element) {
Bri Prebilic Cole902cb042015-02-11 14:04:15 -0800156 $log = _$log_;
157 is = _is_;
158 var table = d3.select(element[0]),
159 sortIconAPI = is.createSortIcon();
Bri Prebilic Cole1f93be22015-02-10 17:16:46 -0800160
Bri Prebilic Cole902cb042015-02-11 14:04:15 -0800161 // when a header is clicked, change its icon tag
162 // and get sorting order to send to the server.
163 table.selectAll('th').on('click', function () {
164 var thElem = d3.select(this);
Bri Prebilic Cole1f93be22015-02-10 17:16:46 -0800165
Bri Prebilic Cole902cb042015-02-11 14:04:15 -0800166 if (thElem.attr('sortable') === '') {
167 updateSortingIcons(thElem, sortIconAPI);
Bri Prebilic Cole902cb042015-02-11 14:04:15 -0800168 scope.ctrlCallback({
Bri Prebilic Cole72eb6db2015-03-30 16:58:53 -0700169 requestParams: sortRequestParams()
170 });
Bri Prebilic Cole902cb042015-02-11 14:04:15 -0800171 }
172 });
173 }
174 };
Bri Prebilic Colee4d5c6c2015-02-02 15:52:45 -0800175 }]);
Bri Prebilic Cole093739a2015-01-23 10:22:50 -0800176
177}());