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