Skip to content

Commit

Permalink
examples
Browse files Browse the repository at this point in the history
  • Loading branch information
deltakosh committed Jul 16, 2020
1 parent 5fc4c57 commit f582df7
Show file tree
Hide file tree
Showing 13 changed files with 334 additions and 8 deletions.
1 change: 1 addition & 0 deletions Playground/imgs/examples.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions Playground/src/components/commandBarComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,14 @@ export class CommandBarComponent extends React.Component<ICommandBarComponentPro
this.props.globalState.onInspectorRequiredObservable.notifyObservers();
}

onExamples() {
this.props.globalState.onExamplesDisplayChangedObservable.notifyObservers();
}

public render() {
return (
<div className={"commands " + (this.props.globalState.language === "JS" ? "background-js" : "background-ts")}>
<div className="commands-left">
<CommandButtonComponent globalState={this.props.globalState} tooltip="Run" icon="play" shortcut="Alt+Enter" isActive={true} onClick={()=> this.onPlay()}/>
<CommandButtonComponent globalState={this.props.globalState} tooltip="Save" icon="save" shortcut="Ctrl+S" isActive={false} onClick={()=> this.onSave()}/>
<CommandButtonComponent globalState={this.props.globalState} tooltip="Inspector" icon="inspector" isActive={false} onClick={()=> this.onInspector()}/>
Expand Down Expand Up @@ -118,6 +123,10 @@ export class CommandBarComponent extends React.Component<ICommandBarComponentPro
onClick: () => {this.props.globalState.onDisplayMetadataObservable.notifyObservers(true)}
}
]}/>
</div>
<div className="commands-right">
<CommandButtonComponent globalState={this.props.globalState} tooltip="Examples" icon="examples" onClick={()=> this.onExamples()} isActive={false}/>
</div>
</div>
);
}
Expand Down
6 changes: 6 additions & 0 deletions Playground/src/components/commandDropdownComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ export class CommandDropdownComponent extends React.Component<ICommandDropdownCo
}}
checked={Utilities.ReadBoolFromStore(m.storeKey!, (m.defaultValue as boolean) || false)}/>
}
{
m.subItems &&
<div className="command-dropdown-arrow">
{">"}
</div>
}
{
m.subItems &&
<div className={"sub-items " + (this.props.globalState.language === "JS" ? "background-js" : "background-ts")}>
Expand Down
136 changes: 136 additions & 0 deletions Playground/src/components/examplesComponent.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
import * as React from "react";
import { GlobalState } from '../globalState';

require("../scss/examples.scss");

interface IExamplesComponentProps {
globalState: GlobalState;
}

export class ExamplesComponent extends React.Component<IExamplesComponentProps, {filter: string}> {
private _state = "";
private _rootRef: React.RefObject<HTMLDivElement>;
private _scripts: {
title: string;
samples: {
title: string;
doc: string;
icon: string;
PGID: string;
description: string;
}[];
}[];

public constructor(props: IExamplesComponentProps) {
super(props);
this._loadScripts();

this.state = {filter: ""};
this._rootRef = React.createRef();

this.props.globalState.onExamplesDisplayChangedObservable.add(() => {
if (this._state === "") {
this._rootRef.current!.classList.add("visible");
this._state = "visible";
} else {
this._rootRef.current!.classList.remove("visible");
this._state = "";
}
});
}

private _loadScripts() {
var xhr = new XMLHttpRequest();

if (this.props.globalState.language === "JS") {
xhr.open('GET', 'https://raw.githubusercontent.com/BabylonJS/Documentation/master/examples/list.json', true);
} else {
xhr.open('GET', 'https://raw.githubusercontent.com/BabylonJS/Documentation/master/examples/list_ts.json', true);
}

xhr.onreadystatechange = () => {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
this._scripts = JSON.parse(xhr.response)["examples"];

this._scripts.sort((a, b) => {
if (a.title < b.title) {
return -1;
}
return 1;
});

this._scripts.forEach(s => {
s.samples.sort((a, b) => {
if (a.title < b.title) {
return -1;
}
return 1;
});
});

this.forceUpdate();
}
}
}

xhr.send(null);
}


private _onLoadPG(id: string) {
this.props.globalState.onLoadRequiredObservable.notifyObservers(id);
}

public render() {
if (!this._scripts) {
return null;
}

return (
<div id="examples" className={this._state} ref={this._rootRef}>
<div id="examples-header">Examples</div>
<div id="examples-filter">
<input type="text" placeholder="Filter examples" value={this.state.filter} onChange={evt => {
this.setState({filter: evt.target.value});
}}/>
</div>
<div id="examples-list">
{
this._scripts.map(s => {
let active = s.samples.filter(ss => {
return !this.state.filter
|| ss.title.toLowerCase().indexOf(this.state.filter.toLowerCase()) !== -1
|| ss.description.toLowerCase().indexOf(this.state.filter.toLowerCase()) !== -1
});

if (active.length === 0) {
return null;
}

return(
<div key={s.title} className="example-category">
<div className="example-category-title">
{s.title}
</div>
{
active.map(ss => {
return (
<div className="example" key={ss.title} onClick={() => this._onLoadPG(ss.PGID)}>
<img src={ss.icon.replace("icons", "https://doc.babylonjs.com/examples/icons")}/>
<div className="example-title">{ss.title}</div>
<div className="example-description">{ss.description}</div>
<a className="example-link" href={ss.doc} target="_blank">Documentation</a>
</div>
)
})
}
</div>
)
})
}
</div>
</div>
)
}
}
6 changes: 6 additions & 0 deletions Playground/src/components/hamburgerMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ export class HamburgerMenuComponent extends React.Component<IHamburgerMenuCompon
this.setState({isExpanded: false});
}

onExamples() {
this.props.globalState.onExamplesDisplayChangedObservable.notifyObservers();
this.setState({isExpanded: false});
}

switch() {
this.setState({isExpanded: !this.state.isExpanded});
}
Expand All @@ -81,6 +86,7 @@ export class HamburgerMenuComponent extends React.Component<IHamburgerMenuCompon
<CommandButtonComponent globalState={this.props.globalState} tooltip="Clear code" icon="clear" isActive={false} onClick={()=> this.onClear()}/>
<CommandButtonComponent globalState={this.props.globalState} tooltip="Format code" icon="options" isActive={false} onClick={()=> this.onFormatCode()}/>
<CommandButtonComponent globalState={this.props.globalState} tooltip="Metadata" icon="options" isActive={false} onClick={()=> this.onMetadata()}/>
<CommandButtonComponent globalState={this.props.globalState} tooltip="Examples" icon="examples" onClick={()=> this.onExamples()} isActive={false}/>
</div>
</>
);
Expand Down
4 changes: 3 additions & 1 deletion Playground/src/globalState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ export class GlobalState {
public onSavedObservable = new Observable<void>();
public onNewRequiredObservable = new Observable<void>();
public onClearRequiredObservable = new Observable<void>();
public onSaveRequiredObservable = new Observable<void>();
public onSaveRequiredObservable = new Observable<void>();
public onLoadRequiredObservable = new Observable<string>();
public onErrorObservable = new Observable<Nullable<CompilationError>>();
public onMobileDefaultModeChangedObservable = new Observable<void>();
public onDisplayWaitRingObservable = new Observable<boolean>();
Expand All @@ -48,6 +49,7 @@ export class GlobalState {
public onFontSizeChangedObservable = new Observable<void>();
public onLanguageChangedObservable = new Observable<void>();
public onNavigateRequiredObservable = new Observable<{lineNumber: number, column: number}>();
public onExamplesDisplayChangedObservable = new Observable<void>();

public loadingCodeInProgress = false;
public onCodeLoaded = new Observable<string>();
Expand Down
2 changes: 2 additions & 0 deletions Playground/src/playground.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { HamburgerMenuComponent } from './components/hamburgerMenu';
import { Utilities } from './tools/utilities';
import { ShortcutManager } from './tools/shortcutManager';
import { ErrorDisplayComponent } from './components/errorDisplayComponent';
import { ExamplesComponent } from './components/examplesComponent';

require("./scss/main.scss");
const Split = require('split.js').default;
Expand Down Expand Up @@ -117,6 +118,7 @@ export class Playground extends React.Component<IPlaygroundProps, {errorMessage:
window.innerWidth < 1024 &&
<HamburgerMenuComponent globalState={this._globalState}/>
}
<ExamplesComponent globalState={this._globalState}/>
<FooterComponent globalState={this._globalState}/>
<ErrorDisplayComponent globalState={this._globalState}/>
<WaitRingComponent globalState={this._globalState}/>
Expand Down
27 changes: 25 additions & 2 deletions Playground/src/scss/commandBar.scss
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,16 @@
.commands {
grid-row: 1;
grid-column: 3;
display: flex;

.commands-left {
float: left;
display: flex;
}

.commands-right {
float: right;
display: flex;
}

&.background-ts {
.command-button, .command-dropdown {
Expand Down Expand Up @@ -48,6 +57,12 @@
transform-origin: center;
transform: scale(0.95);
}

img {
&.active {
filter: invert(100%);
}
}
}

.command-dropdown-root {
Expand Down Expand Up @@ -123,7 +138,6 @@
grid-template-rows: 100%;
position: relative;


&:hover {
.sub-items {
display: block;
Expand All @@ -140,6 +154,15 @@
grid-row: 1;
}

.command-dropdown-arrow {
grid-column: 2;
grid-row: 1;
font-size: 28px;
font-weight: bold;
padding-bottom: 10px;
padding-left: 4px;
}

.sub-items {
position: absolute;
left: 200px;
Expand Down
Loading

0 comments on commit f582df7

Please sign in to comment.