Skip to content

Commit

Permalink
Work-in-progress on routing, renders mock-api content at
Browse files Browse the repository at this point in the history
- /static/#/browse/src
- /static/#/browse/src/lib.rs
  • Loading branch information
nkanderson committed Sep 26, 2017
1 parent f63767e commit 1561349
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 4 deletions.
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@
},
"dependencies": {
"@types/react": "^16.0.5",
"@types/react-dom": "^15.5.4",
"@types/react-redux": "^5.0.8",
"@types/react-router-dom": "^4.0.7",
"@types/redux": "^3.6.0",
"css-loader": "^0.28.7",
"eslint-plugin-jquery": "^1.2.1",
"react": "^15.4.2",
"react-dom": "^15.4.2",
"react-redux": "^5.0.5",
"react-router-dom": "^4.2.2",
"redux": "^3.7.0",
"redux-thunk": "^2.2.0",
"style-loader": "^0.18.2"
Expand Down
22 changes: 19 additions & 3 deletions static/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@ import { DirView } from './dirView';
import { SourceViewController } from './srcView';
import { Summary } from './summary';

import {
HashRouter,
Route,
Redirect
} from 'react-router-dom';

import {
Home,
Search,
Source
} from './pages';

// TODOs in build

class RustwApp extends React.Component {
Expand Down Expand Up @@ -116,9 +128,13 @@ let store = createStore(rustwReducer, applyMiddleware(thunk));

export function renderApp() {
ReactDOM.render(
<Provider store={store}>
<AppController />
</Provider>,
<HashRouter>
<div className="main">
<Route exact path="/" component={Home} />
<Route path="/search" component={Search} />
<Route path="/browse(/\w+\.?\w+)*" component={Source} />
</div>
</HashRouter>,
document.getElementById('container')
);
}
96 changes: 96 additions & 0 deletions static/codeBrowser.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// Copyright 2017 The Rustw Project Developers.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

import * as React from 'react';

import * as utils from './utils';
import { DirView } from './dirView';
import { SourceView } from './srcView';

declare const $: any;

interface Props {
path: string,
}

// TODO: consider switching path to default prop with value of 'src'
// interface DefaultProps {
// path: string,
// }

interface State {
isFile: boolean,
lines?: Array<string>,
files?: any,
highlight?: any,
lineStart?: any,
getSource?: (path :string) => any,
name: any,
}

export class CodeBrowser extends React.Component<Props, State> {
constructor(props: Props) {
super(props);
this.state = {
isFile: false,
lines: [],
files: [],
highlight: [],
lineStart: [],
getSource: (path) => {},
name: this.props.path,
}
}

componentWillMount() {
const path = this.props.path;
$.ajax({
url: 'http://localhost:3000/src' + path, // json-server mock API base url
type: 'GET',
dataType: 'JSON',
cache: false
})
.done((json: any) => {
if (json.Directory) {
this.setState({
isFile: false,
files: json.Directory.files,
lines: [],
highlight: [],
lineStart: [],
name: this.props.path,
});
} else if (json.Source) {
this.setState({
isFile: true,
lines: json.Source.lines,
highlight: [],
lineStart: [],
});
} else {
console.log("Unexpected source data.")
console.log(json);
}
})
.fail(function (xhr: any, status: any, errorThrown: any) {
// console.log(errStr);
console.log("error: " + errorThrown + "; status: " + status);

});
}

render() {
const path = this.props.path.split('/');

if (this.state.isFile) {
return <SourceView path={path} lines={this.state.lines} highlight={this.state.highlight} scrollTo={this.state.lineStart} />
} else {
return <DirView file={this.state.name} files={this.state.files} getSource={this.state.getSource} />;
}
}
}
39 changes: 39 additions & 0 deletions static/pages.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright 2017 The Rustw Project Developers.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

import * as React from 'react';

import { CodeBrowser } from './codeBrowser';

const PageTemplate = ({children}) =>
<div id="div_app">
<div id="div_main">
{children}
</div>
</div>

export const Home = () =>
<PageTemplate>
<section className="home">
[Home]
</section>
</PageTemplate>

export const Search = () =>
<PageTemplate>
<section className="search">
[Search]
</section>
</PageTemplate>

export const Source = ({match}) =>
<PageTemplate>
<section className="source">
<CodeBrowser path={match.params[0]} />
</section>
</PageTemplate>
2 changes: 1 addition & 1 deletion static/srcView.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ function view_in_vcs(target) {
window.open(CONFIG.vcs_link.replace("$file", file_name).replace("$line", line_number), '_blank');
}

class SourceView extends React.Component {
export class SourceView extends React.Component {
constructor(props) {
super(props);
this.state = { refMenu: null };
Expand Down

0 comments on commit 1561349

Please sign in to comment.