]> source.dussan.org Git - jquery-ui.git/commitdiff
1.8: initial version of tree component, dependant upon sortables and droppables
authorPaul Bakaus <paul.bakaus@googlemail.com>
Mon, 28 Jul 2008 18:34:01 +0000 (18:34 +0000)
committerPaul Bakaus <paul.bakaus@googlemail.com>
Mon, 28 Jul 2008 18:34:01 +0000 (18:34 +0000)
1.6: droppables can now operate in a seperate scope, droppables and sortables in the current dragged item are filtered out at start, preventing node hierarchy issues

tests/visual/tree.html
ui/ui.draggable.js
ui/ui.droppable.js
ui/ui.sortable.js

index a2c2e14d0f8a5e013ec1cf1808ef64ba5faae578..e50f280a2c5b1c2629ce8ce754f80bebaa44631e 100644 (file)
 <script type="text/javascript" src="../../ui/ui.droppable.js"></script>\r
 <script type="text/javascript" src="../../ui/ui.resizable.js"></script>\r
 <script type="text/javascript" src="../../ui/ui.sortable.js"></script>\r
+<script type="text/javascript" src="../../../branches/1.8/ui.tree.js"></script>\r
 \r
 \r
 <script>\r
 $(document).ready(function(){\r
 \r
-\r
-$.widget("ui.tree", {\r
-       init: function() {\r
-\r
-               var self = this;\r
-\r
-               this.element.sortable({\r
-                       items: this.options.sortOn,\r
-                       placeholder: "ui-tree-placeholder",\r
-                       start: function() {\r
-                               $(this).data("sortable").placeholder.hide();\r
-                               $(this).data("sortable").refreshPositions(true);        \r
-                       },\r
-                       stop: function() {\r
-                               var self = $(this).data("sortable");\r
-                               $(self.options.items, self.element).css("border-top", "0").css("border-bottom", "0");\r
-                       },\r
-                       sortIndicator: function(e, item, append, hardRefresh) {\r
-               \r
-                               append ? append[0].appendChild(this.placeholder[0]) : item.item[0].parentNode.insertBefore(this.placeholder[0], (this.direction == 'down' ? item.item[0] : item.item[0].nextSibling));\r
-                               \r
-                               $(this.options.items, this.element).css("border-top", "0").css("border-bottom", "0");\r
-                               item.item.css("border-"+(this.direction == "down" ? "top" : "bottom"), "2px solid black");\r
-       \r
-                       }\r
-               });\r
-               \r
-               \r
-               \r
-               //Make certain nodes droppable\r
-               $(this.options.dropOn, this.element).droppable({\r
-                       accept: this.options.sortOn,\r
-                       hoverClass: this.options.hoverClass,\r
-                       //tolerance: "pointer",\r
-                       over: function() {\r
-                               $(self.options.sortOn, self.element).css("border-top", "0").css("border-bottom", "0");\r
-                       },\r
-                       drop: function(e, ui) {\r
-                               $(this).parent().find("ul").append(ui.draggable);\r
-                               self.element.data("sortable")._noFinalSort = true;\r
-                       }\r
-               });\r
-\r
-       }\r
-});\r
-\r
-$.extend($.ui.tree, {\r
-       defaults: {\r
-               sortOn: "*",\r
-               dropOn: "div",\r
-               hoverClass: "ui-tree-hover"\r
-       }\r
-});\r
-\r
 $("ul.sortable").tree({\r
        sortOn: "li",\r
-       dropOn: "div",\r
-       hoverClass: "hover"\r
+       dropOn: ".folder",\r
+       dropHoverClass: "hover"\r
 });\r
 \r
 \r
@@ -96,14 +43,31 @@ font-family:"lucida grande", tahoma, verdana, arial, sans-serif;
 .placeholder {\r
        display: none;\r
 }\r
+\r
+.hover-up {\r
+       border-bottom: 2px solid black;\r
+}\r
+\r
+.hover-down {\r
+       border-top: 2px solid black;\r
+}\r
+\r
+.folder {\r
+       border: 1px solid black;\r
+}\r
+\r
+li {\r
+       padding: 3px;\r
+}\r
+\r
 </style>\r
 </head>\r
 \r
 <body>\r
 \r
        <ul class="sortable">\r
-               <li>Item 1</li>\r
-               <li><div>Item 2</div>\r
+               <li><div class="folder">Item 1</div></li>\r
+               <li><div class="folder">Item 2</div>\r
                        <ul>\r
                                <li>Sub Item 1</li>\r
                                <li>Sub Item 3</li>\r
@@ -111,9 +75,15 @@ font-family:"lucida grande", tahoma, verdana, arial, sans-serif;
                                <li>Sub Item 5</li>\r
                        </ul>\r
                </li>\r
-               <li>Item 3</li>\r
-               <li>Item 4</li>\r
-               <li><div>Item 2</div>\r
+               <li><div class="folder">Item 3</div></li>\r
+               <li><div class="folder">Item 4</div></li>\r
+               <li><div class="folder">Item 4</div></li>\r
+               <li><div class="folder">Item 4</div></li>\r
+               <li><div class="folder">Item 4</div></li>\r
+               <li><div class="folder">Item 4</div></li>\r
+               <li><div class="folder">Item 4</div></li>\r
+               <li><div class="folder">Item 4</div></li>\r
+               <li><div class="folder">Item 2</div>\r
                        <ul>\r
                                <li>Sub Item 1</li>\r
                                <li>Sub Item 3</li>\r
index 80024a4d657dbbabc45121e7905182dea2557837..d72e24685f1ac3aab12009115226eca0cbc11507 100644 (file)
@@ -267,7 +267,8 @@ $.extend($.ui.draggable, {
                cancel: ":input",
                delay: 0,
                distance: 1,
-               helper: "original"
+               helper: "original",
+               scope: "default"
        }
 });
 
index 6833c4dfd37f408d9d8f98f0236f6e4e2ea8ff78..3e5d2999e50c59841267ae6ef9f5243a8275244e 100644 (file)
@@ -27,7 +27,8 @@ $.widget("ui.droppable", {
                this.proportions = { width: this.element[0].offsetWidth, height: this.element[0].offsetHeight };
                
                // Add the reference and positions to the manager
-               $.ui.ddmanager.droppables.push(this);
+               $.ui.ddmanager.droppables[this.options.scope] = $.ui.ddmanager.droppables[this.options.scope] || [];
+               $.ui.ddmanager.droppables[this.options.scope].push(this);
                
        },
        plugins: {},
@@ -42,7 +43,7 @@ $.widget("ui.droppable", {
                };
        },
        destroy: function() {
-               var drop = $.ui.ddmanager.droppables;
+               var drop = $.ui.ddmanager.droppables[this.options.scope];
                for ( var i = 0; i < drop.length; i++ )
                        if ( drop[i] == this )
                                drop.splice(i, 1);
@@ -116,7 +117,8 @@ $.widget("ui.droppable", {
 $.extend($.ui.droppable, {
        defaults: {
                disabled: false,
-               tolerance: 'intersect'
+               tolerance: 'intersect',
+               scope: 'default'
        }
 });
 
@@ -167,26 +169,31 @@ $.ui.intersect = function(draggable, droppable, toleranceMode) {
 */
 $.ui.ddmanager = {
        current: null,
-       droppables: [],
+       droppables: { scope: [] },
        prepareOffsets: function(t, e) {
                
-               var m = $.ui.ddmanager.droppables;
+               var m = $.ui.ddmanager.droppables[t.options.scope];
                var type = e ? e.type : null; // workaround for #2317
+               var list = (t.currentItem || t.element).find(":data(droppable)").andSelf();     
 
-               for (var i = 0; i < m.length; i++) {
-                       if(m[i].options.disabled || (t && !m[i].options.accept.call(m[i].element,(t.currentItem || t.element)))) continue;
-                       m[i].visible = m[i].element.css("display") != "none"; if(!m[i].visible) continue; //If the element is not visible, continue
+               droppablesLoop: for (var i = 0; i < m.length; i++) {
+                       
+                       if(m[i].options.disabled || (t && !m[i].options.accept.call(m[i].element,(t.currentItem || t.element)))) continue;      //No disabled and non-accepted
+                       for (var j=0; j < list.length; j++) { if(list[j] == m[i].element[0]) { m[i].proportions.height = 0; continue droppablesLoop; } }; //Filter out elements in the current dragged item
+                       m[i].visible = m[i].element.css("display") != "none"; if(!m[i].visible) continue;                                                                       //If the element is not visible, continue
+                       
                        m[i].offset = m[i].element.offset();
                        m[i].proportions = { width: m[i].element[0].offsetWidth, height: m[i].element[0].offsetHeight };
                        
-                       if(type == "dragstart" || type == "sortactivate") m[i].activate.call(m[i], e); //Activate the droppable if used directly from draggables
+                       if(type == "dragstart" || type == "sortactivate") m[i].activate.call(m[i], e);                                                                          //Activate the droppable if used directly from draggables
+                       
                }
                
        },
        drop: function(draggable, e) {
                
                var dropped = false;
-               $.each($.ui.ddmanager.droppables, function() {
+               $.each($.ui.ddmanager.droppables[draggable.options.scope], function() {
                        
                        if(!this.options) return;
                        if (!this.options.disabled && this.visible && $.ui.intersect(draggable, this, this.options.tolerance))
@@ -208,7 +215,7 @@ $.ui.ddmanager = {
                
                //Run through all droppables and check their positions based on specific tolerance options
 
-               $.each($.ui.ddmanager.droppables, function() {
+               $.each($.ui.ddmanager.droppables[draggable.options.scope], function() {
                        
                        if(this.options.disabled || this.greedyChild || !this.visible) return;
                        var intersects = $.ui.intersect(draggable, this, this.options.tolerance);
index 803531f43e37cd0e477714532be56a88c0c27c58..24c9348528d0d305135711c506e4fd47c5d07da1 100644 (file)
@@ -127,7 +127,7 @@ $.widget("ui.sortable", $.extend({}, $.ui.mouse, {
                                if ((x1 + dxClick) > l && (x1 + dxClick) < l + item.width/2) return 2;
                                if ((x1 + dxClick) > l + item.width/2 && (x1 + dxClick) < r) return 1;
                        } else {
-                               var height = item.height, helperHeight = this.helperProportions.height;
+                               var height = item.height;
                                var direction = y1 - this.updateOriginalPosition.top < 0 ? 2 : 1; // 2 = up
                                
                                if (direction == 1 && (y1 + dyClick) < t + height/2) { return 2; } // up
@@ -188,6 +188,21 @@ $.widget("ui.sortable", $.extend({}, $.ui.mouse, {
                
        },
        
+       removeCurrentsFromItems: function() {
+                       
+               var list = this.currentItem.find(":data(sortable-item)");       
+       
+               for (var i=0; i < this.items.length; i++) {
+                       
+                       for (var j=0; j < list.length; j++) {
+                               if(list[j] == this.items[i].item[0])
+                                       this.items.splice(i,1);
+                       };
+               
+               };
+               
+       },
+       
        refreshItems: function() {
                
                this.items = [];
@@ -371,6 +386,7 @@ $.widget("ui.sortable", $.extend({}, $.ui.mouse, {
                }
                        
                this.currentItem = currentItem;
+               this.removeCurrentsFromItems();
                return true;    
                        
        },
@@ -437,7 +453,7 @@ $.widget("ui.sortable", $.extend({}, $.ui.mouse, {
 
                //Call plugins and callbacks
                this.propagate("start", e);
-               this.helperProportions = { width: this.helper.outerWidth(), height: this.helper.outerHeight() };//Recache the helper size
+               if(!this._preserveHelperProportions) this.helperProportions = { width: this.helper.outerWidth(), height: this.helper.outerHeight() };//Recache the helper size
                
                if(o.cursorAt) {
                        if(o.cursorAt.left != undefined) this.offset.click.left = o.cursorAt.left;
@@ -705,7 +721,8 @@ $.extend($.ui.sortable, {
                zIndex: 1000,
                dropOnEmpty: true,
                appendTo: "parent",
-               sortIndicator: $.ui.sortable.prototype.rearrange
+               sortIndicator: $.ui.sortable.prototype.rearrange,
+               scope: "default"
        }
 });