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