blob: c8430fbbe70bf53fa0340b23fd257e2921e4ed48 [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
25 function Model(attributes) {
26
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 });
32 this.initialize.apply(this, arguments);
Steven Burrows57e24e92016-08-04 18:38:24 +010033 }
34
35 Model.prototype = {
36
Steven Burrowsec1f45c2016-08-08 16:14:41 +010037 initialize: function () {},
38
39 onChange: function (property, value, options) {},
40
Steven Burrows57e24e92016-08-04 18:38:24 +010041 get: function (attr) {
42 return this.attributes[attr];
43 },
44
Steven Burrowsdfa52b02016-09-02 13:50:43 +010045 set: function (key, val, options) {
Steven Burrowsec1f45c2016-08-08 16:14:41 +010046
47 if (!key) {
48 return this;
49 }
50
51 var attributes;
52 if (typeof key === 'object') {
53 attributes = key;
54 options = val;
55 } else {
56 (attributes = {})[key] = val;
57 }
58
Steven Burrowsdfa52b02016-09-02 13:50:43 +010059 var opts = options || (options = {});
Steven Burrowsec1f45c2016-08-08 16:14:41 +010060
Steven Burrowsdfa52b02016-09-02 13:50:43 +010061 var unset = opts.unset,
62 silent = opts.silent,
Steven Burrowsec1f45c2016-08-08 16:14:41 +010063 changes = [],
Steven Burrowsdfa52b02016-09-02 13:50:43 +010064 changing = this._changing;
Steven Burrowsec1f45c2016-08-08 16:14:41 +010065
66 this._changing = true;
67
68 if (!changing) {
69
70 // NOTE: angular.copy causes issues in chrome
Steven Burrowsdfa52b02016-09-02 13:50:43 +010071 this._previousAttributes = Object.create(
72 Object.getPrototypeOf(this.attributes)
73 );
Steven Burrowsec1f45c2016-08-08 16:14:41 +010074 this.changed = {};
75 }
76
77 var current = this.attributes,
78 changed = this.changed,
79 previous = this._previousAttributes;
80
81 angular.forEach(attributes, function (attribute, index) {
82
83 val = attribute;
84
85 if (!angular.equals(current[index], val)) {
86 changes.push(index);
87 }
88
Steven Burrowsdfa52b02016-09-02 13:50:43 +010089 if (angular.equals(previous[index], val)) {
Steven Burrowsec1f45c2016-08-08 16:14:41 +010090 delete changed[index];
Steven Burrowsdfa52b02016-09-02 13:50:43 +010091 } else {
92 changed[index] = val;
Steven Burrowsec1f45c2016-08-08 16:14:41 +010093 }
94
Steven Burrowsdfa52b02016-09-02 13:50:43 +010095 if (unset) {
96 delete current[index];
97 } else {
98 current[index] = val;
99 }
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100100 });
101
102 // Trigger all relevant attribute changes.
103 if (!silent) {
104 if (changes.length) {
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100105 this._pending = opts;
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100106 }
107 for (var i = 0; i < changes.length; i++) {
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100108 this.onChange(changes[i], this,
109 current[changes[i]], opts);
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100110 }
111 }
112
113 this._changing = false;
114 return this;
115 },
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100116 toJSON: function (options) {
117 return angular.copy(this.attributes);
118 }
Steven Burrows57e24e92016-08-04 18:38:24 +0100119 };
120
Steven Burrows57e24e92016-08-04 18:38:24 +0100121 angular.module('ovTopo2')
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100122 .factory('Topo2Model', [
Steven Burrows86af4352016-11-16 18:19:12 -0600123 'FnService',
124 function (fn) {
125 Model.extend = fn.extend;
Steven Burrowsaf96a212016-12-28 12:57:02 +0000126
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100127 return Model;
128 }
129 ]);
Steven Burrows57e24e92016-08-04 18:38:24 +0100130})();