/
OlObject.js
170 lines (146 loc) · 4.45 KB
/
OlObject.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/* Copyright (c) 2015-present The Open Source Geospatial Foundation
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* Simple model that maps an ol.Object to an Ext.data.Model.
*
* @class GeoExt.data.model.OlObject
*/
Ext.define('GeoExt.data.model.OlObject', {
extend: 'GeoExt.data.model.Base',
mixins: [
'GeoExt.mixin.SymbolCheck'
],
// <debug>
symbols: [
'ol',
'ol.Object',
'ol.Object#on',
'ol.Object#get',
'ol.Object#set'
],
// </debug>
inheritableStatics: {
/**
* Gets a reference to an ol constructor function.
*
* @param {String} str Description of the form `"ol.layer.Base"`.
* @return {Function} the ol constructor.
* @static
*/
getOlCLassRef: function(str) {
var ref = ol;
var members;
if (Ext.isString(str)) {
members = str.split('.');
// shift if description contains namespace
if (Ext.Array.indexOf(members, 'ol') === 0) {
members.shift();
}
// traverse namespace to ref
Ext.Array.each(members, function(member) {
ref = ref[member];
});
}
return ref;
}
},
/**
* String description of the reference path to the wrapped ol class.
*
* @property {String}
*/
olClass: 'ol.Object',
/**
* The underlying ol.Object.
*
* @property {ol.Object}
*/
olObject: null,
proxy: {
type: 'memory',
reader: 'json'
},
/**
* @inheritdoc
*/
constructor: function(data) {
var me = this;
var statics = this.statics();
var OlClass = statics.getOlCLassRef(this.olClass);
data = data || {};
// init ol object if plain data is handed over
if (!(data instanceof OlClass)) {
data = new OlClass(data);
}
me.olObject = data;
// init record with properties of underlying ol object
me.callParent([this.olObject.getProperties()]);
me.onPropertychange = me.onPropertychange.bind(me);
me.olObject.on('propertychange', me.onPropertychange);
},
/**
* Listener to propertychange events of the underlying `ol.Object`. All
* changes on the object will be forwarded to the Ext.data.Model.
*
* @param {ol.ObjectEvent} evt The `ol.ObjectEvent` we receive as handler.
* @private
*/
onPropertychange: function(evt) {
var target = evt.target;
var key = evt.key;
if (!this.__updating) {
this.set(key, target.get(key));
}
},
/**
* Overridden to forward changes to the underlying `ol.Object`. All changes
* on the `Ext.data.Model` properties will be set on the `ol.Object` as
* well.
*
* @param {String|Object} key The key to set.
* @param {Object} newValue The value to set.
*
* @inheritdoc
*/
set: function(key, newValue) {
var o = {};
this.callParent(arguments);
// forward changes to ol object
this.__updating = true;
// wrap simple set operations into an object
if (Ext.isString(key)) {
o[key] = newValue;
} else {
o = key;
}
// iterate over object setting changes to ol.Object
Ext.Object.each(o, function(k, v) {
if (this.olObject.get(k) !== v) {
this.olObject.set(k, v);
}
}, this);
this.__updating = false;
},
/**
* Overridden to unregister all added event listeners on the ol.Object.
*
* @inheritdoc
*/
destroy: function() {
this.olObject.un('propertychange', this.onPropertychange);
this.callParent(arguments);
}
});