Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bookmarks refactor: simplying API for adding a new portal #725

Merged
merged 6 commits into from
Apr 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 77 additions & 13 deletions plugins/bookmarks.js
Original file line number Diff line number Diff line change
Expand Up @@ -367,24 +367,89 @@ 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);
}
}

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) {
Expand Down Expand Up @@ -1187,8 +1252,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.addPortalBookmarkByMarker(portal, true);
}
}, false);

Expand Down
Loading