blob: 3a9773270a2e95b12e892c01c95e602cd0c78f01 [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 // {
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -070028 // scope: $scope, <- controller scope
29 // tag: 'device', <- table identifier
30 // selCb: selCb <- row selection callback (optional)
31 // query: params <- query parameters in URL (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 Colecdc188d2015-04-24 16:40:11 -070035 // Note: query is always an object (empty or containing properties)
36 // it comes from $location.search()
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -070037
38 function buildTable(o) {
39 var handlers = {},
40 root = o.tag + 's',
41 req = o.tag + 'DataRequest',
Thomas Vachuska619c5382015-04-02 13:41:47 -070042 resp = o.tag + 'DataResponse',
43 onSel = fs.isF(o.selCb);
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -070044
Bri Prebilic Colee568ead2015-05-01 09:51:28 -070045 o.scope.tableData = [];
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -070046
47 function respCb(data) {
Bri Prebilic Colee568ead2015-05-01 09:51:28 -070048 o.scope.tableData = data[root];
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -070049 o.scope.$apply();
50 }
51
52 function sortCb(params) {
Bri Prebilic Colecdc188d2015-04-24 16:40:11 -070053 var p = angular.extend({}, params, o.query);
54 wss.sendEvent(req, p);
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -070055 }
56 o.scope.sortCallback = sortCb;
57
Bri Prebilic Coleb699a162015-04-13 12:01:39 -070058 function selCb($event, sel) {
Thomas Vachuska619c5382015-04-02 13:41:47 -070059 o.scope.sel = (o.scope.sel === sel) ? null : sel;
Bri Prebilic Coleb699a162015-04-13 12:01:39 -070060 onSel && onSel($event, o.scope.sel);
Thomas Vachuska619c5382015-04-02 13:41:47 -070061 }
62 o.scope.selectCallback = selCb;
63
64
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -070065 handlers[resp] = respCb;
66 wss.bindHandlers(handlers);
67
68 // Cleanup on destroyed scope
69 o.scope.$on('$destroy', function () {
70 wss.unbindHandlers(handlers);
71 });
72
73 sortCb();
74 }
75
76 angular.module('onosWidget')
77 .factory('TableBuilderService',
Bri Prebilic Cole9cf1a8d2015-04-21 13:15:29 -070078 ['$log', 'FnService', 'WebSocketService',
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -070079
Bri Prebilic Cole9cf1a8d2015-04-21 13:15:29 -070080 function (_$log_, _fs_, _wss_) {
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -070081 $log = _$log_;
Bri Prebilic Cole864cdd62015-04-02 15:46:47 -070082 fs = _fs_;
83 wss = _wss_;
84
85 return {
86 buildTable: buildTable
87 };
88 }]);
89
90}());