Bug Tracker

Changeset 5704

Show
Ignore:
Timestamp:
05/28/08 09:36:28 (8 months ago)
Author:
paul.bakaus
Message:

treeview: fixed sortable tree component, made it up to date with UI

Location:
trunk/plugins/treeview
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • trunk/plugins/treeview/demo/sortable.html

    r5497 r5704  
    2222    <script src="../jquery.treeview.js" type="text/javascript"></script> 
    2323    <script src="../jquery.treeview.edit.js" type="text/javascript"></script> 
     24     
    2425    <script src="../../../ui/ui.core.js" type="text/javascript" charset="utf-8"></script> 
    2526    <script src="../../../ui/ui.droppable.js" type="text/javascript" charset="utf-8"></script> 
     
    7980        this.each(function() { 
    8081             
    81             $(this).treeview().sortable({ 
     82            $(this).treeview().sortableTree({ 
    8283                connectWith: o.connectWith, 
    8384                items: 'li', 
  • trunk/plugins/treeview/jquery.treeview.sortable.js

    r5497 r5704  
    2121    } 
    2222 
    23     $.fn.extend({ 
    24         sortable: function(options) { 
    25              
    26             var args = Array.prototype.slice.call(arguments, 1); 
    27              
    28             if (options == "serialize" || options == "toArray") 
    29                 return $.data(this[0], "sortable")[options](arguments[1]); 
    30              
    31             return this.each(function() { 
    32                 if (typeof options == "string") { 
    33                     var sort = $.data(this, "sortable"); 
    34                     if (sort) sort[options].apply(sort, args); 
    35  
    36                 } else if(!$.data(this, "sortable")) 
    37                     new $.ui.sortable(this, options); 
    38             }); 
    39         } 
    40     }); 
     23 
     24    $.widget("ui.sortableTree", $.extend($.ui.mouse, { 
     25        init: function() { 
     26 
     27            //Initialize needed constants 
     28            var self = this, o = this.options; 
     29            this.containerCache = {}; 
     30            this.element.addClass("ui-sortableTree"); 
     31             
     32            //Get the items 
     33            this.refresh(); 
     34             
     35            //Let's determine the parent's offset 
     36            if(!(/(relative|absolute|fixed)/).test(this.element.css('position'))) this.element.css('position', 'relative'); 
     37            this.offset = this.element.offset(); 
    4138     
    42     $.ui.sortable = function(element, options) { 
    43         //Initialize needed constants 
    44         var self = this; 
    45      
    46         this.element = $(element); 
    47         this.containerCache = {}; 
    48          
    49         $.data(element, "sortable", this); 
    50         this.element.addClass("ui-sortable"); 
    51  
    52         //Prepare the passed options 
    53         this.options = $.extend({}, options); 
    54         var o = this.options; 
    55         $.extend(o, { 
    56             items: this.options.items || '> *', 
    57             zIndex: this.options.zIndex || 1000, 
    58             startCondition: function() { 
    59                 return !self.options.disabled; 
    60             } 
    61         }); 
    62          
    63         $(element).bind("setData.sortable", function(event, key, value){ 
    64             self.options[key] = value; 
    65         }).bind("getData.sortable", function(event, key){ 
    66             return self.options[key]; 
    67         }); 
    68          
    69         //Get the items 
    70         this.refresh(); 
    71          
    72         //Let's determine the parent's offset 
    73         if(!(/(relative|absolute|fixed)/).test(this.element.css('position'))) this.element.css('position', 'relative'); 
    74         this.offset = this.element.offset(); 
    75  
    76         //Initialize mouse events for interaction 
    77         this.element.mouse({ 
    78             executor: this, 
    79             delay: o.delay, 
    80             distance: o.distance || 1, 
    81             dragPrevention: o.prevention ? o.prevention.toLowerCase().split(',') : ['input','textarea','button','select','option'], 
    82             start: this.start, 
    83             stop: this.stop, 
    84             drag: this.drag, 
    85             condition: function(e) { 
    86  
    87                 if(this.options.disabled || this.options.type == 'static') return false; 
    88  
    89                 //Find out if the clicked node (or one of its parents) is a actual item in this.items 
    90                 var currentItem = null, nodes = $(e.target).parents().each(function() {  
    91                     if($.data(this, 'sortable-item')) { 
    92                         currentItem = $(this); 
    93                         return false; 
    94                     } 
    95                 }); 
    96                 if($.data(e.target, 'sortable-item')) currentItem = $(e.target); 
    97                  
    98                 if(!currentItem) return false;   
    99                 if(this.options.handle) { 
    100                     var validHandle = false; 
    101                     $(this.options.handle, currentItem).each(function() { if(this == e.target) validHandle = true; }); 
    102                     if(!validHandle) return false; 
    103                 } 
    104                      
    105                 this.currentItem = currentItem; 
    106                 return true; 
    107  
    108             } 
    109         }); 
    110          
    111         //Prepare cursorAt 
    112         if(o.cursorAt && o.cursorAt.constructor == Array) 
    113             o.cursorAt = { left: o.cursorAt[0], top: o.cursorAt[1] }; 
    114     }; 
    115      
    116     $.extend($.ui.sortable.prototype, { 
     39            //Initialize mouse events for interaction 
     40            this.mouseInit(); 
     41             
     42            //Prepare cursorAt 
     43            if(o.cursorAt && o.cursorAt.constructor == Array) 
     44                o.cursorAt = { left: o.cursorAt[0], top: o.cursorAt[1] }; 
     45 
     46        }, 
    11747        plugins: {}, 
    11848        ui: function(inst) { 
     
    13464        serialize: function(o) { 
    13565             
    136             var items = $(this.options.items, this.element).not('.ui-sortable-helper'); //Only the items of the sortable itself 
     66            var items = $(this.options.items, this.element).not('.ui-sortableTree-helper'); //Only the items of the sortable itself 
    13767            var str = []; o = o || {}; 
    13868             
     
    14676        }, 
    14777        toArray: function(attr) { 
    148             var items = $(this.options.items, this.element).not('.ui-sortable-helper'); //Only the items of the sortable itself 
     78            var items = $(this.options.items, this.element).not('.ui-sortableTree-helper'); //Only the items of the sortable itself 
    14979            var ret = []; 
    15080 
     
    15383        }, 
    15484        enable: function() { 
    155             this.element.removeClass("ui-sortable-disabled"); 
     85            this.element.removeClass("ui-sortableTree-disabled"); 
    15686            this.options.disabled = false; 
    15787        }, 
    15888        disable: function() { 
    159             this.element.addClass("ui-sortable-disabled"); 
     89            this.element.addClass("ui-sortableTree-disabled"); 
    16090            this.options.disabled = true; 
    16191        }, 
     
    178108            var t = item.top,  b = t + item.height; 
    179109 
    180             if(!this.intersectsWith(item.item.parents(".ui-sortable").data("sortable").containerCache)) return false; 
     110            if(!this.intersectsWith(item.item.parents(".ui-sortableTree").data("sortableTree").containerCache)) return false; 
    181111 
    182112            if (!( t < y1 + (this.helperProportions.height / 2)        // Bottom Half 
     
    204134                    var cur = $(this.options.connectWith[i]); 
    205135                    for (var j = cur.length - 1; j >= 0; j--){ 
    206                         var inst = $.data(cur[j], 'sortable'); 
     136                        var inst = $.data(cur[j], 'sortableTree'); 
    207137                        if(inst && !inst.options.disabled) { 
    208138                            queries.push($(inst.options.items, inst.element)); 
     
    215145            for (var i = queries.length - 1; i >= 0; i--){ 
    216146                queries[i].each(function() { 
    217                     $.data(this, 'sortable-item', true); // Data for target checking (mouse manager) 
     147                    $.data(this, 'sortableTree-item', true); // Data for target checking (mouse manager) 
    218148                    items.push({ 
    219149                        item: $(this), 
     
    239169        }, 
    240170        destroy: function() { 
     171 
    241172            this.element 
    242                 .removeClass("ui-sortable ui-sortable-disabled") 
    243                 .removeData("sortable") 
    244                 .unbind(".sortable") 
    245                 .removeMouseInteraction(); 
     173                .removeClass("ui-sortableTree ui-sortableTree-disabled") 
     174                .removeData("sortableTree") 
     175                .unbind(".sortableTree"); 
     176            this.mouseDestroy(); 
    246177             
    247178            for ( var i = this.items.length - 1; i >= 0; i-- ) 
    248                 this.items[i].item.removeData("sortable-item"); 
     179                this.items[i].item.removeData("sortableTree-item"); 
     180                 
    249181        }, 
    250182        contactContainers: function(e) { 
     
    285217            };           
    286218        }, 
    287         start: function(e,el) { 
     219        mouseStart: function(e,el) { 
     220 
     221            if(this.options.disabled || this.options.type == 'static') return false; 
     222 
     223            //Find out if the clicked node (or one of its parents) is a actual item in this.items 
     224            var currentItem = null, nodes = $(e.target).parents().each(function() {  
     225                if($.data(this, 'sortableTree-item')) { 
     226                    currentItem = $(this); 
     227                    return false; 
     228                } 
     229            }); 
     230            if($.data(e.target, 'sortableTree-item')) currentItem = $(e.target); 
     231             
     232            if(!currentItem) return false;   
     233            if(this.options.handle) { 
     234                var validHandle = false; 
     235                $(this.options.handle, currentItem).each(function() { if(this == e.target) validHandle = true; }); 
     236                if(!validHandle) return false; 
     237            } 
     238                 
     239            this.currentItem = currentItem; 
    288240             
    289241            var o = this.options; 
     
    294246            this.helper = typeof o.helper == 'function' ? $(o.helper.apply(this.element[0], [e, this.currentItem])) : this.currentItem.clone(); 
    295247            if(!this.helper.parents('body').length) this.helper.appendTo("body"); //Add the helper to the DOM if that didn't happen already 
    296             this.helper.css({ position: 'absolute', clear: 'both' }).addClass('ui-sortable-helper'); //Position it absolutely and add a helper class 
     248            this.helper.css({ position: 'absolute', clear: 'both' }).addClass('ui-sortableTree-helper'); //Position it absolutely and add a helper class 
    297249             
    298250            //Prepare variables for position generation 
     
    324276 
    325277            this.dragging = true; 
    326             return false; 
    327              
    328         }, 
    329         stop: function(e) { 
     278            return true; 
     279             
     280        }, 
     281        mouseStop: function(e) { 
    330282 
    331283            if(this.newPositionAt) this.options.sortIndication.remove.call(this.currentItem, this.newPositionAt); //remove sort indicator 
     
    363315             
    364316        }, 
    365         drag: function(e) { 
     317        mouseDrag: function(e) { 
    366318 
    367319            //Compute the helpers position 
     
    411363            } 
    412364        } 
     365    })); 
     366     
     367    $.extend($.ui.sortableTree, { 
     368        defaults: { 
     369            items: '> *', 
     370            zIndex: 1000, 
     371            distance: 1 
     372        }, 
     373        getter: "serialize toArray" 
    413374    }); 
    414375