Skip to content

Commit ebb92b0

Browse files
committed
more es6 (wip)
1 parent faa3c14 commit ebb92b0

File tree

7 files changed

+224
-212
lines changed

7 files changed

+224
-212
lines changed

src/directives/view.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
module.exports = function (Vue) {
1+
import { warn } from '../util'
22

3-
var _ = Vue.util
4-
var util = require('../util')
5-
var componentDef = Vue.directive('_component')
3+
export default function (Vue) {
64

5+
let _ = Vue.util
6+
let componentDef = Vue.directive('_component')
77
// <router-view> extends the internal component directive
8-
var viewDef = _.extend({}, componentDef)
8+
let viewDef = _.extend({}, componentDef)
99

1010
// with some overrides
1111
_.extend(viewDef, {
1212

1313
_isRouterView: true,
1414

1515
bind: function () {
16-
var route = this.vm.$route
16+
let route = this.vm.$route
1717
/* istanbul ignore if */
1818
if (!route) {
19-
util.warn(
19+
warn(
2020
'<router-view> can only be used inside a ' +
2121
'router-enabled app.'
2222
)
@@ -31,17 +31,17 @@ module.exports = function (Vue) {
3131
/* istanbul ignore if */
3232
if (this.keepAlive) {
3333
this.keepAlive = false
34-
util.warn('<router-view> does not support keep-alive.')
34+
warn('<router-view> does not support keep-alive.')
3535
}
3636

3737
// all we need to do here is registering this view
3838
// in the router. actual component switching will be
3939
// managed by the pipeline.
40-
var router = this.router = route._router
40+
let router = this.router = route._router
4141
router._views.unshift(this)
4242

4343
// note the views are in reverse order.
44-
var parentView = router._views[1]
44+
let parentView = router._views[1]
4545
if (parentView) {
4646
// register self as a child of the parent view,
4747
// instead of activating now. This is so that the

src/history/hash.js

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

3-
function HashHistory (options) {
4-
this.hashbang = options.hashbang
5-
this.onChange = options.onChange
6-
}
3+
export default class HashHistory {
4+
5+
constructor (options) {
6+
this.hashbang = options.hashbang
7+
this.onChange = options.onChange
8+
}
79

8-
HashHistory.prototype.start = function () {
9-
var self = this
10-
this.listener = function () {
11-
var path = location.hash
12-
var formattedPath = self.formatPath(path, true)
13-
if (formattedPath !== path) {
14-
location.replace(formattedPath)
15-
return
10+
start () {
11+
let self = this
12+
this.listener = function () {
13+
let path = location.hash
14+
let formattedPath = self.formatPath(path, true)
15+
if (formattedPath !== path) {
16+
location.replace(formattedPath)
17+
return
18+
}
19+
let pathToMatch = decodeURI(
20+
path.replace(/^#!?/, '') + location.search
21+
)
22+
self.onChange(pathToMatch)
1623
}
17-
var pathToMatch = decodeURI(
18-
path.replace(/^#!?/, '') + location.search
19-
)
20-
self.onChange(pathToMatch)
24+
window.addEventListener('hashchange', this.listener)
25+
this.listener()
2126
}
22-
window.addEventListener('hashchange', this.listener)
23-
this.listener()
24-
}
2527

26-
HashHistory.prototype.stop = function () {
27-
window.removeEventListener('hashchange', this.listener)
28-
}
28+
stop () {
29+
window.removeEventListener('hashchange', this.listener)
30+
}
2931

30-
HashHistory.prototype.go = function (path, replace) {
31-
path = this.formatPath(path)
32-
if (replace) {
33-
location.replace(path)
34-
} else {
35-
location.hash = path
32+
go (path, replace) {
33+
path = this.formatPath(path)
34+
if (replace) {
35+
location.replace(path)
36+
} else {
37+
location.hash = path
38+
}
3639
}
37-
}
3840

39-
HashHistory.prototype.formatPath = function (path, expectAbsolute) {
40-
path = path.replace(/^#!?/, '')
41-
var isAbsoloute = path.charAt(0) === '/'
42-
if (expectAbsolute && !isAbsoloute) {
43-
path = '/' + path
41+
formatPath (path, expectAbsolute) {
42+
path = path.replace(/^#!?/, '')
43+
let isAbsoloute = path.charAt(0) === '/'
44+
if (expectAbsolute && !isAbsoloute) {
45+
path = '/' + path
46+
}
47+
let prefix = '#' + (this.hashbang ? '!' : '')
48+
return isAbsoloute || expectAbsolute
49+
? prefix + path
50+
: prefix + resolvePath(
51+
location.hash.replace(/^#!?/, ''),
52+
path
53+
)
4454
}
45-
var prefix = '#' + (this.hashbang ? '!' : '')
46-
return isAbsoloute || expectAbsolute
47-
? prefix + path
48-
: prefix + util.resolvePath(
49-
location.hash.replace(/^#!?/, ''),
50-
path
51-
)
5255
}
53-
54-
module.exports = HashHistory

src/history/html5.js

Lines changed: 65 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,76 @@
1-
var util = require('../util')
2-
var hashRE = /#.*$/
1+
import { resolvePath } from '../util'
2+
const hashRE = /#.*$/
33

4-
function HTML5History (options) {
5-
var root = options.root
6-
if (root) {
7-
// make sure there's the starting slash
8-
if (root.charAt(0) !== '/') {
9-
root = '/' + root
4+
export default class HTML5History {
5+
6+
constructor (options) {
7+
let root = options.root
8+
if (root) {
9+
// make sure there's the starting slash
10+
if (root.charAt(0) !== '/') {
11+
root = '/' + root
12+
}
13+
// remove trailing slash
14+
this.root = root.replace(/\/$/, '')
15+
this.rootRE = new RegExp('^\\' + this.root)
16+
} else {
17+
this.root = null
1018
}
11-
// remove trailing slash
12-
this.root = root.replace(/\/$/, '')
13-
this.rootRE = new RegExp('^\\' + this.root)
14-
} else {
15-
this.root = null
19+
this.onChange = options.onChange
20+
// check base tag
21+
let baseEl = document.querySelector('base')
22+
this.base = baseEl && baseEl.getAttribute('href')
1623
}
17-
this.onChange = options.onChange
18-
// check base tag
19-
var baseEl = document.querySelector('base')
20-
this.base = baseEl && baseEl.getAttribute('href')
21-
}
2224

23-
HTML5History.prototype.start = function () {
24-
var self = this
25-
this.listener = function (e) {
26-
var url = decodeURI(location.pathname + location.search)
27-
if (this.root) {
28-
url = url.replace(this.rootRE, '')
25+
start () {
26+
let self = this
27+
this.listener = function (e) {
28+
let url = decodeURI(location.pathname + location.search)
29+
if (this.root) {
30+
url = url.replace(this.rootRE, '')
31+
}
32+
self.onChange(url, e && e.state, location.hash)
2933
}
30-
self.onChange(url, e && e.state, location.hash)
34+
window.addEventListener('popstate', this.listener)
35+
this.listener()
3136
}
32-
window.addEventListener('popstate', this.listener)
33-
this.listener()
34-
}
3537

36-
HTML5History.prototype.stop = function () {
37-
window.removeEventListener('popstate', this.listener)
38-
}
38+
stop () {
39+
window.removeEventListener('popstate', this.listener)
40+
}
3941

40-
HTML5History.prototype.go = function (path, replace) {
41-
var root = this.root
42-
var url = this.formatPath(path, root)
43-
if (replace) {
44-
history.replaceState({}, '', url)
45-
} else {
46-
// record scroll position by replacing current state
47-
history.replaceState({
48-
pos: {
49-
x: window.pageXOffset,
50-
y: window.pageYOffset
51-
}
52-
}, '')
53-
// then push new state
54-
history.pushState({}, '', url)
42+
go (path, replace) {
43+
let root = this.root
44+
let url = this.formatPath(path, root)
45+
if (replace) {
46+
history.replaceState({}, '', url)
47+
} else {
48+
// record scroll position by replacing current state
49+
history.replaceState({
50+
pos: {
51+
x: window.pageXOffset,
52+
y: window.pageYOffset
53+
}
54+
}, '')
55+
// then push new state
56+
history.pushState({}, '', url)
57+
}
58+
let hashMatch = path.match(hashRE)
59+
let hash = hashMatch && hashMatch[0]
60+
path = url
61+
// strip hash so it doesn't mess up params
62+
.replace(hashRE, '')
63+
// remove root before matching
64+
.replace(this.rootRE, '')
65+
this.onChange(path, null, hash)
5566
}
56-
var hashMatch = path.match(hashRE)
57-
var hash = hashMatch && hashMatch[0]
58-
path = url
59-
// strip hash so it doesn't mess up params
60-
.replace(hashRE, '')
61-
// remove root before matching
62-
.replace(this.rootRE, '')
63-
this.onChange(path, null, hash)
64-
}
6567

66-
HTML5History.prototype.formatPath = function (path) {
67-
return path.charAt(0) === '/'
68-
// absolute path
69-
? this.root
70-
? this.root + '/' + path.replace(/^\//, '')
71-
: path
72-
: util.resolvePath(this.base || location.pathname, path)
68+
formatPath (path) {
69+
return path.charAt(0) === '/'
70+
// absolute path
71+
? this.root
72+
? this.root + '/' + path.replace(/^\//, '')
73+
: path
74+
: resolvePath(this.base || location.pathname, path)
75+
}
7376
}
74-
75-
module.exports = HTML5History

src/route.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1+
/**
2+
* Route Context Object
3+
*
4+
* @param {String} path
5+
* @param {Router} router
6+
*/
7+
18
export default class Route {
9+
210
constructor (path, router) {
311
this.path = path
412
let matched = router._recognizer.recognize(path)

src/router/api.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
var routerUtil = require('../util')
1+
import { warn } from '../util'
22

3-
module.exports = function (Vue, Router) {
3+
export default function (Vue, Router) {
44

55
/**
66
* Register a map of top-level paths.
77
*/
88

99
Router.prototype.map = function (map) {
10-
for (var route in map) {
10+
for (let route in map) {
1111
this.on(route, map[route])
1212
}
1313
}
@@ -39,7 +39,7 @@ module.exports = function (Vue, Router) {
3939
*/
4040

4141
Router.prototype.redirect = function (map) {
42-
for (var path in map) {
42+
for (let path in map) {
4343
this._addRedirect(path, map[path])
4444
}
4545
}
@@ -51,7 +51,7 @@ module.exports = function (Vue, Router) {
5151
*/
5252

5353
Router.prototype.alias = function (map) {
54-
for (var path in map) {
54+
for (let path in map) {
5555
this._addAlias(path, map[path])
5656
}
5757
}
@@ -99,7 +99,7 @@ module.exports = function (Vue, Router) {
9999
Router.prototype.start = function (App, container) {
100100
/* istanbul ignore if */
101101
if (this._started) {
102-
routerUtil.warn('already started.')
102+
warn('already started.')
103103
return
104104
}
105105
this._started = true

0 commit comments

Comments
 (0)