blob: 3af8d60652e2368eebf77bfcec6acc4aa06d1c5f [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 () {
72 $log.debug('Refreshing ' + root + ' page');
73 sortCb(o.scope.sortParams);
74 }, refreshInterval);
Bri Prebilic Colee1be1b72015-05-12 16:07:24 -070075 }
Bri Prebilic Cole41d67652015-06-02 10:23:04 -070076
77 function stopRefresh() {
78 if (angular.isDefined(promise)) {
79 $interval.cancel(promise);
80 promise = undefined;
81 }
82 }
83
84 function toggleRefresh() {
85 o.scope.autoRefresh = !o.scope.autoRefresh;
86 o.scope.autoRefresh ? startRefresh() : stopRefresh();
87 }
88 o.scope.toggleRefresh = toggleRefresh;
Thomas Vachuska619c5382015-04-02 13:41:47 -070089
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -070090 handlers[resp] = respCb;
91 wss.bindHandlers(handlers);
92
93 // Cleanup on destroyed scope
94 o.scope.$on('$destroy', function () {
95 wss.unbindHandlers(handlers);
Bri Prebilic Colec2449762015-05-29 14:30:51 -070096 ts.resetSort();
Bri Prebilic Cole41d67652015-06-02 10:23:04 -070097 stopRefresh();
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -070098 });
99
100 sortCb();
Bri Prebilic Cole41d67652015-06-02 10:23:04 -0700101 startRefresh();
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -0700102 }
103
104 angular.module('onosWidget')
105 .factory('TableBuilderService',
Bri Prebilic Colebfab9c72015-06-01 14:33:18 -0700106 ['$log', '$interval', 'FnService', 'WebSocketService', 'TableService',
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -0700107
Bri Prebilic Colebfab9c72015-06-01 14:33:18 -0700108 function (_$log_, _$interval_, _fs_, _wss_, _ts_) {
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -0700109 $log = _$log_;
Bri Prebilic Colebfab9c72015-06-01 14:33:18 -0700110 $interval = _$interval_;
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -0700111 fs = _fs_;
112 wss = _wss_;
Bri Prebilic Colee1be1b72015-05-12 16:07:24 -0700113 ts = _ts_;
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -0700114
115 return {
116 buildTable: buildTable
117 };
118 }]);
119
120}());