blob: 3218988637a87089fd95df9c3e048308a7a1e8ad [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);
Steven Burrowsca1a39c2017-05-08 17:31:08 -040051 data.collection = this;
Steven Burrows3d8d9332017-03-30 15:05:52 +010052 this._byId[data.get('id')] = data;
53 return data;
54 }
55
Steven Burrows2b29ca42017-05-12 10:58:05 -040056 if (this._byId[data.id]) {
57 return this._byId[data.id];
58 }
59
Steven Burrowsb15a3942017-01-17 17:25:04 +000060 var CollectionModel = this.model;
Steven Burrows68d6f952017-03-10 13:53:35 +000061 var model = new CollectionModel(data, this);
Steven Burrowsca1a39c2017-05-08 17:31:08 -040062 model.collection = this;
Steven Burrows57e24e92016-08-04 18:38:24 +010063
Steven Burrowsb15a3942017-01-17 17:25:04 +000064 this.models.push(model);
65 this._byId[data.id] = model;
66
67 return model;
68 },
69 add: function (data) {
Steven Burrows57e24e92016-08-04 18:38:24 +010070 var _this = this;
71
72 if (angular.isArray(data)) {
Steven Burrows57e24e92016-08-04 18:38:24 +010073 data.forEach(function (d) {
Steven Burrowsb15a3942017-01-17 17:25:04 +000074 _this.addModel(d);
Steven Burrows57e24e92016-08-04 18:38:24 +010075 });
Steven Burrowsb15a3942017-01-17 17:25:04 +000076 } else {
77 return this.addModel(data);
Steven Burrows57e24e92016-08-04 18:38:24 +010078 }
Steven Burrows57e24e92016-08-04 18:38:24 +010079 },
Steven Burrows42eb9e22017-02-06 14:20:24 +000080 remove: function (model) {
81 var index = _.indexOf(this.models, model);
82 this.models.splice(index, 1);
83 },
Steven Burrows57e24e92016-08-04 18:38:24 +010084 get: function (id) {
Steven Burrowsdfa52b02016-09-02 13:50:43 +010085
Steven Burrows57e24e92016-08-04 18:38:24 +010086 if (!id) {
Steven Burrowsdfa52b02016-09-02 13:50:43 +010087 return null;
Steven Burrows57e24e92016-08-04 18:38:24 +010088 }
Steven Burrowsdfa52b02016-09-02 13:50:43 +010089
Steven Burrows57e24e92016-08-04 18:38:24 +010090 return this._byId[id] || null;
91 },
92 sort: function () {
93
94 var comparator = this.comparator;
95
96 // Check if function
97 comparator = comparator.bind(this);
98 this.models.sort(comparator);
99
100 return this;
101 },
Steven Burrowse2d77d62016-10-21 12:35:58 -0500102 filter: function (comparator) {
103 return _.filter(this.models, comparator);
104 },
Steven Burrowsca1a39c2017-05-08 17:31:08 -0400105 empty: function () {
106 _.map(this.models, function (m) {
107 m.remove();
108 });
109 this._reset();
110 },
Steven Burrows57e24e92016-08-04 18:38:24 +0100111 _reset: function () {
112 this._byId = [];
113 this.models = [];
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100114 },
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100115 toJSON: function (options) {
116 return this.models.map(function (model) {
117 return model.toJSON(options);
118 });
119 }
Steven Burrows57e24e92016-08-04 18:38:24 +0100120 };
121
Steven Burrows57e24e92016-08-04 18:38:24 +0100122 angular.module('ovTopo2')
123 .factory('Topo2Collection',
Steven Burrows86af4352016-11-16 18:19:12 -0600124 ['Topo2Model', 'FnService',
125 function (_Model_, fn) {
Steven Burrows86af4352016-11-16 18:19:12 -0600126 Collection.extend = fn.extend;
Steven Burrows57e24e92016-08-04 18:38:24 +0100127 Model = _Model_;
128 return Collection;
129 }
130 ]);
131
132})();