webgui/www/extras/yui/examples/dragdrop/list.html
2007-07-05 04:23:55 +00:00

393 lines
11 KiB
HTML

<!doctype html public "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Yahoo! UI Library - Drag and Drop</title>
<link rel="stylesheet" type="text/css" href="./css/screen.css">
</head>
<style type="text/css">
div.workarea { padding:10px; float:left }
ul.draglist {
position: relative;
width: 200px;
height:300px;
background: #f7f7f7;
border: 1px solid gray;
list-style: none;
margin:0;
padding:0;
}
ul.draglist li {
margin: 1px;
cursor: move;
}
ul.draglist_alt {
position: relative;
width: 200px;
list-style: none;
margin:0;
padding:0;
/*
The bottom padding provides the cushion that makes the empty
list targetable. Alternatively, we could leave the padding
off by default, adding it when we detect that the list is empty.
*/
padding-bottom:20px;
}
ul.draglist_alt li {
margin: 1px;
cursor: move;
}
li.list1 {
background-color: #D1E6EC;
border:1px solid #7EA6B2;
}
li.list2 {
background-color: #D8D4E2;
border:1px solid #6B4C86;
}
#user_actions { float:right }
</style>
<script type="text/javascript" src="../../build/yahoo/yahoo-min.js" ></script>
<script type="text/javascript" src="../../build/event/event-min.js" ></script>
<script type="text/javascript" src="../../build/dom/dom-min.js"></script>
<script type="text/javascript" src="../../build/logger/logger-min.js"></script>
<script type="text/javascript" src="../../build/dragdrop/dragdrop-debug.js" ></script>
<script type="text/javascript" src="../../build/animation/animation-min.js"></script>
<script type="text/javascript">
(function() {
var Dom = YAHOO.util.Dom;
var Event = YAHOO.util.Event;
var DDM = YAHOO.util.DragDropMgr;
//////////////////////////////////////////////////////////////////////////////
// example app
//////////////////////////////////////////////////////////////////////////////
YAHOO.example.DDApp = {
init: function() {
var rows=3,cols=2,i,j;
for (i=1;i<cols+1;i=i+1) {
new YAHOO.util.DDTarget("ul"+i);
}
for (i=1;i<cols+1;i=i+1) {
for (j=1;j<rows+1;j=j+1) {
new YAHOO.example.DDList("li" + i + "_" + j);
}
}
Event.on("showButton", "click", this.showOrder);
Event.on("switchButton", "click", this.switchStyles);
},
showOrder: function() {
var parseList = function(ul, title) {
var items = ul.getElementsByTagName("li");
var out = title + ": ";
for (i=0;i<items.length;i=i+1) {
out += items[i].id + " ";
}
return out;
};
var ul1=Dom.get("ul1"), ul2=Dom.get("ul2");
alert(parseList(ul1, "List 1") + "\n" + parseList(ul2, "List 2"));
},
switchStyles: function() {
Dom.get("ul1").className = "draglist_alt";
Dom.get("ul2").className = "draglist_alt";
}
};
//////////////////////////////////////////////////////////////////////////////
// custom drag and drop implementation
//////////////////////////////////////////////////////////////////////////////
YAHOO.example.DDList = function(id, sGroup, config) {
YAHOO.example.DDList.superclass.constructor.call(this, id, sGroup, config);
this.logger = this.logger || YAHOO;
var el = this.getDragEl();
Dom.setStyle(el, "opacity", 0.67); // The proxy is slightly transparent
this.goingUp = false;
this.lastY = 0;
};
YAHOO.extend(YAHOO.example.DDList, YAHOO.util.DDProxy, {
startDrag: function(x, y) {
this.logger.log(this.id + " startDrag");
// make the proxy look like the source element
var dragEl = this.getDragEl();
var clickEl = this.getEl();
Dom.setStyle(clickEl, "visibility", "hidden");
dragEl.innerHTML = clickEl.innerHTML;
Dom.setStyle(dragEl, "color", Dom.getStyle(clickEl, "color"));
Dom.setStyle(dragEl, "backgroundColor", Dom.getStyle(clickEl, "backgroundColor"));
Dom.setStyle(dragEl, "border", "2px solid gray");
},
endDrag: function(e) {
var srcEl = this.getEl();
var proxy = this.getDragEl();
// Show the proxy element and animate it to the src element's location
Dom.setStyle(proxy, "visibility", "");
var a = new YAHOO.util.Motion(
proxy, {
points: {
to: Dom.getXY(srcEl)
}
},
0.2,
YAHOO.util.Easing.easeOut
)
var proxyid = proxy.id;
var thisid = this.id;
// Hide the proxy and show the source element when finished with the animation
a.onComplete.subscribe(function() {
Dom.setStyle(proxyid, "visibility", "hidden");
Dom.setStyle(thisid, "visibility", "");
});
a.animate();
},
onDragDrop: function(e, id) {
// If there is one drop interaction, the li was dropped either on the list,
// or it was dropped on the current location of the source element.
if (DDM.interactionInfo.drop.length === 1) {
// The position of the cursor at the time of the drop (YAHOO.util.Point)
var pt = DDM.interactionInfo.point;
// The region occupied by the source element at the time of the drop
var region = DDM.interactionInfo.sourceRegion;
// Check to see if we are over the source element's location. We will
// append to the bottom of the list once we are sure it was a drop in
// the negative space (the area of the list without any list items)
if (!region.intersect(pt)) {
var destEl = Dom.get(id);
var destDD = DDM.getDDById(id);
destEl.appendChild(this.getEl());
destDD.isEmpty = false;
DDM.refreshCache();
}
}
},
onDrag: function(e) {
// Keep track of the direction of the drag for use during onDragOver
var y = Event.getPageY(e);
if (y < this.lastY) {
this.goingUp = true;
} else if (y > this.lastY) {
this.goingUp = false;
}
this.lastY = y;
},
onDragOver: function(e, id) {
var srcEl = this.getEl();
var destEl = Dom.get(id);
// We are only concerned with list items, we ignore the dragover
// notifications for the list.
if (destEl.nodeName.toLowerCase() == "li") {
var orig_p = srcEl.parentNode;
var p = destEl.parentNode;
if (this.goingUp) {
p.insertBefore(srcEl, destEl); // insert above
} else {
p.insertBefore(srcEl, destEl.nextSibling); // insert below
}
DDM.refreshCache();
}
}
});
Event.onDOMReady(YAHOO.example.DDApp.init, YAHOO.example.DDApp, true);
})();
</script>
<body>
<div id="pageTitle"><h3>Drag and Drop - DDProxy</h3></div>
<style type="text/css">
/* logger default styles */
/* font size is controlled here: default 77% */
#yui-log {position:absolute;top:1em;right:1em;font-size:77%;text-align:left;}
/* width is controlled here: default 31em */
.yui-log {background-color:#AAA;border:1px solid black;font-family:monospace;z-index:9000;}
.yui-log p {margin:1px;padding:.1em;}
.yui-log button {font-family:monospace;}
.yui-log .yui-log-hd {padding:.5em;background-color:#575757;color:#FFF;}
/* height is controlled here: default 20em*/
.yui-log .yui-log-bd {width:100%;height:20em;background-color:#FFF;border:1px solid gray;overflow:auto;}
.yui-log .yui-log-ft {margin-top:.5em;margin-bottom:1em;}
.yui-log .yui-log-ft .yui-log-categoryfilters {}
.yui-log .yui-log-ft .yui-log-sourcefilters {width:100%;border-top:1px solid #575757;margin-top:.75em;padding-top:.75em;}
.yui-log .yui-log-btns {position:relative;float:right;bottom:.25em;}
.yui-log .yui-log-filtergrp {margin-right:.5em;}
.yui-log .info {background-color:#A7CC25;} /* A7CC25 green */
.yui-log .warn {background-color:#F58516;} /* F58516 orange */
.yui-log .error {background-color:#E32F0B;} /* E32F0B red */
.yui-log .time {background-color:#A6C9D7;} /* A6C9D7 blue */
.yui-log .window {background-color:#F2E886;} /* F2E886 tan */
</style>
<img id="ylogo" src="img/logo.gif" />
<div id="container">
<div id="containerTop">
<div id="header">
<h4>&nbsp;</h4>
</div>
<div id="main">
<div id="rightbar">
<div id="rightBarPad">
<h3>Examples</h3>
<div id="linkage">
<ul>
<li><a href="drag.html?mode=dist">Drag test</a></li>
<li><a href="ontop.html?mode=dist">Always on top</a></li>
<li><a href="proxy.html?mode=dist">Proxy drag</a> </li>
<li><a href="list.html?mode=dist">Sortable list</a> </li>
<!--
<li><a href="slider.html?mode=dist">Slider</a></li>
-->
<li><a href="multihandle.html?mode=dist">Multiple handles</a></li>
<li><a href="targetable.html?mode=dist">Targetable affordance</a></li>
<li><a href="grid.html?mode=dist">Grid</a></li>
<li><a href="resize.html?mode=dist">Resize pane</a></li>
<li><a href="circle.html?mode=dist">Custom click validator</a></li>
</ul>
</div>
<script type="text/javascript">
//<![CDATA[
YAHOO.example.logApp = {
reader: null,
init: function() {
if (YAHOO.widget.Logger) {
this.reader = new YAHOO.widget.LogReader( "logDiv",
{ newestOnTop: true, height: "400px" } );
this.reader._onClickPauseBtn(null, this.reader);
}
}
};
YAHOO.util.Event.on(window, "load", YAHOO.example.logApp.init);
//]]>
</script>
</h4>
<div id="logDiv"></div>
</div>
</div>
<div id="content">
<form name="dragDropForm" action="javscript:;">
<div class="newsItem">
<span id="user_actions">
<input type="button" id="showButton" value="Show Current Order" />
<input type="button" id="switchButton" value="Remove List Background" />
</span>
<h3>Sortable List - POINT mode</h3>
<p>
In the sortable list example, we extend DDProxy instead of DD
so that we can use the source element as the "insertion point".
When the drag starts, the proxy element style and content is
adjusted to match the source element, and visibility:hidden is
applied to the source element.
</p><p>
To facilitate dragging into an empty list, we make the two list
elements DDTargets. When interacting with the list items, we
will get two notifications (one for the list, one for the list
item). We ignore all dragOver events that happen on the list
and ignore all dragDrop events unless the drop was in the
list's negative space (not over another list item).
</p>
<div class="workarea">
<h3>List 1</h3>
<ul id="ul1" class="draglist">
<li class="list1" id="li1_1">list 1, item 1</li>
<li class="list1" id="li1_2">list 1, item 2</li>
<li class="list1" id="li1_3">list 1, item 3</li>
</ul>
</div>
<div class="workarea">
<h3>List 2</h3>
<ul id="ul2" class="draglist">
<li class="list2" id="li2_1">list 2, item 1</li>
<li class="list2" id="li2_2">list 2, item 2</li>
<li class="list2" id="li2_3">list 2, item 3</li>
</ul>
</div>
</div>
</form>
</div>
<div id="footerContainer">
<div id="footer">
<p>&nbsp;</p>
</div>
</div>
</div>
</div>
</div>
</body>
</html>