blob: 6da29c4eebebff15e5768550af5cca224aca0f82 [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 this.attributes = {};
Steven Burrowsca1a39c2017-05-08 17:31:08 -040027 this.set(angular.extend({}, attributes || {}), { silent: true });
Simon Hunt0ee20bf2017-05-10 19:59:17 -070028 this.collection = collection;
Steven Burrowsec1f45c2016-08-08 16:14:41 +010029 this.initialize.apply(this, arguments);
Steven Burrows57e24e92016-08-04 18:38:24 +010030 }
31
32 Model.prototype = {
33
Steven Burrowsec1f45c2016-08-08 16:14:41 +010034 initialize: function () {},
35
36 onChange: function (property, value, options) {},
37
Steven Burrows57e24e92016-08-04 18:38:24 +010038 get: function (attr) {
39 return this.attributes[attr];
40 },
41
Steven Burrowsdfa52b02016-09-02 13:50:43 +010042 set: function (key, val, options) {
Steven Burrowsec1f45c2016-08-08 16:14:41 +010043
44 if (!key) {
45 return this;
46 }
47
48 var attributes;
49 if (typeof key === 'object') {
50 attributes = key;
51 options = val;
52 } else {
53 (attributes = {})[key] = val;
54 }
55
Steven Burrowsdfa52b02016-09-02 13:50:43 +010056 var opts = options || (options = {});
Steven Burrowsec1f45c2016-08-08 16:14:41 +010057
Steven Burrowsdfa52b02016-09-02 13:50:43 +010058 var unset = opts.unset,
59 silent = opts.silent,
Steven Burrowsec1f45c2016-08-08 16:14:41 +010060 changes = [],
Steven Burrowsdfa52b02016-09-02 13:50:43 +010061 changing = this._changing;
Steven Burrowsec1f45c2016-08-08 16:14:41 +010062
63 this._changing = true;
64
65 if (!changing) {
66
67 // NOTE: angular.copy causes issues in chrome
Steven Burrowsdfa52b02016-09-02 13:50:43 +010068 this._previousAttributes = Object.create(
69 Object.getPrototypeOf(this.attributes)
70 );
Steven Burrowsec1f45c2016-08-08 16:14:41 +010071 this.changed = {};
72 }
73
74 var current = this.attributes,
75 changed = this.changed,
76 previous = this._previousAttributes;
77
78 angular.forEach(attributes, function (attribute, index) {
79
80 val = attribute;
81
Steven Burrows68d6f952017-03-10 13:53:35 +000082 if (!_.isEqual(current[index], val)) {
Steven Burrowsec1f45c2016-08-08 16:14:41 +010083 changes.push(index);
84 }
85
Steven Burrows68d6f952017-03-10 13:53:35 +000086 if (!_.isEqual(previous[index], val)) {
Steven Burrowsec1f45c2016-08-08 16:14:41 +010087 delete changed[index];
Steven Burrowsdfa52b02016-09-02 13:50:43 +010088 } else {
89 changed[index] = val;
Steven Burrowsec1f45c2016-08-08 16:14:41 +010090 }
91
Steven Burrowsdfa52b02016-09-02 13:50:43 +010092 if (unset) {
93 delete current[index];
94 } else {
95 current[index] = val;
96 }
Steven Burrowsec1f45c2016-08-08 16:14:41 +010097 });
98
99 // Trigger all relevant attribute changes.
100 if (!silent) {
101 if (changes.length) {
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100102 this._pending = opts;
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100103 }
104 for (var i = 0; i < changes.length; i++) {
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100105 this.onChange(changes[i], this,
106 current[changes[i]], opts);
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100107 }
108 }
109
110 this._changing = false;
111 return this;
112 },
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100113 toJSON: function (options) {
114 return angular.copy(this.attributes);
Steven Burrowsca1a39c2017-05-08 17:31:08 -0400115 },
116 remove: function () {
117 if (this.collection) {
118 this.collection.remove(this);
119 }
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100120 }
Steven Burrows57e24e92016-08-04 18:38:24 +0100121 };
122
Steven Burrows57e24e92016-08-04 18:38:24 +0100123 angular.module('ovTopo2')
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100124 .factory('Topo2Model', [
Steven Burrows86af4352016-11-16 18:19:12 -0600125 'FnService',
126 function (fn) {
127 Model.extend = fn.extend;
Steven Burrowsaf96a212016-12-28 12:57:02 +0000128
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100129 return Model;
130 }
131 ]);
Steven Burrows57e24e92016-08-04 18:38:24 +0100132})();