1
// @include lib.form.dragdrop.dragdrop
3
// do the ones that extract innerHTML so that they can get their original HTML before
4
// other behavior rules change them (like YUI buttons.)
5
Behaviour.specify("DIV.hetero-list-container", 'hetero-list', -100, function(e) {
7
if(isInsideRemovable(e)) return;
9
// components for the add button
10
var menu = document.createElement("SELECT");
11
var btns = findElementsBySelector(e,"INPUT.hetero-list-add"),
12
btn = btns[btns.length-1]; // In case nested content also uses hetero-list
13
YAHOO.util.Dom.insertAfter(menu,btn);
15
var prototypes = $(e.lastChild);
16
while(!prototypes.hasClassName("prototypes"))
17
prototypes = prototypes.previous();
18
var insertionPoint = prototypes.previous(); // this is where the new item is inserted.
21
var templates = []; var i=0;
22
$(prototypes).childElements().each(function (n) {
23
var name = n.getAttribute("name");
24
var tooltip = n.getAttribute("tooltip");
25
var descriptorId = n.getAttribute("descriptorId");
26
menu.options[i] = new Option(n.getAttribute("title"),""+i);
27
templates.push({html:n.innerHTML, name:name, tooltip:tooltip,descriptorId:descriptorId});
30
Element.remove(prototypes);
32
var withDragDrop = initContainerDD(e);
34
var menuAlign = (btn.getAttribute("menualign")||"tl-bl");
36
var menuButton = new YAHOO.widget.Button(btn, { type: "menu", menu: menu, menualignment: menuAlign.split("-") });
37
$(menuButton._button).addClassName(btn.className); // copy class names
38
$(menuButton._button).setAttribute("suffix",btn.getAttribute("suffix"));
39
menuButton.getMenu().clickEvent.subscribe(function(type,args,value) {
41
if (item.cfg.getProperty("disabled")) return;
42
var t = templates[parseInt(item.value)];
44
var nc = document.createElement("div");
45
nc.className = "repeated-chunk";
46
nc.setAttribute("name",t.name);
47
nc.setAttribute("descriptorId",t.descriptorId);
48
nc.innerHTML = t.html;
51
var scroll = document.body.scrollTop;
53
renderOnDemand(findElementsBySelector(nc,"TR.config-page")[0],function() {
54
function findInsertionPoint() {
55
// given the element to be inserted 'prospect',
56
// and the array of existing items 'current',
57
// and preferred ordering function, return the position in the array
58
// the prospect should be inserted.
59
// (for example 0 if it should be the first item)
60
function findBestPosition(prospect,current,order) {
61
function desirability(pos) {
63
for (var i=0; i<current.length; i++) {
64
if ((i<pos) == (order(current[i])<=order(prospect)))
72
for (var i=0; i<=current.length; i++) {
73
var d = desirability(i);
74
if (bestScore<=d) {// prefer to insert them toward the end
82
var current = e.childElements().findAll(function(e) {return e.match("DIV.repeated-chunk")});
85
if (Object.isElement(did))
86
did = did.getAttribute("descriptorId");
87
for (var i=0; i<templates.length; i++)
88
if (templates[i].descriptorId==did)
90
return 0; // can't happen
93
var bestPos = findBestPosition(t.descriptorId, current, o);
94
if (bestPos<current.length)
95
return current[bestPos];
97
return insertionPoint;
99
(e.hasClassName("honor-order") ? findInsertionPoint() : insertionPoint).insert({before:nc});
101
if(withDragDrop) prepareDD(nc);
103
new YAHOO.util.Anim(nc, {
105
}, 0.2, YAHOO.util.Easing.easeIn).animate();
107
Behaviour.applySubtree(nc,true);
109
layoutUpdateCallback.call();
113
menuButton.getMenu().renderEvent.subscribe(function() {
114
// hook up tooltip for menu items
115
var items = menuButton.getMenu().getItems();
116
for(i=0; i<items.length; i++) {
117
var t = templates[i].tooltip;
119
applyTooltip(items[i].element,t);
123
if (e.hasClassName("one-each")) {
124
// does this container already has a ocnfigured instance of the specified descriptor ID?
126
return Prototype.Selector.find(e.childElements(),"DIV.repeated-chunk[descriptorId=\""+id+"\"]")!=null;
129
menuButton.getMenu().showEvent.subscribe(function() {
130
var items = menuButton.getMenu().getItems();
131
for(i=0; i<items.length; i++) {
132
items[i].cfg.setProperty("disabled",has(templates[i].descriptorId));
138
Behaviour.specify("DIV.dd-handle", 'hetero-list', -100, function(e) {
140
e.on("mouseover",function() {
141
$(this).up(".repeated-chunk").addClassName("hover");
143
e.on("mouseout",function() {
144
$(this).up(".repeated-chunk").removeClassName("hover");