/src/main/resources/org/apache/struts2/static/tabview/tabview.js

http://struts2yuiplugin.googlecode.com/ · JavaScript · 980 lines · 540 code · 134 blank · 306 comment · 115 complexity · 7392b5f8f49ac84bee714de101a4b0d3 MD5 · raw file

  1. /*
  2. Copyright (c) 2009, Yahoo! Inc. All rights reserved.
  3. Code licensed under the BSD License:
  4. http://developer.yahoo.net/yui/license.txt
  5. version: 2.7.0
  6. */
  7. (function() {
  8. /**
  9. * The tabview module provides a widget for managing content bound to tabs.
  10. * @module tabview
  11. * @requires yahoo, dom, event, element
  12. *
  13. */
  14. var Y = YAHOO.util,
  15. Dom = Y.Dom,
  16. Event = Y.Event,
  17. document = window.document,
  18. // STRING CONSTANTS
  19. ACTIVE = 'active',
  20. ACTIVE_INDEX = 'activeIndex',
  21. ACTIVE_TAB = 'activeTab',
  22. CONTENT_EL = 'contentEl',
  23. ELEMENT = 'element',
  24. /**
  25. * A widget to control tabbed views.
  26. * @namespace YAHOO.widget
  27. * @class TabView
  28. * @extends YAHOO.util.Element
  29. * @constructor
  30. * @param {HTMLElement | String | Object} el(optional) The html
  31. * element that represents the TabView, or the attribute object to use.
  32. * An element will be created if none provided.
  33. * @param {Object} attr (optional) A key map of the tabView's
  34. * initial attributes. Ignored if first arg is attributes object.
  35. */
  36. TabView = function(el, attr) {
  37. attr = attr || {};
  38. if (arguments.length == 1 && !YAHOO.lang.isString(el) && !el.nodeName) {
  39. attr = el; // treat first arg as attr object
  40. el = attr.element || null;
  41. }
  42. if (!el && !attr.element) { // create if we dont have one
  43. el = this._createTabViewElement(attr);
  44. }
  45. TabView.superclass.constructor.call(this, el, attr);
  46. };
  47. YAHOO.extend(TabView, Y.Element, {
  48. /**
  49. * The className to add when building from scratch.
  50. * @property CLASSNAME
  51. * @default "navset"
  52. */
  53. CLASSNAME: 'yui-navset',
  54. /**
  55. * The className of the HTMLElement containing the TabView's tab elements
  56. * to look for when building from existing markup, or to add when building
  57. * from scratch.
  58. * All childNodes of the tab container are treated as Tabs when building
  59. * from existing markup.
  60. * @property TAB_PARENT_CLASSNAME
  61. * @default "nav"
  62. */
  63. TAB_PARENT_CLASSNAME: 'yui-nav',
  64. /**
  65. * The className of the HTMLElement containing the TabView's label elements
  66. * to look for when building from existing markup, or to add when building
  67. * from scratch.
  68. * All childNodes of the content container are treated as content elements when
  69. * building from existing markup.
  70. * @property CONTENT_PARENT_CLASSNAME
  71. * @default "nav-content"
  72. */
  73. CONTENT_PARENT_CLASSNAME: 'yui-content',
  74. _tabParent: null,
  75. _contentParent: null,
  76. /**
  77. * Adds a Tab to the TabView instance.
  78. * If no index is specified, the tab is added to the end of the tab list.
  79. * @method addTab
  80. * @param {YAHOO.widget.Tab} tab A Tab instance to add.
  81. * @param {Integer} index The position to add the tab.
  82. * @return void
  83. */
  84. addTab: function(tab, index) {
  85. var tabs = this.get('tabs'),
  86. before = this.getTab(index),
  87. tabParent = this._tabParent,
  88. contentParent = this._contentParent,
  89. tabElement = tab.get(ELEMENT),
  90. contentEl = tab.get(CONTENT_EL);
  91. if (!tabs) { // not ready yet
  92. this._queue[this._queue.length] = ['addTab', arguments];
  93. return false;
  94. }
  95. index = (index === undefined) ? tabs.length : index;
  96. if ( before ) {
  97. tabParent.insertBefore(tabElement, before.get(ELEMENT));
  98. } else {
  99. tabParent.appendChild(tabElement);
  100. }
  101. if ( contentEl && !Dom.isAncestor(contentParent, contentEl) ) {
  102. contentParent.appendChild(contentEl);
  103. }
  104. if ( !tab.get(ACTIVE) ) {
  105. tab.set('contentVisible', false, true); /* hide if not active */
  106. } else {
  107. this.set(ACTIVE_TAB, tab, true);
  108. }
  109. this._initTabEvents(tab);
  110. tabs.splice(index, 0, tab);
  111. },
  112. _initTabEvents: function(tab) {
  113. tab.addListener( tab.get('activationEvent'), tab._onActivate, this, tab);
  114. tab.addListener('activationEventChange', function(e) {
  115. if (e.prevValue != e.newValue) {
  116. tab.removeListener(e.prevValue, tab._onActivate);
  117. tab.addListener(e.newValue, tab._onActivate, this, tab);
  118. }
  119. });
  120. },
  121. /**
  122. * Routes childNode events.
  123. * @method DOMEventHandler
  124. * @param {event} e The Dom event that is being handled.
  125. * @return void
  126. */
  127. DOMEventHandler: function(e) {
  128. var target = Event.getTarget(e),
  129. tabParent = this._tabParent,
  130. tabs = this.get('tabs'),
  131. tab,
  132. tabEl,
  133. contentEl;
  134. if (Dom.isAncestor(tabParent, target) ) {
  135. for (var i = 0, len = tabs.length; i < len; i++) {
  136. tabEl = tabs[i].get(ELEMENT);
  137. contentEl = tabs[i].get(CONTENT_EL);
  138. if ( target == tabEl || Dom.isAncestor(tabEl, target) ) {
  139. tab = tabs[i];
  140. break; // note break
  141. }
  142. }
  143. if (tab) {
  144. tab.fireEvent(e.type, e);
  145. }
  146. }
  147. },
  148. /**
  149. * Returns the Tab instance at the specified index.
  150. * @method getTab
  151. * @param {Integer} index The position of the Tab.
  152. * @return YAHOO.widget.Tab
  153. */
  154. getTab: function(index) {
  155. return this.get('tabs')[index];
  156. },
  157. /**
  158. * Returns the index of given tab.
  159. * @method getTabIndex
  160. * @param {YAHOO.widget.Tab} tab The tab whose index will be returned.
  161. * @return int
  162. */
  163. getTabIndex: function(tab) {
  164. var index = null,
  165. tabs = this.get('tabs');
  166. for (var i = 0, len = tabs.length; i < len; ++i) {
  167. if (tab == tabs[i]) {
  168. index = i;
  169. break;
  170. }
  171. }
  172. return index;
  173. },
  174. /**
  175. * Removes the specified Tab from the TabView.
  176. * @method removeTab
  177. * @param {YAHOO.widget.Tab} item The Tab instance to be removed.
  178. * @return void
  179. */
  180. removeTab: function(tab) {
  181. var tabCount = this.get('tabs').length,
  182. index = this.getTabIndex(tab);
  183. if ( tab === this.get(ACTIVE_TAB) ) {
  184. if (tabCount > 1) { // select another tab
  185. if (index + 1 === tabCount) { // if last, activate previous
  186. this.set(ACTIVE_INDEX, index - 1);
  187. } else { // activate next tab
  188. this.set(ACTIVE_INDEX, index + 1);
  189. }
  190. } else { // no more tabs
  191. this.set(ACTIVE_TAB, null);
  192. }
  193. }
  194. this._tabParent.removeChild( tab.get(ELEMENT) );
  195. this._contentParent.removeChild( tab.get(CONTENT_EL) );
  196. this._configs.tabs.value.splice(index, 1);
  197. tab.fireEvent('remove', { type: 'remove', tabview: this });
  198. },
  199. /**
  200. * Provides a readable name for the TabView instance.
  201. * @method toString
  202. * @return String
  203. */
  204. toString: function() {
  205. var name = this.get('id') || this.get('tagName');
  206. return "TabView " + name;
  207. },
  208. /**
  209. * The transiton to use when switching between tabs.
  210. * @method contentTransition
  211. */
  212. contentTransition: function(newTab, oldTab) {
  213. if (newTab) {
  214. newTab.set('contentVisible', true);
  215. }
  216. if (oldTab) {
  217. oldTab.set('contentVisible', false);
  218. }
  219. },
  220. /**
  221. * setAttributeConfigs TabView specific properties.
  222. * @method initAttributes
  223. * @param {Object} attr Hash of initial attributes
  224. */
  225. initAttributes: function(attr) {
  226. TabView.superclass.initAttributes.call(this, attr);
  227. if (!attr.orientation) {
  228. attr.orientation = 'top';
  229. }
  230. var el = this.get(ELEMENT);
  231. if (!Dom.hasClass(el, this.CLASSNAME)) {
  232. Dom.addClass(el, this.CLASSNAME);
  233. }
  234. /**
  235. * The Tabs belonging to the TabView instance.
  236. * @attribute tabs
  237. * @type Array
  238. */
  239. this.setAttributeConfig('tabs', {
  240. value: [],
  241. readOnly: true
  242. });
  243. /**
  244. * The container of the tabView's label elements.
  245. * @property _tabParent
  246. * @private
  247. * @type HTMLElement
  248. */
  249. this._tabParent =
  250. this.getElementsByClassName(this.TAB_PARENT_CLASSNAME,
  251. 'ul' )[0] || this._createTabParent();
  252. /**
  253. * The container of the tabView's content elements.
  254. * @property _contentParent
  255. * @type HTMLElement
  256. * @private
  257. */
  258. this._contentParent =
  259. this.getElementsByClassName(this.CONTENT_PARENT_CLASSNAME,
  260. 'div')[0] || this._createContentParent();
  261. /**
  262. * How the Tabs should be oriented relative to the TabView.
  263. * @attribute orientation
  264. * @type String
  265. * @default "top"
  266. */
  267. this.setAttributeConfig('orientation', {
  268. value: attr.orientation,
  269. method: function(value) {
  270. var current = this.get('orientation');
  271. this.addClass('yui-navset-' + value);
  272. if (current != value) {
  273. this.removeClass('yui-navset-' + current);
  274. }
  275. if (value === 'bottom') {
  276. this.appendChild(this._tabParent);
  277. }
  278. }
  279. });
  280. /**
  281. * The index of the tab currently active.
  282. * @attribute activeIndex
  283. * @type Int
  284. */
  285. this.setAttributeConfig(ACTIVE_INDEX, {
  286. value: attr.activeIndex,
  287. method: function(value) {
  288. },
  289. validator: function(value) {
  290. var ret = true;
  291. if (value && this.getTab(value).get('disabled')) { // cannot activate if disabled
  292. ret = false;
  293. }
  294. return ret;
  295. }
  296. });
  297. /**
  298. * The tab currently active.
  299. * @attribute activeTab
  300. * @type YAHOO.widget.Tab
  301. */
  302. this.setAttributeConfig(ACTIVE_TAB, {
  303. value: attr.activeTab,
  304. method: function(tab) {
  305. var activeTab = this.get(ACTIVE_TAB);
  306. if (tab) {
  307. tab.set(ACTIVE, true);
  308. }
  309. if (activeTab && activeTab !== tab) {
  310. activeTab.set(ACTIVE, false);
  311. }
  312. if (activeTab && tab !== activeTab) { // no transition if only 1
  313. this.contentTransition(tab, activeTab);
  314. } else if (tab) {
  315. tab.set('contentVisible', true);
  316. }
  317. },
  318. validator: function(value) {
  319. var ret = true;
  320. if (value && value.get('disabled')) { // cannot activate if disabled
  321. ret = false;
  322. }
  323. return ret;
  324. }
  325. });
  326. this.on('activeTabChange', this._onActiveTabChange);
  327. this.on('activeIndexChange', this._onActiveIndexChange);
  328. if ( this._tabParent ) {
  329. this._initTabs();
  330. }
  331. // Due to delegation we add all DOM_EVENTS to the TabView container
  332. // but IE will leak when unsupported events are added, so remove these
  333. this.DOM_EVENTS.submit = false;
  334. this.DOM_EVENTS.focus = false;
  335. this.DOM_EVENTS.blur = false;
  336. for (var type in this.DOM_EVENTS) {
  337. if ( YAHOO.lang.hasOwnProperty(this.DOM_EVENTS, type) ) {
  338. this.addListener.call(this, type, this.DOMEventHandler);
  339. }
  340. }
  341. },
  342. /**
  343. * Removes selected state from the given tab if it is the activeTab
  344. * @method deselectTab
  345. * @param {Int} index The tab index to deselect
  346. */
  347. deselectTab: function(index) {
  348. if (this.getTab(index) === this.get('activeTab')) {
  349. this.set('activeTab', null);
  350. }
  351. },
  352. /**
  353. * Makes the tab at the given index the active tab
  354. * @method selectTab
  355. * @param {Int} index The tab index to be made active
  356. */
  357. selectTab: function(index) {
  358. this.set('activeTab', this.getTab(index));
  359. },
  360. _onActiveTabChange: function(e) {
  361. var activeIndex = this.get(ACTIVE_INDEX),
  362. newIndex = this.getTabIndex(e.newValue);
  363. if (activeIndex !== newIndex) {
  364. if (!(this.set(ACTIVE_INDEX, newIndex)) ) { // NOTE: setting
  365. // revert if activeIndex update fails (cancelled via beforeChange)
  366. this.set(ACTIVE_TAB, e.prevValue);
  367. }
  368. }
  369. },
  370. _onActiveIndexChange: function(e) {
  371. // no set if called from ActiveTabChange event
  372. if (e.newValue !== this.getTabIndex(this.get(ACTIVE_TAB))) {
  373. if (!(this.set(ACTIVE_TAB, this.getTab(e.newValue))) ) { // NOTE: setting
  374. // revert if activeTab update fails (cancelled via beforeChange)
  375. this.set(ACTIVE_INDEX, e.prevValue);
  376. }
  377. }
  378. },
  379. /**
  380. * Creates Tab instances from a collection of HTMLElements.
  381. * @method _initTabs
  382. * @private
  383. * @return void
  384. */
  385. _initTabs: function() {
  386. var tabs = Dom.getChildren(this._tabParent),
  387. contentElements = Dom.getChildren(this._contentParent),
  388. activeIndex = this.get(ACTIVE_INDEX),
  389. tab,
  390. attr,
  391. active;
  392. for (var i = 0, len = tabs.length; i < len; ++i) {
  393. attr = {};
  394. if (contentElements[i]) {
  395. attr.contentEl = contentElements[i];
  396. }
  397. tab = new YAHOO.widget.Tab(tabs[i], attr);
  398. this.addTab(tab);
  399. if (tab.hasClass(tab.ACTIVE_CLASSNAME) ) {
  400. active = tab;
  401. }
  402. }
  403. if (activeIndex) {
  404. this.set(ACTIVE_TAB, this.getTab(activeIndex));
  405. } else {
  406. this._configs.activeTab.value = active; // dont invoke method
  407. this._configs.activeIndex.value = this.getTabIndex(active);
  408. }
  409. },
  410. _createTabViewElement: function(attr) {
  411. var el = document.createElement('div');
  412. if ( this.CLASSNAME ) {
  413. el.className = this.CLASSNAME;
  414. }
  415. return el;
  416. },
  417. _createTabParent: function(attr) {
  418. var el = document.createElement('ul');
  419. if ( this.TAB_PARENT_CLASSNAME ) {
  420. el.className = this.TAB_PARENT_CLASSNAME;
  421. }
  422. this.get(ELEMENT).appendChild(el);
  423. return el;
  424. },
  425. _createContentParent: function(attr) {
  426. var el = document.createElement('div');
  427. if ( this.CONTENT_PARENT_CLASSNAME ) {
  428. el.className = this.CONTENT_PARENT_CLASSNAME;
  429. }
  430. this.get(ELEMENT).appendChild(el);
  431. return el;
  432. }
  433. });
  434. YAHOO.widget.TabView = TabView;
  435. })();
  436. (function() {
  437. var Y = YAHOO.util,
  438. Dom = Y.Dom,
  439. Lang = YAHOO.lang,
  440. // STRING CONSTANTS
  441. ACTIVE_TAB = 'activeTab',
  442. LABEL = 'label',
  443. LABEL_EL = 'labelEl',
  444. CONTENT = 'content',
  445. CONTENT_EL = 'contentEl',
  446. ELEMENT = 'element',
  447. CACHE_DATA = 'cacheData',
  448. DATA_SRC = 'dataSrc',
  449. DATA_LOADED = 'dataLoaded',
  450. DATA_TIMEOUT = 'dataTimeout',
  451. LOAD_METHOD = 'loadMethod',
  452. POST_DATA = 'postData',
  453. DISABLED = 'disabled',
  454. /**
  455. * A representation of a Tab's label and content.
  456. * @namespace YAHOO.widget
  457. * @class Tab
  458. * @extends YAHOO.util.Element
  459. * @constructor
  460. * @param element {HTMLElement | String} (optional) The html element that
  461. * represents the TabView. An element will be created if none provided.
  462. * @param {Object} properties A key map of initial properties
  463. */
  464. Tab = function(el, attr) {
  465. attr = attr || {};
  466. if (arguments.length == 1 && !Lang.isString(el) && !el.nodeName) {
  467. attr = el;
  468. el = attr.element;
  469. }
  470. if (!el && !attr.element) {
  471. el = this._createTabElement(attr);
  472. }
  473. this.loadHandler = {
  474. success: function(o) {
  475. this.set(CONTENT, o.responseText);
  476. },
  477. failure: function(o) {
  478. }
  479. };
  480. Tab.superclass.constructor.call(this, el, attr);
  481. this.DOM_EVENTS = {}; // delegating to tabView
  482. };
  483. YAHOO.extend(Tab, YAHOO.util.Element, {
  484. /**
  485. * The default tag name for a Tab's inner element.
  486. * @property LABEL_INNER_TAGNAME
  487. * @type String
  488. * @default "em"
  489. */
  490. LABEL_TAGNAME: 'em',
  491. /**
  492. * The class name applied to active tabs.
  493. * @property ACTIVE_CLASSNAME
  494. * @type String
  495. * @default "selected"
  496. */
  497. ACTIVE_CLASSNAME: 'selected',
  498. /**
  499. * The class name applied to active tabs.
  500. * @property ACTIVE_CLASSNAME
  501. * @type String
  502. * @default "selected"
  503. */
  504. HIDDEN_CLASSNAME: 'yui-hidden',
  505. /**
  506. * The title applied to active tabs.
  507. * @property ACTIVE_TITLE
  508. * @type String
  509. * @default "active"
  510. */
  511. ACTIVE_TITLE: 'active',
  512. /**
  513. * The class name applied to disabled tabs.
  514. * @property DISABLED_CLASSNAME
  515. * @type String
  516. * @default "disabled"
  517. */
  518. DISABLED_CLASSNAME: DISABLED,
  519. /**
  520. * The class name applied to dynamic tabs while loading.
  521. * @property LOADING_CLASSNAME
  522. * @type String
  523. * @default "disabled"
  524. */
  525. LOADING_CLASSNAME: 'loading',
  526. /**
  527. * Provides a reference to the connection request object when data is
  528. * loaded dynamically.
  529. * @property dataConnection
  530. * @type Object
  531. */
  532. dataConnection: null,
  533. /**
  534. * Object containing success and failure callbacks for loading data.
  535. * @property loadHandler
  536. * @type object
  537. */
  538. loadHandler: null,
  539. _loading: false,
  540. /**
  541. * Provides a readable name for the tab.
  542. * @method toString
  543. * @return String
  544. */
  545. toString: function() {
  546. var el = this.get(ELEMENT),
  547. id = el.id || el.tagName;
  548. return "Tab " + id;
  549. },
  550. /**
  551. * setAttributeConfigs TabView specific properties.
  552. * @method initAttributes
  553. * @param {Object} attr Hash of initial attributes
  554. */
  555. initAttributes: function(attr) {
  556. attr = attr || {};
  557. Tab.superclass.initAttributes.call(this, attr);
  558. /**
  559. * The event that triggers the tab's activation.
  560. * @attribute activationEvent
  561. * @type String
  562. */
  563. this.setAttributeConfig('activationEvent', {
  564. value: attr.activationEvent || 'click'
  565. });
  566. /**
  567. * The element that contains the tab's label.
  568. * @attribute labelEl
  569. * @type HTMLElement
  570. */
  571. this.setAttributeConfig(LABEL_EL, {
  572. value: attr[LABEL_EL] || this._getLabelEl(),
  573. method: function(value) {
  574. value = Dom.get(value);
  575. var current = this.get(LABEL_EL);
  576. if (current) {
  577. if (current == value) {
  578. return false; // already set
  579. }
  580. current.parentNode.replaceChild(value, current);
  581. this.set(LABEL, value.innerHTML);
  582. }
  583. }
  584. });
  585. /**
  586. * The tab's label text (or innerHTML).
  587. * @attribute label
  588. * @type String
  589. */
  590. this.setAttributeConfig(LABEL, {
  591. value: attr.label || this._getLabel(),
  592. method: function(value) {
  593. var labelEl = this.get(LABEL_EL);
  594. if (!labelEl) { // create if needed
  595. this.set(LABEL_EL, this._createLabelEl());
  596. }
  597. labelEl.innerHTML = value;
  598. }
  599. });
  600. /**
  601. * The HTMLElement that contains the tab's content.
  602. * @attribute contentEl
  603. * @type HTMLElement
  604. */
  605. this.setAttributeConfig(CONTENT_EL, {
  606. value: attr[CONTENT_EL] || document.createElement('div'),
  607. method: function(value) {
  608. value = Dom.get(value);
  609. var current = this.get(CONTENT_EL);
  610. if (current) {
  611. if (current === value) {
  612. return false; // already set
  613. }
  614. if (!this.get('selected')) {
  615. Dom.addClass(value, 'yui-hidden');
  616. }
  617. current.parentNode.replaceChild(value, current);
  618. this.set(CONTENT, value.innerHTML);
  619. }
  620. }
  621. });
  622. /**
  623. * The tab's content.
  624. * @attribute content
  625. * @type String
  626. */
  627. this.setAttributeConfig(CONTENT, {
  628. value: attr[CONTENT],
  629. method: function(value) {
  630. this.get(CONTENT_EL).innerHTML = value;
  631. }
  632. });
  633. /**
  634. * The tab's data source, used for loading content dynamically.
  635. * @attribute dataSrc
  636. * @type String
  637. */
  638. this.setAttributeConfig(DATA_SRC, {
  639. value: attr.dataSrc
  640. });
  641. /**
  642. * Whether or not content should be reloaded for every view.
  643. * @attribute cacheData
  644. * @type Boolean
  645. * @default false
  646. */
  647. this.setAttributeConfig(CACHE_DATA, {
  648. value: attr.cacheData || false,
  649. validator: Lang.isBoolean
  650. });
  651. /**
  652. * The method to use for the data request.
  653. * @attribute loadMethod
  654. * @type String
  655. * @default "GET"
  656. */
  657. this.setAttributeConfig(LOAD_METHOD, {
  658. value: attr.loadMethod || 'GET',
  659. validator: Lang.isString
  660. });
  661. /**
  662. * Whether or not any data has been loaded from the server.
  663. * @attribute dataLoaded
  664. * @type Boolean
  665. */
  666. this.setAttributeConfig(DATA_LOADED, {
  667. value: false,
  668. validator: Lang.isBoolean,
  669. writeOnce: true
  670. });
  671. /**
  672. * Number if milliseconds before aborting and calling failure handler.
  673. * @attribute dataTimeout
  674. * @type Number
  675. * @default null
  676. */
  677. this.setAttributeConfig(DATA_TIMEOUT, {
  678. value: attr.dataTimeout || null,
  679. validator: Lang.isNumber
  680. });
  681. /**
  682. * Arguments to pass when POST method is used
  683. * @attribute postData
  684. * @default null
  685. */
  686. this.setAttributeConfig(POST_DATA, {
  687. value: attr.postData || null
  688. });
  689. /**
  690. * Whether or not the tab is currently active.
  691. * If a dataSrc is set for the tab, the content will be loaded from
  692. * the given source.
  693. * @attribute active
  694. * @type Boolean
  695. */
  696. this.setAttributeConfig('active', {
  697. value: attr.active || this.hasClass(this.ACTIVE_CLASSNAME),
  698. method: function(value) {
  699. if (value === true) {
  700. this.addClass(this.ACTIVE_CLASSNAME);
  701. this.set('title', this.ACTIVE_TITLE);
  702. } else {
  703. this.removeClass(this.ACTIVE_CLASSNAME);
  704. this.set('title', '');
  705. }
  706. },
  707. validator: function(value) {
  708. return Lang.isBoolean(value) && !this.get(DISABLED) ;
  709. }
  710. });
  711. /**
  712. * Whether or not the tab is disabled.
  713. * @attribute disabled
  714. * @type Boolean
  715. */
  716. this.setAttributeConfig(DISABLED, {
  717. value: attr.disabled || this.hasClass(this.DISABLED_CLASSNAME),
  718. method: function(value) {
  719. if (value === true) {
  720. Dom.addClass(this.get(ELEMENT), this.DISABLED_CLASSNAME);
  721. } else {
  722. Dom.removeClass(this.get(ELEMENT), this.DISABLED_CLASSNAME);
  723. }
  724. },
  725. validator: Lang.isBoolean
  726. });
  727. /**
  728. * The href of the tab's anchor element.
  729. * @attribute href
  730. * @type String
  731. * @default '#'
  732. */
  733. this.setAttributeConfig('href', {
  734. value: attr.href ||
  735. this.getElementsByTagName('a')[0].getAttribute('href', 2) || '#',
  736. method: function(value) {
  737. this.getElementsByTagName('a')[0].href = value;
  738. },
  739. validator: Lang.isString
  740. });
  741. /**
  742. * The Whether or not the tab's content is visible.
  743. * @attribute contentVisible
  744. * @type Boolean
  745. * @default false
  746. */
  747. this.setAttributeConfig('contentVisible', {
  748. value: attr.contentVisible,
  749. method: function(value) {
  750. if (value) {
  751. Dom.removeClass(this.get(CONTENT_EL), this.HIDDEN_CLASSNAME);
  752. if ( this.get(DATA_SRC) ) {
  753. // load dynamic content unless already loading or loaded and caching
  754. if ( !this._loading && !(this.get(DATA_LOADED) && this.get(CACHE_DATA)) ) {
  755. this._dataConnect();
  756. }
  757. }
  758. } else {
  759. Dom.addClass(this.get(CONTENT_EL), this.HIDDEN_CLASSNAME);
  760. }
  761. },
  762. validator: Lang.isBoolean
  763. });
  764. },
  765. _dataConnect: function() {
  766. if (!Y.Connect) {
  767. return false;
  768. }
  769. Dom.addClass(this.get(CONTENT_EL).parentNode, this.LOADING_CLASSNAME);
  770. this._loading = true;
  771. this.dataConnection = Y.Connect.asyncRequest(
  772. this.get(LOAD_METHOD),
  773. this.get(DATA_SRC),
  774. {
  775. success: function(o) {
  776. this.loadHandler.success.call(this, o);
  777. this.set(DATA_LOADED, true);
  778. this.dataConnection = null;
  779. Dom.removeClass(this.get(CONTENT_EL).parentNode,
  780. this.LOADING_CLASSNAME);
  781. this._loading = false;
  782. },
  783. failure: function(o) {
  784. this.loadHandler.failure.call(this, o);
  785. this.dataConnection = null;
  786. Dom.removeClass(this.get(CONTENT_EL).parentNode,
  787. this.LOADING_CLASSNAME);
  788. this._loading = false;
  789. },
  790. scope: this,
  791. timeout: this.get(DATA_TIMEOUT)
  792. },
  793. this.get(POST_DATA)
  794. );
  795. },
  796. _createTabElement: function(attr) {
  797. var el = document.createElement('li'),
  798. a = document.createElement('a'),
  799. label = attr.label || null,
  800. labelEl = attr.labelEl || null;
  801. a.href = attr.href || '#'; // TODO: Use Dom.setAttribute?
  802. el.appendChild(a);
  803. if (labelEl) { // user supplied labelEl
  804. if (!label) { // user supplied label
  805. label = this._getLabel();
  806. }
  807. } else {
  808. labelEl = this._createLabelEl();
  809. }
  810. a.appendChild(labelEl);
  811. return el;
  812. },
  813. _getLabelEl: function() {
  814. return this.getElementsByTagName(this.LABEL_TAGNAME)[0];
  815. },
  816. _createLabelEl: function() {
  817. var el = document.createElement(this.LABEL_TAGNAME);
  818. return el;
  819. },
  820. _getLabel: function() {
  821. var el = this.get(LABEL_EL);
  822. if (!el) {
  823. return undefined;
  824. }
  825. return el.innerHTML;
  826. },
  827. _onActivate: function(e, tabview) {
  828. var tab = this,
  829. silent = false;
  830. Y.Event.preventDefault(e);
  831. if (tab === tabview.get(ACTIVE_TAB)) {
  832. silent = true; // dont fire activeTabChange if already active
  833. }
  834. tabview.set(ACTIVE_TAB, tab, silent);
  835. }
  836. });
  837. /**
  838. * Fires when a tab is removed from the tabview
  839. * @event remove
  840. * @type CustomEvent
  841. * @param {Event} An event object with fields for "type" ("remove")
  842. * and "tabview" (the tabview instance it was removed from)
  843. */
  844. YAHOO.widget.Tab = Tab;
  845. })();
  846. YAHOO.register("tabview", YAHOO.widget.TabView, {version: "2.7.0", build: "1799"});