-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added unit dropdowns to maxheight field
- Loading branch information
Showing
9 changed files
with
232 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
{ | ||
"key": "maxheight", | ||
"type": "combo", | ||
"type": "maxheight", | ||
"label": "Max Height", | ||
"placeholder": "4, 4.5, 5, 14'0\", 14'6\", 15'0\"", | ||
"snake_case": false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -59,6 +59,7 @@ | |
"identifier", | ||
"localized", | ||
"manyCombo", | ||
"maxheight", | ||
"maxspeed", | ||
"multiCombo", | ||
"networkCombo", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,197 @@ | ||
import { dispatch as d3_dispatch } from 'd3-dispatch'; | ||
import { select as d3_select } from 'd3-selection'; | ||
import * as countryCoder from '@ideditor/country-coder'; | ||
|
||
import { uiCombobox } from '../combobox'; | ||
import { t } from '../../core/localizer'; | ||
import { utilGetSetValue, utilNoAuto, utilRebind, utilTotalExtent } from '../../util'; | ||
|
||
|
||
export function uiFieldMaxheight(field, context) { | ||
var dispatch = d3_dispatch('change'); | ||
var primaryUnitInput = d3_select(null); | ||
var primaryInput = d3_select(null); | ||
var secondaryInput = d3_select(null); | ||
var secondaryUnitInput = d3_select(null); | ||
var _entityIDs = []; | ||
var _tags; | ||
var _isImperial; | ||
|
||
var primaryUnits = [ | ||
{ | ||
value: 'm', | ||
title: t('inspector.maxheight.meter'), | ||
}, | ||
{ | ||
value: 'ft', | ||
title: t('inspector.maxheight.foot'), | ||
}, | ||
]; | ||
|
||
var unitCombo = uiCombobox(context, 'maxheight-unit') | ||
.data(primaryUnits); | ||
|
||
function maxheight(selection) { | ||
|
||
var wrap = selection.selectAll('.form-field-input-wrap') | ||
.data([0]); | ||
|
||
wrap = wrap.enter() | ||
.append('div') | ||
.attr('class', 'form-field-input-wrap form-field-input-' + field.type) | ||
.merge(wrap); | ||
|
||
|
||
primaryInput = wrap.selectAll('input.maxheight-number') | ||
.data([0]); | ||
|
||
primaryInput = primaryInput.enter() | ||
.append('input') | ||
.attr('type', 'text') | ||
.attr('class', 'maxheight-number') | ||
.attr('id', field.domId) | ||
.call(utilNoAuto) | ||
.merge(primaryInput); | ||
|
||
primaryInput | ||
.on('change', change) | ||
.on('blur', change); | ||
|
||
var loc = combinedEntityExtent().center(); | ||
_isImperial = countryCoder.roadHeightUnit(loc) === 'ft'; | ||
|
||
primaryUnitInput = wrap.selectAll('input.maxheight-unit') | ||
.data([0]); | ||
|
||
primaryUnitInput = primaryUnitInput.enter() | ||
.append('input') | ||
.attr('type', 'text') | ||
.attr('class', 'maxheight-unit') | ||
.call(unitCombo) | ||
.merge(primaryUnitInput); | ||
|
||
primaryUnitInput | ||
.on('blur', changeUnits) | ||
.on('change', changeUnits); | ||
|
||
secondaryInput = wrap.selectAll('input.maxheight-secondary-number') | ||
.data([0]); | ||
|
||
secondaryInput = secondaryInput.enter() | ||
.append('input') | ||
.attr('type', 'text') | ||
.attr('class', 'maxheight-secondary-number') | ||
.call(utilNoAuto) | ||
.merge(secondaryInput); | ||
|
||
secondaryInput | ||
.on('change', change) | ||
.on('blur', change); | ||
|
||
secondaryUnitInput = wrap.selectAll('input.maxheight-secondary-unit') | ||
.data([0]); | ||
secondaryUnitInput = secondaryUnitInput.enter() | ||
.append('input') | ||
.attr('type', 'text') | ||
.call(utilNoAuto) | ||
.classed('disabled', true) | ||
.classed('maxheight-secondary-unit', true) | ||
.attr('readonly', 'readonly') | ||
.merge(secondaryUnitInput); | ||
|
||
|
||
function changeUnits() { | ||
_isImperial = utilGetSetValue(primaryUnitInput) === 'ft'; | ||
utilGetSetValue(primaryUnitInput, _isImperial ? 'ft' : 'm'); | ||
setUnitSuggestions(); | ||
change(); | ||
} | ||
} | ||
|
||
|
||
function setUnitSuggestions() { | ||
utilGetSetValue(primaryUnitInput, _isImperial ? 'ft' : 'm'); | ||
} | ||
|
||
|
||
function change() { | ||
var tag = {}; | ||
var primaryValue = utilGetSetValue(primaryInput).trim(); | ||
var secondaryValue = utilGetSetValue(secondaryInput).trim(); | ||
|
||
// don't override multiple values with blank string | ||
if (!primaryValue && !secondaryValue && Array.isArray(_tags[field.key])) return; | ||
|
||
if (!primaryValue && !secondaryValue) { | ||
tag[field.key] = undefined; | ||
} else if (isNaN(primaryValue) || isNaN(secondaryValue) || !_isImperial) { | ||
tag[field.key] = context.cleanTagValue(primaryValue); | ||
} else { | ||
if (primaryValue !== '') { | ||
primaryValue = context.cleanTagValue(primaryValue + '\''); | ||
} | ||
if (secondaryValue !== '') { | ||
secondaryValue = context.cleanTagValue(secondaryValue + '"'); | ||
} | ||
tag[field.key] = primaryValue + secondaryValue; | ||
} | ||
|
||
dispatch.call('change', this, tag); | ||
} | ||
|
||
|
||
maxheight.tags = function(tags) { | ||
_tags = tags; | ||
|
||
var primaryValue = tags[field.key]; | ||
var secondaryValue; | ||
var isMixed = Array.isArray(primaryValue); | ||
|
||
if (!isMixed) { | ||
if (primaryValue && (primaryValue.indexOf('\'') >= 0 || primaryValue.indexOf('"') >= 0)) { | ||
secondaryValue = primaryValue.match(/(-?[\d.]+)"/); | ||
if (secondaryValue !== null) { | ||
secondaryValue = secondaryValue[1]; | ||
} | ||
primaryValue = primaryValue.match(/(-?[\d.]+)'/); | ||
if (primaryValue !== null) { | ||
primaryValue = primaryValue[1]; | ||
} | ||
_isImperial = true; | ||
} else if (primaryValue) { | ||
_isImperial = false; | ||
} | ||
} | ||
|
||
setUnitSuggestions(); | ||
|
||
utilGetSetValue(primaryInput, typeof primaryValue === 'string' ? primaryValue : '') | ||
.attr('title', isMixed ? primaryValue.filter(Boolean).join('\n') : null) | ||
.attr('placeholder', isMixed ? t('inspector.multiple_values') : field.placeholder()) | ||
.classed('mixed', isMixed); | ||
utilGetSetValue(secondaryInput, typeof secondaryValue === 'string' ? secondaryValue : '') | ||
.attr('placeholder', isMixed ? t('inspector.multiple_values') : (_isImperial ? field.placeholder() : null)) | ||
.classed('mixed', isMixed) | ||
.classed('disabled', !_isImperial) | ||
.attr('readonly', _isImperial ? null : 'readonly'); | ||
secondaryUnitInput.attr('value', _isImperial ? t('inspector.maxheight.inch') : null); | ||
}; | ||
|
||
|
||
maxheight.focus = function() { | ||
primaryInput.node().focus(); | ||
}; | ||
|
||
|
||
maxheight.entityIDs = function(val) { | ||
_entityIDs = val; | ||
}; | ||
|
||
|
||
function combinedEntityExtent() { | ||
return _entityIDs && _entityIDs.length && utilTotalExtent(_entityIDs, context.graph()); | ||
} | ||
|
||
|
||
return utilRebind(maxheight, dispatch, 'on'); | ||
} |