/
SymbolCheck.js
214 lines (207 loc) · 7.93 KB
/
SymbolCheck.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/* Copyright (c) 2015-2016 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/>.
*/
/**
* A utility class providing methods to check for symbols of OpenLayers we
* depend upon.
*
* This class can be mixed into classes to check if the dependencies to external
* symbols are fulfilled. An example:
*
* Ext.define('MyNewClass.DependingOnOpenLayersClasses', {
* mixins: ['GeoExt.mixin.SymbolCheck'],
* // the contents of the `symbols` property will be checked
* symbols: [
* 'ol.Map', // checking a class
* 'ol.View.prototype.constrainResolution', // an instance method
* 'ol.control.ScaleLine#getUnits', // other way for instance method
* 'ol.color.asArray', // one way to reference a static method
* 'ol.color::asString' // other way to reference a static method
* ]
* // … your configuration and methods …
* });
*
* Since this sort of checking usually only makes sense in debug mode, you can
* additionally wrap the `symbols`-configuration in these `<debug>`-line
* comments:
*
* Ext.define('MyNewClass.DependingOnOpenLayersClasses', {
* mixins: ['GeoExt.mixin.SymbolCheck'],
* // <debug>
* symbols: []
* // </debug>
* });
*
* This means that the array of symbols is not defined in production builds
* as the wrapped lines are simply removed from the final JavaScript.
*
* If one of the symbols cannot be found, a warning will be printed to the
* developer console (via `Ext.log.warn`, which will only print in a debug
* build):
*
* [W] The class "MyNewClass.DependingOnOpenLayersClasses" depends on the
* external symbol "ol.color.notExisting", which does not seem to exist.
*
* @class GeoExt.mixin.SymbolCheck
*/
Ext.define('GeoExt.mixin.SymbolCheck', {
extend: 'Ext.Mixin',
statics: {
/**
* An object that we will use to store already looked up references in.
*
* The key will be a symbol (after it has been normalized by the
* method #normalizeSymbol), and the value will be a boolean indicating
* if the symbol was found to be defined when it was checked.
*
* @private
*/
_checked: {
// will be filled while we are checking stuff for existance
},
/**
* Checks whether the required symbols of the given class are defined
* in the global context. Will log to the console if a symbol cannot be
* found.
*
* @param {Ext.Base} cls An ext class defining a property `symbols` that
* that this method will check.
*/
check: function(cls) {
// <debug>
var me = this;
var proto = cls.prototype;
var olSymbols = proto && proto.symbols;
var clsName = proto && proto['$className'];
if (!olSymbols) {
return;
}
Ext.each(olSymbols, function(olSymbol) {
olSymbol = me.normalizeSymbol(olSymbol);
me.checkSymbol(olSymbol, clsName);
});
// </debug>
},
/**
* Normalizes a short form of a symbol to a canonical one we use to
* store the results of the #isDefinedSymbol method. The following two
* normalizations take place:
*
* * A `#` in the symbol is being replaced with `.prototype.` so that
* e.g. the symbol `'ol.Class#methodName'` turns into the symbol
* `'ol.Class.prototype.methodName'`
* * A `::` in the symbol is being replaced with `.` so that
* e.g. the symbol `'ol.Class::staticMethodName'` turns into the
* symbol `'ol.Class.staticMethodName'`
*
* @param {String} symbolStr A string to normalize.
* @return {String} The normalized string.
* @private
*/
normalizeSymbol: (function() {
// <debug>
var hashRegEx = /#/;
var colonRegEx = /::/;
// </debug>
var normalizeFunction = function(symbolStr) {
// <debug>
if (hashRegEx.test(symbolStr)) {
symbolStr = symbolStr.replace(hashRegEx, '.prototype.');
} else if (colonRegEx.test(symbolStr)) {
symbolStr = symbolStr.replace(colonRegEx, '.');
}
return symbolStr;
// </debug>
};
return normalizeFunction;
}()),
/**
* Checks the passed symbolStr and raises a warning if it cannot be
* found.
*
* @param {String} symbolStr A string to check. Usually this string has
* been {@link #normalizeSymbol normalized} already.
* @param {String} [clsName] The optional name of the class that
* requires the passed openlayers symbol.
* @private
*/
checkSymbol: function(symbolStr, clsName) {
// <debug>
var isDefined = this.isDefinedSymbol(symbolStr);
if (!isDefined) {
Ext.log.warn(
'The class "' + (clsName || 'unknown') + '" ' +
'depends on the external symbol "' + symbolStr + '", ' +
'which does not seem to exist.'
);
}
// </debug>
},
/**
* Checks if the passed symbolStr is defined.
*
* @param {String} symbolStr A string to check. Usually this string has
* been {@link #normalizeSymbol normalized} already.
* @return {Boolean} Whether the symbol is defined or not.
* @private
*/
isDefinedSymbol: function(symbolStr) {
// <debug>
var checkedCache = this._checked;
if (Ext.isDefined(checkedCache[symbolStr])) {
return checkedCache[symbolStr];
}
var parts = symbolStr.split('.');
var lastIdx = parts.length - 1;
var curSymbol = Ext.getWin().dom;
var isDefined = false;
var intermediateSymb = '';
Ext.each(parts, function(part, idx) {
if (intermediateSymb !== '') {
intermediateSymb += '.';
}
intermediateSymb += part;
if (curSymbol[part]) {
checkedCache[intermediateSymb] = true;
curSymbol = curSymbol[part];
if (lastIdx === idx) {
isDefined = true;
}
} else {
checkedCache[intermediateSymb] = false;
return false; // break early
}
});
checkedCache[symbolStr] = isDefined;
return isDefined;
// </debug>
}
},
/**
* @property {String[]} symbols The symbols to check.
*/
/**
* Whenever a class mixes in GeoExt.mixin.SymbolCheck, this method will be
* called and it actually runs the checks for all the defined #symbols.
*
* @param {Ext.Class} cls The class that this mixin is mixed into.
* @private
*/
onClassMixedIn: function(cls) {
// <debug>
GeoExt.mixin.SymbolCheck.check(cls);
// </debug>
}
});