blob: b23bfd0252e63968b8492bacfdfde7b790b6e48e [file] [log] [blame]
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -07003 *
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
Simon Hunta50540f2016-01-25 11:25:11 -080027 var refreshInterval = 2000;
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -070028
29 // example params to buildTable:
30 // {
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -070031 // scope: $scope, <- controller scope
32 // tag: 'device', <- table identifier
Bri Prebilic Cole0bc4de22015-07-20 17:07:55 -070033 // selCb: selCb, <- row selection callback (optional)
34 // respCb: respCb, <- websocket response callback (optional)
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -070035 // query: params <- query parameters in URL (optional)
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -070036 // }
Simon Hunt35d18882015-04-02 20:16:26 -070037 // Note: selCb() is passed the row data model of the selected row,
38 // or null when no row is selected.
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -070039 // Note: query is always an object (empty or containing properties)
40 // it comes from $location.search()
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -070041
42 function buildTable(o) {
43 var handlers = {},
44 root = o.tag + 's',
45 req = o.tag + 'DataRequest',
Thomas Vachuska619c5382015-04-02 13:41:47 -070046 resp = o.tag + 'DataResponse',
Bri Prebilic Colebfab9c72015-06-01 14:33:18 -070047 onSel = fs.isF(o.selCb),
Bri Prebilic Cole522e7562015-06-22 15:56:25 -070048 onResp = fs.isF(o.respCb),
Simon Hunt4e412732015-10-27 15:25:39 -070049 idKey = o.idKey || 'id',
Bri Prebilic Cole0bc4de22015-07-20 17:07:55 -070050 oldTableData = [],
Simon Hunta50540f2016-01-25 11:25:11 -080051 refreshPromise;
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -070052
Bri Prebilic Colee568ead2015-05-01 09:51:28 -070053 o.scope.tableData = [];
Bri Prebilic Cole0bc4de22015-07-20 17:07:55 -070054 o.scope.changedData = [];
Simon Hunta678b842016-01-11 17:14:18 -080055 o.scope.sortParams = o.sortParams || {};
Bri Prebilic Cole41d67652015-06-02 10:23:04 -070056 o.scope.autoRefresh = true;
Simon Hunt94f36fc2017-07-12 17:47:17 -070057 o.scope.autoRefreshTip = o.lion_toggle_auto_refresh || 'Toggle auto refresh';
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -070058
Bri Prebilic Cole0bc4de22015-07-20 17:07:55 -070059 // === websocket functions --------------------
60 // response
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -070061 function respCb(data) {
Simon Hunt412adc82015-12-11 15:56:20 -080062 ls.stop();
Bri Prebilic Colee568ead2015-05-01 09:51:28 -070063 o.scope.tableData = data[root];
Jian Li8baf4472016-01-15 15:08:09 -080064 o.scope.annots = data.annots;
Bri Prebilic Cole522e7562015-06-22 15:56:25 -070065 onResp && onResp();
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -070066
Bri Prebilic Cole0bc4de22015-07-20 17:07:55 -070067 // checks if data changed for row flashing
68 if (!angular.equals(o.scope.tableData, oldTableData)) {
69 o.scope.changedData = [];
70 // only flash the row if the data already exists
71 if (oldTableData.length) {
72 angular.forEach(o.scope.tableData, function (item) {
73 if (!fs.containsObj(oldTableData, item)) {
74 o.scope.changedData.push(item);
75 }
76 });
77 }
78 angular.copy(o.scope.tableData, oldTableData);
79 }
Bri Prebilic Cole70aacc42015-07-22 11:28:34 -070080 o.scope.$apply();
Bri Prebilic Cole0bc4de22015-07-20 17:07:55 -070081 }
82 handlers[resp] = respCb;
83 wss.bindHandlers(handlers);
84
85 // request
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -070086 function sortCb(params) {
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -070087 var p = angular.extend({}, params, o.query);
Simon Hunt412adc82015-12-11 15:56:20 -080088 if (wss.isConnected()) {
89 wss.sendEvent(req, p);
Simon Hunta50540f2016-01-25 11:25:11 -080090 ls.start();
Simon Hunt412adc82015-12-11 15:56:20 -080091 }
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -070092 }
93 o.scope.sortCallback = sortCb;
94
Bri Prebilic Cole6e1b4a52015-08-03 17:10:44 -070095
Bri Prebilic Cole0bc4de22015-07-20 17:07:55 -070096 // === selecting a row functions ----------------
Bri Prebilic Colebfab9c72015-06-01 14:33:18 -070097 function selCb($event, selRow) {
Simon Hunt4e412732015-10-27 15:25:39 -070098 var selId = selRow[idKey];
99 o.scope.selId = (o.scope.selId === selId) ? null : selId;
Bri Prebilic Colebfab9c72015-06-01 14:33:18 -0700100 onSel && onSel($event, selRow);
Thomas Vachuska619c5382015-04-02 13:41:47 -0700101 }
102 o.scope.selectCallback = selCb;
103
Bri Prebilic Cole0bc4de22015-07-20 17:07:55 -0700104 // === autoRefresh functions ------------------
Simon Hunt412adc82015-12-11 15:56:20 -0800105 function fetchDataIfNotWaiting() {
Simon Hunta50540f2016-01-25 11:25:11 -0800106 if (!ls.waiting()) {
Simon Hunt4deb0e82015-06-10 16:18:25 -0700107 if (fs.debugOn('widget')) {
108 $log.debug('Refreshing ' + root + ' page');
109 }
Bri Prebilic Cole41d67652015-06-02 10:23:04 -0700110 sortCb(o.scope.sortParams);
Simon Hunt412adc82015-12-11 15:56:20 -0800111 }
112 }
113
114 function startRefresh() {
115 refreshPromise = $interval(fetchDataIfNotWaiting, refreshInterval);
Bri Prebilic Colee1be1b72015-05-12 16:07:24 -0700116 }
Bri Prebilic Cole41d67652015-06-02 10:23:04 -0700117
118 function stopRefresh() {
Simon Hunt412adc82015-12-11 15:56:20 -0800119 if (refreshPromise) {
Bri Prebilic Cole6e1b4a52015-08-03 17:10:44 -0700120 $interval.cancel(refreshPromise);
Simon Hunt412adc82015-12-11 15:56:20 -0800121 refreshPromise = null;
Bri Prebilic Cole41d67652015-06-02 10:23:04 -0700122 }
123 }
124
125 function toggleRefresh() {
126 o.scope.autoRefresh = !o.scope.autoRefresh;
127 o.scope.autoRefresh ? startRefresh() : stopRefresh();
128 }
129 o.scope.toggleRefresh = toggleRefresh;
Thomas Vachuska619c5382015-04-02 13:41:47 -0700130
Bri Prebilic Cole0bc4de22015-07-20 17:07:55 -0700131 // === Cleanup on destroyed scope -----------------
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -0700132 o.scope.$on('$destroy', function () {
133 wss.unbindHandlers(handlers);
Bri Prebilic Cole41d67652015-06-02 10:23:04 -0700134 stopRefresh();
Simon Hunta50540f2016-01-25 11:25:11 -0800135 ls.stop();
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -0700136 });
137
Simon Hunta678b842016-01-11 17:14:18 -0800138 sortCb(o.scope.sortParams);
Bri Prebilic Cole41d67652015-06-02 10:23:04 -0700139 startRefresh();
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -0700140 }
141
142 angular.module('onosWidget')
143 .factory('TableBuilderService',
Bri Prebilic Cole6e1b4a52015-08-03 17:10:44 -0700144 ['$log', '$interval', '$timeout', 'FnService', 'WebSocketService',
Simon Hunt412adc82015-12-11 15:56:20 -0800145 'LoadingService',
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -0700146
Simon Hunt412adc82015-12-11 15:56:20 -0800147 function (_$log_, _$interval_, _$timeout_, _fs_, _wss_, _ls_) {
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -0700148 $log = _$log_;
Bri Prebilic Colebfab9c72015-06-01 14:33:18 -0700149 $interval = _$interval_;
Bri Prebilic Cole6e1b4a52015-08-03 17:10:44 -0700150 $timeout = _$timeout_;
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -0700151 fs = _fs_;
152 wss = _wss_;
Simon Hunt412adc82015-12-11 15:56:20 -0800153 ls = _ls_;
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -0700154
155 return {
156 buildTable: buildTable
157 };
158 }]);
159
160}());