Skip to content
This repository was archived by the owner on Apr 20, 2018. It is now read-only.

Commit 61e7f31

Browse files
Adding dragndrop.ts
Also switching $-style document.ready to a noconflict style
1 parent 79ccf1c commit 61e7f31

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

examples/draganddrop/dragndrop.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
$(function () {
2-
1+
/// <reference path="../../ts/rx.jquery.d.ts" />
2+
jQuery(function ($) {
33
var dragTarget = $('#dragTarget');
44

55
// Get the three major events
@@ -8,8 +8,8 @@ $(function () {
88
var mousedown = dragTarget.bindAsObservable('mousedown').publish().refCount().map(function (event) {
99
// calculate offsets when mouse down
1010
event.preventDefault();
11-
return {
12-
left: event.clientX - dragTarget.offset().left,
11+
return {
12+
left: event.clientX - dragTarget.offset().left,
1313
top: event.clientY - dragTarget.offset().top,
1414
};
1515
});

examples/draganddrop/dragndrop.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/// <reference path="../../ts/rx.jquery.d.ts" />
2+
3+
jQuery(function ($: JQueryStatic) {
4+
var dragTarget = $('#dragTarget');
5+
6+
// Get the three major events
7+
var mouseup = dragTarget.bindAsObservable('mouseup').publish().refCount();
8+
var mousemove = $(document).bindAsObservable('mousemove').publish().refCount();
9+
var mousedown = dragTarget.bindAsObservable('mousedown').publish().refCount().map(function (event: JQueryMouseEventObject) {
10+
// calculate offsets when mouse down
11+
event.preventDefault();
12+
return {
13+
left: event.clientX - dragTarget.offset().left,
14+
top: event.clientY - dragTarget.offset().top,
15+
};
16+
});
17+
18+
// Combine mouse down with mouse move until mouse up
19+
var mousedrag = mousedown.selectMany(function(imageOffset) {
20+
return mousemove.map(function (pos: JQueryMouseEventObject) {
21+
// calculate offsets from mouse down to mouse moves
22+
return {
23+
left: pos.clientX - imageOffset.left, top: pos.clientY - imageOffset.top
24+
};
25+
}).takeUntil(mouseup);
26+
});
27+
28+
var subscription = mousedrag.subscribe (function (pos) {
29+
// Update position
30+
$('#dragTarget').css({top: pos.top, left: pos.left});
31+
});
32+
});

0 commit comments

Comments
 (0)