From ae0f95a0e1fe8b79c1f425266d0f97ee2dc6f2e4 Mon Sep 17 00:00:00 2001 From: Daniel B Date: Thu, 9 Sep 2021 13:03:26 -0500 Subject: [PATCH 1/3] 2021.09.09.01 --- WME-HN-NavPoints.js | 95 ++++++++++++++++++++++++++++++++++----------- 1 file changed, 72 insertions(+), 23 deletions(-) diff --git a/WME-HN-NavPoints.js b/WME-HN-NavPoints.js index 733dd26..1403aae 100644 --- a/WME-HN-NavPoints.js +++ b/WME-HN-NavPoints.js @@ -3,7 +3,7 @@ // @name WME HN NavPoints (beta) // @namespace https://greasyfork.org/users/166843 // @description Shows navigation points of all house numbers in WME -// @version 2021.08.30.01 +// @version 2021.09.09.01 // @author dBsooner // @grant none // @require https://greasyfork.org/scripts/24851-wazewrap/code/WazeWrap.js @@ -29,8 +29,8 @@ const ALERT_UPDATE = true, SCRIPT_NAME = GM_info.script.name.replace('(beta)', 'β'), SCRIPT_VERSION = GM_info.script.version, SCRIPT_VERSION_CHANGES = [ - 'CHANGE: Update zoom levels to new WME numbers.', - 'BUGFIX: Changed forum post URL.' + 'BUGFIX: HN and lines not clearing on first click.', + 'BUGFIX: HN and lines not added back if no changes made after deselection.' ], SETTINGS_STORE_NAME = 'WMEHNNavPoints', _spinners = { @@ -42,7 +42,12 @@ const ALERT_UPDATE = true, bootstrap: undefined, hideTooltip: undefined, saveSettingsToStorage: undefined, - setMarkersEvents: undefined + setMarkersEvents: undefined, + checkMarkersEvents: undefined + }, + _holdFeatures = { + hn: [], + lines: [] }; let _settings = {}, @@ -202,14 +207,14 @@ function doSpinner(spinnerName = '', spin = true) { } } -function processSegmentsToRemove() { +function processSegmentsToRemove(force = false) { if (_segmentsToRemove.length > 0) { const removeMarker = marker => { _HNNavPointsNumbersLayer.removeMarker(marker); }; let linesToRemove = [], hnsToRemove = []; for (let i = _segmentsToRemove.length - 1; i > -1; i--) { const segId = _segmentsToRemove[i]; - if (!W.model.segments.objects[segId]) { + if (!W.model.segments.objects[segId] || force) { _segmentsToRemove.splice(i, 1); linesToRemove = linesToRemove.concat(_HNNavPointsLayer.getFeaturesByAttribute('segmentId', segId)); if (!_settings.enableTooltip) @@ -256,7 +261,9 @@ async function hnNumbersLayerToggled(checked) { function observeHNLayer() { if (W.editingMediator.attributes.editingHouseNumbers && !_HNLayerObserver.observing) { [_wmeHnLayer] = W.map.getLayersByName('houseNumberMarkers'); - _HNLayerObserver.observe(_wmeHnLayer.div, { childList: false, subtree: true, attributes: true }); + _HNLayerObserver.observe(_wmeHnLayer.div, { + childList: false, subtree: true, attributes: true, attributeOldValue: true + }); _HNLayerObserver.observing = true; } else if (_HNLayerObserver.observing) { @@ -265,8 +272,11 @@ function observeHNLayer() { } if (!_HNLayerObserver.observing) { W.model.segmentHouseNumbers.clear(); + const holdSegmentsToRemove = [..._segmentsToRemove]; + _segmentsToRemove = _segmentsToRemove.concat([..._segmentsToProcess]); + processSegmentsToRemove(true); + _segmentsToRemove = [...holdSegmentsToRemove]; processSegs('exithousenumbers', W.model.segments.getByIds(_segmentsToProcess), true); - processSegmentsToRemove(); _wmeHnLayer = undefined; } else { @@ -279,15 +289,41 @@ function observeHNLayer() { }); } -function removeHNs(objArr) { +function flushHeldFeatures() { + if (_holdFeatures.hn.length === 0) + return; + if (_HNNavPointsLayer.getFeaturesByAttribute('featureId', _holdFeatures.hn[0].attributes.featureId).length === 0) { + if (_settings.enableTooltip) + _HNNavPointsNumbersLayer.addMarker(_holdFeatures.hn); + else + _HNNavPointsNumbersLayer.addFeatures(_holdFeatures.hn); + _HNNavPointsLayer.addFeatures(_holdFeatures.lines); + } + _holdFeatures.hn = []; + _holdFeatures.lines = []; +} + +function removeHNs(objArr, holdFeatures = false) { let linesToRemove = [], hnsToRemove = []; + if (_holdFeatures.hn.length > 0) + flushHeldFeatures(); objArr.forEach(hnObj => { linesToRemove = linesToRemove.concat(_HNNavPointsLayer.getFeaturesByAttribute('featureId', hnObj.attributes.id)); - if (!_settings.enableTooltip) + if (holdFeatures) + _holdFeatures.lines = _HNNavPointsLayer.getFeaturesByAttribute('featureId', hnObj.attributes.id); + if (!_settings.enableTooltip) { hnsToRemove = hnsToRemove.concat(_HNNavPointsNumbersLayer.getFeaturesByAttribute('featureId', hnObj.attributes.id)); - else - _HNNavPointsNumbersLayer.markers.filter(a => a.featureId === hnObj.attributes.id).forEach(marker => { _HNNavPointsNumbersLayer.removeMarker(marker); }); + if (holdFeatures) + _holdFeatures.hn = _HNNavPointsNumbersLayer.getFeaturesByAttribute('featureId', hnObj.attributes.id); + } + else { + _HNNavPointsNumbersLayer.markers.filter(a => a.featureId === hnObj.attributes.id).forEach(marker => { + if (holdFeatures) + _holdFeatures.hn = marker; + _HNNavPointsNumbersLayer.removeMarker(marker); + }); + } }); if (linesToRemove.length > 0) _HNNavPointsLayer.removeFeatures(linesToRemove); @@ -374,6 +410,10 @@ function drawHNs(houseNumberArr) { segmentId, featureId, hn_number: hnObj.getNumber(), strokeWidth: 3, Color: strokeColor, textDir })); } + if ((_holdFeatures.hn.length > 0) && (_holdFeatures.hn.map(a => a.attributes.featureId).indexOf(featureId) > -1)) { + _holdFeatures.hn = []; + _holdFeatures.lines = []; + } } } if (lineFeatures.length > 0) @@ -510,7 +550,7 @@ function markerEvent(evt) { return; if (evt.type === 'click:input') { if (evt.object && evt.object.dragging && !evt.object.dragging.last) - removeHNs([evt.object.model]); + removeHNs([evt.object.model], true); } else if (evt.type === 'delete') { removeHNs([evt.object.model]); @@ -539,9 +579,14 @@ function setMarkersEvents() { } } -function checkMarkersEvents() { +function checkMarkersEvents(retry = false, tries = 0) { + checkTimeout({ timeout: 'checkMarkersEvents' }); if (_wmeHnLayer && (_wmeHnLayer.markers.length > 0) && !_wmeHnLayer.markers[0].events.listeners['click:input'].some(callbackFn => callbackFn.func === markerEvent)) setMarkersEvents(); + else if (retry && (tries < 50)) + _timeouts.checkMarkersEvents = window.setTimeout(checkMarkersEvents, 100, true, ++tries); + else if (retry) + logError('Timeout (5 sec) exceeded waiting for markers to popuplate within checkMarkersEvents'); } function segmentsEvent(evt) { @@ -603,7 +648,7 @@ function objectsAddedHNs(evt) { return; if ((evt.length === 1) && evt[0].getSegmentId() && (_segmentsToProcess.indexOf(evt[0].getSegmentId()) === -1)) _segmentsToProcess.push(evt[0].getSegmentId()); - checkMarkersEvents(); + checkMarkersEvents(true, 0); } function zoomEndEvent() { @@ -625,7 +670,7 @@ function afterActionsEvent(evt) { if (evt.type === 'afterundoaction') drawHNs([evt.action.object]); else - removeHNs(evt.action.object); + removeHNs([evt.action.object]); setMarkersEvents(); } else if (evt.action._description && (evt.action._description.indexOf('Updated house number') > -1)) { @@ -638,13 +683,13 @@ function afterActionsEvent(evt) { if (tempEvt.action.oldAttributes && tempEvt.action.oldAttributes.number) tempEvt.action.attributes.number = tempEvt.action.oldAttributes.number; } - removeHNs(tempEvt.action.object); + removeHNs([tempEvt.action.object]); drawHNs([evt.action.object]); setMarkersEvents(); } else if (evt.action._description && (evt.action._description.indexOf('Added house number') > -1)) { if (evt.type === 'afterundoaction') - removeHNs(evt.action.houseNumber); + removeHNs([evt.action.houseNumber]); else drawHNs([evt.action.houseNumber]); } @@ -668,10 +713,14 @@ async function reloadClicked() { function initBackgroundTasks(status) { if (status === 'enable') { _HNLayerObserver = new MutationObserver(mutationsList => { - mutationsList.forEach(() => { - const input = $('div.olLayerDiv.house-numbers-layer div.house-number div.content.active:not(".new") input.number'); - if (input.val() === '') - input[0].addEventListener('change', setMarkersEvents); + mutationsList.forEach(mutation => { + if (mutation.type === 'attributes') { + if ((mutation.oldValue.indexOf('active') > -1) && (_holdFeatures.hn.length > 0) && ($('.active', _wmeHnLayer.div).length === 0)) + flushHeldFeatures(); + const input = $('div.olLayerDiv.house-numbers-layer div.house-number div.content.active:not(".new") input.number'); + if (input.val() === '') + input[0].addEventListener('change', setMarkersEvents); + } }); }); _saveButtonObserver = new MutationObserver(mutationsList => { @@ -1026,7 +1075,7 @@ function bootstrap(tries) { init(); } else if (tries < 1000) { - logDebug(`Bootstrap failed. Retrying ${tries} of 1000`); + logDebug(`WME not ready. Retrying ${tries} of 1000`); _timeouts.bootstrap = window.setTimeout(bootstrap, 200, ++tries); } else { From 18b521ab3c6e37be761e6e493a061d36c88458b5 Mon Sep 17 00:00:00 2001 From: Daniel B Date: Mon, 13 Sep 2021 12:01:57 -0500 Subject: [PATCH 2/3] 2021.09.13.01 --- WME-HN-NavPoints.js | 45 ++++++++++++++++++++++++++++++++++----------- 1 file changed, 34 insertions(+), 11 deletions(-) diff --git a/WME-HN-NavPoints.js b/WME-HN-NavPoints.js index 1403aae..a2e121b 100644 --- a/WME-HN-NavPoints.js +++ b/WME-HN-NavPoints.js @@ -3,7 +3,7 @@ // @name WME HN NavPoints (beta) // @namespace https://greasyfork.org/users/166843 // @description Shows navigation points of all house numbers in WME -// @version 2021.09.09.01 +// @version 2021.09.13.01 // @author dBsooner // @grant none // @require https://greasyfork.org/scripts/24851-wazewrap/code/WazeWrap.js @@ -29,6 +29,7 @@ const ALERT_UPDATE = true, SCRIPT_NAME = GM_info.script.name.replace('(beta)', 'β'), SCRIPT_VERSION = GM_info.script.version, SCRIPT_VERSION_CHANGES = [ + 'NEW: Auto-select input box when adding a new HN.', 'BUGFIX: HN and lines not clearing on first click.', 'BUGFIX: HN and lines not added back if no changes made after deselection.' ], @@ -555,9 +556,17 @@ function markerEvent(evt) { else if (evt.type === 'delete') { removeHNs([evt.object.model]); } + else if (evt.type === 'mousedown') { + if (evt.target.classList.contains('drag-handle') && evt.data && evt.data.marker && evt.data.marker.model) + removeHNs([evt.data.marker.model], true); + } + else if (evt.type === 'mouseup') { + if (evt.target.classList.contains('drag-handle') && (_holdFeatures.hn.length > 0)) + flushHeldFeatures(); + } } -function setMarkersEvents() { +function setMarkersEvents(reclick = false, targetNode = undefined) { if (W.editingMediator.attributes.editingHouseNumbers) { checkTimeout({ timeout: 'setMarkersEvents' }); hideTooltip(); @@ -569,7 +578,13 @@ function setMarkersEvents() { marker.events.unregister('click:input', null, markerEvent); marker.events.unregister('delete', null, markerEvent); marker.events.on({ 'click:input': markerEvent, delete: markerEvent }); + $('.drag-handle', marker.icon.div.children[0]).off('mousedown', { marker }, markerEvent).on('mousedown', { marker }, markerEvent); + $('.drag-handle', marker.icon.div.children[0]).off('mouseup', { marker }, markerEvent).on('mouseup', { marker }, markerEvent); }); + if (reclick) { + const tmpNode = $('input.number', targetNode)[0]; + $(tmpNode).selectRange(tmpNode.selectionStart).click(); + } } else if (_wmeHnLayer) { _wmeHnLayer.markers.forEach(marker => { @@ -579,14 +594,18 @@ function setMarkersEvents() { } } -function checkMarkersEvents(retry = false, tries = 0) { +function checkMarkersEvents(retry = false, tries = 0, reclick, targetNode) { checkTimeout({ timeout: 'checkMarkersEvents' }); - if (_wmeHnLayer && (_wmeHnLayer.markers.length > 0) && !_wmeHnLayer.markers[0].events.listeners['click:input'].some(callbackFn => callbackFn.func === markerEvent)) - setMarkersEvents(); - else if (retry && (tries < 50)) - _timeouts.checkMarkersEvents = window.setTimeout(checkMarkersEvents, 100, true, ++tries); - else if (retry) + if (_wmeHnLayer && (_wmeHnLayer.markers.length > 0)) { + if (!_wmeHnLayer.markers[0].events.listeners['click:input'].some(callbackFn => callbackFn.func === markerEvent)) + setMarkersEvents(reclick, targetNode); + } + else if (retry && (tries < 50)) { + _timeouts.checkMarkersEvents = window.setTimeout(checkMarkersEvents, 100, true, ++tries, reclick, targetNode); + } + else if (retry) { logError('Timeout (5 sec) exceeded waiting for markers to popuplate within checkMarkersEvents'); + } } function segmentsEvent(evt) { @@ -717,9 +736,13 @@ function initBackgroundTasks(status) { if (mutation.type === 'attributes') { if ((mutation.oldValue.indexOf('active') > -1) && (_holdFeatures.hn.length > 0) && ($('.active', _wmeHnLayer.div).length === 0)) flushHeldFeatures(); - const input = $('div.olLayerDiv.house-numbers-layer div.house-number div.content.active:not(".new") input.number'); - if (input.val() === '') - input[0].addEventListener('change', setMarkersEvents); + if ((mutation.oldValue.indexOf('active') === -1) && mutation.target.classList.contains('active')) + checkMarkersEvents(true, 0, true, mutation.target); + const $input = $('div.olLayerDiv.house-numbers-layer div.house-number div.content.active:not(".new") input.number'); + if ($input.val() === '') { + $input[0].addEventListener('change', setMarkersEvents); + $input.select(); + } } }); }); From a33c43a6d43580c88169ac243a7d15b7c8e4f18b Mon Sep 17 00:00:00 2001 From: Daniel B Date: Tue, 14 Sep 2021 16:43:42 -0500 Subject: [PATCH 3/3] 2021.09.14.01 --- WME-HN-NavPoints.js | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/WME-HN-NavPoints.js b/WME-HN-NavPoints.js index a2e121b..ec3a213 100644 --- a/WME-HN-NavPoints.js +++ b/WME-HN-NavPoints.js @@ -3,7 +3,7 @@ // @name WME HN NavPoints (beta) // @namespace https://greasyfork.org/users/166843 // @description Shows navigation points of all house numbers in WME -// @version 2021.09.13.01 +// @version 2021.09.14.01 // @author dBsooner // @grant none // @require https://greasyfork.org/scripts/24851-wazewrap/code/WazeWrap.js @@ -571,7 +571,7 @@ function setMarkersEvents(reclick = false, targetNode = undefined) { checkTimeout({ timeout: 'setMarkersEvents' }); hideTooltip(); if (!_wmeHnLayer || (_wmeHnLayer && (_wmeHnLayer.markers.length === 0))) { - _timeouts.setMarkersEvents = window.setTimeout(setMarkersEvents, 50); + _timeouts.setMarkersEvents = window.setTimeout(setMarkersEvents, 50, reclick, targetNode); return; } _wmeHnLayer.markers.forEach(marker => { @@ -739,10 +739,8 @@ function initBackgroundTasks(status) { if ((mutation.oldValue.indexOf('active') === -1) && mutation.target.classList.contains('active')) checkMarkersEvents(true, 0, true, mutation.target); const $input = $('div.olLayerDiv.house-numbers-layer div.house-number div.content.active:not(".new") input.number'); - if ($input.val() === '') { - $input[0].addEventListener('change', setMarkersEvents); - $input.select(); - } + if ($input.val() === '') + $input.on('change', () => { setMarkersEvents(); }).select(); } }); });