blob: b49de810c1563e4ec76c04cdaa94332f0b7906a5 [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 Burrowsdfa52b02016-09-02 13:50:43 +010029 var opts = options || (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) {
46 var CollectionModel = this.model;
47 var model = new CollectionModel(data);
48 model.collection = this;
Steven Burrows57e24e92016-08-04 18:38:24 +010049
Steven Burrowsb15a3942017-01-17 17:25:04 +000050 this.models.push(model);
51 this._byId[data.id] = model;
52
53 return model;
54 },
55 add: function (data) {
Steven Burrows57e24e92016-08-04 18:38:24 +010056 var _this = this;
57
58 if (angular.isArray(data)) {
Steven Burrows57e24e92016-08-04 18:38:24 +010059 data.forEach(function (d) {
Steven Burrowsb15a3942017-01-17 17:25:04 +000060 _this.addModel(d);
Steven Burrows57e24e92016-08-04 18:38:24 +010061 });
Steven Burrowsb15a3942017-01-17 17:25:04 +000062 } else {
63 return this.addModel(data);
Steven Burrows57e24e92016-08-04 18:38:24 +010064 }
Steven Burrows57e24e92016-08-04 18:38:24 +010065 },
Steven Burrows42eb9e22017-02-06 14:20:24 +000066 remove: function (model) {
67 var index = _.indexOf(this.models, model);
68 this.models.splice(index, 1);
69 },
Steven Burrows57e24e92016-08-04 18:38:24 +010070 get: function (id) {
Steven Burrowsdfa52b02016-09-02 13:50:43 +010071
Steven Burrows57e24e92016-08-04 18:38:24 +010072 if (!id) {
Steven Burrowsdfa52b02016-09-02 13:50:43 +010073 return null;
Steven Burrows57e24e92016-08-04 18:38:24 +010074 }
Steven Burrowsdfa52b02016-09-02 13:50:43 +010075
Steven Burrows57e24e92016-08-04 18:38:24 +010076 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 Burrowse2d77d62016-10-21 12:35:58 -050088 filter: function (comparator) {
89 return _.filter(this.models, comparator);
90 },
Steven Burrows57e24e92016-08-04 18:38:24 +010091 _reset: function () {
92 this._byId = [];
93 this.models = [];
Steven Burrowsec1f45c2016-08-08 16:14:41 +010094 },
Steven Burrowsdfa52b02016-09-02 13:50:43 +010095 toJSON: function (options) {
96 return this.models.map(function (model) {
97 return model.toJSON(options);
98 });
99 }
Steven Burrows57e24e92016-08-04 18:38:24 +0100100 };
101
Steven Burrows57e24e92016-08-04 18:38:24 +0100102 angular.module('ovTopo2')
103 .factory('Topo2Collection',
Steven Burrows86af4352016-11-16 18:19:12 -0600104 ['Topo2Model', 'FnService',
105 function (_Model_, fn) {
Steven Burrows57e24e92016-08-04 18:38:24 +0100106
Steven Burrows86af4352016-11-16 18:19:12 -0600107 Collection.extend = fn.extend;
Steven Burrows57e24e92016-08-04 18:38:24 +0100108 Model = _Model_;
109 return Collection;
110 }
111 ]);
112
113})();