11 |
12 |
13 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "leaflet-history",
3 | "version": "0.1.0",
4 | "main": "dist/",
5 | "devDependencies": {
6 | "grunt": "~0.4.1",
7 | "grunt-cli": "~0.1.11",
8 | "grunt-contrib-uglify": "~0.3.2",
9 | "grunt-contrib-less": "~0.9.0",
10 | "grunt-contrib-clean": "~0.5.0",
11 | "grunt-contrib-concat": "^1.0.1",
12 | "grunt-contrib-jshint": "^1.1.0"
13 | },
14 | "engines": {
15 | "node": ">=0.10.0"
16 | },
17 | "dependencies": {
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/readme.md:
--------------------------------------------------------------------------------
1 | _**Important**_: This repository has not been updated in quite a long time. I haven't worked on any Leaflet projects in so long, and I have no idea if it even works in latest versions. There are some forks that are probably more up-to-date. Please check those out, instead!
2 |
3 | Leaflet-History Control
4 | ==
5 |
6 | Leaflet-History control is a plugin to [leafletjs](http://leafletjs.com) that enables tracking map movements in a history similar to a web browser. I tried to make this plugin extremely easy to use by default, but also extremely extensible if you have want to modify it.
7 |
8 | By default, it is a simple pair of buttons -- back and forward. However, it can be customized to use different icons, different text, allow certain locations to not be saved on move, or even use your own external controls and simply use Leaflet-History as a manager of the history.
9 |
10 | Demo
11 | --
12 | [View sample here.](http://cscott530.github.io/leaflet-history/)
13 |
14 | Installation
15 | --
16 | Leaflet-History is available through bower. You can do:
17 | `bower install leaflet-history`
18 |
19 | Usage
20 | --
21 | Import leaflet-history.js and leaflet-history.css in your page.
22 | ```html
23 |
24 |
25 | ```
26 | Note that the non-minified versions are also availble in `dist/`
27 |
28 | After creating your leaflet map, create and add the controller:
29 |
30 | ```javascript
31 | new L.HistoryControl({}).addTo(map);
32 | ```
33 | Options
34 | --
35 |
36 | When you call `new L.HistoryControl()`, you may pass in an options object to extend its functionality. These options are:
37 |
38 | * **position** - sets which corner of the map to place your controls. possible values are `'topleft'` | `'topright'`(default) | `'bottomleft'` | `'bottomright'`
39 | * **maxMovesToSave** - the number of moves in the history to save before clearing out the oldest. default value is `10`, use `0` or a negative number to make unlimited.
40 | * **backImage**, **forwardImage** - the class used for the button images. defaults are `'fa fa-caret-left'` and `'fa fa-caret-right'`, respectively.
41 | * no image will be displayed if set to empty string.
42 | * **backText**, **forwardText** - the text in the buttons. defaults are `''` (empty).
43 | * **backTooltip**, **forwardTooltip** - tooltip contents. defaults are `'Go to Previous Extent'` and `'Go to Next Extent'`, respectively.
44 | * **backImageBeforeText**, **forwardImageBeforeText** - when both text and image are present, whether to show the image first or the text first (left to right). defaults are `true` and `false`, respectively.
45 | * **orientation** - `'vertical'` | `'horizontal'`(default) - whether to position the buttons on top of one another or side-by-side.
46 | * **useExternalControls** - `true` | `false`(default) - set to true to hide these controls on the map and instead use your own controls. See the [Events](#events) and API for more details on this.
47 | * **shouldSaveMoveInHistory** - `function(zoomCenter) { return true; }` a callback you can provide that gets called with every move. return `false` to not save a move.
48 | * useful if you have certain situations where you move the map programmatically and don't want the user to be able to go back.
49 |
50 | For example, to set your history control to use bootstrap icons and have text:
51 | ```javascript
52 | new L.HistoryControls({
53 | backText: 'Back',
54 | backImage: 'glyphicon glyphicon-chevron-left',
55 | forwardText: 'Forward',
56 | forwardImage: 'glyphicon glyphicon-chevron-right'
57 | }).addTo(map);
58 | ```
59 |
60 | Types
61 | --
62 | ### L.ZoomCenter
63 | Encapsulates both a zoomlevel and the map's center point. Properties:
64 |
65 | * **zoom** - number, value of `map.getZoom()`
66 | * **centerPoint** - `L.LatLng`, value of `map.getCenter()`
67 |
68 | API
69 | --
70 | After you have called `new L.HistoryControl()`, you can use these methods to manage it.
71 |
72 | * **goBack()** - if able, will go to previous map extent. Pushes current to the "future" stack.
73 | * **goForward()** - if able, will go to next map extent. Pushes current to the "back" stack.
74 | * If you set `useExternalControls` to `true` when initializing, use `goBack()` and `goForward()`
75 | * **clearHistory()** - resets the stack of history items.
76 | * **clearFuture()** - resets the stack of future items.
77 | * **performActionWithoutTriggeringEvent(callback)** - will set the map to ignore caching on any events that occur during your (synchronous) callback. Useful if you want to set an initial map location after it's already been configured.
78 | * **moveWithoutTriggeringEvent(zoomCenter)** - convenience wrapper to `performActionWithoutTriggeringEvent`. Rather than passing in a callback, give a `L.ZoomCenter` object.
79 |
80 | For example, if you want to recenter your map after loading some data, but don't want your original location to be in the history:
81 | ```javascript
82 | var history = new L.HistoryControl().addTo(map);
83 | //callbacks to get map data
84 | history.performActionWithoutTriggeringEvent(new L.ZoomCenter(12, L.LatLng([50.5, 30.5])));
85 | ```
86 |
87 | Events
88 | --
89 | Leaflet-History uses the leaflet event API, so subscribing is simple: `map.on('historyback', function(location) {});`
90 | ### Action Events
91 | * **historyback** - fired when `goBack()` is invoked
92 | * **historyfoward** - fired when `goForward()` is invoked
93 | * These get fired whether using the API, or if the default buttons are used.
94 |
95 | In both cases the parameter is an object:
96 | ```
97 | {
98 | currentLocation: L.ZoomCenter object, //the current map location
99 | newLocation: L.ZoomCenter //the new location after the move
100 | }
101 | ```
102 |
103 | ### State Events
104 | * **historybackenabled** - fired when the state of the **back** button changes from disabled to enabled.
105 | * **historybackdisabled** - fired when the state of the **back** button changes from enabled to disabled.
106 | * **historyforwardenabled** - fired when the state of the **forward** button changes from disabled to enabled.
107 | * **historyforwarddisabled** - fired when the state of the **forward** button changes from enabled to disabled.
108 | * Note that these get fired even if using external controls, as a way to help manage your own buttons' state.
109 | * `historybackdisabled` and `historyforwarddisabled` are both fired upon initialization.
110 |
--------------------------------------------------------------------------------
/samples/sample.css:
--------------------------------------------------------------------------------
1 | #map {
2 | height: 100%;
3 | position: absolute;
4 | width: 100%;
5 | }
6 |
7 | body {
8 | margin: 0;
9 | }
--------------------------------------------------------------------------------
/samples/sample1.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Leaflet History - Basic
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
28 |
29 |
--------------------------------------------------------------------------------
/samples/sample2.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Leaflet History - Custom Controls
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 | Back
13 | Forward
14 | Custom Back/Forward buttons are hidden until a move is made.
15 |