diff --git a/plugins/draw-resonators.user.js b/plugins/draw-resonators.user.js index 95624e0e..0bf73ff7 100644 --- a/plugins/draw-resonators.user.js +++ b/plugins/draw-resonators.user.js @@ -2,7 +2,7 @@ // @id iitc-plugin-draw-resonators@xelio // @name IITC plugin: Draw resonators // @category Layer -// @version 0.2.0.@@DATETIMEVERSION@@ +// @version 0.2.1.@@DATETIMEVERSION@@ // @namespace https://github.com/jonatkins/ingress-intel-total-conversion // @updateURL @@UPDATEURL@@ // @downloadURL @@DOWNLOADURL@@ @@ -56,6 +56,7 @@ window.plugin.drawResonators.Render = function(options) { this.deleteResonatorEntities = this.deleteResonatorEntities.bind(this); this.handleResonatorEntitiesBeforeZoom = this.handleResonatorEntitiesBeforeZoom.bind(this); this.handleResonatorEntitiesAfterZoom = this.handleResonatorEntitiesAfterZoom.bind(this); + this.handleEnableZoomLevelChange = this.handleEnableZoomLevelChange.bind(this); this.portalSelectionChange = this.portalSelectionChange.bind(this); }; @@ -161,24 +162,46 @@ window.plugin.drawResonators.Render.prototype.handleResonatorEntitiesBeforeZoom window.plugin.drawResonators.Render.prototype.handleResonatorEntitiesAfterZoom = function() { if(!this.isResonatorsShow()) { - this.resonatorLayerGroup.clearLayers(); - this.resonators = {}; - } else { - // Redraw all resonators if they were deleted - if(this.isResonatorsShowBeforeZoom()) return; + this.clearAllResonators(); + return; + } - var render = this; + // Draw all resonators if they were not drawn + if(!this.isResonatorsShowBeforeZoom()) { + this.drawAllResonators(); + } +} - // loop through level of portals, only draw if the portal is shown on map - for (var guid in window.portals) { - var portal = window.portals[guid]; - // FIXME: need to find a proper way to check if a portal is added to the map without depending on leaflet internals - // (and without depending on portalsLayers either - that's IITC internal) - if (portal._map) { - render.createResonatorEntities(portal); - } +window.plugin.drawResonators.Render.prototype.handleEnableZoomLevelChange = function(zoomLevel) { + this.enableZoomLevel = zoomLevel; + + if(!this.isResonatorsShow()) { + this.clearAllResonators(); + return; + } + + // Draw all resonators if they were not drawn + if(!Object.keys(this.resonators).length > 0) { + this.drawAllResonators(); + } +} + +window.plugin.drawResonators.Render.prototype.clearAllResonators = function() { + this.resonatorLayerGroup.clearLayers(); + this.resonators = {}; +} + +window.plugin.drawResonators.Render.prototype.drawAllResonators = function() { + var render = this; + + // loop through level of portals, only draw if the portal is shown on map + for (var guid in window.portals) { + var portal = window.portals[guid]; + // FIXME: need to find a proper way to check if a portal is added to the map without depending on leaflet internals + // (and without depending on portalsLayers either - that's IITC internal) + if (portal._map) { + render.createResonatorEntities(portal); } - } } @@ -299,14 +322,22 @@ window.plugin.drawResonators.Styler.prototype.defaultConnectorStyle = function(r //////// Options for storing and loading options //////// -// TODO: add callback to notify option changes + window.plugin.drawResonators.Options = function() { this._options = {}; + this._callbacks = {}; +} + +window.plugin.drawResonators.Options.prototype.addCallback = function(name, callback) { + if (!this._callbacks[name]) { + this._callbacks[name] = []; + } + this._callbacks[name].push(callback); } window.plugin.drawResonators.Options.prototype.newOption = function(name, defaultValue) { - this._options[name] = this.loadLocal(this.getStorageKey, defaultValue) + this._options[name] = this.loadLocal(this.getStorageKey(name), defaultValue) } window.plugin.drawResonators.Options.prototype.getOption = function(name) { @@ -315,9 +346,16 @@ window.plugin.drawResonators.Options.prototype.getOption = function(name) { window.plugin.drawResonators.Options.prototype.changeOption = function(name, value) { if(!(name in this._options)) return false; + if(value === this._options[name]) return false; this._options[name] = value; - this.storeLocal(name, this._options[name]); + this.storeLocal(this.getStorageKey(name), this._options[name]); + + if (this._callbacks[name] !== null) { + for(var i in this._callbacks[name]) { + this._callbacks[name][i](value); + } + } } window.plugin.drawResonators.Options.prototype.getStorageKey = function(name) { @@ -343,21 +381,128 @@ window.plugin.drawResonators.Options.prototype.storeLocal = function(key, value) +//////// Dialog + +window.plugin.drawResonators.Dialog = function() { + this._dialogEntries = {}; +} + +window.plugin.drawResonators.Dialog.prototype.addLink = function() { + $('#toolbox').append('Resonators '); +} + +window.plugin.drawResonators.Dialog.prototype.addEntry = function(dialogEntry) { + this._dialogEntries[dialogEntry.name] = dialogEntry; +} + + +window.plugin.drawResonators.Dialog.prototype.show = function() { + window.dialog({html: this.getDialogHTML(), title: 'Resonators', modal: true, id: 'draw-reso-setting'}); + + // Attach entries event + for(var name in this._dialogEntries) { + var events = this._dialogEntries[name].getOnEvents(); + for(var i in events) { + var event = events[i]; + $('#draw-reso-dialog').on(event.event, '#' + event.id, event.callback); + } + } +} + +window.plugin.drawResonators.Dialog.prototype.getDialogHTML = function() { + var html = '