tree-header.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. "use strict";
  6. // Make this available to both AMD and CJS environments
  7. define(function (require, exports, module) {
  8. // ReactJS
  9. const React = require("devtools/client/shared/vendor/react");
  10. // Shortcuts
  11. const { thead, tr, td, div } = React.DOM;
  12. const PropTypes = React.PropTypes;
  13. /**
  14. * This component is responsible for rendering tree header.
  15. * It's based on <thead> element.
  16. */
  17. let TreeHeader = React.createClass({
  18. displayName: "TreeHeader",
  19. // See also TreeView component for detailed info about properties.
  20. propTypes: {
  21. // Custom tree decorator
  22. decorator: PropTypes.object,
  23. // True if the header should be visible
  24. header: PropTypes.bool,
  25. // Array with column definition
  26. columns: PropTypes.array
  27. },
  28. getDefaultProps: function () {
  29. return {
  30. columns: [{
  31. id: "default"
  32. }]
  33. };
  34. },
  35. getHeaderClass: function (colId) {
  36. let decorator = this.props.decorator;
  37. if (!decorator || !decorator.getHeaderClass) {
  38. return [];
  39. }
  40. // Decorator can return a simple string or array of strings.
  41. let classNames = decorator.getHeaderClass(colId);
  42. if (!classNames) {
  43. return [];
  44. }
  45. if (typeof classNames == "string") {
  46. classNames = [classNames];
  47. }
  48. return classNames;
  49. },
  50. render: function () {
  51. let cells = [];
  52. let visible = this.props.header;
  53. // Render the rest of the columns (if any)
  54. this.props.columns.forEach(col => {
  55. let cellStyle = {
  56. "width": col.width ? col.width : "",
  57. };
  58. let classNames = [];
  59. if (visible) {
  60. classNames = this.getHeaderClass(col.id);
  61. classNames.push("treeHeaderCell");
  62. }
  63. cells.push(
  64. td({
  65. className: classNames.join(" "),
  66. style: cellStyle,
  67. key: col.id},
  68. div({ className: visible ? "treeHeaderCellBox" : "" },
  69. visible ? col.title : ""
  70. )
  71. )
  72. );
  73. });
  74. return (
  75. thead({}, tr({ className: visible ? "treeHeaderRow" : "" },
  76. cells
  77. ))
  78. );
  79. }
  80. });
  81. // Exports from this module
  82. module.exports = TreeHeader;
  83. });