~ubuntu-branches/ubuntu/saucy/jenkins/saucy

« back to all changes in this revision

Viewing changes to core/src/main/resources/lib/form/hetero-list/hetero-list.js

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2013-01-10 09:50:50 UTC
  • mfrom: (5.1.10 experimental)
  • Revision ID: package-import@ubuntu.com-20130110095050-kj8xuw20gcfh62k3
Tags: 1.480.2+dfsg-1~exp1
* New upstream release (Closes: #696816, #697617):
  - d/control: Added new BD on libjbcrypt-java.
  - d/control: Versioned BD jenkins-winstone >= 0.9.10-jenkins-40.
  - d/control: Versioned BD jenkins-trilead-ssh2 >= 214-jenkins-1.
  - Fixes the following security vulnerabilities:
    CVE-2012-6072, CVE-2012-6073, CVE-2012-6072, CVE-2013-0158.
* Tidied lintian warnings.
* Bumped Standards-Version: 3.9.4, no changes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// @include lib.form.dragdrop.dragdrop
 
2
 
 
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) {
 
6
        e=$(e);
 
7
        if(isInsideRemovable(e))    return;
 
8
 
 
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);
 
14
 
 
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.
 
19
 
 
20
        // extract templates
 
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});
 
28
            i++;
 
29
        });
 
30
        Element.remove(prototypes);
 
31
 
 
32
        var withDragDrop = initContainerDD(e);
 
33
 
 
34
        var menuAlign = (btn.getAttribute("menualign")||"tl-bl");
 
35
 
 
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) {
 
40
            var item = args[1];
 
41
            if (item.cfg.getProperty("disabled"))   return;
 
42
            var t = templates[parseInt(item.value)];
 
43
 
 
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;
 
49
            $(nc).setOpacity(0);
 
50
 
 
51
            var scroll = document.body.scrollTop;
 
52
 
 
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) {
 
62
                            var count=0;
 
63
                            for (var i=0; i<current.length; i++) {
 
64
                                if ((i<pos) == (order(current[i])<=order(prospect)))
 
65
                                    count++;
 
66
                            }
 
67
                            return count;
 
68
                        }
 
69
 
 
70
                        var bestScore = -1;
 
71
                        var bestPos = 0;
 
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
 
75
                                bestScore = d;
 
76
                                bestPos = i;
 
77
                            }
 
78
                        }
 
79
                        return bestPos;
 
80
                    }
 
81
 
 
82
                    var current = e.childElements().findAll(function(e) {return e.match("DIV.repeated-chunk")});
 
83
 
 
84
                    function o(did) {
 
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)
 
89
                                return i;
 
90
                        return 0; // can't happen
 
91
                    }
 
92
 
 
93
                    var bestPos = findBestPosition(t.descriptorId, current, o);
 
94
                    if (bestPos<current.length)
 
95
                        return current[bestPos];
 
96
                    else
 
97
                        return insertionPoint;
 
98
                }
 
99
                (e.hasClassName("honor-order") ? findInsertionPoint() : insertionPoint).insert({before:nc});
 
100
 
 
101
                if(withDragDrop)    prepareDD(nc);
 
102
 
 
103
                new YAHOO.util.Anim(nc, {
 
104
                    opacity: { to:1 }
 
105
                }, 0.2, YAHOO.util.Easing.easeIn).animate();
 
106
 
 
107
                Behaviour.applySubtree(nc,true);
 
108
                ensureVisible(nc);
 
109
                layoutUpdateCallback.call();
 
110
            },true);
 
111
        });
 
112
 
 
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;
 
118
                if(t!=null)
 
119
                    applyTooltip(items[i].element,t);
 
120
            }
 
121
        });
 
122
 
 
123
        if (e.hasClassName("one-each")) {
 
124
            // does this container already has a ocnfigured instance of the specified descriptor ID?
 
125
            function has(id) {
 
126
                return Prototype.Selector.find(e.childElements(),"DIV.repeated-chunk[descriptorId=\""+id+"\"]")!=null;
 
127
            }
 
128
 
 
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));
 
133
                }
 
134
            });
 
135
        }
 
136
    });
 
137
 
 
138
Behaviour.specify("DIV.dd-handle", 'hetero-list', -100, function(e) {
 
139
        e=$(e);
 
140
        e.on("mouseover",function() {
 
141
            $(this).up(".repeated-chunk").addClassName("hover");
 
142
        });
 
143
        e.on("mouseout",function() {
 
144
            $(this).up(".repeated-chunk").removeClassName("hover");
 
145
        });
 
146
});