Add an index parameter (in addition to position) for Map#addControl
Nobody has claimed this yet.
- Dominant language
- TypeScript
- Stars
- 12.4k
- Forks
- 2.4k
- PR merge metrics
- No merged PRs in 30d
Description
## Motivation
Currently, `Map#addControl` enables developers to specify a `position` from the list of `'top-left'` , `'top-right'` , `'bottom-left'` , and `'bottom-right'`, but the chosen `position` determines whether further controls added to said `position` are appended or prepended to controls already added to the `position`. Enabling developers to specify an `index` within the `position` to add the new control would support a wider range of use cases, such as appending rather than prepending (as is currently the default and only behavior) when adding to a `position` of `'bottom-left'` or `'bottom-right'`.
## Design Alternatives
There is currently no way to "correctly" accommodate this -- developers can simply access the `Map#_controlPositions` private class member to re-order the controls, but use of this method likely shouldn't be encouraged.
An alternative suggestion which would require no changes to the GL JS API would be for developers to change the order in which particular calls to `Map#addControl` are made. However, this could be a considerable overhead if developers are porting functionality from other mapping providers which use different default ordering.
## Design
Adding an optional `index` parameter to `Map#addControl` which allows the developer to specify the index at which to add the new control.
### Mock-Up
```
// Add a navigation control to the bottom right.
map.addControl(new mapboxgl.NavigationControl(), 'bottom-right');
// Add a GeolocateControl below the existing NavigationControl, rather than on top.
map.addControl(new mapboxgl.GeolocateControl(), 'bottom-right', 1);
```
### Concepts
There aren't many places in the Mapbox GL ecosystem where an "index" would be of particular important to a developer (beyond the "ordering" of layers, which is more Style Specification-specific). That being said, I believe `index` is a relatively intuitive term for most developers. Perhaps `order` would be a more accessible choice. I'm open to considering other terminology to make the parameter's intention as clear as possible.
### Implementation
In the implementation of `map#addControl`:
```
const positionContainer = this._controlPositions[position];
const childNodes = positionContainer.childNodes;
if (index && index <= childNodes.length) {
const refNode = index < childNodes.length ? childNodes[index] : null;
positionContainer.insertBefore(control, refNode);
else {
// Current logic for adding new control's DOM node
if (position.indexOf('bottom') !== -1) {
positionContainer.insertBefore(controlElement, positionContainer.firstChild);
} else {
positionContainer.appendChild(controlElement);
}
}
```
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start at the implementation of map#addControl and inspect _controlPositions and its position containers. Confirm how the optional index should interact with existing top and bottom insertion behavior; done means indexed controls are inserted at the requested position while calls without an index retain current behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, typescript
- Domain
- frontend
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100