Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added titlePrefix and titleSuffix properties inline with title. #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
33 changes: 29 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import mixin from './mixin'
import { setPageTitle } from './page-title'
import { setup as setupRouter } from './router'
import {safeString} from './utils'

const install = (Vue, options = {}) => {
// prevent double install
Expand All @@ -10,26 +11,50 @@ const install = (Vue, options = {}) => {

// title state
const $page = {
title: ''
title: '',
prefix: safeString(options.prefix),
suffix: safeString(options.suffix)
}

const setTitle = value => {
setPageTitle(value, options)
$page.title = value
}

// make reactive title
const setPrefix = value => {
options.prefix = value
setTitle($page.title)
}

const setSuffix = value => {
options.suffix = value
setTitle($page.title)
}

// make reactive title properties
Vue.util.defineReactive($page, 'title', '')
Vue.util.defineReactive($page, 'prefix', '')
Vue.util.defineReactive($page, 'suffix', '')

// add title to component context
// add title elements to component context
Object.defineProperty(Vue.prototype, '$title', {
get: () => $page.title,
set: value => setTitle(value)
})

Object.defineProperty(Vue.prototype, '$titlePrefix', {
get: () => $page.prefix,
set: value => setPrefix(value)
})

Object.defineProperty(Vue.prototype, '$titleSuffix', {
get: () => $page.suffix,
set: value => setSuffix(value)
})

// vue router support
if (options.router) {
setupRouter(setTitle, options)
setupRouter(setTitle, setPrefix, setSuffix, options)
}

// add global mixin
Expand Down
18 changes: 16 additions & 2 deletions src/mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,28 @@ import { isFunction } from './utils'

const pageTitleMixin = {
created () {
const { title } = this.$options
const { title, titlePrefix, titleSuffix } = this.$options

if (title !== undefined) {
// allow use dinamic title system
// allow use dynamic title system
this.$title = isFunction(title)
? title.call(this, this)
: title
}

if (titlePrefix !== undefined) {
// allow use dynamic titlePrefix system
this.$titlePrefix = isFunction(titlePrefix)
? titlePrefix.call(this, this)
: titlePrefix
}

if (titleSuffix !== undefined) {
// allow use dynamic titleSuffix system
this.$titleSuffix = isFunction(titleSuffix)
? titleSuffix.call(this, this)
: titleSuffix
}
}
}

Expand Down
21 changes: 17 additions & 4 deletions src/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,26 @@
* @param {Object} options { router: RouterInstance }
* @return {void}
*/
const setup = (setTitle, { router }) => {
const setup = (setTitle, setPrefix, setSuffix, { router }) => {
router.afterEach((to, from) => {
const { meta } = to

// if has meta and title
if (meta && meta.title) {
setTitle(meta.title)
// if has meta
if (meta) {
// if has title
if (meta.title) {
setTitle(meta.title)
}

// if has titlePrefix
if (meta.titlePrefix) {
setPrefix(meta.titlePrefix)
}

// if has titleSuffix
if (meta.titleSuffix) {
setSuffix(meta.titleSuffix)
}
}
})
}
Expand Down