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 SubRegion Module. |
| 19 | Module that holds the sub-regions for a region |
| 20 | */ |
| 21 | |
| 22 | (function () { |
| 23 | 'use strict'; |
| 24 | |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame^] | 25 | var wss, is, sus; |
Steven Burrows | 9edc7e0 | 2016-08-29 11:52:07 +0100 | [diff] [blame] | 26 | var Collection, Model; |
Steven Burrows | 6deb4ce | 2016-08-26 16:06:23 +0100 | [diff] [blame] | 27 | |
| 28 | var remappedDeviceTypes = { |
| 29 | virtual: 'cord' |
| 30 | }; |
| 31 | |
| 32 | // configuration |
| 33 | var devIconDim = 36, |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame^] | 34 | halfDevIcon = devIconDim / 2; |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 35 | |
| 36 | function createSubRegionCollection(data, region) { |
| 37 | |
| 38 | var SubRegionCollection = Collection.extend({ |
| 39 | model: Model |
| 40 | }); |
| 41 | |
| 42 | return new SubRegionCollection(data); |
| 43 | } |
| 44 | |
Steven Burrows | 6deb4ce | 2016-08-26 16:06:23 +0100 | [diff] [blame] | 45 | function mapDeviceTypeToGlyph(type) { |
| 46 | return remappedDeviceTypes[type] || type || 'switch'; |
| 47 | } |
| 48 | |
| 49 | function iconBox(dim, labelWidth) { |
| 50 | return { |
| 51 | x: -dim / 2, |
| 52 | y: -dim / 2, |
| 53 | width: dim + labelWidth, |
| 54 | height: dim |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame^] | 55 | }; |
Steven Burrows | 6deb4ce | 2016-08-26 16:06:23 +0100 | [diff] [blame] | 56 | } |
| 57 | |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 58 | angular.module('ovTopo2') |
| 59 | .factory('Topo2SubRegionService', |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame^] | 60 | ['WebSocketService', 'Topo2Collection', 'Topo2NodeModel', |
| 61 | 'IconService', 'SvgUtilService', 'ThemeService', 'Topo2ViewService', |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 62 | |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame^] | 63 | function (_wss_, _c_, _NodeModel_, _is_, _sus_, _ts_, _t2vs_) { |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 64 | |
Steven Burrows | 9edc7e0 | 2016-08-29 11:52:07 +0100 | [diff] [blame] | 65 | wss = _wss_; |
Steven Burrows | 6deb4ce | 2016-08-26 16:06:23 +0100 | [diff] [blame] | 66 | is = _is_; |
| 67 | sus = _sus_; |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame^] | 68 | Collection = _c_; |
Steven Burrows | 6deb4ce | 2016-08-26 16:06:23 +0100 | [diff] [blame] | 69 | |
| 70 | Model = _NodeModel_.extend({ |
| 71 | initialize: function () { |
| 72 | this.set('weight', 0); |
| 73 | this.constructor.__super__.initialize.apply(this, arguments); |
| 74 | }, |
| 75 | nodeType: 'sub-region', |
| 76 | mapDeviceTypeToGlyph: mapDeviceTypeToGlyph, |
Steven Burrows | 9edc7e0 | 2016-08-29 11:52:07 +0100 | [diff] [blame] | 77 | onClick: function () { |
| 78 | wss.sendEvent('topo2navRegion', { |
| 79 | dir: 'down', |
| 80 | rid: this.get('id') |
| 81 | }); |
| 82 | }, |
Steven Burrows | 6deb4ce | 2016-08-26 16:06:23 +0100 | [diff] [blame] | 83 | onEnter: function (el) { |
| 84 | |
| 85 | var node = d3.select(el), |
| 86 | glyphId = mapDeviceTypeToGlyph(this.get('type')), |
| 87 | label = this.trimLabel(this.label()), |
| 88 | glyph, labelWidth; |
| 89 | |
| 90 | this.el = node; |
Steven Burrows | 9edc7e0 | 2016-08-29 11:52:07 +0100 | [diff] [blame] | 91 | this.el.on('click', this.onClick.bind(this)); |
Steven Burrows | 6deb4ce | 2016-08-26 16:06:23 +0100 | [diff] [blame] | 92 | |
| 93 | // Label |
| 94 | var labelElements = this.addLabelElements(label); |
| 95 | labelWidth = label ? this.computeLabelWidth(node) : 0; |
| 96 | labelElements.rect.attr(iconBox(devIconDim, labelWidth)); |
| 97 | |
| 98 | // Icon |
| 99 | glyph = is.addDeviceIcon(node, glyphId, devIconDim); |
| 100 | glyph.attr(iconBox(devIconDim, 0)); |
| 101 | |
| 102 | node.attr('transform', sus.translate(-halfDevIcon, -halfDevIcon)); |
| 103 | this.render(); |
| 104 | }, |
| 105 | onExit: function () {}, |
| 106 | render: function () {} |
| 107 | }); |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 108 | |
| 109 | return { |
| 110 | createSubRegionCollection: createSubRegionCollection |
| 111 | }; |
| 112 | } |
| 113 | ]); |
| 114 | |
| 115 | })(); |