refactor(router): clean global api

This commit is contained in:
qingwei.li 2017-05-29 19:55:28 +08:00 • committed by cinwell.li
commit 8741c746df
12 changed files with 46 additions and 126 deletions

View file

@ -6,13 +6,13 @@ import { scrollIntoView } from './scroll'
export function eventMixin (proto) {
proto.$resetEvents = function () {
scrollIntoView(this.route.query.id)
sidebar.getAndActive('nav')
sidebar.getAndActive(this.router, 'nav')
}
}
export function initEvent (vm) {
// Bind toggle button
sidebar.btn('button.sidebar-toggle')
sidebar.btn('button.sidebar-toggle', vm.router)
// Bind sticky effect
if (vm.config.coverpage) {
!isMobile && on('scroll', sidebar.sticky)

View file

@ -1,6 +1,5 @@
import { isMobile } from '../util/env'
import * as dom from '../util/dom'
import { parse } from '../router/hash'
const nav = {}
let hoverOver = false
@ -53,7 +52,7 @@ function highlight () {
}
}
export function scrollActiveSidebar () {
export function scrollActiveSidebar (router) {
if (isMobile) return
const sidebar = dom.getNode('.sidebar')
@ -66,7 +65,7 @@ export function scrollActiveSidebar () {
let href = a.getAttribute('href')
if (href !== '/') {
href = parse(href).query.id
href = router.parse(href).query.id
}
nav[decodeURIComponent(href)] = li

View file

@ -1,12 +1,11 @@
import { isMobile } from '../util/env'
import * as dom from '../util/dom'
import { getHash } from '../router/hash'
const title = dom.$.title
/**
* Toggle button
*/
export function btn (el) {
export function btn (el, router) {
const toggle = _ => dom.body.classList.toggle('close')
el = dom.getNode(el)
@ -21,7 +20,7 @@ export function btn (el) {
dom.body.classList.contains('close') && toggle()
)
dom.on(sidebar, 'click', _ =>
setTimeout((_ => getAndActive(sidebar, true, true), 0))
setTimeout((_ => getAndActive(router, sidebar, true, true), 0))
)
}
@ -39,16 +38,17 @@ export function sticky () {
/**
* Get and active link
* @param {object} router
* @param {string|element} el
* @param {Boolean} isParent acitve parent
* @param {Boolean} autoTitle auto set title
* @return {element}
*/
export function getAndActive (el, isParent, autoTitle) {
export function getAndActive (router, el, isParent, autoTitle) {
el = dom.getNode(el)
const links = dom.findAll(el, 'a')
const hash = '#' + getHash()
const hash = router.toURL(router.getCurrentPath())
let target
links