blob: 28f134c2d618a4d094ad76f8845d47d364503cf6 [file] [log] [blame]
Steven Burrowsad74af82017-04-28 15:27:19 -04001/*
2 * Copyright 2017-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 -- Topo2Label
19 The base class for creating a label
20 */
21
22(function () {
23
24 // TODO: THEME
25 var defaultStyles = {
26 label: {
27 text: {
28 fill: '#000000'
29 },
30 rect: {
31 fill: '#ffffff'
32 }
33 },
34 icon: {
35 glyph: {
36 fill: '#000000'
37 },
38 rect: {
39 fill: '#ffffff'
40 }
41 }
42 };
43
44 angular.module('ovTopo2')
45 .factory('Topo2Label', [
46 'Topo2Model', 'SvgUtilService', 'IconService', 'Topo2ZoomService',
47 function (Model, sus, is, t2zs) {
48 return Model.extend({
49 className: 'topo2-label',
50 _iconG: {},
51 _labelG: {},
52
Steven Burrowsca1a39c2017-05-08 17:31:08 -040053 initialize: function (data, node) {
Steven Burrowsad74af82017-04-28 15:27:19 -040054 this.parent = node;
Steven Burrowsad74af82017-04-28 15:27:19 -040055 t2zs.addZoomEventListener(this.setScale.bind(this));
Steven Burrowsca1a39c2017-05-08 17:31:08 -040056 this.beforeRender();
Steven Burrowsad74af82017-04-28 15:27:19 -040057 this.render();
Steven Burrowsca1a39c2017-05-08 17:31:08 -040058 this.afterRender();
Steven Burrowsad74af82017-04-28 15:27:19 -040059 },
Steven Burrowsca1a39c2017-05-08 17:31:08 -040060 onChange: function (property) {
Steven Burrowsad74af82017-04-28 15:27:19 -040061 if (property === 'x' || property === 'y') {
62 this._position();
63 }
Steven Burrowsca1a39c2017-05-08 17:31:08 -040064
65 if (property === 'label') {
66 var width = this._labelG.text.node().getBBox().width + 20,
67 height = this._labelG.text.node().getBBox().height + 10;
68
69 this._labelG.text.text(this.get('label'));
70 this._labelG.rect.attr({
71 width: width,
72 height: height
73 }).style({
74 transform: sus.translate(-(width/2) + 'px', -(height/2) + 'px')
75 });
76 }
Steven Burrowsad74af82017-04-28 15:27:19 -040077 },
78
79 setPosition: function () {},
80 setScale: function () {},
81
82 applyStyles: function () {
Steven Burrowsca1a39c2017-05-08 17:31:08 -040083 var styles = _.extend({}, defaultStyles, this.get('styles') || {});
Steven Burrowsad74af82017-04-28 15:27:19 -040084
Steven Burrowsca1a39c2017-05-08 17:31:08 -040085 if (this.get('label')) {
Steven Burrowsad74af82017-04-28 15:27:19 -040086 this._labelG.text.style(styles.label.text);
87 this._labelG.rect.style(styles.label.rect);
88 }
89
90 if (this.get('icon')) {
91 this._iconG.glyph.style(styles.icon.glyph);
92 this._iconG.rect.style(styles.icon.rect);
93 }
94 },
Steven Burrowsad74af82017-04-28 15:27:19 -040095 _position: function () {
96 this.el.style('transform', sus.translate(this.get('x') + 'px',
97 this.get('y') + 'px'));
98 },
Steven Burrowsad74af82017-04-28 15:27:19 -040099 renderText: function () {
100 this._labelG.el = this.content.append('g')
101 .attr('class', 'label-group');
102
103 this._labelG.rect = this._labelG.el.append('rect');
104 this._labelG.text = this._labelG.el.append('text')
Steven Burrowsca1a39c2017-05-08 17:31:08 -0400105 .text(this.get('label'))
Steven Burrowsad74af82017-04-28 15:27:19 -0400106 .attr('y', '0.4em')
107 .style('text-anchor', 'middle');
108
109 this._labelG.rect.attr({
110 width: this._labelG.text.node().getBBox().width + 20,
111 height: this._labelG.text.node().getBBox().height + 10
112 }).style({
113 transform: sus.translate('-50%', '-50%')
114 });
115 },
116 renderIcon: function () {
117 var bbox = this._labelG.el.node().getBBox();
118 this.iconSize = bbox.height;
119
120 this._iconG.el = this.content.append('g')
121 .attr('class', 'icon-group');
122
123 this._iconG.rect = this._iconG.el.append('rect')
124 .attr({
125 width: this.iconSize,
126 height: this.iconSize
127 });
128
129 this._iconG.glyph = is.addDeviceIcon(this._iconG.el,
130 this.get('icon'), this.iconSize);
131
132
133 var iconX = (-bbox.width / 2) - this.iconSize + 'px',
134 iconY = -this.iconSize /2 + 'px';
135 this._iconG.el.style({
136 transform: sus.translate(iconX, iconY)
137 });
138 },
Steven Burrowsca1a39c2017-05-08 17:31:08 -0400139 beforeRender: function () {},
Steven Burrowsad74af82017-04-28 15:27:19 -0400140 render: function () {
141 this.el = this.parent.append('g')
142 .attr('class', 'topo2-label')
143 .style({
144 transform: 'translate(300px, 300px)'
145 });
146
147 this.content = this.el.append('g')
148 .attr('class', 'topo2-label__content');
149
150 this.renderText();
151
152 if (this.get('icon')) {
153 this.renderIcon();
154 }
155
156 this.applyStyles();
157 this.setPosition();
158 this.setScale();
Steven Burrowsca1a39c2017-05-08 17:31:08 -0400159 },
160 afterRender: function () {},
161 remove: function () {
162 this.el.remove();
Steven Burrowsad74af82017-04-28 15:27:19 -0400163 }
164 });
165 }
166 ]);
167})();