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