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 | 86af435 | 2016-11-16 18:19:12 -0600 | [diff] [blame] | 25 | var extend; |
| 26 | |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 27 | function Model(attributes) { |
| 28 | |
| 29 | var attrs = attributes || {}; |
| 30 | this.attributes = {}; |
| 31 | |
| 32 | attrs = angular.extend({}, attrs); |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 33 | this.set(attrs, { silent: true }); |
| 34 | this.initialize.apply(this, arguments); |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | Model.prototype = { |
| 38 | |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 39 | initialize: function () {}, |
| 40 | |
| 41 | onChange: function (property, value, options) {}, |
| 42 | |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 43 | get: function (attr) { |
| 44 | return this.attributes[attr]; |
| 45 | }, |
| 46 | |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 47 | set: function (key, val, options) { |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 48 | |
| 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 Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 61 | var opts = options || (options = {}); |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 62 | |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 63 | var unset = opts.unset, |
| 64 | silent = opts.silent, |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 65 | changes = [], |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 66 | changing = this._changing; |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 67 | |
| 68 | this._changing = true; |
| 69 | |
| 70 | if (!changing) { |
| 71 | |
| 72 | // NOTE: angular.copy causes issues in chrome |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 73 | this._previousAttributes = Object.create( |
| 74 | Object.getPrototypeOf(this.attributes) |
| 75 | ); |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 76 | 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 Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 91 | if (angular.equals(previous[index], val)) { |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 92 | delete changed[index]; |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 93 | } else { |
| 94 | changed[index] = val; |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 95 | } |
| 96 | |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 97 | if (unset) { |
| 98 | delete current[index]; |
| 99 | } else { |
| 100 | current[index] = val; |
| 101 | } |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 102 | }); |
| 103 | |
| 104 | // Trigger all relevant attribute changes. |
| 105 | if (!silent) { |
| 106 | if (changes.length) { |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 107 | this._pending = opts; |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 108 | } |
| 109 | for (var i = 0; i < changes.length; i++) { |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 110 | this.onChange(changes[i], this, |
| 111 | current[changes[i]], opts); |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 112 | } |
| 113 | } |
| 114 | |
| 115 | this._changing = false; |
| 116 | return this; |
| 117 | }, |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 118 | toJSON: function (options) { |
| 119 | return angular.copy(this.attributes); |
| 120 | } |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 121 | }; |
| 122 | |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 123 | angular.module('ovTopo2') |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 124 | .factory('Topo2Model', [ |
Steven Burrows | 86af435 | 2016-11-16 18:19:12 -0600 | [diff] [blame] | 125 | 'FnService', |
| 126 | function (fn) { |
| 127 | Model.extend = fn.extend; |
| 128 | |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 129 | return Model; |
| 130 | } |
| 131 | ]); |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 132 | })(); |