Tabs: Cleaned up manipulation demo.

This commit is contained in:
Scott González 2011-05-11 15:16:04 -04:00
parent a02e393786
commit ffc983248d

View File

@ -22,18 +22,12 @@
</style> </style>
<script> <script>
$(function() { $(function() {
var tab_title_input = $( "#tab_title"), var tabTitle = $( "#tab_title" ),
tab_content_input = $( "#tab_content" ), tabContent = $( "#tab_content" ),
tabTemplate = "<li><a href='#{href}'>#{label}</a> <span class='ui-icon ui-icon-close'>Remove Tab</span></li>", tabTemplate = "<li><a href='#{href}'>#{label}</a> <span class='ui-icon ui-icon-close'>Remove Tab</span></li>",
tab_counter = 2; tabCounter = 2;
// tabs init with a custom tab template and an "add" callback filling in the content var tabs = $( "#tabs" ).tabs();
var tabs = $( "#tabs").tabs({
add: function( event, ui ) {
var tab_content = tab_content_input.val() || "Tab " + tab_counter + " content.";
$( ui.panel ).append( "<p>" + tab_content + "</p>" );
}
});
// modal dialog init: custom buttons and a "close" callback reseting the form inside // modal dialog init: custom buttons and a "close" callback reseting the form inside
var dialog = $( "#dialog" ).dialog({ var dialog = $( "#dialog" ).dialog({
@ -48,32 +42,29 @@
$( this ).dialog( "close" ); $( this ).dialog( "close" );
} }
}, },
open: function() {
tab_title_input.focus();
},
close: function() { close: function() {
form[ 0 ].reset(); form[ 0 ].reset();
} }
}); });
// addTab form: calls addTab function on submit and closes the dialog // addTab form: calls addTab function on submit and closes the dialog
var form = $( "form", dialog ).submit(function() { var form = dialog.find( "form" ).submit(function( event ) {
addTab(); addTab();
dialog.dialog( "close" ); dialog.dialog( "close" );
return false; event.preventDefault();
}); });
// actual addTab function: adds new tab using the title input from the form above // actual addTab function: adds new tab using the input from the form above
function addTab() { function addTab() {
var label = tab_title_input.val() || "Tab " + tab_counter, var label = tabTitle.val() || "Tab " + tabCounter,
id = "tabs-" + tab_counter, id = "tabs-" + tabCounter,
li = $( tabTemplate.replace( /#\{href\}/g, "#" + id ).replace( /#\{label\}/g, label ) ), li = $( tabTemplate.replace( /#\{href\}/g, "#" + id ).replace( /#\{label\}/g, label ) ),
tab_content = tab_content_input.val() || "Tab " + tab_counter + " content."; tabContentHtml = tabContent.val() || "Tab " + tabCounter + " content.";
tabs.find('ul').append( li ); tabs.find( ".ui-tabs-nav" ).append( li );
tabs.append( "<div id='" + id + "'><p>" + tab_content + "</p></div>" ); tabs.append( "<div id='" + id + "'><p>" + tabContentHtml + "</p></div>" );
tabs.tabs( "refresh" ); tabs.tabs( "refresh" );
tab_counter++; tabCounter++;
} }
// addTab button: just opens the dialog // addTab button: just opens the dialog
@ -84,7 +75,6 @@
}); });
// close icon: removing the tab on click // close icon: removing the tab on click
// note: closable tabs gonna be an option in the future - see http://dev.jqueryui.com/ticket/3924
$( "#tabs span.ui-icon-close" ).live( "click", function() { $( "#tabs span.ui-icon-close" ).live( "click", function() {
$( this ).closest( "li" ).remove(); $( this ).closest( "li" ).remove();
$( "#" + $( this ).prev().attr( "aria-controls" ) ).remove(); $( "#" + $( this ).prev().attr( "aria-controls" ) ).remove();