blob: 2dd79977699889847fb27b565ae3e50e70cffb09 [file] [log] [blame]
Steven Burrows57e24e92016-08-04 18:38:24 +01001/*
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 Burrowsdfa52b02016-09-02 13:50:43 +010025 var wss, is, sus;
Steven Burrows9edc7e02016-08-29 11:52:07 +010026 var Collection, Model;
Steven Burrows6deb4ce2016-08-26 16:06:23 +010027
28 var remappedDeviceTypes = {
29 virtual: 'cord'
30 };
31
32 // configuration
33 var devIconDim = 36,
Steven Burrowsdfa52b02016-09-02 13:50:43 +010034 halfDevIcon = devIconDim / 2;
Steven Burrows57e24e92016-08-04 18:38:24 +010035
36 function createSubRegionCollection(data, region) {
37
38 var SubRegionCollection = Collection.extend({
39 model: Model
40 });
41
42 return new SubRegionCollection(data);
43 }
44
Steven Burrows6deb4ce2016-08-26 16:06:23 +010045 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 Burrowsdfa52b02016-09-02 13:50:43 +010055 };
Steven Burrows6deb4ce2016-08-26 16:06:23 +010056 }
57
Steven Burrows57e24e92016-08-04 18:38:24 +010058 angular.module('ovTopo2')
59 .factory('Topo2SubRegionService',
Steven Burrowsdfa52b02016-09-02 13:50:43 +010060 ['WebSocketService', 'Topo2Collection', 'Topo2NodeModel',
61 'IconService', 'SvgUtilService', 'ThemeService', 'Topo2ViewService',
Steven Burrows57e24e92016-08-04 18:38:24 +010062
Steven Burrowsdfa52b02016-09-02 13:50:43 +010063 function (_wss_, _c_, _NodeModel_, _is_, _sus_, _ts_, _t2vs_) {
Steven Burrows57e24e92016-08-04 18:38:24 +010064
Steven Burrows9edc7e02016-08-29 11:52:07 +010065 wss = _wss_;
Steven Burrows6deb4ce2016-08-26 16:06:23 +010066 is = _is_;
67 sus = _sus_;
Steven Burrowsdfa52b02016-09-02 13:50:43 +010068 Collection = _c_;
Steven Burrows6deb4ce2016-08-26 16:06:23 +010069
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 Burrows9edc7e02016-08-29 11:52:07 +010077 onClick: function () {
Steven Burrowse3a18842016-09-22 15:33:33 +010078
79 if (d3.event.defaultPrevented) return;
80
Steven Burrows9edc7e02016-08-29 11:52:07 +010081 wss.sendEvent('topo2navRegion', {
82 dir: 'down',
83 rid: this.get('id')
84 });
85 },
Steven Burrows6deb4ce2016-08-26 16:06:23 +010086 onEnter: function (el) {
87
88 var node = d3.select(el),
89 glyphId = mapDeviceTypeToGlyph(this.get('type')),
90 label = this.trimLabel(this.label()),
91 glyph, labelWidth;
92
93 this.el = node;
Steven Burrows9edc7e02016-08-29 11:52:07 +010094 this.el.on('click', this.onClick.bind(this));
Steven Burrows6deb4ce2016-08-26 16:06:23 +010095
96 // Label
97 var labelElements = this.addLabelElements(label);
98 labelWidth = label ? this.computeLabelWidth(node) : 0;
99 labelElements.rect.attr(iconBox(devIconDim, labelWidth));
100
101 // Icon
102 glyph = is.addDeviceIcon(node, glyphId, devIconDim);
103 glyph.attr(iconBox(devIconDim, 0));
104
105 node.attr('transform', sus.translate(-halfDevIcon, -halfDevIcon));
106 this.render();
107 },
108 onExit: function () {},
109 render: function () {}
110 });
Steven Burrows57e24e92016-08-04 18:38:24 +0100111
112 return {
113 createSubRegionCollection: createSubRegionCollection
114 };
115 }
116 ]);
117
118})();