blob: 31ec0f6741863b8a82c4564b53613dae64a00e58 [file] [log] [blame]
Steven Burrows57e24e92016-08-04 18:38:24 +01001/*
Steven Burrowsec1f45c2016-08-08 16:14:41 +01002* Copyright 2016-present 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*/
Steven Burrows57e24e92016-08-04 18:38:24 +010016
17/*
Steven Burrowsec1f45c2016-08-08 16:14:41 +010018ONOS GUI -- Topology Force Module.
19Visualization of the topology in an SVG layer, using a D3 Force Layout.
20*/
Steven Burrows57e24e92016-08-04 18:38:24 +010021
22(function () {
23 'use strict';
24
Steven Burrows68d6f952017-03-10 13:53:35 +000025 function Model(attributes, collection) {
Steven Burrows57e24e92016-08-04 18:38:24 +010026
27 var attrs = attributes || {};
28 this.attributes = {};
29
30 attrs = angular.extend({}, attrs);
Steven Burrowsec1f45c2016-08-08 16:14:41 +010031 this.set(attrs, { silent: true });
Steven Burrows68d6f952017-03-10 13:53:35 +000032 this.collection = collection;
Steven Burrowsec1f45c2016-08-08 16:14:41 +010033 this.initialize.apply(this, arguments);
Steven Burrows57e24e92016-08-04 18:38:24 +010034 }
35
36 Model.prototype = {
37
Steven Burrowsec1f45c2016-08-08 16:14:41 +010038 initialize: function () {},
39
40 onChange: function (property, value, options) {},
41
Steven Burrows57e24e92016-08-04 18:38:24 +010042 get: function (attr) {
43 return this.attributes[attr];
44 },
45
Steven Burrowsdfa52b02016-09-02 13:50:43 +010046 set: function (key, val, options) {
Steven Burrowsec1f45c2016-08-08 16:14:41 +010047
48 if (!key) {
49 return this;
50 }
51
52 var attributes;
53 if (typeof key === 'object') {
54 attributes = key;
55 options = val;
56 } else {
57 (attributes = {})[key] = val;
58 }
59
Steven Burrowsdfa52b02016-09-02 13:50:43 +010060 var opts = options || (options = {});
Steven Burrowsec1f45c2016-08-08 16:14:41 +010061
Steven Burrowsdfa52b02016-09-02 13:50:43 +010062 var unset = opts.unset,
63 silent = opts.silent,
Steven Burrowsec1f45c2016-08-08 16:14:41 +010064 changes = [],
Steven Burrowsdfa52b02016-09-02 13:50:43 +010065 changing = this._changing;
Steven Burrowsec1f45c2016-08-08 16:14:41 +010066
67 this._changing = true;
68
69 if (!changing) {
70
71 // NOTE: angular.copy causes issues in chrome
Steven Burrowsdfa52b02016-09-02 13:50:43 +010072 this._previousAttributes = Object.create(
73 Object.getPrototypeOf(this.attributes)
74 );
Steven Burrowsec1f45c2016-08-08 16:14:41 +010075 this.changed = {};
76 }
77
78 var current = this.attributes,
79 changed = this.changed,
80 previous = this._previousAttributes;
81
82 angular.forEach(attributes, function (attribute, index) {
83
84 val = attribute;
85
Steven Burrows68d6f952017-03-10 13:53:35 +000086 if (!_.isEqual(current[index], val)) {
Steven Burrowsec1f45c2016-08-08 16:14:41 +010087 changes.push(index);
88 }
89
Steven Burrows68d6f952017-03-10 13:53:35 +000090 if (!_.isEqual(previous[index], val)) {
Steven Burrowsec1f45c2016-08-08 16:14:41 +010091 delete changed[index];
Steven Burrowsdfa52b02016-09-02 13:50:43 +010092 } else {
93 changed[index] = val;
Steven Burrowsec1f45c2016-08-08 16:14:41 +010094 }
95
Steven Burrowsdfa52b02016-09-02 13:50:43 +010096 if (unset) {
97 delete current[index];
98 } else {
99 current[index] = val;
100 }
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100101 });
102
103 // Trigger all relevant attribute changes.
104 if (!silent) {
105 if (changes.length) {
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100106 this._pending = opts;
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100107 }
108 for (var i = 0; i < changes.length; i++) {
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100109 this.onChange(changes[i], this,
110 current[changes[i]], opts);
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100111 }
112 }
113
114 this._changing = false;
115 return this;
116 },
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100117 toJSON: function (options) {
118 return angular.copy(this.attributes);
119 }
Steven Burrows57e24e92016-08-04 18:38:24 +0100120 };
121
Steven Burrows57e24e92016-08-04 18:38:24 +0100122 angular.module('ovTopo2')
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100123 .factory('Topo2Model', [
Steven Burrows86af4352016-11-16 18:19:12 -0600124 'FnService',
125 function (fn) {
126 Model.extend = fn.extend;
Steven Burrowsaf96a212016-12-28 12:57:02 +0000127
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100128 return Model;
129 }
130 ]);
Steven Burrows57e24e92016-08-04 18:38:24 +0100131})();