blob: c7e585e4aecefcef80285b09c95715cb07ce78af [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
Simon Hunt412adc82015-12-11 15:56:20 -080024 var $log, $interval, $timeout, fs, wss, ls;
Bri Prebilic Colebfab9c72015-06-01 14:33:18 -070025
26 // constants
Bri Prebilic Cole6e1b4a52015-08-03 17:10:44 -070027 var refreshInterval = 2000,
Simon Hunt412adc82015-12-11 15:56:20 -080028 tardyWait = 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),
Simon Hunt4e412732015-10-27 15:25:39 -070050 idKey = o.idKey || 'id',
Bri Prebilic Cole0bc4de22015-07-20 17:07:55 -070051 oldTableData = [],
Simon Hunt412adc82015-12-11 15:56:20 -080052 refreshPromise,
53 tardyPromise;
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -070054
Bri Prebilic Colee568ead2015-05-01 09:51:28 -070055 o.scope.tableData = [];
Bri Prebilic Cole0bc4de22015-07-20 17:07:55 -070056 o.scope.changedData = [];
Simon Hunta678b842016-01-11 17:14:18 -080057 o.scope.sortParams = o.sortParams || {};
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) {
Simon Hunt412adc82015-12-11 15:56:20 -080064 cancelTardy();
65 ls.stop();
Bri Prebilic Colee568ead2015-05-01 09:51:28 -070066 o.scope.tableData = data[root];
Jian Li8baf4472016-01-15 15:08:09 -080067 o.scope.annots = data.annots;
Bri Prebilic Cole522e7562015-06-22 15:56:25 -070068 onResp && onResp();
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -070069
Bri Prebilic Cole0bc4de22015-07-20 17:07:55 -070070 // checks if data changed for row flashing
71 if (!angular.equals(o.scope.tableData, oldTableData)) {
72 o.scope.changedData = [];
73 // only flash the row if the data already exists
74 if (oldTableData.length) {
75 angular.forEach(o.scope.tableData, function (item) {
76 if (!fs.containsObj(oldTableData, item)) {
77 o.scope.changedData.push(item);
78 }
79 });
80 }
81 angular.copy(o.scope.tableData, oldTableData);
82 }
Bri Prebilic Cole70aacc42015-07-22 11:28:34 -070083 o.scope.$apply();
Bri Prebilic Cole0bc4de22015-07-20 17:07:55 -070084 }
85 handlers[resp] = respCb;
86 wss.bindHandlers(handlers);
87
Simon Hunt412adc82015-12-11 15:56:20 -080088 // handle "loading..." animation
89 function scheduleTardy() {
90 tardyPromise = $timeout(ls.start, tardyWait);
91 }
92
93 function cancelTardy() {
94 if (tardyPromise) {
95 $timeout.cancel(tardyPromise);
96 tardyPromise = null;
97 }
98 }
99
Bri Prebilic Cole0bc4de22015-07-20 17:07:55 -0700100 // request
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -0700101 function sortCb(params) {
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -0700102 var p = angular.extend({}, params, o.query);
Simon Hunt412adc82015-12-11 15:56:20 -0800103 if (wss.isConnected()) {
104 wss.sendEvent(req, p);
105 scheduleTardy();
106 }
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -0700107 }
108 o.scope.sortCallback = sortCb;
109
Bri Prebilic Cole6e1b4a52015-08-03 17:10:44 -0700110
Bri Prebilic Cole0bc4de22015-07-20 17:07:55 -0700111 // === selecting a row functions ----------------
Bri Prebilic Colebfab9c72015-06-01 14:33:18 -0700112 function selCb($event, selRow) {
Simon Hunt4e412732015-10-27 15:25:39 -0700113 var selId = selRow[idKey];
114 o.scope.selId = (o.scope.selId === selId) ? null : selId;
Bri Prebilic Colebfab9c72015-06-01 14:33:18 -0700115 onSel && onSel($event, selRow);
Thomas Vachuska619c5382015-04-02 13:41:47 -0700116 }
117 o.scope.selectCallback = selCb;
118
Bri Prebilic Cole0bc4de22015-07-20 17:07:55 -0700119 // === autoRefresh functions ------------------
Simon Hunt412adc82015-12-11 15:56:20 -0800120 function fetchDataIfNotWaiting() {
121 if (!tardyPromise) {
Simon Hunt4deb0e82015-06-10 16:18:25 -0700122 if (fs.debugOn('widget')) {
123 $log.debug('Refreshing ' + root + ' page');
124 }
Bri Prebilic Cole41d67652015-06-02 10:23:04 -0700125 sortCb(o.scope.sortParams);
Simon Hunt412adc82015-12-11 15:56:20 -0800126 }
127 }
128
129 function startRefresh() {
130 refreshPromise = $interval(fetchDataIfNotWaiting, refreshInterval);
Bri Prebilic Colee1be1b72015-05-12 16:07:24 -0700131 }
Bri Prebilic Cole41d67652015-06-02 10:23:04 -0700132
133 function stopRefresh() {
Simon Hunt412adc82015-12-11 15:56:20 -0800134 if (refreshPromise) {
Bri Prebilic Cole6e1b4a52015-08-03 17:10:44 -0700135 $interval.cancel(refreshPromise);
Simon Hunt412adc82015-12-11 15:56:20 -0800136 refreshPromise = null;
Bri Prebilic Cole41d67652015-06-02 10:23:04 -0700137 }
138 }
139
140 function toggleRefresh() {
141 o.scope.autoRefresh = !o.scope.autoRefresh;
142 o.scope.autoRefresh ? startRefresh() : stopRefresh();
143 }
144 o.scope.toggleRefresh = toggleRefresh;
Thomas Vachuska619c5382015-04-02 13:41:47 -0700145
Bri Prebilic Cole0bc4de22015-07-20 17:07:55 -0700146 // === Cleanup on destroyed scope -----------------
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -0700147 o.scope.$on('$destroy', function () {
148 wss.unbindHandlers(handlers);
Bri Prebilic Cole41d67652015-06-02 10:23:04 -0700149 stopRefresh();
Simon Hunt412adc82015-12-11 15:56:20 -0800150 cancelTardy();
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -0700151 });
152
Simon Hunta678b842016-01-11 17:14:18 -0800153 sortCb(o.scope.sortParams);
Bri Prebilic Cole41d67652015-06-02 10:23:04 -0700154 startRefresh();
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -0700155 }
156
157 angular.module('onosWidget')
158 .factory('TableBuilderService',
Bri Prebilic Cole6e1b4a52015-08-03 17:10:44 -0700159 ['$log', '$interval', '$timeout', 'FnService', 'WebSocketService',
Simon Hunt412adc82015-12-11 15:56:20 -0800160 'LoadingService',
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -0700161
Simon Hunt412adc82015-12-11 15:56:20 -0800162 function (_$log_, _$interval_, _$timeout_, _fs_, _wss_, _ls_) {
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -0700163 $log = _$log_;
Bri Prebilic Colebfab9c72015-06-01 14:33:18 -0700164 $interval = _$interval_;
Bri Prebilic Cole6e1b4a52015-08-03 17:10:44 -0700165 $timeout = _$timeout_;
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -0700166 fs = _fs_;
167 wss = _wss_;
Simon Hunt412adc82015-12-11 15:56:20 -0800168 ls = _ls_;
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -0700169
170 return {
171 buildTable: buildTable
172 };
173 }]);
174
175}());