2016-11-07 01:34:58 +00:00
|
|
|
'use strict'
|
|
|
|
|
|
|
|
export function getMatchedComponents (route) {
|
|
|
|
return [].concat.apply([], route.matched.map(function (m) {
|
|
|
|
return Object.keys(m.components).map(function (key) {
|
|
|
|
return m.components[key]
|
|
|
|
})
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
2016-11-22 23:27:07 +00:00
|
|
|
export function getMatchedComponentsInstances (route) {
|
|
|
|
return [].concat.apply([], route.matched.map(function (m) {
|
|
|
|
return Object.keys(m.instances).map(function (key) {
|
|
|
|
return m.instances[key]
|
|
|
|
})
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
2016-11-07 01:34:58 +00:00
|
|
|
export function flatMapComponents (route, fn) {
|
|
|
|
return Array.prototype.concat.apply([], route.matched.map(function (m, index) {
|
|
|
|
return Object.keys(m.components).map(function (key) {
|
|
|
|
return fn(m.components[key], m.instances[key], m, key, index)
|
|
|
|
})
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getContext (context) {
|
|
|
|
let ctx = {
|
|
|
|
isServer: !!context.isServer,
|
|
|
|
isClient: !!context.isClient,
|
2016-11-19 21:16:26 +00:00
|
|
|
isDev: <%= isDev %>,
|
2016-11-07 01:34:58 +00:00
|
|
|
<%= (store ? 'store: context.store,' : '') %>
|
2016-11-17 10:57:02 +00:00
|
|
|
route: (context.to ? context.to : context.route),
|
2016-12-04 18:15:11 +00:00
|
|
|
error: context.error,
|
|
|
|
base: '<%= router.base %>',
|
|
|
|
env: <%= JSON.stringify(env) %>
|
2016-11-07 01:34:58 +00:00
|
|
|
}
|
2016-11-21 13:14:35 +00:00
|
|
|
const next = context.next
|
2016-11-07 20:38:51 +00:00
|
|
|
ctx.params = ctx.route.params || {}
|
|
|
|
ctx.query = ctx.route.query || {}
|
2016-11-11 00:35:05 +00:00
|
|
|
ctx.redirect = function (status, path, query) {
|
|
|
|
if (!status) return
|
|
|
|
// if only 1 or 2 arguments: redirect('/') or redirect('/', { foo: 'bar' })
|
|
|
|
if (typeof status === 'string' && (typeof path === 'undefined' || typeof path === 'object')) {
|
|
|
|
query = path || {}
|
|
|
|
path = status
|
|
|
|
status = 302
|
|
|
|
}
|
2016-11-21 13:14:35 +00:00
|
|
|
next({
|
2016-11-11 00:35:05 +00:00
|
|
|
path: path,
|
|
|
|
query: query,
|
|
|
|
status: status
|
|
|
|
})
|
2016-11-10 23:01:36 +00:00
|
|
|
}
|
2016-11-07 01:34:58 +00:00
|
|
|
if (context.req) ctx.req = context.req
|
2016-11-07 12:53:05 +00:00
|
|
|
if (context.res) ctx.res = context.res
|
2016-11-07 01:34:58 +00:00
|
|
|
return ctx
|
|
|
|
}
|
|
|
|
|
2016-11-19 21:16:26 +00:00
|
|
|
export function promisify (fn, context) {
|
|
|
|
let promise
|
|
|
|
if (fn.length === 2) {
|
|
|
|
// fn(context, callback)
|
|
|
|
promise = new Promise((resolve) => {
|
2016-11-21 13:14:35 +00:00
|
|
|
fn(context, function (err, data) {
|
2016-11-19 21:16:26 +00:00
|
|
|
if (err) {
|
|
|
|
context.error(err)
|
|
|
|
}
|
|
|
|
data = data || {}
|
|
|
|
resolve(data)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
} else {
|
2016-11-21 13:14:35 +00:00
|
|
|
promise = fn(context)
|
2016-11-19 21:16:26 +00:00
|
|
|
}
|
|
|
|
if (!(promise instanceof Promise)) {
|
|
|
|
promise = Promise.resolve(promise)
|
|
|
|
}
|
|
|
|
return promise
|
|
|
|
}
|
|
|
|
|
2016-11-07 01:34:58 +00:00
|
|
|
// Imported from vue-router
|
|
|
|
export function getLocation (base) {
|
|
|
|
var path = window.location.pathname
|
|
|
|
if (base && path.indexOf(base) === 0) {
|
|
|
|
path = path.slice(base.length)
|
|
|
|
}
|
|
|
|
return (path || '/') + window.location.search + window.location.hash
|
|
|
|
}
|
2016-11-10 23:01:36 +00:00
|
|
|
|
2016-11-18 09:38:47 +00:00
|
|
|
export function urlJoin () {
|
2016-11-10 23:01:36 +00:00
|
|
|
return [].slice.call(arguments).join('/').replace(/\/+/g, '/')
|
|
|
|
}
|