Steven Burrows | ad74af8 | 2017-04-28 15:27:19 -0400 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Steven Burrows | ad74af8 | 2017-04-28 15:27:19 -0400 | [diff] [blame] | 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: { |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 28 | fill: '#000000', |
Steven Burrows | ad74af8 | 2017-04-28 15:27:19 -0400 | [diff] [blame] | 29 | }, |
| 30 | rect: { |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 31 | fill: '#ffffff', |
| 32 | }, |
Steven Burrows | ad74af8 | 2017-04-28 15:27:19 -0400 | [diff] [blame] | 33 | }, |
| 34 | icon: { |
| 35 | glyph: { |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 36 | fill: '#000000', |
Steven Burrows | ad74af8 | 2017-04-28 15:27:19 -0400 | [diff] [blame] | 37 | }, |
| 38 | rect: { |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 39 | fill: '#ffffff', |
| 40 | }, |
| 41 | }, |
Steven Burrows | ad74af8 | 2017-04-28 15:27:19 -0400 | [diff] [blame] | 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 Burrows | ca1a39c | 2017-05-08 17:31:08 -0400 | [diff] [blame] | 53 | initialize: function (data, node) { |
Steven Burrows | ad74af8 | 2017-04-28 15:27:19 -0400 | [diff] [blame] | 54 | this.parent = node; |
Steven Burrows | ad74af8 | 2017-04-28 15:27:19 -0400 | [diff] [blame] | 55 | t2zs.addZoomEventListener(this.setScale.bind(this)); |
Steven Burrows | ca1a39c | 2017-05-08 17:31:08 -0400 | [diff] [blame] | 56 | this.beforeRender(); |
Steven Burrows | ad74af8 | 2017-04-28 15:27:19 -0400 | [diff] [blame] | 57 | this.render(); |
Steven Burrows | ca1a39c | 2017-05-08 17:31:08 -0400 | [diff] [blame] | 58 | this.afterRender(); |
Steven Burrows | ad74af8 | 2017-04-28 15:27:19 -0400 | [diff] [blame] | 59 | }, |
Steven Burrows | ca1a39c | 2017-05-08 17:31:08 -0400 | [diff] [blame] | 60 | onChange: function (property) { |
Steven Burrows | ad74af8 | 2017-04-28 15:27:19 -0400 | [diff] [blame] | 61 | if (property === 'x' || property === 'y') { |
| 62 | this._position(); |
| 63 | } |
Steven Burrows | ca1a39c | 2017-05-08 17:31:08 -0400 | [diff] [blame] | 64 | |
| 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, |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 72 | height: height, |
Steven Burrows | ca1a39c | 2017-05-08 17:31:08 -0400 | [diff] [blame] | 73 | }).style({ |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 74 | transform: sus.translate(-(width/2) + 'px', -(height/2) + 'px'), |
Steven Burrows | ca1a39c | 2017-05-08 17:31:08 -0400 | [diff] [blame] | 75 | }); |
| 76 | } |
Steven Burrows | ad74af8 | 2017-04-28 15:27:19 -0400 | [diff] [blame] | 77 | }, |
| 78 | |
| 79 | setPosition: function () {}, |
| 80 | setScale: function () {}, |
| 81 | |
| 82 | applyStyles: function () { |
Steven Burrows | ca1a39c | 2017-05-08 17:31:08 -0400 | [diff] [blame] | 83 | var styles = _.extend({}, defaultStyles, this.get('styles') || {}); |
Steven Burrows | ad74af8 | 2017-04-28 15:27:19 -0400 | [diff] [blame] | 84 | |
Steven Burrows | ca1a39c | 2017-05-08 17:31:08 -0400 | [diff] [blame] | 85 | if (this.get('label')) { |
Steven Burrows | ad74af8 | 2017-04-28 15:27:19 -0400 | [diff] [blame] | 86 | 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 Burrows | ad74af8 | 2017-04-28 15:27:19 -0400 | [diff] [blame] | 95 | _position: function () { |
| 96 | this.el.style('transform', sus.translate(this.get('x') + 'px', |
| 97 | this.get('y') + 'px')); |
| 98 | }, |
Steven Burrows | ad74af8 | 2017-04-28 15:27:19 -0400 | [diff] [blame] | 99 | 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 Burrows | ca1a39c | 2017-05-08 17:31:08 -0400 | [diff] [blame] | 105 | .text(this.get('label')) |
Steven Burrows | ad74af8 | 2017-04-28 15:27:19 -0400 | [diff] [blame] | 106 | .attr('y', '0.4em') |
| 107 | .style('text-anchor', 'middle'); |
| 108 | |
| 109 | this._labelG.rect.attr({ |
| 110 | width: this._labelG.text.node().getBBox().width + 20, |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 111 | height: this._labelG.text.node().getBBox().height + 10, |
Steven Burrows | ad74af8 | 2017-04-28 15:27:19 -0400 | [diff] [blame] | 112 | }).style({ |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 113 | transform: sus.translate('-50%', '-50%'), |
Steven Burrows | ad74af8 | 2017-04-28 15:27:19 -0400 | [diff] [blame] | 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, |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 126 | height: this.iconSize, |
Steven Burrows | ad74af8 | 2017-04-28 15:27:19 -0400 | [diff] [blame] | 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', |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 134 | iconY = -this.iconSize /2 + 'px'; |
Steven Burrows | ad74af8 | 2017-04-28 15:27:19 -0400 | [diff] [blame] | 135 | this._iconG.el.style({ |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 136 | transform: sus.translate(iconX, iconY), |
Steven Burrows | ad74af8 | 2017-04-28 15:27:19 -0400 | [diff] [blame] | 137 | }); |
| 138 | }, |
Steven Burrows | ca1a39c | 2017-05-08 17:31:08 -0400 | [diff] [blame] | 139 | beforeRender: function () {}, |
Steven Burrows | ad74af8 | 2017-04-28 15:27:19 -0400 | [diff] [blame] | 140 | render: function () { |
| 141 | this.el = this.parent.append('g') |
| 142 | .attr('class', 'topo2-label') |
| 143 | .style({ |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 144 | transform: 'translate(300px, 300px)', |
Steven Burrows | ad74af8 | 2017-04-28 15:27:19 -0400 | [diff] [blame] | 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 Burrows | ca1a39c | 2017-05-08 17:31:08 -0400 | [diff] [blame] | 159 | }, |
| 160 | afterRender: function () {}, |
| 161 | remove: function () { |
| 162 | this.el.remove(); |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 163 | }, |
Steven Burrows | ad74af8 | 2017-04-28 15:27:19 -0400 | [diff] [blame] | 164 | }); |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 165 | }, |
Steven Burrows | ad74af8 | 2017-04-28 15:27:19 -0400 | [diff] [blame] | 166 | ]); |
| 167 | })(); |