blob: ba04b3023db4e49c79e63936aa8dce39cbac5b3e [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),
Bri Prebilic Cole522e7562015-06-22 15:56:25 -070047 onResp = fs.isF(o.respCb),
Bri Prebilic Colebfab9c72015-06-01 14:33:18 -070048 promise;
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -070049
Bri Prebilic Colee568ead2015-05-01 09:51:28 -070050 o.scope.tableData = [];
Bri Prebilic Colebfab9c72015-06-01 14:33:18 -070051 o.scope.sortParams = {};
Bri Prebilic Cole41d67652015-06-02 10:23:04 -070052 o.scope.autoRefresh = true;
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -070053
54 function respCb(data) {
Bri Prebilic Colee568ead2015-05-01 09:51:28 -070055 o.scope.tableData = data[root];
Bri Prebilic Cole522e7562015-06-22 15:56:25 -070056 onResp && onResp();
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -070057 o.scope.$apply();
58 }
59
60 function sortCb(params) {
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -070061 var p = angular.extend({}, params, o.query);
62 wss.sendEvent(req, p);
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -070063 }
64 o.scope.sortCallback = sortCb;
65
Bri Prebilic Colebfab9c72015-06-01 14:33:18 -070066 function selCb($event, selRow) {
67 o.scope.selId = (o.scope.selId === selRow.id) ? null : selRow.id;
68 onSel && onSel($event, selRow);
Thomas Vachuska619c5382015-04-02 13:41:47 -070069 }
70 o.scope.selectCallback = selCb;
71
Bri Prebilic Cole41d67652015-06-02 10:23:04 -070072 function startRefresh() {
73 promise = $interval(function () {
Simon Hunt4deb0e82015-06-10 16:18:25 -070074 if (fs.debugOn('widget')) {
75 $log.debug('Refreshing ' + root + ' page');
76 }
Bri Prebilic Cole41d67652015-06-02 10:23:04 -070077 sortCb(o.scope.sortParams);
78 }, refreshInterval);
Bri Prebilic Colee1be1b72015-05-12 16:07:24 -070079 }
Bri Prebilic Cole41d67652015-06-02 10:23:04 -070080
81 function stopRefresh() {
82 if (angular.isDefined(promise)) {
83 $interval.cancel(promise);
84 promise = undefined;
85 }
86 }
87
88 function toggleRefresh() {
89 o.scope.autoRefresh = !o.scope.autoRefresh;
90 o.scope.autoRefresh ? startRefresh() : stopRefresh();
91 }
92 o.scope.toggleRefresh = toggleRefresh;
Thomas Vachuska619c5382015-04-02 13:41:47 -070093
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -070094 handlers[resp] = respCb;
95 wss.bindHandlers(handlers);
96
97 // Cleanup on destroyed scope
98 o.scope.$on('$destroy', function () {
99 wss.unbindHandlers(handlers);
Bri Prebilic Colec2449762015-05-29 14:30:51 -0700100 ts.resetSort();
Bri Prebilic Cole41d67652015-06-02 10:23:04 -0700101 stopRefresh();
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -0700102 });
103
104 sortCb();
Bri Prebilic Cole41d67652015-06-02 10:23:04 -0700105 startRefresh();
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -0700106 }
107
108 angular.module('onosWidget')
109 .factory('TableBuilderService',
Bri Prebilic Colebfab9c72015-06-01 14:33:18 -0700110 ['$log', '$interval', 'FnService', 'WebSocketService', 'TableService',
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -0700111
Bri Prebilic Colebfab9c72015-06-01 14:33:18 -0700112 function (_$log_, _$interval_, _fs_, _wss_, _ts_) {
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -0700113 $log = _$log_;
Bri Prebilic Colebfab9c72015-06-01 14:33:18 -0700114 $interval = _$interval_;
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -0700115 fs = _fs_;
116 wss = _wss_;
Bri Prebilic Colee1be1b72015-05-12 16:07:24 -0700117 ts = _ts_;
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -0700118
119 return {
120 buildTable: buildTable
121 };
122 }]);
123
124}());