blob: c3662e625745869d7425eebf583ed83dd0db1c3c [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 Burrows25fa1dc2017-04-17 18:29:22 -040034 this.initialize.apply(this, arguments);
35
Steven Burrowsdfa52b02016-09-02 13:50:43 +010036 if (opts.comparator) {
37 this.comparator = opts.comparator;
38 }
Steven Burrows57e24e92016-08-04 18:38:24 +010039
40 if (models) {
41 this.add(models);
42 }
43 }
44
45 Collection.prototype = {
46 model: Model,
Steven Burrows25fa1dc2017-04-17 18:29:22 -040047 initialize: function () {},
Steven Burrowsb15a3942017-01-17 17:25:04 +000048 addModel: function (data) {
Steven Burrows3d8d9332017-03-30 15:05:52 +010049 if (Object.getPrototypeOf(data) !== Object.prototype) {
50 this.models.push(data);
51 this._byId[data.get('id')] = data;
52 return data;
53 }
54
Steven Burrowsb15a3942017-01-17 17:25:04 +000055 var CollectionModel = this.model;
Steven Burrows68d6f952017-03-10 13:53:35 +000056 var model = new CollectionModel(data, this);
Steven Burrows57e24e92016-08-04 18:38:24 +010057
Steven Burrowsb15a3942017-01-17 17:25:04 +000058 this.models.push(model);
59 this._byId[data.id] = model;
60
61 return model;
62 },
63 add: function (data) {
Steven Burrows57e24e92016-08-04 18:38:24 +010064 var _this = this;
65
66 if (angular.isArray(data)) {
Steven Burrows57e24e92016-08-04 18:38:24 +010067 data.forEach(function (d) {
Steven Burrowsb15a3942017-01-17 17:25:04 +000068 _this.addModel(d);
Steven Burrows57e24e92016-08-04 18:38:24 +010069 });
Steven Burrowsb15a3942017-01-17 17:25:04 +000070 } else {
71 return this.addModel(data);
Steven Burrows57e24e92016-08-04 18:38:24 +010072 }
Steven Burrows57e24e92016-08-04 18:38:24 +010073 },
Steven Burrows42eb9e22017-02-06 14:20:24 +000074 remove: function (model) {
75 var index = _.indexOf(this.models, model);
76 this.models.splice(index, 1);
77 },
Steven Burrows57e24e92016-08-04 18:38:24 +010078 get: function (id) {
Steven Burrowsdfa52b02016-09-02 13:50:43 +010079
Steven Burrows57e24e92016-08-04 18:38:24 +010080 if (!id) {
Steven Burrowsdfa52b02016-09-02 13:50:43 +010081 return null;
Steven Burrows57e24e92016-08-04 18:38:24 +010082 }
Steven Burrowsdfa52b02016-09-02 13:50:43 +010083
Steven Burrows57e24e92016-08-04 18:38:24 +010084 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 Burrowse2d77d62016-10-21 12:35:58 -050096 filter: function (comparator) {
97 return _.filter(this.models, comparator);
98 },
Steven Burrows57e24e92016-08-04 18:38:24 +010099 _reset: function () {
100 this._byId = [];
101 this.models = [];
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100102 },
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100103 toJSON: function (options) {
104 return this.models.map(function (model) {
105 return model.toJSON(options);
106 });
107 }
Steven Burrows57e24e92016-08-04 18:38:24 +0100108 };
109
Steven Burrows57e24e92016-08-04 18:38:24 +0100110 angular.module('ovTopo2')
111 .factory('Topo2Collection',
Steven Burrows86af4352016-11-16 18:19:12 -0600112 ['Topo2Model', 'FnService',
113 function (_Model_, fn) {
Steven Burrows57e24e92016-08-04 18:38:24 +0100114
Steven Burrows86af4352016-11-16 18:19:12 -0600115 Collection.extend = fn.extend;
Steven Burrows57e24e92016-08-04 18:38:24 +0100116 Model = _Model_;
117 return Collection;
118 }
119 ]);
120
121})();