blob: c731600533975b4d5966fd5ee08a4a5a5f0a5903 [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 Cole0c41ba22015-07-06 15:09:48 -070024 var $log, $interval, fs, wss;
Bri Prebilic Colebfab9c72015-06-01 14:33:18 -070025
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
Bri Prebilic Cole0bc4de22015-07-20 17:07:55 -070033 // selCb: selCb, <- row selection callback (optional)
34 // respCb: respCb, <- websocket response callback (optional)
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -070035 // query: params <- query parameters in URL (optional)
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -070036 // }
Simon Hunt35d18882015-04-02 20:16:26 -070037 // Note: selCb() is passed the row data model of the selected row,
38 // or null when no row is selected.
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -070039 // Note: query is always an object (empty or containing properties)
40 // it comes from $location.search()
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -070041
42 function buildTable(o) {
43 var handlers = {},
44 root = o.tag + 's',
45 req = o.tag + 'DataRequest',
Thomas Vachuska619c5382015-04-02 13:41:47 -070046 resp = o.tag + 'DataResponse',
Bri Prebilic Colebfab9c72015-06-01 14:33:18 -070047 onSel = fs.isF(o.selCb),
Bri Prebilic Cole522e7562015-06-22 15:56:25 -070048 onResp = fs.isF(o.respCb),
Bri Prebilic Cole0bc4de22015-07-20 17:07:55 -070049 oldTableData = [],
Bri Prebilic Colebfab9c72015-06-01 14:33:18 -070050 promise;
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -070051
Bri Prebilic Colee568ead2015-05-01 09:51:28 -070052 o.scope.tableData = [];
Bri Prebilic Cole0bc4de22015-07-20 17:07:55 -070053 o.scope.changedData = [];
Bri Prebilic Colebfab9c72015-06-01 14:33:18 -070054 o.scope.sortParams = {};
Bri Prebilic Cole41d67652015-06-02 10:23:04 -070055 o.scope.autoRefresh = true;
Bri Prebilic Coleeef67ae2015-07-01 16:26:59 -070056 o.scope.autoRefreshTip = 'Toggle auto refresh';
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -070057
Bri Prebilic Cole0bc4de22015-07-20 17:07:55 -070058 // === websocket functions --------------------
59 // response
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -070060 function respCb(data) {
Bri Prebilic Colee568ead2015-05-01 09:51:28 -070061 o.scope.tableData = data[root];
Bri Prebilic Cole522e7562015-06-22 15:56:25 -070062 onResp && onResp();
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -070063
Bri Prebilic Cole0bc4de22015-07-20 17:07:55 -070064 // checks if data changed for row flashing
65 if (!angular.equals(o.scope.tableData, oldTableData)) {
66 o.scope.changedData = [];
67 // only flash the row if the data already exists
68 if (oldTableData.length) {
69 angular.forEach(o.scope.tableData, function (item) {
70 if (!fs.containsObj(oldTableData, item)) {
71 o.scope.changedData.push(item);
72 }
73 });
74 }
75 angular.copy(o.scope.tableData, oldTableData);
76 }
Bri Prebilic Cole70aacc42015-07-22 11:28:34 -070077 o.scope.$apply();
Bri Prebilic Cole0bc4de22015-07-20 17:07:55 -070078 }
79 handlers[resp] = respCb;
80 wss.bindHandlers(handlers);
81
82 // request
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -070083 function sortCb(params) {
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -070084 var p = angular.extend({}, params, o.query);
85 wss.sendEvent(req, p);
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -070086 }
87 o.scope.sortCallback = sortCb;
88
Bri Prebilic Cole0bc4de22015-07-20 17:07:55 -070089 // === selecting a row functions ----------------
Bri Prebilic Colebfab9c72015-06-01 14:33:18 -070090 function selCb($event, selRow) {
91 o.scope.selId = (o.scope.selId === selRow.id) ? null : selRow.id;
92 onSel && onSel($event, selRow);
Thomas Vachuska619c5382015-04-02 13:41:47 -070093 }
94 o.scope.selectCallback = selCb;
95
Bri Prebilic Cole0bc4de22015-07-20 17:07:55 -070096 // === autoRefresh functions ------------------
Bri Prebilic Cole41d67652015-06-02 10:23:04 -070097 function startRefresh() {
98 promise = $interval(function () {
Simon Hunt4deb0e82015-06-10 16:18:25 -070099 if (fs.debugOn('widget')) {
100 $log.debug('Refreshing ' + root + ' page');
101 }
Bri Prebilic Cole41d67652015-06-02 10:23:04 -0700102 sortCb(o.scope.sortParams);
103 }, refreshInterval);
Bri Prebilic Colee1be1b72015-05-12 16:07:24 -0700104 }
Bri Prebilic Cole41d67652015-06-02 10:23:04 -0700105
106 function stopRefresh() {
107 if (angular.isDefined(promise)) {
108 $interval.cancel(promise);
109 promise = undefined;
110 }
111 }
112
113 function toggleRefresh() {
114 o.scope.autoRefresh = !o.scope.autoRefresh;
115 o.scope.autoRefresh ? startRefresh() : stopRefresh();
116 }
117 o.scope.toggleRefresh = toggleRefresh;
Thomas Vachuska619c5382015-04-02 13:41:47 -0700118
Bri Prebilic Cole0bc4de22015-07-20 17:07:55 -0700119 // === Cleanup on destroyed scope -----------------
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -0700120 o.scope.$on('$destroy', function () {
121 wss.unbindHandlers(handlers);
Bri Prebilic Cole41d67652015-06-02 10:23:04 -0700122 stopRefresh();
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -0700123 });
124
125 sortCb();
Bri Prebilic Cole41d67652015-06-02 10:23:04 -0700126 startRefresh();
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -0700127 }
128
129 angular.module('onosWidget')
130 .factory('TableBuilderService',
Bri Prebilic Cole0c41ba22015-07-06 15:09:48 -0700131 ['$log', '$interval', 'FnService', 'WebSocketService',
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -0700132
Bri Prebilic Cole0c41ba22015-07-06 15:09:48 -0700133 function (_$log_, _$interval_, _fs_, _wss_) {
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -0700134 $log = _$log_;
Bri Prebilic Colebfab9c72015-06-01 14:33:18 -0700135 $interval = _$interval_;
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -0700136 fs = _fs_;
137 wss = _wss_;
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -0700138
139 return {
140 buildTable: buildTable
141 };
142 }]);
143
144}());