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 | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame^] | 45 | set: function(key, val, options) { |
| 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 | |
| 59 | options || (options = {}); |
| 60 | |
| 61 | var unset = options.unset, |
| 62 | silent = options.silent, |
| 63 | changes = [], |
| 64 | changing = this._changing; |
| 65 | |
| 66 | this._changing = true; |
| 67 | |
| 68 | if (!changing) { |
| 69 | |
| 70 | // NOTE: angular.copy causes issues in chrome |
| 71 | this._previousAttributes = Object.create(Object.getPrototypeOf(this.attributes)); |
| 72 | this.changed = {}; |
| 73 | } |
| 74 | |
| 75 | var current = this.attributes, |
| 76 | changed = this.changed, |
| 77 | previous = this._previousAttributes; |
| 78 | |
| 79 | angular.forEach(attributes, function (attribute, index) { |
| 80 | |
| 81 | val = attribute; |
| 82 | |
| 83 | if (!angular.equals(current[index], val)) { |
| 84 | changes.push(index); |
| 85 | } |
| 86 | |
| 87 | if (!angular.equals(previous[index], val)) { |
| 88 | changed[index] = val; |
| 89 | } else { |
| 90 | delete changed[index]; |
| 91 | } |
| 92 | |
| 93 | unset ? delete current[index] : current[index] = val; |
| 94 | }); |
| 95 | |
| 96 | // Trigger all relevant attribute changes. |
| 97 | if (!silent) { |
| 98 | if (changes.length) { |
| 99 | this._pending = options; |
| 100 | } |
| 101 | for (var i = 0; i < changes.length; i++) { |
| 102 | this.onChange(changes[i], this, current[changes[i]], options); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | this._changing = false; |
| 107 | return this; |
| 108 | }, |
| 109 | toJSON: function(options) { |
| 110 | return angular.copy(this.attributes) |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 111 | }, |
| 112 | }; |
| 113 | |
| 114 | |
| 115 | Model.extend = function (protoProps, staticProps) { |
| 116 | |
| 117 | var parent = this; |
| 118 | var child; |
| 119 | |
| 120 | child = function () { |
| 121 | return parent.apply(this, arguments); |
| 122 | }; |
| 123 | |
| 124 | angular.extend(child, parent, staticProps); |
| 125 | |
| 126 | // Set the prototype chain to inherit from `parent`, without calling |
| 127 | // `parent`'s constructor function and add the prototype properties. |
| 128 | child.prototype = angular.extend({}, parent.prototype, protoProps); |
| 129 | child.prototype.constructor = child; |
| 130 | |
| 131 | // Set a convenience property in case the parent's prototype is needed |
| 132 | // later. |
| 133 | child.__super__ = parent.prototype; |
| 134 | |
| 135 | return child; |
| 136 | }; |
| 137 | |
| 138 | angular.module('ovTopo2') |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame^] | 139 | .factory('Topo2Model', |
| 140 | [ |
| 141 | function () { |
| 142 | return Model; |
| 143 | } |
| 144 | ]); |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 145 | |
| 146 | })(); |