Skip to content

Commit c04056a

Browse files
committed
fixed bug that causes layer with no paint property to crash app, also added default color support for added layers - closes #46
1 parent a654ba0 commit c04056a

File tree

6 files changed

+152
-12
lines changed

6 files changed

+152
-12
lines changed

src/config/layer.json

Lines changed: 80 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,46 +3,117 @@
33
"background":{
44
"label":"background",
55
"icon":"public",
6-
"colorProperty":"background-color"
6+
"colorProperty":"background-color",
7+
"default":{
8+
"layout":{
9+
"visibility":"visible"
10+
},
11+
"paint":{
12+
"background-color":"#37474F"
13+
}
14+
}
715
},
816
"circle":{
917
"label":"circle",
1018
"icon":"grain",
11-
"colorProperty":"circle-color"
19+
"colorProperty":"circle-color",
20+
"default":{
21+
"layout":{
22+
"visibility":"visible"
23+
},
24+
"paint":{
25+
"circle-color":{"color":"light"},
26+
"circle-radius":3
27+
}
28+
}
1229
},
1330
"fill":{
1431
"label":"fill",
1532
"icon":"stop",
16-
"colorProperty":"fill-color"
33+
"colorProperty":"fill-color",
34+
"default":{
35+
"layout":{
36+
"visibility":"visible"
37+
},
38+
"paint":{
39+
"fill-color":{"color":"light"}
40+
}
41+
}
1742
},
1843
"fill-extrusion":{
1944
"label":"fill-extrusion",
2045
"icon":"filter_none",
21-
"colorProperty":"fill-extrusion-color"
46+
"colorProperty":"fill-extrusion-color",
47+
"default":{
48+
"layout":{
49+
"visibility":"visible"
50+
},
51+
"paint":{
52+
"fill-extrusion-color":{"color":"light"}
53+
}
54+
}
2255
},
2356
"heatmap":{
2457
"label":"heatmap",
2558
"icon":"bubble_chart",
26-
"colorProperty":"heatmap-color"
59+
"colorProperty":"heatmap-color",
60+
"default":{
61+
"layout":{
62+
"visibility":"visible"
63+
},
64+
"paint":{
65+
"heatmap-color":{"color":"light"}
66+
}
67+
}
2768
},
2869
"hillshade":{
2970
"label":"hillshade",
3071
"icon":"landscape",
31-
"colorProperty":["hillshade-shadow-color","hillshade-highlight-color","hillshade-accent-color"]
72+
"colorProperty":["hillshade-shadow-color","hillshade-highlight-color","hillshade-accent-color"],
73+
"default":{
74+
"layout":{
75+
"visibility":"visible"
76+
},
77+
"paint":{
78+
"hillshade-shadow-color":{"color":"light"}
79+
}
80+
}
3281
},
3382
"line":{
3483
"label":"line",
3584
"icon":"show_chart",
36-
"colorProperty":"line-color"
85+
"colorProperty":"line-color",
86+
"default":{
87+
"layout":{
88+
"visibility":"visible"
89+
},
90+
"paint":{
91+
"line-color":{"color":"light"}
92+
}
93+
}
3794
},
3895
"raster":{
3996
"label":"raster",
40-
"icon":"image"
97+
"icon":"image",
98+
"default":{
99+
"layout":{
100+
"visibility":"visible"
101+
}
102+
}
41103
},
42104
"symbol":{
43105
"label":"symbol",
44106
"icon":"text_format",
45-
"colorProperty":["text-color","icon-color"]
107+
"colorProperty":["text-color","icon-color"],
108+
"default":{
109+
"layout":{
110+
"visibility":"visible",
111+
"text-font":["Open Sans Regular","Arial Unicode MS Regular"]
112+
},
113+
"paint":{
114+
"text-color":{"color":"light"}
115+
}
116+
}
46117
}
47118
},
48119

src/model/Mlayer.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ import Store from '../Store';
33
import Mstyle from './Mstyle';
44
//import Model from './Model';
55

6+
import MaterialColor from '../utility/MaterialColor';
7+
8+
import config from '../config/layer.json';
9+
610
const types = [
711
{name:'fill',value:'fill'},
812
{name:'line',value:'line'},
@@ -22,6 +26,9 @@ export default {
2226
return new Promise((resolve,reject)=>{
2327
if (!layer.id) return reject('no layerId');
2428

29+
// add default properties based on layer type
30+
layer = this.addDefaultProps(layer);
31+
2532
Store.dispatch({
2633
type:'LAYER_ADD',
2734
payload:layer
@@ -33,6 +40,28 @@ export default {
3340

3441
},
3542

43+
addDefaultProps:function(layer){
44+
if (!config.types || !config.types[layer.type] || !config.types[layer.type].default) return layer;
45+
46+
for (let i in config.types[layer.type].default){
47+
if (!layer[i]) layer[i] = config.types[layer.type].default[i];
48+
}
49+
50+
return this.addDefaultColors(layer,layer);
51+
},
52+
53+
addDefaultColors:function(layer,part){
54+
for (let i in part){
55+
if (part[i] && part[i].color && part[i].color === 'light'){
56+
part[i] = MaterialColor.getLight(layer.id);
57+
}
58+
if (typeof part[i] === 'object' && part[i] !== null){
59+
part[i] = this.addDefaultColors(layer,part[i]);
60+
}
61+
}
62+
return part;
63+
},
64+
3665
set:function(layerId,layer){
3766
return new Promise((resolve,reject)=>{
3867
if (!layerId) return reject('no layerId');

src/utility/MaterialColor.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,37 @@ export default {
5252
return colors[color][shade];
5353
},
5454

55+
getLight:(key)=>{
56+
57+
let patternColor;
58+
59+
// look for patterns
60+
for (const i in PATTERNS){
61+
if (key.indexOf(i) !== -1){
62+
patternColor = PATTERNS[i];
63+
//console.log('pattern color:',patternColor);
64+
}
65+
}
66+
67+
const code = hashCode(key);
68+
const ratio = (code+CODE_MAX)/CODE_RANGE;
69+
70+
let a=[];
71+
while(a.length<3){
72+
a.push(ratio%Math.pow(0.1,a.length)*Math.pow(10,a.length));
73+
}
74+
75+
const colorInd = Math.floor(a[0]*COLOR.length);
76+
const color = patternColor || COLOR[colorInd];
77+
78+
const shadeInd = Math.floor(a[2]*SHADE.length);
79+
const shade = SHADE[shadeInd];
80+
81+
//console.log('code:',code,ratio,color,shade,shadeInd,over);
82+
83+
return colors[color][shade];
84+
},
85+
5586
getAll:()=>{
5687
return colors;
5788
}

src/view/Vlayer/VlayerAdd.jsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,14 @@ export default class VlayerAdd extends React.Component {
3636
//return;
3737
//}
3838

39-
Mlayer.add(this.state).then((layer)=>{
39+
const rec = {
40+
id:this.state.id,
41+
source:this.state.source,
42+
type:this.state.type,
43+
'source-layer':this.state['source-layer']
44+
};
45+
46+
Mlayer.add(rec).then((layer)=>{
4047
console.log('added:',layer);
4148
handle.route('layer/'+layer.id);
4249
}).catch((e)=>{

src/view/Vlayer/VlayerGroup/VlayerGroupLayout.jsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React from 'react';
22
import PropTypes from 'prop-types';
3+
import {Map, List} from 'immutable';
34

45
import styleSpec from '../../../vendor/style-spec/style-spec';
56

@@ -47,7 +48,7 @@ export default class VlayerGroupLayout extends React.Component {
4748
//console.log('layout error:',error);
4849

4950
return <div className="p-1">
50-
{layerGroup && layerGroup.keySeq().map((key)=>{
51+
{layerGroup && Map.isMap(layerGroup) && layerGroup.keySeq().map((key)=>{
5152
let name = group+'.'+key;
5253

5354
return <Vproperty key={name} property={{

src/view/Vlayer/VlayerGroup/VlayerGroupPaint.jsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React from 'react';
22
import PropTypes from 'prop-types';
3+
import {Map, List} from 'immutable';
34

45
import styleSpec from '../../../vendor/style-spec/style-spec';
56

@@ -47,7 +48,7 @@ export default class VlayerGroupPaint extends React.Component {
4748
//console.log('paint error:',error);
4849

4950
return <div className="p-1">
50-
{layerGroup.keySeq().map((key)=>{
51+
{layerGroup && Map.isMap(layerGroup) && layerGroup.keySeq().map((key)=>{
5152
let name = group+'.'+key;
5253

5354
return <Vproperty key={name} property={{

0 commit comments

Comments
 (0)