treecontrol.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  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.TreeControl class, which
  16. * provides a way to view a hierarchical set of data.
  17. *
  18. * @author arv@google.com (Erik Arvidsson)
  19. * @author eae@google.com (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. *
  24. * @see ../../demos/tree/demo.html
  25. */
  26. goog.provide('goog.ui.tree.TreeControl');
  27. goog.require('goog.a11y.aria');
  28. goog.require('goog.asserts');
  29. goog.require('goog.dom.classlist');
  30. goog.require('goog.events.EventType');
  31. goog.require('goog.events.FocusHandler');
  32. goog.require('goog.events.KeyHandler');
  33. goog.require('goog.html.SafeHtml');
  34. goog.require('goog.log');
  35. goog.require('goog.ui.tree.BaseNode');
  36. goog.require('goog.ui.tree.TreeNode');
  37. goog.require('goog.ui.tree.TypeAhead');
  38. goog.require('goog.userAgent');
  39. /**
  40. * This creates a TreeControl object. A tree control provides a way to
  41. * view a hierarchical set of data.
  42. * @param {string|!goog.html.SafeHtml} content The content of the node label.
  43. * Strings are treated as plain-text and will be HTML escaped.
  44. * @param {Object=} opt_config The configuration for the tree. See
  45. * goog.ui.tree.TreeControl.defaultConfig. If not specified, a default config
  46. * will be used.
  47. * @param {goog.dom.DomHelper=} opt_domHelper Optional DOM helper.
  48. * @constructor
  49. * @extends {goog.ui.tree.BaseNode}
  50. */
  51. goog.ui.tree.TreeControl = function(content, opt_config, opt_domHelper) {
  52. goog.ui.tree.BaseNode.call(this, content, opt_config, opt_domHelper);
  53. // The root is open and selected by default.
  54. this.setExpandedInternal(true);
  55. this.setSelectedInternal(true);
  56. this.selectedItem_ = this;
  57. /**
  58. * Used for typeahead support.
  59. * @private {!goog.ui.tree.TypeAhead}
  60. */
  61. this.typeAhead_ = new goog.ui.tree.TypeAhead();
  62. /**
  63. * The object handling keyboard events.
  64. * @private {?goog.events.KeyHandler}
  65. */
  66. this.keyHandler_ = null;
  67. /**
  68. * The object handling focus events.
  69. * @private {?goog.events.FocusHandler}
  70. */
  71. this.focusHandler_ = null;
  72. /**
  73. * Logger
  74. * @private {?goog.log.Logger}
  75. */
  76. this.logger_ = goog.log.getLogger('this');
  77. /**
  78. * Whether the tree is focused.
  79. * @private {boolean}
  80. */
  81. this.focused_ = false;
  82. /**
  83. * Child node that currently has focus.
  84. * @private {?goog.ui.tree.BaseNode}
  85. */
  86. this.focusedNode_ = null;
  87. /**
  88. * Whether to show lines.
  89. * @private {boolean}
  90. */
  91. this.showLines_ = true;
  92. /**
  93. * Whether to show expanded lines.
  94. * @private {boolean}
  95. */
  96. this.showExpandIcons_ = true;
  97. /**
  98. * Whether to show the root node.
  99. * @private {boolean}
  100. */
  101. this.showRootNode_ = true;
  102. /**
  103. * Whether to show the root lines.
  104. * @private {boolean}
  105. */
  106. this.showRootLines_ = true;
  107. if (goog.userAgent.IE) {
  108. try {
  109. // works since IE6SP1
  110. document.execCommand('BackgroundImageCache', false, true);
  111. } catch (e) {
  112. goog.log.warning(this.logger_, 'Failed to enable background image cache');
  113. }
  114. }
  115. };
  116. goog.inherits(goog.ui.tree.TreeControl, goog.ui.tree.BaseNode);
  117. /** @override */
  118. goog.ui.tree.TreeControl.prototype.getTree = function() {
  119. return this;
  120. };
  121. /** @override */
  122. goog.ui.tree.TreeControl.prototype.getDepth = function() {
  123. return 0;
  124. };
  125. /**
  126. * Expands the parent chain of this node so that it is visible.
  127. * @override
  128. */
  129. goog.ui.tree.TreeControl.prototype.reveal = function() {
  130. // always expanded by default
  131. // needs to be overriden so that we don't try to reveal our parent
  132. // which is a generic component
  133. };
  134. /**
  135. * Handles focus on the tree.
  136. * @param {!goog.events.BrowserEvent} e The browser event.
  137. * @private
  138. */
  139. goog.ui.tree.TreeControl.prototype.handleFocus_ = function(e) {
  140. this.focused_ = true;
  141. goog.dom.classlist.add(
  142. goog.asserts.assert(this.getElement()), goog.getCssName('focused'));
  143. if (this.selectedItem_) {
  144. this.selectedItem_.select();
  145. }
  146. };
  147. /**
  148. * Handles blur on the tree.
  149. * @param {!goog.events.BrowserEvent} e The browser event.
  150. * @private
  151. */
  152. goog.ui.tree.TreeControl.prototype.handleBlur_ = function(e) {
  153. this.focused_ = false;
  154. goog.dom.classlist.remove(
  155. goog.asserts.assert(this.getElement()), goog.getCssName('focused'));
  156. };
  157. /**
  158. * @return {boolean} Whether the tree has keyboard focus.
  159. */
  160. goog.ui.tree.TreeControl.prototype.hasFocus = function() {
  161. return this.focused_;
  162. };
  163. /** @override */
  164. goog.ui.tree.TreeControl.prototype.getExpanded = function() {
  165. return !this.showRootNode_ ||
  166. goog.ui.tree.TreeControl.superClass_.getExpanded.call(this);
  167. };
  168. /** @override */
  169. goog.ui.tree.TreeControl.prototype.setExpanded = function(expanded) {
  170. if (!this.showRootNode_) {
  171. this.setExpandedInternal(expanded);
  172. } else {
  173. goog.ui.tree.TreeControl.superClass_.setExpanded.call(this, expanded);
  174. }
  175. };
  176. /** @override */
  177. goog.ui.tree.TreeControl.prototype.getExpandIconSafeHtml = function() {
  178. // no expand icon for root element
  179. return goog.html.SafeHtml.EMPTY;
  180. };
  181. /** @override */
  182. goog.ui.tree.TreeControl.prototype.getIconElement = function() {
  183. var el = this.getRowElement();
  184. return el ? /** @type {Element} */ (el.firstChild) : null;
  185. };
  186. /** @override */
  187. goog.ui.tree.TreeControl.prototype.getExpandIconElement = function() {
  188. // no expand icon for root element
  189. return null;
  190. };
  191. /** @override */
  192. goog.ui.tree.TreeControl.prototype.updateExpandIcon = function() {
  193. // no expand icon
  194. };
  195. /** @override */
  196. goog.ui.tree.TreeControl.prototype.getRowClassName = function() {
  197. return goog.ui.tree.TreeControl.superClass_.getRowClassName.call(this) +
  198. (this.showRootNode_ ? '' : ' ' + this.getConfig().cssHideRoot);
  199. };
  200. /**
  201. * Returns the source for the icon.
  202. * @return {string} Src for the icon.
  203. * @override
  204. */
  205. goog.ui.tree.TreeControl.prototype.getCalculatedIconClass = function() {
  206. var expanded = this.getExpanded();
  207. var expandedIconClass = this.getExpandedIconClass();
  208. if (expanded && expandedIconClass) {
  209. return expandedIconClass;
  210. }
  211. var iconClass = this.getIconClass();
  212. if (!expanded && iconClass) {
  213. return iconClass;
  214. }
  215. // fall back on default icons
  216. var config = this.getConfig();
  217. if (expanded && config.cssExpandedRootIcon) {
  218. return config.cssExpandedRootIcon + ' '+ config.cssTreeIcon;
  219. } else if (!expanded && config.cssCollapsedRootIcon) {
  220. return config.cssCollapsedRootIcon + ' ' + config.cssTreeIcon;
  221. }
  222. return '';
  223. };
  224. /**
  225. * Sets the selected item.
  226. * @param {goog.ui.tree.BaseNode} node The item to select.
  227. */
  228. goog.ui.tree.TreeControl.prototype.setSelectedItem = function(node) {
  229. if (this.selectedItem_ == node) {
  230. return;
  231. }
  232. var hadFocus = false;
  233. if (this.selectedItem_) {
  234. hadFocus = this.selectedItem_ == this.focusedNode_;
  235. this.selectedItem_.setSelectedInternal(false);
  236. }
  237. this.selectedItem_ = node;
  238. if (node) {
  239. node.setSelectedInternal(true);
  240. if (hadFocus) {
  241. node.select();
  242. }
  243. }
  244. this.dispatchEvent(goog.events.EventType.CHANGE);
  245. };
  246. /**
  247. * Returns the selected item.
  248. * @return {goog.ui.tree.BaseNode} The currently selected item.
  249. */
  250. goog.ui.tree.TreeControl.prototype.getSelectedItem = function() {
  251. return this.selectedItem_;
  252. };
  253. /**
  254. * Sets whether to show lines.
  255. * @param {boolean} b Whether to show lines.
  256. */
  257. goog.ui.tree.TreeControl.prototype.setShowLines = function(b) {
  258. if (this.showLines_ != b) {
  259. this.showLines_ = b;
  260. if (this.isInDocument()) {
  261. this.updateLinesAndExpandIcons_();
  262. }
  263. }
  264. };
  265. /**
  266. * @return {boolean} Whether to show lines.
  267. */
  268. goog.ui.tree.TreeControl.prototype.getShowLines = function() {
  269. return this.showLines_;
  270. };
  271. /**
  272. * Updates the lines after the tree has been drawn.
  273. * @private
  274. */
  275. goog.ui.tree.TreeControl.prototype.updateLinesAndExpandIcons_ = function() {
  276. var tree = this;
  277. var showLines = tree.getShowLines();
  278. var showRootLines = tree.getShowRootLines();
  279. /**
  280. * Recursively walk through all nodes and update the class names of the
  281. * expand icon and the children element.
  282. * @param {!goog.ui.tree.BaseNode} node
  283. */
  284. function updateShowLines(node) {
  285. var childrenEl = node.getChildrenElement();
  286. if (childrenEl) {
  287. var hideLines = !showLines || tree == node.getParent() && !showRootLines;
  288. var childClass = hideLines ? node.getConfig().cssChildrenNoLines :
  289. node.getConfig().cssChildren;
  290. childrenEl.className = childClass;
  291. var expandIconEl = node.getExpandIconElement();
  292. if (expandIconEl) {
  293. expandIconEl.className = node.getExpandIconClass();
  294. }
  295. }
  296. node.forEachChild(updateShowLines);
  297. }
  298. updateShowLines(this);
  299. };
  300. /**
  301. * Sets whether to show root lines.
  302. * @param {boolean} b Whether to show root lines.
  303. */
  304. goog.ui.tree.TreeControl.prototype.setShowRootLines = function(b) {
  305. if (this.showRootLines_ != b) {
  306. this.showRootLines_ = b;
  307. if (this.isInDocument()) {
  308. this.updateLinesAndExpandIcons_();
  309. }
  310. }
  311. };
  312. /**
  313. * @return {boolean} Whether to show root lines.
  314. */
  315. goog.ui.tree.TreeControl.prototype.getShowRootLines = function() {
  316. return this.showRootLines_;
  317. };
  318. /**
  319. * Sets whether to show expand icons.
  320. * @param {boolean} b Whether to show expand icons.
  321. */
  322. goog.ui.tree.TreeControl.prototype.setShowExpandIcons = function(b) {
  323. if (this.showExpandIcons_ != b) {
  324. this.showExpandIcons_ = b;
  325. if (this.isInDocument()) {
  326. this.updateLinesAndExpandIcons_();
  327. }
  328. }
  329. };
  330. /**
  331. * @return {boolean} Whether to show expand icons.
  332. */
  333. goog.ui.tree.TreeControl.prototype.getShowExpandIcons = function() {
  334. return this.showExpandIcons_;
  335. };
  336. /**
  337. * Sets whether to show the root node.
  338. * @param {boolean} b Whether to show the root node.
  339. */
  340. goog.ui.tree.TreeControl.prototype.setShowRootNode = function(b) {
  341. if (this.showRootNode_ != b) {
  342. this.showRootNode_ = b;
  343. if (this.isInDocument()) {
  344. var el = this.getRowElement();
  345. if (el) {
  346. el.className = this.getRowClassName();
  347. }
  348. }
  349. // Ensure that we do not hide the selected item.
  350. if (!b && this.getSelectedItem() == this && this.getFirstChild()) {
  351. this.setSelectedItem(this.getFirstChild());
  352. }
  353. }
  354. };
  355. /**
  356. * @return {boolean} Whether to show the root node.
  357. */
  358. goog.ui.tree.TreeControl.prototype.getShowRootNode = function() {
  359. return this.showRootNode_;
  360. };
  361. /**
  362. * Add roles and states.
  363. * @protected
  364. * @override
  365. */
  366. goog.ui.tree.TreeControl.prototype.initAccessibility = function() {
  367. goog.ui.tree.TreeControl.superClass_.initAccessibility.call(this);
  368. var elt = this.getElement();
  369. goog.asserts.assert(elt, 'The DOM element for the tree cannot be null.');
  370. goog.a11y.aria.setRole(elt, 'tree');
  371. goog.a11y.aria.setState(elt, 'labelledby', this.getLabelElement().id);
  372. };
  373. /** @override */
  374. goog.ui.tree.TreeControl.prototype.enterDocument = function() {
  375. goog.ui.tree.TreeControl.superClass_.enterDocument.call(this);
  376. var el = this.getElement();
  377. el.className = this.getConfig().cssRoot;
  378. el.setAttribute('hideFocus', 'true');
  379. this.attachEvents_();
  380. this.initAccessibility();
  381. };
  382. /** @override */
  383. goog.ui.tree.TreeControl.prototype.exitDocument = function() {
  384. goog.ui.tree.TreeControl.superClass_.exitDocument.call(this);
  385. this.detachEvents_();
  386. };
  387. /**
  388. * Adds the event listeners to the tree.
  389. * @private
  390. */
  391. goog.ui.tree.TreeControl.prototype.attachEvents_ = function() {
  392. var el = this.getElement();
  393. el.tabIndex = 0;
  394. var kh = this.keyHandler_ = new goog.events.KeyHandler(el);
  395. var fh = this.focusHandler_ = new goog.events.FocusHandler(el);
  396. this.getHandler()
  397. .listen(fh, goog.events.FocusHandler.EventType.FOCUSOUT, this.handleBlur_)
  398. .listen(fh, goog.events.FocusHandler.EventType.FOCUSIN, this.handleFocus_)
  399. .listen(kh, goog.events.KeyHandler.EventType.KEY, this.handleKeyEvent)
  400. .listen(el, goog.events.EventType.MOUSEDOWN, this.handleMouseEvent_)
  401. .listen(el, goog.events.EventType.CLICK, this.handleMouseEvent_)
  402. .listen(el, goog.events.EventType.DBLCLICK, this.handleMouseEvent_);
  403. };
  404. /**
  405. * Removes the event listeners from the tree.
  406. * @private
  407. */
  408. goog.ui.tree.TreeControl.prototype.detachEvents_ = function() {
  409. this.keyHandler_.dispose();
  410. this.keyHandler_ = null;
  411. this.focusHandler_.dispose();
  412. this.focusHandler_ = null;
  413. };
  414. /**
  415. * Handles mouse events.
  416. * @param {!goog.events.BrowserEvent} e The browser event.
  417. * @private
  418. */
  419. goog.ui.tree.TreeControl.prototype.handleMouseEvent_ = function(e) {
  420. goog.log.fine(this.logger_, 'Received event ' + e.type);
  421. var node = this.getNodeFromEvent_(e);
  422. if (node) {
  423. switch (e.type) {
  424. case goog.events.EventType.MOUSEDOWN:
  425. node.onMouseDown(e);
  426. break;
  427. case goog.events.EventType.CLICK:
  428. node.onClick_(e);
  429. break;
  430. case goog.events.EventType.DBLCLICK:
  431. node.onDoubleClick_(e);
  432. break;
  433. }
  434. }
  435. };
  436. /**
  437. * Handles key down on the tree.
  438. * @param {!goog.events.BrowserEvent} e The browser event.
  439. * @return {boolean} The handled value.
  440. */
  441. goog.ui.tree.TreeControl.prototype.handleKeyEvent = function(e) {
  442. var handled = false;
  443. // Handle typeahead and navigation keystrokes.
  444. handled = this.typeAhead_.handleNavigation(e) ||
  445. (this.selectedItem_ && this.selectedItem_.onKeyDown(e)) ||
  446. this.typeAhead_.handleTypeAheadChar(e);
  447. if (handled) {
  448. e.preventDefault();
  449. }
  450. return handled;
  451. };
  452. /**
  453. * Finds the containing node given an event.
  454. * @param {!goog.events.BrowserEvent} e The browser event.
  455. * @return {goog.ui.tree.BaseNode} The containing node or null if no node is
  456. * found.
  457. * @private
  458. */
  459. goog.ui.tree.TreeControl.prototype.getNodeFromEvent_ = function(e) {
  460. // find the right node
  461. var node = null;
  462. var target = e.target;
  463. while (target != null) {
  464. var id = target.id;
  465. node = goog.ui.tree.BaseNode.allNodes[id];
  466. if (node) {
  467. return node;
  468. }
  469. if (target == this.getElement()) {
  470. break;
  471. }
  472. target = target.parentNode;
  473. }
  474. return null;
  475. };
  476. /**
  477. * Creates a new tree node using the same config as the root.
  478. * @param {string=} opt_content The content of the node label. Strings are
  479. * treated as plain-text and will be HTML escaped. To set SafeHtml content,
  480. * omit opt_content and call setSafeHtml on the resulting node.
  481. * @return {!goog.ui.tree.TreeNode} The new item.
  482. */
  483. goog.ui.tree.TreeControl.prototype.createNode = function(opt_content) {
  484. return new goog.ui.tree.TreeNode(opt_content || goog.html.SafeHtml.EMPTY,
  485. this.getConfig(), this.getDomHelper());
  486. };
  487. /**
  488. * Allows the caller to notify that the given node has been added or just had
  489. * been updated in the tree.
  490. * @param {goog.ui.tree.BaseNode} node New node being added or existing node
  491. * that just had been updated.
  492. */
  493. goog.ui.tree.TreeControl.prototype.setNode = function(node) {
  494. this.typeAhead_.setNodeInMap(node);
  495. };
  496. /**
  497. * Allows the caller to notify that the given node is being removed from the
  498. * tree.
  499. * @param {goog.ui.tree.BaseNode} node Node being removed.
  500. */
  501. goog.ui.tree.TreeControl.prototype.removeNode = function(node) {
  502. this.typeAhead_.removeNodeFromMap(node);
  503. };
  504. /**
  505. * Clear the typeahead buffer.
  506. */
  507. goog.ui.tree.TreeControl.prototype.clearTypeAhead = function() {
  508. this.typeAhead_.clear();
  509. };
  510. /**
  511. * A default configuration for the tree.
  512. */
  513. goog.ui.tree.TreeControl.defaultConfig = goog.ui.tree.BaseNode.defaultConfig;