|
1 | 1 | leaflet-button
|
2 | 2 | ==============
|
3 | 3 |
|
4 |
| -A simple button control for the leaflet map framework |
| 4 | +A simple button control for the [Leaflet](http://leafletjs.com) map framework |
| 5 | + |
| 6 | + |
| 7 | +## Options |
| 8 | +The button can be configured with these options |
| 9 | + - **position**: the position of the button. Valid positions are the normal positions for leaflet controls: `topright`, |
| 10 | + `topleft`, `bottomright`, and `bottomleft`. |
| 11 | + - **className**: additional css classes you wish to add to the button. |
| 12 | + - **toggleButton**: Turns the button into a toggle button. The value of toggleButton will be the css Class that is toggled on and off. |
| 13 | + |
| 14 | +## A Simple Example |
| 15 | + |
| 16 | +```javascript |
| 17 | +var button = new L.Control.Button('Click me'); |
| 18 | +button.addTo(map); |
| 19 | +button.on('click', function () { |
| 20 | + alert('you clicked the button!'); |
| 21 | +}); |
| 22 | +``` |
| 23 | + |
| 24 | +## Adding a Toggle button |
| 25 | +Setting the option `toggleButton` will turn button into a toggle button. The value you set `toggleButton` |
| 26 | +is the css class added to the button, so you can style against it. |
| 27 | + |
| 28 | +In your click handler, you can use the `isToggled()` method to peform conditional actions |
| 29 | +```javascript |
| 30 | +var button = new L.Control.Button('Toggle me', { |
| 31 | + toggleButton: 'active' |
| 32 | +}); |
| 33 | +button.addTo(map); |
| 34 | +button.on('click', function () { |
| 35 | + if (button.isToggled()) { |
| 36 | + sidebar.hide(); |
| 37 | + } else { |
| 38 | + sidebar.show(); |
| 39 | + } |
| 40 | +}); |
| 41 | +``` |
| 42 | + |
| 43 | +## Further Customizing your button |
| 44 | +You can use an existing button in your document instead of the default button. |
| 45 | +```html |
| 46 | +<button id="helpbutton" role="button" class="btn btn-primary">Learn More</button> |
| 47 | +``` |
| 48 | +Create a new button using a reference to the existing button: |
| 49 | +```javascript |
| 50 | +var button = new L.Control.Button(L.DomUtil.get('helpbutton'), { toggleButton: 'active' }); |
| 51 | +button.addTo(map); |
| 52 | +button.on('click', function () { |
| 53 | + window.location.href = 'http://www.yourdomain.com/help' |
| 54 | +}); |
| 55 | +``` |
| 56 | + |
| 57 | +## Example of Integrating it with [L.sidebar](https://github.com/Turbo87/leaflet-sidebar) |
| 58 | +```html |
| 59 | +<button id="helpbutton" role="button" class="btn btn-primary">Learn More</button> |
| 60 | +<div id="helpsidebar"> |
| 61 | + <h1>Quick Reference Guide</h1> |
| 62 | + <div>This is the quick help document where you can find useful information about the map you're looking at! |
| 63 | +</div> |
| 64 | +``` |
| 65 | +create the sidebar and button in javascript, and link them together using the click handler |
| 66 | +```javascript |
| 67 | +sidebar = L.control.sidebar('helpsidebar', { position: 'right' }); |
| 68 | +sidebar.addTo(map); |
| 69 | + |
| 70 | +helpButton = new L.Control.Button(L.DomUtil.get('helpbutton'), { toggleButton: 'active' }); |
| 71 | +helpButton.addTo(map); |
| 72 | +helpButton.on('click', function () { |
| 73 | + if (helpButton.isToggled()) { |
| 74 | + sidebar.hide(); |
| 75 | + } else { |
| 76 | + sidebar.show(); |
| 77 | + } |
| 78 | +}); |
| 79 | +``` |
0 commit comments