blob: cab5c8ed4305bd435cfbf8e94130f8494c1cf523 [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/*
Simon Hunt105bc4e2017-07-21 11:07:01 -070018 ONOS GUI -- Widget -- Table Builder Service
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -070019 */
20(function () {
21 'use strict';
22
23 // injected refs
Simon Hunt5c3ed732017-07-20 19:03:28 +000024 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
Simon Hunt105bc4e2017-07-21 11:07:01 -070042 // Additional Notes:
43 // When sending a request for table data, the $scope will be checked
44 // for a .payloadParams object which, if it exists, will be merged into
45 // the event payload. By modifying this object (via toggle buttons, or
46 // other user interaction) additional parameters / state can be passed
47 // to the server in the data request.
48
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -070049 function buildTable(o) {
50 var handlers = {},
51 root = o.tag + 's',
52 req = o.tag + 'DataRequest',
Thomas Vachuska619c5382015-04-02 13:41:47 -070053 resp = o.tag + 'DataResponse',
Bri Prebilic Colebfab9c72015-06-01 14:33:18 -070054 onSel = fs.isF(o.selCb),
Bri Prebilic Cole522e7562015-06-22 15:56:25 -070055 onResp = fs.isF(o.respCb),
Simon Hunt4e412732015-10-27 15:25:39 -070056 idKey = o.idKey || 'id',
Bri Prebilic Cole0bc4de22015-07-20 17:07:55 -070057 oldTableData = [],
Simon Hunta50540f2016-01-25 11:25:11 -080058 refreshPromise;
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -070059
Bri Prebilic Colee568ead2015-05-01 09:51:28 -070060 o.scope.tableData = [];
Bri Prebilic Cole0bc4de22015-07-20 17:07:55 -070061 o.scope.changedData = [];
Simon Hunta678b842016-01-11 17:14:18 -080062 o.scope.sortParams = o.sortParams || {};
Bri Prebilic Cole41d67652015-06-02 10:23:04 -070063 o.scope.autoRefresh = true;
Simon Hunt94f36fc2017-07-12 17:47:17 -070064 o.scope.autoRefreshTip = o.lion_toggle_auto_refresh || 'Toggle auto refresh';
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -070065
Bri Prebilic Cole0bc4de22015-07-20 17:07:55 -070066 // === websocket functions --------------------
Simon Hunt105bc4e2017-07-21 11:07:01 -070067
68 // === Table Data Response
69 function tableDataResponseCb(data) {
Simon Hunt412adc82015-12-11 15:56:20 -080070 ls.stop();
Bri Prebilic Colee568ead2015-05-01 09:51:28 -070071 o.scope.tableData = data[root];
Jian Li8baf4472016-01-15 15:08:09 -080072 o.scope.annots = data.annots;
Bri Prebilic Cole522e7562015-06-22 15:56:25 -070073 onResp && onResp();
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -070074
Bri Prebilic Cole0bc4de22015-07-20 17:07:55 -070075 // checks if data changed for row flashing
76 if (!angular.equals(o.scope.tableData, oldTableData)) {
77 o.scope.changedData = [];
78 // only flash the row if the data already exists
79 if (oldTableData.length) {
80 angular.forEach(o.scope.tableData, function (item) {
81 if (!fs.containsObj(oldTableData, item)) {
82 o.scope.changedData.push(item);
83 }
84 });
85 }
86 angular.copy(o.scope.tableData, oldTableData);
87 }
Bri Prebilic Cole70aacc42015-07-22 11:28:34 -070088 o.scope.$apply();
Bri Prebilic Cole0bc4de22015-07-20 17:07:55 -070089 }
Simon Hunt105bc4e2017-07-21 11:07:01 -070090 handlers[resp] = tableDataResponseCb;
Bri Prebilic Cole0bc4de22015-07-20 17:07:55 -070091 wss.bindHandlers(handlers);
92
Simon Hunt105bc4e2017-07-21 11:07:01 -070093 // === Table Data Request
94 function requestTableData() {
95 var sortParams = o.scope.sortParams,
96 pp = fs.isO(o.scope.payloadParams),
97 payloadParams = pp || {},
98 p = angular.extend({}, sortParams, payloadParams, o.query);
99
Simon Hunt412adc82015-12-11 15:56:20 -0800100 if (wss.isConnected()) {
Simon Hunt105bc4e2017-07-21 11:07:01 -0700101 if (fs.debugOn('table')) {
102 $log.debug('Table data REQUEST:', req, p);
103 }
Simon Hunt412adc82015-12-11 15:56:20 -0800104 wss.sendEvent(req, p);
Simon Hunta50540f2016-01-25 11:25:11 -0800105 ls.start();
Simon Hunt412adc82015-12-11 15:56:20 -0800106 }
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -0700107 }
Simon Hunt105bc4e2017-07-21 11:07:01 -0700108 o.scope.sortCallback = requestTableData;
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -0700109
Bri Prebilic Cole6e1b4a52015-08-03 17:10:44 -0700110
Simon Hunt105bc4e2017-07-21 11:07:01 -0700111 // === Row Selected
112 function rowSelectionCb($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 }
Simon Hunt105bc4e2017-07-21 11:07:01 -0700117 o.scope.selectCallback = rowSelectionCb;
Thomas Vachuska619c5382015-04-02 13:41:47 -0700118
Simon Hunt105bc4e2017-07-21 11:07:01 -0700119 // === autoRefresh functions
Simon Hunt412adc82015-12-11 15:56:20 -0800120 function fetchDataIfNotWaiting() {
Simon Hunta50540f2016-01-25 11:25:11 -0800121 if (!ls.waiting()) {
Simon Hunt4deb0e82015-06-10 16:18:25 -0700122 if (fs.debugOn('widget')) {
123 $log.debug('Refreshing ' + root + ' page');
124 }
Simon Hunt105bc4e2017-07-21 11:07:01 -0700125 requestTableData();
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
Simon Hunt105bc4e2017-07-21 11:07:01 -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 Hunta50540f2016-01-25 11:25:11 -0800150 ls.stop();
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -0700151 });
152
Simon Hunt105bc4e2017-07-21 11:07:01 -0700153 requestTableData();
Bri Prebilic Cole41d67652015-06-02 10:23:04 -0700154 startRefresh();
Viswanath KSPd25440b2017-07-21 13:48:06 +0530155
156 return {
157 forceRefesh : requestTableData
158 };
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -0700159 }
160
161 angular.module('onosWidget')
162 .factory('TableBuilderService',
Simon Hunt5c3ed732017-07-20 19:03:28 +0000163 ['$log', '$interval', '$timeout', 'FnService', 'WebSocketService',
Simon Hunt412adc82015-12-11 15:56:20 -0800164 'LoadingService',
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -0700165
Simon Hunt5c3ed732017-07-20 19:03:28 +0000166 function (_$log_, _$interval_, _$timeout_, _fs_, _wss_, _ls_) {
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -0700167 $log = _$log_;
Bri Prebilic Colebfab9c72015-06-01 14:33:18 -0700168 $interval = _$interval_;
Simon Hunt5c3ed732017-07-20 19:03:28 +0000169 $timeout = _$timeout_;
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -0700170 fs = _fs_;
171 wss = _wss_;
Simon Hunt412adc82015-12-11 15:56:20 -0800172 ls = _ls_;
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -0700173
174 return {
Simon Hunt5c3ed732017-07-20 19:03:28 +0000175 buildTable: buildTable
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -0700176 };
177 }]);
178
179}());