blob: 6a5ffb1fa0ddf98d04819aad35a0e1bfd6c56cb3 [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),
Simon Hunt4e412732015-10-27 15:25:39 -070050 idKey = o.idKey || 'id',
Bri Prebilic Cole0bc4de22015-07-20 17:07:55 -070051 oldTableData = [],
Bri Prebilic Cole6e1b4a52015-08-03 17:10:44 -070052 loaded = false,
53 refreshPromise, loadingPromise;
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 = [];
Bri Prebilic Colebfab9c72015-06-01 14:33:18 -070057 o.scope.sortParams = {};
Bri Prebilic Cole6e1b4a52015-08-03 17:10:44 -070058 o.scope.loading = true;
Bri Prebilic Cole41d67652015-06-02 10:23:04 -070059 o.scope.autoRefresh = true;
Bri Prebilic Coleeef67ae2015-07-01 16:26:59 -070060 o.scope.autoRefreshTip = 'Toggle auto refresh';
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -070061
Bri Prebilic Cole0bc4de22015-07-20 17:07:55 -070062 // === websocket functions --------------------
63 // response
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -070064 function respCb(data) {
Bri Prebilic Cole6e1b4a52015-08-03 17:10:44 -070065 loaded = true;
66 o.scope.loading = false;
Bri Prebilic Colee568ead2015-05-01 09:51:28 -070067 o.scope.tableData = data[root];
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
88 // request
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -070089 function sortCb(params) {
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -070090 var p = angular.extend({}, params, o.query);
91 wss.sendEvent(req, p);
Bri Prebilic Cole6e1b4a52015-08-03 17:10:44 -070092 stillLoading();
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -070093 }
94 o.scope.sortCallback = sortCb;
95
Bri Prebilic Cole6e1b4a52015-08-03 17:10:44 -070096 // show loading wheel if it's taking a while for the server to respond
97 function stillLoading() {
98 loaded = false;
99 loadingPromise = $timeout(function () {
100 if (!loaded) {
101 o.scope.loading = true;
102 }
103 }, loadingWait);
104 }
105
Bri Prebilic Cole0bc4de22015-07-20 17:07:55 -0700106 // === selecting a row functions ----------------
Bri Prebilic Colebfab9c72015-06-01 14:33:18 -0700107 function selCb($event, selRow) {
Simon Hunt4e412732015-10-27 15:25:39 -0700108 var selId = selRow[idKey];
109 o.scope.selId = (o.scope.selId === selId) ? null : selId;
Bri Prebilic Colebfab9c72015-06-01 14:33:18 -0700110 onSel && onSel($event, selRow);
Thomas Vachuska619c5382015-04-02 13:41:47 -0700111 }
112 o.scope.selectCallback = selCb;
113
Bri Prebilic Cole0bc4de22015-07-20 17:07:55 -0700114 // === autoRefresh functions ------------------
Bri Prebilic Cole41d67652015-06-02 10:23:04 -0700115 function startRefresh() {
Bri Prebilic Cole6e1b4a52015-08-03 17:10:44 -0700116 refreshPromise = $interval(function () {
Simon Hunt4deb0e82015-06-10 16:18:25 -0700117 if (fs.debugOn('widget')) {
118 $log.debug('Refreshing ' + root + ' page');
119 }
Bri Prebilic Cole41d67652015-06-02 10:23:04 -0700120 sortCb(o.scope.sortParams);
121 }, refreshInterval);
Bri Prebilic Colee1be1b72015-05-12 16:07:24 -0700122 }
Bri Prebilic Cole41d67652015-06-02 10:23:04 -0700123
124 function stopRefresh() {
Bri Prebilic Cole6e1b4a52015-08-03 17:10:44 -0700125 if (angular.isDefined(refreshPromise)) {
126 $interval.cancel(refreshPromise);
127 refreshPromise = undefined;
Bri Prebilic Cole41d67652015-06-02 10:23:04 -0700128 }
129 }
130
131 function toggleRefresh() {
132 o.scope.autoRefresh = !o.scope.autoRefresh;
133 o.scope.autoRefresh ? startRefresh() : stopRefresh();
134 }
135 o.scope.toggleRefresh = toggleRefresh;
Thomas Vachuska619c5382015-04-02 13:41:47 -0700136
Bri Prebilic Cole0bc4de22015-07-20 17:07:55 -0700137 // === Cleanup on destroyed scope -----------------
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -0700138 o.scope.$on('$destroy', function () {
139 wss.unbindHandlers(handlers);
Bri Prebilic Cole41d67652015-06-02 10:23:04 -0700140 stopRefresh();
Bri Prebilic Cole6e1b4a52015-08-03 17:10:44 -0700141 if (angular.isDefined(loadingPromise)) {
142 $timeout.cancel(loadingPromise);
143 loadingPromise = undefined;
144 }
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -0700145 });
146
147 sortCb();
Bri Prebilic Cole41d67652015-06-02 10:23:04 -0700148 startRefresh();
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -0700149 }
150
151 angular.module('onosWidget')
152 .factory('TableBuilderService',
Bri Prebilic Cole6e1b4a52015-08-03 17:10:44 -0700153 ['$log', '$interval', '$timeout', 'FnService', 'WebSocketService',
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -0700154
Bri Prebilic Cole6e1b4a52015-08-03 17:10:44 -0700155 function (_$log_, _$interval_, _$timeout_, _fs_, _wss_) {
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -0700156 $log = _$log_;
Bri Prebilic Colebfab9c72015-06-01 14:33:18 -0700157 $interval = _$interval_;
Bri Prebilic Cole6e1b4a52015-08-03 17:10:44 -0700158 $timeout = _$timeout_;
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -0700159 fs = _fs_;
160 wss = _wss_;
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -0700161
162 return {
163 buildTable: buildTable
164 };
165 }]);
166
167}());