blob: b3d759e29da03b83cc8ce025581880d1f0aab895 [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 Colebfab9c72015-06-01 14:33:18 -070024 var $log, $interval, fs, wss, ts;
25
26 // constants
27 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
33 // selCb: selCb <- row selection callback (optional)
34 // query: params <- query parameters in URL (optional)
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -070035 // }
Simon Hunt35d18882015-04-02 20:16:26 -070036 // Note: selCb() is passed the row data model of the selected row,
37 // or null when no row is selected.
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -070038 // Note: query is always an object (empty or containing properties)
39 // it comes from $location.search()
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -070040
41 function buildTable(o) {
42 var handlers = {},
43 root = o.tag + 's',
44 req = o.tag + 'DataRequest',
Thomas Vachuska619c5382015-04-02 13:41:47 -070045 resp = o.tag + 'DataResponse',
Bri Prebilic Colebfab9c72015-06-01 14:33:18 -070046 onSel = fs.isF(o.selCb),
47 promise;
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -070048
Bri Prebilic Colee568ead2015-05-01 09:51:28 -070049 o.scope.tableData = [];
Bri Prebilic Colebfab9c72015-06-01 14:33:18 -070050 o.scope.sortParams = {};
Bri Prebilic Cole41d67652015-06-02 10:23:04 -070051 o.scope.autoRefresh = true;
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -070052
53 function respCb(data) {
Bri Prebilic Colee568ead2015-05-01 09:51:28 -070054 o.scope.tableData = data[root];
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -070055 o.scope.$apply();
56 }
57
58 function sortCb(params) {
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -070059 var p = angular.extend({}, params, o.query);
60 wss.sendEvent(req, p);
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -070061 }
62 o.scope.sortCallback = sortCb;
63
Bri Prebilic Colebfab9c72015-06-01 14:33:18 -070064 function selCb($event, selRow) {
65 o.scope.selId = (o.scope.selId === selRow.id) ? null : selRow.id;
66 onSel && onSel($event, selRow);
Thomas Vachuska619c5382015-04-02 13:41:47 -070067 }
68 o.scope.selectCallback = selCb;
69
Bri Prebilic Cole41d67652015-06-02 10:23:04 -070070 function startRefresh() {
71 promise = $interval(function () {
Simon Hunt562e87b2015-06-10 16:18:25 -070072 if (fs.debugOn('widget')) {
73 $log.debug('Refreshing ' + root + ' page');
74 }
Bri Prebilic Cole41d67652015-06-02 10:23:04 -070075 sortCb(o.scope.sortParams);
76 }, refreshInterval);
Bri Prebilic Colee1be1b72015-05-12 16:07:24 -070077 }
Bri Prebilic Cole41d67652015-06-02 10:23:04 -070078
79 function stopRefresh() {
80 if (angular.isDefined(promise)) {
81 $interval.cancel(promise);
82 promise = undefined;
83 }
84 }
85
86 function toggleRefresh() {
87 o.scope.autoRefresh = !o.scope.autoRefresh;
88 o.scope.autoRefresh ? startRefresh() : stopRefresh();
89 }
90 o.scope.toggleRefresh = toggleRefresh;
Thomas Vachuska619c5382015-04-02 13:41:47 -070091
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -070092 handlers[resp] = respCb;
93 wss.bindHandlers(handlers);
94
95 // Cleanup on destroyed scope
96 o.scope.$on('$destroy', function () {
97 wss.unbindHandlers(handlers);
Bri Prebilic Colec2449762015-05-29 14:30:51 -070098 ts.resetSort();
Bri Prebilic Cole41d67652015-06-02 10:23:04 -070099 stopRefresh();
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -0700100 });
101
102 sortCb();
Bri Prebilic Cole41d67652015-06-02 10:23:04 -0700103 startRefresh();
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -0700104 }
105
106 angular.module('onosWidget')
107 .factory('TableBuilderService',
Bri Prebilic Colebfab9c72015-06-01 14:33:18 -0700108 ['$log', '$interval', 'FnService', 'WebSocketService', 'TableService',
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -0700109
Bri Prebilic Colebfab9c72015-06-01 14:33:18 -0700110 function (_$log_, _$interval_, _fs_, _wss_, _ts_) {
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -0700111 $log = _$log_;
Bri Prebilic Colebfab9c72015-06-01 14:33:18 -0700112 $interval = _$interval_;
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -0700113 fs = _fs_;
114 wss = _wss_;
Bri Prebilic Colee1be1b72015-05-12 16:07:24 -0700115 ts = _ts_;
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -0700116
117 return {
118 buildTable: buildTable
119 };
120 }]);
121
122}());