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 | |
| 25 | function Model(attributes) { |
| 26 | |
| 27 | var attrs = attributes || {}; |
| 28 | this.attributes = {}; |
| 29 | |
| 30 | attrs = angular.extend({}, attrs); |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 31 | this.set(attrs, { silent: true }); |
| 32 | this.initialize.apply(this, arguments); |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | Model.prototype = { |
| 36 | |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 37 | initialize: function () {}, |
| 38 | |
| 39 | onChange: function (property, value, options) {}, |
| 40 | |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 41 | get: function (attr) { |
| 42 | return this.attributes[attr]; |
| 43 | }, |
| 44 | |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 45 | set: function (key, val, options) { |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 46 | |
| 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 Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 59 | var opts = options || (options = {}); |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 60 | |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 61 | var unset = opts.unset, |
| 62 | silent = opts.silent, |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 63 | changes = [], |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 64 | changing = this._changing; |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 65 | |
| 66 | this._changing = true; |
| 67 | |
| 68 | if (!changing) { |
| 69 | |
| 70 | // NOTE: angular.copy causes issues in chrome |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 71 | this._previousAttributes = Object.create( |
| 72 | Object.getPrototypeOf(this.attributes) |
| 73 | ); |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 74 | 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 Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 89 | if (angular.equals(previous[index], val)) { |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 90 | delete changed[index]; |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 91 | } else { |
| 92 | changed[index] = val; |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 93 | } |
| 94 | |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 95 | if (unset) { |
| 96 | delete current[index]; |
| 97 | } else { |
| 98 | current[index] = val; |
| 99 | } |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 100 | }); |
| 101 | |
| 102 | // Trigger all relevant attribute changes. |
| 103 | if (!silent) { |
| 104 | if (changes.length) { |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 105 | this._pending = opts; |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 106 | } |
| 107 | for (var i = 0; i < changes.length; i++) { |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 108 | this.onChange(changes[i], this, |
| 109 | current[changes[i]], opts); |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 110 | } |
| 111 | } |
| 112 | |
| 113 | this._changing = false; |
| 114 | return this; |
| 115 | }, |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 116 | toJSON: function (options) { |
| 117 | return angular.copy(this.attributes); |
| 118 | } |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 119 | }; |
| 120 | |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 121 | Model.extend = function (protoProps, staticProps) { |
| 122 | |
| 123 | var parent = this; |
| 124 | var child; |
| 125 | |
| 126 | child = function () { |
| 127 | return parent.apply(this, arguments); |
| 128 | }; |
| 129 | |
| 130 | angular.extend(child, parent, staticProps); |
| 131 | |
| 132 | // Set the prototype chain to inherit from `parent`, without calling |
| 133 | // `parent`'s constructor function and add the prototype properties. |
| 134 | child.prototype = angular.extend({}, parent.prototype, protoProps); |
| 135 | child.prototype.constructor = child; |
| 136 | |
| 137 | // Set a convenience property in case the parent's prototype is needed |
| 138 | // later. |
| 139 | child.__super__ = parent.prototype; |
| 140 | |
| 141 | return child; |
| 142 | }; |
| 143 | |
| 144 | angular.module('ovTopo2') |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 145 | .factory('Topo2Model', [ |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 146 | function () { |
| 147 | return Model; |
| 148 | } |
| 149 | ]); |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 150 | |
| 151 | })(); |