Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 1 | /* |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 2 | * 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 Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 16 | |
| 17 | /* |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 18 | ONOS GUI -- Topology Force Module. |
| 19 | Visualization of the topology in an SVG layer, using a D3 Force Layout. |
| 20 | */ |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 21 | |
| 22 | (function () { |
| 23 | 'use strict'; |
| 24 | |
Steven Burrows | 68d6f95 | 2017-03-10 13:53:35 +0000 | [diff] [blame] | 25 | function Model(attributes, collection) { |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 26 | this.attributes = {}; |
Steven Burrows | ca1a39c | 2017-05-08 17:31:08 -0400 | [diff] [blame^] | 27 | this.set(angular.extend({}, attributes || {}), { silent: true }); |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 28 | this.initialize.apply(this, arguments); |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | Model.prototype = { |
| 32 | |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 33 | initialize: function () {}, |
| 34 | |
| 35 | onChange: function (property, value, options) {}, |
| 36 | |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 37 | get: function (attr) { |
| 38 | return this.attributes[attr]; |
| 39 | }, |
| 40 | |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 41 | set: function (key, val, options) { |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 42 | |
| 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 Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 55 | var opts = options || (options = {}); |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 56 | |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 57 | var unset = opts.unset, |
| 58 | silent = opts.silent, |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 59 | changes = [], |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 60 | changing = this._changing; |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 61 | |
| 62 | this._changing = true; |
| 63 | |
| 64 | if (!changing) { |
| 65 | |
| 66 | // NOTE: angular.copy causes issues in chrome |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 67 | this._previousAttributes = Object.create( |
| 68 | Object.getPrototypeOf(this.attributes) |
| 69 | ); |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 70 | 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 Burrows | 68d6f95 | 2017-03-10 13:53:35 +0000 | [diff] [blame] | 81 | if (!_.isEqual(current[index], val)) { |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 82 | changes.push(index); |
| 83 | } |
| 84 | |
Steven Burrows | 68d6f95 | 2017-03-10 13:53:35 +0000 | [diff] [blame] | 85 | if (!_.isEqual(previous[index], val)) { |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 86 | delete changed[index]; |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 87 | } else { |
| 88 | changed[index] = val; |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 89 | } |
| 90 | |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 91 | if (unset) { |
| 92 | delete current[index]; |
| 93 | } else { |
| 94 | current[index] = val; |
| 95 | } |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 96 | }); |
| 97 | |
| 98 | // Trigger all relevant attribute changes. |
| 99 | if (!silent) { |
| 100 | if (changes.length) { |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 101 | this._pending = opts; |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 102 | } |
| 103 | for (var i = 0; i < changes.length; i++) { |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 104 | this.onChange(changes[i], this, |
| 105 | current[changes[i]], opts); |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 106 | } |
| 107 | } |
| 108 | |
| 109 | this._changing = false; |
| 110 | return this; |
| 111 | }, |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 112 | toJSON: function (options) { |
| 113 | return angular.copy(this.attributes); |
Steven Burrows | ca1a39c | 2017-05-08 17:31:08 -0400 | [diff] [blame^] | 114 | }, |
| 115 | remove: function () { |
| 116 | if (this.collection) { |
| 117 | this.collection.remove(this); |
| 118 | } |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 119 | } |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 120 | }; |
| 121 | |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 122 | angular.module('ovTopo2') |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 123 | .factory('Topo2Model', [ |
Steven Burrows | 86af435 | 2016-11-16 18:19:12 -0600 | [diff] [blame] | 124 | 'FnService', |
| 125 | function (fn) { |
| 126 | Model.extend = fn.extend; |
Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame] | 127 | |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 128 | return Model; |
| 129 | } |
| 130 | ]); |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 131 | })(); |