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 |
Bri Prebilic Cole | bfab9c7 | 2015-06-01 14:33:18 -0700 | [diff] [blame] | 24 | var $log, $interval, fs, wss, ts; |
| 25 | |
| 26 | // constants |
| 27 | var refreshInterval = 2000; |
Bri Prebilic Cole | 864cdd6 | 2015-04-02 15:46:47 -0700 | [diff] [blame] | 28 | |
| 29 | // example params to buildTable: |
| 30 | // { |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 31 | // 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 Cole | 864cdd6 | 2015-04-02 15:46:47 -0700 | [diff] [blame] | 35 | // } |
Simon Hunt | 35d1888 | 2015-04-02 20:16:26 -0700 | [diff] [blame] | 36 | // Note: selCb() is passed the row data model of the selected row, |
| 37 | // or null when no row is selected. |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 38 | // Note: query is always an object (empty or containing properties) |
| 39 | // it comes from $location.search() |
Bri Prebilic Cole | 864cdd6 | 2015-04-02 15:46:47 -0700 | [diff] [blame] | 40 | |
| 41 | function buildTable(o) { |
| 42 | var handlers = {}, |
| 43 | root = o.tag + 's', |
| 44 | req = o.tag + 'DataRequest', |
Thomas Vachuska | 619c538 | 2015-04-02 13:41:47 -0700 | [diff] [blame] | 45 | resp = o.tag + 'DataResponse', |
Bri Prebilic Cole | bfab9c7 | 2015-06-01 14:33:18 -0700 | [diff] [blame] | 46 | onSel = fs.isF(o.selCb), |
Bri Prebilic Cole | 522e756 | 2015-06-22 15:56:25 -0700 | [diff] [blame] | 47 | onResp = fs.isF(o.respCb), |
Bri Prebilic Cole | bfab9c7 | 2015-06-01 14:33:18 -0700 | [diff] [blame] | 48 | promise; |
Bri Prebilic Cole | 864cdd6 | 2015-04-02 15:46:47 -0700 | [diff] [blame] | 49 | |
Bri Prebilic Cole | e568ead | 2015-05-01 09:51:28 -0700 | [diff] [blame] | 50 | o.scope.tableData = []; |
Bri Prebilic Cole | bfab9c7 | 2015-06-01 14:33:18 -0700 | [diff] [blame] | 51 | o.scope.sortParams = {}; |
Bri Prebilic Cole | 41d6765 | 2015-06-02 10:23:04 -0700 | [diff] [blame] | 52 | o.scope.autoRefresh = true; |
Bri Prebilic Cole | eef67ae | 2015-07-01 16:26:59 -0700 | [diff] [blame^] | 53 | o.scope.autoRefreshTip = 'Toggle auto refresh'; |
Bri Prebilic Cole | 864cdd6 | 2015-04-02 15:46:47 -0700 | [diff] [blame] | 54 | |
| 55 | function respCb(data) { |
Bri Prebilic Cole | e568ead | 2015-05-01 09:51:28 -0700 | [diff] [blame] | 56 | o.scope.tableData = data[root]; |
Bri Prebilic Cole | 522e756 | 2015-06-22 15:56:25 -0700 | [diff] [blame] | 57 | onResp && onResp(); |
Bri Prebilic Cole | 864cdd6 | 2015-04-02 15:46:47 -0700 | [diff] [blame] | 58 | o.scope.$apply(); |
| 59 | } |
| 60 | |
| 61 | function sortCb(params) { |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 62 | var p = angular.extend({}, params, o.query); |
| 63 | wss.sendEvent(req, p); |
Bri Prebilic Cole | 864cdd6 | 2015-04-02 15:46:47 -0700 | [diff] [blame] | 64 | } |
| 65 | o.scope.sortCallback = sortCb; |
| 66 | |
Bri Prebilic Cole | bfab9c7 | 2015-06-01 14:33:18 -0700 | [diff] [blame] | 67 | function selCb($event, selRow) { |
| 68 | o.scope.selId = (o.scope.selId === selRow.id) ? null : selRow.id; |
| 69 | onSel && onSel($event, selRow); |
Thomas Vachuska | 619c538 | 2015-04-02 13:41:47 -0700 | [diff] [blame] | 70 | } |
| 71 | o.scope.selectCallback = selCb; |
| 72 | |
Bri Prebilic Cole | 41d6765 | 2015-06-02 10:23:04 -0700 | [diff] [blame] | 73 | function startRefresh() { |
| 74 | promise = $interval(function () { |
Simon Hunt | 4deb0e8 | 2015-06-10 16:18:25 -0700 | [diff] [blame] | 75 | if (fs.debugOn('widget')) { |
| 76 | $log.debug('Refreshing ' + root + ' page'); |
| 77 | } |
Bri Prebilic Cole | 41d6765 | 2015-06-02 10:23:04 -0700 | [diff] [blame] | 78 | sortCb(o.scope.sortParams); |
| 79 | }, refreshInterval); |
Bri Prebilic Cole | e1be1b7 | 2015-05-12 16:07:24 -0700 | [diff] [blame] | 80 | } |
Bri Prebilic Cole | 41d6765 | 2015-06-02 10:23:04 -0700 | [diff] [blame] | 81 | |
| 82 | function stopRefresh() { |
| 83 | if (angular.isDefined(promise)) { |
| 84 | $interval.cancel(promise); |
| 85 | promise = undefined; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | function toggleRefresh() { |
| 90 | o.scope.autoRefresh = !o.scope.autoRefresh; |
| 91 | o.scope.autoRefresh ? startRefresh() : stopRefresh(); |
| 92 | } |
| 93 | o.scope.toggleRefresh = toggleRefresh; |
Thomas Vachuska | 619c538 | 2015-04-02 13:41:47 -0700 | [diff] [blame] | 94 | |
Bri Prebilic Cole | 864cdd6 | 2015-04-02 15:46:47 -0700 | [diff] [blame] | 95 | handlers[resp] = respCb; |
| 96 | wss.bindHandlers(handlers); |
| 97 | |
| 98 | // Cleanup on destroyed scope |
| 99 | o.scope.$on('$destroy', function () { |
| 100 | wss.unbindHandlers(handlers); |
Bri Prebilic Cole | c244976 | 2015-05-29 14:30:51 -0700 | [diff] [blame] | 101 | ts.resetSort(); |
Bri Prebilic Cole | 41d6765 | 2015-06-02 10:23:04 -0700 | [diff] [blame] | 102 | stopRefresh(); |
Bri Prebilic Cole | 864cdd6 | 2015-04-02 15:46:47 -0700 | [diff] [blame] | 103 | }); |
| 104 | |
| 105 | sortCb(); |
Bri Prebilic Cole | 41d6765 | 2015-06-02 10:23:04 -0700 | [diff] [blame] | 106 | startRefresh(); |
Bri Prebilic Cole | 864cdd6 | 2015-04-02 15:46:47 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | angular.module('onosWidget') |
| 110 | .factory('TableBuilderService', |
Bri Prebilic Cole | bfab9c7 | 2015-06-01 14:33:18 -0700 | [diff] [blame] | 111 | ['$log', '$interval', 'FnService', 'WebSocketService', 'TableService', |
Bri Prebilic Cole | 864cdd6 | 2015-04-02 15:46:47 -0700 | [diff] [blame] | 112 | |
Bri Prebilic Cole | bfab9c7 | 2015-06-01 14:33:18 -0700 | [diff] [blame] | 113 | function (_$log_, _$interval_, _fs_, _wss_, _ts_) { |
Bri Prebilic Cole | 864cdd6 | 2015-04-02 15:46:47 -0700 | [diff] [blame] | 114 | $log = _$log_; |
Bri Prebilic Cole | bfab9c7 | 2015-06-01 14:33:18 -0700 | [diff] [blame] | 115 | $interval = _$interval_; |
Bri Prebilic Cole | 864cdd6 | 2015-04-02 15:46:47 -0700 | [diff] [blame] | 116 | fs = _fs_; |
| 117 | wss = _wss_; |
Bri Prebilic Cole | e1be1b7 | 2015-05-12 16:07:24 -0700 | [diff] [blame] | 118 | ts = _ts_; |
Bri Prebilic Cole | 864cdd6 | 2015-04-02 15:46:47 -0700 | [diff] [blame] | 119 | |
| 120 | return { |
| 121 | buildTable: buildTable |
| 122 | }; |
| 123 | }]); |
| 124 | |
| 125 | }()); |