blob: f76456e307128c01b68d0b272d7fc91c77bdd6a9 [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 Burrows86af4352016-11-16 18:19:12 -060025 var extend;
26
Steven Burrows57e24e92016-08-04 18:38:24 +010027 function Model(attributes) {
28
29 var attrs = attributes || {};
30 this.attributes = {};
31
32 attrs = angular.extend({}, attrs);
Steven Burrowsec1f45c2016-08-08 16:14:41 +010033 this.set(attrs, { silent: true });
34 this.initialize.apply(this, arguments);
Steven Burrows57e24e92016-08-04 18:38:24 +010035 }
36
37 Model.prototype = {
38
Steven Burrowsec1f45c2016-08-08 16:14:41 +010039 initialize: function () {},
40
41 onChange: function (property, value, options) {},
42
Steven Burrows57e24e92016-08-04 18:38:24 +010043 get: function (attr) {
44 return this.attributes[attr];
45 },
46
Steven Burrowsdfa52b02016-09-02 13:50:43 +010047 set: function (key, val, options) {
Steven Burrowsec1f45c2016-08-08 16:14:41 +010048
49 if (!key) {
50 return this;
51 }
52
53 var attributes;
54 if (typeof key === 'object') {
55 attributes = key;
56 options = val;
57 } else {
58 (attributes = {})[key] = val;
59 }
60
Steven Burrowsdfa52b02016-09-02 13:50:43 +010061 var opts = options || (options = {});
Steven Burrowsec1f45c2016-08-08 16:14:41 +010062
Steven Burrowsdfa52b02016-09-02 13:50:43 +010063 var unset = opts.unset,
64 silent = opts.silent,
Steven Burrowsec1f45c2016-08-08 16:14:41 +010065 changes = [],
Steven Burrowsdfa52b02016-09-02 13:50:43 +010066 changing = this._changing;
Steven Burrowsec1f45c2016-08-08 16:14:41 +010067
68 this._changing = true;
69
70 if (!changing) {
71
72 // NOTE: angular.copy causes issues in chrome
Steven Burrowsdfa52b02016-09-02 13:50:43 +010073 this._previousAttributes = Object.create(
74 Object.getPrototypeOf(this.attributes)
75 );
Steven Burrowsec1f45c2016-08-08 16:14:41 +010076 this.changed = {};
77 }
78
79 var current = this.attributes,
80 changed = this.changed,
81 previous = this._previousAttributes;
82
83 angular.forEach(attributes, function (attribute, index) {
84
85 val = attribute;
86
87 if (!angular.equals(current[index], val)) {
88 changes.push(index);
89 }
90
Steven Burrowsdfa52b02016-09-02 13:50:43 +010091 if (angular.equals(previous[index], val)) {
Steven Burrowsec1f45c2016-08-08 16:14:41 +010092 delete changed[index];
Steven Burrowsdfa52b02016-09-02 13:50:43 +010093 } else {
94 changed[index] = val;
Steven Burrowsec1f45c2016-08-08 16:14:41 +010095 }
96
Steven Burrowsdfa52b02016-09-02 13:50:43 +010097 if (unset) {
98 delete current[index];
99 } else {
100 current[index] = val;
101 }
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100102 });
103
104 // Trigger all relevant attribute changes.
105 if (!silent) {
106 if (changes.length) {
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100107 this._pending = opts;
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100108 }
109 for (var i = 0; i < changes.length; i++) {
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100110 this.onChange(changes[i], this,
111 current[changes[i]], opts);
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100112 }
113 }
114
115 this._changing = false;
116 return this;
117 },
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100118 toJSON: function (options) {
119 return angular.copy(this.attributes);
120 }
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;
128
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100129 return Model;
130 }
131 ]);
Steven Burrows57e24e92016-08-04 18:38:24 +0100132})();