Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexxNB committed Apr 16, 2020
1 parent c9a025a commit d192b74
Show file tree
Hide file tree
Showing 26 changed files with 2,498 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
dist
cmp/aors.js
37 changes: 37 additions & 0 deletions cmp/Route copy.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<script context="module">
let count = 1;
</script>
<script>
import {getContext,setContext,onMount} from 'svelte';
import {routes,current} from './../dist/aors_lib';
const routeId = count++;
let exact = true;
let subroutes = [];
export let path = '/';
//export let fallback = false;
const parentRoute = getContext('ROUTE:data');
let base = path==='/' ? '' : path;
if(parentRoute){
base = parentRoute.base+base;
onMount(_ => parentRoute.mount(routeId));
}
setContext('ROUTE:data',{base,mount: id => {
subroutes.push(id)
subroutes = subroutes;
console.log('abc',subroutes);
return _ => subroutes = subroutes.filter(e=>e!==id);
}});
$: console.log(base,subroutes);
// $: routes.register(base,routeId,exact,fallback);
</script>

{#if $current[routeId]}
<slot></slot>
{/if}
40 changes: 40 additions & 0 deletions cmp/Route.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<script>
import {current_component} from 'svelte/internal';
import {getContext,setContext,afterUpdate} from 'svelte';
import {url,getPathData,formatPath} from './../dist/aors_lib';
export let path = '/*';
export let fallback = false;
let route = null;
let show_content = false;
let fallback_cb = null;
let childs = [];
let exact = fallback || !path.endsWith('/*');
path = formatPath(path);
const ctx = getContext('ROUTER:context');
if(ctx) path = ctx.base+path;
if(ctx && fallback) ctx.regFB( _ => show_content=true );
const show_fallback = _ => fallback_cb ? fallback_cb() : ctx ? ctx.showFB() : null;
setContext('ROUTER:context',{
base: path,
nochilds: exact,
child: (show,path) => show ? childs.push(path) : childs=childs.filter( e => e!==path ),
regFB: func => fallback_cb = func,
showFB: show_fallback
});
$: route = getPathData(path,$url.path);
$: show_content = fallback ? false : !!route && ( (exact && route.exact) || !exact );
$: if(ctx && !fallback) ctx.child(show_content,path);
afterUpdate( _ => (route && !route.exact && !exact && childs.length === 0) ? show_fallback() : null);
</script>

{#if show_content}
<slot></slot>
{/if}
13 changes: 13 additions & 0 deletions cmp/Router.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<script>
import {getContext,setContext,onMount} from 'svelte';
import {url,formatPath} from './../dist/aors_lib';
export let base='/';
let fallback = false;
setContext('ROUTER:base',formatPath(base));
setContext('ROUTER:fallback',cb => fallback ? console.error("Fallback already exist") : fallback=cb);
</script>

<slot></slot>
3 changes: 3 additions & 0 deletions cmp/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export {default as Router} from './Router.svelte';
export {default as Route} from './Route.svelte';
export * from './../dist/aors_lib';
Loading

0 comments on commit d192b74

Please sign in to comment.