Mercurial > pub > ImplabJs
comparison src/djol/DynamicStyle.js @ 8:f0035923ff3e
добавлена библиотека для работы с openlayers 3+
author | cin |
---|---|
date | Mon, 21 Aug 2017 17:47:00 +0300 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
7:9c0943c68a90 | 8:f0035923ff3e |
---|---|
1 define([ "./declare-style", "dojo/_base/declare", "ol", "implab/safe" ], | |
2 | |
3 function(declare, dojoDeclare, ol, safe) { | |
4 return declare([], { | |
5 _cache : null, | |
6 _getKey : null, | |
7 _getZoom : null, | |
8 | |
9 defaultStyle : null, | |
10 | |
11 style : null, | |
12 | |
13 styleFunction : null, | |
14 | |
15 filter : null, | |
16 | |
17 constructor : function(opts) { | |
18 if (opts) | |
19 dojoDeclare.safeMixin(this, opts); | |
20 | |
21 this._cache = {}; | |
22 | |
23 if (this.zoom) { | |
24 if (this.zoom instanceof Function) { | |
25 this._getZoom = this.zoom; | |
26 } else { | |
27 var levels = [], max = "max"; | |
28 for ( var p in this.zoom) { | |
29 if (safe.isNumber(this.zoom[p])) | |
30 levels.push({ | |
31 name : p, | |
32 zoom : Number(this.zoom[p]) | |
33 }); | |
34 else if (this.zoom[p] == "max") | |
35 max = p; | |
36 } | |
37 | |
38 levels.sort(function(x, y) { | |
39 return x.zoom - y.zoom; | |
40 }); | |
41 | |
42 this.zoomMax = max; | |
43 this.zoomLevels = levels; | |
44 | |
45 this._getZoom = function(z) { | |
46 for (var i = 0; i < levels.length; i++) { | |
47 if (z <= levels[i].zoom) | |
48 return levels[i].name; | |
49 } | |
50 return max; | |
51 }; | |
52 } | |
53 } else { | |
54 this._getZoom = function(z) { | |
55 return "max"; | |
56 }; | |
57 } | |
58 | |
59 if (this.key) { | |
60 if (this.key instanceof Function) { | |
61 this._getKey = this.key; | |
62 } else if (typeof this.key === "string") { | |
63 this._getKey = function(ft) { | |
64 return ft.get(this.key); | |
65 }; | |
66 } else { | |
67 this._getKey = function(ft, z) { | |
68 var k = this.key[z]; | |
69 if (k instanceof Function) | |
70 return k.call(this, ft, z); | |
71 else if (typeof k == "string") | |
72 return ft.get(k); | |
73 else | |
74 return this.defaultStyle; | |
75 }; | |
76 } | |
77 } else { | |
78 this._getKey = function() { | |
79 return this.defaultStyle; | |
80 }; | |
81 } | |
82 | |
83 if (this.style) { | |
84 if (this.style instanceof Function) { | |
85 this._style = this.style; | |
86 } else { | |
87 this._style = function(ft, res, key, zoom) { | |
88 var s = this.style[zoom]; | |
89 return s && (s instanceof Function) ? s.apply( | |
90 this, | |
91 arguments) : s; | |
92 }; | |
93 } | |
94 } | |
95 }, | |
96 | |
97 getFeatureStyle : function(ft, res) { | |
98 safe.argumentNotNull(ft, "ft"); | |
99 | |
100 if (this.filter && this.filter(ft) === false) | |
101 return null; | |
102 | |
103 var z = this._getZoom(res); | |
104 var k = this._getKey(ft, z); | |
105 | |
106 var cid = [ k, z ].join('-'); | |
107 | |
108 var style = this._cache[cid]; | |
109 if (!style) { | |
110 style = this._style ? this._style(ft, res, k, z) : null; | |
111 this._cache[cid] = style; | |
112 } | |
113 | |
114 | |
115 return safe.isNull(style) || style instanceof Array ? style : [ style ]; | |
116 } | |
117 | |
118 }); | |
119 }); |