aratcliffe / Leaflet.contextmenu

A context menu for Leaflet.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Problem with Electron

heretic13 opened this issue · comments

Hi.

I create some applications with Electron. Now I have a problem with "Leaflet.contextmenu".
I'ts not working.
I began investigating and found a bug place:

(function(factory) { // Packaging/modules magic dance var L; if (typeof define === 'function' && define.amd) { // AMD define(['leaflet'], factory); } else if (typeof module === 'object' && typeof module.exports === 'object') { // Node/CommonJS L = require('leaflet'); module.exports = factory(L); } else { // Browser globals if (typeof window.L === 'undefined') { throw new Error('Leaflet must be loaded first'); } factory(window.L); } })

The fact is that Electron supports the require method on web pages(since it is based on Node.js). But this does not mean that it is necessary to load the leaflet module.

Here I see two nuances:

  1. If I wanted to write an Electron application, I could probably use the leaflet module (I have never done that and I don’t know if it will work) instead of explicitly registering the link to leaflet.js on my html page. But I think it is necessary to give the user a choice of how to act.
  2. A more interesting case is when Electron loads a web page from a site and the page uses leaflet and Leaflet.contextmenu. You cannot change the page content and should not force Electron to load the leaflet module. You must use a leaflet.js that is explicitly registered on the page.

I do not know the right decision.

  • It may be worth refusing to explicitly require ('leaflet').
  • Perhaps it is worth making a more intelligent determination of the presence of a loaded leaflet on the page.
  • Perhaps it is worth checking that the modules are supported and the type of browser - Electron (but I do not know how this will help).

Solution is simple:

comment this:
/*
else if (typeof module === 'object' && typeof module.exports === 'object') {
// Node/CommonJS
L = require('leaflet');
module.exports = factory(L);
}
*/

Please anybody make a pull request.

Some advices from me to community:

  1. leaflet as a module - very bad idea. It has limited scope - only browsers that support modules and agree it.
  2. autoload 'leaflet as a module' for leaflet.contextmenu - is a still a big bad idea. LOAD LEAFLET MANUALLY before load leaflet.contextmenu.js !!!!