examplesComponent.tsx 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import * as React from "react";
  2. import { GlobalState } from "../globalState";
  3. require("../scss/examples.scss");
  4. interface IExamplesComponentProps {
  5. globalState: GlobalState;
  6. }
  7. export class ExamplesComponent extends React.Component<IExamplesComponentProps, { filter: string }> {
  8. private _state = "removed";
  9. private _rootRef: React.RefObject<HTMLDivElement>;
  10. private _scripts: {
  11. title: string;
  12. samples: {
  13. title: string;
  14. doc: string;
  15. icon: string;
  16. PGID: string;
  17. description: string;
  18. }[];
  19. }[];
  20. public constructor(props: IExamplesComponentProps) {
  21. super(props);
  22. this._loadScripts();
  23. this.state = { filter: "" };
  24. this._rootRef = React.createRef();
  25. this.props.globalState.onExamplesDisplayChangedObservable.add(() => {
  26. if (this._state !== "visible") {
  27. this._rootRef.current!.classList.remove("removed");
  28. setTimeout(() => {
  29. this._rootRef.current!.classList.add("visible");
  30. this._state = "visible";
  31. }, 16);
  32. } else {
  33. this._rootRef.current!.classList.remove("visible");
  34. this._state = "";
  35. setTimeout(() => {
  36. this._rootRef.current!.classList.add("removed");
  37. }, 200);
  38. }
  39. });
  40. }
  41. private _loadScripts() {
  42. var xhr = new XMLHttpRequest();
  43. if (this.props.globalState.language === "JS") {
  44. xhr.open("GET", "https://raw.githubusercontent.com/BabylonJS/Documentation/master/examples/list.json", true);
  45. } else {
  46. xhr.open("GET", "https://raw.githubusercontent.com/BabylonJS/Documentation/master/examples/list_ts.json", true);
  47. }
  48. xhr.onreadystatechange = () => {
  49. if (xhr.readyState === 4) {
  50. if (xhr.status === 200) {
  51. this._scripts = JSON.parse(xhr.response)["examples"];
  52. this._scripts.sort((a, b) => {
  53. if (a.title < b.title) {
  54. return -1;
  55. }
  56. return 1;
  57. });
  58. this._scripts.forEach((s) => {
  59. s.samples.sort((a, b) => {
  60. if (a.title < b.title) {
  61. return -1;
  62. }
  63. return 1;
  64. });
  65. });
  66. this.forceUpdate();
  67. }
  68. }
  69. };
  70. xhr.send(null);
  71. }
  72. private _onLoadPG(id: string) {
  73. this.props.globalState.onLoadRequiredObservable.notifyObservers(id);
  74. if (window.innerWidth < this.props.globalState.MobileSizeTrigger) {
  75. this.props.globalState.onExamplesDisplayChangedObservable.notifyObservers();
  76. }
  77. }
  78. public render() {
  79. if (!this._scripts) {
  80. return null;
  81. }
  82. return (
  83. <div id="examples" className={this._state} ref={this._rootRef}>
  84. <div id="examples-header">Examples</div>
  85. <div id="examples-filter">
  86. <input
  87. id="examples-filter-text"
  88. type="text"
  89. placeholder="Filter examples"
  90. value={this.state.filter}
  91. onChange={(evt) => {
  92. this.setState({ filter: evt.target.value });
  93. }}
  94. />
  95. </div>
  96. <div id="examples-list">
  97. {this._scripts.map((s) => {
  98. let active = s.samples.filter((ss) => {
  99. return !this.state.filter || ss.title.toLowerCase().indexOf(this.state.filter.toLowerCase()) !== -1 || ss.description.toLowerCase().indexOf(this.state.filter.toLowerCase()) !== -1;
  100. });
  101. if (active.length === 0) {
  102. return null;
  103. }
  104. return (
  105. <div key={s.title} className="example-category">
  106. <div className="example-category-title">{s.title}</div>
  107. {active.map((ss) => {
  108. return (
  109. <div className="example" key={ss.title} onClick={() => this._onLoadPG(ss.PGID)}>
  110. <img src={ss.icon.replace("icons", "https://doc.babylonjs.com/examples/icons")} />
  111. <div className="example-title">{ss.title}</div>
  112. <div className="example-description">{ss.description}</div>
  113. <a className="example-link" href={ss.doc} target="_blank">
  114. Documentation
  115. </a>
  116. </div>
  117. );
  118. })}
  119. </div>
  120. );
  121. })}
  122. </div>
  123. </div>
  124. );
  125. }
  126. }