Bri Prebilic Cole | 864cdd6 | 2015-04-02 15:46:47 -0700 | [diff] [blame] | 1 | /* |
| 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 |
| 24 | var $log, $window, fs, wss; |
| 25 | |
| 26 | // example params to buildTable: |
| 27 | // { |
Simon Hunt | 35d1888 | 2015-04-02 20:16:26 -0700 | [diff] [blame] | 28 | // self: this, <- controller object |
| 29 | // scope: $scope, <- controller scope |
| 30 | // tag: 'device', <- table identifier |
| 31 | // selCb: selCb <- row selection callback (optional) |
Bri Prebilic Cole | 864cdd6 | 2015-04-02 15:46:47 -0700 | [diff] [blame] | 32 | // } |
Simon Hunt | 35d1888 | 2015-04-02 20:16:26 -0700 | [diff] [blame] | 33 | // Note: selCb() is passed the row data model of the selected row, |
| 34 | // or null when no row is selected. |
Bri Prebilic Cole | 864cdd6 | 2015-04-02 15:46:47 -0700 | [diff] [blame] | 35 | |
| 36 | function buildTable(o) { |
| 37 | var handlers = {}, |
| 38 | root = o.tag + 's', |
| 39 | req = o.tag + 'DataRequest', |
Thomas Vachuska | 619c538 | 2015-04-02 13:41:47 -0700 | [diff] [blame] | 40 | resp = o.tag + 'DataResponse', |
| 41 | onSel = fs.isF(o.selCb); |
Bri Prebilic Cole | 864cdd6 | 2015-04-02 15:46:47 -0700 | [diff] [blame] | 42 | |
| 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 | |
Thomas Vachuska | 619c538 | 2015-04-02 13:41:47 -0700 | [diff] [blame] | 55 | function selCb(sel) { |
| 56 | o.scope.sel = (o.scope.sel === sel) ? null : sel; |
| 57 | onSel && onSel(o.scope.sel); |
| 58 | } |
| 59 | o.scope.selectCallback = selCb; |
| 60 | |
| 61 | |
Bri Prebilic Cole | 864cdd6 | 2015-04-02 15:46:47 -0700 | [diff] [blame] | 62 | 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', |
| 75 | ['$log', '$window', 'FnService', 'WebSocketService', |
| 76 | |
| 77 | function (_$log_, _$window_, _fs_, _wss_) { |
| 78 | $log = _$log_; |
| 79 | $window = _$window_; |
| 80 | fs = _fs_; |
| 81 | wss = _wss_; |
| 82 | |
| 83 | return { |
| 84 | buildTable: buildTable |
| 85 | }; |
| 86 | }]); |
| 87 | |
| 88 | }()); |