Skip to content

Commit

Permalink
feat(search): Localization for search placeholder, close #80
Browse files Browse the repository at this point in the history
  • Loading branch information
QingWei-Li committed Feb 19, 2017
1 parent 079bd00 commit 2351c3e
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 6 deletions.
4 changes: 4 additions & 0 deletions docs/plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ window.$docsify = {
// ...
})

hook.mounted(function() {
// Called after initial completion. Only trigger once, no arguments.
})

hook.ready(function() {
// Called after initial completion, no arguments.
})
Expand Down
4 changes: 4 additions & 0 deletions docs/zh-cn/plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ window.$docsify = {
// ...
})

hook.mounted(function() {
// 初始化完成后调用 ,只调用一次,没有参数。
})

hook.ready(function() {
// 初始化并第一次加完成数据后调用,没有参数。
})
Expand Down
1 change: 1 addition & 0 deletions src/core/init/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export function initMixin (proto) {
initEvent(vm) // Bind events
initRoute(vm) // Add hashchange eventListener
initFetch(vm) // Fetch data
callHook(vm, 'mounted')
}
}

Expand Down
9 changes: 8 additions & 1 deletion src/core/init/lifecycle.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import { noop } from '../util/core'

export function initLifecycle (vm) {
const hooks = ['init', 'beforeEach', 'afterEach', 'doneEach', 'ready']
const hooks = [
'init',
'mounted',
'beforeEach',
'afterEach',
'doneEach',
'ready'
]

vm._hooks = {}
vm._lifecycle = {}
Expand Down
20 changes: 18 additions & 2 deletions src/plugins/search/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ function style () {

function tpl (opts) {
const html =
`<input type="search" placeholder="${opts.placeholder}" />` +
`<input type="search" />` +
'<div class="results-panel"></div>' +
'</div>'
const el = dom.create('div', html)
Expand Down Expand Up @@ -108,9 +108,25 @@ function bindEvents () {
})
}

export default function (opts) {
function updatePlaceholder (text, path) {
const $input = dom.getNode('.search input[type="search"]')

if (typeof text === 'string') {
$input.placeholder = text
} else {
const match = Object.keys(text).find(key => path.indexOf(key) > -1)
$input.placeholder = text[match]
}
}

export function init (opts) {
dom = Docsify.dom
style()
tpl(opts)
bindEvents()
}

export function update (opts, vm) {
updatePlaceholder(opts.placeholder, vm.route.path)
}

9 changes: 6 additions & 3 deletions src/plugins/search/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import initComponet from './component'
import { init as initComponet, update as updateComponent } from './component'
import { init as initSearch } from './search'

const CONFIG = {
Expand All @@ -21,11 +21,14 @@ const install = function (hook, vm) {

const isAuto = CONFIG.paths === 'auto'

hook.ready(_ => {
hook.mounted(_ => {
initComponet(CONFIG)
isAuto && initSearch(CONFIG, vm)
})
!isAuto && hook.doneEach(_ => initSearch(CONFIG, vm))
hook.doneEach(_ => {
updateComponent(CONFIG, vm)
!isAuto && initSearch(CONFIG, vm)
})
}

window.$docsify.plugins = [].concat(install, window.$docsify.plugins)

0 comments on commit 2351c3e

Please sign in to comment.