-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
132 lines (128 loc) · 3.66 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import angular from 'angular'
import React from 'react'
import { render } from 'react-dom'
import _ from 'lodash'
const templateProvider = (err,uiView)=>{
if(err){
throw err;
}
const tpl= `${uiView}`;
return tpl;
}
let options ={
state:'react',basePath:'/react',templateProvider:templateProvider
};
class Link extends React.Component {
render () {
return React.createElement('a', {
href: options.basePath + this.props.to,
...this.props
})
}
}
let _injector
const browserHistory = {
push (args) {
const state = _injector.get('$state')
const path = _.trimStart(args.pathname + args.search, '/');
state.go(options.state, { path });
}
}
window.ReactRouter = {
Link,
browserHistory
}
export const provider= (opts,$stateProvider) =>{
options=routeFactory.configure(opts);
const {state , basePath ,routeResolver, templateProvider } = options;
$stateProvider.state(state, {
url: basePath+'/:path',
params : {path : {type : "string",raw : true}},
templateProvider: function ($timeout, $stateParams) {
const statePath = _.trimStart($stateParams.path, '/');
return routeResolver(statePath).then((routes)=>{
routeFactory.registerRoute(routes.path,routes);
return new Promise((resolve,reject)=>{
let router=`<react-router name="${routes.path}" path="${statePath}"></react-router>`;
resolve(templateProvider(null,router));
});
},(err)=>{
return new Promise((resolve,reject)=>{
resolve(templateProvider(err));
});
});
}
})
}
const routeFactory = (() => {
const routes = {}
return {
configure: (opts) =>{
return Object.assign({},options,opts);
},
getRoute: (name) => {
if (routes[name]) {
return routes[name].promise
}else {
routes[name] = {}
routes[name].promise = new Promise((resolve) => {
routes[name].resolver = resolve
})
}
return routes[name].promise
},
registerRoute: (name, component) => {
if (routes[name]) {
return routes[name].resolver(component)
}else {
routes[name] = {}
routes[name].promise = new Promise((resolve) => {
routes[name].resolver = resolve
resolve(component)
})
}
return routes[name].promise
}
}
})()
const resolveRoute = (routePath, routes, query ) =>{
if(routePath.length==0){
return null;
}
const path = routePath.shift();
const { component, childRoutes, indexRoute } = routes;
let children = null;
if(routePath.length===0 && indexRoute){
children=React.createElement(indexRoute.component, null, null);
}else{
const matchRoutes=(childRoutes||[]).filter((route)=>route.path==routePath[0]);
children =resolveRoute(routePath,matchRoutes[0], query);
}
return React.createElement(component, null, children)
}
const reactRouter = function ($injector) {
_injector = $injector
return {
restrict: 'E',
replace: true,
link: function (scope, elem, attrs) {
routeFactory.getRoute(attrs.name).then((routes) => {
const tmp =attrs.path.split('?');
const query=tmp[1]
const path = tmp[0].split('/');
const tree = resolveRoute(path,routes, query);
render(tree, elem[0])
})
scope.$on('$destroy', function () {
if (!attrs.onScopeDestroy) {
ReactDOM.unmountComponentAtNode(elem[0])
} else {
scope.$eval(attrs.onScopeDestroy, {
unmountComponent: ReactDOM.unmountComponentAtNode.bind(this, elem[0])
})
}
})
}
}
};
angular.module('ng-react-router', []).directive('reactRouter', ['$injector', reactRouter])