blob: 1166fb8e4f8871b1eb176fb7885e266a110c3d8b [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 Burrowsb15a3942017-01-17 17:25:04 +000056 var CollectionModel = this.model;
Steven Burrows68d6f952017-03-10 13:53:35 +000057 var model = new CollectionModel(data, this);
Steven Burrowsca1a39c2017-05-08 17:31:08 -040058 model.collection = this;
Steven Burrows57e24e92016-08-04 18:38:24 +010059
Steven Burrowsb15a3942017-01-17 17:25:04 +000060 this.models.push(model);
61 this._byId[data.id] = model;
62
63 return model;
64 },
65 add: function (data) {
Steven Burrows57e24e92016-08-04 18:38:24 +010066 var _this = this;
67
68 if (angular.isArray(data)) {
Steven Burrows57e24e92016-08-04 18:38:24 +010069 data.forEach(function (d) {
Steven Burrowsb15a3942017-01-17 17:25:04 +000070 _this.addModel(d);
Steven Burrows57e24e92016-08-04 18:38:24 +010071 });
Steven Burrowsb15a3942017-01-17 17:25:04 +000072 } else {
73 return this.addModel(data);
Steven Burrows57e24e92016-08-04 18:38:24 +010074 }
Steven Burrows57e24e92016-08-04 18:38:24 +010075 },
Steven Burrows42eb9e22017-02-06 14:20:24 +000076 remove: function (model) {
77 var index = _.indexOf(this.models, model);
78 this.models.splice(index, 1);
79 },
Steven Burrows57e24e92016-08-04 18:38:24 +010080 get: function (id) {
Steven Burrowsdfa52b02016-09-02 13:50:43 +010081
Steven Burrows57e24e92016-08-04 18:38:24 +010082 if (!id) {
Steven Burrowsdfa52b02016-09-02 13:50:43 +010083 return null;
Steven Burrows57e24e92016-08-04 18:38:24 +010084 }
Steven Burrowsdfa52b02016-09-02 13:50:43 +010085
Steven Burrows57e24e92016-08-04 18:38:24 +010086 return this._byId[id] || null;
87 },
88 sort: function () {
89
90 var comparator = this.comparator;
91
92 // Check if function
93 comparator = comparator.bind(this);
94 this.models.sort(comparator);
95
96 return this;
97 },
Steven Burrowse2d77d62016-10-21 12:35:58 -050098 filter: function (comparator) {
99 return _.filter(this.models, comparator);
100 },
Steven Burrowsca1a39c2017-05-08 17:31:08 -0400101 empty: function () {
102 _.map(this.models, function (m) {
103 m.remove();
104 });
105 this._reset();
106 },
Steven Burrows57e24e92016-08-04 18:38:24 +0100107 _reset: function () {
108 this._byId = [];
109 this.models = [];
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100110 },
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100111 toJSON: function (options) {
112 return this.models.map(function (model) {
113 return model.toJSON(options);
114 });
115 }
Steven Burrows57e24e92016-08-04 18:38:24 +0100116 };
117
Steven Burrows57e24e92016-08-04 18:38:24 +0100118 angular.module('ovTopo2')
119 .factory('Topo2Collection',
Steven Burrows86af4352016-11-16 18:19:12 -0600120 ['Topo2Model', 'FnService',
121 function (_Model_, fn) {
Steven Burrows86af4352016-11-16 18:19:12 -0600122 Collection.extend = fn.extend;
Steven Burrows57e24e92016-08-04 18:38:24 +0100123 Model = _Model_;
124 return Collection;
125 }
126 ]);
127
128})();