Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2015 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 Event Module. |
| 19 | Defines event handling for events received from the server. |
| 20 | */ |
| 21 | |
| 22 | (function () { |
| 23 | 'use strict'; |
| 24 | |
| 25 | // injected refs |
| 26 | var $log, sus; |
| 27 | |
| 28 | // internal state |
| 29 | var settings, |
| 30 | force, // force layout object |
| 31 | drag, // drag behavior handler |
| 32 | network = { |
| 33 | nodes: [], |
| 34 | links: [], |
| 35 | lookup: {}, |
| 36 | revLinkToKey: {} |
| 37 | }; |
| 38 | |
| 39 | |
| 40 | // SVG elements; |
| 41 | var linkG, linkLabelG, nodeG; |
| 42 | |
| 43 | // D3 selections; |
| 44 | var link, linkLabel, node; |
| 45 | |
| 46 | // default settings for force layout |
| 47 | var defaultSettings = { |
| 48 | gravity: 0.4, |
| 49 | friction: 0.7, |
| 50 | charge: { |
| 51 | // note: key is node.class |
| 52 | device: -8000, |
| 53 | host: -5000, |
| 54 | _def_: -12000 |
| 55 | }, |
| 56 | linkDistance: { |
| 57 | // note: key is link.type |
| 58 | direct: 100, |
| 59 | optical: 120, |
| 60 | hostLink: 3, |
| 61 | _def_: 50 |
| 62 | }, |
| 63 | linkStrength: { |
| 64 | // note: key is link.type |
| 65 | // range: {0.0 ... 1.0} |
| 66 | //direct: 1.0, |
| 67 | //optical: 1.0, |
| 68 | //hostLink: 1.0, |
| 69 | _def_: 1.0 |
| 70 | } |
| 71 | }; |
| 72 | |
| 73 | |
| 74 | // force layout tick function |
| 75 | function tick() { |
| 76 | |
| 77 | } |
| 78 | |
| 79 | |
| 80 | function selectCb() { } |
| 81 | function atDragEnd() {} |
| 82 | function dragEnabled() {} |
| 83 | function clickEnabled() {} |
| 84 | |
| 85 | |
| 86 | // ========================== |
| 87 | |
| 88 | angular.module('ovTopo') |
| 89 | .factory('TopoForceService', |
| 90 | ['$log', 'SvgUtilService', |
| 91 | |
| 92 | function (_$log_, _sus_) { |
| 93 | $log = _$log_; |
| 94 | sus = _sus_; |
| 95 | |
| 96 | // forceG is the SVG group to display the force layout in |
| 97 | // w, h are the initial dimensions of the SVG |
| 98 | // opts are, well, optional :) |
| 99 | function initForce (forceG, w, h, opts) { |
| 100 | // TODO: create the force layout and initialize |
| 101 | settings = angular.extend({}, defaultSettings, opts); |
| 102 | |
| 103 | linkG = forceG.append('g').attr('id', 'topo-links'); |
| 104 | linkLabelG = forceG.append('g').attr('id', 'topo-linkLabels'); |
| 105 | nodeG = forceG.append('g').attr('id', 'topo-nodes'); |
| 106 | |
| 107 | link = linkG.selectAll('.link'); |
| 108 | linkLabel = linkLabelG.selectAll('.linkLabel'); |
| 109 | node = nodeG.selectAll('.node'); |
| 110 | |
| 111 | force = d3.layout.force() |
| 112 | .size(w, h) |
| 113 | .nodes(network.nodes) |
| 114 | .links(network.links) |
| 115 | .gravity(settings.gravity) |
| 116 | .friction(settings.friction) |
| 117 | .charge(settings.charge._def_) |
| 118 | .linkDistance(settings.linkDistance._def_) |
| 119 | .linkStrength(settings.linkStrength._def_) |
| 120 | .on('tick', tick); |
| 121 | |
| 122 | drag = sus.createDragBehavior(force, |
| 123 | selectCb, atDragEnd, dragEnabled, clickEnabled); |
| 124 | } |
| 125 | |
| 126 | function resize(w, h) { |
| 127 | force.size(w, h); |
| 128 | // Review -- do we need to nudge the layout ? |
| 129 | } |
| 130 | |
| 131 | return { |
| 132 | initForce: initForce, |
| 133 | resize: resize |
| 134 | }; |
| 135 | }]); |
| 136 | }()); |