blob: 369da23ec40b6638323bf5afe1e0fb0efe66aa09 [file] [log] [blame]
Steven Burrows57e24e92016-08-04 18:38:24 +01001/*
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 Burrowsaf96a212016-12-28 12:57:02 +000025 var Model;
Steven Burrows57e24e92016-08-04 18:38:24 +010026
27 function Collection(models, options) {
28
Steven Burrows448468c2017-04-13 16:09:30 -070029 var opts = options || {};
Steven Burrows57e24e92016-08-04 18:38:24 +010030
31 this.models = [];
32 this._reset();
33
Steven Burrowsdfa52b02016-09-02 13:50:43 +010034 if (opts.comparator) {
35 this.comparator = opts.comparator;
36 }
Steven Burrows57e24e92016-08-04 18:38:24 +010037
38 if (models) {
39 this.add(models);
40 }
41 }
42
43 Collection.prototype = {
44 model: Model,
Steven Burrowsb15a3942017-01-17 17:25:04 +000045 addModel: function (data) {
Steven Burrows3d8d9332017-03-30 15:05:52 +010046 if (Object.getPrototypeOf(data) !== Object.prototype) {
47 this.models.push(data);
48 this._byId[data.get('id')] = data;
49 return data;
50 }
51
Steven Burrowsb15a3942017-01-17 17:25:04 +000052 var CollectionModel = this.model;
Steven Burrows68d6f952017-03-10 13:53:35 +000053 var model = new CollectionModel(data, this);
Steven Burrows57e24e92016-08-04 18:38:24 +010054
Steven Burrowsb15a3942017-01-17 17:25:04 +000055 this.models.push(model);
56 this._byId[data.id] = model;
57
58 return model;
59 },
60 add: function (data) {
Steven Burrows57e24e92016-08-04 18:38:24 +010061 var _this = this;
62
63 if (angular.isArray(data)) {
Steven Burrows57e24e92016-08-04 18:38:24 +010064 data.forEach(function (d) {
Steven Burrowsb15a3942017-01-17 17:25:04 +000065 _this.addModel(d);
Steven Burrows57e24e92016-08-04 18:38:24 +010066 });
Steven Burrowsb15a3942017-01-17 17:25:04 +000067 } else {
68 return this.addModel(data);
Steven Burrows57e24e92016-08-04 18:38:24 +010069 }
Steven Burrows57e24e92016-08-04 18:38:24 +010070 },
Steven Burrows42eb9e22017-02-06 14:20:24 +000071 remove: function (model) {
72 var index = _.indexOf(this.models, model);
73 this.models.splice(index, 1);
74 },
Steven Burrows57e24e92016-08-04 18:38:24 +010075 get: function (id) {
Steven Burrowsdfa52b02016-09-02 13:50:43 +010076
Steven Burrows57e24e92016-08-04 18:38:24 +010077 if (!id) {
Steven Burrowsdfa52b02016-09-02 13:50:43 +010078 return null;
Steven Burrows57e24e92016-08-04 18:38:24 +010079 }
Steven Burrowsdfa52b02016-09-02 13:50:43 +010080
Steven Burrows57e24e92016-08-04 18:38:24 +010081 return this._byId[id] || null;
82 },
83 sort: function () {
84
85 var comparator = this.comparator;
86
87 // Check if function
88 comparator = comparator.bind(this);
89 this.models.sort(comparator);
90
91 return this;
92 },
Steven Burrowse2d77d62016-10-21 12:35:58 -050093 filter: function (comparator) {
94 return _.filter(this.models, comparator);
95 },
Steven Burrows57e24e92016-08-04 18:38:24 +010096 _reset: function () {
97 this._byId = [];
98 this.models = [];
Steven Burrowsec1f45c2016-08-08 16:14:41 +010099 },
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100100 toJSON: function (options) {
101 return this.models.map(function (model) {
102 return model.toJSON(options);
103 });
104 }
Steven Burrows57e24e92016-08-04 18:38:24 +0100105 };
106
Steven Burrows57e24e92016-08-04 18:38:24 +0100107 angular.module('ovTopo2')
108 .factory('Topo2Collection',
Steven Burrows86af4352016-11-16 18:19:12 -0600109 ['Topo2Model', 'FnService',
110 function (_Model_, fn) {
Steven Burrows57e24e92016-08-04 18:38:24 +0100111
Steven Burrows86af4352016-11-16 18:19:12 -0600112 Collection.extend = fn.extend;
Steven Burrows57e24e92016-08-04 18:38:24 +0100113 Model = _Model_;
114 return Collection;
115 }
116 ]);
117
118})();