Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present 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 -- Topology Region Module. |
| 19 | Module that holds the current region in memory |
| 20 | */ |
| 21 | |
| 22 | (function () { |
| 23 | 'use strict'; |
| 24 | |
Steven Burrows | 6deb4ce | 2016-08-26 16:06:23 +0100 | [diff] [blame] | 25 | // Injected Services |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 26 | var Model; |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 27 | |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 28 | // Internal |
Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame^] | 29 | var instance |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 30 | |
Steven Burrows | 8ea5dea | 2016-12-27 13:28:41 +0000 | [diff] [blame] | 31 | // 'static' vars |
| 32 | var ROOT = '(root)'; |
| 33 | |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 34 | angular.module('ovTopo2') |
Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame^] | 35 | .factory('Topo2RegionService', [ |
| 36 | '$log', 'Topo2Model', 'Topo2SubRegionService', 'Topo2DeviceService', |
Steven Burrows | 1aa4f58 | 2016-12-13 15:05:41 -0500 | [diff] [blame] | 37 | 'Topo2HostService', 'Topo2LinkService', 'Topo2ZoomService', 'Topo2DetailsPanelService', |
Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame^] | 38 | 'Topo2BreadcrumbService', 'Topo2ViewController', |
| 39 | function ($log, _Model_, t2sr, t2ds, t2hs, t2ls, t2zs, t2dps, t2bcs, ViewController) { |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 40 | |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 41 | Model = _Model_; |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 42 | |
Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame^] | 43 | var Region = ViewController.extend({ |
| 44 | initialize: function () { |
| 45 | instance = this; |
| 46 | this.model = null; |
| 47 | }, |
| 48 | addRegion: function (data) { |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 49 | |
Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame^] | 50 | var RegionModel = Model.extend({ |
| 51 | findNodeById: this.findNodeById, |
| 52 | nodes: this.regionNodes.bind(this) |
| 53 | }); |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 54 | |
Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame^] | 55 | this.model = new RegionModel({ |
| 56 | id: data.id, |
| 57 | layerOrder: data.layerOrder |
| 58 | }); |
| 59 | |
| 60 | this.model.set({ |
| 61 | subregions: t2sr.createSubRegionCollection(data.subregions, this), |
| 62 | devices: t2ds.createDeviceCollection(data.devices, this), |
| 63 | hosts: t2hs.createHostCollection(data.hosts, this), |
| 64 | links: t2ls.createLinkCollection(data.links, this) |
| 65 | }); |
| 66 | |
| 67 | angular.forEach(this.model.get('links').models, function (link) { |
| 68 | link.createLink(); |
| 69 | }); |
| 70 | |
| 71 | // Hide Breadcrumbs if there are no subregions configured in the root region |
| 72 | if (this.isRootRegion() && !this.model.get('subregions').models.length) { |
| 73 | t2bcs.hide(); |
| 74 | } |
| 75 | }, |
| 76 | isRootRegion: function () { |
| 77 | return this.model.get('id') === ROOT; |
| 78 | }, |
| 79 | findNodeById: function (link, id) { |
| 80 | if (link.get('type') !== 'UiEdgeLink') { |
| 81 | // Remove /{port} from id if needed |
| 82 | var regex = new RegExp('^[^/]*'); |
| 83 | id = regex.exec(id)[0]; |
| 84 | } |
| 85 | return this.model.get('devices').get(id) || |
| 86 | this.model.get('hosts').get(id) || |
| 87 | this.model.get('subregions').get(id); |
| 88 | }, |
| 89 | regionNodes: function () { |
| 90 | |
| 91 | if (this.model) { |
| 92 | return [].concat( |
| 93 | this.model.get('devices').models, |
| 94 | this.model.get('hosts').models, |
| 95 | this.model.get('subregions').models |
| 96 | ); |
| 97 | } |
| 98 | |
| 99 | return []; |
| 100 | }, |
| 101 | regionLinks: function () { |
| 102 | return (this.model) ? this.model.get('links').models : []; |
| 103 | }, |
| 104 | filterRegionNodes: function (predicate) { |
| 105 | var nodes = this.regionNodes(); |
| 106 | return _.filter(nodes, predicate); |
| 107 | }, |
| 108 | deselectAllNodes: function () { |
| 109 | var selected = this.filterRegionNodes(function (node) { |
| 110 | return node.get('selected', true); |
| 111 | }); |
| 112 | |
| 113 | if (selected.length) { |
| 114 | |
| 115 | selected.forEach(function (node) { |
| 116 | node.deselect(); |
| 117 | }); |
| 118 | |
| 119 | t2dps().el.hide(); |
| 120 | return true; |
| 121 | } |
| 122 | |
| 123 | return false; |
| 124 | }, |
| 125 | deselectLink: function () { |
| 126 | var selected = _.filter(this.regionLinks(), function (link) { |
| 127 | return link.get('selected', true); |
| 128 | }); |
| 129 | |
| 130 | if (selected.length) { |
| 131 | |
| 132 | selected.forEach(function (link) { |
| 133 | link.deselect(); |
| 134 | }); |
| 135 | |
| 136 | t2dps().el.hide(); |
| 137 | return true; |
| 138 | } |
| 139 | |
| 140 | return false; |
| 141 | } |
| 142 | }); |
| 143 | |
| 144 | function getInstance() { |
| 145 | return instance || new Region(); |
| 146 | } |
| 147 | |
| 148 | return getInstance(); |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 149 | }]); |
| 150 | |
| 151 | })(); |