blob: 84f11b7f3e12cc97ba75c9a28a11cb7cd5bb4605 [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 Cole864cdd62015-04-02 15:46:47 -070051
52 function respCb(data) {
Bri Prebilic Colee568ead2015-05-01 09:51:28 -070053 o.scope.tableData = data[root];
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -070054 o.scope.$apply();
55 }
56
57 function sortCb(params) {
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -070058 var p = angular.extend({}, params, o.query);
59 wss.sendEvent(req, p);
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -070060 }
61 o.scope.sortCallback = sortCb;
62
Bri Prebilic Colebfab9c72015-06-01 14:33:18 -070063 function selCb($event, selRow) {
64 o.scope.selId = (o.scope.selId === selRow.id) ? null : selRow.id;
65 onSel && onSel($event, selRow);
Thomas Vachuska619c5382015-04-02 13:41:47 -070066 }
67 o.scope.selectCallback = selCb;
68
Bri Prebilic Colebfab9c72015-06-01 14:33:18 -070069 function refresh(params) {
Bri Prebilic Colee1be1b72015-05-12 16:07:24 -070070 $log.debug('Refreshing ' + root + ' page');
Bri Prebilic Colebfab9c72015-06-01 14:33:18 -070071 sortCb(params);
Bri Prebilic Colee1be1b72015-05-12 16:07:24 -070072 }
73 o.scope.refresh = refresh;
Thomas Vachuska619c5382015-04-02 13:41:47 -070074
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -070075 handlers[resp] = respCb;
76 wss.bindHandlers(handlers);
77
78 // Cleanup on destroyed scope
79 o.scope.$on('$destroy', function () {
80 wss.unbindHandlers(handlers);
Bri Prebilic Colec2449762015-05-29 14:30:51 -070081 ts.resetSort();
Bri Prebilic Colebfab9c72015-06-01 14:33:18 -070082 if (angular.isDefined(promise)) {
83 $interval.cancel(promise);
84 promise = undefined;
85 }
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -070086 });
87
88 sortCb();
Bri Prebilic Colebfab9c72015-06-01 14:33:18 -070089
90 promise = $interval(function () {
91 refresh(o.scope.sortParams);
92 }, refreshInterval);
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -070093 }
94
95 angular.module('onosWidget')
96 .factory('TableBuilderService',
Bri Prebilic Colebfab9c72015-06-01 14:33:18 -070097 ['$log', '$interval', 'FnService', 'WebSocketService', 'TableService',
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -070098
Bri Prebilic Colebfab9c72015-06-01 14:33:18 -070099 function (_$log_, _$interval_, _fs_, _wss_, _ts_) {
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -0700100 $log = _$log_;
Bri Prebilic Colebfab9c72015-06-01 14:33:18 -0700101 $interval = _$interval_;
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -0700102 fs = _fs_;
103 wss = _wss_;
Bri Prebilic Colee1be1b72015-05-12 16:07:24 -0700104 ts = _ts_;
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -0700105
106 return {
107 buildTable: buildTable
108 };
109 }]);
110
111}());