Skip to content
This repository was archived by the owner on Aug 18, 2020. It is now read-only.

Add local and global hooks for check current activity #10

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 24 additions & 6 deletions src/utils/create-router.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ const getPathVariables = (sections, matches) => {
return pathVariables
}

const getContent = (options, path, target, pathVariables) => {
let { Component, props } = options[path]
const getContent = (options, path, target, pathVariables, onGlobalActivate) => {
let { Component, props, onActivate } = options[path]
if (!Component) Component = options[path]
let extraData = {
data: {
Expand All @@ -39,14 +39,32 @@ const getContent = (options, path, target, pathVariables) => {
props.data['path'] = pathVariables
extraData = {}
}
return new Component({
let component = new Component({
target: target,
...props,
...extraData
})

if (onActivate != null) {
onActivate({
path,
props,
component
})
}

if (onGlobalActivate != null) {
onGlobalActivate({
path,
props,
component
})
}

return component
}

const createRouter = options => {
const createRouter = (options, onGlobalActivate) => {
let _target // target DOM
let _unlisten // history listener
let _content // route instance
Expand All @@ -62,14 +80,14 @@ const createRouter = options => {
const matches = location.pathname.match(new RegExp(`^${regexPath}$`))
if (matches !== null) {
const pathVariables = getPathVariables(sections, matches)
_content = getContent(options, path, _target, pathVariables)
_content = getContent(options, path, _target, pathVariables, onGlobalActivate)
found = true
break
}
}
}
if (!found && options[DEFAULT_ROUTE]) {
_content = getContent(options, DEFAULT_ROUTE, _target, {})
_content = getContent(options, DEFAULT_ROUTE, _target, {}, onGlobalActivate)
}
}

Expand Down