From 2e92e12b4935076ba17976aeadb990d7d941cb72 Mon Sep 17 00:00:00 2001 From: Mike Castle Date: Sun, 31 Mar 2024 20:53:41 -0700 Subject: [PATCH 1/6] Factor out code common to bookmarking a specific portal. --- plugins/bookmarks.js | 52 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/plugins/bookmarks.js b/plugins/bookmarks.js index 0bd4f0751..75310cdab 100644 --- a/plugins/bookmarks.js +++ b/plugins/bookmarks.js @@ -374,6 +374,58 @@ window.plugin.bookmarks.loadStorageBox = function() { } } + /** + * Adds a portal to the default bookmark folder. + * + * @param {L.circleMarker} marker - As enhanced when added to + * window.portals. + * @param {boolean} doPostProcess - Whether additional post processing + * should be done after the bookmark was added. E.g., saving to local + * storage, refreshing the widget, and running hooks. If part of a batch + * update, this should probably be false. + */ + window.plugin.bookmarks.addPortalBookmarkByMarker = function(marker, doPostProcess) { + const guid = marker.options.guid; + const label = marker.options.data.title; + const ll = marker.getLatLng(); + const latlng = `${ll.lat},${ll.lng}`; + const ID = window.plugin.bookmarks.generateID(); + + window.plugin.bookmarks.bkmrksObj['portals'][window.plugin.bookmarks.KEY_OTHER_BKMRK]['bkmrk'][ID] = { + guid: guid, + latlng: latlng, + label: label, + }; + + if (doPostProcess) { + window.plugin.bookmarks.saveStorage(); + window.plugin.bookmarks.refreshBkmrks(); + window.runHooks('pluginBkmrksEdit', { + target: 'portal', + action: 'add', + id: ID, + guid: guid, + }); + console.log(`BOOKMARKS: added portal ${ID}`); + } + } + + /** + * Adds a portal to the default bookmark folder. + * + * @param {string} guid - The GUID of the portal. + * @param {boolean} doPostProcess - Whether additional post processing + * should be done after the bookmark was added. E.g., saving to local + * storage, refreshing the widget, and running hooks. If part of a batch + * update, this should probably be false. + */ + window.plugin.bookmarks.addPortalBookmarkByGuid = function(guid, doPostProcess) { + const marker = window.portals[guid]; + if (marker) { + window.plugin.bookmarks.addPortalBookmarkByMarker(marker, doPostProcess); + } + } + plugin.bookmarks.addPortalBookmark = function(guid, latlng, label) { var ID = window.plugin.bookmarks.generateID(); From b5845f0038de3dcd9de591cb2bb2be91bbd218e7 Mon Sep 17 00:00:00 2001 From: Mike Castle Date: Tue, 9 Apr 2024 13:47:02 -0700 Subject: [PATCH 2/6] Migrate to new functions that set the portal details for bookmarks. --- plugins/bookmarks.js | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/plugins/bookmarks.js b/plugins/bookmarks.js index 75310cdab..58fe5601a 100644 --- a/plugins/bookmarks.js +++ b/plugins/bookmarks.js @@ -367,10 +367,7 @@ window.plugin.bookmarks.loadStorageBox = function() { } // If portal isn't saved in bookmarks: Add this bookmark else{ - // Get portal name and coordinates - var p = window.portals[guid]; - var ll = p.getLatLng(); - plugin.bookmarks.addPortalBookmark(guid, ll.lat+','+ll.lng, p.options.data.title); + window.plugin.bookmarks.addPortalBookmarkByGuid(guid, true); } } @@ -1239,8 +1236,7 @@ window.plugin.bookmarks.loadStorageBox = function() { if(window.plugin.bookmarks.findByGuid(guid)) { window.plugin.bookmarks.switchStarPortal(guid); } else { - var ll = portal.getLatLng(); - plugin.bookmarks.addPortalBookmark(guid, ll.lat+','+ll.lng, portal.options.data.title); + window.plugin.bookmarks.addPortalBookmarkByGuid(guid, true); } }, false); From 2d838a36f1254fdc4d4394366040f108e2fad66c Mon Sep 17 00:00:00 2001 From: Mike Castle Date: Tue, 9 Apr 2024 15:19:24 -0700 Subject: [PATCH 3/6] Add jsdoc and mark older function as deprecated. --- plugins/bookmarks.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/plugins/bookmarks.js b/plugins/bookmarks.js index 58fe5601a..2d39d55f3 100644 --- a/plugins/bookmarks.js +++ b/plugins/bookmarks.js @@ -423,6 +423,19 @@ window.plugin.bookmarks.loadStorageBox = function() { } } + /** + * Adds a portal to the default bookmark folder. + * + * The window.plugin.bookmarks.addPortalBookmarkBy{Guid,Marker}() functions + * should be used for new code. + * + * @deprecated + * @param {string} guid - The GUID of the portal. + * @param {string} latlng - 'lat,lng' for the portal. + * @param {string} label - The title of the portal. Typically this is the + * same value as the options.data.title property from the appropriate + * window.portals entry, though nothing enforces this. + */ plugin.bookmarks.addPortalBookmark = function(guid, latlng, label) { var ID = window.plugin.bookmarks.generateID(); From 9aa7f706bb92c43c018aa49bebb8d9d7e54c266f Mon Sep 17 00:00:00 2001 From: Mike Castle Date: Sun, 14 Apr 2024 11:46:38 -0700 Subject: [PATCH 4/6] Switch *Portals list* to use `addPortalBookmarkByMarker()`. --- plugins/bookmarks.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/bookmarks.js b/plugins/bookmarks.js index 2d39d55f3..b12b254d2 100644 --- a/plugins/bookmarks.js +++ b/plugins/bookmarks.js @@ -1249,7 +1249,7 @@ window.plugin.bookmarks.loadStorageBox = function() { if(window.plugin.bookmarks.findByGuid(guid)) { window.plugin.bookmarks.switchStarPortal(guid); } else { - window.plugin.bookmarks.addPortalBookmarkByGuid(guid, true); + window.plugin.bookmarks.addPortalBookmarkByMarker(portal, true); } }, false); From 53a5dc1eaa5c670c8d1745d8ab3b4d205522d444 Mon Sep 17 00:00:00 2001 From: Mike Castle Date: Sun, 14 Apr 2024 12:02:10 -0700 Subject: [PATCH 5/6] Throw `Error` if guid could not be looked up. --- plugins/bookmarks.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/plugins/bookmarks.js b/plugins/bookmarks.js index b12b254d2..80c9bdb68 100644 --- a/plugins/bookmarks.js +++ b/plugins/bookmarks.js @@ -415,11 +415,14 @@ window.plugin.bookmarks.loadStorageBox = function() { * should be done after the bookmark was added. E.g., saving to local * storage, refreshing the widget, and running hooks. If part of a batch * update, this should probably be false. + * @throws {Error} - If guid does not exist in window.portals. */ window.plugin.bookmarks.addPortalBookmarkByGuid = function(guid, doPostProcess) { const marker = window.portals[guid]; if (marker) { window.plugin.bookmarks.addPortalBookmarkByMarker(marker, doPostProcess); + } else { + throw new Error(`Could not find portal information for guid "${guid}"`); } } From fad6e625875ad138b5d2ed6698b97d2e67c1c552 Mon Sep 17 00:00:00 2001 From: Alexander Danilov Date: Thu, 18 Apr 2024 11:05:26 +0300 Subject: [PATCH 6/6] Fix eslint --- plugins/bookmarks.js | 156 +++++++++++++++++++++---------------------- 1 file changed, 78 insertions(+), 78 deletions(-) diff --git a/plugins/bookmarks.js b/plugins/bookmarks.js index 80c9bdb68..51ac6b4ed 100644 --- a/plugins/bookmarks.js +++ b/plugins/bookmarks.js @@ -367,89 +367,89 @@ window.plugin.bookmarks.loadStorageBox = function() { } // If portal isn't saved in bookmarks: Add this bookmark else{ - window.plugin.bookmarks.addPortalBookmarkByGuid(guid, true); + window.plugin.bookmarks.addPortalBookmarkByGuid(guid, true); } } - /** - * Adds a portal to the default bookmark folder. - * - * @param {L.circleMarker} marker - As enhanced when added to - * window.portals. - * @param {boolean} doPostProcess - Whether additional post processing - * should be done after the bookmark was added. E.g., saving to local - * storage, refreshing the widget, and running hooks. If part of a batch - * update, this should probably be false. - */ - window.plugin.bookmarks.addPortalBookmarkByMarker = function(marker, doPostProcess) { - const guid = marker.options.guid; - const label = marker.options.data.title; - const ll = marker.getLatLng(); - const latlng = `${ll.lat},${ll.lng}`; - const ID = window.plugin.bookmarks.generateID(); - - window.plugin.bookmarks.bkmrksObj['portals'][window.plugin.bookmarks.KEY_OTHER_BKMRK]['bkmrk'][ID] = { - guid: guid, - latlng: latlng, - label: label, - }; - - if (doPostProcess) { - window.plugin.bookmarks.saveStorage(); - window.plugin.bookmarks.refreshBkmrks(); - window.runHooks('pluginBkmrksEdit', { - target: 'portal', - action: 'add', - id: ID, - guid: guid, - }); - console.log(`BOOKMARKS: added portal ${ID}`); - } - } - - /** - * Adds a portal to the default bookmark folder. - * - * @param {string} guid - The GUID of the portal. - * @param {boolean} doPostProcess - Whether additional post processing - * should be done after the bookmark was added. E.g., saving to local - * storage, refreshing the widget, and running hooks. If part of a batch - * update, this should probably be false. - * @throws {Error} - If guid does not exist in window.portals. - */ - window.plugin.bookmarks.addPortalBookmarkByGuid = function(guid, doPostProcess) { - const marker = window.portals[guid]; - if (marker) { - window.plugin.bookmarks.addPortalBookmarkByMarker(marker, doPostProcess); - } else { - throw new Error(`Could not find portal information for guid "${guid}"`); - } - } - - /** - * Adds a portal to the default bookmark folder. - * - * The window.plugin.bookmarks.addPortalBookmarkBy{Guid,Marker}() functions - * should be used for new code. - * - * @deprecated - * @param {string} guid - The GUID of the portal. - * @param {string} latlng - 'lat,lng' for the portal. - * @param {string} label - The title of the portal. Typically this is the - * same value as the options.data.title property from the appropriate - * window.portals entry, though nothing enforces this. - */ - plugin.bookmarks.addPortalBookmark = function(guid, latlng, label) { - var ID = window.plugin.bookmarks.generateID(); - - // Add bookmark in the localStorage - window.plugin.bookmarks.bkmrksObj['portals'][window.plugin.bookmarks.KEY_OTHER_BKMRK]['bkmrk'][ID] = {"guid":guid,"latlng":latlng,"label":label}; +/** + * Adds a portal to the default bookmark folder. + * + * @param {L.circleMarker} marker - As enhanced when added to + * window.portals. + * @param {boolean} doPostProcess - Whether additional post-processing + * should be done after the bookmark was added. E.g., saving to local + * storage, refreshing the widget, and running hooks. If part of a batch + * update, this should probably be false. + */ +window.plugin.bookmarks.addPortalBookmarkByMarker = function (marker, doPostProcess) { + const guid = marker.options.guid; + const label = marker.options.data.title; + const ll = marker.getLatLng(); + const latlng = `${ll.lat},${ll.lng}`; + const ID = window.plugin.bookmarks.generateID(); + + window.plugin.bookmarks.bkmrksObj['portals'][window.plugin.bookmarks.KEY_OTHER_BKMRK]['bkmrk'][ID] = { + guid: guid, + latlng: latlng, + label: label, + }; + if (doPostProcess) { window.plugin.bookmarks.saveStorage(); window.plugin.bookmarks.refreshBkmrks(); - window.runHooks('pluginBkmrksEdit', {"target": "portal", "action": "add", "id": ID, "guid": guid}); - console.log('BOOKMARKS: added portal '+ID); - } + window.runHooks('pluginBkmrksEdit', { + target: 'portal', + action: 'add', + id: ID, + guid: guid, + }); + console.log(`BOOKMARKS: added portal ${ID}`); + } +}; + +/** + * Adds a portal to the default bookmark folder. + * + * @param {string} guid - The GUID of the portal. + * @param {boolean} doPostProcess - Whether additional post processing + * should be done after the bookmark was added. E.g., saving to local + * storage, refreshing the widget, and running hooks. If part of a batch + * update, this should probably be false. + * @throws {Error} - If guid does not exist in window.portals. + */ +window.plugin.bookmarks.addPortalBookmarkByGuid = function (guid, doPostProcess) { + const marker = window.portals[guid]; + if (marker) { + window.plugin.bookmarks.addPortalBookmarkByMarker(marker, doPostProcess); + } else { + throw new Error(`Could not find portal information for guid "${guid}"`); + } +}; + +/** + * Adds a portal to the default bookmark folder. + * + * The window.plugin.bookmarks.addPortalBookmarkBy{Guid,Marker}() functions + * should be used for new code. + * + * @deprecated + * @param {string} guid - The GUID of the portal. + * @param {string} latlng - 'lat,lng' for the portal. + * @param {string} label - The title of the portal. Typically this is the + * same value as the options.data.title property from the appropriate + * window.portals entry, though nothing enforces this. + */ +window.plugin.bookmarks.addPortalBookmark = function (guid, latlng, label) { + var ID = window.plugin.bookmarks.generateID(); + + // Add bookmark in the localStorage + window.plugin.bookmarks.bkmrksObj['portals'][window.plugin.bookmarks.KEY_OTHER_BKMRK]['bkmrk'][ID] = { guid: guid, latlng: latlng, label: label }; + + window.plugin.bookmarks.saveStorage(); + window.plugin.bookmarks.refreshBkmrks(); + window.runHooks('pluginBkmrksEdit', { target: 'portal', action: 'add', id: ID, guid: guid }); + console.log('BOOKMARKS: added portal ' + ID); +}; // Add BOOKMARK/FOLDER window.plugin.bookmarks.addElement = function(elem, type) { @@ -1252,7 +1252,7 @@ window.plugin.bookmarks.loadStorageBox = function() { if(window.plugin.bookmarks.findByGuid(guid)) { window.plugin.bookmarks.switchStarPortal(guid); } else { - window.plugin.bookmarks.addPortalBookmarkByMarker(portal, true); + window.plugin.bookmarks.addPortalBookmarkByMarker(portal, true); } }, false);