blob: ab684eb70b562b6b5e8f772ee37e77d72a8e36d0 [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 Burrows86af4352016-11-16 18:19:12 -060025 var Model,
26 extend;
Steven Burrows57e24e92016-08-04 18:38:24 +010027
28 function Collection(models, options) {
29
Steven Burrowsdfa52b02016-09-02 13:50:43 +010030 var opts = options || (options = {});
Steven Burrows57e24e92016-08-04 18:38:24 +010031
32 this.models = [];
33 this._reset();
34
Steven Burrowsdfa52b02016-09-02 13:50:43 +010035 if (opts.comparator) {
36 this.comparator = opts.comparator;
37 }
Steven Burrows57e24e92016-08-04 18:38:24 +010038
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 Burrowsdfa52b02016-09-02 13:50:43 +010054 var CollectionModel = _this.model;
55 var model = new CollectionModel(d);
Steven Burrows57e24e92016-08-04 18:38:24 +010056 model.collection = _this;
57
58 _this.models.push(model);
59 _this._byId[d.id] = model;
60 });
61 }
Steven Burrows57e24e92016-08-04 18:38:24 +010062 },
63 get: function (id) {
Steven Burrowsdfa52b02016-09-02 13:50:43 +010064
Steven Burrows57e24e92016-08-04 18:38:24 +010065 if (!id) {
Steven Burrowsdfa52b02016-09-02 13:50:43 +010066 return null;
Steven Burrows57e24e92016-08-04 18:38:24 +010067 }
Steven Burrowsdfa52b02016-09-02 13:50:43 +010068
Steven Burrows57e24e92016-08-04 18:38:24 +010069 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 Burrowse2d77d62016-10-21 12:35:58 -050081 filter: function (comparator) {
82 return _.filter(this.models, comparator);
83 },
Steven Burrows57e24e92016-08-04 18:38:24 +010084 _reset: function () {
85 this._byId = [];
86 this.models = [];
Steven Burrowsec1f45c2016-08-08 16:14:41 +010087 },
Steven Burrowsdfa52b02016-09-02 13:50:43 +010088 toJSON: function (options) {
89 return this.models.map(function (model) {
90 return model.toJSON(options);
91 });
92 }
Steven Burrows57e24e92016-08-04 18:38:24 +010093 };
94
Steven Burrows57e24e92016-08-04 18:38:24 +010095 angular.module('ovTopo2')
96 .factory('Topo2Collection',
Steven Burrows86af4352016-11-16 18:19:12 -060097 ['Topo2Model', 'FnService',
98 function (_Model_, fn) {
Steven Burrows57e24e92016-08-04 18:38:24 +010099
Steven Burrows86af4352016-11-16 18:19:12 -0600100 Collection.extend = fn.extend;
Steven Burrows57e24e92016-08-04 18:38:24 +0100101 Model = _Model_;
102 return Collection;
103 }
104 ]);
105
106})();