1 define(['next-ui'], function () {
5 * Service to handle next directive
7 function NextTopologyService() {
9 * Create base nx topology object
11 * @returns {nx.graphic.Topology}
13 this.getNxTopClass = function (topoColors) {
14 return new nx.graphic.Topology({
19 iconType: 'model.icon',
20 color: function (node, model) {
21 return topoColors.getItem('forwarding-box');
25 // connected to hosts links have different colors
26 width: function (model, link) {
27 return (model._data.gLinks && model._data.gLinks.length > 2) ? 5 : 3;
31 tooltipManagerConfig: {
32 nodeTooltipContentClass: 'ExtendedTooltip',
33 showLinkTooltip: false
35 dataProcessor: 'force',
39 enableSmartNode: false,
40 linkInstanceClass: 'ExtendedLink',
41 nodeInstanceClass: 'ExtendedNode'
46 * Method for getting right link's color based on status
51 this.getLinkColor = function (status, topoColors) {
55 // all links are operational
56 if ( status.operational === status.configured ) {
57 color = topoColors.getItem('operational');
59 // operational less than configured
60 if (status.operational < status.configured && status.operational) {
61 color = topoColors.getItem('operational-mixed');
63 // if operational and something else presented
64 if (!status.operational && status.configured) {
65 // if the link is between forwarding boxes, it's considered down
66 /*if ( model.source()._data.type === 'forwarding-box' && model.target()._data.type === 'forwarding-box' ) {
67 color = topoColors.getItem('down');
69 // otherwise just configured connection
70 color = topoColors.getItem('configured');
73 color = topoColors.getItem('down');
76 color = topoColors.getItem('default');
82 color = topoColors.getItem('default');
89 * Service for reading topo nodes data from local storage
92 this.readTopologyDataFromLS = function(){
96 data = JSON.parse(localStorage.getItem("verizonTopologyData"));
98 console.info('Local Storage read parse error:', e);
105 * Set loaded nodes data from local storage
110 this.setTopologyDataFromLS = function (data, topo, nodesDict) {
111 if ( data && data.nodes ) {
113 data.nodes.forEach(function(node, index){
114 var nodeInst = topo.getNode(nodesDict.getItem(node.nodeName));
115 if(nodeInst !== undefined)
116 nodeInst.position({'x': node.x, 'y': node.y});
122 * Prepare and save data to local storage
125 this.saveTopologyDataToLS = function (topo) {
126 var data = {'nodes': []},
127 nodesLayer = topo.getLayer('nodes');
129 // prepare data for writing
130 nodesLayer.eachNode(function(node){
134 'nodeName': node.model()._data['node-id']
138 // save to local storage
140 localStorage.setItem("verizonTopologyData", JSON.stringify(data));
142 console.info('Local Storage save error:', e);
147 * Service for fade out all topo layers
150 this.fadeOutAllLayers = function(topo){
151 nx.each(topo.layers(), function(layer) {
157 * Service for fade in all topo layers
160 this.fadeInAllLayers = function(topo){
161 //fade out all layers
162 var linksLayerHighlightElements = topo.getLayer('links').highlightedElements(),
163 nodeLayerHighlightElements = topo.getLayer('nodes').highlightedElements();
166 nodeLayerHighlightElements.clear();
167 linksLayerHighlightElements.clear();
169 nx.each(topo.layers(), function(layer) {
175 * Service for highlighting node with(without) links
180 this.highlightNode = function (topo, targetId, noLinks) {
181 var nodeLayer = topo.getLayer('nodes'),
182 linksLayer = topo.getLayer('links'),
183 linksLayerHighlightElements = linksLayer.highlightedElements(),
184 nodeLayerHighlightElements = nodeLayer.highlightedElements();
187 nodeLayerHighlightElements.clear();
188 linksLayerHighlightElements.clear();
191 nodeLayerHighlightElements.add(topo.getNode(targetId));
194 linksLayerHighlightElements.addRange(nx.util.values(topo.getNode(targetId).links()));
197 linksLayer.fadeOut(true);
202 * Service for highlighting link
206 this.highlightLink = function(topo, linkId) {
207 var nodeLayer = topo.getLayer('nodes'),
208 linksLayer = topo.getLayer('links'),
209 linksLayerHighlightElements = linksLayer.highlightedElements(),
210 nodeLayerHighlightElements = nodeLayer.highlightedElements(),
211 link = topo.getLink(linkId);
214 nodeLayerHighlightElements.clear();
215 linksLayerHighlightElements.clear();
218 linksLayerHighlightElements.add(link);
219 //highlight connected nodes
220 nodeLayerHighlightElements.addRange(nx.util.values({source: topo.getNode(link.model().sourceID()), target: topo.getNode(link.model().targetID())}));
224 * Service for highlighting selected links path
226 * @param links - array of nx links obj
228 this.highlightPath = function(topo, links){
229 // clear the path layer and get its instance
230 var pathLayer = this.clearPathLayer(topo),
232 path = new nx.graphic.Topology.Path({
239 pathLayer.addPath(path);
243 * Completely clear all paths from path layer
245 * @returns {*} path instance
247 this.clearPathLayer = function(topo){
248 var pathLayer = topo.getLayer("paths");
254 * Service for returning nx tooltip skeleton
255 * @returns {{content: *[]}}
257 this.getTooltipContent = function () {
263 class: "n-topology-tooltip-header"
269 class: "n-topology-tooltip-header-text"
271 content: '{#node.model.label}'
278 class: "n-topology-tooltip-content n-list"
284 class: "n-list-wrap",
285 style: "font-size: 0.8em"
291 class: "n-list-item-i",
299 style: "display: block; margin-top: 10px;"
304 content: "{#node.model.status}"
311 class: "n-list-item-i",
317 content: "DataPath ID",
319 style: "display: block; margin-top: 10px;"
324 content: "{#node.model.datapath-id}"
331 class: "n-list-item-i",
339 style: "display: block; margin-top: 10px;"
344 content: "{#node.model.data.type}"
357 NextTopologyService.$inject=[];
359 return NextTopologyService;