Instantly share code, notes, and snippets.
Last active
December 26, 2018 12:59
-
Save johnd0e/110464cc35dd9d5e5ad6585c8e297b47 to your computer and use it in GitHub Desktop.
IITC plugin: NavBar
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
// ==UserScript== | |
// @id iitc-plugin-navbar | |
// @name IITC plugin: NavBar | |
// @description Simple navigation control that allows back and forward navigation through map's view history | |
// @category test | |
// @version 0.1.1 | |
// @author jd | |
// @namespace https://gist.github.com/johnd0e | |
// @homepageURL https://gist.github.com/johnd0e/110464cc35dd9d5e5ad6585c8e297b47 | |
// @supportURL https://gist.github.com/johnd0e/110464cc35dd9d5e5ad6585c8e297b47#new_comment_field | |
// @updateURL https://gist.github.com/johnd0e/110464cc35dd9d5e5ad6585c8e297b47/raw/NavBar.user.js | |
// @downloadURL https://gist.github.com/johnd0e/110464cc35dd9d5e5ad6585c8e297b47/raw/NavBar.user.js | |
// @include https://intel.ingress.com/intel* | |
// @grant none | |
// @run-at document-start | |
// ==/UserScript== | |
function wrapper(plugin_info) { | |
// ensure plugin framework is there, even if iitc is not yet loaded | |
if(typeof window.plugin !== 'function') window.plugin = function() {}; | |
// PLUGIN START //////////////////////////////////////////////////////// | |
// https://github.com/davidchouse/Leaflet.NavBar | |
let css = '\ | |
/* Icons from Font-Awesome | Font Awesome by Dave Gandy - http://fontawesome.io*/\ | |
/* Thanks to http://fa2png.io/ for icon png generation */\ | |
\ | |
.leaflet-control-navbar-fwd {\ | |
background-image: url("img/arrow-right_000000_14.png");\ | |
}\ | |
\ | |
.leaflet-control-navbar-back {\ | |
background-image: url("img/arrow-left_000000_14.png");\ | |
}\ | |
\ | |
.leaflet-control-navbar-home {\ | |
background-image: url("img/home_000000_14.png");\ | |
}\ | |
\ | |
\ | |
.leaflet-control-navbar-fwd-disabled {\ | |
background-image: url("img/arrow-right_bbbbbb_14.png");\ | |
}\ | |
\ | |
.leaflet-control-navbar-back-disabled {\ | |
background-image: url("img/arrow-left_bbbbbb_14.png");\ | |
}\ | |
\ | |
.leaflet-control-navbar-home-disabled {\ | |
background-image: url("img/home_bbbbbb_14.png");\ | |
}'; | |
function setup () { | |
/* | |
* Simple navigation control that allows back and forward navigation through map's view history | |
*/ | |
(function() { | |
L.Control.NavBar = L.Control.extend({ | |
options: { | |
position: 'topleft', | |
//center:, | |
//zoom :, | |
//bbox:, //Alternative to center/zoom for home button, takes precedence if included | |
forwardTitle: 'Go forward in map view history', | |
backTitle: 'Go back in map view history', | |
homeTitle: 'Go to home map view' | |
}, | |
onAdd: function(map) { | |
// Set options | |
if (!this.options.center) { | |
this.options.center = map.getCenter(); | |
} | |
if (!this.options.zoom) { | |
this.options.zoom = map.getZoom(); | |
} | |
var options = this.options; | |
// Create toolbar | |
var controlName = 'leaflet-control-navbar', | |
container = L.DomUtil.create('div', controlName + ' leaflet-bar'); | |
// Add toolbar buttons | |
this._homeButton = this._createButton(options.homeTitle, controlName + '-home', container, this._goHome); | |
this._fwdButton = this._createButton(options.forwardTitle, controlName + '-fwd', container, this._goFwd); | |
this._backButton = this._createButton(options.backTitle, controlName + '-back', container, this._goBack); | |
// Initialize view history and index | |
this._viewHistory = [{center: this.options.center, zoom: this.options.zoom}]; | |
this._curIndx = 0; | |
this._updateDisabled(); | |
map.once('moveend', function() {this._map.on('moveend', this._updateHistory, this);}, this); | |
// Set intial view to home | |
map.setView(options.center, options.zoom); | |
return container; | |
}, | |
onRemove: function(map) { | |
map.off('moveend', this._updateHistory, this); | |
}, | |
_goHome: function() { | |
if (this.options.bbox){ | |
try { | |
this._map.fitBounds(this.options.bbox); | |
} catch(err){ | |
this._map.setView(this.options.center, this.options.zoom); //Use default if invalid bbox input. | |
} | |
} | |
this._map.setView(this.options.center, this.options.zoom); | |
}, | |
_goBack: function() { | |
if (this._curIndx !== 0) { | |
this._map.off('moveend', this._updateHistory, this); | |
this._map.once('moveend', function() {this._map.on('moveend', this._updateHistory, this);}, this); | |
this._curIndx--; | |
this._updateDisabled(); | |
var view = this._viewHistory[this._curIndx]; | |
this._map.setView(view.center, view.zoom); | |
} | |
}, | |
_goFwd: function() { | |
if (this._curIndx != this._viewHistory.length - 1) { | |
this._map.off('moveend', this._updateHistory, this); | |
this._map.once('moveend', function() {this._map.on('moveend', this._updateHistory, this);}, this); | |
this._curIndx++; | |
this._updateDisabled(); | |
var view = this._viewHistory[this._curIndx]; | |
this._map.setView(view.center, view.zoom); | |
} | |
}, | |
_createButton: function(title, className, container, fn) { | |
// Modified from Leaflet zoom control | |
var link = L.DomUtil.create('a', className, container); | |
link.href = '#'; | |
link.title = title; | |
L.DomEvent | |
.on(link, 'mousedown dblclick', L.DomEvent.stopPropagation) | |
.on(link, 'click', L.DomEvent.stop) | |
.on(link, 'click', fn, this) | |
.on(link, 'click', this._refocusOnMap, this); | |
return link; | |
}, | |
_updateHistory: function() { | |
var newView = {center: this._map.getCenter(), zoom: this._map.getZoom()}; | |
var insertIndx = this._curIndx + 1; | |
this._viewHistory.splice(insertIndx, this._viewHistory.length - insertIndx, newView); | |
this._curIndx++; | |
// Update disabled state of toolbar buttons | |
this._updateDisabled(); | |
}, | |
_setFwdEnabled: function(enabled) { | |
var leafletDisabled = 'leaflet-disabled'; | |
var fwdDisabled = 'leaflet-control-navbar-fwd-disabled'; | |
if (enabled === true) { | |
L.DomUtil.removeClass(this._fwdButton, fwdDisabled); | |
L.DomUtil.removeClass(this._fwdButton, leafletDisabled); | |
}else { | |
L.DomUtil.addClass(this._fwdButton, fwdDisabled); | |
L.DomUtil.addClass(this._fwdButton, leafletDisabled); | |
} | |
}, | |
_setBackEnabled: function(enabled) { | |
var leafletDisabled = 'leaflet-disabled'; | |
var backDisabled = 'leaflet-control-navbar-back-disabled'; | |
if (enabled === true) { | |
L.DomUtil.removeClass(this._backButton, backDisabled); | |
L.DomUtil.removeClass(this._backButton, leafletDisabled); | |
}else { | |
L.DomUtil.addClass(this._backButton, backDisabled); | |
L.DomUtil.addClass(this._backButton, leafletDisabled); | |
} | |
}, | |
_updateDisabled: function() { | |
if (this._curIndx == (this._viewHistory.length - 1)) { | |
this._setFwdEnabled(false); | |
}else { | |
this._setFwdEnabled(true); | |
} | |
if (this._curIndx <= 0) { | |
this._setBackEnabled(false); | |
}else { | |
this._setBackEnabled(true); | |
} | |
} | |
}); | |
L.control.navbar = function(options) { | |
return new L.Control.NavBar(options); | |
}; | |
})(); | |
//$('<style>').prop('type', 'text/css').html(css).appendTo('head'); | |
window.addHook('iitcLoaded', function(data) { | |
// defer in order to get proper home location | |
L.control.navbar().addTo(map); | |
// or alternatively we could specify it in options: L.control.navbar(getPosition()).addTo(map); | |
}); | |
} | |
// PLUGIN END ////////////////////////////////////////////////////////// | |
setup.info = plugin_info; //add the script info data to the function as a property | |
if(!window.bootPlugins) window.bootPlugins = []; | |
window.bootPlugins.push(setup); | |
// if IITC has already booted, immediately run the 'setup' function | |
if(window.iitcLoaded && typeof setup === 'function') setup(); | |
} // wrapper end | |
// inject code into site context | |
var script = document.createElement('script'); | |
var info = {}; | |
if (typeof GM_info !== 'undefined' && GM_info && GM_info.script) info.script = { version: GM_info.script.version, name: GM_info.script.name, description: GM_info.script.description }; | |
script.appendChild(document.createTextNode('('+ wrapper +')('+JSON.stringify(info)+');')); | |
(document.body || document.head || document.documentElement).appendChild(script); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment