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