examplesComponent.tsx 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 = "";
  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 === "") {
  27. this._rootRef.current!.classList.add("visible");
  28. this._state = "visible";
  29. } else {
  30. this._rootRef.current!.classList.remove("visible");
  31. this._state = "";
  32. }
  33. });
  34. }
  35. private _loadScripts() {
  36. var xhr = new XMLHttpRequest();
  37. if (this.props.globalState.language === "JS") {
  38. xhr.open('GET', 'https://raw.githubusercontent.com/BabylonJS/Documentation/master/examples/list.json', true);
  39. } else {
  40. xhr.open('GET', 'https://raw.githubusercontent.com/BabylonJS/Documentation/master/examples/list_ts.json', true);
  41. }
  42. xhr.onreadystatechange = () => {
  43. if (xhr.readyState === 4) {
  44. if (xhr.status === 200) {
  45. this._scripts = JSON.parse(xhr.response)["examples"];
  46. this._scripts.sort((a, b) => {
  47. if (a.title < b.title) {
  48. return -1;
  49. }
  50. return 1;
  51. });
  52. this._scripts.forEach(s => {
  53. s.samples.sort((a, b) => {
  54. if (a.title < b.title) {
  55. return -1;
  56. }
  57. return 1;
  58. });
  59. });
  60. this.forceUpdate();
  61. }
  62. }
  63. }
  64. xhr.send(null);
  65. }
  66. private _onLoadPG(id: string) {
  67. this.props.globalState.onLoadRequiredObservable.notifyObservers(id);
  68. }
  69. public render() {
  70. if (!this._scripts) {
  71. return null;
  72. }
  73. return (
  74. <div id="examples" className={this._state} ref={this._rootRef}>
  75. <div id="examples-header">Examples</div>
  76. <div id="examples-filter">
  77. <input id="examples-filter-text" type="text" placeholder="Filter examples" value={this.state.filter} onChange={evt => {
  78. this.setState({filter: evt.target.value});
  79. }}/>
  80. </div>
  81. <div id="examples-list">
  82. {
  83. this._scripts.map(s => {
  84. let active = s.samples.filter(ss => {
  85. return !this.state.filter
  86. || ss.title.toLowerCase().indexOf(this.state.filter.toLowerCase()) !== -1
  87. || ss.description.toLowerCase().indexOf(this.state.filter.toLowerCase()) !== -1
  88. });
  89. if (active.length === 0) {
  90. return null;
  91. }
  92. return(
  93. <div key={s.title} className="example-category">
  94. <div className="example-category-title">
  95. {s.title}
  96. </div>
  97. {
  98. active.map(ss => {
  99. return (
  100. <div className="example" key={ss.title} onClick={() => this._onLoadPG(ss.PGID)}>
  101. <img src={ss.icon.replace("icons", "https://doc.babylonjs.com/examples/icons")}/>
  102. <div className="example-title">{ss.title}</div>
  103. <div className="example-description">{ss.description}</div>
  104. <a className="example-link" href={ss.doc} target="_blank">Documentation</a>
  105. </div>
  106. )
  107. })
  108. }
  109. </div>
  110. )
  111. })
  112. }
  113. </div>
  114. </div>
  115. )
  116. }
  117. }