66* LICENSE file in the root directory of this source tree.
77*/
88
9-
109'use strict' ;
1110
1211var Lib = require ( '../../lib' ) ;
1312var convertTextOpts = require ( './convert_text_opts' ) ;
1413
15-
1614function MapboxLayer ( mapbox , index ) {
1715 this . mapbox = mapbox ;
1816 this . map = mapbox . map ;
@@ -36,32 +34,26 @@ var proto = MapboxLayer.prototype;
3634
3735proto . update = function update ( opts ) {
3836 if ( ! this . visible ) {
39-
4037 // IMPORTANT: must create source before layer to not cause errors
4138 this . updateSource ( opts ) ;
4239 this . updateLayer ( opts ) ;
43- }
44- else if ( this . needsNewSource ( opts ) ) {
45-
40+ } else if ( this . needsNewSource ( opts ) ) {
4641 // IMPORTANT: must delete layer before source to not cause errors
4742 this . updateLayer ( opts ) ;
4843 this . updateSource ( opts ) ;
49- }
50- else if ( this . needsNewLayer ( opts ) ) {
44+ } else if ( this . needsNewLayer ( opts ) ) {
5145 this . updateLayer ( opts ) ;
46+ } else {
47+ this . updateStyle ( opts ) ;
5248 }
5349
54- this . updateStyle ( opts ) ;
55-
5650 this . visible = isVisible ( opts ) ;
5751} ;
5852
5953proto . needsNewSource = function ( opts ) {
60-
6154 // for some reason changing layer to 'fill' or 'symbol'
6255 // w/o changing the source throws an exception in mapbox-gl 0.18 ;
6356 // stay safe and make new source on type changes
64-
6557 return (
6658 this . sourceType !== opts . sourcetype ||
6759 this . source !== opts . source ||
@@ -93,37 +85,34 @@ proto.updateSource = function(opts) {
9385
9486proto . updateLayer = function ( opts ) {
9587 var map = this . map ;
88+ var convertedOpts = convertOpts ( opts ) ;
9689
9790 if ( map . getLayer ( this . idLayer ) ) map . removeLayer ( this . idLayer ) ;
9891
9992 this . layerType = opts . type ;
10093
101- if ( ! isVisible ( opts ) ) return ;
102-
103- map . addLayer ( {
104- id : this . idLayer ,
105- source : this . idSource ,
106- 'source-layer' : opts . sourcelayer || '' ,
107- type : opts . type
108- } , opts . below ) ;
109-
110- // the only way to make a layer invisible is to remove it
111- var layoutOpts = { visibility : 'visible' } ;
112- this . mapbox . setOptions ( this . idLayer , 'setLayoutProperty' , layoutOpts ) ;
94+ if ( isVisible ( opts ) ) {
95+ map . addLayer ( {
96+ id : this . idLayer ,
97+ source : this . idSource ,
98+ 'source-layer' : opts . sourcelayer || '' ,
99+ type : opts . type ,
100+ layout : convertedOpts . layout ,
101+ paint : convertedOpts . paint
102+ } , opts . below ) ;
103+ }
113104} ;
114105
115106proto . updateStyle = function ( opts ) {
116- var convertedOpts = convertOpts ( opts ) ;
117-
118107 if ( isVisible ( opts ) ) {
108+ var convertedOpts = convertOpts ( opts ) ;
119109 this . mapbox . setOptions ( this . idLayer , 'setLayoutProperty' , convertedOpts . layout ) ;
120110 this . mapbox . setOptions ( this . idLayer , 'setPaintProperty' , convertedOpts . paint ) ;
121111 }
122112} ;
123113
124114proto . dispose = function dispose ( ) {
125115 var map = this . map ;
126-
127116 map . removeLayer ( this . idLayer ) ;
128117 map . removeSource ( this . idSource ) ;
129118} ;
@@ -198,19 +187,18 @@ function convertOpts(opts) {
198187}
199188
200189function convertSourceOpts ( opts ) {
201- var sourceType = opts . sourcetype ,
202- source = opts . source ,
203- sourceOpts = { type : sourceType } ,
204- isSourceAString = ( typeof source === 'string' ) ,
205- field ;
206-
207- if ( sourceType === 'geojson' ) field = 'data' ;
208- else if ( sourceType === 'vector' ) {
209- field = isSourceAString ? 'url' : 'tiles' ;
190+ var sourceType = opts . sourcetype ;
191+ var source = opts . source ;
192+ var sourceOpts = { type : sourceType } ;
193+ var field ;
194+
195+ if ( sourceType === 'geojson' ) {
196+ field = 'data' ;
197+ } else if ( sourceType === 'vector' ) {
198+ field = typeof source === 'string' ? 'url' : 'tiles' ;
210199 }
211200
212201 sourceOpts [ field ] = source ;
213-
214202 return sourceOpts ;
215203}
216204
0 commit comments