blob: efcd2011533ced5b894343a6e68cb17c44c68f37 [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 });
Steven Burrowsec1f45c2016-08-08 16:14:41 +010028 this.initialize.apply(this, arguments);
Steven Burrows57e24e92016-08-04 18:38:24 +010029 }
30
31 Model.prototype = {
32
Steven Burrowsec1f45c2016-08-08 16:14:41 +010033 initialize: function () {},
34
35 onChange: function (property, value, options) {},
36
Steven Burrows57e24e92016-08-04 18:38:24 +010037 get: function (attr) {
38 return this.attributes[attr];
39 },
40
Steven Burrowsdfa52b02016-09-02 13:50:43 +010041 set: function (key, val, options) {
Steven Burrowsec1f45c2016-08-08 16:14:41 +010042
43 if (!key) {
44 return this;
45 }
46
47 var attributes;
48 if (typeof key === 'object') {
49 attributes = key;
50 options = val;
51 } else {
52 (attributes = {})[key] = val;
53 }
54
Steven Burrowsdfa52b02016-09-02 13:50:43 +010055 var opts = options || (options = {});
Steven Burrowsec1f45c2016-08-08 16:14:41 +010056
Steven Burrowsdfa52b02016-09-02 13:50:43 +010057 var unset = opts.unset,
58 silent = opts.silent,
Steven Burrowsec1f45c2016-08-08 16:14:41 +010059 changes = [],
Steven Burrowsdfa52b02016-09-02 13:50:43 +010060 changing = this._changing;
Steven Burrowsec1f45c2016-08-08 16:14:41 +010061
62 this._changing = true;
63
64 if (!changing) {
65
66 // NOTE: angular.copy causes issues in chrome
Steven Burrowsdfa52b02016-09-02 13:50:43 +010067 this._previousAttributes = Object.create(
68 Object.getPrototypeOf(this.attributes)
69 );
Steven Burrowsec1f45c2016-08-08 16:14:41 +010070 this.changed = {};
71 }
72
73 var current = this.attributes,
74 changed = this.changed,
75 previous = this._previousAttributes;
76
77 angular.forEach(attributes, function (attribute, index) {
78
79 val = attribute;
80
Steven Burrows68d6f952017-03-10 13:53:35 +000081 if (!_.isEqual(current[index], val)) {
Steven Burrowsec1f45c2016-08-08 16:14:41 +010082 changes.push(index);
83 }
84
Steven Burrows68d6f952017-03-10 13:53:35 +000085 if (!_.isEqual(previous[index], val)) {
Steven Burrowsec1f45c2016-08-08 16:14:41 +010086 delete changed[index];
Steven Burrowsdfa52b02016-09-02 13:50:43 +010087 } else {
88 changed[index] = val;
Steven Burrowsec1f45c2016-08-08 16:14:41 +010089 }
90
Steven Burrowsdfa52b02016-09-02 13:50:43 +010091 if (unset) {
92 delete current[index];
93 } else {
94 current[index] = val;
95 }
Steven Burrowsec1f45c2016-08-08 16:14:41 +010096 });
97
98 // Trigger all relevant attribute changes.
99 if (!silent) {
100 if (changes.length) {
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100101 this._pending = opts;
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100102 }
103 for (var i = 0; i < changes.length; i++) {
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100104 this.onChange(changes[i], this,
105 current[changes[i]], opts);
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100106 }
107 }
108
109 this._changing = false;
110 return this;
111 },
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100112 toJSON: function (options) {
113 return angular.copy(this.attributes);
Steven Burrowsca1a39c2017-05-08 17:31:08 -0400114 },
115 remove: function () {
116 if (this.collection) {
117 this.collection.remove(this);
118 }
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100119 }
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})();