blob: 3116a6f68e6f8811c59ef4abe6d57a9c1d95483d [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
29 options || (options = {});
30
31 this.models = [];
32 this._reset();
33
34 if (options.comparator !== void 0) this.comparator = options.comparator;
35
36 if (models) {
37 this.add(models);
38 }
39 }
40
41 Collection.prototype = {
42 model: Model,
43 add: function (data) {
44
45 var _this = this;
46
47 if (angular.isArray(data)) {
48
49 data.forEach(function (d) {
50
51 var model = new _this.model(d);
52 model.collection = _this;
53
54 _this.models.push(model);
55 _this._byId[d.id] = model;
56 });
57 }
58
59// this.sort();
60 },
61 get: function (id) {
62 if (!id) {
63 return void 0;
64 }
65 return this._byId[id] || null;
66 },
67 sort: function () {
68
69 var comparator = this.comparator;
70
71 // Check if function
72 comparator = comparator.bind(this);
73 this.models.sort(comparator);
74
75 return this;
76 },
77 _reset: function () {
78 this._byId = [];
79 this.models = [];
80 }
81 };
82
83 Collection.extend = function (protoProps, staticProps) {
84
85 var parent = this;
86 var child;
87
88 child = function () {
89 return parent.apply(this, arguments);
90 };
91
92 angular.extend(child, parent, staticProps);
93
94 // Set the prototype chain to inherit from `parent`, without calling
95 // `parent`'s constructor function and add the prototype properties.
96 child.prototype = angular.extend({}, parent.prototype, protoProps);
97 child.prototype.constructor = child;
98
99 // Set a convenience property in case the parent's prototype is needed
100 // later.
101 child.__super__ = parent.prototype;
102
103 return child;
104 };
105
106 angular.module('ovTopo2')
107 .factory('Topo2Collection',
108 ['Topo2Model',
109 function (_Model_) {
110
111 Model = _Model_;
112 return Collection;
113 }
114 ]);
115
116})();