Matteo Scandolo | fb46ae6 | 2017-08-08 09:10:50 -0700 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2017-present Open Networking Foundation |
| 4 | |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | |
Matteo Scandolo | 0c61c9b | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 19 | import './coarse.component.scss'; |
| 20 | import * as d3 from 'd3'; |
| 21 | import * as $ from 'jquery'; |
Matteo Scandolo | bafd8d6 | 2017-03-29 23:23:00 -0700 | [diff] [blame] | 22 | import * as _ from 'lodash'; |
Matteo Scandolo | 7218159 | 2017-07-25 14:49:40 -0700 | [diff] [blame] | 23 | import {IXosServiceGraphStore} from '../../services/service-graph.store'; |
Matteo Scandolo | 0c61c9b | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 24 | import {IXosServiceGraph, IXosServiceGraphNode, IXosServiceGraphLink} from '../../interfaces'; |
| 25 | import {XosServiceGraphConfig as config} from '../../graph.config'; |
Matteo Scandolo | a160eef | 2017-03-06 17:21:26 -0800 | [diff] [blame] | 26 | import {IXosDebouncer} from '../../../core/services/helpers/debounce.helper'; |
| 27 | import {Subscription} from 'rxjs'; |
Matteo Scandolo | 7629cc4 | 2017-03-13 14:12:15 -0700 | [diff] [blame] | 28 | import {IXosGraphHelpers} from '../../services/d3-helpers/graph.helpers'; |
Matteo Scandolo | bafd8d6 | 2017-03-29 23:23:00 -0700 | [diff] [blame] | 29 | import {IXosServiceGraphReducer, IXosServiceGraphExtender} from '../../services/graph.extender'; |
Matteo Scandolo | 0c61c9b | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 30 | |
Matteo Scandolo | 968e7f2 | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 31 | class XosCoarseTenancyGraphCtrl { |
| 32 | |
Matteo Scandolo | a160eef | 2017-03-06 17:21:26 -0800 | [diff] [blame] | 33 | static $inject = [ |
| 34 | '$log', |
| 35 | 'XosServiceGraphStore', |
Matteo Scandolo | 7629cc4 | 2017-03-13 14:12:15 -0700 | [diff] [blame] | 36 | 'XosDebouncer', |
Matteo Scandolo | bafd8d6 | 2017-03-29 23:23:00 -0700 | [diff] [blame] | 37 | 'XosGraphHelpers', |
| 38 | 'XosServiceGraphExtender' |
Matteo Scandolo | a160eef | 2017-03-06 17:21:26 -0800 | [diff] [blame] | 39 | ]; |
Matteo Scandolo | 968e7f2 | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 40 | |
| 41 | public graph: IXosServiceGraph; |
| 42 | |
Matteo Scandolo | a160eef | 2017-03-06 17:21:26 -0800 | [diff] [blame] | 43 | private CoarseGraphSubscription: Subscription; |
Matteo Scandolo | 0c61c9b | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 44 | private svg; |
| 45 | private forceLayout; |
| 46 | private linkGroup; |
| 47 | private nodeGroup; |
Matteo Scandolo | 6a7435f | 2017-03-24 18:07:17 -0700 | [diff] [blame] | 48 | private textSize = 20; |
| 49 | private textOffset = this.textSize / 4; |
Matteo Scandolo | 0c61c9b | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 50 | |
Matteo Scandolo | 7517178 | 2017-03-08 14:17:01 -0800 | [diff] [blame] | 51 | // debounced functions |
Matteo Scandolo | a160eef | 2017-03-06 17:21:26 -0800 | [diff] [blame] | 52 | private renderGraph; |
| 53 | |
Matteo Scandolo | 968e7f2 | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 54 | constructor ( |
| 55 | private $log: ng.ILogService, |
Matteo Scandolo | a160eef | 2017-03-06 17:21:26 -0800 | [diff] [blame] | 56 | private XosServiceGraphStore: IXosServiceGraphStore, |
Matteo Scandolo | 7629cc4 | 2017-03-13 14:12:15 -0700 | [diff] [blame] | 57 | private XosDebouncer: IXosDebouncer, |
Matteo Scandolo | bafd8d6 | 2017-03-29 23:23:00 -0700 | [diff] [blame] | 58 | private XosGraphHelpers: IXosGraphHelpers, |
| 59 | private XosServiceGraphExtender: IXosServiceGraphExtender |
Matteo Scandolo | 968e7f2 | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 60 | ) { |
| 61 | |
Matteo Scandolo | a160eef | 2017-03-06 17:21:26 -0800 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | $onInit() { |
| 65 | this.renderGraph = this.XosDebouncer.debounce(this._renderGraph, 500, this); |
| 66 | |
| 67 | this.CoarseGraphSubscription = this.XosServiceGraphStore.getCoarse() |
| 68 | .subscribe( |
Matteo Scandolo | bafd8d6 | 2017-03-29 23:23:00 -0700 | [diff] [blame] | 69 | (graph: IXosServiceGraph) => { |
| 70 | this.$log.debug(`[XosCoarseTenancyGraph] Coarse Event and render`, graph); |
Matteo Scandolo | 7517178 | 2017-03-08 14:17:01 -0800 | [diff] [blame] | 71 | |
Matteo Scandolo | a160eef | 2017-03-06 17:21:26 -0800 | [diff] [blame] | 72 | // id there are no data, do nothing |
Matteo Scandolo | bafd8d6 | 2017-03-29 23:23:00 -0700 | [diff] [blame] | 73 | if (graph.nodes.length === 0) { |
Matteo Scandolo | a160eef | 2017-03-06 17:21:26 -0800 | [diff] [blame] | 74 | return; |
| 75 | } |
Matteo Scandolo | bafd8d6 | 2017-03-29 23:23:00 -0700 | [diff] [blame] | 76 | this.graph = graph; |
| 77 | |
| 78 | _.forEach(this.XosServiceGraphExtender.getCoarse(), (r: IXosServiceGraphReducer) => { |
| 79 | graph = r.reducer(graph); |
| 80 | }); |
Matteo Scandolo | a160eef | 2017-03-06 17:21:26 -0800 | [diff] [blame] | 81 | this.renderGraph(); |
| 82 | }, |
| 83 | err => { |
| 84 | this.$log.error(`[XosCoarseTenancyGraph] Coarse Event error`, err); |
| 85 | }); |
Matteo Scandolo | 0c61c9b | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 86 | |
| 87 | this.handleSvg(); |
Matteo Scandolo | a160eef | 2017-03-06 17:21:26 -0800 | [diff] [blame] | 88 | this.setupForceLayout(); |
| 89 | |
| 90 | $(window).on('resize', () => { |
| 91 | this.setupForceLayout(); |
| 92 | this.renderGraph(); |
| 93 | }); |
| 94 | } |
| 95 | |
| 96 | $onDestroy() { |
| 97 | this.CoarseGraphSubscription.unsubscribe(); |
Matteo Scandolo | a160eef | 2017-03-06 17:21:26 -0800 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | private _renderGraph() { |
Matteo Scandolo | 9b46004 | 2017-04-14 16:24:45 -0700 | [diff] [blame] | 101 | if (!angular.isDefined(this.graph) || !angular.isDefined(this.graph.nodes) || !angular.isDefined(this.graph.links)) { |
| 102 | return; |
| 103 | } |
Matteo Scandolo | a160eef | 2017-03-06 17:21:26 -0800 | [diff] [blame] | 104 | this.addNodeLinksToForceLayout(this.graph); |
| 105 | this.renderNodes(this.graph.nodes); |
| 106 | this.renderLinks(this.graph.links); |
Matteo Scandolo | 0c61c9b | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 107 | } |
| 108 | |
Max Chu | 2bfddde | 2017-06-29 13:41:52 -0700 | [diff] [blame] | 109 | private getSvgDimensions(): {width: number, height: number} { |
Matteo Scandolo | 0c61c9b | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 110 | return { |
Matteo Scandolo | 9b46004 | 2017-04-14 16:24:45 -0700 | [diff] [blame] | 111 | width: $('xos-coarse-tenancy-graph svg').width() || 0, |
Max Chu | 2bfddde | 2017-06-29 13:41:52 -0700 | [diff] [blame] | 112 | height: $('xos-coarse-tenancy-graph svg').height() || 0 |
Matteo Scandolo | 0c61c9b | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 113 | }; |
| 114 | } |
| 115 | |
| 116 | private handleSvg() { |
| 117 | this.svg = d3.select('svg'); |
| 118 | |
| 119 | this.svg.append('svg:defs') |
| 120 | .selectAll('marker') |
| 121 | .data(config.markers) |
| 122 | .enter() |
| 123 | .append('svg:marker') |
| 124 | .attr('id', d => d.id) |
| 125 | .attr('viewBox', d => d.viewBox) |
| 126 | .attr('refX', d => d.refX) |
| 127 | .attr('refY', d => d.refY) |
| 128 | .attr('markerWidth', d => d.width) |
| 129 | .attr('markerHeight', d => d.height) |
| 130 | .attr('orient', 'auto') |
| 131 | .attr('class', d => `${d.id}-marker`) |
| 132 | .append('svg:path') |
| 133 | .attr('d', d => d.path); |
| 134 | |
| 135 | this.linkGroup = this.svg.append('g') |
| 136 | .attr({ |
| 137 | class: 'link-group' |
| 138 | }); |
| 139 | |
| 140 | this.nodeGroup = this.svg.append('g') |
| 141 | .attr({ |
| 142 | class: 'node-group' |
| 143 | }); |
| 144 | } |
| 145 | |
Max Chu | 2bfddde | 2017-06-29 13:41:52 -0700 | [diff] [blame] | 146 | private collide(n: any) { |
| 147 | const svgDim = this.getSvgDimensions(); |
| 148 | const x = Math.max(n.width / 2, Math.min(n.x, svgDim.width - (n.width / 2))); |
| 149 | const y = Math.max(n.height / 2, Math.min(n.y, svgDim.height - (n.height / 2))); |
| 150 | return `${x}, ${y}`; |
| 151 | } |
| 152 | |
Matteo Scandolo | a160eef | 2017-03-06 17:21:26 -0800 | [diff] [blame] | 153 | private setupForceLayout() { |
Matteo Scandolo | 0c61c9b | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 154 | |
Max Chu | 2bfddde | 2017-06-29 13:41:52 -0700 | [diff] [blame] | 155 | let svgDim = this.getSvgDimensions(); |
| 156 | |
Matteo Scandolo | 0c61c9b | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 157 | const tick = () => { |
Max Chu | 2bfddde | 2017-06-29 13:41:52 -0700 | [diff] [blame] | 158 | |
Matteo Scandolo | 0c61c9b | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 159 | this.nodeGroup.selectAll('g.node') |
| 160 | .attr({ |
Max Chu | 2bfddde | 2017-06-29 13:41:52 -0700 | [diff] [blame] | 161 | transform: d => `translate(${this.collide(d)})` |
Matteo Scandolo | 0c61c9b | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 162 | }); |
| 163 | |
| 164 | this.linkGroup.selectAll('line') |
| 165 | .attr({ |
Matteo Scandolo | a160eef | 2017-03-06 17:21:26 -0800 | [diff] [blame] | 166 | x1: l => l.source.x || 0, |
| 167 | y1: l => l.source.y || 0, |
| 168 | x2: l => l.target.x || 0, |
| 169 | y2: l => l.target.y || 0, |
Matteo Scandolo | 0c61c9b | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 170 | }); |
| 171 | }; |
Max Chu | 2bfddde | 2017-06-29 13:41:52 -0700 | [diff] [blame] | 172 | |
Matteo Scandolo | 0c61c9b | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 173 | this.forceLayout = d3.layout.force() |
Max Chu | 2bfddde | 2017-06-29 13:41:52 -0700 | [diff] [blame] | 174 | .size([svgDim.width, svgDim.height]) |
Matteo Scandolo | 0c61c9b | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 175 | .linkDistance(config.force.linkDistance) |
| 176 | .charge(config.force.charge) |
| 177 | .gravity(config.force.gravity) |
| 178 | .on('tick', tick); |
| 179 | } |
| 180 | |
Matteo Scandolo | a160eef | 2017-03-06 17:21:26 -0800 | [diff] [blame] | 181 | private addNodeLinksToForceLayout(data: IXosServiceGraph) { |
| 182 | this.forceLayout |
| 183 | .nodes(data.nodes) |
| 184 | .links(data.links) |
| 185 | .start(); |
| 186 | } |
| 187 | |
Matteo Scandolo | 0c61c9b | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 188 | private renderNodes(nodes: IXosServiceGraphNode[]) { |
| 189 | const self = this; |
| 190 | const node = this.nodeGroup |
Matteo Scandolo | a160eef | 2017-03-06 17:21:26 -0800 | [diff] [blame] | 191 | .selectAll('g.node') |
| 192 | .data(nodes, n => n.id); |
Matteo Scandolo | 0c61c9b | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 193 | |
Matteo Scandolo | a160eef | 2017-03-06 17:21:26 -0800 | [diff] [blame] | 194 | const svgDim = this.getSvgDimensions(); |
Matteo Scandolo | 0c61c9b | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 195 | const entering = node.enter() |
| 196 | .append('g') |
| 197 | .attr({ |
Matteo Scandolo | 7218159 | 2017-07-25 14:49:40 -0700 | [diff] [blame] | 198 | id: n => n.id, |
Matteo Scandolo | 7629cc4 | 2017-03-13 14:12:15 -0700 | [diff] [blame] | 199 | class: n => `node ${this.XosGraphHelpers.parseElemClasses(n.d3Class)}`, |
Max Chu | 2bfddde | 2017-06-29 13:41:52 -0700 | [diff] [blame] | 200 | transform: `translate(${svgDim.width / 2}, ${svgDim.height / 2})` |
Matteo Scandolo | 0c61c9b | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 201 | }) |
| 202 | .call(this.forceLayout.drag) |
Matteo Scandolo | a160eef | 2017-03-06 17:21:26 -0800 | [diff] [blame] | 203 | .on('mousedown', () => { |
| 204 | d3.event.stopPropagation(); |
| 205 | }) |
| 206 | .on('mouseup', (d) => { |
| 207 | d.fixed = true; |
| 208 | }); |
Matteo Scandolo | 0c61c9b | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 209 | |
| 210 | entering.append('rect') |
| 211 | .attr({ |
| 212 | rx: config.node.radius, |
| 213 | ry: config.node.radius |
| 214 | }); |
| 215 | |
| 216 | entering.append('text') |
| 217 | .attr({ |
Matteo Scandolo | 6a7435f | 2017-03-24 18:07:17 -0700 | [diff] [blame] | 218 | 'text-anchor': 'middle', |
| 219 | 'transform': `translate(0,${this.textOffset})` |
Matteo Scandolo | 0c61c9b | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 220 | }) |
| 221 | .text(n => n.label); |
Matteo Scandolo | a160eef | 2017-03-06 17:21:26 -0800 | [diff] [blame] | 222 | // .text(n => `${n.id} - ${n.label}`); |
Matteo Scandolo | 0c61c9b | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 223 | |
| 224 | const existing = node.selectAll('rect'); |
| 225 | |
Matteo Scandolo | 0c61c9b | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 226 | // resize node > rect as contained text |
Max Chu | 2bfddde | 2017-06-29 13:41:52 -0700 | [diff] [blame] | 227 | |
| 228 | existing.each(function(d: any) { |
Matteo Scandolo | 7629cc4 | 2017-03-13 14:12:15 -0700 | [diff] [blame] | 229 | const textBBox = self.XosGraphHelpers.getSiblingTextBBox(this); |
Matteo Scandolo | 0c61c9b | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 230 | const rect = d3.select(this); |
| 231 | rect.attr({ |
| 232 | width: textBBox.width + config.node.padding, |
| 233 | height: textBBox.height + config.node.padding, |
| 234 | x: textBBox.x - (config.node.padding / 2), |
Matteo Scandolo | 6a7435f | 2017-03-24 18:07:17 -0700 | [diff] [blame] | 235 | y: (textBBox.y + self.textOffset) - (config.node.padding / 2) |
Matteo Scandolo | 0c61c9b | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 236 | }); |
Max Chu | 2bfddde | 2017-06-29 13:41:52 -0700 | [diff] [blame] | 237 | d.width = textBBox.width + config.node.padding; |
| 238 | d.height = textBBox.height + config.node.padding; |
Matteo Scandolo | 0c61c9b | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 239 | }); |
Max Chu | 2bfddde | 2017-06-29 13:41:52 -0700 | [diff] [blame] | 240 | |
Matteo Scandolo | 0c61c9b | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | private renderLinks(links: IXosServiceGraphLink[]) { |
| 244 | const link = this.linkGroup |
Matteo Scandolo | a160eef | 2017-03-06 17:21:26 -0800 | [diff] [blame] | 245 | .selectAll('line') |
| 246 | .data(links, l => l.id); |
Matteo Scandolo | 0c61c9b | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 247 | |
Matteo Scandolo | a160eef | 2017-03-06 17:21:26 -0800 | [diff] [blame] | 248 | const entering = link.enter(); |
Matteo Scandolo | 7629cc4 | 2017-03-13 14:12:15 -0700 | [diff] [blame] | 249 | |
| 250 | // TODO read classes from graph links |
Matteo Scandolo | 0c61c9b | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 251 | |
| 252 | entering.append('line') |
Matteo Scandolo | a160eef | 2017-03-06 17:21:26 -0800 | [diff] [blame] | 253 | .attr({ |
Matteo Scandolo | 7218159 | 2017-07-25 14:49:40 -0700 | [diff] [blame] | 254 | id: n => n.id, |
Matteo Scandolo | 7629cc4 | 2017-03-13 14:12:15 -0700 | [diff] [blame] | 255 | class: l => `link ${this.XosGraphHelpers.parseElemClasses(l.d3Class)}`, |
Matteo Scandolo | a160eef | 2017-03-06 17:21:26 -0800 | [diff] [blame] | 256 | 'marker-start': 'url(#arrow)' |
| 257 | }); |
Matteo Scandolo | 968e7f2 | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 258 | } |
| 259 | } |
| 260 | |
| 261 | export const XosCoarseTenancyGraph: angular.IComponentOptions = { |
| 262 | template: require('./coarse.component.html'), |
| 263 | controllerAs: 'vm', |
| 264 | controller: XosCoarseTenancyGraphCtrl, |
| 265 | }; |