Debian Codegraph @

closure-library/closure/goog/ui/tabpane.js

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