Last active
December 17, 2021 15:27
-
-
Save johnd0e/b378cf7593008f933778c07f7ae03fea to your computer and use it in GitHub Desktop.
IITC plugin: Collapsible portal levels / ornaments [https://gist.github.com/johnd0e/b378cf7593008f933778c07f7ae03fea/raw/collapsible-layers.user.js]
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== | |
// @author johnd0e | |
// @name IITC plugin: Collapsible portal levels / ornaments | |
// @category Layer | |
// @version 0.1.0 | |
// @description Replace mutliple overlays with single layerChooser entry; togglable on faction layers longclick | |
// @id collapsible-layers | |
// @namespace https://gist.github.com/johnd0e | |
// @homepageURL https://gist.github.com/johnd0e/b378cf7593008f933778c07f7ae03fea | |
// @supportURL https://gist.github.com/johnd0e/b378cf7593008f933778c07f7ae03fea#new_comment_field | |
// @downloadURL https://gist.github.com/johnd0e/b378cf7593008f933778c07f7ae03fea/raw/collapsible-layers.user.js | |
// @match https://intel.ingress.com/* | |
// @grant none | |
// ==/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() {}; | |
// use own namespace for plugin | |
var collapsibleLayers = {}; | |
window.plugin.collapsibleLayers = collapsibleLayers; | |
collapsibleLayers.initial = true; // collapsed | |
function setup () { | |
if (window.plugin.hideLevels) { | |
throw new Error("Unable to work together with hide-portal-levels"); | |
} | |
var CollapsibleLayers = L.LayerGroup.extend({ | |
initialize: function (match, options) { | |
options = options || {}; | |
L.LayerGroup.prototype.initialize.call(this, null, options); | |
this._name = options.name || match; | |
this._ctrl = window.layerChooser; | |
this._allLayers = []; | |
if (match) { this.addLayers(match); } | |
}, | |
addLayers: function (match) { | |
this._allLayers = this._ctrl._layers.filter(function (data) { | |
return data.overlay && data.name.endsWith(match); | |
}).concat(this._allLayers); | |
return this; | |
}, | |
bindTo: function () { //?? | |
Array.prototype.forEach.call(arguments, function (name) { | |
this._ctrl.getLayer(name).on('longclick', function (e) { | |
e.preventDefault(); | |
this.toggle(); | |
}, this); | |
}, this); | |
return this; | |
}, | |
_collapse: function () { | |
var disabled = []; | |
this._allLayers.forEach(function (data) { | |
this._ctrl.removeLayer(data.layer, {keepOnMap: true}); | |
if (data.layer._map) { | |
this.addLayer(data.layer); | |
} else { | |
disabled.push(data); | |
} | |
}, this); | |
if (disabled.length === this._allLayers.length) { | |
disabled.forEach(function (data) { | |
this.addLayer(data.layer); | |
}, this); | |
disabled.length = 0; | |
disabled.all = true; | |
} | |
var label; | |
if (disabled.length) { | |
var enableAll = function (e) { | |
e.preventDefault(); | |
var map = e.control._map; | |
disabled.forEach(function (data) { | |
map.addLayer(data.layer); | |
this.addLayer(data.layer); | |
}, this); | |
/*?? | |
disabled.forEach(function (data) { | |
map.addLayer(data.layer); | |
this.addLayer(data.layer); | |
e.control._storeOverlayState(data.name, true); | |
}, this); | |
*/ | |
// restore name | |
e.control.setLabel(this, this._name); | |
}; | |
this.once('longclick', enableAll); | |
this.once('expand', function () { | |
this.off('longclick', enableAll); | |
}); | |
label = this._name + ' [' + disabled.length + ']'; | |
label = '<i>' + label + '</i>'; | |
} | |
this._ctrl.addOverlay(this, label || this._name, { | |
enable: !disabled.all, | |
permanent: false, | |
sortPriority: this.options.sortPriority | |
}); | |
return this.fire('collapse'); | |
}, | |
_expand: function () { | |
this._allLayers.forEach(function (data) { | |
this._ctrl.addOverlay(data.layer, data.name, {default: data.layer._map}); | |
}, this); | |
this._layers = {}; | |
this._ctrl.removeLayer(this); | |
return this.fire('expand'); | |
}, | |
toggle: function (collapse) { | |
if (!arguments.length) { | |
collapse = !this._ctrl.getLayer(this); // layer not in chooser | |
} | |
return this[collapse ? '_collapse' : '_expand'](); | |
}, | |
}); | |
collapsibleLayers.portals = new CollapsibleLayers('Portals', { | |
sortPriority: -1000 | |
}) | |
.bindTo('Resistance', 'Enlightened') | |
.toggle(collapsibleLayers.initial); | |
collapsibleLayers.ornaments = new CollapsibleLayers('Ornaments', { | |
sortPriority: 1000 | |
}) | |
.addLayers('Beacons') | |
.addLayers('Frackers') | |
.addLayers('Artifacts') | |
.bindTo('Resistance', 'Enlightened') | |
.toggle(collapsibleLayers.initial); | |
} | |
/* exported setup */ | |
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