Bug Tracker

Changeset 3069

Show
Ignore:
Timestamp:
09/02/07 21:01:16 (1 year ago)
Author:
klaus.hartl
Message:

Tabs - work in progress

Location:
trunk/plugins/ui
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • trunk/plugins/ui/tests/tabs.html

    r3056 r3069  
    2626                    fxFade: true, 
    2727                    fxSpeed: 'fast', 
    28                     onClick: function() { 
    29                         alert('onClick'); 
     28                    click: function() { 
     29                        alert('click callback'); 
    3030                    }, 
    31                     onHide: function() { 
    32                         alert('onHide'); 
     31                    hide: function() { 
     32                        alert('hide callback'); 
    3333                    }, 
    34                     onShow: function() { 
    35                         alert('onShow'); 
     34                    show: function() { 
     35                        alert('show callback'); 
    3636                    } 
    3737                }); 
    3838                //$('#container-7 ul').tabs({ fxAutoHeight: true }); 
    3939                $('#container-7 ul').tabs({ fxShow: { height: 'show', opacity: 'show' }, fxSpeed: 'normal' }); 
    40                 $('#container8 ul').tabs(); 
     40                $('#container-8 ul').tabs(); 
    4141                $('#container-9 ul').tabs({ disabled: [3] }); 
    4242 
     
    317317        <div id="container-8"> 
    318318            <ul> 
    319                 <li><a href="ahah_1.html"><span>One</span></a></li> 
    320                 <li><a href="ahah_2.html"><span>Two</span></a></li> 
    321                 <li><a href="ahah_3.html"><span>Three</span></a></li> 
     319                <li><a href="ahah_1.html" title="Tab one"><span>One</span></a></li> 
     320                <li><a href="ahah_2.html" title="Tab two"><span>Two</span></a></li> 
     321                <li><a href="ahah_3.html" title="Tab three"><span>Three</span></a></li> 
    322322            </ul> 
    323323        </div> 
  • trunk/plugins/ui/ui.tabs.js

    r3056 r3069  
    88     
    99    $.fn.tabs = function(initial, options) { 
    10         if (initial && initial.constructor == Object) { 
     10        if (initial && initial.constructor == Object) { // shift arguments 
    1111            options = initial; 
    1212            initial = null; 
     
    2323     
    2424    // create chainable tabs methods 
    25     $.each(['add', 'remove'], function(i, method) { 
     25    $.each(['add', 'remove', 'enable', 'disable', 'show', 'load'], function(i, method) { 
    2626        $.fn[method + 'Tab'] = function() { 
    2727            var args = arguments; 
     
    4444            this.jQueryTabs.remove.apply(this.jQueryTabs, args); 
    4545        }); 
     46    }; 
     47     
     48    $.fn.enableTab = function(position) { 
     49        return this.each(function() { 
     50             
     51        }); 
     52    }; 
     53     
     54    $.fn.disableTab = function(position) { 
     55        return this.each(function() { 
     56             
     57        }); 
     58    }; 
     59     
     60    $.fn.showTab = function(position) { 
     61        return this.each(function() { 
     62             
     63        }); 
     64    }; 
     65     
     66    $.fn.loadTab = function(position, url, callback) { 
     67        // frequently requested: if url is passed reload tab with that url 
     68        return this.each(function() { 
     69             
     70        }); 
    4671    };*/ 
    47      
    48     $.fn.enableTab = function(position) { 
    49         return this.each(function() { 
    50              
    51         }); 
    52     }; 
    53      
    54     $.fn.disableTab = function(position) { 
    55         return this.each(function() { 
    56              
    57         }); 
    58     }; 
    59      
    60     $.fn.showTab = function(position) { 
    61         return this.each(function() { 
    62              
    63         }); 
    64     }; 
    65      
    66     $.fn.reloadTab = function(position, url) { 
    67         // frequently requested: if url is passed reload tab with that url 
    68         return this.each(function() { 
    69              
    70         }); 
    71     }; 
    7272     
    7373    $.fn.activeTab = function() { 
     
    8181        this.options = $.extend({ 
    8282            initial: 0, 
    83             /*disabled: null,*/ 
     83            disabled: [], 
    8484            // TODO bookmarkable: $.ajaxHistory ? true : false, 
    8585            spinner: 'Loading&#8230;', 
     
    8989            fxShow: null, 
    9090            fxHide: null,*/ 
     91            closable: false, // TODO 
     92            closed: false, // TODO selected? 
    9193            fxSpeed: 'normal', 
    9294            /*fxShowSpeed: null, 
    9395            fxHideSpeed: null,*/ 
    94             click: null, 
    95             show: null, 
     96            click: function() {}, 
     97            hide: function() {}, 
     98            show: function() {}, 
    9699            navClass: 'tabs-nav', 
    97100            selectedClass: 'tabs-selected', 
     
    118121                    instance.$containers = instance.$containers.add(a.hash); // jQuery's add() does not work somehow 
    119122                } else { // remote tab 
    120                     // TODO create and add container 
     123                    // TODO create unique ids 
     124                    var id = a.title && a.title.replace(/\s/g, '_') || options.hashPrefix + (i + 1), url = a.href; 
     125                    a.href = '#' + id; 
     126                    a.url = url; 
     127                    instance.$containers = instance.$containers.add( 
     128                        $('<div id="' + id + '" class="' + options.containerClass + '"></div>') 
     129                            .insertAfter( instance.$containers[i - 1] || $source ) 
     130                    ); 
    121131                } 
    122132            }); 
     
    162172            //tabs.eq(options.initial).trigger('loadRemoteTab').end(); 
    163173             
     174            // disabled tabs 
     175            for (var i = 0, k = options.disabled.length; i < k; i++) { 
     176                this.disable(--options.disabled[i]); 
     177            } 
     178             
    164179            // setup animations 
    165180            var showAnim = {}, hideAnim = {}, showSpeed = options.fxShowSpeed || options.fxSpeed, hideSpeed = options.fxHideSpeed || options.fxSpeed; 
     
    188203            } 
    189204             
     205            // callbacks 
     206            var click = options.click, hide = options.hide, show = options.show; 
     207             
    190208            var $containers = this.$containers; 
    191209             
     
    196214                //var trueClick = e.clientX; // add to history only if true click occured, not a triggered click 
    197215                var clicked = this, $li = $(this).parents('li:eq(0)'), $show = $(this.hash), $hide = $containers.filter(':visible'); 
    198  
     216                 
    199217                // if animation is still running, tab is selected or disabled or onClick callback returns false stop here 
    200218                // check if onClick returns false last so that it is not executed for a disabled tab 
    201                 if ($source['locked'] || $li.is('.' + options.selectedClass, '.' + options.disabledClass)  
    202                     || typeof onClick == 'function' && onClick(this, $show[0], $hide[0]) === false) { 
     219                if ($source['locked'] || $li.is('.' + options.selectedClass + ', .' + options.disabledClass)  
     220                    || click(this, $show[0], $hide[0]) === false) { 
    203221                    this.blur(); 
    204222                    return false; 
     
    208226 
    209227                // show new tab 
    210                 if ($show.size()) { 
     228                if ($show.length) { 
    211229 
    212230                    // prevent scrollbar scrolling to 0 and than back in IE7, happens only if bookmarking/history is enabled 
     
    232250                            $(clicked).parents('li:eq(0)').addClass(options.selectedClass).siblings().removeClass(options.selectedClass); 
    233251                            $hide.addClass(options.hideClass).css(resetCSS); // maintain flexible height and accessibility in print etc.                         
    234                             if (typeof onHide == 'function') { 
    235                                 onHide(clicked, $show[0], $hide[0]); 
    236                             } 
     252                            hide(clicked, $show[0], $hide[0]); 
    237253                            if (!(options.fxSlide || options.fxFade || options.fxShow)) { 
    238254                                $show.css('display', 'block'); // prevent occasionally occuring flicker in Firefox cause by gap between showing and hiding the tab containers 
     
    244260                                    $show[0].style.filter = ''; 
    245261                                } 
    246                                 if (typeof onShow == 'function') { 
    247                                     onShow(clicked, $show[0], $hide[0]); 
    248                                 } 
     262                                show(clicked, $show[0], $hide[0]); 
    249263                                $source['locked'] = null; 
    250264                            }); 
    251265                        }); 
    252266                    } 
    253                     switchTab(); 
    254  
    255                     /*if (!options.remote) { 
     267                     
     268                    if (this.url) { // remote tab 
     269                        instance.load(instance.$tabs.index(this) + 1, this.url, switchTab) 
     270                    } else { 
    256271                        switchTab(); 
    257                     } else { 
    258                         $(clicked).trigger('loadRemoteTab', [switchTab]); 
    259                     }*/ 
     272                    } 
     273 
     274                    // Set scrollbar to saved position - need to use timeout with 0 to prevent browser scroll to target of hash 
     275                    var scrollX = window.pageXOffset || document.documentElement && document.documentElement.scrollLeft || document.body.scrollLeft || 0; 
     276                    var scrollY = window.pageYOffset || document.documentElement && document.documentElement.scrollTop || document.body.scrollTop || 0; 
     277                    setTimeout(function() { 
     278                        scrollTo(scrollX, scrollY); 
     279                    }, 0); 
    260280 
    261281                } else { 
    262282                    alert('There is no such container.'); 
    263283                } 
    264  
    265                 // Set scrollbar to saved position - need to use timeout with 0 to prevent browser scroll to target of hash 
    266                 var scrollX = window.pageXOffset || document.documentElement && document.documentElement.scrollLeft || document.body.scrollLeft || 0; 
    267                 var scrollY = window.pageYOffset || document.documentElement && document.documentElement.scrollTop || document.body.scrollTop || 0; 
    268                 setTimeout(function() { 
    269                     scrollTo(scrollX, scrollY); 
    270                 }, 0); 
    271284 
    272285                this.blur(); // prevent IE from keeping other link focussed when using the back button 
     
    298311            }            
    299312        }, 
    300         remove: function(position) { // TODO callback 
     313        remove: function(position, callback) { 
    301314            if (position && position.constructor == Number) { 
    302315                --position; 
     
    306319                this.tabify(); 
    307320            } 
    308         }, 
    309         enable: function(position) { // TODO callback 
    310              
    311         }, 
    312         disable: function(position) { // TODO callback 
    313              
    314         }, 
    315         show: function(position) { // TODO callback 
    316              
    317         }, 
    318         reload: function(position, url) { // TODO callback 
    319              
     321            if (callback && callback.constructor == Function) { 
     322                callback(); 
     323            } 
     324        }, 
     325        enable: function(position, callback) { 
     326            var $li = this.$tabs.slice(position - 1, position).parents('li:eq(0)'); 
     327            $li.removeClass(this.options.disabledClass); 
     328            if ($.browser.safari) { /* fix disappearing tab after enabling in Safari... TODO check Safari 3 */ 
     329                $li.animate({ opacity: 1 }, 1, function() { 
     330                    $li.css({ opacity: '' }); 
     331                }); 
     332            } 
     333            if (callback && callback.constructor == Function) { 
     334                callback(); 
     335            } 
     336        }, 
     337        disable: function(position, callback) { 
     338            var $li = this.$tabs.slice(position - 1, position).parents('li:eq(0)');             
     339            if ($.browser.safari) { /* fix opacity of tab after disabling in Safari... TODO check Safari 3 */ 
     340                $li.animate({ opacity: 0 }, 1, function() { 
     341                   $li.css({ opacity: '' }); 
     342                }); 
     343            } 
     344            $li.addClass(this.options.disabledClass); 
     345            if (callback && callback.constructor == Function) { 
     346                callback(); 
     347            } 
     348        }, 
     349        show: function(position, callback) { // TODO callback 
     350             
     351        }, 
     352        load: function(position, url, callback) { // TODO callback 
     353            var options = this.options; 
     354            if (url && url.constructor == Function) {  // shift arguments 
     355                callback = url; 
     356            } 
     357            var $a = this.$tabs.slice(position - 1, position).addClass(options.loadingClass), 
     358                $span = $('span', $a), text = $span.html(); 
     359            if (options.spinner) { 
     360                // TODO if spinner is image 
     361                $span.html('<em>' + options.spinner + '</em>'); 
     362            } 
     363            setTimeout(function() { // Timeout is again required in IE, "wait" for id being restored 
     364                $($a[0].hash).load(url, function() { 
     365                    if (options.spinner) { 
     366                        $span.html(text); 
     367                    } 
     368                    $a.removeClass(options.loadingClass); 
     369                    if (callback && callback.constructor == Function) { 
     370                        callback(); 
     371                    } 
     372                }); 
     373            }, 0);             
    320374        } 
    321375    }); 
    322376     
    323      
    324377})(jQuery); 
    325378