tabpane.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  1. // Copyright 2006 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /**
  15. * @fileoverview TabPane widget implementation.
  16. *
  17. * @author eae@google.com (Emil A Eklund)
  18. */
  19. goog.provide('goog.ui.TabPane');
  20. goog.provide('goog.ui.TabPane.Events');
  21. goog.provide('goog.ui.TabPane.TabLocation');
  22. goog.provide('goog.ui.TabPane.TabPage');
  23. goog.provide('goog.ui.TabPaneEvent');
  24. goog.require('goog.asserts');
  25. goog.require('goog.dom');
  26. goog.require('goog.dom.TagName');
  27. goog.require('goog.dom.classlist');
  28. goog.require('goog.events');
  29. goog.require('goog.events.Event');
  30. goog.require('goog.events.EventTarget');
  31. goog.require('goog.events.EventType');
  32. goog.require('goog.events.KeyCodes');
  33. goog.require('goog.html.SafeStyleSheet');
  34. goog.require('goog.style');
  35. /**
  36. * TabPane widget. All children already inside the tab pane container element
  37. * will be be converted to tabs. Each tab is represented by a goog.ui.TabPane.
  38. * TabPage object. Further pages can be constructed either from an existing
  39. * container or created from scratch.
  40. *
  41. * @param {Element} el Container element to create the tab pane out of.
  42. * @param {goog.ui.TabPane.TabLocation=} opt_tabLocation Location of the tabs
  43. * in relation to the content container. Default is top.
  44. * @param {goog.dom.DomHelper=} opt_domHelper Optional DOM helper.
  45. * @param {boolean=} opt_useMouseDown Whether to use MOUSEDOWN instead of CLICK
  46. * for tab changes.
  47. * @extends {goog.events.EventTarget}
  48. * @constructor
  49. * @see ../demos/tabpane.html
  50. * @deprecated Use goog.ui.TabBar instead.
  51. */
  52. goog.ui.TabPane = function(
  53. el, opt_tabLocation, opt_domHelper, opt_useMouseDown) {
  54. goog.events.EventTarget.call(this);
  55. /**
  56. * DomHelper used to interact with the document, allowing components to be
  57. * created in a different window. This property is considered protected;
  58. * subclasses of Component may refer to it directly.
  59. * @type {goog.dom.DomHelper}
  60. * @protected
  61. * @suppress {underscore|visibility}
  62. */
  63. this.dom_ = opt_domHelper || goog.dom.getDomHelper();
  64. /**
  65. * Tab pane element.
  66. * @type {Element}
  67. * @private
  68. */
  69. this.el_ = el;
  70. /**
  71. * Collection of tab panes.
  72. * @type {Array<goog.ui.TabPane.TabPage>}
  73. * @private
  74. */
  75. this.pages_ = [];
  76. /**
  77. * Location of the tabs with respect to the content box.
  78. * @type {goog.ui.TabPane.TabLocation}
  79. * @private
  80. */
  81. this.tabLocation_ =
  82. opt_tabLocation ? opt_tabLocation : goog.ui.TabPane.TabLocation.TOP;
  83. /**
  84. * Whether to use MOUSEDOWN instead of CLICK for tab change events. This
  85. * fixes some focus problems on Safari/Chrome.
  86. * @type {boolean}
  87. * @private
  88. */
  89. this.useMouseDown_ = !!opt_useMouseDown;
  90. this.create_();
  91. };
  92. goog.inherits(goog.ui.TabPane, goog.events.EventTarget);
  93. goog.tagUnsealableClass(goog.ui.TabPane);
  94. /**
  95. * Element containing the tab buttons.
  96. * @type {Element}
  97. * @private
  98. */
  99. goog.ui.TabPane.prototype.elButtonBar_;
  100. /**
  101. * Element containing the tab pages.
  102. * @type {Element}
  103. * @private
  104. */
  105. goog.ui.TabPane.prototype.elContent_;
  106. /**
  107. * Selected page.
  108. * @type {goog.ui.TabPane.TabPage?}
  109. * @private
  110. */
  111. goog.ui.TabPane.prototype.selected_;
  112. /**
  113. * Constants for event names
  114. *
  115. * @const
  116. */
  117. goog.ui.TabPane.Events = {
  118. CHANGE: 'change'
  119. };
  120. /**
  121. * Enum for representing the location of the tabs in relation to the content.
  122. *
  123. * @enum {number}
  124. */
  125. goog.ui.TabPane.TabLocation = {
  126. TOP: 0,
  127. BOTTOM: 1,
  128. LEFT: 2,
  129. RIGHT: 3
  130. };
  131. /**
  132. * Creates HTML nodes for tab pane.
  133. *
  134. * @private
  135. */
  136. goog.ui.TabPane.prototype.create_ = function() {
  137. this.el_.className = goog.getCssName('goog-tabpane');
  138. var nodes = this.getChildNodes_();
  139. // Create tab strip
  140. this.elButtonBar_ = this.dom_.createDom(
  141. goog.dom.TagName.UL,
  142. {'className': goog.getCssName('goog-tabpane-tabs'), 'tabIndex': '0'});
  143. // Create content area
  144. this.elContent_ = this.dom_.createDom(
  145. goog.dom.TagName.DIV, goog.getCssName('goog-tabpane-cont'));
  146. this.el_.appendChild(this.elContent_);
  147. var element = goog.asserts.assertElement(this.el_);
  148. switch (this.tabLocation_) {
  149. case goog.ui.TabPane.TabLocation.TOP:
  150. element.insertBefore(this.elButtonBar_, this.elContent_);
  151. element.insertBefore(this.createClear_(), this.elContent_);
  152. goog.dom.classlist.add(element, goog.getCssName('goog-tabpane-top'));
  153. break;
  154. case goog.ui.TabPane.TabLocation.BOTTOM:
  155. element.appendChild(this.elButtonBar_);
  156. element.appendChild(this.createClear_());
  157. goog.dom.classlist.add(element, goog.getCssName('goog-tabpane-bottom'));
  158. break;
  159. case goog.ui.TabPane.TabLocation.LEFT:
  160. element.insertBefore(this.elButtonBar_, this.elContent_);
  161. goog.dom.classlist.add(element, goog.getCssName('goog-tabpane-left'));
  162. break;
  163. case goog.ui.TabPane.TabLocation.RIGHT:
  164. element.insertBefore(this.elButtonBar_, this.elContent_);
  165. goog.dom.classlist.add(element, goog.getCssName('goog-tabpane-right'));
  166. break;
  167. default:
  168. throw Error('Invalid tab location');
  169. }
  170. // Listen for click and keydown events on header
  171. this.elButtonBar_.tabIndex = 0;
  172. goog.events.listen(
  173. this.elButtonBar_, this.useMouseDown_ ? goog.events.EventType.MOUSEDOWN :
  174. goog.events.EventType.CLICK,
  175. this.onHeaderClick_, false, this);
  176. goog.events.listen(
  177. this.elButtonBar_, goog.events.EventType.KEYDOWN, this.onHeaderKeyDown_,
  178. false, this);
  179. this.createPages_(nodes);
  180. };
  181. /**
  182. * Creates the HTML node for the clearing div, and associated style in
  183. * the <HEAD>.
  184. *
  185. * @return {!Element} Reference to a DOM div node.
  186. * @private
  187. */
  188. goog.ui.TabPane.prototype.createClear_ = function() {
  189. var clearFloatStyle = goog.html.SafeStyleSheet.createRule(
  190. '.' + goog.getCssName('goog-tabpane-clear'),
  191. {'clear': 'both', 'height': '0', 'overflow': 'hidden'});
  192. goog.style.installSafeStyleSheet(clearFloatStyle);
  193. return this.dom_.createDom(
  194. goog.dom.TagName.DIV, goog.getCssName('goog-tabpane-clear'));
  195. };
  196. /** @override */
  197. goog.ui.TabPane.prototype.disposeInternal = function() {
  198. goog.ui.TabPane.superClass_.disposeInternal.call(this);
  199. goog.events.unlisten(
  200. this.elButtonBar_, this.useMouseDown_ ? goog.events.EventType.MOUSEDOWN :
  201. goog.events.EventType.CLICK,
  202. this.onHeaderClick_, false, this);
  203. goog.events.unlisten(
  204. this.elButtonBar_, goog.events.EventType.KEYDOWN, this.onHeaderKeyDown_,
  205. false, this);
  206. delete this.el_;
  207. this.elButtonBar_ = null;
  208. this.elContent_ = null;
  209. };
  210. /**
  211. * @return {!Array<Element>} The element child nodes of tab pane container.
  212. * @private
  213. */
  214. goog.ui.TabPane.prototype.getChildNodes_ = function() {
  215. var nodes = [];
  216. var child = goog.dom.getFirstElementChild(this.el_);
  217. while (child) {
  218. nodes.push(child);
  219. child = goog.dom.getNextElementSibling(child);
  220. }
  221. return nodes;
  222. };
  223. /**
  224. * Creates pages out of a collection of elements.
  225. *
  226. * @param {Array<Element>} nodes Array of elements to create pages out of.
  227. * @private
  228. */
  229. goog.ui.TabPane.prototype.createPages_ = function(nodes) {
  230. for (var node, i = 0; node = nodes[i]; i++) {
  231. this.addPage(new goog.ui.TabPane.TabPage(node));
  232. }
  233. };
  234. /**
  235. * Adds a page to the tab pane.
  236. *
  237. * @param {goog.ui.TabPane.TabPage} page Tab page to add.
  238. * @param {number=} opt_index Zero based index to insert tab at. Inserted at the
  239. * end if not specified.
  240. */
  241. goog.ui.TabPane.prototype.addPage = function(page, opt_index) {
  242. // If page is already in another tab pane it's removed from that one before it
  243. // can be added to this one.
  244. if (page.parent_ && page.parent_ != this &&
  245. page.parent_ instanceof goog.ui.TabPane) {
  246. page.parent_.removePage(page);
  247. }
  248. // Insert page at specified position
  249. var index = this.pages_.length;
  250. if (goog.isDef(opt_index) && opt_index != index) {
  251. index = opt_index;
  252. this.pages_.splice(index, 0, page);
  253. this.elButtonBar_.insertBefore(
  254. page.elTitle_, this.elButtonBar_.childNodes[index]);
  255. }
  256. // Append page to end
  257. else {
  258. this.pages_.push(page);
  259. this.elButtonBar_.appendChild(page.elTitle_);
  260. }
  261. page.setParent_(this, index);
  262. // Select first page and fire change event
  263. if (!this.selected_) {
  264. this.selected_ = page;
  265. this.dispatchEvent(
  266. new goog.ui.TabPaneEvent(
  267. goog.ui.TabPane.Events.CHANGE, this, this.selected_));
  268. }
  269. // Move page content to the tab pane and update visibility.
  270. this.elContent_.appendChild(page.elContent_);
  271. page.setVisible_(page == this.selected_);
  272. // Update index for following pages
  273. for (var pg, i = index + 1; pg = this.pages_[i]; i++) {
  274. pg.index_ = i;
  275. }
  276. };
  277. /**
  278. * Removes the specified page from the tab pane.
  279. *
  280. * @param {goog.ui.TabPane.TabPage|number} page Reference to tab page or zero
  281. * based index.
  282. */
  283. goog.ui.TabPane.prototype.removePage = function(page) {
  284. if (goog.isNumber(page)) {
  285. page = this.pages_[page];
  286. }
  287. this.pages_.splice(page.index_, 1);
  288. page.setParent_(null);
  289. goog.dom.removeNode(page.elTitle_);
  290. goog.dom.removeNode(page.elContent_);
  291. for (var pg, i = 0; pg = this.pages_[i]; i++) {
  292. pg.setParent_(this, i);
  293. }
  294. };
  295. /**
  296. * Gets the tab page by zero based index.
  297. *
  298. * @param {number} index Index of page to return.
  299. * @return {goog.ui.TabPane.TabPage?} page The tab page.
  300. */
  301. goog.ui.TabPane.prototype.getPage = function(index) {
  302. return this.pages_[index];
  303. };
  304. /**
  305. * Sets the selected tab page by object reference.
  306. *
  307. * @param {goog.ui.TabPane.TabPage} page Tab page to select.
  308. */
  309. goog.ui.TabPane.prototype.setSelectedPage = function(page) {
  310. if (page.isEnabled() && (!this.selected_ || page != this.selected_)) {
  311. this.selected_.setVisible_(false);
  312. page.setVisible_(true);
  313. this.selected_ = page;
  314. // Fire changed event
  315. this.dispatchEvent(
  316. new goog.ui.TabPaneEvent(
  317. goog.ui.TabPane.Events.CHANGE, this, this.selected_));
  318. }
  319. };
  320. /**
  321. * Sets the selected tab page by zero based index.
  322. *
  323. * @param {number} index Index of page to select.
  324. */
  325. goog.ui.TabPane.prototype.setSelectedIndex = function(index) {
  326. if (index >= 0 && index < this.pages_.length) {
  327. this.setSelectedPage(this.pages_[index]);
  328. }
  329. };
  330. /**
  331. * @return {number} The index for the selected tab page or -1 if no page is
  332. * selected.
  333. */
  334. goog.ui.TabPane.prototype.getSelectedIndex = function() {
  335. return this.selected_ ? /** @type {number} */ (this.selected_.index_) : -1;
  336. };
  337. /**
  338. * @return {goog.ui.TabPane.TabPage?} The selected tab page.
  339. */
  340. goog.ui.TabPane.prototype.getSelectedPage = function() {
  341. return this.selected_ || null;
  342. };
  343. /**
  344. * @return {Element} The element that contains the tab pages.
  345. */
  346. goog.ui.TabPane.prototype.getContentElement = function() {
  347. return this.elContent_ || null;
  348. };
  349. /**
  350. * @return {Element} The main element for the tabpane.
  351. */
  352. goog.ui.TabPane.prototype.getElement = function() {
  353. return this.el_ || null;
  354. };
  355. /**
  356. * Click event handler for header element, handles clicks on tabs.
  357. *
  358. * @param {goog.events.BrowserEvent} event Click event.
  359. * @private
  360. */
  361. goog.ui.TabPane.prototype.onHeaderClick_ = function(event) {
  362. var el = event.target;
  363. // Determine index if a tab (li element) was clicked.
  364. while (el != this.elButtonBar_) {
  365. if (el.tagName == goog.dom.TagName.LI) {
  366. var i;
  367. // {} prevents compiler warning
  368. for (i = 0; el = el.previousSibling; i++) {
  369. }
  370. this.setSelectedIndex(i);
  371. break;
  372. }
  373. el = el.parentNode;
  374. }
  375. event.preventDefault();
  376. };
  377. /**
  378. * KeyDown event handler for header element. Arrow keys moves between pages.
  379. * Home and end selects the first/last page.
  380. *
  381. * @param {goog.events.BrowserEvent} event KeyDown event.
  382. * @private
  383. */
  384. goog.ui.TabPane.prototype.onHeaderKeyDown_ = function(event) {
  385. if (event.altKey || event.metaKey || event.ctrlKey) {
  386. return;
  387. }
  388. switch (event.keyCode) {
  389. case goog.events.KeyCodes.LEFT:
  390. var index = this.selected_.getIndex() - 1;
  391. this.setSelectedIndex(index < 0 ? this.pages_.length - 1 : index);
  392. break;
  393. case goog.events.KeyCodes.RIGHT:
  394. var index = this.selected_.getIndex() + 1;
  395. this.setSelectedIndex(index >= this.pages_.length ? 0 : index);
  396. break;
  397. case goog.events.KeyCodes.HOME:
  398. this.setSelectedIndex(0);
  399. break;
  400. case goog.events.KeyCodes.END:
  401. this.setSelectedIndex(this.pages_.length - 1);
  402. break;
  403. }
  404. };
  405. /**
  406. * Object representing an individual tab pane.
  407. *
  408. * @param {Element=} opt_el Container element to create the pane out of.
  409. * @param {(Element|string)=} opt_title Pane title or element to use as the
  410. * title. If not specified the first element in the container is used as
  411. * the title.
  412. * @param {goog.dom.DomHelper=} opt_domHelper Optional DOM helper
  413. * The first parameter can be omitted.
  414. * @constructor
  415. */
  416. goog.ui.TabPane.TabPage = function(opt_el, opt_title, opt_domHelper) {
  417. var title = null, el;
  418. if (goog.isString(opt_el) && !goog.isDef(opt_title)) {
  419. title = opt_el;
  420. } else if (opt_title) {
  421. title = opt_title;
  422. el = opt_el;
  423. } else if (opt_el) {
  424. var child = goog.dom.getFirstElementChild(opt_el);
  425. if (child) {
  426. title = goog.dom.getTextContent(child);
  427. child.parentNode.removeChild(child);
  428. }
  429. el = opt_el;
  430. }
  431. /**
  432. * DomHelper used to interact with the document, allowing components to be
  433. * created in a different window. This property is considered protected;
  434. * subclasses of Component may refer to it directly.
  435. * @type {goog.dom.DomHelper}
  436. * @protected
  437. * @suppress {underscore|visibility}
  438. */
  439. this.dom_ = opt_domHelper || goog.dom.getDomHelper();
  440. /**
  441. * Content element
  442. * @type {Element}
  443. * @private
  444. */
  445. this.elContent_ = el || this.dom_.createDom(goog.dom.TagName.DIV);
  446. /**
  447. * Title element
  448. * @type {Element}
  449. * @private
  450. */
  451. this.elTitle_ = this.dom_.createDom(goog.dom.TagName.LI, null, title);
  452. /**
  453. * Parent TabPane reference.
  454. * @type {goog.ui.TabPane?}
  455. * @private
  456. */
  457. this.parent_ = null;
  458. /**
  459. * Index for page in tab pane.
  460. * @type {?number}
  461. * @private
  462. */
  463. this.index_ = null;
  464. /**
  465. * Flags if this page is enabled and can be selected.
  466. * @type {boolean}
  467. * @private
  468. */
  469. this.enabled_ = true;
  470. };
  471. /**
  472. * @return {string} The title for tab page.
  473. */
  474. goog.ui.TabPane.TabPage.prototype.getTitle = function() {
  475. return goog.dom.getTextContent(this.elTitle_);
  476. };
  477. /**
  478. * Sets title for tab page.
  479. *
  480. * @param {string} title Title for tab page.
  481. */
  482. goog.ui.TabPane.TabPage.prototype.setTitle = function(title) {
  483. goog.dom.setTextContent(this.elTitle_, title);
  484. };
  485. /**
  486. * @return {Element} The title element.
  487. */
  488. goog.ui.TabPane.TabPage.prototype.getTitleElement = function() {
  489. return this.elTitle_;
  490. };
  491. /**
  492. * @return {Element} The content element.
  493. */
  494. goog.ui.TabPane.TabPage.prototype.getContentElement = function() {
  495. return this.elContent_;
  496. };
  497. /**
  498. * @return {?number} The index of page in tab pane.
  499. */
  500. goog.ui.TabPane.TabPage.prototype.getIndex = function() {
  501. return this.index_;
  502. };
  503. /**
  504. * @return {goog.ui.TabPane?} The parent tab pane for page.
  505. */
  506. goog.ui.TabPane.TabPage.prototype.getParent = function() {
  507. return this.parent_;
  508. };
  509. /**
  510. * Selects page in the associated tab pane.
  511. */
  512. goog.ui.TabPane.TabPage.prototype.select = function() {
  513. if (this.parent_) {
  514. this.parent_.setSelectedPage(this);
  515. }
  516. };
  517. /**
  518. * Sets the enabled state.
  519. *
  520. * @param {boolean} enabled Enabled state.
  521. */
  522. goog.ui.TabPane.TabPage.prototype.setEnabled = function(enabled) {
  523. this.enabled_ = enabled;
  524. this.elTitle_.className = enabled ?
  525. goog.getCssName('goog-tabpane-tab') :
  526. goog.getCssName('goog-tabpane-tab-disabled');
  527. };
  528. /**
  529. * Returns if the page is enabled.
  530. * @return {boolean} Whether the page is enabled or not.
  531. */
  532. goog.ui.TabPane.TabPage.prototype.isEnabled = function() {
  533. return this.enabled_;
  534. };
  535. /**
  536. * Sets visible state for page content and updates style of tab.
  537. *
  538. * @param {boolean} visible Visible state.
  539. * @private
  540. */
  541. goog.ui.TabPane.TabPage.prototype.setVisible_ = function(visible) {
  542. if (this.isEnabled()) {
  543. this.elContent_.style.display = visible ? '' : 'none';
  544. this.elTitle_.className = visible ?
  545. goog.getCssName('goog-tabpane-tab-selected') :
  546. goog.getCssName('goog-tabpane-tab');
  547. }
  548. };
  549. /**
  550. * Sets parent tab pane for tab page.
  551. *
  552. * @param {goog.ui.TabPane?} tabPane Tab strip object.
  553. * @param {number=} opt_index Index of page in pane.
  554. * @private
  555. */
  556. goog.ui.TabPane.TabPage.prototype.setParent_ = function(tabPane, opt_index) {
  557. this.parent_ = tabPane;
  558. this.index_ = goog.isDef(opt_index) ? opt_index : null;
  559. };
  560. /**
  561. * Object representing a tab pane page changed event.
  562. *
  563. * @param {string} type Event type.
  564. * @param {goog.ui.TabPane} target Tab widget initiating event.
  565. * @param {goog.ui.TabPane.TabPage} page Selected page in tab pane.
  566. * @extends {goog.events.Event}
  567. * @constructor
  568. * @final
  569. */
  570. goog.ui.TabPaneEvent = function(type, target, page) {
  571. goog.events.Event.call(this, type, target);
  572. /**
  573. * The selected page.
  574. * @type {goog.ui.TabPane.TabPage}
  575. */
  576. this.page = page;
  577. };
  578. goog.inherits(goog.ui.TabPaneEvent, goog.events.Event);