blob: 93249ed3413cb4960a09e80646ba212e4a102b43 [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 Cole9cf1a8d2015-04-21 13:15:29 -070024 var $log, fs, wss;
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -070025
26 // example params to buildTable:
27 // {
Simon Hunt35d18882015-04-02 20:16:26 -070028 // self: this, <- controller object
29 // scope: $scope, <- controller scope
30 // tag: 'device', <- table identifier
31 // selCb: selCb <- row selection callback (optional)
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -070032 // }
Simon Hunt35d18882015-04-02 20:16:26 -070033 // Note: selCb() is passed the row data model of the selected row,
34 // or null when no row is selected.
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -070035
36 function buildTable(o) {
37 var handlers = {},
38 root = o.tag + 's',
39 req = o.tag + 'DataRequest',
Thomas Vachuska619c5382015-04-02 13:41:47 -070040 resp = o.tag + 'DataResponse',
41 onSel = fs.isF(o.selCb);
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -070042
43 o.self.tableData = [];
44
45 function respCb(data) {
46 o.self.tableData = data[root];
47 o.scope.$apply();
48 }
49
50 function sortCb(params) {
51 wss.sendEvent(req, params);
52 }
53 o.scope.sortCallback = sortCb;
54
Bri Prebilic Coleb699a162015-04-13 12:01:39 -070055 function selCb($event, sel) {
Thomas Vachuska619c5382015-04-02 13:41:47 -070056 o.scope.sel = (o.scope.sel === sel) ? null : sel;
Bri Prebilic Coleb699a162015-04-13 12:01:39 -070057 onSel && onSel($event, o.scope.sel);
Thomas Vachuska619c5382015-04-02 13:41:47 -070058 }
59 o.scope.selectCallback = selCb;
60
61
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -070062 handlers[resp] = respCb;
63 wss.bindHandlers(handlers);
64
65 // Cleanup on destroyed scope
66 o.scope.$on('$destroy', function () {
67 wss.unbindHandlers(handlers);
68 });
69
70 sortCb();
71 }
72
73 angular.module('onosWidget')
74 .factory('TableBuilderService',
Bri Prebilic Cole9cf1a8d2015-04-21 13:15:29 -070075 ['$log', 'FnService', 'WebSocketService',
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -070076
Bri Prebilic Cole9cf1a8d2015-04-21 13:15:29 -070077 function (_$log_, _fs_, _wss_) {
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -070078 $log = _$log_;
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -070079 fs = _fs_;
80 wss = _wss_;
81
82 return {
83 buildTable: buildTable
84 };
85 }]);
86
87}());