Bri Prebilic Cole | 864cdd6 | 2015-04-02 15:46:47 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Bri Prebilic Cole | 864cdd6 | 2015-04-02 15:46:47 -0700 | [diff] [blame] | 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 | /* |
Simon Hunt | 105bc4e | 2017-07-21 11:07:01 -0700 | [diff] [blame] | 18 | ONOS GUI -- Widget -- Table Builder Service |
Bri Prebilic Cole | 864cdd6 | 2015-04-02 15:46:47 -0700 | [diff] [blame] | 19 | */ |
| 20 | (function () { |
| 21 | 'use strict'; |
| 22 | |
| 23 | // injected refs |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 24 | var $log, $interval, fs, wss, ls; |
Bri Prebilic Cole | bfab9c7 | 2015-06-01 14:33:18 -0700 | [diff] [blame] | 25 | |
| 26 | // constants |
Simon Hunt | a50540f | 2016-01-25 11:25:11 -0800 | [diff] [blame] | 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 |
Bri Prebilic Cole | 0bc4de2 | 2015-07-20 17:07:55 -0700 | [diff] [blame] | 33 | // selCb: selCb, <- row selection callback (optional) |
| 34 | // respCb: respCb, <- websocket response callback (optional) |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 35 | // query: params <- query parameters in URL (optional) |
Bri Prebilic Cole | 864cdd6 | 2015-04-02 15:46:47 -0700 | [diff] [blame] | 36 | // } |
Simon Hunt | 35d1888 | 2015-04-02 20:16:26 -0700 | [diff] [blame] | 37 | // Note: selCb() is passed the row data model of the selected row, |
| 38 | // or null when no row is selected. |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 39 | // Note: query is always an object (empty or containing properties) |
| 40 | // it comes from $location.search() |
Bri Prebilic Cole | 864cdd6 | 2015-04-02 15:46:47 -0700 | [diff] [blame] | 41 | |
Simon Hunt | 105bc4e | 2017-07-21 11:07:01 -0700 | [diff] [blame] | 42 | // Additional Notes: |
| 43 | // When sending a request for table data, the $scope will be checked |
| 44 | // for a .payloadParams object which, if it exists, will be merged into |
| 45 | // the event payload. By modifying this object (via toggle buttons, or |
| 46 | // other user interaction) additional parameters / state can be passed |
| 47 | // to the server in the data request. |
| 48 | |
Bri Prebilic Cole | 864cdd6 | 2015-04-02 15:46:47 -0700 | [diff] [blame] | 49 | function buildTable(o) { |
| 50 | var handlers = {}, |
| 51 | root = o.tag + 's', |
| 52 | req = o.tag + 'DataRequest', |
Thomas Vachuska | 619c538 | 2015-04-02 13:41:47 -0700 | [diff] [blame] | 53 | resp = o.tag + 'DataResponse', |
Bri Prebilic Cole | bfab9c7 | 2015-06-01 14:33:18 -0700 | [diff] [blame] | 54 | onSel = fs.isF(o.selCb), |
Bri Prebilic Cole | 522e756 | 2015-06-22 15:56:25 -0700 | [diff] [blame] | 55 | onResp = fs.isF(o.respCb), |
Simon Hunt | 4e41273 | 2015-10-27 15:25:39 -0700 | [diff] [blame] | 56 | idKey = o.idKey || 'id', |
Bri Prebilic Cole | 0bc4de2 | 2015-07-20 17:07:55 -0700 | [diff] [blame] | 57 | oldTableData = [], |
Simon Hunt | a50540f | 2016-01-25 11:25:11 -0800 | [diff] [blame] | 58 | refreshPromise; |
Bri Prebilic Cole | 864cdd6 | 2015-04-02 15:46:47 -0700 | [diff] [blame] | 59 | |
Bri Prebilic Cole | e568ead | 2015-05-01 09:51:28 -0700 | [diff] [blame] | 60 | o.scope.tableData = []; |
Bri Prebilic Cole | 0bc4de2 | 2015-07-20 17:07:55 -0700 | [diff] [blame] | 61 | o.scope.changedData = []; |
Simon Hunt | a678b84 | 2016-01-11 17:14:18 -0800 | [diff] [blame] | 62 | o.scope.sortParams = o.sortParams || {}; |
Bri Prebilic Cole | 41d6765 | 2015-06-02 10:23:04 -0700 | [diff] [blame] | 63 | o.scope.autoRefresh = true; |
Simon Hunt | 94f36fc | 2017-07-12 17:47:17 -0700 | [diff] [blame] | 64 | o.scope.autoRefreshTip = o.lion_toggle_auto_refresh || 'Toggle auto refresh'; |
Bri Prebilic Cole | 864cdd6 | 2015-04-02 15:46:47 -0700 | [diff] [blame] | 65 | |
Bri Prebilic Cole | 0bc4de2 | 2015-07-20 17:07:55 -0700 | [diff] [blame] | 66 | // === websocket functions -------------------- |
Simon Hunt | 105bc4e | 2017-07-21 11:07:01 -0700 | [diff] [blame] | 67 | |
| 68 | // === Table Data Response |
| 69 | function tableDataResponseCb(data) { |
Simon Hunt | 412adc8 | 2015-12-11 15:56:20 -0800 | [diff] [blame] | 70 | ls.stop(); |
Bri Prebilic Cole | e568ead | 2015-05-01 09:51:28 -0700 | [diff] [blame] | 71 | o.scope.tableData = data[root]; |
Jian Li | 8baf447 | 2016-01-15 15:08:09 -0800 | [diff] [blame] | 72 | o.scope.annots = data.annots; |
Bri Prebilic Cole | 522e756 | 2015-06-22 15:56:25 -0700 | [diff] [blame] | 73 | onResp && onResp(); |
Bri Prebilic Cole | 864cdd6 | 2015-04-02 15:46:47 -0700 | [diff] [blame] | 74 | |
Bri Prebilic Cole | 0bc4de2 | 2015-07-20 17:07:55 -0700 | [diff] [blame] | 75 | // checks if data changed for row flashing |
| 76 | if (!angular.equals(o.scope.tableData, oldTableData)) { |
| 77 | o.scope.changedData = []; |
| 78 | // only flash the row if the data already exists |
| 79 | if (oldTableData.length) { |
| 80 | angular.forEach(o.scope.tableData, function (item) { |
| 81 | if (!fs.containsObj(oldTableData, item)) { |
| 82 | o.scope.changedData.push(item); |
| 83 | } |
| 84 | }); |
| 85 | } |
| 86 | angular.copy(o.scope.tableData, oldTableData); |
| 87 | } |
Bri Prebilic Cole | 70aacc4 | 2015-07-22 11:28:34 -0700 | [diff] [blame] | 88 | o.scope.$apply(); |
Bri Prebilic Cole | 0bc4de2 | 2015-07-20 17:07:55 -0700 | [diff] [blame] | 89 | } |
Simon Hunt | 105bc4e | 2017-07-21 11:07:01 -0700 | [diff] [blame] | 90 | handlers[resp] = tableDataResponseCb; |
Bri Prebilic Cole | 0bc4de2 | 2015-07-20 17:07:55 -0700 | [diff] [blame] | 91 | wss.bindHandlers(handlers); |
| 92 | |
Simon Hunt | 105bc4e | 2017-07-21 11:07:01 -0700 | [diff] [blame] | 93 | // === Table Data Request |
| 94 | function requestTableData() { |
| 95 | var sortParams = o.scope.sortParams, |
| 96 | pp = fs.isO(o.scope.payloadParams), |
| 97 | payloadParams = pp || {}, |
| 98 | p = angular.extend({}, sortParams, payloadParams, o.query); |
| 99 | |
Simon Hunt | 412adc8 | 2015-12-11 15:56:20 -0800 | [diff] [blame] | 100 | if (wss.isConnected()) { |
Simon Hunt | 105bc4e | 2017-07-21 11:07:01 -0700 | [diff] [blame] | 101 | if (fs.debugOn('table')) { |
| 102 | $log.debug('Table data REQUEST:', req, p); |
| 103 | } |
Simon Hunt | 412adc8 | 2015-12-11 15:56:20 -0800 | [diff] [blame] | 104 | wss.sendEvent(req, p); |
Simon Hunt | a50540f | 2016-01-25 11:25:11 -0800 | [diff] [blame] | 105 | ls.start(); |
Simon Hunt | 412adc8 | 2015-12-11 15:56:20 -0800 | [diff] [blame] | 106 | } |
Bri Prebilic Cole | 864cdd6 | 2015-04-02 15:46:47 -0700 | [diff] [blame] | 107 | } |
Simon Hunt | 105bc4e | 2017-07-21 11:07:01 -0700 | [diff] [blame] | 108 | o.scope.sortCallback = requestTableData; |
Bri Prebilic Cole | 864cdd6 | 2015-04-02 15:46:47 -0700 | [diff] [blame] | 109 | |
Bri Prebilic Cole | 6e1b4a5 | 2015-08-03 17:10:44 -0700 | [diff] [blame] | 110 | |
Simon Hunt | 105bc4e | 2017-07-21 11:07:01 -0700 | [diff] [blame] | 111 | // === Row Selected |
| 112 | function rowSelectionCb($event, selRow) { |
Simon Hunt | 4e41273 | 2015-10-27 15:25:39 -0700 | [diff] [blame] | 113 | var selId = selRow[idKey]; |
| 114 | o.scope.selId = (o.scope.selId === selId) ? null : selId; |
Bri Prebilic Cole | bfab9c7 | 2015-06-01 14:33:18 -0700 | [diff] [blame] | 115 | onSel && onSel($event, selRow); |
Thomas Vachuska | 619c538 | 2015-04-02 13:41:47 -0700 | [diff] [blame] | 116 | } |
Simon Hunt | 105bc4e | 2017-07-21 11:07:01 -0700 | [diff] [blame] | 117 | o.scope.selectCallback = rowSelectionCb; |
Thomas Vachuska | 619c538 | 2015-04-02 13:41:47 -0700 | [diff] [blame] | 118 | |
Simon Hunt | 105bc4e | 2017-07-21 11:07:01 -0700 | [diff] [blame] | 119 | // === autoRefresh functions |
Simon Hunt | 412adc8 | 2015-12-11 15:56:20 -0800 | [diff] [blame] | 120 | function fetchDataIfNotWaiting() { |
Simon Hunt | a50540f | 2016-01-25 11:25:11 -0800 | [diff] [blame] | 121 | if (!ls.waiting()) { |
Simon Hunt | 4deb0e8 | 2015-06-10 16:18:25 -0700 | [diff] [blame] | 122 | if (fs.debugOn('widget')) { |
| 123 | $log.debug('Refreshing ' + root + ' page'); |
| 124 | } |
Simon Hunt | 105bc4e | 2017-07-21 11:07:01 -0700 | [diff] [blame] | 125 | requestTableData(); |
Simon Hunt | 412adc8 | 2015-12-11 15:56:20 -0800 | [diff] [blame] | 126 | } |
| 127 | } |
| 128 | |
| 129 | function startRefresh() { |
| 130 | refreshPromise = $interval(fetchDataIfNotWaiting, refreshInterval); |
Bri Prebilic Cole | e1be1b7 | 2015-05-12 16:07:24 -0700 | [diff] [blame] | 131 | } |
Bri Prebilic Cole | 41d6765 | 2015-06-02 10:23:04 -0700 | [diff] [blame] | 132 | |
| 133 | function stopRefresh() { |
Simon Hunt | 412adc8 | 2015-12-11 15:56:20 -0800 | [diff] [blame] | 134 | if (refreshPromise) { |
Bri Prebilic Cole | 6e1b4a5 | 2015-08-03 17:10:44 -0700 | [diff] [blame] | 135 | $interval.cancel(refreshPromise); |
Simon Hunt | 412adc8 | 2015-12-11 15:56:20 -0800 | [diff] [blame] | 136 | refreshPromise = null; |
Bri Prebilic Cole | 41d6765 | 2015-06-02 10:23:04 -0700 | [diff] [blame] | 137 | } |
| 138 | } |
| 139 | |
| 140 | function toggleRefresh() { |
| 141 | o.scope.autoRefresh = !o.scope.autoRefresh; |
| 142 | o.scope.autoRefresh ? startRefresh() : stopRefresh(); |
| 143 | } |
| 144 | o.scope.toggleRefresh = toggleRefresh; |
Thomas Vachuska | 619c538 | 2015-04-02 13:41:47 -0700 | [diff] [blame] | 145 | |
Simon Hunt | 105bc4e | 2017-07-21 11:07:01 -0700 | [diff] [blame] | 146 | // === Cleanup on destroyed scope |
Bri Prebilic Cole | 864cdd6 | 2015-04-02 15:46:47 -0700 | [diff] [blame] | 147 | o.scope.$on('$destroy', function () { |
| 148 | wss.unbindHandlers(handlers); |
Bri Prebilic Cole | 41d6765 | 2015-06-02 10:23:04 -0700 | [diff] [blame] | 149 | stopRefresh(); |
Simon Hunt | a50540f | 2016-01-25 11:25:11 -0800 | [diff] [blame] | 150 | ls.stop(); |
Bri Prebilic Cole | 864cdd6 | 2015-04-02 15:46:47 -0700 | [diff] [blame] | 151 | }); |
| 152 | |
Simon Hunt | 105bc4e | 2017-07-21 11:07:01 -0700 | [diff] [blame] | 153 | requestTableData(); |
Bri Prebilic Cole | 41d6765 | 2015-06-02 10:23:04 -0700 | [diff] [blame] | 154 | startRefresh(); |
Viswanath KSP | d25440b | 2017-07-21 13:48:06 +0530 | [diff] [blame] | 155 | |
| 156 | return { |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 157 | forceRefesh: requestTableData, |
Viswanath KSP | d25440b | 2017-07-21 13:48:06 +0530 | [diff] [blame] | 158 | }; |
Bri Prebilic Cole | 864cdd6 | 2015-04-02 15:46:47 -0700 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | angular.module('onosWidget') |
| 162 | .factory('TableBuilderService', |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 163 | ['$log', '$interval', 'FnService', 'WebSocketService', |
Simon Hunt | 412adc8 | 2015-12-11 15:56:20 -0800 | [diff] [blame] | 164 | 'LoadingService', |
Bri Prebilic Cole | 864cdd6 | 2015-04-02 15:46:47 -0700 | [diff] [blame] | 165 | |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 166 | function (_$log_, _$interval_, _fs_, _wss_, _ls_) { |
Bri Prebilic Cole | 864cdd6 | 2015-04-02 15:46:47 -0700 | [diff] [blame] | 167 | $log = _$log_; |
Bri Prebilic Cole | bfab9c7 | 2015-06-01 14:33:18 -0700 | [diff] [blame] | 168 | $interval = _$interval_; |
Bri Prebilic Cole | 864cdd6 | 2015-04-02 15:46:47 -0700 | [diff] [blame] | 169 | fs = _fs_; |
| 170 | wss = _wss_; |
Simon Hunt | 412adc8 | 2015-12-11 15:56:20 -0800 | [diff] [blame] | 171 | ls = _ls_; |
Bri Prebilic Cole | 864cdd6 | 2015-04-02 15:46:47 -0700 | [diff] [blame] | 172 | |
| 173 | return { |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 174 | buildTable: buildTable, |
Bri Prebilic Cole | 864cdd6 | 2015-04-02 15:46:47 -0700 | [diff] [blame] | 175 | }; |
| 176 | }]); |
| 177 | |
| 178 | }()); |