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 committed Nov 17, 2016
1 parent e7e50c0 commit fe40f72
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 324 deletions.
29 changes: 14 additions & 15 deletions js/style/style.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class Style extends Evented {
this._loaded = true;
this.stylesheet = stylesheet;

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

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

_applyClasses(classes, options) {
_applyPaintPropertyUpdates(options) {
if (!this._loaded) return;

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

Expand All @@ -170,10 +169,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 @@ -242,7 +241,7 @@ class Style extends Evented {
/**
* Apply queued style updates in a batch
*/
update(classes, options) {
update(options) {
if (!this._changed) return;

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

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

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

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

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

setPaintProperty(layerId, name, value, klass) {
setPaintProperty(layerId, name, value) {
this._checkLoaded();

const layer = this.getLayer(layerId);

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 @@ -516,19 +515,19 @@ class Style extends Evented {
this._updateLayer(layer);
}

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

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

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

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

this._paintTransitions = {}; // {[propertyName]: StyleTransition}
this._paintTransitionOptions = {}; // {[className]: {[propertyName]: { duration:Number, delay:Number }}}
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 @@ -93,44 +87,33 @@ class StyleLayer extends Evented {
}
}

setPaintProperty(name, value, klass, options) {
const validateStyleKey = `layers.${this.id}${klass ? `["paint.${klass}"].` : '.paint.'}${name}`;
setPaintProperty(name, value, options) {
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);
}
if (this._validate(validateStyle.paintProperty, validateStyleKey, name, value, options)) return;
this._paintDeclarations[name] = new StyleDeclaration(this._paintSpecifications[name], value);
}
}

getPaintProperty(name, klass) {
klass = klass || '';
getPaintProperty(name) {
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 @@ -200,30 +183,19 @@ class StyleLayer extends Evented {
return false;
}

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

updatePaintTransitions(options, globalOptions, animationLoop, zoomHistory) {
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, classes, options, globalOptions, animationLoop, zoomHistory) {
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, options, globalOptions, animationLoop, zoomHistory) {
const declaration = this._paintDeclarations[name];
this._applyPaintDeclaration(name, declaration, options, globalOptions, animationLoop, zoomHistory);
}

Expand All @@ -247,16 +219,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 fe40f72

Please sign in to comment.