treenode.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. * @fileoverview Definition of the goog.ui.tree.TreeNode class.
  16. *
  17. * @author arv@google.com (Erik Arvidsson)
  18. * @author eae@google.com (Emil A Eklund)
  19. *
  20. * This is a based on the webfx tree control. See file comment in
  21. * treecontrol.js.
  22. */
  23. goog.provide('goog.ui.tree.TreeNode');
  24. goog.require('goog.ui.tree.BaseNode');
  25. goog.forwardDeclare('goog.ui.tree.TreeControl'); // circular
  26. /**
  27. * A single node in the tree.
  28. * @param {string|!goog.html.SafeHtml} content The content of the node label.
  29. * Strings are treated as plain-text and will be HTML escaped.
  30. * @param {Object=} opt_config The configuration for the tree. See
  31. * goog.ui.tree.TreeControl.defaultConfig. If not specified, a default config
  32. * will be used.
  33. * @param {goog.dom.DomHelper=} opt_domHelper Optional DOM helper.
  34. * @constructor
  35. * @extends {goog.ui.tree.BaseNode}
  36. */
  37. goog.ui.tree.TreeNode = function(content, opt_config, opt_domHelper) {
  38. goog.ui.tree.BaseNode.call(this, content, opt_config, opt_domHelper);
  39. };
  40. goog.inherits(goog.ui.tree.TreeNode, goog.ui.tree.BaseNode);
  41. /**
  42. * Returns the tree.
  43. * @return {?goog.ui.tree.TreeControl} The tree.
  44. * @override
  45. */
  46. goog.ui.tree.TreeNode.prototype.getTree = function() {
  47. if (this.tree) {
  48. return this.tree;
  49. }
  50. var parent = this.getParent();
  51. if (parent) {
  52. var tree = parent.getTree();
  53. if (tree) {
  54. this.setTreeInternal(tree);
  55. return tree;
  56. }
  57. }
  58. return null;
  59. };
  60. /**
  61. * Returns the source for the icon.
  62. * @return {string} Src for the icon.
  63. * @override
  64. */
  65. goog.ui.tree.TreeNode.prototype.getCalculatedIconClass = function() {
  66. var expanded = this.getExpanded();
  67. var expandedIconClass = this.getExpandedIconClass();
  68. if (expanded && expandedIconClass) {
  69. return expandedIconClass;
  70. }
  71. var iconClass = this.getIconClass();
  72. if (!expanded && iconClass) {
  73. return iconClass;
  74. }
  75. // fall back on default icons
  76. var config = this.getConfig();
  77. if (this.hasChildren()) {
  78. if (expanded && config.cssExpandedFolderIcon) {
  79. // return config.cssTreeIcon + ' ' + config.cssExpandedFolderIcon;
  80. return config.cssExpandedFolderIcon + ' ' +config.cssTreeIcon;
  81. } else if (!expanded && config.cssCollapsedFolderIcon) {
  82. // return config.cssTreeIcon + ' ' + config.cssCollapsedFolderIcon;
  83. return config.cssCollapsedFolderIcon + ' ' + config.cssTreeIcon;
  84. }
  85. } else {
  86. if (config.cssFileIcon) {
  87. // return config.cssTreeIcon + ' ' + config.cssFileIcon;
  88. return config.cssFileIcon + ' ' + config.cssTreeIcon;
  89. }
  90. }
  91. return '';
  92. };