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 Devices Module. |
| 19 | Module that holds the devices for a region |
| 20 | */ |
| 21 | |
| 22 | (function () { |
| 23 | 'use strict'; |
| 24 | |
Steven Burrows | bbe3dda | 2016-09-26 14:41:59 -0700 | [diff] [blame] | 25 | var Collection, Model; |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 26 | |
| 27 | var remappedDeviceTypes = { |
Steven Burrows | bbe3dda | 2016-09-26 14:41:59 -0700 | [diff] [blame] | 28 | switch: 'm_switch', |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 29 | virtual: 'cord' |
| 30 | }; |
| 31 | |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 32 | function createDeviceCollection(data, region) { |
| 33 | |
| 34 | var DeviceCollection = Collection.extend({ |
Steven Burrows | aa57d93 | 2016-10-21 10:01:48 -0500 | [diff] [blame] | 35 | model: Model |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 36 | }); |
| 37 | |
| 38 | var devices = []; |
| 39 | data.forEach(function (deviceLayer) { |
| 40 | deviceLayer.forEach(function (device) { |
| 41 | devices.push(device); |
| 42 | }); |
| 43 | }); |
| 44 | |
Steven Burrows | aa57d93 | 2016-10-21 10:01:48 -0500 | [diff] [blame] | 45 | return new DeviceCollection(devices); |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | angular.module('ovTopo2') |
| 49 | .factory('Topo2DeviceService', |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 50 | ['Topo2Collection', 'Topo2NodeModel', 'Topo2DeviceDetailsPanel', |
| 51 | function (_c_, _nm_, detailsPanel) { |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 52 | |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 53 | Collection = _c_; |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 54 | |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 55 | Model = _nm_.extend({ |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 56 | initialize: function () { |
Steven Burrows | bbe3dda | 2016-09-26 14:41:59 -0700 | [diff] [blame] | 57 | this.super = this.constructor.__super__; |
| 58 | this.super.initialize.apply(this, arguments); |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 59 | }, |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 60 | events: { |
| 61 | 'click': 'onClick' |
| 62 | }, |
Steven Burrows | e2d77d6 | 2016-10-21 12:35:58 -0500 | [diff] [blame] | 63 | onChange: function () { |
| 64 | |
| 65 | // Update class names when the model changes |
| 66 | if (this.el) { |
| 67 | this.el.attr('class', this.svgClassName()); |
| 68 | } |
| 69 | }, |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 70 | nodeType: 'device', |
Steven Burrows | bbe3dda | 2016-09-26 14:41:59 -0700 | [diff] [blame] | 71 | icon: function () { |
| 72 | var type = this.get('type'); |
| 73 | return remappedDeviceTypes[type] || type || 'unknown'; |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 74 | }, |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 75 | onClick: function () { |
| 76 | |
Steven Burrows | aa57d93 | 2016-10-21 10:01:48 -0500 | [diff] [blame] | 77 | var ev = d3.event; |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 78 | |
Steven Burrows | aa57d93 | 2016-10-21 10:01:48 -0500 | [diff] [blame] | 79 | if (ev.shiftKey) { |
| 80 | // TODO: Multi-Select Details Panel |
Steven Burrows | e2d77d6 | 2016-10-21 12:35:58 -0500 | [diff] [blame] | 81 | this.set('selected', true); |
Steven Burrows | aa57d93 | 2016-10-21 10:01:48 -0500 | [diff] [blame] | 82 | } else { |
| 83 | |
Steven Burrows | e2d77d6 | 2016-10-21 12:35:58 -0500 | [diff] [blame] | 84 | var s = Boolean(this.get('selected')); |
| 85 | // Clear all selected Items |
Steven Burrows | aa57d93 | 2016-10-21 10:01:48 -0500 | [diff] [blame] | 86 | _.each(this.collection.models, function (m) { |
| 87 | m.set('selected', false); |
Steven Burrows | aa57d93 | 2016-10-21 10:01:48 -0500 | [diff] [blame] | 88 | }); |
| 89 | |
Steven Burrows | e2d77d6 | 2016-10-21 12:35:58 -0500 | [diff] [blame] | 90 | this.set('selected', !s); |
| 91 | } |
| 92 | |
| 93 | var selected = this.collection.filter(function (m) { |
| 94 | return m.get('selected'); |
| 95 | }); |
| 96 | |
| 97 | if (_.isArray(selected) && selected.length > 0) { |
| 98 | if (selected.length === 1) { |
| 99 | var model = selected[0], |
| 100 | id = model.get('id'), |
| 101 | nodeType = model.get('nodeType'); |
Steven Burrows | aa57d93 | 2016-10-21 10:01:48 -0500 | [diff] [blame] | 102 | detailsPanel.updateDetails(id, nodeType); |
| 103 | detailsPanel.show(); |
Steven Burrows | e2d77d6 | 2016-10-21 12:35:58 -0500 | [diff] [blame] | 104 | } else { |
| 105 | // Multi Panel |
| 106 | detailsPanel.showMulti(selected); |
Steven Burrows | aa57d93 | 2016-10-21 10:01:48 -0500 | [diff] [blame] | 107 | } |
Steven Burrows | e2d77d6 | 2016-10-21 12:35:58 -0500 | [diff] [blame] | 108 | } else { |
| 109 | detailsPanel.hide(); |
Steven Burrows | aa57d93 | 2016-10-21 10:01:48 -0500 | [diff] [blame] | 110 | } |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 111 | }, |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 112 | onExit: function () { |
| 113 | var node = this.el; |
| 114 | node.select('use') |
| 115 | .style('opacity', 0.5) |
| 116 | .transition() |
| 117 | .duration(800) |
| 118 | .style('opacity', 0); |
| 119 | |
| 120 | node.selectAll('rect') |
| 121 | .style('stroke-fill', '#555') |
| 122 | .style('fill', '#888') |
| 123 | .style('opacity', 0.5); |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 124 | } |
| 125 | }); |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 126 | |
| 127 | return { |
| 128 | createDeviceCollection: createDeviceCollection |
| 129 | }; |
| 130 | } |
| 131 | ]); |
| 132 | |
| 133 | })(); |