| 1 |
<!doctype html> |
| 2 |
<html lang="en"> |
| 3 |
<head> |
| 4 |
<meta charset="utf-8"> |
| 5 |
<meta name="viewport" content="width=device-width, initial-scale=1"> |
| 6 |
<title>jQuery UI Droppable - Simple photo manager</title> |
| 7 |
<link rel="stylesheet" href="../../themes/base/all.css"> |
| 8 |
<link rel="stylesheet" href="../demos.css"> |
| 9 |
<style> |
| 10 |
#gallery { float: left; width: 65%; min-height: 12em; } |
| 11 |
.gallery.custom-state-active { background: #eee; } |
| 12 |
.gallery li { float: left; width: 96px; padding: 0.4em; margin: 0 0.4em 0.4em 0; text-align: center; } |
| 13 |
.gallery li h5 { margin: 0 0 0.4em; cursor: move; } |
| 14 |
.gallery li a { float: right; } |
| 15 |
.gallery li a.ui-icon-zoomin { float: left; } |
| 16 |
.gallery li img { width: 100%; cursor: move; } |
| 17 |
|
| 18 |
#trash { float: right; width: 32%; min-height: 18em; padding: 1%; } |
| 19 |
#trash h4 { line-height: 16px; margin: 0 0 0.4em; } |
| 20 |
#trash h4 .ui-icon { float: left; } |
| 21 |
#trash .gallery h5 { display: none; } |
| 22 |
</style> |
| 23 |
<script src="../../external/requirejs/require.js"></script> |
| 24 |
<script src="../bootstrap.js" data-modules="dialog"> |
| 25 |
|
| 26 |
// There's the gallery and the trash |
| 27 |
var $gallery = $( "#gallery" ), |
| 28 |
$trash = $( "#trash" ); |
| 29 |
|
| 30 |
// Let the gallery items be draggable |
| 31 |
$( "li", $gallery ).draggable({ |
| 32 |
cancel: "a.ui-icon", // clicking an icon won't initiate dragging |
| 33 |
revert: "invalid", // when not dropped, the item will revert back to its initial position |
| 34 |
containment: "document", |
| 35 |
helper: "clone", |
| 36 |
cursor: "move" |
| 37 |
}); |
| 38 |
|
| 39 |
// Let the trash be droppable, accepting the gallery items |
| 40 |
$trash.droppable({ |
| 41 |
accept: "#gallery > li", |
| 42 |
classes: { |
| 43 |
"ui-droppable-active": "ui-state-highlight" |
| 44 |
}, |
| 45 |
drop: function( event, ui ) { |
| 46 |
deleteImage( ui.draggable ); |
| 47 |
} |
| 48 |
}); |
| 49 |
|
| 50 |
// Let the gallery be droppable as well, accepting items from the trash |
| 51 |
$gallery.droppable({ |
| 52 |
accept: "#trash li", |
| 53 |
classes: { |
| 54 |
"ui-droppable-active": "custom-state-active" |
| 55 |
}, |
| 56 |
drop: function( event, ui ) { |
| 57 |
recycleImage( ui.draggable ); |
| 58 |
} |
| 59 |
}); |
| 60 |
|
| 61 |
// Image deletion function |
| 62 |
var recycle_icon = "<a href='link/to/recycle/script/when/we/have/js/off' title='Recycle this image' class='ui-icon ui-icon-refresh'>Recycle image</a>"; |
| 63 |
function deleteImage( $item ) { |
| 64 |
$item.fadeOut(function() { |
| 65 |
var $list = $( "ul", $trash ).length ? |
| 66 |
$( "ul", $trash ) : |
| 67 |
$( "<ul class='gallery ui-helper-reset'/>" ).appendTo( $trash ); |
| 68 |
|
| 69 |
$item.find( "a.ui-icon-trash" ).remove(); |
| 70 |
$item.append( recycle_icon ).appendTo( $list ).fadeIn(function() { |
| 71 |
$item |
| 72 |
.animate({ width: "48px" }) |
| 73 |
.find( "img" ) |
| 74 |
.animate({ height: "36px" }); |
| 75 |
}); |
| 76 |
}); |
| 77 |
} |
| 78 |
|
| 79 |
// Image recycle function |
| 80 |
var trash_icon = "<a href='link/to/trash/script/when/we/have/js/off' title='Delete this image' class='ui-icon ui-icon-trash'>Delete image</a>"; |
| 81 |
function recycleImage( $item ) { |
| 82 |
$item.fadeOut(function() { |
| 83 |
$item |
| 84 |
.find( "a.ui-icon-refresh" ) |
| 85 |
.remove() |
| 86 |
.end() |
| 87 |
.css( "width", "96px") |
| 88 |
.append( trash_icon ) |
| 89 |
.find( "img" ) |
| 90 |
.css( "height", "72px" ) |
| 91 |
.end() |
| 92 |
.appendTo( $gallery ) |
| 93 |
.fadeIn(); |
| 94 |
}); |
| 95 |
} |
| 96 |
|
| 97 |
// Image preview function, demonstrating the ui.dialog used as a modal window |
| 98 |
function viewLargerImage( $link ) { |
| 99 |
var src = $link.attr( "href" ), |
| 100 |
title = $link.siblings( "img" ).attr( "alt" ), |
| 101 |
$modal = $( "img[src$='" + src + "']" ); |
| 102 |
|
| 103 |
if ( $modal.length ) { |
| 104 |
$modal.dialog( "open" ); |
| 105 |
} else { |
| 106 |
var img = $( "<img alt='" + title + "' width='384' height='288' style='display: none; padding: 8px;' />" ) |
| 107 |
.attr( "src", src ).appendTo( "body" ); |
| 108 |
setTimeout(function() { |
| 109 |
img.dialog({ |
| 110 |
title: title, |
| 111 |
width: 400, |
| 112 |
modal: true |
| 113 |
}); |
| 114 |
}, 1 ); |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
// Resolve the icons behavior with event delegation |
| 119 |
$( "ul.gallery > li" ).on( "click", function( event ) { |
| 120 |
var $item = $( this ), |
| 121 |
$target = $( event.target ); |
| 122 |
|
| 123 |
if ( $target.is( "a.ui-icon-trash" ) ) { |
| 124 |
deleteImage( $item ); |
| 125 |
} else if ( $target.is( "a.ui-icon-zoomin" ) ) { |
| 126 |
viewLargerImage( $target ); |
| 127 |
} else if ( $target.is( "a.ui-icon-refresh" ) ) { |
| 128 |
recycleImage( $item ); |
| 129 |
} |
| 130 |
|
| 131 |
return false; |
| 132 |
}); |
| 133 |
</script> |
| 134 |
</head> |
| 135 |
<body> |
| 136 |
|
| 137 |
<div class="ui-widget ui-helper-clearfix"> |
| 138 |
|
| 139 |
<ul id="gallery" class="gallery ui-helper-reset ui-helper-clearfix"> |
| 140 |
<li class="ui-widget-content ui-corner-tr"> |
| 141 |
<h5 class="ui-widget-header">High Tatras</h5> |
| 142 |
<img src="images/high_tatras_min.jpg" alt="The peaks of High Tatras" width="96" height="72" /> |
| 143 |
<a href="images/high_tatras.jpg" title="View larger image" class="ui-icon ui-icon-zoomin">View larger</a> |
| 144 |
<a href="link/to/trash/script/when/we/have/js/off" title="Delete this image" class="ui-icon ui-icon-trash">Delete image</a> |
| 145 |
</li> |
| 146 |
<li class="ui-widget-content ui-corner-tr"> |
| 147 |
<h5 class="ui-widget-header">High Tatras 2</h5> |
| 148 |
<img src="images/high_tatras2_min.jpg" alt="The chalet at the Green mountain lake" width="96" height="72" /> |
| 149 |
<a href="images/high_tatras2.jpg" title="View larger image" class="ui-icon ui-icon-zoomin">View larger</a> |
| 150 |
<a href="link/to/trash/script/when/we/have/js/off" title="Delete this image" class="ui-icon ui-icon-trash">Delete image</a> |
| 151 |
</li> |
| 152 |
<li class="ui-widget-content ui-corner-tr"> |
| 153 |
<h5 class="ui-widget-header">High Tatras 3</h5> |
| 154 |
<img src="images/high_tatras3_min.jpg" alt="Planning the ascent" width="96" height="72" /> |
| 155 |
<a href="images/high_tatras3.jpg" title="View larger image" class="ui-icon ui-icon-zoomin">View larger</a> |
| 156 |
<a href="link/to/trash/script/when/we/have/js/off" title="Delete this image" class="ui-icon ui-icon-trash">Delete image</a> |
| 157 |
</li> |
| 158 |
<li class="ui-widget-content ui-corner-tr"> |
| 159 |
<h5 class="ui-widget-header">High Tatras 4</h5> |
| 160 |
<img src="images/high_tatras4_min.jpg" alt="On top of Kozi kopka" width="96" height="72" /> |
| 161 |
<a href="images/high_tatras4.jpg" title="View larger image" class="ui-icon ui-icon-zoomin">View larger</a> |
| 162 |
<a href="link/to/trash/script/when/we/have/js/off" title="Delete this image" class="ui-icon ui-icon-trash">Delete image</a> |
| 163 |
</li> |
| 164 |
</ul> |
| 165 |
|
| 166 |
<div id="trash" class="ui-widget-content ui-state-default"> |
| 167 |
<h4 class="ui-widget-header"><span class="ui-icon ui-icon-trash">Trash</span> Trash</h4> |
| 168 |
</div> |
| 169 |
|
| 170 |
</div> |
| 171 |
|
| 172 |
<div class="demo-description"> |
| 173 |
<p>You can delete an image either by dragging it to the Trash or by clicking the trash icon.</p> |
| 174 |
<p>You can "recycle" an image by dragging it back to the gallery or by clicking the recycle icon.</p> |
| 175 |
<p>You can view larger image by clicking the zoom icon. jQuery UI dialog widget is used for the modal window.</p> |
| 176 |
</div> |
| 177 |
</body> |
| 178 |
</html> |