JavaScript DHTML/Scriptaculous/Sortable — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 07:26, 26 мая 2010
Содержание
drag and drop to sort
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Draggable demo</title>
<style type="text/css" media="screen">
body {
font-family: "Trebuchet MS", sans-serif;
}
#container {
width: 200px;
list-style-type: none;
margin-left: 0;
padding-left: 0;
}
#container li, .foo {
background-color: #f9f9f9;
border: 1px solid #ccc;
padding: 3px 5px;
padding-left: 0;
margin: 10px 0;
}
#container li .handle {
background-color: #090;
color: #fff;
font-weight: bold;
padding: 3px;
}
#container, #drop_zone {
width: 200px;
height: 300px;
list-style-type: none;
margin-left: 0;
margin-right: 20px;
float: left;
padding: 0;
border: 2px dashed #999;
}
</style>
<script src="scriptaculous-js-1.8.2/lib/prototype.js" type="text/javascript" charset="utf-8"></script>
<script src="scriptaculous-js-1.8.2/src/scriptaculous.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
document.observe("dom:loaded", function() {
Sortable.create("container", { scroll: window });
});
</script>
</head>
<body>
<ul id="container">
<li class="foo" id="item_1">Lorem</li>
<li class="foo" id="item_2">Ipsum</li>
<li class="foo" id="item_3">Dolor</li>
<li class="foo" id="item_4">Sit</li>
<li class="foo" id="item_5">Amet</li>
</ul>
</body>
</html>
Horizontally Sortable
<html>
<head>
<title>Untitled Document</title>
<script src="scriptaculous-js-1.8.2/lib/prototype.js" type="text/javascript"></script>
<script src="scriptaculous-js-1.8.2/src/scriptaculous.js" type="text/javascript"></script>
<script src="scriptaculous-js-1.8.2/src/unittest.js" type="text/javascript"></script>
<script type="text/javascript">
Event.observe(window, "load", function() {
Sortable.create("myList", { constraint: "horizontal", overlap: "horizontal" });
});
</script>
<style>
#myList {
list-style-type: none;
padding: 0;
}
#myList li {
float: left; cursor: move;
margin-left: 1em; padding: 0.2em; width: 10em;
border: 0.05em solid gray; background: #ddd;
text-align: center;
}
</style>
</head>
<body>
<ol id="myList">
<li>A</li>
<li>B</li>
<li>C</li>
<li>D</li>
<li>E</li>
</ol>
</body>
</html>
Sortabled list with handlers
<!--
Copyright (c) 2005-2008 Thomas Fuchs (http://script.aculo.us, http://mir.aculo.us)
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-->
<!-- revised from scriptaculous-js-1.8.2 demo code -->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>script.aculo.us sortable functional test file</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<script src="scriptaculous-js-1.8.2/lib/prototype.js" type="text/javascript"></script>
<script src="scriptaculous-js-1.8.2/src/scriptaculous.js" type="text/javascript"></script>
<script src="scriptaculous-js-1.8.2/src/unittest.js" type="text/javascript"></script>
<link rel="stylesheet" href="../test.css" type="text/css" />
<style type="text/css" media="screen">
ul.sortablelist {
list-style-image:none;
list-style-type:none;
margin-top:5px;
margin:0px;
padding:0px;
}
ul.sortabledemo li {
padding:0px;
margin:0px;
}
span.handle {
background-color: #E8A400;
color:white;
cursor: move;
}
li.green {
background-color: #ECF3E1;
border:1px solid #C5DEA1;
cursor: move;
}
li.orange {
border:1px solid #E8A400;
background-color: #FFF4D8;
}
</style>
</head>
<body>
<h1>script.aculo.us: Two floating sortables with containment and dropOnEmpty</h1>
<div style="height:200px;">
<div style="float:left;">
<h3>This is the first list</h3>
<ul class="sortabledemo" id="firstlist" style="height:150px;width:200px;">
<li class="green" id="firstlist_firstlist1">Item 1 from first list.</li>
<li class="green" id="firstlist_firstlist2">Item 2 from first list.</li>
<li class="green" id="firstlist_firstlist3">Item 3 from first list.</li>
</ul>
</div>
<div style="float:left;">
<h3>And now the second list</h3>
<ul class="sortabledemo" id="secondlist" style="height:150px;width:200px;">
<li class="orange" id="secondlist_secondlist1">
<span class="handle">DRAG HERE</span> Item 1 from second list.
</li>
<li class="orange" id="secondlist_secondlist2">
<span class="handle">DRAG HERE</span> Item 2 from second list.
</li>
<li class="orange" id="secondlist_secondlist3">
<span class="handle">DRAG HERE</span> Item 3 from second list.
</li>
</ul>
</div>
</div>
<hr style="clear:both" />
<pre id="firstlist_debug"></pre>
<pre id="secondlist_debug"></pre>
<script type="text/javascript">
Sortable.create("firstlist",
{dropOnEmpty:true,containment:["firstlist","secondlist"],constraint:false,
onChange:function(){$("firstlist_debug").innerHTML = Sortable.serialize("firstlist") }});
Sortable.create("secondlist",
{dropOnEmpty:true,handle:"handle",containment:["firstlist","secondlist"],constraint:false,
onChange:function(){$("secondlist_debug").innerHTML = Sortable.serialize("secondlist") }});
</script>
</body>
</html>
Sortable is Not just for lists
<html>
<head>
<title>Untitled Document</title>
<script src="scriptaculous-js-1.8.2/lib/prototype.js" type="text/javascript"></script>
<script src="scriptaculous-js-1.8.2/src/scriptaculous.js" type="text/javascript"></script>
<script src="scriptaculous-js-1.8.2/src/unittest.js" type="text/javascript"></script>
<script type="text/javascript">
Event.observe(window, "load", function() {
Sortable.create("chapter", { tag: "p", only: ["orderable", "moveIt"] });
});
</script>
</head>
<body>
<div id="chapter">
<p>This stays on top.</p>
<p class="orderable">Sortable.</p>
<p class="orderable">And this too!</p>
<p class="moveIt">sortable.</p>
<p>This stays at bottom.</p>
</div>
</body>
</html>
Sortable list updated event
<!--
Copyright (c) 2005-2008 Thomas Fuchs (http://script.aculo.us, http://mir.aculo.us)
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-->
<!-- revised from scriptaculous-js-1.8.2 demo code -->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>script.aculo.us sortable functional test file</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<script src="scriptaculous-js-1.8.2/lib/prototype.js" type="text/javascript"></script>
<script src="scriptaculous-js-1.8.2/src/scriptaculous.js" type="text/javascript"></script>
<script src="scriptaculous-js-1.8.2/src/unittest.js" type="text/javascript"></script>
<link rel="stylesheet" href="../test.css" type="text/css" />
</head>
<body>
<div id="container_div">
</div>
<script type="text/javascript" language="javascript" charset="utf-8">
var new_ul_id = "new_id_123";
var ul = document.createElement("ul");
var ul_id = document.createAttribute("id");
ul_id.nodeValue = new_ul_id;
ul.setAttributeNode(ul_id);
var li = document.createElement("li");
li.setAttribute("id", "test_1");
li.appendChild(document.createTextNode("blah1"));
ul.appendChild(li);
var li2 = document.createElement("li");
li2.setAttribute("id", "test_2");
li2.appendChild(document.createTextNode("blah2"));
ul.appendChild(li2);
$("container_div").appendChild(ul);
Sortable.create(new_ul_id, { onUpdate: function() { alert(Sortable.serialize("new_id_123")); } });
</script>
</body>
</html>
Sortable onChange event and onUpdate event
<html>
<head>
<title>Untitled Document</title>
<script src="scriptaculous-js-1.8.2/lib/prototype.js" type="text/javascript"></script>
<script src="scriptaculous-js-1.8.2/src/scriptaculous.js" type="text/javascript"></script>
<script src="scriptaculous-js-1.8.2/src/unittest.js" type="text/javascript"></script>
<script type="text/javascript">
Event.observe(window, "load", function() {
var changeEffect;
Sortable.create("myList", {
onChange: function(item) {
var list = Sortable.options(item).element;
alert(Sortable.serialize(list).escapeHTML());
if (changeEffect) changeEffect.cancel();
changeEffect = new Effect.Highlight("changeNotif", { restorecolor: "transparent" });
},
onUpdate: function(list) {
alert(Sortable.serialize(list).escapeHTML());
}
});
});
</script>
</head>
<body>
<ol id="myList">
<li>A</li>
<li>B</li>
<li>C</li>
<li>D</li>
<li>E</li>
</ol>
</body>
</html>
Sortable two-lists
<!--
Copyright (c) 2005-2008 Thomas Fuchs (http://script.aculo.us, http://mir.aculo.us)
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-->
<!-- revised from scriptaculous-js-1.8.2 demo code -->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>script.aculo.us sortable functional test file</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<script src="scriptaculous-js-1.8.2/lib/prototype.js" type="text/javascript"></script>
<script src="scriptaculous-js-1.8.2/src/scriptaculous.js" type="text/javascript"></script>
<script src="scriptaculous-js-1.8.2/src/unittest.js" type="text/javascript"></script>
<link rel="stylesheet" href="../test.css" type="text/css" />
<style type="text/css" media="screen">
ul { height: 100px; border:1px dotted #888; }
</style>
</head>
<body>
<h1>script.aculo.us Sortable two-lists w/ dropOnEmpty functional test file</h1>
<ul id="thelist1">
<li id="thelist1_1">Hey there! I"m item#1/1</li>
<li id="thelist1_2">Hey there! I"m item#1/2</li>
<li id="thelist1_3">Hey there! I"m item#1/3</li>
</ul>
<ul id="thelist2">
</ul>
<script type="text/javascript" language="javascript" charset="utf-8">
Sortable.create("thelist1",{containment:["thelist1","thelist2"], dropOnEmpty:true});
Sortable.create("thelist2",{containment:["thelist1","thelist2"], dropOnEmpty:true});
</script>
<a href="#" onclick="alert(Sortable.serialize("thelist1"));return false;">Serialize sortable1</a>
<a href="#" onclick="alert(Sortable.serialize("thelist2"));return false;">Serialize sortable2</a>
<div id="debug"></div>
</body>
</html>
Sortable with ghosting effects
<!--
Copyright (c) 2005-2008 Thomas Fuchs (http://script.aculo.us, http://mir.aculo.us)
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-->
<!-- revised from scriptaculous-js-1.8.2 demo code -->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>script.aculo.us Sortable ghosting functional test file</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<script src="scriptaculous-js-1.8.2/lib/prototype.js" type="text/javascript"></script>
<script src="scriptaculous-js-1.8.2/src/scriptaculous.js" type="text/javascript"></script>
<script src="scriptaculous-js-1.8.2/src/unittest.js" type="text/javascript"></script>
<link rel="stylesheet" href="../test.css" type="text/css" />
<style>
ul.testlist {
list-style-type:none;
margin:0;
padding:0;
}
ul.testlist li {
width:200px;
font:12px Verdana;
padding:4px;
cursor:move;
}
ul.testlist li.over {
background-color:#fcb;
}
ul.testlist li img {
float:left;
margin-right:8px;
}
div.dropmarker {
height:6px;
width:200px;
background: url(dropmarker.png) left top;
margin-top:-3px;
margin-left:-5px;
z-index:1000;
overflow: hidden;
}
#testlist4 li {
float:left;
}
</style>
</head>
<body>
<h1>script.aculo.us Sortable ghosting functional test file</h1>
<h2>Ghosting</h2>
<ul id="testlist" class="testlist">
<li id="item_1"><img src="icon.png" alt=""/> Lorem ipsum dolor</li>
<li id="item_2"><img src="icon.png" alt=""/> sit amet</li>
<li id="item_3"><img src="icon.png" alt=""/> consectetur adipisicing</li>
<li id="item_4"><img src="icon.png" alt=""/> elit</li>
<li id="item_5"><img src="icon.png" alt=""/> sed do eiusmod</li>
<li id="item_6"><img src="icon.png" alt=""/> tempor incididunt</li>
<li id="item_7"><img src="icon.png" alt=""/> ut labore et dolore</li>
<li id="item_8"><img src="icon.png" alt=""/> magna aliqua</li>
</ul>
<p id="testlist_serialize">(waiting for onChange)</p>
<script type="text/javascript" language="javascript" charset="utf-8">
Sortable.create("testlist",{ghosting:true,constraint:false,hoverclass:"over",
onUpdate:function(sortable){alert(Sortable.serialize(sortable))},
onChange:function(element){$("testlist_serialize").innerHTML = Sortable.serialize(element.parentNode)}
});
</script>
<h2>No ghosting</h2>
<ul id="testlist2" class="testlist">
<li id="item_9"><img src="icon.png" alt=""/> Lorem ipsum dolor</li>
<li id="item_10"><img src="icon.png" alt=""/> sit amet</li>
<li id="item_11"><img src="icon.png" alt=""/> consectetur adipisicing</li>
<li id="item_12"><img src="icon.png" alt=""/> elit</li>
<li id="item_13"><img src="icon.png" alt=""/> sed do eiusmod</li>
<li id="item_14"><img src="icon.png" alt=""/> tempor incididunt</li>
<li id="item_15"><img src="icon.png" alt=""/> ut labore et dolore</li>
<li id="item_16"><img src="icon.png" alt=""/> magna aliqua</li>
</ul>
<p id="testlist2_serialize">(waiting for onChange)</p>
<script type="text/javascript" language="javascript" charset="utf-8">
Sortable.create("testlist2",{ghosting:false,constraint:false,hoverclass:"over",
onUpdate:function(sortable){alert(Sortable.serialize(sortable))},
onChange:function(element){$("testlist2_serialize").innerHTML = Sortable.serialize(element.parentNode)}
});
</script>
<h2>Ghosting (inside position:relative container)</h2>
<div style="position:relative;left:100px;top:-5px;">
<ul id="testlist3" class="testlist">
<li id="item_17"><img src="icon.png" alt=""/> Lorem ipsum dolor</li>
<li id="item_18"><img src="icon.png" alt=""/> sit amet</li>
<li id="item_19"><img src="icon.png" alt=""/> consectetur adipisicing</li>
<li id="item_20"><img src="icon.png" alt=""/> elit</li>
<li id="item_21"><img src="icon.png" alt=""/> sed do eiusmod</li>
<li id="item_22"><img src="icon.png" alt=""/> tempor incididunt</li>
<li id="item_23"><img src="icon.png" alt=""/> ut labore et dolore</li>
<li id="item_24"><img src="icon.png" alt=""/> magna aliqua</li>
</ul>
</div>
<p id="testlist3_serialize">(waiting for onChange)</p>
<script type="text/javascript" language="javascript" charset="utf-8">
Sortable.create("testlist3",{ghosting:true,constraint:false,
onUpdate:function(sortable){alert(Sortable.serialize(sortable))},
onChange:function(element){$("testlist3_serialize").innerHTML = Sortable.serialize(element.parentNode)}
});
</script>
<h2>Ghosting (inside position:relative container)</h2>
<div style="position:relative;left:100px;top:-5px;">
<ul id="testlist4" class="testlist">
<li id="item_417"><img src="icon.png" alt=""/> Lorem ipsum dolor</li>
<li id="item_418"><img src="icon.png" alt=""/> sit amet</li>
<li id="item_419"><img src="icon.png" alt=""/> consectetur adipisicing</li>
<li id="item_420"><img src="icon.png" alt=""/> elit</li>
<li id="item_421"><img src="icon.png" alt=""/> sed do eiusmod</li>
<li id="item_422"><img src="icon.png" alt=""/> tempor incididunt</li>
<li id="item_423"><img src="icon.png" alt=""/> ut labore et dolore</li>
<li id="item_424"><img src="icon.png" alt=""/> magna aliqua</li>
</ul>
</div>
<p id="testlist4_serialize">(waiting for onChange)</p>
<script type="text/javascript" language="javascript" charset="utf-8">
Sortable.create("testlist4",{overlap:"horizontal",ghosting:true,constraint:false,
onUpdate:function(sortable){alert(Sortable.serialize(sortable))},
onChange:function(element){$("testlist4_serialize").innerHTML = Sortable.serialize(element.parentNode)}
});
</script>
<div id="debug"></div>
</body>
</html>
Sort and reorder the ordered list
<html>
<head>
<title>Untitled Document</title>
<script src="scriptaculous-js-1.8.2/lib/prototype.js" type="text/javascript"></script>
<script src="scriptaculous-js-1.8.2/src/scriptaculous.js" type="text/javascript"></script>
<script src="scriptaculous-js-1.8.2/src/unittest.js" type="text/javascript"></script>
<script type="text/javascript">
Event.observe(window, "load", function() {
Sortable.create("guys");
});
</script>
</head>
<body>
<ol id="guys">
<li>A</li>
<li>B</li>
<li>C</li>
<li>D</li>
<li>E</li>
</ol></body>
</html>
Two sortable groups
<html>
<head>
<title>Untitled Document</title>
<script src="scriptaculous-js-1.8.2/lib/prototype.js" type="text/javascript"></script>
<script src="scriptaculous-js-1.8.2/src/scriptaculous.js" type="text/javascript"></script>
<script src="scriptaculous-js-1.8.2/src/unittest.js" type="text/javascript"></script>
<script type="text/javascript">
Event.observe(window, "load", function() {
var options = {
constraint: false, containment: ["myList", "myList2"],
dropOnEmpty: true, onUpdate: function(list) {
var methodStart = list.down("li") ? "remove" : "add";
list[methodStart + "ClassName"]("empty");
}
};
Sortable.create("myList", options);
Sortable.create("myList2", options);
});
</script>
</head>
<body>
<ul id="myList">
<li>A</li>
<li>B</li>
<li>C</li>
</ul>
<ul id="myList2">
<li>D</li>
<li>E</li>
<li>F</li>
</ul>
</body>
</html>