blob: 11953e1f4f48e8114a37fcab88bed0b436772fe0 [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
25 var Model;
26
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,
45 add: function (data) {
46
47 var _this = this;
48
49 if (angular.isArray(data)) {
50
51 data.forEach(function (d) {
52
Steven Burrowsdfa52b02016-09-02 13:50:43 +010053 var CollectionModel = _this.model;
54 var model = new CollectionModel(d);
Steven Burrows57e24e92016-08-04 18:38:24 +010055 model.collection = _this;
56
57 _this.models.push(model);
58 _this._byId[d.id] = model;
59 });
60 }
Steven Burrows57e24e92016-08-04 18:38:24 +010061 },
62 get: function (id) {
Steven Burrowsdfa52b02016-09-02 13:50:43 +010063
Steven Burrows57e24e92016-08-04 18:38:24 +010064 if (!id) {
Steven Burrowsdfa52b02016-09-02 13:50:43 +010065 return null;
Steven Burrows57e24e92016-08-04 18:38:24 +010066 }
Steven Burrowsdfa52b02016-09-02 13:50:43 +010067
Steven Burrows57e24e92016-08-04 18:38:24 +010068 return this._byId[id] || null;
69 },
70 sort: function () {
71
72 var comparator = this.comparator;
73
74 // Check if function
75 comparator = comparator.bind(this);
76 this.models.sort(comparator);
77
78 return this;
79 },
Steven Burrowse2d77d62016-10-21 12:35:58 -050080 filter: function (comparator) {
81 return _.filter(this.models, comparator);
82 },
Steven Burrows57e24e92016-08-04 18:38:24 +010083 _reset: function () {
84 this._byId = [];
85 this.models = [];
Steven Burrowsec1f45c2016-08-08 16:14:41 +010086 },
Steven Burrowsdfa52b02016-09-02 13:50:43 +010087 toJSON: function (options) {
88 return this.models.map(function (model) {
89 return model.toJSON(options);
90 });
91 }
Steven Burrows57e24e92016-08-04 18:38:24 +010092 };
93
94 Collection.extend = function (protoProps, staticProps) {
95
96 var parent = this;
97 var child;
98
99 child = function () {
100 return parent.apply(this, arguments);
101 };
102
103 angular.extend(child, parent, staticProps);
104
105 // Set the prototype chain to inherit from `parent`, without calling
106 // `parent`'s constructor function and add the prototype properties.
107 child.prototype = angular.extend({}, parent.prototype, protoProps);
108 child.prototype.constructor = child;
109
110 // Set a convenience property in case the parent's prototype is needed
111 // later.
112 child.__super__ = parent.prototype;
113
114 return child;
115 };
116
117 angular.module('ovTopo2')
118 .factory('Topo2Collection',
119 ['Topo2Model',
120 function (_Model_) {
121
122 Model = _Model_;
123 return Collection;
124 }
125 ]);
126
127})();