tabs.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892
  1. /*!
  2. * jQuery UI Tabs 1.14.1
  3. * https://jqueryui.com
  4. *
  5. * Copyright OpenJS Foundation and other contributors
  6. * Released under the MIT license.
  7. * https://jquery.org/license
  8. */
  9. //>>label: Tabs
  10. //>>group: Widgets
  11. //>>description: Transforms a set of container elements into a tab structure.
  12. //>>docs: https://api.jqueryui.com/tabs/
  13. //>>demos: https://jqueryui.com/tabs/
  14. //>>css.structure: ../../themes/base/core.css
  15. //>>css.structure: ../../themes/base/tabs.css
  16. //>>css.theme: ../../themes/base/theme.css
  17. ( function( factory ) {
  18. "use strict";
  19. if ( typeof define === "function" && define.amd ) {
  20. // AMD. Register as an anonymous module.
  21. define( [
  22. "jquery",
  23. "../keycode",
  24. "../unique-id",
  25. "../version",
  26. "../widget"
  27. ], factory );
  28. } else {
  29. // Browser globals
  30. factory( jQuery );
  31. }
  32. } )( function( $ ) {
  33. "use strict";
  34. $.widget( "ui.tabs", {
  35. version: "1.14.1",
  36. delay: 300,
  37. options: {
  38. active: null,
  39. classes: {
  40. "ui-tabs": "ui-corner-all",
  41. "ui-tabs-nav": "ui-corner-all",
  42. "ui-tabs-panel": "ui-corner-bottom",
  43. "ui-tabs-tab": "ui-corner-top"
  44. },
  45. collapsible: false,
  46. event: "click",
  47. heightStyle: "content",
  48. hide: null,
  49. show: null,
  50. // Callbacks
  51. activate: null,
  52. beforeActivate: null,
  53. beforeLoad: null,
  54. load: null
  55. },
  56. _isLocal: ( function() {
  57. var rhash = /#.*$/;
  58. return function( anchor ) {
  59. var anchorUrl, locationUrl;
  60. anchorUrl = anchor.href.replace( rhash, "" );
  61. locationUrl = location.href.replace( rhash, "" );
  62. // Decoding may throw an error if the URL isn't UTF-8 (#9518)
  63. try {
  64. anchorUrl = decodeURIComponent( anchorUrl );
  65. } catch ( error ) {}
  66. try {
  67. locationUrl = decodeURIComponent( locationUrl );
  68. } catch ( error ) {}
  69. return anchor.hash.length > 1 && anchorUrl === locationUrl;
  70. };
  71. } )(),
  72. _create: function() {
  73. var that = this,
  74. options = this.options;
  75. this.running = false;
  76. this._addClass( "ui-tabs", "ui-widget ui-widget-content" );
  77. this._toggleClass( "ui-tabs-collapsible", null, options.collapsible );
  78. this._processTabs();
  79. options.active = this._initialActive();
  80. // Take disabling tabs via class attribute from HTML
  81. // into account and update option properly.
  82. if ( Array.isArray( options.disabled ) ) {
  83. options.disabled = $.uniqueSort( options.disabled.concat(
  84. $.map( this.tabs.filter( ".ui-state-disabled" ), function( li ) {
  85. return that.tabs.index( li );
  86. } )
  87. ) ).sort();
  88. }
  89. // Check for length avoids error when initializing empty list
  90. if ( this.options.active !== false && this.anchors.length ) {
  91. this.active = this._findActive( options.active );
  92. } else {
  93. this.active = $();
  94. }
  95. this._refresh();
  96. if ( this.active.length ) {
  97. this.load( options.active );
  98. }
  99. },
  100. _initialActive: function() {
  101. var active = this.options.active,
  102. collapsible = this.options.collapsible,
  103. locationHashDecoded = decodeURIComponent( location.hash.substring( 1 ) );
  104. if ( active === null ) {
  105. // check the fragment identifier in the URL
  106. if ( locationHashDecoded ) {
  107. this.tabs.each( function( i, tab ) {
  108. if ( $( tab ).attr( "aria-controls" ) === locationHashDecoded ) {
  109. active = i;
  110. return false;
  111. }
  112. } );
  113. }
  114. // Check for a tab marked active via a class
  115. if ( active === null ) {
  116. active = this.tabs.index( this.tabs.filter( ".ui-tabs-active" ) );
  117. }
  118. // No active tab, set to false
  119. if ( active === null || active === -1 ) {
  120. active = this.tabs.length ? 0 : false;
  121. }
  122. }
  123. // Handle numbers: negative, out of range
  124. if ( active !== false ) {
  125. active = this.tabs.index( this.tabs.eq( active ) );
  126. if ( active === -1 ) {
  127. active = collapsible ? false : 0;
  128. }
  129. }
  130. // Don't allow collapsible: false and active: false
  131. if ( !collapsible && active === false && this.anchors.length ) {
  132. active = 0;
  133. }
  134. return active;
  135. },
  136. _getCreateEventData: function() {
  137. return {
  138. tab: this.active,
  139. panel: !this.active.length ? $() : this._getPanelForTab( this.active )
  140. };
  141. },
  142. _tabKeydown: function( event ) {
  143. var focusedTab = $( this.document[ 0 ].activeElement ).closest( "li" ),
  144. selectedIndex = this.tabs.index( focusedTab ),
  145. goingForward = true;
  146. if ( this._handlePageNav( event ) ) {
  147. return;
  148. }
  149. switch ( event.keyCode ) {
  150. case $.ui.keyCode.RIGHT:
  151. case $.ui.keyCode.DOWN:
  152. selectedIndex++;
  153. break;
  154. case $.ui.keyCode.UP:
  155. case $.ui.keyCode.LEFT:
  156. goingForward = false;
  157. selectedIndex--;
  158. break;
  159. case $.ui.keyCode.END:
  160. selectedIndex = this.anchors.length - 1;
  161. break;
  162. case $.ui.keyCode.HOME:
  163. selectedIndex = 0;
  164. break;
  165. case $.ui.keyCode.SPACE:
  166. // Activate only, no collapsing
  167. event.preventDefault();
  168. clearTimeout( this.activating );
  169. this._activate( selectedIndex );
  170. return;
  171. case $.ui.keyCode.ENTER:
  172. // Toggle (cancel delayed activation, allow collapsing)
  173. event.preventDefault();
  174. clearTimeout( this.activating );
  175. // Determine if we should collapse or activate
  176. this._activate( selectedIndex === this.options.active ? false : selectedIndex );
  177. return;
  178. default:
  179. return;
  180. }
  181. // Focus the appropriate tab, based on which key was pressed
  182. event.preventDefault();
  183. clearTimeout( this.activating );
  184. selectedIndex = this._focusNextTab( selectedIndex, goingForward );
  185. // Navigating with control/command key will prevent automatic activation
  186. if ( !event.ctrlKey && !event.metaKey ) {
  187. // Update aria-selected immediately so that AT think the tab is already selected.
  188. // Otherwise AT may confuse the user by stating that they need to activate the tab,
  189. // but the tab will already be activated by the time the announcement finishes.
  190. focusedTab.attr( "aria-selected", "false" );
  191. this.tabs.eq( selectedIndex ).attr( "aria-selected", "true" );
  192. this.activating = this._delay( function() {
  193. this.option( "active", selectedIndex );
  194. }, this.delay );
  195. }
  196. },
  197. _panelKeydown: function( event ) {
  198. if ( this._handlePageNav( event ) ) {
  199. return;
  200. }
  201. // Ctrl+up moves focus to the current tab
  202. if ( event.ctrlKey && event.keyCode === $.ui.keyCode.UP ) {
  203. event.preventDefault();
  204. this.active.trigger( "focus" );
  205. }
  206. },
  207. // Alt+page up/down moves focus to the previous/next tab (and activates)
  208. _handlePageNav: function( event ) {
  209. if ( event.altKey && event.keyCode === $.ui.keyCode.PAGE_UP ) {
  210. this._activate( this._focusNextTab( this.options.active - 1, false ) );
  211. return true;
  212. }
  213. if ( event.altKey && event.keyCode === $.ui.keyCode.PAGE_DOWN ) {
  214. this._activate( this._focusNextTab( this.options.active + 1, true ) );
  215. return true;
  216. }
  217. },
  218. _findNextTab: function( index, goingForward ) {
  219. var lastTabIndex = this.tabs.length - 1;
  220. function constrain() {
  221. if ( index > lastTabIndex ) {
  222. index = 0;
  223. }
  224. if ( index < 0 ) {
  225. index = lastTabIndex;
  226. }
  227. return index;
  228. }
  229. while ( $.inArray( constrain(), this.options.disabled ) !== -1 ) {
  230. index = goingForward ? index + 1 : index - 1;
  231. }
  232. return index;
  233. },
  234. _focusNextTab: function( index, goingForward ) {
  235. index = this._findNextTab( index, goingForward );
  236. this.tabs.eq( index ).trigger( "focus" );
  237. return index;
  238. },
  239. _setOption: function( key, value ) {
  240. if ( key === "active" ) {
  241. // _activate() will handle invalid values and update this.options
  242. this._activate( value );
  243. return;
  244. }
  245. this._super( key, value );
  246. if ( key === "collapsible" ) {
  247. this._toggleClass( "ui-tabs-collapsible", null, value );
  248. // Setting collapsible: false while collapsed; open first panel
  249. if ( !value && this.options.active === false ) {
  250. this._activate( 0 );
  251. }
  252. }
  253. if ( key === "event" ) {
  254. this._setupEvents( value );
  255. }
  256. if ( key === "heightStyle" ) {
  257. this._setupHeightStyle( value );
  258. }
  259. },
  260. refresh: function() {
  261. var options = this.options,
  262. lis = this.tablist.children( ":has(a[href])" );
  263. // Get disabled tabs from class attribute from HTML
  264. // this will get converted to a boolean if needed in _refresh()
  265. options.disabled = $.map( lis.filter( ".ui-state-disabled" ), function( tab ) {
  266. return lis.index( tab );
  267. } );
  268. this._processTabs();
  269. // Was collapsed or no tabs
  270. if ( options.active === false || !this.anchors.length ) {
  271. options.active = false;
  272. this.active = $();
  273. // was active, but active tab is gone
  274. } else if ( this.active.length && !$.contains( this.tablist[ 0 ], this.active[ 0 ] ) ) {
  275. // all remaining tabs are disabled
  276. if ( this.tabs.length === options.disabled.length ) {
  277. options.active = false;
  278. this.active = $();
  279. // activate previous tab
  280. } else {
  281. this._activate( this._findNextTab( Math.max( 0, options.active - 1 ), false ) );
  282. }
  283. // was active, active tab still exists
  284. } else {
  285. // make sure active index is correct
  286. options.active = this.tabs.index( this.active );
  287. }
  288. this._refresh();
  289. },
  290. _refresh: function() {
  291. this._setOptionDisabled( this.options.disabled );
  292. this._setupEvents( this.options.event );
  293. this._setupHeightStyle( this.options.heightStyle );
  294. this.tabs.not( this.active ).attr( {
  295. "aria-selected": "false",
  296. "aria-expanded": "false",
  297. tabIndex: -1
  298. } );
  299. this.panels.not( this._getPanelForTab( this.active ) )
  300. .hide()
  301. .attr( {
  302. "aria-hidden": "true"
  303. } );
  304. // Make sure one tab is in the tab order
  305. if ( !this.active.length ) {
  306. this.tabs.eq( 0 ).attr( "tabIndex", 0 );
  307. } else {
  308. this.active
  309. .attr( {
  310. "aria-selected": "true",
  311. "aria-expanded": "true",
  312. tabIndex: 0
  313. } );
  314. this._addClass( this.active, "ui-tabs-active", "ui-state-active" );
  315. this._getPanelForTab( this.active )
  316. .show()
  317. .attr( {
  318. "aria-hidden": "false"
  319. } );
  320. }
  321. },
  322. _processTabs: function() {
  323. var that = this,
  324. prevTabs = this.tabs,
  325. prevAnchors = this.anchors,
  326. prevPanels = this.panels;
  327. this.tablist = this._getList().attr( "role", "tablist" );
  328. this._addClass( this.tablist, "ui-tabs-nav",
  329. "ui-helper-reset ui-helper-clearfix ui-widget-header" );
  330. // Prevent users from focusing disabled tabs via click
  331. this.tablist
  332. .on( "mousedown" + this.eventNamespace, "> li", function( event ) {
  333. if ( $( this ).is( ".ui-state-disabled" ) ) {
  334. event.preventDefault();
  335. }
  336. } );
  337. this.tabs = this.tablist.find( "> li:has(a[href])" )
  338. .attr( {
  339. role: "tab",
  340. tabIndex: -1
  341. } );
  342. this._addClass( this.tabs, "ui-tabs-tab", "ui-state-default" );
  343. this.anchors = this.tabs.map( function() {
  344. return $( "a", this )[ 0 ];
  345. } )
  346. .attr( {
  347. tabIndex: -1
  348. } );
  349. this._addClass( this.anchors, "ui-tabs-anchor" );
  350. this.panels = $();
  351. this.anchors.each( function( i, anchor ) {
  352. var selector, panel, panelId,
  353. anchorId = $( anchor ).uniqueId().attr( "id" ),
  354. tab = $( anchor ).closest( "li" ),
  355. originalAriaControls = tab.attr( "aria-controls" );
  356. // Inline tab
  357. if ( that._isLocal( anchor ) ) {
  358. selector = decodeURIComponent( anchor.hash );
  359. panelId = selector.substring( 1 );
  360. panel = that.element.find( "#" + CSS.escape( panelId ) );
  361. // remote tab
  362. } else {
  363. // If the tab doesn't already have aria-controls,
  364. // generate an id by using a throw-away element
  365. panelId = tab.attr( "aria-controls" ) || $( {} ).uniqueId()[ 0 ].id;
  366. selector = "#" + panelId;
  367. panel = that.element.find( selector );
  368. if ( !panel.length ) {
  369. panel = that._createPanel( panelId );
  370. panel.insertAfter( that.panels[ i - 1 ] || that.tablist );
  371. }
  372. panel.attr( "aria-live", "polite" );
  373. }
  374. if ( panel.length ) {
  375. that.panels = that.panels.add( panel );
  376. }
  377. if ( originalAriaControls ) {
  378. tab.data( "ui-tabs-aria-controls", originalAriaControls );
  379. }
  380. tab.attr( {
  381. "aria-controls": panelId,
  382. "aria-labelledby": anchorId
  383. } );
  384. panel.attr( "aria-labelledby", anchorId );
  385. } );
  386. this.panels.attr( "role", "tabpanel" );
  387. this._addClass( this.panels, "ui-tabs-panel", "ui-widget-content" );
  388. // Avoid memory leaks (#10056)
  389. if ( prevTabs ) {
  390. this._off( prevTabs.not( this.tabs ) );
  391. this._off( prevAnchors.not( this.anchors ) );
  392. this._off( prevPanels.not( this.panels ) );
  393. }
  394. },
  395. // Allow overriding how to find the list for rare usage scenarios (#7715)
  396. _getList: function() {
  397. return this.tablist || this.element.find( "ol, ul" ).eq( 0 );
  398. },
  399. _createPanel: function( id ) {
  400. return $( "<div>" )
  401. .attr( "id", id )
  402. .data( "ui-tabs-destroy", true );
  403. },
  404. _setOptionDisabled: function( disabled ) {
  405. var currentItem, li, i;
  406. if ( Array.isArray( disabled ) ) {
  407. if ( !disabled.length ) {
  408. disabled = false;
  409. } else if ( disabled.length === this.anchors.length ) {
  410. disabled = true;
  411. }
  412. }
  413. // Disable tabs
  414. for ( i = 0; ( li = this.tabs[ i ] ); i++ ) {
  415. currentItem = $( li );
  416. if ( disabled === true || $.inArray( i, disabled ) !== -1 ) {
  417. currentItem.attr( "aria-disabled", "true" );
  418. this._addClass( currentItem, null, "ui-state-disabled" );
  419. } else {
  420. currentItem.removeAttr( "aria-disabled" );
  421. this._removeClass( currentItem, null, "ui-state-disabled" );
  422. }
  423. }
  424. this.options.disabled = disabled;
  425. this._toggleClass( this.widget(), this.widgetFullName + "-disabled", null,
  426. disabled === true );
  427. },
  428. _setupEvents: function( event ) {
  429. var events = {};
  430. if ( event ) {
  431. $.each( event.split( " " ), function( index, eventName ) {
  432. events[ eventName ] = "_eventHandler";
  433. } );
  434. }
  435. this._off( this.anchors.add( this.tabs ).add( this.panels ) );
  436. // Always prevent the default action, even when disabled
  437. this._on( true, this.anchors, {
  438. click: function( event ) {
  439. event.preventDefault();
  440. }
  441. } );
  442. this._on( this.anchors, events );
  443. this._on( this.tabs, { keydown: "_tabKeydown" } );
  444. this._on( this.panels, { keydown: "_panelKeydown" } );
  445. this._focusable( this.tabs );
  446. this._hoverable( this.tabs );
  447. },
  448. _setupHeightStyle: function( heightStyle ) {
  449. var maxHeight,
  450. parent = this.element.parent();
  451. if ( heightStyle === "fill" ) {
  452. maxHeight = parent.height();
  453. maxHeight -= this.element.outerHeight() - this.element.height();
  454. this.element.siblings( ":visible" ).each( function() {
  455. var elem = $( this ),
  456. position = elem.css( "position" );
  457. if ( position === "absolute" || position === "fixed" ) {
  458. return;
  459. }
  460. maxHeight -= elem.outerHeight( true );
  461. } );
  462. this.element.children().not( this.panels ).each( function() {
  463. maxHeight -= $( this ).outerHeight( true );
  464. } );
  465. this.panels.each( function() {
  466. $( this ).height( Math.max( 0, maxHeight -
  467. $( this ).innerHeight() + $( this ).height() ) );
  468. } )
  469. .css( "overflow", "auto" );
  470. } else if ( heightStyle === "auto" ) {
  471. maxHeight = 0;
  472. this.panels.each( function() {
  473. maxHeight = Math.max( maxHeight, $( this ).height( "" ).height() );
  474. } ).height( maxHeight );
  475. }
  476. },
  477. _eventHandler: function( event ) {
  478. var options = this.options,
  479. active = this.active,
  480. anchor = $( event.currentTarget ),
  481. tab = anchor.closest( "li" ),
  482. clickedIsActive = tab[ 0 ] === active[ 0 ],
  483. collapsing = clickedIsActive && options.collapsible,
  484. toShow = collapsing ? $() : this._getPanelForTab( tab ),
  485. toHide = !active.length ? $() : this._getPanelForTab( active ),
  486. eventData = {
  487. oldTab: active,
  488. oldPanel: toHide,
  489. newTab: collapsing ? $() : tab,
  490. newPanel: toShow
  491. };
  492. event.preventDefault();
  493. if ( tab.hasClass( "ui-state-disabled" ) ||
  494. // tab is already loading
  495. tab.hasClass( "ui-tabs-loading" ) ||
  496. // can't switch durning an animation
  497. this.running ||
  498. // click on active header, but not collapsible
  499. ( clickedIsActive && !options.collapsible ) ||
  500. // allow canceling activation
  501. ( this._trigger( "beforeActivate", event, eventData ) === false ) ) {
  502. return;
  503. }
  504. options.active = collapsing ? false : this.tabs.index( tab );
  505. this.active = clickedIsActive ? $() : tab;
  506. if ( this.xhr ) {
  507. this.xhr.abort();
  508. }
  509. if ( !toHide.length && !toShow.length ) {
  510. $.error( "jQuery UI Tabs: Mismatching fragment identifier." );
  511. }
  512. if ( toShow.length ) {
  513. this.load( this.tabs.index( tab ), event );
  514. }
  515. this._toggle( event, eventData );
  516. },
  517. // Handles show/hide for selecting tabs
  518. _toggle: function( event, eventData ) {
  519. var that = this,
  520. toShow = eventData.newPanel,
  521. toHide = eventData.oldPanel;
  522. this.running = true;
  523. function complete() {
  524. that.running = false;
  525. that._trigger( "activate", event, eventData );
  526. }
  527. function show() {
  528. that._addClass( eventData.newTab.closest( "li" ), "ui-tabs-active", "ui-state-active" );
  529. if ( toShow.length && that.options.show ) {
  530. that._show( toShow, that.options.show, complete );
  531. } else {
  532. toShow.show();
  533. complete();
  534. }
  535. }
  536. // Start out by hiding, then showing, then completing
  537. if ( toHide.length && this.options.hide ) {
  538. this._hide( toHide, this.options.hide, function() {
  539. that._removeClass( eventData.oldTab.closest( "li" ),
  540. "ui-tabs-active", "ui-state-active" );
  541. show();
  542. } );
  543. } else {
  544. this._removeClass( eventData.oldTab.closest( "li" ),
  545. "ui-tabs-active", "ui-state-active" );
  546. toHide.hide();
  547. show();
  548. }
  549. toHide.attr( "aria-hidden", "true" );
  550. eventData.oldTab.attr( {
  551. "aria-selected": "false",
  552. "aria-expanded": "false"
  553. } );
  554. // If we're switching tabs, remove the old tab from the tab order.
  555. // If we're opening from collapsed state, remove the previous tab from the tab order.
  556. // If we're collapsing, then keep the collapsing tab in the tab order.
  557. if ( toShow.length && toHide.length ) {
  558. eventData.oldTab.attr( "tabIndex", -1 );
  559. } else if ( toShow.length ) {
  560. this.tabs.filter( function() {
  561. return $( this ).attr( "tabIndex" ) === 0;
  562. } )
  563. .attr( "tabIndex", -1 );
  564. }
  565. toShow.attr( "aria-hidden", "false" );
  566. eventData.newTab.attr( {
  567. "aria-selected": "true",
  568. "aria-expanded": "true",
  569. tabIndex: 0
  570. } );
  571. },
  572. _activate: function( index ) {
  573. var anchor,
  574. active = this._findActive( index );
  575. // Trying to activate the already active panel
  576. if ( active[ 0 ] === this.active[ 0 ] ) {
  577. return;
  578. }
  579. // Trying to collapse, simulate a click on the current active header
  580. if ( !active.length ) {
  581. active = this.active;
  582. }
  583. anchor = active.find( ".ui-tabs-anchor" )[ 0 ];
  584. this._eventHandler( {
  585. target: anchor,
  586. currentTarget: anchor,
  587. preventDefault: $.noop
  588. } );
  589. },
  590. _findActive: function( index ) {
  591. return index === false ? $() : this.tabs.eq( index );
  592. },
  593. _getIndex: function( index ) {
  594. // meta-function to give users option to provide a href string instead of a numerical index.
  595. if ( typeof index === "string" ) {
  596. index = this.anchors.index( this.anchors.filter( "[href$='" +
  597. CSS.escape( index ) + "']" ) );
  598. }
  599. return index;
  600. },
  601. _destroy: function() {
  602. if ( this.xhr ) {
  603. this.xhr.abort();
  604. }
  605. this.tablist
  606. .removeAttr( "role" )
  607. .off( this.eventNamespace );
  608. this.anchors
  609. .removeAttr( "role tabIndex" )
  610. .removeUniqueId();
  611. this.tabs.add( this.panels ).each( function() {
  612. if ( $.data( this, "ui-tabs-destroy" ) ) {
  613. $( this ).remove();
  614. } else {
  615. $( this ).removeAttr( "role tabIndex " +
  616. "aria-live aria-busy aria-selected aria-labelledby aria-hidden aria-expanded" );
  617. }
  618. } );
  619. this.tabs.each( function() {
  620. var li = $( this ),
  621. prev = li.data( "ui-tabs-aria-controls" );
  622. if ( prev ) {
  623. li
  624. .attr( "aria-controls", prev )
  625. .removeData( "ui-tabs-aria-controls" );
  626. } else {
  627. li.removeAttr( "aria-controls" );
  628. }
  629. } );
  630. this.panels.show();
  631. if ( this.options.heightStyle !== "content" ) {
  632. this.panels.css( "height", "" );
  633. }
  634. },
  635. enable: function( index ) {
  636. var disabled = this.options.disabled;
  637. if ( disabled === false ) {
  638. return;
  639. }
  640. if ( index === undefined ) {
  641. disabled = false;
  642. } else {
  643. index = this._getIndex( index );
  644. if ( Array.isArray( disabled ) ) {
  645. disabled = $.map( disabled, function( num ) {
  646. return num !== index ? num : null;
  647. } );
  648. } else {
  649. disabled = $.map( this.tabs, function( li, num ) {
  650. return num !== index ? num : null;
  651. } );
  652. }
  653. }
  654. this._setOptionDisabled( disabled );
  655. },
  656. disable: function( index ) {
  657. var disabled = this.options.disabled;
  658. if ( disabled === true ) {
  659. return;
  660. }
  661. if ( index === undefined ) {
  662. disabled = true;
  663. } else {
  664. index = this._getIndex( index );
  665. if ( $.inArray( index, disabled ) !== -1 ) {
  666. return;
  667. }
  668. if ( Array.isArray( disabled ) ) {
  669. disabled = $.merge( [ index ], disabled ).sort();
  670. } else {
  671. disabled = [ index ];
  672. }
  673. }
  674. this._setOptionDisabled( disabled );
  675. },
  676. load: function( index, event ) {
  677. index = this._getIndex( index );
  678. var that = this,
  679. tab = this.tabs.eq( index ),
  680. anchor = tab.find( ".ui-tabs-anchor" ),
  681. panel = this._getPanelForTab( tab ),
  682. eventData = {
  683. tab: tab,
  684. panel: panel
  685. },
  686. complete = function( jqXHR, status ) {
  687. if ( status === "abort" ) {
  688. that.panels.stop( false, true );
  689. }
  690. that._removeClass( tab, "ui-tabs-loading" );
  691. panel.removeAttr( "aria-busy" );
  692. if ( jqXHR === that.xhr ) {
  693. delete that.xhr;
  694. }
  695. };
  696. // Not remote
  697. if ( this._isLocal( anchor[ 0 ] ) ) {
  698. return;
  699. }
  700. this.xhr = $.ajax( this._ajaxSettings( anchor, event, eventData ) );
  701. if ( this.xhr.statusText !== "canceled" ) {
  702. this._addClass( tab, "ui-tabs-loading" );
  703. panel.attr( "aria-busy", "true" );
  704. this.xhr
  705. .done( function( response, status, jqXHR ) {
  706. panel.html( response );
  707. that._trigger( "load", event, eventData );
  708. complete( jqXHR, status );
  709. } )
  710. .fail( function( jqXHR, status ) {
  711. complete( jqXHR, status );
  712. } );
  713. }
  714. },
  715. _ajaxSettings: function( anchor, event, eventData ) {
  716. var that = this;
  717. return {
  718. url: anchor.attr( "href" ),
  719. beforeSend: function( jqXHR, settings ) {
  720. return that._trigger( "beforeLoad", event,
  721. $.extend( { jqXHR: jqXHR, ajaxSettings: settings }, eventData ) );
  722. }
  723. };
  724. },
  725. _getPanelForTab: function( tab ) {
  726. var id = $( tab ).attr( "aria-controls" );
  727. return this.element.find( "#" + CSS.escape( id ) );
  728. }
  729. } );
  730. // DEPRECATED
  731. // TODO: Switch return back to widget declaration at top of file when this is removed
  732. if ( $.uiBackCompat === true ) {
  733. // Backcompat for ui-tab class (now ui-tabs-tab)
  734. $.widget( "ui.tabs", $.ui.tabs, {
  735. _processTabs: function() {
  736. this._superApply( arguments );
  737. this._addClass( this.tabs, "ui-tab" );
  738. }
  739. } );
  740. }
  741. return $.ui.tabs;
  742. } );