Skip to content

Commit 1230d4f

Browse files
authored
Merge pull request #103 from boldtrn/mappings_callback_function
Make it possible to pass a callback function in the mappings object
2 parents 1bf1e49 + 5c2cff9 commit 1230d4f

5 files changed

Lines changed: 33 additions & 38 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
# Changelog
22
All notable changes to this project will be documented in this file.
33

4+
## v1.4.0 (XXXX)
5+
6+
**Added**
7+
- Option pass a callback function in the mappings object. This can be used for path details that have many possible values. See the example mappings.js for an example.
8+
49
## v1.3.3 (2020-10-21)
510

611
**Added**

‎dist/L.Control.Heightgraph.js‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4966,6 +4966,7 @@ var schemeSet3 = colors("8dd3c7ffffb3bebadafb807280b1d3fdb462b3de69fccde5d9d9d9b
49664966
var i = void 0,
49674967
cnt = 0;
49684968
var usedColors = {};
4969+
var isMappingFunction = this._mappings !== undefined && typeof this._mappings[data[y].properties.summary] === 'function';
49694970

49704971
for (i = 0; i < data[y].features.length; i++) {
49714972
// data is redundant in every element of data which is why we collect it once
@@ -4991,8 +4992,15 @@ var schemeSet3 = colors("8dd3c7ffffb3bebadafb807280b1d3fdb462b3de69fccde5d9d9d9b
49914992
usedColors[attributeType] = color;
49924993
}
49934994
} else {
4994-
text = this._mappings[data[y].properties.summary][attributeType].text;
4995-
color = this._mappings[data[y].properties.summary][attributeType].color;
4995+
if (isMappingFunction) {
4996+
var result = this._mappings[data[y].properties.summary](attributeType);
4997+
4998+
text = result.text;
4999+
color = result.color;
5000+
} else {
5001+
text = this._mappings[data[y].properties.summary][attributeType].text;
5002+
color = this._mappings[data[y].properties.summary][attributeType].color;
5003+
}
49965004
}
49975005

49985006
var attribute = {

‎dist/L.Control.Heightgraph.min.js‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎example/mappings.js‎

Lines changed: 8 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -169,39 +169,14 @@ const colorMappings = {
169169
color: '#dbe3ff'
170170
}
171171
},
172-
suitability: {
173-
'3': {
174-
text: '3/10',
175-
color: '#3D3D3D'
176-
},
177-
'4': {
178-
text: '4/10',
179-
color: '#4D4D4D'
180-
},
181-
'5': {
182-
text: '5/10',
183-
color: '#5D5D5D'
184-
},
185-
'6': {
186-
text: '6/10',
187-
color: '#6D6D6D'
188-
},
189-
'7': {
190-
text: '7/10',
191-
color: '#7C7C7C'
192-
},
193-
'8': {
194-
text: '8/10',
195-
color: '#8D8D8D'
196-
},
197-
'9': {
198-
text: '9/10',
199-
color: '#9D9D9D'
200-
},
201-
'10': {
202-
text: '10/10',
203-
color: '#ADADAD'
204-
}
172+
suitability: function(data){
173+
174+
const hex = parseInt(data).toString(16).toUpperCase();
175+
176+
return {
177+
text: `${data}/10`,
178+
color: `#${hex}D${hex}D${hex}D`
179+
};
205180
},
206181
conditionally_closed: {
207182
'true': {

‎src/L.Control.Heightgraph.js‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,7 @@ import {
353353
};
354354
let i, cnt = 0
355355
const usedColors = {}
356+
const isMappingFunction = this._mappings !== undefined && typeof this._mappings[data[y].properties.summary] === 'function';
356357
for (i = 0; i < data[y].features.length; i++) {
357358
// data is redundant in every element of data which is why we collect it once
358359
let altitude, ptA, ptB, ptDistance
@@ -372,8 +373,14 @@ import {
372373
usedColors[attributeType] = color;
373374
}
374375
} else {
375-
text = this._mappings[data[y].properties.summary][attributeType].text;
376-
color = this._mappings[data[y].properties.summary][attributeType].color;
376+
if (isMappingFunction) {
377+
const result = this._mappings[data[y].properties.summary](attributeType);
378+
text = result.text;
379+
color = result.color;
380+
} else {
381+
text = this._mappings[data[y].properties.summary][attributeType].text;
382+
color = this._mappings[data[y].properties.summary][attributeType].color;
383+
}
377384
}
378385
const attribute = {
379386
type: attributeType, text: text, color: color

0 commit comments

Comments
 (0)