Skip to content

Commit faa3c14

Browse files
committed
upgrade to es6 (WIP)
1 parent 059cd16 commit faa3c14

File tree

9 files changed

+353
-333
lines changed

9 files changed

+353
-333
lines changed

src/directives/link.js

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,31 @@
1+
import { warn } from '../util'
2+
13
// install v-link, which provides navigation support for
24
// HTML5 history mode
5+
export default function (Vue) {
36

4-
module.exports = function (Vue) {
5-
6-
var _ = Vue.util
7-
var routerUtil = require('../util')
7+
let _ = Vue.util
88

99
Vue.directive('link', {
1010

1111
isLiteral: true,
1212

1313
bind: function () {
14-
var vm = this.vm
14+
let vm = this.vm
1515
/* istanbul ignore if */
1616
if (!vm.$route) {
17-
routerUtil.warn(
17+
warn(
1818
'v-link can only be used inside a ' +
1919
'router-enabled app.'
2020
)
2121
return
2222
}
23-
var self = this
24-
var router = vm.$route._router
25-
this.handler = function (e) {
23+
let router = vm.$route._router
24+
this.handler = (e) => {
2625
if (e.button === 0) {
2726
e.preventDefault()
28-
if (self.destination != null) {
29-
router.go(self.destination)
27+
if (this.destination != null) {
28+
router.go(this.destination)
3029
}
3130
}
3231
}
@@ -42,11 +41,11 @@ module.exports = function (Vue) {
4241
},
4342

4443
updateClasses: function (path) {
45-
var el = this.el
46-
var dest = this.destination
47-
var router = this.vm.$route._router
48-
var activeClass = router._linkActiveClass
49-
var exactClass = activeClass + '-exact'
44+
let el = this.el
45+
let dest = this.destination
46+
let router = this.vm.$route._router
47+
let activeClass = router._linkActiveClass
48+
let exactClass = activeClass + '-exact'
5049
if (path.indexOf(dest) === 0 && path !== '/') {
5150
_.addClass(el, activeClass)
5251
} else {
@@ -63,10 +62,10 @@ module.exports = function (Vue) {
6362
this.destination = path
6463
this.updateClasses(this.vm.$route.path)
6564
path = path || ''
66-
var router = this.vm.$route._router
67-
var isAbsolute = path.charAt(0) === '/'
65+
let router = this.vm.$route._router
66+
let isAbsolute = path.charAt(0) === '/'
6867
// do not format non-hash relative paths
69-
var href = router.mode === 'hash' || isAbsolute
68+
let href = router.mode === 'hash' || isAbsolute
7069
? router.history.formatPath(path)
7170
: path
7271
if (this.el.tagName === 'A') {

src/history/abstract.js

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,28 @@
1-
var util = require('../util')
1+
import { resolvePath } from '../util'
22

3-
function AbstractHistory (options) {
4-
this.onChange = options.onChange
5-
this.currentPath = '/'
6-
}
3+
export default class AbstractHistory {
74

8-
AbstractHistory.prototype.start = function () {
9-
this.onChange('/')
10-
}
5+
constructor (options) {
6+
this.onChange = options.onChange
7+
this.currentPath = '/'
8+
}
119

12-
AbstractHistory.prototype.stop = function () {
13-
// noop
14-
}
10+
start () {
11+
this.onChange('/')
12+
}
1513

16-
AbstractHistory.prototype.go = function (path) {
17-
path = this.currentPath = this.formatPath(path)
18-
this.onChange(path)
19-
}
14+
stop () {
15+
// noop
16+
}
2017

21-
AbstractHistory.prototype.formatPath = function (path) {
22-
return path.charAt(0) === '/'
23-
? path
24-
: util.resolvePath(this.currentPath, path)
25-
}
18+
go (path) {
19+
path = this.currentPath = this.formatPath(path)
20+
this.onChange(path)
21+
}
2622

27-
module.exports = AbstractHistory
23+
formatPath (path) {
24+
return path.charAt(0) === '/'
25+
? path
26+
: resolvePath(this.currentPath, path)
27+
}
28+
}

src/index.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
var routerUtil = require('./util')
2-
var Router = require('./router')
1+
import { warn } from './util'
2+
import Router from './router'
33

44
/**
55
* Installation interface.
@@ -9,15 +9,15 @@ var Router = require('./router')
99
Router.install = function (Vue) {
1010
/* istanbul ignore if */
1111
if (Router.installed) {
12-
routerUtil.warn('already installed.')
12+
warn('already installed.')
1313
return
1414
}
1515
require('./router/api')(Vue, Router)
1616
require('./router/internal')(Vue, Router)
1717
require('./directives/view')(Vue)
1818
require('./directives/link')(Vue)
1919
require('./override')(Vue)
20-
routerUtil.Vue = Vue
20+
Router.Vue = Vue
2121
Router.installed = true
2222
}
2323

@@ -27,4 +27,4 @@ if (typeof window !== 'undefined' && window.Vue) {
2727
Router.install(window.Vue)
2828
}
2929

30-
module.exports = Router
30+
export default Router

src/override.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,32 @@
11
// overriding Vue's $addChild method, so that every child
22
// instance inherits the route data
33

4-
module.exports = function (Vue) {
4+
export default function (Vue) {
55

6-
var addChild = Vue.prototype.$addChild
6+
let addChild = Vue.prototype.$addChild
77

88
Vue.prototype.$addChild = function (opts, Ctor) {
99

10-
var route = this.$route
11-
var router = route && route._router
10+
let route = this.$route
11+
let router = route && route._router
1212

1313
// inject meta
1414
if (router) {
1515
opts = opts || {}
16-
var meta = opts._meta = opts._meta || {}
16+
let meta = opts._meta = opts._meta || {}
1717
meta.$route = route
1818
if (opts._isRouterView) {
1919
meta.$loadingRouteData = meta.$loadingRouteData || false
2020
}
2121
}
2222

23-
var child = addChild.call(this, opts, Ctor)
23+
let child = addChild.call(this, opts, Ctor)
2424

2525
if (router) {
2626
// keep track of all children created so we can
2727
// update the routes
2828
router._children.push(child)
29-
child.$on('hook:beforeDestroy', function () {
29+
child.$on('hook:beforeDestroy', () => {
3030
router._children.$remove(child)
3131
})
3232
}

src/pipeline.js

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var util = require('./util')
1+
import { getRouteConfig, resolveAsyncComponent } from './util'
22

33
/**
44
* Determine the reusability of an existing router view.
@@ -8,8 +8,8 @@ var util = require('./util')
88
* @param {Transition} transition
99
*/
1010

11-
exports.canReuse = function (view, handler, transition) {
12-
var component = view.childVM
11+
export function canReuse (view, handler, transition) {
12+
let component = view.childVM
1313
if (!component || !handler) {
1414
return false
1515
}
@@ -18,7 +18,7 @@ exports.canReuse = function (view, handler, transition) {
1818
if (view.Component !== handler.component) {
1919
return false
2020
}
21-
var canReuseFn = util.getRouteConfig(component, 'canReuse')
21+
let canReuseFn = getRouteConfig(component, 'canReuse')
2222
return typeof canReuseFn === 'boolean'
2323
? canReuseFn
2424
: canReuseFn
@@ -37,9 +37,9 @@ exports.canReuse = function (view, handler, transition) {
3737
* @param {Function} next
3838
*/
3939

40-
exports.canDeactivate = function (view, transition, next) {
41-
var fromComponent = view.childVM
42-
var hook = util.getRouteConfig(fromComponent, 'canDeactivate')
40+
export function canDeactivate (view, transition, next) {
41+
let fromComponent = view.childVM
42+
let hook = getRouteConfig(fromComponent, 'canDeactivate')
4343
if (!hook) {
4444
next()
4545
} else {
@@ -55,14 +55,14 @@ exports.canDeactivate = function (view, transition, next) {
5555
* @param {Function} next
5656
*/
5757

58-
exports.canActivate = function (handler, transition, next) {
59-
util.resolveAsyncComponent(handler, function (Component) {
58+
export function canActivate (handler, transition, next) {
59+
resolveAsyncComponent(handler, (Component) => {
6060
// have to check due to async-ness
6161
if (transition.aborted) {
6262
return
6363
}
6464
// determine if this component can be activated
65-
var hook = util.getRouteConfig(Component, 'canActivate')
65+
let hook = getRouteConfig(Component, 'canActivate')
6666
if (!hook) {
6767
next()
6868
} else {
@@ -79,9 +79,9 @@ exports.canActivate = function (handler, transition, next) {
7979
* @param {Function} next
8080
*/
8181

82-
exports.deactivate = function (view, transition, next) {
83-
var component = view.childVM
84-
var hook = util.getRouteConfig(component, 'deactivate')
82+
export function deactivate (view, transition, next) {
83+
let component = view.childVM
84+
let hook = getRouteConfig(component, 'deactivate')
8585
if (!hook) {
8686
next()
8787
} else {
@@ -98,40 +98,40 @@ exports.deactivate = function (view, transition, next) {
9898
* @param {Function} [cb]
9999
*/
100100

101-
exports.activate = function (view, transition, depth, cb) {
102-
var handler = transition.activateQueue[depth]
101+
export function activate (view, transition, depth, cb) {
102+
let handler = transition.activateQueue[depth]
103103
if (!handler) {
104104
view.setComponent(null)
105105
cb && cb()
106106
return
107107
}
108108

109-
var Component = view.Component = handler.component
110-
var activateHook = util.getRouteConfig(Component, 'activate')
111-
var dataHook = util.getRouteConfig(Component, 'data')
112-
var waitForData = util.getRouteConfig(Component, 'waitForData')
109+
let Component = view.Component = handler.component
110+
let activateHook = getRouteConfig(Component, 'activate')
111+
let dataHook = getRouteConfig(Component, 'data')
112+
let waitForData = getRouteConfig(Component, 'waitForData')
113113

114114
// unbuild current component. this step also destroys
115115
// and removes all nested child views.
116116
view.unbuild(true)
117117
// build the new component. this will also create the
118118
// direct child view of the current one. it will register
119119
// itself as view.childView.
120-
var component = view.build({
120+
let component = view.build({
121121
_meta: {
122122
$loadingRouteData: !!(dataHook && !waitForData)
123123
}
124124
})
125125

126126
// cleanup the component in case the transition is aborted
127127
// before the component is ever inserted.
128-
var cleanup = function () {
128+
let cleanup = () => {
129129
component.$destroy()
130130
}
131131

132132
// actually insert the component and trigger transition
133-
var insert = function () {
134-
var router = transition.router
133+
let insert = () => {
134+
let router = transition.router
135135
if (router._rendered || router._transitionOnLoad) {
136136
view.transition(component)
137137
} else {
@@ -143,7 +143,7 @@ exports.activate = function (view, transition, depth, cb) {
143143
}
144144

145145
// called after activation hook is resolved
146-
var afterActivate = function () {
146+
let afterActivate = () => {
147147
// activate the child view
148148
if (view.childView) {
149149
exports.activate(view.childView, transition, depth + 1)
@@ -174,9 +174,9 @@ exports.activate = function (view, transition, depth, cb) {
174174
* @param {Transition} transition
175175
*/
176176

177-
exports.reuse = function (view, transition) {
178-
var component = view.childVM
179-
var dataHook = util.getRouteConfig(component, 'data')
177+
export function reuse (view, transition) {
178+
let component = view.childVM
179+
let dataHook = getRouteConfig(component, 'data')
180180
if (dataHook) {
181181
loadData(component, transition, dataHook)
182182
}
@@ -194,8 +194,8 @@ exports.reuse = function (view, transition) {
194194

195195
function loadData (component, transition, hook, cb, cleanup) {
196196
component.$loadingRouteData = true
197-
transition.callHook(hook, component, function (data) {
198-
for (var key in data) {
197+
transition.callHook(hook, component, (data) => {
198+
for (let key in data) {
199199
component.$set(key, data[key])
200200
}
201201
component.$loadingRouteData = false

0 commit comments

Comments
 (0)