I have an AngularJS component that should respond either to one click or to drag and drop (resize area).
I started using RxJS (ReactiveX) in my application, so I'm trying to find a solution using it. The Angular side of the request is negligible ...
To simplify the task (and train), I created a slider pointer based on the example rx.angular.js drag'n'drop: http://plnkr.co/edit/UqdyB2
See the Slide.js file (another code is for other experiments ) The code for this logic is:
function(scope, element, attributes)
{
var thumb = element.children(0);
var sliderPosition = element[0].getBoundingClientRect().left;
var sliderWidth = element[0].getBoundingClientRect().width;
var thumbPosition = thumb[0].getBoundingClientRect().left;
var thumbWidth = thumb[0].getBoundingClientRect().width;
var mousedown = rx.Observable.fromEvent(thumb, 'mousedown');
var mousemove = rx.Observable.fromEvent(element, 'mousemove');
var mouseup = rx.Observable.fromEvent($document, 'mouseup');
var locatedMouseDown = mousedown.map(function (event)
{
event.preventDefault();
var initialThumbPosition = thumb[0].getBoundingClientRect().left - sliderPosition;
return { from: initialThumbPosition, offset: event.clientX - sliderPosition };
});
var mousedrag = locatedMouseDown.flatMap(function (clickInfo)
{
return mousemove.map(function (event)
{
var move = event.clientX - sliderPosition - clickInfo.offset;
return clickInfo.from + move;
}).takeUntil(mouseup);
});
mousedrag
.map(function (position)
{
if (position < 0)
return 0;
if (position > sliderWidth - thumbWidth)
return sliderWidth - thumbWidth;
return position;
})
.subscribe(function (position)
{
thumb.css({ left: position + 'px' });
});
}
Basically, D'n'D is limited horizontally and within a given range.
, mousedown, (, 200 , ), , (, ).
delay().takeUntil(mouseup)
, SO-, . , switch() ( ).
? .