Skip to content

Commit

Permalink
Merge pull request #12 from jonataswalker/release-v1.1.0
Browse files Browse the repository at this point in the history
Release v1.1.0
  • Loading branch information
jonataswalker committed Feb 15, 2016
2 parents 91517d1 + eb6cb73 commit eea27b8
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 144 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
LAST_VERSION := 1.0.1
NOW := $(shell date --iso=seconds)
ROOT_DIR := $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))
SRC_DIR := $(ROOT_DIR)/src
Expand All @@ -8,6 +7,8 @@ JS_FINAL := $(BUILD_DIR)/ol3-contextmenu.js
CSS_COMBINED := $(BUILD_DIR)/ol3-contextmenu.css
CSS_FINAL := $(BUILD_DIR)/ol3-contextmenu.min.css
TMPFILE := $(BUILD_DIR)/tmp
PACKAGE_JSON := $(ROOT_DIR)/package.json
LAST_VERSION := $(shell cat $(PACKAGE_JSON) | node -pe "JSON.parse(require('fs').readFileSync('/dev/stdin').toString()).version")

JS_FILES := $(SRC_DIR)/wrapper-head.js \
$(SRC_DIR)/base.js \
Expand Down
171 changes: 33 additions & 138 deletions build/ol3-contextmenu-debug.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Custom Context Menu for Openlayers 3.
// https://github.com/jonataswalker/ol3-contextmenu
// Version: v1.0.1
// Built: 2015-12-04T15:34:43-0200
// Version: v1.1.0
// Built: 2016-02-15T16:01:34-0200

(function(win, doc) {
'use strict';
Expand Down Expand Up @@ -67,6 +67,13 @@
}
};

/**
* @return {Array} Returns default items
*/
ContextMenu.prototype.getDefaultItems = function() {
return ContextMenu.defaultItems;
};

/**
* Not supposed to be used on app.
*/
Expand All @@ -79,6 +86,7 @@
(function(win, doc) {
ContextMenu.Internal = function(menu) {
this.map = undefined;
this.contextmenu = menu;
this.container = menu.container;
this.$html = menu.$html;
this.coordinate_clicked = undefined;
Expand All @@ -94,7 +102,7 @@
getCoordinateClicked: function() {
return this.coordinate_clicked;
},
openMenu: function(pixel) {
openMenu: function(pixel, coordinate) {
var
this_ = this,
map_size = this.map.getSize(),
Expand All @@ -116,9 +124,17 @@
utils.removeClass(this.container, 'hidden');
this.container.style.left = left + 'px';
this.container.style.top = top + 'px';
this.contextmenu.dispatchEvent({
type: ContextMenu.EventType.OPEN,
pixel: pixel,
coordinate: coordinate,
});
},
closeMenu: function() {
utils.addClass(this.container, 'hidden');
this.contextmenu.dispatchEvent({
type: ContextMenu.EventType.CLOSE
});
},
getNextItemIndex: function() {
return Object.keys(ContextMenu.items).length;
Expand All @@ -131,8 +147,8 @@
menu = function(evt) {
evt.stopPropagation();
evt.preventDefault();
this_.openMenu(map.getEventPixel(evt));
this_.coordinate_clicked = map.getEventCoordinate(evt);
this_.openMenu(map.getEventPixel(evt), this_.coordinate_clicked);

//one-time fire
canvas.addEventListener('mousedown', {
Expand All @@ -158,7 +174,8 @@
li.addEventListener('click', function(evt) {
evt.preventDefault();
var obj = {
coordinate: this_.getCoordinateClicked()
coordinate: this_.getCoordinateClicked(),
data: ContextMenu.items[index].data || null
};
this_.closeMenu();
callback.call(undefined, obj, this_.map);
Expand All @@ -168,138 +185,15 @@
}
};

ContextMenu.items = {};

ContextMenu.defaultItems = [{
text: 'Zoom In',
classname: 'zoom-in ol-contextmenu-icon',
callback: function(obj, map) {
var
view = map.getView(),
pan = ol.animation.pan({
duration: 1000,
source: view.getCenter()
}),
zoom = ol.animation.zoom({
duration: 1000,
resolution: view.getResolution()
});

map.beforeRender(pan, zoom);
view.setCenter(obj.coordinate);
view.setZoom(+view.getZoom() + 1);
}
}, {
text: 'Zoom Out',
classname: 'zoom-out ol-contextmenu-icon',
callback: function(obj, map) {
var
view = map.getView(),
pan = ol.animation.pan({
duration: 1000,
source: view.getCenter()
}),
zoom = ol.animation.zoom({
duration: 1000,
resolution: view.getResolution()
});
map.beforeRender(pan, zoom);
view.setCenter(obj.coordinate);
view.setZoom(+view.getZoom() - 1);
}
}];

})(win, doc);
(function(win, doc) {
ContextMenu.Internal = function(menu) {
this.map = undefined;
this.container = menu.container;
this.$html = menu.$html;
this.coordinate_clicked = undefined;
};
ContextMenu.Internal.prototype = {
init: function(map) {
this.map = map;
// subscribe
this.setListeners();
// publish
this.$html.createMenu();
},
getCoordinateClicked: function() {
return this.coordinate_clicked;
},
openMenu: function(pixel) {
var
this_ = this,
map_size = this.map.getSize(),
menu_size = [
this.container.offsetWidth,
this.container.offsetHeight
],
map_width = map_size[0],
map_height = map_size[1],
menu_width = menu_size[0],
menu_height = menu_size[1],
height_left = map_height - pixel[1],
width_left = map_width - pixel[0],
top = (height_left >= menu_height) ?
pixel[1] - 10 : pixel[1] - menu_height - 10,
left = (width_left >= menu_width) ?
pixel[0] + 5 : pixel[0] - menu_width - 5;

utils.removeClass(this.container, 'hidden');
this.container.style.left = left + 'px';
this.container.style.top = top + 'px';
},
closeMenu: function() {
utils.addClass(this.container, 'hidden');
},
getNextItemIndex: function() {
return Object.keys(ContextMenu.items).length;
},
setListeners: function() {
var
this_ = this,
map = this.map,
canvas = map.getTargetElement(),
menu = function(evt) {
evt.stopPropagation();
evt.preventDefault();
this_.openMenu(map.getEventPixel(evt));
this_.coordinate_clicked = map.getEventCoordinate(evt);

//one-time fire
canvas.addEventListener('mousedown', {
handleEvent: function(evt) {
this_.closeMenu();
canvas.removeEventListener(evt.type, this, false);
}
}, false);
};
canvas.addEventListener('contextmenu', menu, false);

// subscribe to later menu entries
events.subscribe(ContextMenu.Constants.eventType.ADD_MENU_ENTRY,
function(obj) {
this_.setItemListener(obj.element, obj.index);
}
);
},
setItemListener: function(li, index) {
var this_ = this;
if (li && typeof ContextMenu.items[index].callback === 'function') {
(function(callback) {
li.addEventListener('click', function(evt) {
evt.preventDefault();
var obj = {
coordinate: this_.getCoordinateClicked()
};
this_.closeMenu();
callback.call(undefined, obj, this_.map);
}, false);
})(ContextMenu.items[index].callback);
}
}
ContextMenu.EventType = {
/**
* Triggered when context menu is openned.
*/
OPEN: 'open',
/**
* Triggered when context menu is closed.
*/
CLOSE: 'close'
};

ContextMenu.items = {};
Expand Down Expand Up @@ -400,7 +294,8 @@

ContextMenu.items[index] = {
id: index,
callback: item.callback
callback: item.callback,
data: item.data || null
};

// publish to add listener
Expand Down
2 changes: 1 addition & 1 deletion build/ol3-contextmenu.css
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
opacity: 0;
visibility: hidden;
-webkit-transition: opacity .25s linear, visibility 0 linear .25s;
transition: opacity .25s linear, visibility 0 linear .25s;
transition: opacity .25s linear, visibility 0 linear .25s;
}
.ol-contextmenu-icon{
text-indent: 20px;
Expand Down
Loading

0 comments on commit eea27b8

Please sign in to comment.