Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 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 | */ |
Sean Condon | 7191054 | 2019-02-16 18:16:42 +0000 | [diff] [blame] | 16 | import { |
| 17 | Directive, |
| 18 | ElementRef, |
| 19 | EventEmitter, |
| 20 | Input, |
| 21 | OnChanges, Output |
| 22 | } from '@angular/core'; |
Sean Condon | 1ae1580 | 2019-03-02 09:07:18 +0000 | [diff] [blame] | 23 | import {ForceDirectedGraph, Node} from '../models'; |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 24 | import * as d3 from 'd3'; |
Sean Condon | 3dd062f | 2020-04-14 09:25:00 +0100 | [diff] [blame] | 25 | import {LogService, MetaUi, ZoomUtils} from 'org_onosproject_onos/web/gui2-fw-lib/public_api'; |
Sean Condon | 7191054 | 2019-02-16 18:16:42 +0000 | [diff] [blame] | 26 | import {BackgroundSvgComponent} from '../../backgroundsvg/backgroundsvg.component'; |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 27 | |
| 28 | @Directive({ |
| 29 | selector: '[onosDraggableNode]' |
| 30 | }) |
| 31 | export class DraggableDirective implements OnChanges { |
| 32 | @Input() draggableNode: Node; |
| 33 | @Input() draggableInGraph: ForceDirectedGraph; |
Sean Condon | 7191054 | 2019-02-16 18:16:42 +0000 | [diff] [blame] | 34 | @Output() newLocation = new EventEmitter<MetaUi>(); |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 35 | |
| 36 | constructor( |
Sean Condon | 7191054 | 2019-02-16 18:16:42 +0000 | [diff] [blame] | 37 | private _element: ElementRef, |
| 38 | private log: LogService |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 39 | ) { |
Sean Condon | 7191054 | 2019-02-16 18:16:42 +0000 | [diff] [blame] | 40 | this.log.debug('DraggableDirective constructed'); |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | ngOnChanges() { |
| 44 | this.applyDraggableBehaviour( |
| 45 | this._element.nativeElement, |
| 46 | this.draggableNode, |
Sean Condon | 7191054 | 2019-02-16 18:16:42 +0000 | [diff] [blame] | 47 | this.draggableInGraph, |
| 48 | this.newLocation); |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | /** |
| 52 | * A method to bind a draggable behaviour to an svg element |
| 53 | */ |
Sean Condon | 7191054 | 2019-02-16 18:16:42 +0000 | [diff] [blame] | 54 | applyDraggableBehaviour(element, node: Node, graph: ForceDirectedGraph, newLocation: EventEmitter<MetaUi>) { |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 55 | const d3element = d3.select(element); |
| 56 | |
| 57 | function started() { |
| 58 | /** Preventing propagation of dragstart to parent elements */ |
| 59 | d3.event.sourceEvent.stopPropagation(); |
| 60 | |
| 61 | if (!d3.event.active) { |
| 62 | graph.simulation.alphaTarget(0.3).restart(); |
| 63 | } |
| 64 | |
Sean Condon | 7191054 | 2019-02-16 18:16:42 +0000 | [diff] [blame] | 65 | d3.event.on('drag', () => dragged()).on('end', () => ended()); |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 66 | |
| 67 | function dragged() { |
| 68 | node.fx = d3.event.x; |
| 69 | node.fy = d3.event.y; |
| 70 | } |
| 71 | |
| 72 | function ended() { |
| 73 | if (!d3.event.active) { |
| 74 | graph.simulation.alphaTarget(0); |
| 75 | } |
Sean Condon | 1ae1580 | 2019-03-02 09:07:18 +0000 | [diff] [blame] | 76 | newLocation.emit(ZoomUtils.convertXYtoGeo(node.fx, node.fy)); |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 77 | |
Sean Condon | 7191054 | 2019-02-16 18:16:42 +0000 | [diff] [blame] | 78 | // node.fx = null; |
| 79 | // node.fy = null; |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 80 | } |
| 81 | } |
| 82 | |
| 83 | d3element.call(d3.drag() |
| 84 | .on('start', started)); |
| 85 | } |
| 86 | } |