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 | |
Steven Burrows | 86af435 | 2016-11-16 18:19:12 -0600 | [diff] [blame] | 25 | var Model, |
| 26 | extend; |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 27 | |
| 28 | function Collection(models, options) { |
| 29 | |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 30 | var opts = options || (options = {}); |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 31 | |
| 32 | this.models = []; |
| 33 | this._reset(); |
| 34 | |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 35 | if (opts.comparator) { |
| 36 | this.comparator = opts.comparator; |
| 37 | } |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 38 | |
| 39 | if (models) { |
| 40 | this.add(models); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | Collection.prototype = { |
| 45 | model: Model, |
| 46 | add: function (data) { |
| 47 | |
| 48 | var _this = this; |
| 49 | |
| 50 | if (angular.isArray(data)) { |
| 51 | |
| 52 | data.forEach(function (d) { |
| 53 | |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 54 | var CollectionModel = _this.model; |
| 55 | var model = new CollectionModel(d); |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 56 | model.collection = _this; |
| 57 | |
| 58 | _this.models.push(model); |
| 59 | _this._byId[d.id] = model; |
| 60 | }); |
| 61 | } |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 62 | }, |
| 63 | get: function (id) { |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 64 | |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 65 | if (!id) { |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 66 | return null; |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 67 | } |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 68 | |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 69 | return this._byId[id] || null; |
| 70 | }, |
| 71 | sort: function () { |
| 72 | |
| 73 | var comparator = this.comparator; |
| 74 | |
| 75 | // Check if function |
| 76 | comparator = comparator.bind(this); |
| 77 | this.models.sort(comparator); |
| 78 | |
| 79 | return this; |
| 80 | }, |
Steven Burrows | e2d77d6 | 2016-10-21 12:35:58 -0500 | [diff] [blame] | 81 | filter: function (comparator) { |
| 82 | return _.filter(this.models, comparator); |
| 83 | }, |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 84 | _reset: function () { |
| 85 | this._byId = []; |
| 86 | this.models = []; |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 87 | }, |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 88 | toJSON: function (options) { |
| 89 | return this.models.map(function (model) { |
| 90 | return model.toJSON(options); |
| 91 | }); |
| 92 | } |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 93 | }; |
| 94 | |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 95 | angular.module('ovTopo2') |
| 96 | .factory('Topo2Collection', |
Steven Burrows | 86af435 | 2016-11-16 18:19:12 -0600 | [diff] [blame] | 97 | ['Topo2Model', 'FnService', |
| 98 | function (_Model_, fn) { |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 99 | |
Steven Burrows | 86af435 | 2016-11-16 18:19:12 -0600 | [diff] [blame] | 100 | Collection.extend = fn.extend; |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 101 | Model = _Model_; |
| 102 | return Collection; |
| 103 | } |
| 104 | ]); |
| 105 | |
| 106 | })(); |