Debian Codegraph @

closure-library/closure/goog/ui/tree/basenode.js

raw
1 // Copyright 2007 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 Definition of the goog.ui.tree.BaseNode class.
17 *
18 * @author [email protected] (Erik Arvidsson)
19 * @author [email protected] (Emil A Eklund)
20 *
21 * This is a based on the webfx tree control. It since been updated to add
22 * typeahead support, as well as accessibility support using ARIA framework.
23 * See file comment in treecontrol.js.
24 */
25
26 goog.provide('goog.ui.tree.BaseNode');
27 goog.provide('goog.ui.tree.BaseNode.EventType');
28
29 goog.require('goog.Timer');
30 goog.require('goog.a11y.aria');
31 goog.require('goog.a11y.aria.State');
32 goog.require('goog.asserts');
33 goog.require('goog.dom.safe');
34 goog.require('goog.events.Event');
35 goog.require('goog.events.KeyCodes');
36 goog.require('goog.html.SafeHtml');
37 goog.require('goog.html.SafeStyle');
38 goog.require('goog.string');
39 goog.require('goog.string.StringBuffer');
40 goog.require('goog.style');
41 goog.require('goog.ui.Component');
42
43 goog.forwardDeclare('goog.ui.tree.TreeControl'); // circular
44
45
46
47 /**
48 * An abstract base class for a node in the tree.
49 *
50 * @param {string|!goog.html.SafeHtml} content The content of the node label.
51 * Strings are treated as plain-text and will be HTML escaped.
52 * @param {Object=} opt_config The configuration for the tree. See
53 * {@link goog.ui.tree.BaseNode.defaultConfig}. If not specified the
54 * default config will be used.
55 * @param {goog.dom.DomHelper=} opt_domHelper Optional DOM helper.
56 * @constructor
57 * @extends {goog.ui.Component}
58 */
59 goog.ui.tree.BaseNode = function(content, opt_config, opt_domHelper) {
60 goog.ui.Component.call(this, opt_domHelper);
61
62 /**
63 * The configuration for the tree.
64 * @type {Object}
65 * @private
66 */
67 this.config_ = opt_config || goog.ui.tree.BaseNode.defaultConfig;
68
69 /**
70 * HTML content of the node label.
71 * @type {!goog.html.SafeHtml}
72 * @private
73 */
74 this.html_ = goog.html.SafeHtml.htmlEscapePreservingNewlines(content);
75
76 /** @private {string} */
77 this.iconClass_;
78
79 /** @private {string} */
80 this.expandedIconClass_;
81
82 /** @protected {goog.ui.tree.TreeControl} */
83 this.tree;
84
85 /** @private {goog.ui.tree.BaseNode} */
86 this.previousSibling_;
87
88 /** @private {goog.ui.tree.BaseNode} */
89 this.nextSibling_;
90
91 /** @private {goog.ui.tree.BaseNode} */
92 this.firstChild_;
93
94 /** @private {goog.ui.tree.BaseNode} */
95 this.lastChild_;
96
97 /**
98 * Whether the tree item is selected.
99 * @private {boolean}
100 */
101 this.selected_ = false;
102
103 /**
104 * Whether the tree node is expanded.
105 * @private {boolean}
106 */
107 this.expanded_ = false;
108
109 /**
110 * Tooltip for the tree item
111 * @private {?string}
112 */
113 this.toolTip_ = null;
114
115 /**
116 * HTML that can appear after the label (so not inside the anchor).
117 * @private {!goog.html.SafeHtml}
118 */
119 this.afterLabelHtml_ = goog.html.SafeHtml.EMPTY;
120
121 /**
122 * Whether to allow user to collapse this node.
123 * @private {boolean}
124 */
125 this.isUserCollapsible_ = true;
126
127 /**
128 * Nesting depth of this node; cached result of computeDepth_.
129 * -1 if value has not been cached.
130 * @private {number}
131 */
132 this.depth_ = -1;
133 };
134 goog.inherits(goog.ui.tree.BaseNode, goog.ui.Component);
135
136
137 /**
138 * The event types dispatched by this class.
139 * @enum {string}
140 */
141 goog.ui.tree.BaseNode.EventType = {
142 BEFORE_EXPAND: 'beforeexpand',
143 EXPAND: 'expand',
144 BEFORE_COLLAPSE: 'beforecollapse',
145 COLLAPSE: 'collapse'
146 };
147
148
149 /**
150 * Map of nodes in existence. Needed to route events to the appropriate nodes.
151 * Nodes are added to the map at {@link #enterDocument} time and removed at
152 * {@link #exitDocument} time.
153 * @type {Object}
154 * @protected
155 */
156 goog.ui.tree.BaseNode.allNodes = {};
157
158
159 /** @override */
160 goog.ui.tree.BaseNode.prototype.disposeInternal = function() {
161 goog.ui.tree.BaseNode.superClass_.disposeInternal.call(this);
162 if (this.tree) {
163 this.tree.removeNode(this);
164 this.tree = null;
165 }
166 this.setElementInternal(null);
167 };
168
169
170 /**
171 * Adds roles and states.
172 * @protected
173 */
174 goog.ui.tree.BaseNode.prototype.initAccessibility = function() {
175 var el = this.getElement();
176 if (el) {
177 // Set an id for the label
178 var label = this.getLabelElement();
179 if (label && !label.id) {
180 label.id = this.getId() + '.label';
181 }
182
183 goog.a11y.aria.setRole(el, 'treeitem');
184 goog.a11y.aria.setState(el, 'selected', false);
185 goog.a11y.aria.setState(el, 'level', this.getDepth());
186 if (label) {
187 goog.a11y.aria.setState(el, 'labelledby', label.id);
188 }
189
190 var img = this.getIconElement();
191 if (img) {
192 goog.a11y.aria.setRole(img, 'presentation');
193 }
194 var ei = this.getExpandIconElement();
195 if (ei) {
196 goog.a11y.aria.setRole(ei, 'presentation');
197 }
198
199 var ce = this.getChildrenElement();
200 if (ce) {
201 goog.a11y.aria.setRole(ce, 'group');
202
203 // In case the children will be created lazily.
204 if (ce.hasChildNodes()) {
205 // Only set aria-expanded if the node has children (can be expanded).
206 goog.a11y.aria.setState(el, goog.a11y.aria.State.EXPANDED, false);
207
208 // do setsize for each child
209 var count = this.getChildCount();
210 for (var i = 1; i <= count; i++) {
211 var child = this.getChildAt(i - 1).getElement();
212 goog.asserts.assert(child, 'The child element cannot be null');
213 goog.a11y.aria.setState(child, 'setsize', count);
214 goog.a11y.aria.setState(child, 'posinset', i);
215 }
216 }
217 }
218 }
219 };
220
221
222 /** @override */
223 goog.ui.tree.BaseNode.prototype.createDom = function() {
224 var element = this.getDomHelper().safeHtmlToNode(this.toSafeHtml());
225 this.setElementInternal(/** @type {!Element} */ (element));
226 };
227
228
229 /** @override */
230 goog.ui.tree.BaseNode.prototype.enterDocument = function() {
231 goog.ui.tree.BaseNode.superClass_.enterDocument.call(this);
232 goog.ui.tree.BaseNode.allNodes[this.getId()] = this;
233 this.initAccessibility();
234 };
235
236
237 /** @override */
238 goog.ui.tree.BaseNode.prototype.exitDocument = function() {
239 goog.ui.tree.BaseNode.superClass_.exitDocument.call(this);
240 delete goog.ui.tree.BaseNode.allNodes[this.getId()];
241 };
242
243
244 /**
245 * The method assumes that the child doesn't have parent node yet.
246 * The {@code opt_render} argument is not used. If the parent node is expanded,
247 * the child node's state will be the same as the parent's. Otherwise the
248 * child's DOM tree won't be created.
249 * @override
250 */
251 goog.ui.tree.BaseNode.prototype.addChildAt = function(
252 child, index, opt_render) {
253 goog.asserts.assert(!child.getParent());
254 goog.asserts.assertInstanceof(child, goog.ui.tree.BaseNode);
255 var prevNode = this.getChildAt(index - 1);
256 var nextNode = this.getChildAt(index);
257
258 goog.ui.tree.BaseNode.superClass_.addChildAt.call(this, child, index);
259
260 child.previousSibling_ = prevNode;
261 child.nextSibling_ = nextNode;
262
263 if (prevNode) {
264 prevNode.nextSibling_ = child;
265 } else {
266 this.firstChild_ = child;
267 }
268 if (nextNode) {
269 nextNode.previousSibling_ = child;
270 } else {
271 this.lastChild_ = child;
272 }
273
274 var tree = this.getTree();
275 if (tree) {
276 child.setTreeInternal(tree);
277 }
278
279 child.setDepth_(this.getDepth() + 1);
280
281 var el = this.getElement();
282 if (el) {
283 this.updateExpandIcon();
284 goog.a11y.aria.setState(
285 el, goog.a11y.aria.State.EXPANDED, this.getExpanded());
286 if (this.getExpanded()) {
287 var childrenEl = this.getChildrenElement();
288 if (!child.getElement()) {
289 child.createDom();
290 }
291 var childElement = child.getElement();
292 var nextElement = nextNode && nextNode.getElement();
293 childrenEl.insertBefore(childElement, nextElement);
294
295 if (this.isInDocument()) {
296 child.enterDocument();
297 }
298
299 if (!nextNode) {
300 if (prevNode) {
301 prevNode.updateExpandIcon();
302 } else {
303 goog.style.setElementShown(childrenEl, true);
304 this.setExpanded(this.getExpanded());
305 }
306 }
307 }
308 }
309 };
310
311
312 /**
313 * Adds a node as a child to the current node.
314 * @param {goog.ui.tree.BaseNode} child The child to add.
315 * @param {goog.ui.tree.BaseNode=} opt_before If specified, the new child is
316 * added as a child before this one. If not specified, it's appended to the
317 * end.
318 * @return {!goog.ui.tree.BaseNode} The added child.
319 */
320 goog.ui.tree.BaseNode.prototype.add = function(child, opt_before) {
321 goog.asserts.assert(
322 !opt_before || opt_before.getParent() == this,
323 'Can only add nodes before siblings');
324 if (child.getParent()) {
325 child.getParent().removeChild(child);
326 }
327 this.addChildAt(
328 child, opt_before ? this.indexOfChild(opt_before) : this.getChildCount());
329 return child;
330 };
331
332
333 /**
334 * Removes a child. The caller is responsible for disposing the node.
335 * @param {goog.ui.Component|string} childNode The child to remove. Must be a
336 * {@link goog.ui.tree.BaseNode}.
337 * @param {boolean=} opt_unrender Unused. The child will always be unrendered.
338 * @return {!goog.ui.tree.BaseNode} The child that was removed.
339 * @override
340 */
341 goog.ui.tree.BaseNode.prototype.removeChild = function(
342 childNode, opt_unrender) {
343 // In reality, this only accepts BaseNodes.
344 var child = /** @type {goog.ui.tree.BaseNode} */ (childNode);
345
346 // if we remove selected or tree with the selected we should select this
347 var tree = this.getTree();
348 var selectedNode = tree ? tree.getSelectedItem() : null;
349 if (selectedNode == child || child.contains(selectedNode)) {
350 if (tree.hasFocus()) {
351 this.select();
352 goog.Timer.callOnce(this.onTimeoutSelect_, 10, this);
353 } else {
354 this.select();
355 }
356 }
357
358 goog.ui.tree.BaseNode.superClass_.removeChild.call(this, child);
359
360 if (this.lastChild_ == child) {
361 this.lastChild_ = child.previousSibling_;
362 }
363 if (this.firstChild_ == child) {
364 this.firstChild_ = child.nextSibling_;
365 }
366 if (child.previousSibling_) {
367 child.previousSibling_.nextSibling_ = child.nextSibling_;
368 }
369 if (child.nextSibling_) {
370 child.nextSibling_.previousSibling_ = child.previousSibling_;
371 }
372
373 var wasLast = child.isLastSibling();
374
375 child.tree = null;
376 child.depth_ = -1;
377
378 if (tree) {
379 // Tell the tree control that the child node is now removed.
380 tree.removeNode(child);
381
382 if (this.isInDocument()) {
383 var childrenEl = this.getChildrenElement();
384
385 if (child.isInDocument()) {
386 var childEl = child.getElement();
387 childrenEl.removeChild(childEl);
388
389 child.exitDocument();
390 }
391
392 if (wasLast) {
393 var newLast = this.getLastChild();
394 if (newLast) {
395 newLast.updateExpandIcon();
396 }
397 }
398 if (!this.hasChildren()) {
399 childrenEl.style.display = 'none';
400 this.updateExpandIcon();
401 this.updateIcon_();
402
403 var el = this.getElement();
404 if (el) {
405 goog.a11y.aria.removeState(el, goog.a11y.aria.State.EXPANDED);
406 }
407 }
408 }
409 }
410
411 return child;
412 };
413
414
415 /**
416 * @deprecated Use {@link #removeChild}.
417 */
418 goog.ui.tree.BaseNode.prototype.remove =
419 goog.ui.tree.BaseNode.prototype.removeChild;
420
421
422 /**
423 * Handler for setting focus asynchronously.
424 * @private
425 */
426 goog.ui.tree.BaseNode.prototype.onTimeoutSelect_ = function() {
427 this.select();
428 };
429
430
431 /**
432 * Returns the tree.
433 * @return {?goog.ui.tree.TreeControl}
434 */
435 goog.ui.tree.BaseNode.prototype.getTree = goog.abstractMethod;
436
437
438 /**
439 * Returns the depth of the node in the tree. Should not be overridden.
440 * @return {number} The non-negative depth of this node (the root is zero).
441 */
442 goog.ui.tree.BaseNode.prototype.getDepth = function() {
443 var depth = this.depth_;
444 if (depth < 0) {
445 depth = this.computeDepth_();
446 this.setDepth_(depth);
447 }
448 return depth;
449 };
450
451
452 /**
453 * Computes the depth of the node in the tree.
454 * Called only by getDepth, when the depth hasn't already been cached.
455 * @return {number} The non-negative depth of this node (the root is zero).
456 * @private
457 */
458 goog.ui.tree.BaseNode.prototype.computeDepth_ = function() {
459 var parent = this.getParent();
460 if (parent) {
461 return parent.getDepth() + 1;
462 } else {
463 return 0;
464 }
465 };
466
467
468 /**
469 * Changes the depth of a node (and all its descendants).
470 * @param {number} depth The new nesting depth; must be non-negative.
471 * @private
472 */
473 goog.ui.tree.BaseNode.prototype.setDepth_ = function(depth) {
474 if (depth != this.depth_) {
475 this.depth_ = depth;
476 var row = this.getRowElement();
477 if (row) {
478 var indent = this.getPixelIndent_() + 'px';
479 if (this.isRightToLeft()) {
480 row.style.paddingRight = indent;
481 } else {
482 row.style.paddingLeft = indent;
483 }
484 }
485 this.forEachChild(function(child) { child.setDepth_(depth + 1); });
486 }
487 };
488
489
490 /**
491 * Returns true if the node is a descendant of this node
492 * @param {goog.ui.tree.BaseNode} node The node to check.
493 * @return {boolean} True if the node is a descendant of this node, false
494 * otherwise.
495 */
496 goog.ui.tree.BaseNode.prototype.contains = function(node) {
497 var current = node;
498 while (current) {
499 if (current == this) {
500 return true;
501 }
502 current = current.getParent();
503 }
504 return false;
505 };
506
507
508 /**
509 * An array of empty children to return for nodes that have no children.
510 * @type {!Array<!goog.ui.tree.BaseNode>}
511 * @private
512 */
513 goog.ui.tree.BaseNode.EMPTY_CHILDREN_ = [];
514
515
516 /**
517 * @param {number} index 0-based index.
518 * @return {goog.ui.tree.BaseNode} The child at the given index; null if none.
519 */
520 goog.ui.tree.BaseNode.prototype.getChildAt;
521
522
523 /**
524 * Returns the children of this node.
525 * @return {!Array<!goog.ui.tree.BaseNode>} The children.
526 */
527 goog.ui.tree.BaseNode.prototype.getChildren = function() {
528 var children = [];
529 this.forEachChild(function(child) { children.push(child); });
530 return children;
531 };
532
533
534 /**
535 * @return {goog.ui.tree.BaseNode} The first child of this node.
536 */
537 goog.ui.tree.BaseNode.prototype.getFirstChild = function() {
538 return this.getChildAt(0);
539 };
540
541
542 /**
543 * @return {goog.ui.tree.BaseNode} The last child of this node.
544 */
545 goog.ui.tree.BaseNode.prototype.getLastChild = function() {
546 return this.getChildAt(this.getChildCount() - 1);
547 };
548
549
550 /**
551 * @return {goog.ui.tree.BaseNode} The previous sibling of this node.
552 */
553 goog.ui.tree.BaseNode.prototype.getPreviousSibling = function() {
554 return this.previousSibling_;
555 };
556
557
558 /**
559 * @return {goog.ui.tree.BaseNode} The next sibling of this node.
560 */
561 goog.ui.tree.BaseNode.prototype.getNextSibling = function() {
562 return this.nextSibling_;
563 };
564
565
566 /**
567 * @return {boolean} Whether the node is the last sibling.
568 */
569 goog.ui.tree.BaseNode.prototype.isLastSibling = function() {
570 return !this.nextSibling_;
571 };
572
573
574 /**
575 * @return {boolean} Whether the node is selected.
576 */
577 goog.ui.tree.BaseNode.prototype.isSelected = function() {
578 return this.selected_;
579 };
580
581
582 /**
583 * Selects the node.
584 */
585 goog.ui.tree.BaseNode.prototype.select = function() {
586 var tree = this.getTree();
587 if (tree) {
588 tree.setSelectedItem(this);
589 }
590 };
591
592
593 /**
594 * Originally it was intended to deselect the node but never worked.
595 * @deprecated Use {@code tree.setSelectedItem(null)}.
596 */
597 goog.ui.tree.BaseNode.prototype.deselect = goog.nullFunction;
598
599
600 /**
601 * Called from the tree to instruct the node change its selection state.
602 * @param {boolean} selected The new selection state.
603 * @protected
604 */
605 goog.ui.tree.BaseNode.prototype.setSelectedInternal = function(selected) {
606 if (this.selected_ == selected) {
607 return;
608 }
609 this.selected_ = selected;
610
611 this.updateRow();
612
613 var el = this.getElement();
614 if (el) {
615 goog.a11y.aria.setState(el, 'selected', selected);
616 if (selected) {
617 var treeElement = this.getTree().getElement();
618 goog.asserts.assert(
619 treeElement, 'The DOM element for the tree cannot be null');
620 goog.a11y.aria.setState(treeElement, 'activedescendant', this.getId());
621 }
622 }
623 };
624
625
626 /**
627 * @return {boolean} Whether the node is expanded.
628 */
629 goog.ui.tree.BaseNode.prototype.getExpanded = function() {
630 return this.expanded_;
631 };
632
633
634 /**
635 * Sets the node to be expanded internally, without state change events.
636 * @param {boolean} expanded Whether to expand or close the node.
637 */
638 goog.ui.tree.BaseNode.prototype.setExpandedInternal = function(expanded) {
639 this.expanded_ = expanded;
640 };
641
642
643 /**
644 * Sets the node to be expanded.
645 * @param {boolean} expanded Whether to expand or close the node.
646 */
647 goog.ui.tree.BaseNode.prototype.setExpanded = function(expanded) {
648 var isStateChange = expanded != this.expanded_;
649 if (isStateChange) {
650 // Only fire events if the expanded state has actually changed.
651 var prevented = !this.dispatchEvent(
652 expanded ? goog.ui.tree.BaseNode.EventType.BEFORE_EXPAND :
653 goog.ui.tree.BaseNode.EventType.BEFORE_COLLAPSE);
654 if (prevented) return;
655 }
656 var ce;
657 this.expanded_ = expanded;
658 var tree = this.getTree();
659 var el = this.getElement();
660
661 if (this.hasChildren()) {
662 if (!expanded && tree && this.contains(tree.getSelectedItem())) {
663 this.select();
664 }
665
666 if (el) {
667 ce = this.getChildrenElement();
668 if (ce) {
669 goog.style.setElementShown(ce, expanded);
670 goog.a11y.aria.setState(el, goog.a11y.aria.State.EXPANDED, expanded);
671
672 // Make sure we have the HTML for the children here.
673 if (expanded && this.isInDocument() && !ce.hasChildNodes()) {
674 var children = [];
675 this.forEachChild(function(child) {
676 children.push(child.toSafeHtml());
677 });
678 goog.dom.safe.setInnerHtml(ce, goog.html.SafeHtml.concat(children));
679 this.forEachChild(function(child) { child.enterDocument(); });
680 }
681 }
682 this.updateExpandIcon();
683 }
684 } else {
685 ce = this.getChildrenElement();
686 if (ce) {
687 goog.style.setElementShown(ce, false);
688 }
689 }
690 if (el) {
691 this.updateIcon_();
692 }
693
694 if (isStateChange) {
695 this.dispatchEvent(
696 expanded ? goog.ui.tree.BaseNode.EventType.EXPAND :
697 goog.ui.tree.BaseNode.EventType.COLLAPSE);
698 }
699 };
700
701
702 /**
703 * Toggles the expanded state of the node.
704 */
705 goog.ui.tree.BaseNode.prototype.toggle = function() {
706 this.setExpanded(!this.getExpanded());
707 };
708
709
710 /**
711 * Expands the node.
712 */
713 goog.ui.tree.BaseNode.prototype.expand = function() {
714 this.setExpanded(true);
715 };
716
717
718 /**
719 * Collapses the node.
720 */
721 goog.ui.tree.BaseNode.prototype.collapse = function() {
722 this.setExpanded(false);
723 };
724
725
726 /**
727 * Collapses the children of the node.
728 */
729 goog.ui.tree.BaseNode.prototype.collapseChildren = function() {
730 this.forEachChild(function(child) { child.collapseAll(); });
731 };
732
733
734 /**
735 * Collapses the children and the node.
736 */
737 goog.ui.tree.BaseNode.prototype.collapseAll = function() {
738 this.collapseChildren();
739 this.collapse();
740 };
741
742
743 /**
744 * Expands the children of the node.
745 */
746 goog.ui.tree.BaseNode.prototype.expandChildren = function() {
747 this.forEachChild(function(child) { child.expandAll(); });
748 };
749
750
751 /**
752 * Expands the children and the node.
753 */
754 goog.ui.tree.BaseNode.prototype.expandAll = function() {
755 this.expandChildren();
756 this.expand();
757 };
758
759
760 /**
761 * Expands the parent chain of this node so that it is visible.
762 */
763 goog.ui.tree.BaseNode.prototype.reveal = function() {
764 var parent = this.getParent();
765 if (parent) {
766 parent.setExpanded(true);
767 parent.reveal();
768 }
769 };
770
771
772 /**
773 * Sets whether the node will allow the user to collapse it.
774 * @param {boolean} isCollapsible Whether to allow node collapse.
775 */
776 goog.ui.tree.BaseNode.prototype.setIsUserCollapsible = function(isCollapsible) {
777 this.isUserCollapsible_ = isCollapsible;
778 if (!this.isUserCollapsible_) {
779 this.expand();
780 }
781 if (this.getElement()) {
782 this.updateExpandIcon();
783 }
784 };
785
786
787 /**
788 * @return {boolean} Whether the node is collapsible by user actions.
789 */
790 goog.ui.tree.BaseNode.prototype.isUserCollapsible = function() {
791 return this.isUserCollapsible_;
792 };
793
794
795 /**
796 * Creates HTML for the node.
797 * @return {!goog.html.SafeHtml}
798 * @protected
799 */
800 goog.ui.tree.BaseNode.prototype.toSafeHtml = function() {
801 var tree = this.getTree();
802 var hideLines = !tree.getShowLines() ||
803 tree == this.getParent() && !tree.getShowRootLines();
804
805 var childClass =
806 hideLines ? this.config_.cssChildrenNoLines : this.config_.cssChildren;
807
808 var nonEmptyAndExpanded = this.getExpanded() && this.hasChildren();
809
810 var attributes = {'class': childClass, 'style': this.getLineStyle()};
811
812 var content = [];
813 if (nonEmptyAndExpanded) {
814 // children
815 this.forEachChild(function(child) { content.push(child.toSafeHtml()); });
816 }
817
818 var children = goog.html.SafeHtml.create('div', attributes, content);
819
820 return goog.html.SafeHtml.create(
821 'div', {'class': this.config_.cssItem, 'id': this.getId()},
822 [this.getRowSafeHtml(), children]);
823 };
824
825
826 /**
827 * @return {number} The pixel indent of the row.
828 * @private
829 */
830 goog.ui.tree.BaseNode.prototype.getPixelIndent_ = function() {
831 return Math.max(0, (this.getDepth() - 1) * this.config_.indentWidth);
832 };
833
834
835 /**
836 * @return {!goog.html.SafeHtml} The html for the row.
837 * @protected
838 */
839 goog.ui.tree.BaseNode.prototype.getRowSafeHtml = function() {
840 var style = {};
841 style['padding-' + (this.isRightToLeft() ? 'right' : 'left')] =
842 this.getPixelIndent_() + 'px';
843 var attributes = {'class': this.getRowClassName(), 'style': style};
844 var content = [
845 this.getExpandIconSafeHtml(), this.getIconSafeHtml(),
846 this.getLabelSafeHtml()
847 ];
848 return goog.html.SafeHtml.create('div', attributes, content);
849 };
850
851
852 /**
853 * @return {string} The class name for the row.
854 * @protected
855 */
856 goog.ui.tree.BaseNode.prototype.getRowClassName = function() {
857 var selectedClass;
858 if (this.isSelected()) {
859 selectedClass = ' ' + this.config_.cssSelectedRow;
860 } else {
861 selectedClass = '';
862 }
863 return this.config_.cssTreeRow + selectedClass;
864 };
865
866
867 /**
868 * @return {!goog.html.SafeHtml} The html for the label.
869 * @protected
870 */
871 goog.ui.tree.BaseNode.prototype.getLabelSafeHtml = function() {
872 var html = goog.html.SafeHtml.create(
873 'span',
874 {'class': this.config_.cssItemLabel, 'title': this.getToolTip() || null},
875 this.getSafeHtml());
876 return goog.html.SafeHtml.concat(
877 html,
878 goog.html.SafeHtml.create('span', {}, this.getAfterLabelSafeHtml()));
879 };
880
881
882 /**
883 * Returns the html that appears after the label. This is useful if you want to
884 * put extra UI on the row of the label but not inside the anchor tag.
885 * @return {string} The html.
886 * @final
887 */
888 goog.ui.tree.BaseNode.prototype.getAfterLabelHtml = function() {
889 return goog.html.SafeHtml.unwrap(this.getAfterLabelSafeHtml());
890 };
891
892
893 /**
894 * Returns the html that appears after the label. This is useful if you want to
895 * put extra UI on the row of the label but not inside the anchor tag.
896 * @return {!goog.html.SafeHtml} The html.
897 */
898 goog.ui.tree.BaseNode.prototype.getAfterLabelSafeHtml = function() {
899 return this.afterLabelHtml_;
900 };
901
902
903 /**
904 * Sets the html that appears after the label. This is useful if you want to
905 * put extra UI on the row of the label but not inside the anchor tag.
906 * @param {!goog.html.SafeHtml} html The html.
907 */
908 goog.ui.tree.BaseNode.prototype.setAfterLabelSafeHtml = function(html) {
909 this.afterLabelHtml_ = html;
910 var el = this.getAfterLabelElement();
911 if (el) {
912 goog.dom.safe.setInnerHtml(el, html);
913 }
914 };
915
916
917 /**
918 * @return {!goog.html.SafeHtml} The html for the icon.
919 * @protected
920 */
921 goog.ui.tree.BaseNode.prototype.getIconSafeHtml = function() {
922 return goog.html.SafeHtml.create('span', {
923 'style': {'display': 'inline-block'},
924 'class': this.getCalculatedIconClass()
925 });
926 };
927
928
929 /**
930 * Gets the calculated icon class.
931 * @protected
932 */
933 goog.ui.tree.BaseNode.prototype.getCalculatedIconClass = goog.abstractMethod;
934
935
936 /**
937 * @return {!goog.html.SafeHtml} The source for the icon.
938 * @protected
939 */
940 goog.ui.tree.BaseNode.prototype.getExpandIconSafeHtml = function() {
941 return goog.html.SafeHtml.create('span', {
942 'type': 'expand',
943 'style': {'display': 'inline-block'},
944 'class': this.getExpandIconClass()
945 });
946 };
947
948
949 /**
950 * @return {string} The class names of the icon used for expanding the node.
951 * @protected
952 */
953 goog.ui.tree.BaseNode.prototype.getExpandIconClass = function() {
954 var tree = this.getTree();
955 var hideLines = !tree.getShowLines() ||
956 tree == this.getParent() && !tree.getShowRootLines();
957
958 var config = this.config_;
959 var sb = new goog.string.StringBuffer();
960 sb.append(config.cssTreeIcon, ' ', config.cssExpandTreeIcon, ' ');
961
962 if (this.hasChildren()) {
963 var bits = 0;
964 /*
965 Bitmap used to determine which icon to use
966 1 Plus
967 2 Minus
968 4 T Line
969 8 L Line
970 */
971
972 if (tree.getShowExpandIcons() && this.isUserCollapsible_) {
973 if (this.getExpanded()) {
974 bits = 2;
975 } else {
976 bits = 1;
977 }
978 }
979
980 if (!hideLines) {
981 if (this.isLastSibling()) {
982 bits += 4;
983 } else {
984 bits += 8;
985 }
986 }
987
988 switch (bits) {
989 case 1:
990 sb.append(config.cssExpandTreeIconPlus);
991 break;
992 case 2:
993 sb.append(config.cssExpandTreeIconMinus);
994 break;
995 case 4:
996 sb.append(config.cssExpandTreeIconL);
997 break;
998 case 5:
999 sb.append(config.cssExpandTreeIconLPlus);
1000 break;
1001 case 6:
1002 sb.append(config.cssExpandTreeIconLMinus);
1003 break;
1004 case 8:
1005 sb.append(config.cssExpandTreeIconT);
1006 break;
1007 case 9:
1008 sb.append(config.cssExpandTreeIconTPlus);
1009 break;
1010 case 10:
1011 sb.append(config.cssExpandTreeIconTMinus);
1012 break;
1013 default: // 0
1014 sb.append(config.cssExpandTreeIconBlank);
1015 }
1016 } else {
1017 if (hideLines) {
1018 sb.append(config.cssExpandTreeIconBlank);
1019 } else if (this.isLastSibling()) {
1020 sb.append(config.cssExpandTreeIconL);
1021 } else {
1022 sb.append(config.cssExpandTreeIconT);
1023 }
1024 }
1025 return sb.toString();
1026 };
1027
1028
1029 /**
1030 * @return {!goog.html.SafeStyle} The line style.
1031 */
1032 goog.ui.tree.BaseNode.prototype.getLineStyle = function() {
1033 var nonEmptyAndExpanded = this.getExpanded() && this.hasChildren();
1034 return goog.html.SafeStyle.create({
1035 'background-position': this.getBackgroundPosition(),
1036 'display': nonEmptyAndExpanded ? null : 'none'
1037 });
1038 };
1039
1040
1041 /**
1042 * @return {string} The background position style value.
1043 */
1044 goog.ui.tree.BaseNode.prototype.getBackgroundPosition = function() {
1045 return (this.isLastSibling() ? '-100' : (this.getDepth() - 1) *
1046 this.config_.indentWidth) +
1047 'px 0';
1048 };
1049
1050
1051 /**
1052 * @return {Element} The element for the tree node.
1053 * @override
1054 */
1055 goog.ui.tree.BaseNode.prototype.getElement = function() {
1056 var el = goog.ui.tree.BaseNode.superClass_.getElement.call(this);
1057 if (!el) {
1058 el = this.getDomHelper().getElement(this.getId());
1059 this.setElementInternal(el);
1060 }
1061 return el;
1062 };
1063
1064
1065 /**
1066 * @return {Element} The row is the div that is used to draw the node without
1067 * the children.
1068 */
1069 goog.ui.tree.BaseNode.prototype.getRowElement = function() {
1070 var el = this.getElement();
1071 return el ? /** @type {Element} */ (el.firstChild) : null;
1072 };
1073
1074
1075 /**
1076 * @return {Element} The expanded icon element.
1077 * @protected
1078 */
1079 goog.ui.tree.BaseNode.prototype.getExpandIconElement = function() {
1080 var el = this.getRowElement();
1081 return el ? /** @type {Element} */ (el.firstChild) : null;
1082 };
1083
1084
1085 /**
1086 * @return {Element} The icon element.
1087 * @protected
1088 */
1089 goog.ui.tree.BaseNode.prototype.getIconElement = function() {
1090 var el = this.getRowElement();
1091 return el ? /** @type {Element} */ (el.childNodes[1]) : null;
1092 };
1093
1094
1095 /**
1096 * @return {Element} The label element.
1097 */
1098 goog.ui.tree.BaseNode.prototype.getLabelElement = function() {
1099 var el = this.getRowElement();
1100 // TODO: find/fix race condition that requires us to add
1101 // the lastChild check
1102 return el && el.lastChild ?
1103 /** @type {Element} */ (el.lastChild.previousSibling) :
1104 null;
1105 };
1106
1107
1108 /**
1109 * @return {Element} The element after the label.
1110 */
1111 goog.ui.tree.BaseNode.prototype.getAfterLabelElement = function() {
1112 var el = this.getRowElement();
1113 return el ? /** @type {Element} */ (el.lastChild) : null;
1114 };
1115
1116
1117 /**
1118 * @return {Element} The div containing the children.
1119 * @protected
1120 */
1121 goog.ui.tree.BaseNode.prototype.getChildrenElement = function() {
1122 var el = this.getElement();
1123 return el ? /** @type {Element} */ (el.lastChild) : null;
1124 };
1125
1126
1127 /**
1128 * Sets the icon class for the node.
1129 * @param {string} s The icon class.
1130 */
1131 goog.ui.tree.BaseNode.prototype.setIconClass = function(s) {
1132 this.iconClass_ = s;
1133 if (this.isInDocument()) {
1134 this.updateIcon_();
1135 }
1136 };
1137
1138
1139 /**
1140 * Gets the icon class for the node.
1141 * @return {string} s The icon source.
1142 */
1143 goog.ui.tree.BaseNode.prototype.getIconClass = function() {
1144 return this.iconClass_;
1145 };
1146
1147
1148 /**
1149 * Sets the icon class for when the node is expanded.
1150 * @param {string} s The expanded icon class.
1151 */
1152 goog.ui.tree.BaseNode.prototype.setExpandedIconClass = function(s) {
1153 this.expandedIconClass_ = s;
1154 if (this.isInDocument()) {
1155 this.updateIcon_();
1156 }
1157 };
1158
1159
1160 /**
1161 * Gets the icon class for when the node is expanded.
1162 * @return {string} The class.
1163 */
1164 goog.ui.tree.BaseNode.prototype.getExpandedIconClass = function() {
1165 return this.expandedIconClass_;
1166 };
1167
1168
1169 /**
1170 * Sets the text of the label.
1171 * @param {string} s The plain text of the label.
1172 */
1173 goog.ui.tree.BaseNode.prototype.setText = function(s) {
1174 this.setSafeHtml(goog.html.SafeHtml.htmlEscape(s));
1175 };
1176
1177
1178 /**
1179 * Returns the text of the label. If the text was originally set as HTML, the
1180 * return value is unspecified.
1181 * @return {string} The plain text of the label.
1182 */
1183 goog.ui.tree.BaseNode.prototype.getText = function() {
1184 return goog.string.unescapeEntities(goog.html.SafeHtml.unwrap(this.html_));
1185 };
1186
1187
1188 /**
1189 * Sets the HTML of the label.
1190 * @param {!goog.html.SafeHtml} html The HTML object for the label.
1191 */
1192 goog.ui.tree.BaseNode.prototype.setSafeHtml = function(html) {
1193 this.html_ = html;
1194 var el = this.getLabelElement();
1195 if (el) {
1196 goog.dom.safe.setInnerHtml(el, html);
1197 }
1198 var tree = this.getTree();
1199 if (tree) {
1200 // Tell the tree control about the updated label text.
1201 tree.setNode(this);
1202 }
1203 };
1204
1205
1206 /**
1207 * Returns the html of the label.
1208 * @return {string} The html string of the label.
1209 * @final
1210 */
1211 goog.ui.tree.BaseNode.prototype.getHtml = function() {
1212 return goog.html.SafeHtml.unwrap(this.getSafeHtml());
1213 };
1214
1215
1216 /**
1217 * Returns the html of the label.
1218 * @return {!goog.html.SafeHtml} The html string of the label.
1219 */
1220 goog.ui.tree.BaseNode.prototype.getSafeHtml = function() {
1221 return this.html_;
1222 };
1223
1224
1225 /**
1226 * Sets the text of the tooltip.
1227 * @param {string} s The tooltip text to set.
1228 */
1229 goog.ui.tree.BaseNode.prototype.setToolTip = function(s) {
1230 this.toolTip_ = s;
1231 var el = this.getLabelElement();
1232 if (el) {
1233 el.title = s;
1234 }
1235 };
1236
1237
1238 /**
1239 * Returns the text of the tooltip.
1240 * @return {?string} The tooltip text.
1241 */
1242 goog.ui.tree.BaseNode.prototype.getToolTip = function() {
1243 return this.toolTip_;
1244 };
1245
1246
1247 /**
1248 * Updates the row styles.
1249 */
1250 goog.ui.tree.BaseNode.prototype.updateRow = function() {
1251 var rowEl = this.getRowElement();
1252 if (rowEl) {
1253 rowEl.className = this.getRowClassName();
1254 }
1255 };
1256
1257
1258 /**
1259 * Updates the expand icon of the node.
1260 */
1261 goog.ui.tree.BaseNode.prototype.updateExpandIcon = function() {
1262 var img = this.getExpandIconElement();
1263 if (img) {
1264 img.className = this.getExpandIconClass();
1265 }
1266 var cel = this.getChildrenElement();
1267 if (cel) {
1268 cel.style.backgroundPosition = this.getBackgroundPosition();
1269 }
1270 };
1271
1272
1273 /**
1274 * Updates the icon of the node. Assumes that this.getElement() is created.
1275 * @private
1276 */
1277 goog.ui.tree.BaseNode.prototype.updateIcon_ = function() {
1278 this.getIconElement().className = this.getCalculatedIconClass();
1279 };
1280
1281
1282 /**
1283 * Handles mouse down event.
1284 * @param {!goog.events.BrowserEvent} e The browser event.
1285 * @protected
1286 */
1287 goog.ui.tree.BaseNode.prototype.onMouseDown = function(e) {
1288 var el = e.target;
1289 // expand icon
1290 var type = el.getAttribute('type');
1291 if (type == 'expand' && this.hasChildren()) {
1292 if (this.isUserCollapsible_) {
1293 this.toggle();
1294 }
1295 return;
1296 }
1297
1298 this.select();
1299 this.updateRow();
1300 };
1301
1302
1303 /**
1304 * Handles a click event.
1305 * @param {!goog.events.BrowserEvent} e The browser event.
1306 * @protected
1307 * @suppress {underscore|visibility}
1308 */
1309 goog.ui.tree.BaseNode.prototype.onClick_ = goog.events.Event.preventDefault;
1310
1311
1312 /**
1313 * Handles a double click event.
1314 * @param {!goog.events.BrowserEvent} e The browser event.
1315 * @protected
1316 * @suppress {underscore|visibility}
1317 */
1318 goog.ui.tree.BaseNode.prototype.onDoubleClick_ = function(e) {
1319 var el = e.target;
1320 // expand icon
1321 var type = el.getAttribute('type');
1322 if (type == 'expand' && this.hasChildren()) {
1323 return;
1324 }
1325
1326 if (this.isUserCollapsible_) {
1327 this.toggle();
1328 }
1329 };
1330
1331
1332 /**
1333 * Handles a key down event.
1334 * @param {!goog.events.BrowserEvent} e The browser event.
1335 * @return {boolean} The handled value.
1336 * @protected
1337 */
1338 goog.ui.tree.BaseNode.prototype.onKeyDown = function(e) {
1339 var handled = true;
1340 switch (e.keyCode) {
1341 case goog.events.KeyCodes.RIGHT:
1342 if (e.altKey) {
1343 break;
1344 }
1345 if (this.hasChildren()) {
1346 if (!this.getExpanded()) {
1347 this.setExpanded(true);
1348 } else {
1349 this.getFirstChild().select();
1350 }
1351 }
1352 break;
1353
1354 case goog.events.KeyCodes.LEFT:
1355 if (e.altKey) {
1356 break;
1357 }
1358 if (this.hasChildren() && this.getExpanded() && this.isUserCollapsible_) {
1359 this.setExpanded(false);
1360 } else {
1361 var parent = this.getParent();
1362 var tree = this.getTree();
1363 // don't go to root if hidden
1364 if (parent && (tree.getShowRootNode() || parent != tree)) {
1365 parent.select();
1366 }
1367 }
1368 break;
1369
1370 case goog.events.KeyCodes.DOWN:
1371 var nextNode = this.getNextShownNode();
1372 if (nextNode) {
1373 nextNode.select();
1374 }
1375 break;
1376
1377 case goog.events.KeyCodes.UP:
1378 var previousNode = this.getPreviousShownNode();
1379 if (previousNode) {
1380 previousNode.select();
1381 }
1382 break;
1383
1384 default:
1385 handled = false;
1386 }
1387
1388 if (handled) {
1389 e.preventDefault();
1390 var tree = this.getTree();
1391 if (tree) {
1392 // clear type ahead buffer as user navigates with arrow keys
1393 tree.clearTypeAhead();
1394 }
1395 }
1396
1397 return handled;
1398 };
1399
1400
1401
1402 /**
1403 * @return {goog.ui.tree.BaseNode} The last shown descendant.
1404 */
1405 goog.ui.tree.BaseNode.prototype.getLastShownDescendant = function() {
1406 if (!this.getExpanded() || !this.hasChildren()) {
1407 return this;
1408 }
1409 // we know there is at least 1 child
1410 return this.getLastChild().getLastShownDescendant();
1411 };
1412
1413
1414 /**
1415 * @return {goog.ui.tree.BaseNode} The next node to show or null if there isn't
1416 * a next node to show.
1417 */
1418 goog.ui.tree.BaseNode.prototype.getNextShownNode = function() {
1419 if (this.hasChildren() && this.getExpanded()) {
1420 return this.getFirstChild();
1421 } else {
1422 var parent = this;
1423 var next;
1424 while (parent != this.getTree()) {
1425 next = parent.getNextSibling();
1426 if (next != null) {
1427 return next;
1428 }
1429 parent = parent.getParent();
1430 }
1431 return null;
1432 }
1433 };
1434
1435
1436 /**
1437 * @return {goog.ui.tree.BaseNode} The previous node to show.
1438 */
1439 goog.ui.tree.BaseNode.prototype.getPreviousShownNode = function() {
1440 var ps = this.getPreviousSibling();
1441 if (ps != null) {
1442 return ps.getLastShownDescendant();
1443 }
1444 var parent = this.getParent();
1445 var tree = this.getTree();
1446 if (!tree.getShowRootNode() && parent == tree) {
1447 return null;
1448 }
1449 // The root is the first node.
1450 if (this == tree) {
1451 return null;
1452 }
1453 return /** @type {goog.ui.tree.BaseNode} */ (parent);
1454 };
1455
1456
1457 /**
1458 * @return {*} Data set by the client.
1459 * @deprecated Use {@link #getModel} instead.
1460 */
1461 goog.ui.tree.BaseNode.prototype.getClientData =
1462 goog.ui.tree.BaseNode.prototype.getModel;
1463
1464
1465 /**
1466 * Sets client data to associate with the node.
1467 * @param {*} data The client data to associate with the node.
1468 * @deprecated Use {@link #setModel} instead.
1469 */
1470 goog.ui.tree.BaseNode.prototype.setClientData =
1471 goog.ui.tree.BaseNode.prototype.setModel;
1472
1473
1474 /**
1475 * @return {Object} The configuration for the tree.
1476 */
1477 goog.ui.tree.BaseNode.prototype.getConfig = function() {
1478 return this.config_;
1479 };
1480
1481
1482 /**
1483 * Internal method that is used to set the tree control on the node.
1484 * @param {goog.ui.tree.TreeControl} tree The tree control.
1485 */
1486 goog.ui.tree.BaseNode.prototype.setTreeInternal = function(tree) {
1487 if (this.tree != tree) {
1488 this.tree = tree;
1489 // Add new node to the type ahead node map.
1490 tree.setNode(this);
1491 this.forEachChild(function(child) { child.setTreeInternal(tree); });
1492 }
1493 };
1494
1495
1496 /**
1497 * A default configuration for the tree.
1498 */
1499 goog.ui.tree.BaseNode.defaultConfig = {
1500 indentWidth: 19,
1501 cssRoot: goog.getCssName('goog-tree-root') + ' ' +
1502 goog.getCssName('goog-tree-item'),
1503 cssHideRoot: goog.getCssName('goog-tree-hide-root'),
1504 cssItem: goog.getCssName('goog-tree-item'),
1505 cssChildren: goog.getCssName('goog-tree-children'),
1506 cssChildrenNoLines: goog.getCssName('goog-tree-children-nolines'),
1507 cssTreeRow: goog.getCssName('goog-tree-row'),
1508 cssItemLabel: goog.getCssName('goog-tree-item-label'),
1509 cssTreeIcon: goog.getCssName('goog-tree-icon'),
1510 cssExpandTreeIcon: goog.getCssName('goog-tree-expand-icon'),
1511 cssExpandTreeIconPlus: goog.getCssName('goog-tree-expand-icon-plus'),
1512 cssExpandTreeIconMinus: goog.getCssName('goog-tree-expand-icon-minus'),
1513 cssExpandTreeIconTPlus: goog.getCssName('goog-tree-expand-icon-tplus'),
1514 cssExpandTreeIconTMinus: goog.getCssName('goog-tree-expand-icon-tminus'),
1515 cssExpandTreeIconLPlus: goog.getCssName('goog-tree-expand-icon-lplus'),
1516 cssExpandTreeIconLMinus: goog.getCssName('goog-tree-expand-icon-lminus'),
1517 cssExpandTreeIconT: goog.getCssName('goog-tree-expand-icon-t'),
1518 cssExpandTreeIconL: goog.getCssName('goog-tree-expand-icon-l'),
1519 cssExpandTreeIconBlank: goog.getCssName('goog-tree-expand-icon-blank'),
1520 cssExpandedFolderIcon: goog.getCssName('goog-tree-expanded-folder-icon'),
1521 cssCollapsedFolderIcon: goog.getCssName('goog-tree-collapsed-folder-icon'),
1522 cssFileIcon: goog.getCssName('goog-tree-file-icon'),
1523 cssExpandedRootIcon: goog.getCssName('goog-tree-expanded-folder-icon'),
1524 cssCollapsedRootIcon: goog.getCssName('goog-tree-collapsed-folder-icon'),
1525 cssSelectedRow: goog.getCssName('selected')
1526 };