|
22 | 22 | </style>
|
23 | 23 | <script>
|
24 | 24 | $(function() {
|
25 |
| - var tab_title_input = $( "#tab_title"), |
26 |
| - tab_content_input = $( "#tab_content" ), |
| 25 | + var tabTitle = $( "#tab_title" ), |
| 26 | + tabContent = $( "#tab_content" ), |
27 | 27 | tabTemplate = "<li><a href='#{href}'>#{label}</a> <span class='ui-icon ui-icon-close'>Remove Tab</span></li>",
|
28 |
| - tab_counter = 2; |
| 28 | + tabCounter = 2; |
29 | 29 |
|
30 |
| - // tabs init with a custom tab template and an "add" callback filling in the content |
31 |
| - var tabs = $( "#tabs").tabs({ |
32 |
| - add: function( event, ui ) { |
33 |
| - var tab_content = tab_content_input.val() || "Tab " + tab_counter + " content."; |
34 |
| - $( ui.panel ).append( "<p>" + tab_content + "</p>" ); |
35 |
| - } |
36 |
| - }); |
| 30 | + var tabs = $( "#tabs" ).tabs(); |
37 | 31 |
|
38 | 32 | // modal dialog init: custom buttons and a "close" callback reseting the form inside
|
39 | 33 | var dialog = $( "#dialog" ).dialog({
|
|
48 | 42 | $( this ).dialog( "close" );
|
49 | 43 | }
|
50 | 44 | },
|
51 |
| - open: function() { |
52 |
| - tab_title_input.focus(); |
53 |
| - }, |
54 | 45 | close: function() {
|
55 | 46 | form[ 0 ].reset();
|
56 | 47 | }
|
57 | 48 | });
|
58 | 49 |
|
59 | 50 | // addTab form: calls addTab function on submit and closes the dialog
|
60 |
| - var form = $( "form", dialog ).submit(function() { |
| 51 | + var form = dialog.find( "form" ).submit(function( event ) { |
61 | 52 | addTab();
|
62 | 53 | dialog.dialog( "close" );
|
63 |
| - return false; |
| 54 | + event.preventDefault(); |
64 | 55 | });
|
65 | 56 |
|
66 |
| - // actual addTab function: adds new tab using the title input from the form above |
| 57 | + // actual addTab function: adds new tab using the input from the form above |
67 | 58 | function addTab() {
|
68 |
| - var label = tab_title_input.val() || "Tab " + tab_counter, |
69 |
| - id = "tabs-" + tab_counter, |
| 59 | + var label = tabTitle.val() || "Tab " + tabCounter, |
| 60 | + id = "tabs-" + tabCounter, |
70 | 61 | li = $( tabTemplate.replace( /#\{href\}/g, "#" + id ).replace( /#\{label\}/g, label ) ),
|
71 |
| - tab_content = tab_content_input.val() || "Tab " + tab_counter + " content."; |
| 62 | + tabContentHtml = tabContent.val() || "Tab " + tabCounter + " content."; |
72 | 63 |
|
73 |
| - tabs.find('ul').append( li ); |
74 |
| - tabs.append( "<div id='" + id + "'><p>" + tab_content + "</p></div>" ); |
| 64 | + tabs.find( ".ui-tabs-nav" ).append( li ); |
| 65 | + tabs.append( "<div id='" + id + "'><p>" + tabContentHtml + "</p></div>" ); |
75 | 66 | tabs.tabs( "refresh" );
|
76 |
| - tab_counter++; |
| 67 | + tabCounter++; |
77 | 68 | }
|
78 | 69 |
|
79 | 70 | // addTab button: just opens the dialog
|
|
84 | 75 | });
|
85 | 76 |
|
86 | 77 | // close icon: removing the tab on click
|
87 |
| - // note: closable tabs gonna be an option in the future - see http://dev.jqueryui.com/ticket/3924 |
88 | 78 | $( "#tabs span.ui-icon-close" ).live( "click", function() {
|
89 | 79 | $( this ).closest( "li" ).remove();
|
90 | 80 | $( "#" + $( this ).prev().attr( "aria-controls" ) ).remove();
|
|
0 commit comments