[Pkg-javascript-commits] [leaflet-contextmenu] 01/05: New upstream version 1.4.0+dfsg

Dominik George natureshadow-guest at moszumanska.debian.org
Tue Mar 14 22:51:44 UTC 2017


This is an automated email from the git hooks/post-receive script.

natureshadow-guest pushed a commit to branch master
in repository leaflet-contextmenu.

commit c7427c44e2220ac336a7fc8fae20e3c05dd893f1
Author: Dominik George <nik at naturalnet.de>
Date:   Tue Mar 14 23:45:29 2017 +0100

    New upstream version 1.4.0+dfsg
---
 .editorconfig                |  11 ++
 .gitignore                   |   2 +
 LICENSE.md                   |  10 +
 README.md                    | 236 ++++++++++++++++++++++
 bower.json                   |  24 +++
 dist/leaflet.contextmenu.css |  54 ++++++
 package.json                 |  35 ++++
 src/Map.ContextMenu.js       | 451 +++++++++++++++++++++++++++++++++++++++++++
 src/Mixin.ContextMenu.js     | 108 +++++++++++
 src/copyright.js             |  25 +++
 src/end.js                   |   2 +
 11 files changed, 958 insertions(+)

diff --git a/.editorconfig b/.editorconfig
new file mode 100644
index 0000000..36e05d9
--- /dev/null
+++ b/.editorconfig
@@ -0,0 +1,11 @@
+[*]
+charset = utf-8
+trim_trailing_whitespace = true
+insert_final_newline = true
+
+[{.,}*.{js{,*},y{a,}ml}]
+indent_style = tab
+indent_size = 2
+
+[Makefile,*.{less,css}]
+indent_style = tab
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..3c624d5
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+node_modules
+*.DS_Store
\ No newline at end of file
diff --git a/LICENSE.md b/LICENSE.md
new file mode 100644
index 0000000..e4c421a
--- /dev/null
+++ b/LICENSE.md
@@ -0,0 +1,10 @@
+The MIT License (MIT)
+--
+
+Copyright (c) 2017 adam.ratcliffe at gmail.com
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..f93727b
--- /dev/null
+++ b/README.md
@@ -0,0 +1,236 @@
+#Leaflet.contextmenu
+====================
+[![CDNJS](https://img.shields.io/cdnjs/v/leaflet-contextmenu.svg)](https://cdnjs.com/libraries/leaflet-contextmenu)
+[![npm](https://img.shields.io/npm/v/leaflet-contextmenu.svg)](https://www.npmjs.com/package/leaflet-contextmenu)
+[![Bower](https://img.shields.io/bower/v/leaflet.contextmenu.svg)](https://libraries.io/bower/Leaflet.contextmenu)
+
+A context menu for Leaflet. See the [demo](http://aratcliffe.github.io/Leaflet.contextmenu/examples/index.html).
+
+Now supporting Leaflet 1.0
+
+##Usage
+The context menu is implemented as a map interaction handler.  To use the plugin include the script and enable using the map `contextmenu` option.
+
+````javascript
+var map = L.map('map', {
+	contextmenu: true,
+    contextmenuWidth: 140,
+	contextmenuItems: [{
+	    text: 'Show coordinates',
+	    callback: showCoordinates
+	}, {
+	    text: 'Center map here',
+	    callback: centerMap
+	}, '-', {
+	    text: 'Zoom in',
+	    icon: 'images/zoom-in.png',
+	    callback: zoomIn
+	}, {
+	    text: 'Zoom out',
+	    icon: 'images/zoom-out.png',
+	    callback: zoomOut
+	}]
+});
+
+
+function showCoordinates (e) {
+	alert(e.latlng);
+}
+
+function centerMap (e) {
+	map.panTo(e.latlng);
+}
+
+function zoomIn (e) {
+	map.zoomIn();
+}
+
+function zoomOut (e) {
+	map.zoomOut();
+}
+````
+
+The context menu mixin allows markers and vector features to extend the map context menu with their own menu items. In addition to the menu item options available for the map context menu marker's also accept an `index` option that specifies where the menu item should be inserted relative to the existing map menu items.
+
+````javascript
+L.marker(ll, {
+    contextmenu: true,
+    contextmenuItems: [{
+        text: 'Marker item',
+        index: 0
+    }, {
+        separator: true,
+        index: 1
+    }]
+}).addTo(map);
+````
+
+###All Options
+####Map Context Menu Options
+
+| Option | Type | Default | Description
+| --- | --- | --- | ---
+| contextmenu | Bool | `false` | Enables the context menu.
+| contextmenuWidth | Number | `undefined` | If defined sets the context menu width, if `undefined` the menu will be sized by the maximum width of its menu items.
+| contextmenuAnchor | L.Point/Array | `undefined` | An offset applied to the click point to control the context menu position.
+| contextmenuItems | Array | `[]` | Specification for the context menu items. See following options for individual menu items. A separator may also be added with a dash character `'-'`.
+
+####Menu Item Options
+
+| Option | Type | Default | Description
+| --- | --- | --- | ---
+| text | String | `undefined` | The label to use for the menu item (required).
+| icon | String | `undefined` | Url for a 16x16px icon to display to the left of the label.
+| retinaIcon | String | `undefined` | Url for a retina-sized version (32x32px) icon to display to the left of the label.
+| iconCls | String | `undefined` | A CSS class which sets the background image for the icon (exclusive of the `icon` option).
+| retinaIconCls | String | `undefined` | A CSS class which sets the background image for a retina version of the icon (exclusive of the `retinaIcon` option).
+| callback | Function | `undefined` | A callback function to be invoked when the menu item is clicked. The callback is passed an object with properties identifying the location the menu was opened at: `latlng`, `layerPoint` and `containerPoint`.
+| context | Object | The map | The scope the callback will be executed in.
+| disabled | Bool | `false` | If `true` the menu item will initially be in a disabled state and will not respond to click events.
+| separator | Bool | `undefined` | If `true` a separator will be created instead of a menu item.
+| hideOnSelect | Bool | `true` | If `true` the context menu will be automatically hidden when a menu item is selected
+
+####Mixin Options
+
+| Option | Type | Default | Description
+| --- | --- | --- | ---
+| contextmenu | Bool | `false` | Enables the context menu.
+| contextmenuItems | Array | `[]` | Specification for the context menu items.
+| contextmenuInheritItems | Bool | `true` | If `true` (the default) the feature menu items are displayed in addition to the map's context menu items.
+
+
+###Methods
+
+A reference to the map's context menu can be obtained through the map variable e.g. `map.contextmenu`.
+
+````javascript
+showAt(L.Point/L.LatLng, [data])
+````
+Opens the map's context menu at the specified point. `data` is an optional hash of key/value pairs that will be included on the map's `contextmenu.show` event.
+
+````javascript
+hide()
+````
+Hides the map's context menu if showing.
+
+````javascript
+addItem(options)
+````
+Adds a new menu item to the context menu.
+
+````javascript
+insertItem(options, index)
+````
+Adds a new menu item to the context menu at the specified index. If the index is invalid the menu item will be appended to the menu.
+
+````javascript
+removeItem(HTMLElement/index)
+````
+Removes a menu item.
+
+````javascript
+removeAllItems()
+````
+Removes all menu items.
+
+````javascript
+setDisabled(HTMLElement/index, disabled)
+````
+Set's the disabled state of a menu item.
+
+````javascript
+isVisible()
+````
+Returns `true` if the context menu is currently visible.
+
+###Mixin Methods
+The following methods are available on supported layer types when using the context menu mixin.
+
+````javascript
+bindContextMenu(contextMenuOptions)
+````
+Binds a context menu to the feature the method is called on.
+
+````javascript
+unbindContextMenu()
+````
+Unbinds the context menu previously bound to the feature with the bindContextMenu() method.
+
+###Events
+
+The following events are triggered on the map:
+
+####contextmenu.show
+
+Fired when the context menu is shown. If the context menu was shown in response to a map `contextmenu` event the event object will extend [MouseEvent](http://leafletjs.com/reference.html#mouse-event).
+
+| Property | Type | Description
+| --- | --- | ---
+| contextmenu | Map.ContextMenu | The context menu.
+| relatedTarget | L.Marker/L.Path/undefined | If the context menu was opened for a map feature this property will contain a reference to that feature.
+
+####contextmenu.hide
+
+Fired when the context menu is hidden.
+
+| Property | Type | Description
+| --- | --- | ---
+| contextmenu | Map.ContextMenu | The context menu.
+
+####contextmenu.select
+
+Fired when a context menu item is selected.
+
+| Property | Type | Description
+| --- | --- | ---
+| contextmenu | Map.ContextMenu | The context menu.
+| el | HTMLElement | The context menu item element that was selected.
+
+####contextmenu.additem
+
+Fired when a menu item is added to the context menu.
+
+| Property | Type | Description
+| --- | --- | ---
+| contextmenu | Map.ContextMenu | The context menu.
+| el | HTMLElement | The context menu item element.
+| index | Number | The index at which the menu item was added.
+
+####contextmenu.removeitem
+
+Fired when a menu item is removed from the context menu.
+
+| Property | Type | Description
+| --- | --- | ---
+| contextmenu | Map.ContextMenu | The context menu.
+| el | HTMLElement | The context menu item element.
+
+####contextmenu.enableitem
+
+Fired when a menu item is enabled.
+
+| Property | Type | Description
+| --- | --- | ---
+| contextmenu | Map.ContextMenu | The context menu.
+| el | HTMLElement | The context menu item element.
+
+####contextmenu.disableitem
+
+Fired when a menu item is disabled.
+
+| Property | Type | Description
+| --- | --- | ---
+| contextmenu | Map.ContextMenu | The context menu.
+| el | HTMLElement | The context menu item element.
+
+##Development
+
+Edit files in `src/`. To build the files in `dist/`, run:
+
+````shell
+npm install
+npm run build
+````
+
+##License
+This software is released under the [MIT licence](http://www.opensource.org/licenses/mit-license.php). Icons used in the example are from [http://glyphicons.com](http://glyphicons.com).
diff --git a/bower.json b/bower.json
new file mode 100644
index 0000000..2576cee
--- /dev/null
+++ b/bower.json
@@ -0,0 +1,24 @@
+{
+  "name": "leaflet.contextmenu",
+  "homepage": "https://github.com/aratcliffe/Leaflet.contextmenu",
+  "description": "A context menu for Leaflet",
+  "main": "dist/leaflet.contextmenu.js",
+  "keywords": [
+    "gis"
+  ],
+  "license": "MIT",
+  "ignore": [
+    "src",
+    "examples",
+    "package.json",
+    "bower.json",
+    ".DS_Store",
+    ".git",
+    ".gitingore",
+    ".editorconfig",
+    "README.md"
+  ],
+  "dependencies": {
+      "leaflet": "1.x"
+  }
+}
diff --git a/dist/leaflet.contextmenu.css b/dist/leaflet.contextmenu.css
new file mode 100644
index 0000000..0b5e2de
--- /dev/null
+++ b/dist/leaflet.contextmenu.css
@@ -0,0 +1,54 @@
+.leaflet-contextmenu {
+    display: none;
+    box-shadow: 0 1px 7px rgba(0,0,0,0.4);
+    -webkit-border-radius: 4px;
+    border-radius: 4px;
+    padding: 4px 0;
+    background-color: #fff;
+    cursor: default;
+    -webkit-user-select: none;
+    -moz-user-select: none;
+    user-select: none;
+}
+
+.leaflet-contextmenu a.leaflet-contextmenu-item {
+    display: block;
+    color: #222;
+    font-size: 12px;
+    line-height: 20px;
+    text-decoration: none;
+    padding: 0 12px;
+    border-top: 1px solid transparent;
+    border-bottom: 1px solid transparent;
+    cursor: default;
+    outline: none;
+}
+
+.leaflet-contextmenu a.leaflet-contextmenu-item-disabled {
+    opacity: 0.5;
+}
+
+.leaflet-contextmenu a.leaflet-contextmenu-item.over {
+    background-color: #f4f4f4;
+    border-top: 1px solid #f0f0f0;
+    border-bottom: 1px solid #f0f0f0;
+}
+
+.leaflet-contextmenu a.leaflet-contextmenu-item-disabled.over {
+    background-color: inherit;
+    border-top: 1px solid transparent;
+    border-bottom: 1px solid transparent;
+}
+
+.leaflet-contextmenu-icon {
+    margin: 2px 8px 0 0;
+    width: 16px;
+    height: 16px;
+    float: left;
+    border: 0;
+}
+
+.leaflet-contextmenu-separator {
+    border-bottom: 1px solid #ccc;
+    margin: 5px 0;
+}
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..81bd14f
--- /dev/null
+++ b/package.json
@@ -0,0 +1,35 @@
+{
+  "name": "leaflet-contextmenu",
+  "version": "1.4.0",
+  "description": "A context menu for Leaflet",
+  "main": "dist/leaflet.contextmenu.js",
+  "directories": {
+    "example": "examples"
+  },
+  "scripts": {
+    "concat": "cat src/copyright.js src/Map.ContextMenu.js src/Mixin.ContextMenu.js src/end.js > dist/leaflet.contextmenu.js",
+    "compress": "node_modules/uglify-js/bin/uglifyjs dist/leaflet.contextmenu.js -m --comments -o dist/leaflet.contextmenu.min.js; node_modules/uglifycss/uglifycss dist/leaflet.contextmenu.css > dist/leaflet.contextmenu.min.css",
+    "build": "npm run concat && npm run compress"
+  },
+  "repository": {
+    "type": "git",
+    "url": "https://github.com/aratcliffe/Leaflet.contextmenu.git"
+  },
+  "keywords": [
+    "leaflet",
+    "contextmenu",
+    "mixin",
+    "context",
+    "menu"
+  ],
+  "author": "Adam Ratcliffe",
+  "license": "MIT",
+  "bugs": {
+    "url": "https://github.com/aratcliffe/Leaflet.contextmenu/issues"
+  },
+  "homepage": "https://github.com/aratcliffe/Leaflet.contextmenu",
+  "devDependencies": {
+    "uglify-js": "^2.7.4",
+    "uglifycss": "^0.0.25"
+  }
+}
diff --git a/src/Map.ContextMenu.js b/src/Map.ContextMenu.js
new file mode 100644
index 0000000..65b4f1c
--- /dev/null
+++ b/src/Map.ContextMenu.js
@@ -0,0 +1,451 @@
+L.Map.mergeOptions({
+    contextmenuItems: []
+});
+
+L.Map.ContextMenu = L.Handler.extend({
+    _touchstart: L.Browser.msPointer ? 'MSPointerDown' : L.Browser.pointer ? 'pointerdown' : 'touchstart',
+    
+    statics: {
+        BASE_CLS: 'leaflet-contextmenu'
+    },
+    
+    initialize: function (map) {
+        L.Handler.prototype.initialize.call(this, map);
+        
+        this._items = [];
+        this._visible = false;
+
+        var container = this._container = L.DomUtil.create('div', L.Map.ContextMenu.BASE_CLS, map._container);
+        container.style.zIndex = 10000;
+        container.style.position = 'absolute';
+
+        if (map.options.contextmenuWidth) {
+            container.style.width = map.options.contextmenuWidth + 'px';
+        }
+
+        this._createItems();
+
+        L.DomEvent
+            .on(container, 'click', L.DomEvent.stop)
+            .on(container, 'mousedown', L.DomEvent.stop)
+            .on(container, 'dblclick', L.DomEvent.stop)
+            .on(container, 'contextmenu', L.DomEvent.stop);
+    },
+
+    addHooks: function () {
+        var container = this._map.getContainer();
+
+        L.DomEvent
+            .on(container, 'mouseleave', this._hide, this)
+            .on(document, 'keydown', this._onKeyDown, this);
+
+        if (L.Browser.touch) {
+            L.DomEvent.on(document, this._touchstart, this._hide, this);
+        }
+
+        this._map.on({
+            contextmenu: this._show,
+            mousedown: this._hide,
+            movestart: this._hide,
+            zoomstart: this._hide
+        }, this);
+    },
+
+    removeHooks: function () {
+        var container = this._map.getContainer();
+
+        L.DomEvent
+            .off(container, 'mouseleave', this._hide, this)
+            .off(document, 'keydown', this._onKeyDown, this);
+
+        if (L.Browser.touch) {
+            L.DomEvent.off(document, this._touchstart, this._hide, this);
+        }
+
+        this._map.off({
+            contextmenu: this._show,
+            mousedown: this._hide,
+            movestart: this._hide,
+            zoomstart: this._hide
+        }, this);
+    },
+
+    showAt: function (point, data) {
+        if (point instanceof L.LatLng) {
+            point = this._map.latLngToContainerPoint(point);
+        }
+        this._showAtPoint(point, data);
+    },
+
+    hide: function () {
+        this._hide();
+    },
+
+    addItem: function (options) {
+        return this.insertItem(options);
+    },
+
+    insertItem: function (options, index) {
+        index = index !== undefined ? index: this._items.length;
+
+        var item = this._createItem(this._container, options, index);
+
+        this._items.push(item);
+
+        this._sizeChanged = true;
+
+        this._map.fire('contextmenu.additem', {
+            contextmenu: this,
+            el: item.el,
+            index: index
+        });
+
+        return item.el;
+    },
+
+    removeItem: function (item) {
+        var container = this._container;
+
+        if (!isNaN(item)) {
+            item = container.children[item];
+        }
+
+        if (item) {
+            this._removeItem(L.Util.stamp(item));
+
+            this._sizeChanged = true;
+
+            this._map.fire('contextmenu.removeitem', {
+                contextmenu: this,
+                el: item
+            });
+
+            return item;
+        }
+
+        return null;
+    },
+
+    removeAllItems: function () {
+        var items = this._container.children,
+            item;
+
+        while (items.length) {
+            item = items[0];
+            this._removeItem(L.Util.stamp(item));
+        }
+        return items;
+    },
+
+    hideAllItems: function () {
+        var item, i, l;
+
+        for (i = 0, l = this._items.length; i < l; i++) {
+            item = this._items[i];
+            item.el.style.display = 'none';
+        }
+    },
+
+    showAllItems: function () {
+        var item, i, l;
+
+        for (i = 0, l = this._items.length; i < l; i++) {
+            item = this._items[i];
+            item.el.style.display = '';
+        }
+    },
+
+    setDisabled: function (item, disabled) {
+        var container = this._container,
+        itemCls = L.Map.ContextMenu.BASE_CLS + '-item';
+
+        if (!isNaN(item)) {
+            item = container.children[item];
+        }
+
+        if (item && L.DomUtil.hasClass(item, itemCls)) {
+            if (disabled) {
+                L.DomUtil.addClass(item, itemCls + '-disabled');
+                this._map.fire('contextmenu.disableitem', {
+                    contextmenu: this,
+                    el: item
+                });
+            } else {
+                L.DomUtil.removeClass(item, itemCls + '-disabled');
+                this._map.fire('contextmenu.enableitem', {
+                    contextmenu: this,
+                    el: item
+                });
+            }
+        }
+    },
+
+    isVisible: function () {
+        return this._visible;
+    },
+
+    _createItems: function () {
+        var itemOptions = this._map.options.contextmenuItems,
+            item,
+            i, l;
+
+        for (i = 0, l = itemOptions.length; i < l; i++) {
+            this._items.push(this._createItem(this._container, itemOptions[i]));
+        }
+    },
+
+    _createItem: function (container, options, index) {
+        if (options.separator || options === '-') {
+            return this._createSeparator(container, index);
+        }
+
+        var itemCls = L.Map.ContextMenu.BASE_CLS + '-item',
+            cls = options.disabled ? (itemCls + ' ' + itemCls + '-disabled') : itemCls,
+            el = this._insertElementAt('a', cls, container, index),
+            callback = this._createEventHandler(el, options.callback, options.context, options.hideOnSelect),
+            icon = this._getIcon(options),
+            iconCls = this._getIconCls(options),
+            html = '';
+
+        if (icon) {
+            html = '<img class="' + L.Map.ContextMenu.BASE_CLS + '-icon" src="' + icon + '"/>';
+        } else if (iconCls) {
+            html = '<span class="' + L.Map.ContextMenu.BASE_CLS + '-icon ' + iconCls + '"></span>';
+        }
+
+        el.innerHTML = html + options.text;
+        el.href = '#';
+
+        L.DomEvent
+            .on(el, 'mouseover', this._onItemMouseOver, this)
+            .on(el, 'mouseout', this._onItemMouseOut, this)
+            .on(el, 'mousedown', L.DomEvent.stopPropagation)
+            .on(el, 'click', callback);
+
+        if (L.Browser.touch) {
+            L.DomEvent.on(el, this._touchstart, L.DomEvent.stopPropagation);
+        }
+
+        // Devices without a mouse fire "mouseover" on tap, but never “mouseout"
+        if (!L.Browser.pointer) {
+            L.DomEvent.on(el, 'click', this._onItemMouseOut, this);
+        }
+
+        return {
+            id: L.Util.stamp(el),
+            el: el,
+            callback: callback
+        };
+    },
+
+    _removeItem: function (id) {
+        var item,
+            el,
+            i, l, callback;
+
+        for (i = 0, l = this._items.length; i < l; i++) {
+            item = this._items[i];
+
+            if (item.id === id) {
+                el = item.el;
+                callback = item.callback;
+
+                if (callback) {
+                    L.DomEvent
+                        .off(el, 'mouseover', this._onItemMouseOver, this)
+                        .off(el, 'mouseover', this._onItemMouseOut, this)
+                        .off(el, 'mousedown', L.DomEvent.stopPropagation)
+                        .off(el, 'click', callback);
+
+                    if (L.Browser.touch) {
+                        L.DomEvent.off(el, this._touchstart, L.DomEvent.stopPropagation);
+                    }
+
+                    if (!L.Browser.pointer) {
+                        L.DomEvent.on(el, 'click', this._onItemMouseOut, this);
+                    }
+                }
+
+                this._container.removeChild(el);
+                this._items.splice(i, 1);
+
+                return item;
+            }
+        }
+        return null;
+    },
+
+    _createSeparator: function (container, index) {
+        var el = this._insertElementAt('div', L.Map.ContextMenu.BASE_CLS + '-separator', container, index);
+
+        return {
+            id: L.Util.stamp(el),
+            el: el
+        };
+    },
+
+    _createEventHandler: function (el, func, context, hideOnSelect) {
+        var me = this,
+            map = this._map,
+            disabledCls = L.Map.ContextMenu.BASE_CLS + '-item-disabled',
+            hideOnSelect = (hideOnSelect !== undefined) ? hideOnSelect : true;
+
+        return function (e) {
+            if (L.DomUtil.hasClass(el, disabledCls)) {
+                return;
+            }
+
+            if (hideOnSelect) {
+                me._hide();
+            }
+
+            if (func) {
+                func.call(context || map, me._showLocation);
+            }
+
+            me._map.fire('contextmenu.select', {
+                contextmenu: me,
+                el: el
+            });
+        };
+    },
+
+    _insertElementAt: function (tagName, className, container, index) {
+        var refEl,
+            el = document.createElement(tagName);
+
+        el.className = className;
+
+        if (index !== undefined) {
+            refEl = container.children[index];
+        }
+
+        if (refEl) {
+            container.insertBefore(el, refEl);
+        } else {
+            container.appendChild(el);
+        }
+
+        return el;
+    },
+
+    _show: function (e) {
+        this._showAtPoint(e.containerPoint, e);
+    },
+
+    _showAtPoint: function (pt, data) {
+        if (this._items.length) {
+            var map = this._map,
+            layerPoint = map.containerPointToLayerPoint(pt),
+            latlng = map.layerPointToLatLng(layerPoint),
+            event = L.extend(data || {}, {contextmenu: this});
+
+            this._showLocation = {
+                latlng: latlng,
+                layerPoint: layerPoint,
+                containerPoint: pt
+            };
+
+            if (data && data.relatedTarget){
+                this._showLocation.relatedTarget = data.relatedTarget;
+            }
+
+            this._setPosition(pt);
+
+            if (!this._visible) {
+                this._container.style.display = 'block';
+                this._visible = true;
+            }
+
+            this._map.fire('contextmenu.show', event);
+        }
+    },
+
+    _hide: function () {
+        if (this._visible) {
+            this._visible = false;
+            this._container.style.display = 'none';
+            this._map.fire('contextmenu.hide', {contextmenu: this});
+        }
+    },
+
+    _getIcon: function (options) {
+        return L.Browser.retina && options.retinaIcon || options.icon;
+    },
+
+    _getIconCls: function (options) {
+        return L.Browser.retina && options.retinaIconCls || options.iconCls;
+    },
+
+    _setPosition: function (pt) {
+        var mapSize = this._map.getSize(),
+            container = this._container,
+            containerSize = this._getElementSize(container),
+            anchor;
+
+        if (this._map.options.contextmenuAnchor) {
+            anchor = L.point(this._map.options.contextmenuAnchor);
+            pt = pt.add(anchor);
+        }
+
+        container._leaflet_pos = pt;
+
+        if (pt.x + containerSize.x > mapSize.x) {
+            container.style.left = 'auto';
+            container.style.right = Math.min(Math.max(mapSize.x - pt.x, 0), mapSize.x - containerSize.x - 1) + 'px';
+        } else {
+            container.style.left = Math.max(pt.x, 0) + 'px';
+            container.style.right = 'auto';
+        }
+
+        if (pt.y + containerSize.y > mapSize.y) {
+            container.style.top = 'auto';
+            container.style.bottom = Math.min(Math.max(mapSize.y - pt.y, 0), mapSize.y - containerSize.y - 1) + 'px';
+        } else {
+            container.style.top = Math.max(pt.y, 0) + 'px';
+            container.style.bottom = 'auto';
+        }
+    },
+
+    _getElementSize: function (el) {
+        var size = this._size,
+            initialDisplay = el.style.display;
+
+        if (!size || this._sizeChanged) {
+            size = {};
+
+            el.style.left = '-999999px';
+            el.style.right = 'auto';
+            el.style.display = 'block';
+
+            size.x = el.offsetWidth;
+            size.y = el.offsetHeight;
+
+            el.style.left = 'auto';
+            el.style.display = initialDisplay;
+
+            this._sizeChanged = false;
+        }
+
+        return size;
+    },
+
+    _onKeyDown: function (e) {
+        var key = e.keyCode;
+
+        // If ESC pressed and context menu is visible hide it
+        if (key === 27) {
+            this._hide();
+        }
+    },
+
+    _onItemMouseOver: function (e) {
+        L.DomUtil.addClass(e.target || e.srcElement, 'over');
+    },
+
+    _onItemMouseOut: function (e) {
+        L.DomUtil.removeClass(e.target || e.srcElement, 'over');
+    }
+});
+
+L.Map.addInitHook('addHandler', 'contextmenu', L.Map.ContextMenu);
diff --git a/src/Mixin.ContextMenu.js b/src/Mixin.ContextMenu.js
new file mode 100644
index 0000000..4411b98
--- /dev/null
+++ b/src/Mixin.ContextMenu.js
@@ -0,0 +1,108 @@
+L.Mixin.ContextMenu = {
+    bindContextMenu: function (options) {
+        L.setOptions(this, options);
+        this._initContextMenu();
+
+        return this;
+    },
+
+    unbindContextMenu: function (){
+        this.off('contextmenu', this._showContextMenu, this);
+
+        return this;
+    },
+
+    addContextMenuItem: function (item) {
+            this.options.contextmenuItems.push(item);
+    },
+
+    removeContextMenuItemWithIndex: function (index) {
+        var items = [];
+        for (var i = 0; i < this.options.contextmenuItems.length; i++) {
+            if (this.options.contextmenuItems[i].index == index){
+                items.push(i);
+            }
+        }
+        var elem = items.pop();
+        while (elem !== undefined) {
+            this.options.contextmenuItems.splice(elem,1);
+            elem = items.pop();
+        }
+    },
+
+    replaceContextMenuItem: function (item) {
+        this.removeContextMenuItemWithIndex(item.index);
+        this.addContextMenuItem(item);
+    },
+
+    _initContextMenu: function () {
+        this._items = [];
+
+        this.on('contextmenu', this._showContextMenu, this);
+    },
+
+    _showContextMenu: function (e) {
+        var itemOptions,
+            data, pt, i, l;
+
+        if (this._map.contextmenu) {
+            data = L.extend({relatedTarget: this}, e);
+
+            pt = this._map.mouseEventToContainerPoint(e.originalEvent);
+
+            if (!this.options.contextmenuInheritItems) {
+                this._map.contextmenu.hideAllItems();
+            }
+
+            for (i = 0, l = this.options.contextmenuItems.length; i < l; i++) {
+                itemOptions = this.options.contextmenuItems[i];
+                this._items.push(this._map.contextmenu.insertItem(itemOptions, itemOptions.index));
+            }
+
+            this._map.once('contextmenu.hide', this._hideContextMenu, this);
+
+            this._map.contextmenu.showAt(pt, data);
+        }
+    },
+
+    _hideContextMenu: function () {
+        var i, l;
+
+        for (i = 0, l = this._items.length; i < l; i++) {
+            this._map.contextmenu.removeItem(this._items[i]);
+        }
+        this._items.length = 0;
+
+        if (!this.options.contextmenuInheritItems) {
+            this._map.contextmenu.showAllItems();
+        }
+    }
+};
+
+var classes = [L.Marker, L.Path],
+    defaultOptions = {
+        contextmenu: false,
+        contextmenuItems: [],
+        contextmenuInheritItems: true
+    },
+    cls, i, l;
+
+for (i = 0, l = classes.length; i < l; i++) {
+    cls = classes[i];
+
+    // L.Class should probably provide an empty options hash, as it does not test
+    // for it here and add if needed
+    if (!cls.prototype.options) {
+        cls.prototype.options = defaultOptions;
+    } else {
+        cls.mergeOptions(defaultOptions);
+    }
+
+    cls.addInitHook(function () {
+        if (this.options.contextmenu) {
+            this._initContextMenu();
+        }
+    });
+
+    cls.include(L.Mixin.ContextMenu);
+}
diff --git a/src/copyright.js b/src/copyright.js
new file mode 100644
index 0000000..c72686e
--- /dev/null
+++ b/src/copyright.js
@@ -0,0 +1,25 @@
+/*
+	Leaflet.contextmenu, a context menu for Leaflet.
+	(c) 2015, Adam Ratcliffe, GeoSmart Maps Limited
+
+	@preserve
+*/
+
+(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);
+	}
+})(function(L) {
diff --git a/src/end.js b/src/end.js
new file mode 100644
index 0000000..db06399
--- /dev/null
+++ b/src/end.js
@@ -0,0 +1,2 @@
+return L.Map.ContextMenu;
+});

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/leaflet-contextmenu.git



More information about the Pkg-javascript-commits mailing list