blob: f4e078353c5cb8cffd4a81f6e95f2c0bd1d46e4d [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];
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
Simon Hunt412adc82015-12-11 15:56:20 -080087 // handle "loading..." animation
88 function scheduleTardy() {
89 tardyPromise = $timeout(ls.start, tardyWait);
90 }
91
92 function cancelTardy() {
93 if (tardyPromise) {
94 $timeout.cancel(tardyPromise);
95 tardyPromise = null;
96 }
97 }
98
Bri Prebilic Cole0bc4de22015-07-20 17:07:55 -070099 // request
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -0700100 function sortCb(params) {
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -0700101 var p = angular.extend({}, params, o.query);
Simon Hunt412adc82015-12-11 15:56:20 -0800102 if (wss.isConnected()) {
103 wss.sendEvent(req, p);
104 scheduleTardy();
105 }
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -0700106 }
107 o.scope.sortCallback = sortCb;
108
Bri Prebilic Cole6e1b4a52015-08-03 17:10:44 -0700109
Bri Prebilic Cole0bc4de22015-07-20 17:07:55 -0700110 // === selecting a row functions ----------------
Bri Prebilic Colebfab9c72015-06-01 14:33:18 -0700111 function selCb($event, selRow) {
Simon Hunt4e412732015-10-27 15:25:39 -0700112 var selId = selRow[idKey];
113 o.scope.selId = (o.scope.selId === selId) ? null : selId;
Bri Prebilic Colebfab9c72015-06-01 14:33:18 -0700114 onSel && onSel($event, selRow);
Thomas Vachuska619c5382015-04-02 13:41:47 -0700115 }
116 o.scope.selectCallback = selCb;
117
Bri Prebilic Cole0bc4de22015-07-20 17:07:55 -0700118 // === autoRefresh functions ------------------
Simon Hunt412adc82015-12-11 15:56:20 -0800119 function fetchDataIfNotWaiting() {
120 if (!tardyPromise) {
Simon Hunt4deb0e82015-06-10 16:18:25 -0700121 if (fs.debugOn('widget')) {
122 $log.debug('Refreshing ' + root + ' page');
123 }
Bri Prebilic Cole41d67652015-06-02 10:23:04 -0700124 sortCb(o.scope.sortParams);
Simon Hunt412adc82015-12-11 15:56:20 -0800125 }
126 }
127
128 function startRefresh() {
129 refreshPromise = $interval(fetchDataIfNotWaiting, refreshInterval);
Bri Prebilic Colee1be1b72015-05-12 16:07:24 -0700130 }
Bri Prebilic Cole41d67652015-06-02 10:23:04 -0700131
132 function stopRefresh() {
Simon Hunt412adc82015-12-11 15:56:20 -0800133 if (refreshPromise) {
Bri Prebilic Cole6e1b4a52015-08-03 17:10:44 -0700134 $interval.cancel(refreshPromise);
Simon Hunt412adc82015-12-11 15:56:20 -0800135 refreshPromise = null;
Bri Prebilic Cole41d67652015-06-02 10:23:04 -0700136 }
137 }
138
139 function toggleRefresh() {
140 o.scope.autoRefresh = !o.scope.autoRefresh;
141 o.scope.autoRefresh ? startRefresh() : stopRefresh();
142 }
143 o.scope.toggleRefresh = toggleRefresh;
Thomas Vachuska619c5382015-04-02 13:41:47 -0700144
Bri Prebilic Cole0bc4de22015-07-20 17:07:55 -0700145 // === Cleanup on destroyed scope -----------------
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -0700146 o.scope.$on('$destroy', function () {
147 wss.unbindHandlers(handlers);
Bri Prebilic Cole41d67652015-06-02 10:23:04 -0700148 stopRefresh();
Simon Hunt412adc82015-12-11 15:56:20 -0800149 cancelTardy();
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -0700150 });
151
Simon Hunta678b842016-01-11 17:14:18 -0800152 sortCb(o.scope.sortParams);
Bri Prebilic Cole41d67652015-06-02 10:23:04 -0700153 startRefresh();
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -0700154 }
155
156 angular.module('onosWidget')
157 .factory('TableBuilderService',
Bri Prebilic Cole6e1b4a52015-08-03 17:10:44 -0700158 ['$log', '$interval', '$timeout', 'FnService', 'WebSocketService',
Simon Hunt412adc82015-12-11 15:56:20 -0800159 'LoadingService',
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -0700160
Simon Hunt412adc82015-12-11 15:56:20 -0800161 function (_$log_, _$interval_, _$timeout_, _fs_, _wss_, _ls_) {
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -0700162 $log = _$log_;
Bri Prebilic Colebfab9c72015-06-01 14:33:18 -0700163 $interval = _$interval_;
Bri Prebilic Cole6e1b4a52015-08-03 17:10:44 -0700164 $timeout = _$timeout_;
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -0700165 fs = _fs_;
166 wss = _wss_;
Simon Hunt412adc82015-12-11 15:56:20 -0800167 ls = _ls_;
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -0700168
169 return {
170 buildTable: buildTable
171 };
172 }]);
173
174}());