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