Skip to content
This repository has been archived by the owner on Apr 10, 2018. It is now read-only.

Drop paint classes #576

Merged
merged 10 commits into from
Nov 17, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions API.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,51 @@

### `declassStyle(style, classes)`

Returns a new style with the given 'paint classes' merged into each layer's
main `paint` definiton, and with all `paint.*` properties removed.


### Parameters

| parameter | type | description |
| --------- | ----------------- | ---------------------------------------------- |
| `style` | Object | A style JSON object. |
| `classes` | Array\.\<string\> | An array of paint classes to apply, in order. |


### Example

```js
var declass = require('mapbox-gl-style-spec/lib/declass')
var baseStyle = { ... style with a 'paint.night' property in some layers ... }
var nightStyle = declass(baseStyle, ['night'])
// nightStyle now has each layer's `paint.night` properties merged in to the
// main `paint` property.
```


### `derefLayers(layers)`

Given an array of layers, some of which may contain `ref` properties
whose value is the `id` of another property, return a new array where
such layers have been augmented with the 'type', 'source', etc. properties
from the parent layer, and the `ref` property has been removed.

The input is not modified. The output may contain references to portions
of the input.


### Parameters

| parameter | type | description |
| --------- | ---------------- | ----------- |
| `layers` | Array\.\<Layer\> | |



**Returns** `Array.<Layer>`,


### `diffStyles([before], after)`

Diff two stylesheet
Expand Down Expand Up @@ -60,6 +107,29 @@ fs.writeFileSync('./dest.min.json', format(style, 0));
**Returns** `string`, stringified formatted JSON


### `groupByLayout(layers)`

Given an array of layers, return an array of arrays of layers where all
layers in each group have identical layout-affecting properties. These
are the properties that were formerly used by explicit `ref` mechanism
for layers: 'type', 'source', 'source-layer', 'minzoom', 'maxzoom',
'filter', and 'layout'.

The input is not modified. The output layers are references to the
input layers.


### Parameters

| parameter | type | description |
| --------- | ---------------- | ----------- |
| `layers` | Array\.\<Layer\> | |



**Returns** `Array.<Array.<Layer>>`,


### `migrate(style)`

Migrate a Mapbox GL Style to the latest version.
Expand Down
3 changes: 0 additions & 3 deletions docs/_generate/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -628,9 +628,6 @@ <h2><a href='#layers' title='link to layers'>Layers</a></h2>
<p>
<em>Paint properties</em> are applied later in the rendering process. A layer that shares layout properties with another
layer can have independent paint properties. Paint properties appear in the layer's <code>"paint"</code> object.
Layers can also have class-specific paint properties, which are applied only when the map has a certain class
name set. For example, a layer with a <code>"paint.night"</code> property would have those properties applied
when the map has the <code>"night"</code> class set.
</p>
<p class='space-bottom4 quiet small'>Key:
<a class='icon smooth-ramp quiet micro space-right inline' href='#function' title='Supports interpolated functions'>supports interpolated functions</a>
Expand Down
15 changes: 12 additions & 3 deletions lib/declass.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,23 @@

var extend = require('./util/extend');

module.exports = declassStyle;

/**
* Returns a new style with the given 'paint classes' merged into each layer's
* main `paint` definiton.
* main `paint` definiton, and with all `paint.*` properties removed.
*
* @param {Object} style A style JSON object.
* @param {Array<string>} classes An array of paint classes to apply, in order.
*
* @example
* var declass = require('mapbox-gl-style-spec/lib/declass')
* var baseStyle = { ... style with a 'paint.night' property in some layers ... }
* var nightStyle = declass(baseStyle, ['night'])
* // nightStyle now has each layer's `paint.night` properties merged in to the
* // main `paint` property.
*/
module.exports = function declassStyle(style, classes) {
function declassStyle(style, classes) {
return extend({}, style, {
layers: style.layers.map(function (layer) {
var result = classes.reduce(declassLayer, layer);
Expand All @@ -24,7 +33,7 @@ module.exports = function declassStyle(style, classes) {
return result;
})
});
};
}

function declassLayer(layer, klass) {
return extend({}, layer, {
Expand Down
6 changes: 4 additions & 2 deletions lib/deref.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ function deref(layer, parent) {
return result;
}

module.exports = derefLayers;

/**
* Given an array of layers, some of which may contain `ref` properties
* whose value is the `id` of another property, return a new array where
Expand All @@ -32,7 +34,7 @@ function deref(layer, parent) {
* @param {Array<Layer>} layers
* @returns {Array<Layer>}
*/
module.exports = function (layers) {
function derefLayers(layers) {
layers = layers.slice();

var map = Object.create(null), i;
Expand All @@ -47,4 +49,4 @@ module.exports = function (layers) {
}

return layers;
};
}
6 changes: 4 additions & 2 deletions lib/group_by_layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ function key(layer) {
}));
}

module.exports = groupByLayout;

/**
* Given an array of layers, return an array of arrays of layers where all
* layers in each group have identical layout-affecting properties. These
Expand All @@ -22,7 +24,7 @@ function key(layer) {
* @param {Array<Layer>} layers
* @returns {Array<Array<Layer>>}
*/
module.exports = function (layers) {
function groupByLayout(layers) {
var groups = {}, i, k;

for (i = 0; i < layers.length; i++) {
Expand All @@ -41,4 +43,4 @@ module.exports = function (layers) {
}

return result;
};
}
27 changes: 27 additions & 0 deletions migrations/v9.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict';

var deref = require('../lib/deref');

function eachLayer(style, callback) {
for (var k in style.layers) {
callback(style.layers[k]);
}
}

module.exports = function(style) {
style.version = 9;

// remove user-specified refs
style.layers = deref(style.layers);

// remove class-specific paint properties
eachLayer(style, function (layer) {
for (var k in layer) {
if (/paint\..*/.test(k)) {
delete layer[k];
}
}
});

return style;
};
2 changes: 1 addition & 1 deletion reference/v8.json
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@
},
"paint.*": {
"type": "paint",
"doc": "Class-specific paint properties for this layer. The class name is the part after the first dot."
"doc": "[Deprecated] Class-specific paint properties for this layer. The class name is the part after the first dot. Note: class-specific paint properties are deprecated and will be removed in the next version of this spec."
}
},
"layout": [
Expand Down
78 changes: 78 additions & 0 deletions test/migrations/v9.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
'use strict';

var t = require('tape'),
migrate = require('../../migrations/v9');

t('deref layers', function (t) {
var input = {
version: 8,
sources: {
a: { type: 'vector', tiles: [ 'http://dev/null' ] }
},
layers: [{
id: 'parent',
source: 'a',
'source-layer': 'x',
type: 'fill'
}, {
id: 'child',
ref: 'parent'
}]
};

t.deepEqual(migrate(input), {
version: 9,
sources: {
a: { type: 'vector', tiles: [ 'http://dev/null' ] }
},
layers: [{
id: 'parent',
source: 'a',
'source-layer': 'x',
type: 'fill'
}, {
id: 'child',
source: 'a',
'source-layer': 'x',
type: 'fill'
}]
});

t.end();
});

t('declass style', function (t) {
var input = {
version: 8,
sources: {
a: { type: 'vector', tiles: [ 'http://dev/null' ] }
},
layers: [{
id: 'a',
source: 'a',
type: 'fill',
paint: {},
'paint.right': {
'fill-color': 'red'
},
'paint.left': {
'fill-color': 'blue'
}
}]
};

t.deepEqual(migrate(input), {
version: 9,
sources: {
a: { type: 'vector', tiles: [ 'http://dev/null' ] }
},
layers: [{
id: 'a',
source: 'a',
type: 'fill',
paint: {}
}]
});

t.end();
});