blob: 24161bbb0eeb61d500e9f279bf42da69d265e4af [file] [log] [blame]
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -07001/*
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
23 // injected refs
Bri Prebilic Cole6e1b4a52015-08-03 17:10:44 -070024 var $log, $interval, $timeout, fs, wss;
Bri Prebilic Colebfab9c72015-06-01 14:33:18 -070025
26 // constants
Bri Prebilic Cole6e1b4a52015-08-03 17:10:44 -070027 var refreshInterval = 2000,
28 loadingWait = 500;
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -070029
30 // example params to buildTable:
31 // {
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -070032 // scope: $scope, <- controller scope
33 // tag: 'device', <- table identifier
Bri Prebilic Cole0bc4de22015-07-20 17:07:55 -070034 // selCb: selCb, <- row selection callback (optional)
35 // respCb: respCb, <- websocket response callback (optional)
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -070036 // query: params <- query parameters in URL (optional)
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -070037 // }
Simon Hunt35d18882015-04-02 20:16:26 -070038 // Note: selCb() is passed the row data model of the selected row,
39 // or null when no row is selected.
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -070040 // Note: query is always an object (empty or containing properties)
41 // it comes from $location.search()
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -070042
43 function buildTable(o) {
44 var handlers = {},
45 root = o.tag + 's',
46 req = o.tag + 'DataRequest',
Thomas Vachuska619c5382015-04-02 13:41:47 -070047 resp = o.tag + 'DataResponse',
Bri Prebilic Colebfab9c72015-06-01 14:33:18 -070048 onSel = fs.isF(o.selCb),
Bri Prebilic Cole522e7562015-06-22 15:56:25 -070049 onResp = fs.isF(o.respCb),
Bri Prebilic Cole0bc4de22015-07-20 17:07:55 -070050 oldTableData = [],
Bri Prebilic Cole6e1b4a52015-08-03 17:10:44 -070051 loaded = false,
52 refreshPromise, loadingPromise;
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -070053
Bri Prebilic Colee568ead2015-05-01 09:51:28 -070054 o.scope.tableData = [];
Bri Prebilic Cole0bc4de22015-07-20 17:07:55 -070055 o.scope.changedData = [];
Bri Prebilic Colebfab9c72015-06-01 14:33:18 -070056 o.scope.sortParams = {};
Bri Prebilic Cole6e1b4a52015-08-03 17:10:44 -070057 o.scope.loading = true;
Bri Prebilic Cole41d67652015-06-02 10:23:04 -070058 o.scope.autoRefresh = true;
Bri Prebilic Coleeef67ae2015-07-01 16:26:59 -070059 o.scope.autoRefreshTip = 'Toggle auto refresh';
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -070060
Bri Prebilic Cole0bc4de22015-07-20 17:07:55 -070061 // === websocket functions --------------------
62 // response
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -070063 function respCb(data) {
Bri Prebilic Cole6e1b4a52015-08-03 17:10:44 -070064 loaded = true;
65 o.scope.loading = false;
Bri Prebilic Colee568ead2015-05-01 09:51:28 -070066 o.scope.tableData = data[root];
Bri Prebilic Cole522e7562015-06-22 15:56:25 -070067 onResp && onResp();
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -070068
Bri Prebilic Cole0bc4de22015-07-20 17:07:55 -070069 // checks if data changed for row flashing
70 if (!angular.equals(o.scope.tableData, oldTableData)) {
71 o.scope.changedData = [];
72 // only flash the row if the data already exists
73 if (oldTableData.length) {
74 angular.forEach(o.scope.tableData, function (item) {
75 if (!fs.containsObj(oldTableData, item)) {
76 o.scope.changedData.push(item);
77 }
78 });
79 }
80 angular.copy(o.scope.tableData, oldTableData);
81 }
Bri Prebilic Cole70aacc42015-07-22 11:28:34 -070082 o.scope.$apply();
Bri Prebilic Cole0bc4de22015-07-20 17:07:55 -070083 }
84 handlers[resp] = respCb;
85 wss.bindHandlers(handlers);
86
87 // request
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -070088 function sortCb(params) {
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -070089 var p = angular.extend({}, params, o.query);
90 wss.sendEvent(req, p);
Bri Prebilic Cole6e1b4a52015-08-03 17:10:44 -070091 stillLoading();
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -070092 }
93 o.scope.sortCallback = sortCb;
94
Bri Prebilic Cole6e1b4a52015-08-03 17:10:44 -070095 // show loading wheel if it's taking a while for the server to respond
96 function stillLoading() {
97 loaded = false;
98 loadingPromise = $timeout(function () {
99 if (!loaded) {
100 o.scope.loading = true;
101 }
102 }, loadingWait);
103 }
104
Bri Prebilic Cole0bc4de22015-07-20 17:07:55 -0700105 // === selecting a row functions ----------------
Bri Prebilic Colebfab9c72015-06-01 14:33:18 -0700106 function selCb($event, selRow) {
107 o.scope.selId = (o.scope.selId === selRow.id) ? null : selRow.id;
108 onSel && onSel($event, selRow);
Thomas Vachuska619c5382015-04-02 13:41:47 -0700109 }
110 o.scope.selectCallback = selCb;
111
Bri Prebilic Cole0bc4de22015-07-20 17:07:55 -0700112 // === autoRefresh functions ------------------
Bri Prebilic Cole41d67652015-06-02 10:23:04 -0700113 function startRefresh() {
Bri Prebilic Cole6e1b4a52015-08-03 17:10:44 -0700114 refreshPromise = $interval(function () {
Simon Hunt4deb0e82015-06-10 16:18:25 -0700115 if (fs.debugOn('widget')) {
116 $log.debug('Refreshing ' + root + ' page');
117 }
Bri Prebilic Cole41d67652015-06-02 10:23:04 -0700118 sortCb(o.scope.sortParams);
119 }, refreshInterval);
Bri Prebilic Colee1be1b72015-05-12 16:07:24 -0700120 }
Bri Prebilic Cole41d67652015-06-02 10:23:04 -0700121
122 function stopRefresh() {
Bri Prebilic Cole6e1b4a52015-08-03 17:10:44 -0700123 if (angular.isDefined(refreshPromise)) {
124 $interval.cancel(refreshPromise);
125 refreshPromise = undefined;
Bri Prebilic Cole41d67652015-06-02 10:23:04 -0700126 }
127 }
128
129 function toggleRefresh() {
130 o.scope.autoRefresh = !o.scope.autoRefresh;
131 o.scope.autoRefresh ? startRefresh() : stopRefresh();
132 }
133 o.scope.toggleRefresh = toggleRefresh;
Thomas Vachuska619c5382015-04-02 13:41:47 -0700134
Bri Prebilic Cole0bc4de22015-07-20 17:07:55 -0700135 // === Cleanup on destroyed scope -----------------
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -0700136 o.scope.$on('$destroy', function () {
137 wss.unbindHandlers(handlers);
Bri Prebilic Cole41d67652015-06-02 10:23:04 -0700138 stopRefresh();
Bri Prebilic Cole6e1b4a52015-08-03 17:10:44 -0700139 if (angular.isDefined(loadingPromise)) {
140 $timeout.cancel(loadingPromise);
141 loadingPromise = undefined;
142 }
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -0700143 });
144
145 sortCb();
Bri Prebilic Cole41d67652015-06-02 10:23:04 -0700146 startRefresh();
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -0700147 }
148
149 angular.module('onosWidget')
150 .factory('TableBuilderService',
Bri Prebilic Cole6e1b4a52015-08-03 17:10:44 -0700151 ['$log', '$interval', '$timeout', 'FnService', 'WebSocketService',
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -0700152
Bri Prebilic Cole6e1b4a52015-08-03 17:10:44 -0700153 function (_$log_, _$interval_, _$timeout_, _fs_, _wss_) {
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -0700154 $log = _$log_;
Bri Prebilic Colebfab9c72015-06-01 14:33:18 -0700155 $interval = _$interval_;
Bri Prebilic Cole6e1b4a52015-08-03 17:10:44 -0700156 $timeout = _$timeout_;
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -0700157 fs = _fs_;
158 wss = _wss_;
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -0700159
160 return {
161 buildTable: buildTable
162 };
163 }]);
164
165}());