Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 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 Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame] | 25 | var Model; |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 26 | |
| 27 | function Collection(models, options) { |
| 28 | |
Steven Burrows | 448468c | 2017-04-13 16:09:30 -0700 | [diff] [blame] | 29 | var opts = options || {}; |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 30 | |
| 31 | this.models = []; |
| 32 | this._reset(); |
| 33 | |
Steven Burrows | 25fa1dc | 2017-04-17 18:29:22 -0400 | [diff] [blame] | 34 | this.initialize.apply(this, arguments); |
| 35 | |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 36 | if (opts.comparator) { |
| 37 | this.comparator = opts.comparator; |
| 38 | } |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 39 | |
| 40 | if (models) { |
| 41 | this.add(models); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | Collection.prototype = { |
| 46 | model: Model, |
Steven Burrows | 25fa1dc | 2017-04-17 18:29:22 -0400 | [diff] [blame] | 47 | initialize: function () {}, |
Steven Burrows | b15a394 | 2017-01-17 17:25:04 +0000 | [diff] [blame] | 48 | addModel: function (data) { |
Steven Burrows | 3d8d933 | 2017-03-30 15:05:52 +0100 | [diff] [blame] | 49 | if (Object.getPrototypeOf(data) !== Object.prototype) { |
| 50 | this.models.push(data); |
Steven Burrows | ca1a39c | 2017-05-08 17:31:08 -0400 | [diff] [blame] | 51 | data.collection = this; |
Steven Burrows | 3d8d933 | 2017-03-30 15:05:52 +0100 | [diff] [blame] | 52 | this._byId[data.get('id')] = data; |
| 53 | return data; |
| 54 | } |
| 55 | |
Steven Burrows | 2b29ca4 | 2017-05-12 10:58:05 -0400 | [diff] [blame] | 56 | if (this._byId[data.id]) { |
| 57 | return this._byId[data.id]; |
| 58 | } |
| 59 | |
Steven Burrows | b15a394 | 2017-01-17 17:25:04 +0000 | [diff] [blame] | 60 | var CollectionModel = this.model; |
Steven Burrows | 68d6f95 | 2017-03-10 13:53:35 +0000 | [diff] [blame] | 61 | var model = new CollectionModel(data, this); |
Steven Burrows | ca1a39c | 2017-05-08 17:31:08 -0400 | [diff] [blame] | 62 | model.collection = this; |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 63 | |
Steven Burrows | b15a394 | 2017-01-17 17:25:04 +0000 | [diff] [blame] | 64 | this.models.push(model); |
| 65 | this._byId[data.id] = model; |
| 66 | |
| 67 | return model; |
| 68 | }, |
| 69 | add: function (data) { |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 70 | var _this = this; |
| 71 | |
| 72 | if (angular.isArray(data)) { |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 73 | data.forEach(function (d) { |
Steven Burrows | b15a394 | 2017-01-17 17:25:04 +0000 | [diff] [blame] | 74 | _this.addModel(d); |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 75 | }); |
Steven Burrows | b15a394 | 2017-01-17 17:25:04 +0000 | [diff] [blame] | 76 | } else { |
| 77 | return this.addModel(data); |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 78 | } |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 79 | }, |
Steven Burrows | 42eb9e2 | 2017-02-06 14:20:24 +0000 | [diff] [blame] | 80 | remove: function (model) { |
| 81 | var index = _.indexOf(this.models, model); |
| 82 | this.models.splice(index, 1); |
| 83 | }, |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 84 | get: function (id) { |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 85 | |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 86 | if (!id) { |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 87 | return null; |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 88 | } |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 89 | |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 90 | 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 Burrows | e2d77d6 | 2016-10-21 12:35:58 -0500 | [diff] [blame] | 102 | filter: function (comparator) { |
| 103 | return _.filter(this.models, comparator); |
| 104 | }, |
Steven Burrows | ca1a39c | 2017-05-08 17:31:08 -0400 | [diff] [blame] | 105 | empty: function () { |
| 106 | _.map(this.models, function (m) { |
| 107 | m.remove(); |
| 108 | }); |
| 109 | this._reset(); |
| 110 | }, |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 111 | _reset: function () { |
| 112 | this._byId = []; |
| 113 | this.models = []; |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 114 | }, |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 115 | toJSON: function (options) { |
| 116 | return this.models.map(function (model) { |
| 117 | return model.toJSON(options); |
| 118 | }); |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame^] | 119 | }, |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 120 | }; |
| 121 | |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 122 | angular.module('ovTopo2') |
| 123 | .factory('Topo2Collection', |
Steven Burrows | 86af435 | 2016-11-16 18:19:12 -0600 | [diff] [blame] | 124 | ['Topo2Model', 'FnService', |
| 125 | function (_Model_, fn) { |
Steven Burrows | 86af435 | 2016-11-16 18:19:12 -0600 | [diff] [blame] | 126 | Collection.extend = fn.extend; |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 127 | Model = _Model_; |
| 128 | return Collection; |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame^] | 129 | }, |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 130 | ]); |
| 131 | |
| 132 | })(); |