Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | |
| 17 | /* |
| 18 | ONOS GUI -- Topology Collection Module. |
| 19 | A Data Store that contains model data from the server |
| 20 | */ |
| 21 | |
| 22 | (function () { |
| 23 | 'use strict'; |
| 24 | |
| 25 | var Model; |
| 26 | |
| 27 | function Collection(models, options) { |
| 28 | |
| 29 | options || (options = {}); |
| 30 | |
| 31 | this.models = []; |
| 32 | this._reset(); |
| 33 | |
| 34 | if (options.comparator !== void 0) this.comparator = options.comparator; |
| 35 | |
| 36 | if (models) { |
| 37 | this.add(models); |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | Collection.prototype = { |
| 42 | model: Model, |
| 43 | add: function (data) { |
| 44 | |
| 45 | var _this = this; |
| 46 | |
| 47 | if (angular.isArray(data)) { |
| 48 | |
| 49 | data.forEach(function (d) { |
| 50 | |
| 51 | var model = new _this.model(d); |
| 52 | model.collection = _this; |
| 53 | |
| 54 | _this.models.push(model); |
| 55 | _this._byId[d.id] = model; |
| 56 | }); |
| 57 | } |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 58 | }, |
| 59 | get: function (id) { |
| 60 | if (!id) { |
| 61 | return void 0; |
| 62 | } |
| 63 | return this._byId[id] || null; |
| 64 | }, |
| 65 | sort: function () { |
| 66 | |
| 67 | var comparator = this.comparator; |
| 68 | |
| 69 | // Check if function |
| 70 | comparator = comparator.bind(this); |
| 71 | this.models.sort(comparator); |
| 72 | |
| 73 | return this; |
| 74 | }, |
| 75 | _reset: function () { |
| 76 | this._byId = []; |
| 77 | this.models = []; |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 78 | }, |
| 79 | toJSON: function(options) { |
| 80 | return this.models.map(function(model) { return model.toJSON(options); }); |
| 81 | }, |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 82 | }; |
| 83 | |
| 84 | Collection.extend = function (protoProps, staticProps) { |
| 85 | |
| 86 | var parent = this; |
| 87 | var child; |
| 88 | |
| 89 | child = function () { |
| 90 | return parent.apply(this, arguments); |
| 91 | }; |
| 92 | |
| 93 | angular.extend(child, parent, staticProps); |
| 94 | |
| 95 | // Set the prototype chain to inherit from `parent`, without calling |
| 96 | // `parent`'s constructor function and add the prototype properties. |
| 97 | child.prototype = angular.extend({}, parent.prototype, protoProps); |
| 98 | child.prototype.constructor = child; |
| 99 | |
| 100 | // Set a convenience property in case the parent's prototype is needed |
| 101 | // later. |
| 102 | child.__super__ = parent.prototype; |
| 103 | |
| 104 | return child; |
| 105 | }; |
| 106 | |
| 107 | angular.module('ovTopo2') |
| 108 | .factory('Topo2Collection', |
| 109 | ['Topo2Model', |
| 110 | function (_Model_) { |
| 111 | |
| 112 | Model = _Model_; |
| 113 | return Collection; |
| 114 | } |
| 115 | ]); |
| 116 | |
| 117 | })(); |