Skip to content

Commit

Permalink
Remove paint class support
Browse files Browse the repository at this point in the history
  • Loading branch information
Anand Thakker authored and jfirebaugh committed Oct 5, 2017
1 parent ed85ac5 commit e4201fc
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 350 deletions.
29 changes: 14 additions & 15 deletions src/style/style.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ class Style extends Evented {
this._loaded = true;
this.stylesheet = json;

this.updateClasses();
this.updatePaintProperties();

for (const id in json.sources) {
this.addSource(id, json.sources[id], {validate: false});
Expand Down Expand Up @@ -274,10 +274,9 @@ class Style extends Evented {
return ids.map((id) => this._layers[id].serialize());
}

_applyClasses(classes?: Array<string>, options: ?{}) {
_applyPaintPropertyUpdates(options: ?{}) {
if (!this._loaded) return;

classes = classes || [];
options = options || {transition: true};
const transition = this.stylesheet.transition || {};

Expand All @@ -288,10 +287,10 @@ class Style extends Evented {
const props = this._updatedPaintProps[id];

if (this._updatedAllPaintProps || props.all) {
layer.updatePaintTransitions(classes, options, transition, this.animationLoop, this.zoomHistory);
layer.updatePaintTransitions(options, transition, this.animationLoop, this.zoomHistory);
} else {
for (const paintName in props) {
this._layers[id].updatePaintTransition(paintName, classes, options, transition, this.animationLoop, this.zoomHistory);
this._layers[id].updatePaintTransition(paintName, options, transition, this.animationLoop, this.zoomHistory);
}
}
}
Expand Down Expand Up @@ -360,7 +359,7 @@ class Style extends Evented {
/**
* Apply queued style updates in a batch
*/
update(classes: Array<string>, options: ?{}) {
update(options: ?{}) {
if (!this._changed) return;

const updatedIds = Object.keys(this._updatedLayers);
Expand All @@ -379,7 +378,7 @@ class Style extends Evented {
}
}

this._applyClasses(classes, options);
this._applyPaintPropertyUpdates(options);
this._resetUpdates();

this.fire('data', {dataType: 'style'});
Expand Down Expand Up @@ -602,7 +601,7 @@ class Style extends Evented {
this._updatedSymbolOrder = true;
}

this.updateClasses(id);
this.updatePaintProperties(id);
}

/**
Expand Down Expand Up @@ -775,7 +774,7 @@ class Style extends Evented {
return this.getLayer(layer).getLayoutProperty(name);
}

setPaintProperty(layerId: string, name: string, value: any, klass?: string) {
setPaintProperty(layerId: string, name: string, value: any) {
this._checkLoaded();

const layer = this.getLayer(layerId);
Expand All @@ -789,10 +788,10 @@ class Style extends Evented {
return;
}

if (util.deepEqual(layer.getPaintProperty(name, klass), value)) return;
if (util.deepEqual(layer.getPaintProperty(name), value)) return;

const wasFeatureConstant = layer.isPaintValueFeatureConstant(name);
layer.setPaintProperty(name, value, klass);
layer.setPaintProperty(name, value);

const isFeatureConstant = !(
value &&
Expand All @@ -805,19 +804,19 @@ class Style extends Evented {
this._updateLayer(layer);
}

this.updateClasses(layerId, name);
this.updatePaintProperties(layerId, name);
}

getPaintProperty(layer: string, name: string, klass?: string) {
return this.getLayer(layer).getPaintProperty(name, klass);
getPaintProperty(layer: string, name: string) {
return this.getLayer(layer).getPaintProperty(name);
}

getTransition() {
return util.extend({ duration: 300, delay: 0 },
this.stylesheet && this.stylesheet.transition);
}

updateClasses(layerId?: string, paintName?: string) {
updatePaintProperties(layerId?: string, paintName?: string) {
this._changed = true;
if (!layerId) {
this._updatedAllPaintProps = true;
Expand Down
84 changes: 27 additions & 57 deletions src/style/style_layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,23 +70,17 @@ class StyleLayer extends Evented {
this._layoutSpecifications = styleSpec[`layout_${this.type}`];

this._paintTransitions = {}; // {[propertyName]: StyleTransition}
this._paintTransitionOptions = {}; //
this._paintDeclarations = {}; // {[className]: {[propertyName]: StyleDeclaration}}
this._paintTransitionOptions = {}; // {[propertyName]: { duration:Number, delay:Number }}
this._paintDeclarations = {}; // {[propertyName]: StyleDeclaration}
this._layoutDeclarations = {}; // {[propertyName]: StyleDeclaration}
this._layoutFunctions = {}; // {[propertyName]: Boolean}

let paintName, layoutName;
const options = {validate: false};

// Resolve paint declarations
for (const key in layer) {
const match = key.match(/^paint(?:\.(.*))?$/);
if (match) {
const klass = match[1] || '';
for (paintName in layer[key]) {
this.setPaintProperty(paintName, layer[key][paintName], klass, options);
}
}
for (paintName in layer.paint) {
this.setPaintProperty(paintName, layer.paint[paintName], options);
}

// Resolve layout declarations
Expand Down Expand Up @@ -132,44 +126,33 @@ class StyleLayer extends Evented {
}
}

setPaintProperty(name: string, value: any, klass: string, options: any) {
const validateStyleKey = `layers.${this.id}${klass ? `["paint.${klass}"].` : '.paint.'}${name}`;
setPaintProperty(name: string, value: any, options: any) {
const validateStyleKey = `layers.${this.id}.paint.${name}`;

if (util.endsWith(name, TRANSITION_SUFFIX)) {
if (!this._paintTransitionOptions[klass || '']) {
this._paintTransitionOptions[klass || ''] = {};
}
if (value === null || value === undefined) {
delete this._paintTransitionOptions[klass || ''][name];
delete this._paintTransitionOptions[name];
} else {
if (this._validate(validateStyle.paintProperty, validateStyleKey, name, value, options)) return;
this._paintTransitionOptions[klass || ''][name] = value;
this._paintTransitionOptions[name] = value;
}
} else if (value === null || value === undefined) {
delete this._paintDeclarations[name];
} else {
if (!this._paintDeclarations[klass || '']) {
this._paintDeclarations[klass || ''] = {};
}
if (value === null || value === undefined) {
delete this._paintDeclarations[klass || ''][name];
} else {
if (this._validate(validateStyle.paintProperty, validateStyleKey, name, value, options)) return;
this._paintDeclarations[klass || ''][name] = new StyleDeclaration(this._paintSpecifications[name], value, name);
}
if (this._validate(validateStyle.paintProperty, validateStyleKey, name, value, options)) return;
this._paintDeclarations[name] = new StyleDeclaration(this._paintSpecifications[name], value, name);
}
}

getPaintProperty(name: string, klass?: string) {
klass = klass || '';
getPaintProperty(name: string) {
if (util.endsWith(name, TRANSITION_SUFFIX)) {
return (
this._paintTransitionOptions[klass] &&
this._paintTransitionOptions[klass][name]
this._paintTransitionOptions[name]
);
} else {
return (
this._paintDeclarations[klass] &&
this._paintDeclarations[klass][name] &&
this._paintDeclarations[klass][name].value
this._paintDeclarations[name] &&
this._paintDeclarations[name].value
);
}
}
Expand Down Expand Up @@ -210,30 +193,19 @@ class StyleLayer extends Evented {
return false;
}

updatePaintTransitions(classes: any, options: any, globalOptions: any, animationLoop: any, zoomHistory: any) {
const declarations = util.extend({}, this._paintDeclarations['']);
for (let i = 0; i < classes.length; i++) {
util.extend(declarations, this._paintDeclarations[classes[i]]);
}

updatePaintTransitions(options: any, globalOptions: any, animationLoop: any, zoomHistory: any) {
let name;
for (name in declarations) { // apply new declarations
this._applyPaintDeclaration(name, declarations[name], options, globalOptions, animationLoop, zoomHistory);
for (name in this._paintDeclarations) { // apply new declarations
this._applyPaintDeclaration(name, this._paintDeclarations[name], options, globalOptions, animationLoop, zoomHistory);
}
for (name in this._paintTransitions) {
if (!(name in declarations)) // apply removed declarations
if (!(name in this._paintDeclarations)) // apply removed declarations
this._applyPaintDeclaration(name, null, options, globalOptions, animationLoop, zoomHistory);
}
}

updatePaintTransition(name: any, classes: any, options: any, globalOptions: any, animationLoop: any, zoomHistory: any) {
let declaration = this._paintDeclarations[''][name];
for (let i = 0; i < classes.length; i++) {
const classPaintDeclarations = this._paintDeclarations[classes[i]];
if (classPaintDeclarations && classPaintDeclarations[name]) {
declaration = classPaintDeclarations[name];
}
}
updatePaintTransition(name: any, options: any, globalOptions: any, animationLoop: any, zoomHistory: any) {
const declaration = this._paintDeclarations[name];
this._applyPaintDeclaration(name, declaration, options, globalOptions, animationLoop, zoomHistory);
}

Expand All @@ -257,16 +229,14 @@ class StyleLayer extends Evented {
'minzoom': this.minzoom,
'maxzoom': this.maxzoom,
'filter': this.filter,
'layout': util.mapObject(this._layoutDeclarations, getDeclarationValue)
'layout': util.mapObject(this._layoutDeclarations, getDeclarationValue),
'paint': util.mapObject(this._paintDeclarations, getDeclarationValue)
};

for (const klass in this._paintDeclarations) {
const key = klass === '' ? 'paint' : `paint.${klass}`;
output[key] = util.mapObject(this._paintDeclarations[klass], getDeclarationValue);
}

return util.filterObject(output, (value, key) => {
return value !== undefined && !(key === 'layout' && !Object.keys(value).length);
return value !== undefined &&
!(key === 'layout' && !Object.keys(value).length) &&
!(key === 'paint' && !Object.keys(value).length);
});
}

Expand Down
Loading

0 comments on commit e4201fc

Please sign in to comment.