Bri Prebilic Cole | 864cdd6 | 2015-04-02 15:46:47 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
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 | /* |
| 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 |
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 | |
| 42 | function buildTable(o) { |
| 43 | var handlers = {}, |
| 44 | root = o.tag + 's', |
| 45 | req = o.tag + 'DataRequest', |
Thomas Vachuska | 619c538 | 2015-04-02 13:41:47 -0700 | [diff] [blame] | 46 | resp = o.tag + 'DataResponse', |
Bri Prebilic Cole | bfab9c7 | 2015-06-01 14:33:18 -0700 | [diff] [blame] | 47 | onSel = fs.isF(o.selCb), |
Bri Prebilic Cole | 522e756 | 2015-06-22 15:56:25 -0700 | [diff] [blame] | 48 | onResp = fs.isF(o.respCb), |
Simon Hunt | 4e41273 | 2015-10-27 15:25:39 -0700 | [diff] [blame] | 49 | idKey = o.idKey || 'id', |
Bri Prebilic Cole | 0bc4de2 | 2015-07-20 17:07:55 -0700 | [diff] [blame] | 50 | oldTableData = [], |
Simon Hunt | a50540f | 2016-01-25 11:25:11 -0800 | [diff] [blame] | 51 | refreshPromise; |
Bri Prebilic Cole | 864cdd6 | 2015-04-02 15:46:47 -0700 | [diff] [blame] | 52 | |
Bri Prebilic Cole | e568ead | 2015-05-01 09:51:28 -0700 | [diff] [blame] | 53 | o.scope.tableData = []; |
Bri Prebilic Cole | 0bc4de2 | 2015-07-20 17:07:55 -0700 | [diff] [blame] | 54 | o.scope.changedData = []; |
Simon Hunt | a678b84 | 2016-01-11 17:14:18 -0800 | [diff] [blame] | 55 | o.scope.sortParams = o.sortParams || {}; |
Bri Prebilic Cole | 41d6765 | 2015-06-02 10:23:04 -0700 | [diff] [blame] | 56 | o.scope.autoRefresh = true; |
Bri Prebilic Cole | eef67ae | 2015-07-01 16:26:59 -0700 | [diff] [blame] | 57 | o.scope.autoRefreshTip = 'Toggle auto refresh'; |
Bri Prebilic Cole | 864cdd6 | 2015-04-02 15:46:47 -0700 | [diff] [blame] | 58 | |
Bri Prebilic Cole | 0bc4de2 | 2015-07-20 17:07:55 -0700 | [diff] [blame] | 59 | // === websocket functions -------------------- |
| 60 | // response |
Bri Prebilic Cole | 864cdd6 | 2015-04-02 15:46:47 -0700 | [diff] [blame] | 61 | function respCb(data) { |
Simon Hunt | 412adc8 | 2015-12-11 15:56:20 -0800 | [diff] [blame] | 62 | ls.stop(); |
Bri Prebilic Cole | e568ead | 2015-05-01 09:51:28 -0700 | [diff] [blame] | 63 | o.scope.tableData = data[root]; |
Jian Li | 8baf447 | 2016-01-15 15:08:09 -0800 | [diff] [blame] | 64 | o.scope.annots = data.annots; |
Bri Prebilic Cole | 522e756 | 2015-06-22 15:56:25 -0700 | [diff] [blame] | 65 | onResp && onResp(); |
Bri Prebilic Cole | 864cdd6 | 2015-04-02 15:46:47 -0700 | [diff] [blame] | 66 | |
Bri Prebilic Cole | 0bc4de2 | 2015-07-20 17:07:55 -0700 | [diff] [blame] | 67 | // checks if data changed for row flashing |
| 68 | if (!angular.equals(o.scope.tableData, oldTableData)) { |
| 69 | o.scope.changedData = []; |
| 70 | // only flash the row if the data already exists |
| 71 | if (oldTableData.length) { |
| 72 | angular.forEach(o.scope.tableData, function (item) { |
| 73 | if (!fs.containsObj(oldTableData, item)) { |
| 74 | o.scope.changedData.push(item); |
| 75 | } |
| 76 | }); |
| 77 | } |
| 78 | angular.copy(o.scope.tableData, oldTableData); |
| 79 | } |
Bri Prebilic Cole | 70aacc4 | 2015-07-22 11:28:34 -0700 | [diff] [blame] | 80 | o.scope.$apply(); |
Bri Prebilic Cole | 0bc4de2 | 2015-07-20 17:07:55 -0700 | [diff] [blame] | 81 | } |
| 82 | handlers[resp] = respCb; |
| 83 | wss.bindHandlers(handlers); |
| 84 | |
| 85 | // request |
Bri Prebilic Cole | 864cdd6 | 2015-04-02 15:46:47 -0700 | [diff] [blame] | 86 | function sortCb(params) { |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 87 | var p = angular.extend({}, params, o.query); |
Simon Hunt | 412adc8 | 2015-12-11 15:56:20 -0800 | [diff] [blame] | 88 | if (wss.isConnected()) { |
| 89 | wss.sendEvent(req, p); |
Simon Hunt | a50540f | 2016-01-25 11:25:11 -0800 | [diff] [blame] | 90 | ls.start(); |
Simon Hunt | 412adc8 | 2015-12-11 15:56:20 -0800 | [diff] [blame] | 91 | } |
Bri Prebilic Cole | 864cdd6 | 2015-04-02 15:46:47 -0700 | [diff] [blame] | 92 | } |
| 93 | o.scope.sortCallback = sortCb; |
| 94 | |
Bri Prebilic Cole | 6e1b4a5 | 2015-08-03 17:10:44 -0700 | [diff] [blame] | 95 | |
Bri Prebilic Cole | 0bc4de2 | 2015-07-20 17:07:55 -0700 | [diff] [blame] | 96 | // === selecting a row functions ---------------- |
Bri Prebilic Cole | bfab9c7 | 2015-06-01 14:33:18 -0700 | [diff] [blame] | 97 | function selCb($event, selRow) { |
Simon Hunt | 4e41273 | 2015-10-27 15:25:39 -0700 | [diff] [blame] | 98 | var selId = selRow[idKey]; |
| 99 | o.scope.selId = (o.scope.selId === selId) ? null : selId; |
Bri Prebilic Cole | bfab9c7 | 2015-06-01 14:33:18 -0700 | [diff] [blame] | 100 | onSel && onSel($event, selRow); |
Thomas Vachuska | 619c538 | 2015-04-02 13:41:47 -0700 | [diff] [blame] | 101 | } |
| 102 | o.scope.selectCallback = selCb; |
| 103 | |
Bri Prebilic Cole | 0bc4de2 | 2015-07-20 17:07:55 -0700 | [diff] [blame] | 104 | // === autoRefresh functions ------------------ |
Simon Hunt | 412adc8 | 2015-12-11 15:56:20 -0800 | [diff] [blame] | 105 | function fetchDataIfNotWaiting() { |
Simon Hunt | a50540f | 2016-01-25 11:25:11 -0800 | [diff] [blame] | 106 | if (!ls.waiting()) { |
Simon Hunt | 4deb0e8 | 2015-06-10 16:18:25 -0700 | [diff] [blame] | 107 | if (fs.debugOn('widget')) { |
| 108 | $log.debug('Refreshing ' + root + ' page'); |
| 109 | } |
Bri Prebilic Cole | 41d6765 | 2015-06-02 10:23:04 -0700 | [diff] [blame] | 110 | sortCb(o.scope.sortParams); |
Simon Hunt | 412adc8 | 2015-12-11 15:56:20 -0800 | [diff] [blame] | 111 | } |
| 112 | } |
| 113 | |
| 114 | function startRefresh() { |
| 115 | refreshPromise = $interval(fetchDataIfNotWaiting, refreshInterval); |
Bri Prebilic Cole | e1be1b7 | 2015-05-12 16:07:24 -0700 | [diff] [blame] | 116 | } |
Bri Prebilic Cole | 41d6765 | 2015-06-02 10:23:04 -0700 | [diff] [blame] | 117 | |
| 118 | function stopRefresh() { |
Simon Hunt | 412adc8 | 2015-12-11 15:56:20 -0800 | [diff] [blame] | 119 | if (refreshPromise) { |
Bri Prebilic Cole | 6e1b4a5 | 2015-08-03 17:10:44 -0700 | [diff] [blame] | 120 | $interval.cancel(refreshPromise); |
Simon Hunt | 412adc8 | 2015-12-11 15:56:20 -0800 | [diff] [blame] | 121 | refreshPromise = null; |
Bri Prebilic Cole | 41d6765 | 2015-06-02 10:23:04 -0700 | [diff] [blame] | 122 | } |
| 123 | } |
| 124 | |
| 125 | function toggleRefresh() { |
| 126 | o.scope.autoRefresh = !o.scope.autoRefresh; |
| 127 | o.scope.autoRefresh ? startRefresh() : stopRefresh(); |
| 128 | } |
| 129 | o.scope.toggleRefresh = toggleRefresh; |
Thomas Vachuska | 619c538 | 2015-04-02 13:41:47 -0700 | [diff] [blame] | 130 | |
Bri Prebilic Cole | 0bc4de2 | 2015-07-20 17:07:55 -0700 | [diff] [blame] | 131 | // === Cleanup on destroyed scope ----------------- |
Bri Prebilic Cole | 864cdd6 | 2015-04-02 15:46:47 -0700 | [diff] [blame] | 132 | o.scope.$on('$destroy', function () { |
| 133 | wss.unbindHandlers(handlers); |
Bri Prebilic Cole | 41d6765 | 2015-06-02 10:23:04 -0700 | [diff] [blame] | 134 | stopRefresh(); |
Simon Hunt | a50540f | 2016-01-25 11:25:11 -0800 | [diff] [blame] | 135 | ls.stop(); |
Bri Prebilic Cole | 864cdd6 | 2015-04-02 15:46:47 -0700 | [diff] [blame] | 136 | }); |
| 137 | |
Simon Hunt | a678b84 | 2016-01-11 17:14:18 -0800 | [diff] [blame] | 138 | sortCb(o.scope.sortParams); |
Bri Prebilic Cole | 41d6765 | 2015-06-02 10:23:04 -0700 | [diff] [blame] | 139 | startRefresh(); |
Bri Prebilic Cole | 864cdd6 | 2015-04-02 15:46:47 -0700 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | angular.module('onosWidget') |
| 143 | .factory('TableBuilderService', |
Bri Prebilic Cole | 6e1b4a5 | 2015-08-03 17:10:44 -0700 | [diff] [blame] | 144 | ['$log', '$interval', '$timeout', 'FnService', 'WebSocketService', |
Simon Hunt | 412adc8 | 2015-12-11 15:56:20 -0800 | [diff] [blame] | 145 | 'LoadingService', |
Bri Prebilic Cole | 864cdd6 | 2015-04-02 15:46:47 -0700 | [diff] [blame] | 146 | |
Simon Hunt | 412adc8 | 2015-12-11 15:56:20 -0800 | [diff] [blame] | 147 | function (_$log_, _$interval_, _$timeout_, _fs_, _wss_, _ls_) { |
Bri Prebilic Cole | 864cdd6 | 2015-04-02 15:46:47 -0700 | [diff] [blame] | 148 | $log = _$log_; |
Bri Prebilic Cole | bfab9c7 | 2015-06-01 14:33:18 -0700 | [diff] [blame] | 149 | $interval = _$interval_; |
Bri Prebilic Cole | 6e1b4a5 | 2015-08-03 17:10:44 -0700 | [diff] [blame] | 150 | $timeout = _$timeout_; |
Bri Prebilic Cole | 864cdd6 | 2015-04-02 15:46:47 -0700 | [diff] [blame] | 151 | fs = _fs_; |
| 152 | wss = _wss_; |
Simon Hunt | 412adc8 | 2015-12-11 15:56:20 -0800 | [diff] [blame] | 153 | ls = _ls_; |
Bri Prebilic Cole | 864cdd6 | 2015-04-02 15:46:47 -0700 | [diff] [blame] | 154 | |
| 155 | return { |
| 156 | buildTable: buildTable |
| 157 | }; |
| 158 | }]); |
| 159 | |
| 160 | }()); |