Skip to content

Commit

Permalink
first version
Browse files Browse the repository at this point in the history
  • Loading branch information
guillaumebort committed Mar 4, 2010
0 parents commit f269ee0
Show file tree
Hide file tree
Showing 4 changed files with 6,645 additions and 0 deletions.
11 changes: 11 additions & 0 deletions README.textile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
h1. JQuery plugin for HTML5 native Drag&Drop

This is a work in progress. Tested on Firefox 3.6 and Safari 4.0.4 on MacOS. Comments are welcome.

h2. Fixing JQuery events

This plugin restore the **dataTransfer** property to JQuery events:

bc. $('img').bind('dragstart', function(e) {
e.dataTransfer.setData( 'URL', $(this).attr('href) ) // <-- yes e.dataTransfer is now available!
})
234 changes: 234 additions & 0 deletions example.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,234 @@
<!DOCTYPE html>
<html>
<head>
<script src="jquery-1.4.2.js" type="text/javascript"></script>
<script src="jquery.ndd.js" type="text/javascript"></script>
<style>
.event {
background: pink;
border: 1px solid #c00;
width: 220px;
padding: 10px;
margin: 0 0 20px 0;
}

img {
width: 240px;
border: 1px solid #666;
}

#things {
float: left;
}

#dropZones {
margin-left: 400px;
}

#dropZones > div {
width: 300px;
padding: 10px;
border: 1px solid #666;
margin-bottom: 15px;
}

#dropZones > div h3, #dropZones > div h4 {
margin: 0;
}

</style>
</head>
<body>

<h1>Native Drag&amp;Drop example</h1>

<p>
You can also try to drag and drop things between several browser windows. Even between different browsers in some cases.
</p>

<div id="things">

<p id="event1" class="event" draggable="true">
Something to do - March 21 2010
</p>

<p id="event2" class="event" draggable="true">
Meeting - April 04 2010
</p>

<p>
<img alt="Commodore 64" src="http://upload.wikimedia.org/wikipedia/commons/thumb/9/9d/Commodore64.jpg/800px-Commodore64.jpg" draggable="true">
</p>

<p>
<img alt="Atari ST" src="http://upload.wikimedia.org/wikipedia/commons/thumb/3/39/Atari_1040STf.jpg/800px-Atari_1040STf.jpg" draggable="true">
</p>

</div>

<div id="dropZones">

<div id="events">
<h3>You can drop events here</h3>
<ul></ul>
</div>

<div id="images">
<h3>You can drop an image here</h3>
<p></p>
</div>

<div id="links">
<h3>You can drop any link here</h3>
<h4>(even from other apps)</h4>
<ul></ul>
</div>

<div id="any">
<h3>You can drop any text here</h3>
<h4>(even from other apps)</h4>
<ul></ul>
</div>

<div id="files">
<h3>You can drop any file here</h3>
<ul></ul>
</div>

</div>

<script type="text/javascript" charset="utf-8">

// Make events draggable
$('.event').draggable(
function() {
return {
effect: 'move',
'my/event': $(this).html(),
'URL': 'http://foo.com/events/' + $(this).attr('id'),
'Text': $(this).html()
}
},
function() {
$(this).animate({opacity: 0}, function() {
$(this).animate({opacity: 1})
})
}
)

// Make img draggable
$('img[draggable]').draggable(
function() {
return {
effect: 'copy',
'my/image': $(this).attr('src'),
'URL': $(this).attr('src'),
'Text': $(this).attr('alt')
}
}
)

// Events container
$('#events').droppable(
'my/event',

// Drag enter
function() {
$(this).css('background', 'yellow')
},

// Drag leave
function() {
$(this).css('background', 'none')
},

// Drop!
function(e) {
$('ul', this).append('<li>' + e.dataTransfer.getData('my/event') + '</li>')
}
)

// Images container
$('#images').droppable(
'my/image',

// Drag enter
function(e) {
$(this).css('background', 'yellow')
},

// Drag leave
function() {
$(this).css('background', 'none')
},

// Drop!
function(e) {
$('p', this).html('<img src="' + e.dataTransfer.getData('my/image') + '">')
}
)

// Links container
$('#links').droppable(
'URL',

// Drag enter
function(e) {
$(this).css('background', 'yellow')
},

// Drag leave
function() {
$(this).css('background', 'none')
},

// Drop!
function(e) {
$('ul', this).append('<li><a href="' + e.dataTransfer.getData('URL') + '">' + e.dataTransfer.getData('Text') + '</a></li>')
}
)

// Any container
$('#any').droppable(
'Text',

// Drag enter
function(e) {
$(this).css('background', 'yellow')
},

// Drag leave
function() {
$(this).css('background', 'none')
},

// Drop!
function(e) {
$('ul', this).append('<li>' + e.dataTransfer.getData('Text') + '</li>')
}
)

// Files container
$('#files').droppable(
'Files',

// Drag enter
function(e) {
$(this).css('background', 'yellow')
},

// Drag leave
function() {
$(this).css('background', 'none')
},

// Drop!
function(e) {
$('ul', this).append('<li>' + e.dataTransfer.files[0].fileName + '</li>')
}
)

</script>

</body>
</html>
Loading

0 comments on commit f269ee0

Please sign in to comment.