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 |
Simon Hunt | 412adc8 | 2015-12-11 15:56:20 -0800 | [diff] [blame] | 24 | var $log, $interval, $timeout, fs, wss, ls; |
Bri Prebilic Cole | bfab9c7 | 2015-06-01 14:33:18 -0700 | [diff] [blame] | 25 | |
| 26 | // constants |
Bri Prebilic Cole | 6e1b4a5 | 2015-08-03 17:10:44 -0700 | [diff] [blame] | 27 | var refreshInterval = 2000, |
Simon Hunt | 412adc8 | 2015-12-11 15:56:20 -0800 | [diff] [blame] | 28 | tardyWait = 500; |
Bri Prebilic Cole | 864cdd6 | 2015-04-02 15:46:47 -0700 | [diff] [blame] | 29 | |
| 30 | // example params to buildTable: |
| 31 | // { |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 32 | // scope: $scope, <- controller scope |
| 33 | // tag: 'device', <- table identifier |
Bri Prebilic Cole | 0bc4de2 | 2015-07-20 17:07:55 -0700 | [diff] [blame] | 34 | // selCb: selCb, <- row selection callback (optional) |
| 35 | // respCb: respCb, <- websocket response callback (optional) |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 36 | // query: params <- query parameters in URL (optional) |
Bri Prebilic Cole | 864cdd6 | 2015-04-02 15:46:47 -0700 | [diff] [blame] | 37 | // } |
Simon Hunt | 35d1888 | 2015-04-02 20:16:26 -0700 | [diff] [blame] | 38 | // Note: selCb() is passed the row data model of the selected row, |
| 39 | // or null when no row is selected. |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 40 | // Note: query is always an object (empty or containing properties) |
| 41 | // it comes from $location.search() |
Bri Prebilic Cole | 864cdd6 | 2015-04-02 15:46:47 -0700 | [diff] [blame] | 42 | |
| 43 | function buildTable(o) { |
| 44 | var handlers = {}, |
| 45 | root = o.tag + 's', |
| 46 | req = o.tag + 'DataRequest', |
Thomas Vachuska | 619c538 | 2015-04-02 13:41:47 -0700 | [diff] [blame] | 47 | resp = o.tag + 'DataResponse', |
Bri Prebilic Cole | bfab9c7 | 2015-06-01 14:33:18 -0700 | [diff] [blame] | 48 | onSel = fs.isF(o.selCb), |
Bri Prebilic Cole | 522e756 | 2015-06-22 15:56:25 -0700 | [diff] [blame] | 49 | onResp = fs.isF(o.respCb), |
Simon Hunt | 4e41273 | 2015-10-27 15:25:39 -0700 | [diff] [blame] | 50 | idKey = o.idKey || 'id', |
Bri Prebilic Cole | 0bc4de2 | 2015-07-20 17:07:55 -0700 | [diff] [blame] | 51 | oldTableData = [], |
Simon Hunt | 412adc8 | 2015-12-11 15:56:20 -0800 | [diff] [blame] | 52 | refreshPromise, |
| 53 | tardyPromise; |
Bri Prebilic Cole | 864cdd6 | 2015-04-02 15:46:47 -0700 | [diff] [blame] | 54 | |
Bri Prebilic Cole | e568ead | 2015-05-01 09:51:28 -0700 | [diff] [blame] | 55 | o.scope.tableData = []; |
Bri Prebilic Cole | 0bc4de2 | 2015-07-20 17:07:55 -0700 | [diff] [blame] | 56 | o.scope.changedData = []; |
Simon Hunt | a678b84 | 2016-01-11 17:14:18 -0800 | [diff] [blame] | 57 | o.scope.sortParams = o.sortParams || {}; |
Bri Prebilic Cole | 41d6765 | 2015-06-02 10:23:04 -0700 | [diff] [blame] | 58 | o.scope.autoRefresh = true; |
Bri Prebilic Cole | eef67ae | 2015-07-01 16:26:59 -0700 | [diff] [blame] | 59 | o.scope.autoRefreshTip = 'Toggle auto refresh'; |
Bri Prebilic Cole | 864cdd6 | 2015-04-02 15:46:47 -0700 | [diff] [blame] | 60 | |
Bri Prebilic Cole | 0bc4de2 | 2015-07-20 17:07:55 -0700 | [diff] [blame] | 61 | // === websocket functions -------------------- |
| 62 | // response |
Bri Prebilic Cole | 864cdd6 | 2015-04-02 15:46:47 -0700 | [diff] [blame] | 63 | function respCb(data) { |
Simon Hunt | 412adc8 | 2015-12-11 15:56:20 -0800 | [diff] [blame] | 64 | cancelTardy(); |
| 65 | ls.stop(); |
Bri Prebilic Cole | e568ead | 2015-05-01 09:51:28 -0700 | [diff] [blame] | 66 | o.scope.tableData = data[root]; |
Jian Li | 8baf447 | 2016-01-15 15:08:09 -0800 | [diff] [blame] | 67 | o.scope.annots = data.annots; |
Bri Prebilic Cole | 522e756 | 2015-06-22 15:56:25 -0700 | [diff] [blame] | 68 | onResp && onResp(); |
Bri Prebilic Cole | 864cdd6 | 2015-04-02 15:46:47 -0700 | [diff] [blame] | 69 | |
Bri Prebilic Cole | 0bc4de2 | 2015-07-20 17:07:55 -0700 | [diff] [blame] | 70 | // checks if data changed for row flashing |
| 71 | if (!angular.equals(o.scope.tableData, oldTableData)) { |
| 72 | o.scope.changedData = []; |
| 73 | // only flash the row if the data already exists |
| 74 | if (oldTableData.length) { |
| 75 | angular.forEach(o.scope.tableData, function (item) { |
| 76 | if (!fs.containsObj(oldTableData, item)) { |
| 77 | o.scope.changedData.push(item); |
| 78 | } |
| 79 | }); |
| 80 | } |
| 81 | angular.copy(o.scope.tableData, oldTableData); |
| 82 | } |
Bri Prebilic Cole | 70aacc4 | 2015-07-22 11:28:34 -0700 | [diff] [blame] | 83 | o.scope.$apply(); |
Bri Prebilic Cole | 0bc4de2 | 2015-07-20 17:07:55 -0700 | [diff] [blame] | 84 | } |
| 85 | handlers[resp] = respCb; |
| 86 | wss.bindHandlers(handlers); |
| 87 | |
Simon Hunt | 412adc8 | 2015-12-11 15:56:20 -0800 | [diff] [blame] | 88 | // handle "loading..." animation |
| 89 | function scheduleTardy() { |
| 90 | tardyPromise = $timeout(ls.start, tardyWait); |
| 91 | } |
| 92 | |
| 93 | function cancelTardy() { |
| 94 | if (tardyPromise) { |
| 95 | $timeout.cancel(tardyPromise); |
| 96 | tardyPromise = null; |
| 97 | } |
| 98 | } |
| 99 | |
Bri Prebilic Cole | 0bc4de2 | 2015-07-20 17:07:55 -0700 | [diff] [blame] | 100 | // request |
Bri Prebilic Cole | 864cdd6 | 2015-04-02 15:46:47 -0700 | [diff] [blame] | 101 | function sortCb(params) { |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 102 | var p = angular.extend({}, params, o.query); |
Simon Hunt | 412adc8 | 2015-12-11 15:56:20 -0800 | [diff] [blame] | 103 | if (wss.isConnected()) { |
| 104 | wss.sendEvent(req, p); |
| 105 | scheduleTardy(); |
| 106 | } |
Bri Prebilic Cole | 864cdd6 | 2015-04-02 15:46:47 -0700 | [diff] [blame] | 107 | } |
| 108 | o.scope.sortCallback = sortCb; |
| 109 | |
Bri Prebilic Cole | 6e1b4a5 | 2015-08-03 17:10:44 -0700 | [diff] [blame] | 110 | |
Bri Prebilic Cole | 0bc4de2 | 2015-07-20 17:07:55 -0700 | [diff] [blame] | 111 | // === selecting a row functions ---------------- |
Bri Prebilic Cole | bfab9c7 | 2015-06-01 14:33:18 -0700 | [diff] [blame] | 112 | function selCb($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 | } |
| 117 | o.scope.selectCallback = selCb; |
| 118 | |
Bri Prebilic Cole | 0bc4de2 | 2015-07-20 17:07:55 -0700 | [diff] [blame] | 119 | // === autoRefresh functions ------------------ |
Simon Hunt | 412adc8 | 2015-12-11 15:56:20 -0800 | [diff] [blame] | 120 | function fetchDataIfNotWaiting() { |
| 121 | if (!tardyPromise) { |
Simon Hunt | 4deb0e8 | 2015-06-10 16:18:25 -0700 | [diff] [blame] | 122 | if (fs.debugOn('widget')) { |
| 123 | $log.debug('Refreshing ' + root + ' page'); |
| 124 | } |
Bri Prebilic Cole | 41d6765 | 2015-06-02 10:23:04 -0700 | [diff] [blame] | 125 | sortCb(o.scope.sortParams); |
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 | |
Bri Prebilic Cole | 0bc4de2 | 2015-07-20 17:07:55 -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 | 412adc8 | 2015-12-11 15:56:20 -0800 | [diff] [blame] | 150 | cancelTardy(); |
Bri Prebilic Cole | 864cdd6 | 2015-04-02 15:46:47 -0700 | [diff] [blame] | 151 | }); |
| 152 | |
Simon Hunt | a678b84 | 2016-01-11 17:14:18 -0800 | [diff] [blame] | 153 | sortCb(o.scope.sortParams); |
Bri Prebilic Cole | 41d6765 | 2015-06-02 10:23:04 -0700 | [diff] [blame] | 154 | startRefresh(); |
Bri Prebilic Cole | 864cdd6 | 2015-04-02 15:46:47 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | angular.module('onosWidget') |
| 158 | .factory('TableBuilderService', |
Bri Prebilic Cole | 6e1b4a5 | 2015-08-03 17:10:44 -0700 | [diff] [blame] | 159 | ['$log', '$interval', '$timeout', 'FnService', 'WebSocketService', |
Simon Hunt | 412adc8 | 2015-12-11 15:56:20 -0800 | [diff] [blame] | 160 | 'LoadingService', |
Bri Prebilic Cole | 864cdd6 | 2015-04-02 15:46:47 -0700 | [diff] [blame] | 161 | |
Simon Hunt | 412adc8 | 2015-12-11 15:56:20 -0800 | [diff] [blame] | 162 | function (_$log_, _$interval_, _$timeout_, _fs_, _wss_, _ls_) { |
Bri Prebilic Cole | 864cdd6 | 2015-04-02 15:46:47 -0700 | [diff] [blame] | 163 | $log = _$log_; |
Bri Prebilic Cole | bfab9c7 | 2015-06-01 14:33:18 -0700 | [diff] [blame] | 164 | $interval = _$interval_; |
Bri Prebilic Cole | 6e1b4a5 | 2015-08-03 17:10:44 -0700 | [diff] [blame] | 165 | $timeout = _$timeout_; |
Bri Prebilic Cole | 864cdd6 | 2015-04-02 15:46:47 -0700 | [diff] [blame] | 166 | fs = _fs_; |
| 167 | wss = _wss_; |
Simon Hunt | 412adc8 | 2015-12-11 15:56:20 -0800 | [diff] [blame] | 168 | ls = _ls_; |
Bri Prebilic Cole | 864cdd6 | 2015-04-02 15:46:47 -0700 | [diff] [blame] | 169 | |
| 170 | return { |
| 171 | buildTable: buildTable |
| 172 | }; |
| 173 | }]); |
| 174 | |
| 175 | }()); |