Skip to content

Commit

Permalink
[BUG] Viglino#41 Test event before use
Browse files Browse the repository at this point in the history
  • Loading branch information
Viglino committed Jun 24, 2017
1 parent c95cb7d commit afb3ad0
Showing 1 changed file with 28 additions and 11 deletions.
39 changes: 28 additions & 11 deletions control/layerswitchercontrol.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ ol.control.LayerSwitcher = function(opt_options)
{ element = $("<div>").addClass((options.switcherClass || 'ol-layerswitcher') +' ol-unselectable ol-control ol-collapsed');

this.button = $("<button>")
.attr('type','button')
.on("touchstart", function(e)
{ element.toggleClass("ol-collapsed");
e.preventDefault();
Expand Down Expand Up @@ -147,8 +148,8 @@ ol.control.LayerSwitcher.prototype.overflow = function(dir)
{ // Bug IE: need to have an height defined
$(this.element).css("height", "100%");
switch (dir)
{ case 1: top += 2*$("li",this.panel_).height(); break;
case -1: top -= 2*$("li",this.panel_).height(); break;
{ case 1: top += 2*$("li.visible .li-content",this.panel_).height(); break;
case -1: top -= 2*$("li.visible .li-content",this.panel_).height(); break;
case "+50%": top += Math.round(h/2); break;
case "-50%": top -= Math.round(h/2); break;
default: break;
Expand Down Expand Up @@ -280,14 +281,17 @@ ol.control.LayerSwitcher.prototype.dragOrdering_ = function(e)
case 'mousedown':
case 'touchstart':
{ e.stopPropagation();
//e.preventDefault();
e.preventDefault();
var pageY = e.pageY
|| (e.originalEvent.touches && e.originalEvent.touches.length && e.originalEvent.touches[0].pageY)
|| (e.originalEvent.changedTouches && e.originalEvent.changedTouches.length && e.originalEvent.changedTouches[0].pageY);
drag =
{ self: drag.self,
elt: $(e.currentTarget).closest("li"),
start: true,
element: drag.self.element,
panel: drag.self.panel_,
pageY: e.pageY || e.originalEvent.touches[0].pageY
pageY: pageY
};
drag.elt.parent().addClass('drag');
$(document).on("mouseup mousemove touchend touchcancel touchmove", drag, drag.self.dragOrdering_);
Expand Down Expand Up @@ -333,9 +337,13 @@ ol.control.LayerSwitcher.prototype.dragOrdering_ = function(e)
break;
}
// Ordering
default:
case 'mousemove':
case 'touchmove':
{ // First drag (more than 2 px) => show drag element (ghost)
if (drag.start && Math.abs(drag.pageY - (e.pageY || e.originalEvent.touches[0].pageY)) > 2)
var pageY = e.pageY
|| (e.originalEvent.touches && e.originalEvent.touches.length && e.originalEvent.touches[0].pageY)
|| (e.originalEvent.changedTouches && e.originalEvent.changedTouches.length && e.originalEvent.changedTouches[0].pageY);
if (drag.start && Math.abs(drag.pageY - pageY) > 2)
{ drag.start = false;
drag.elt.addClass("drag");
drag.layer = drag.elt.data('layer');
Expand All @@ -355,13 +363,14 @@ ol.control.LayerSwitcher.prototype.dragOrdering_ = function(e)
e.stopPropagation();

// Ghost div
drag.div.css ({ top:(e.pageY || e.originalEvent.touches[0].pageY)-drag.panel.offset().top+5 });
drag.div.css ({ top:pageY - drag.panel.offset().top + drag.panel.scrollTop() +5 });

var li;
if (e.pageX) li = $(e.target);
else li = $(document.elementFromPoint(e.originalEvent.touches[0].clientX, e.originalEvent.touches[0].clientY));
else li = $(document.elementFromPoint(e.originalEvent.touches[0].clientX, e.originalEvent.touches[0].clientY));
if (li.hasClass("ol-switcherbottomdiv"))
{ drag.self.overflow(-1);
console.log('bottom')
}
else if (li.hasClass("ol-switchertopdiv"))
{ drag.self.overflow(1);
Expand Down Expand Up @@ -391,6 +400,8 @@ ol.control.LayerSwitcher.prototype.dragOrdering_ = function(e)
}
break;
}
default: break;

}
};

Expand All @@ -407,7 +418,9 @@ ol.control.LayerSwitcher.prototype.dragOpacity_ = function(e)
case 'touchstart':
{ e.stopPropagation();
e.preventDefault();
drag.start = e.pageX || e.originalEvent.touches[0].pageX;
drag.start = e.pageX
|| (e.originalEvent.touches && e.originalEvent.touches.length && e.originalEvent.touches[0].pageX)
|| (e.originalEvent.changedTouches && e.originalEvent.changedTouches.length && e.originalEvent.changedTouches[0].pageX);
drag.elt = $(e.target);
drag.layer = drag.elt.closest("li").data('layer')
drag.self.dragging_ = true;
Expand All @@ -426,7 +439,9 @@ ol.control.LayerSwitcher.prototype.dragOpacity_ = function(e)
}
// Move opcaity
default:
{ var x = e.pageX || e.originalEvent.touches[0].pageX;
{ var x = e.pageX
|| (e.originalEvent.touches && e.originalEvent.touches.length && e.originalEvent.touches[0].pageX)
|| (e.originalEvent.changedTouches && e.originalEvent.changedTouches.length && e.originalEvent.changedTouches[0].pageX);
var dx = Math.max ( 0, Math.min( 1, (x - drag.elt.parent().offset().left) / drag.elt.parent().width() ));
drag.elt.css("left", (dx*100)+"%");
drag.opacity = dx;
Expand Down Expand Up @@ -592,7 +607,9 @@ ol.control.LayerSwitcher.prototype.drawList = function(ul, collection)
.on("click", function(e)
{ e.stopPropagation();
e.preventDefault();
var x = e.pageX || e.originalEvent.touches[0].pageX;
var x = e.pageX
|| (e.originalEvent.touches && e.originalEvent.touches.length && e.originalEvent.touches[0].pageX)
|| (e.originalEvent.changedTouches && e.originalEvent.changedTouches.length && e.originalEvent.changedTouches[0].pageX);
var dx = Math.max ( 0, Math.min( 1, (x - $(this).offset().left) / $(this).width() ));
$(this).closest("li").data('layer').setOpacity(dx);
})
Expand Down

0 comments on commit afb3ad0

Please sign in to comment.