Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
andreypopp committed Jan 30, 2014
0 parents commit 9237328
Show file tree
Hide file tree
Showing 8 changed files with 251 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules/
specs/client.bundle.js
19 changes: 19 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"undef": true,
"unused": "vars",
"asi": true,
"expr": true,
"globalstrict": true,
"globals": {
"window": false,
"Buffer": false,
"require": false,
"process": false,
"module": false,
"exports": false,
"console": false,
"document": false,
"setTimeout": false,
"__callback": false
}
}
20 changes: 20 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
The MIT License (MIT)

Copyright (c) 2013 Andrey Popp

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
37 changes: 37 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
BIN = ./node_modules/.bin
REPO = $(shell cat .git/config | grep url | xargs echo | sed -E 's/^url = //g')
REPONAME = $(shell echo $(REPO) | sed -E 's_.+:([a-zA-Z0-9_\-]+)/([a-zA-Z0-9_\-]+)\.git_\1/\2_')

install link:
@npm $@

lint:
@$(BIN)/jshint *.js

test:
@$(BIN)/mocha -b -R spec specs/*.js

release-patch: test lint
@$(call release,patch)

release-minor: test lint
@$(call release,minor)

release-major: test lint
@$(call release,major)

publish:
git push --tags origin HEAD:master
npm publish

define release
VERSION=`node -pe "require('./package.json').version"` && \
NEXT_VERSION=`node -pe "require('semver').inc(\"$$VERSION\", '$(1)')"` && \
node -e "\
var j = require('./package.json');\
j.version = \"$$NEXT_VERSION\";\
var s = JSON.stringify(j, null, 2);\
require('fs').writeFileSync('./package.json', s);" && \
git commit -m "release $$NEXT_VERSION" -- package.json && \
git tag "$$NEXT_VERSION" -m "release $$NEXT_VERSION"
endef
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# react-router

Declarative router component for [React][react]:

module React from 'react';
import {Pages, Page} from 'react-router';

module.exports = React.createClass({
render: function() {
return (
<html>
<head>
<script src="/bundle.js"></script>
</head>
<Pages location={this.props.location}>
<Page path="/">
<div>Main page</div>
</Page>
<Page path="/users/:username">
<div>Some user page</div>
</Page>
<NotFound>
Sorry, 404!
</NotFound>
</Pages>
</html>
);
}
});

[react]: https://facebook.github.io/react
87 changes: 87 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
"use strict";
var React = require('react');
var invariant = require('react/lib/invariant');
var pattern = require('url-pattern');

function createRouter(component) {
return React.createClass({
getInitialState: function() {
return {
location: this.props.location || window.location
}
},

componentDidMount: function() {
window.addEventListener('popstate', this.onPopState);
},

componentWillUnmount: function() {
window.removeEventListener('popstate', this.onPopState);
},

onPopState: function(e) {
e.preventDefault();
this.setState({location: window.location});
},

render: function() {
var match, page, notFound;
var len, i;

for (i = 0, len = this.props.children.length; i < len; i++) {
var current = this.props.children[i];

if (process.env.NODE_ENV !== "production") {
invariant(
current.children !== undefined && current.path !== undefined,
"Router should contain either Route or NotFound components as children")
}

if (current.path) {
current.pattern = current.pattern || pattern(current.path);
if (!page) {
match = current.pattern.match(this.state.location.pathname);
if (match) {
page = current;
}
}
}
if (!notFound && current.path === null) {
notFound = current;
}
}

var children = page ? page.children.slice(0) :
notFound ? notFound.children.slice(0) :
[];

for (i = 0, len = children.length; i < len; i++) {
children[i].props.route = match;
}

children.unshift(this.props);
return component.apply(component, children);
}
});
}

function Route(props) {
var children = Array.prototype.slice.call(arguments, 1);
return {path: props.path, children: children};
}

function NotFound(_props) {
var children = Array.prototype.slice.call(arguments, 1);
return {path: null, children: children};
}

module.exports = {

Pages: createRouter(React.DOM.body),
Page: Route,

Locations: createRouter(React.DOM.div),
Location: Route,

NotFound: NotFound
}
35 changes: 35 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"name": "react-router",
"version": "0.0.0",
"description": "Declarative router component for React",
"main": "index.js",
"dependencies": {
"url-pattern": "~0.2.2"
},
"devDependencies": {
"react": "~0.8.0",
"semver": "~2.2.1",
"mocha": "~1.17.1",
"jshint": "~2.4.3"
},
"scripts": {
"test": "make test"
},
"repository": {
"type": "git",
"url": "git://github.com/andreypopp/react-router"
},
"keywords": [
"react",
"react-component",
"router",
"history",
"pushState"
],
"author": "Andrey Popp <[email protected]>",
"license": "MIT",
"bugs": {
"url": "https://github.com/andreypopp/react-router/issues"
},
"homepage": "https://github.com/andreypopp/react-router"
}
20 changes: 20 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
require('node-jsx').install();

var React = require('react');
var App = require('./index');

var location = {
"origin": "https://github.com",
"hash": "",
"search": "",
"pathname": "/users/Andrey",
"port": "",
"hostname": "github.com",
"host": "github.com",
"protocol": "https:",
"href": "https://github.com/"
}

React.renderComponentToString(App({location: location}), function(markup) {
console.log(markup);
});

0 comments on commit 9237328

Please sign in to comment.