Skip to content

Commit 36d55ec

Browse files
committed
[build] 2.0.0-beta.2
1 parent a1e3306 commit 36d55ec

File tree

2 files changed

+98
-66
lines changed

2 files changed

+98
-66
lines changed

dist/vue-router.js

Lines changed: 97 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -185,13 +185,15 @@
185185
return true
186186
}
187187

188-
function assert (condition, message) {
188+
/* */
189+
190+
function assert (condition , message ) {
189191
if (!condition) {
190192
throw new Error(("[vue-router] " + message))
191193
}
192194
}
193195

194-
function warn (condition, message) {
196+
function warn (condition , message ) {
195197
if (!condition) {
196198
typeof console !== 'undefined' && console.warn(("[vue-router] " + message))
197199
}
@@ -404,7 +406,7 @@
404406
beforeCreate: function beforeCreate () {
405407
if (this.$options.router) {
406408
this._router = this.$options.router
407-
this._router.app = this
409+
this._router.init(this)
408410
Vue.util.defineReactive(this, '_route', this._router.history.current)
409411
}
410412
}
@@ -1034,7 +1036,7 @@
10341036
hash: hash
10351037
}, undefined, location)
10361038
} else {
1037-
warn(("invalid redirect option: " + (JSON.stringify(redirect))))
1039+
warn(false, ("invalid redirect option: " + (JSON.stringify(redirect))))
10381040
return createRouteContext(null, location)
10391041
}
10401042
}
@@ -1104,7 +1106,7 @@
11041106
(regexpCompileCache[path] = Regexp.compile(path))
11051107
return filler(params || {}, { pretty: true })
11061108
} catch (e) {
1107-
assert(("missing param for " + routeMsg + ": " + (e.message)))
1109+
assert(false, ("missing param for " + routeMsg + ": " + (e.message)))
11081110
return ''
11091111
}
11101112
}
@@ -1201,7 +1203,7 @@
12011203
// deactivate guards
12021204
extractLeaveGuards(deactivated),
12031205
// global before hooks
1204-
nomralizeGuards(this.router.options.beforeEach),
1206+
this.router.beforeHooks,
12051207
// activate guards
12061208
activated.map(function (m) { return m.beforeEnter; })
12071209
).filter(function (_) { return _; })
@@ -1224,7 +1226,7 @@
12241226
History.prototype.updateRoute = function updateRoute (route ) {
12251227
this.current = route
12261228
this.cb && this.cb(route)
1227-
nomralizeGuards(this.router.options.afterEach).forEach(function (hook) {
1229+
this.router.afterHooks.forEach(function (hook) {
12281230
hook && hook(route)
12291231
})
12301232
};
@@ -1271,16 +1273,6 @@
12711273
}
12721274
}
12731275

1274-
function nomralizeGuards (guards ) {
1275-
if (!guards) {
1276-
return []
1277-
}
1278-
if (typeof guards === 'function') {
1279-
return [guards]
1280-
}
1281-
return guards
1282-
}
1283-
12841276
function extractLeaveGuards (matched ) {
12851277
return Array.prototype.concat.apply([], matched.map(function (m) {
12861278
return Object.keys(m.components).map(function (key) {
@@ -1298,6 +1290,45 @@
12981290
}).reverse())
12991291
}
13001292

1293+
/* */
1294+
1295+
function saveScrollPosition (key ) {
1296+
if (!key) return
1297+
window.sessionStorage.setItem(key, JSON.stringify({
1298+
x: window.pageXOffset,
1299+
y: window.pageYOffset
1300+
}))
1301+
}
1302+
1303+
function getScrollPosition (key ) {
1304+
if (!key) return
1305+
return JSON.parse(window.sessionStorage.getItem(key))
1306+
}
1307+
1308+
function getElementPosition (el ) {
1309+
var docRect = document.documentElement.getBoundingClientRect()
1310+
var elRect = el.getBoundingClientRect()
1311+
return {
1312+
x: elRect.left - docRect.left,
1313+
y: elRect.top - docRect.top
1314+
}
1315+
}
1316+
1317+
function isValidPosition (obj ) {
1318+
return isNumber(obj.x) || isNumber(obj.y)
1319+
}
1320+
1321+
function normalizePosotion (obj ) {
1322+
return {
1323+
x: isNumber(obj.x) ? obj.x : window.pageXOffset,
1324+
y: isNumber(obj.y) ? obj.y : window.pageYOffset
1325+
}
1326+
}
1327+
1328+
function isNumber (v ) {
1329+
return typeof v === 'number'
1330+
}
1331+
13011332
var genKey = function () { return String(Date.now()); }
13021333
var _key = genKey()
13031334

@@ -1326,7 +1357,9 @@
13261357
})
13271358

13281359
if (expectScroll) {
1329-
window.addEventListener('scroll', saveScrollPosition)
1360+
window.addEventListener('scroll', function () {
1361+
saveScrollPosition(_key)
1362+
})
13301363
}
13311364
}
13321365

@@ -1372,30 +1405,25 @@
13721405
if (!behavior) {
13731406
return
13741407
}
1375-
13761408
assert(typeof behavior === 'function', "scrollBehavior must be a function")
13771409

1378-
var position = getScrollPosition()
1379-
var shouldScroll = behavior(to, from, isPop ? position : null)
1380-
if (!shouldScroll) {
1381-
return
1382-
}
1383-
13841410
// wait until re-render finishes before scrolling
13851411
router.app.$nextTick(function () {
1412+
var position = getScrollPosition(_key)
1413+
var shouldScroll = behavior(to, from, isPop ? position : null)
1414+
if (!shouldScroll) {
1415+
return
1416+
}
13861417
var isObject = typeof shouldScroll === 'object'
1387-
if (isObject && shouldScroll.x != null && shouldScroll.y != null) {
1388-
position = shouldScroll
1389-
} else if (isObject && shouldScroll.anchor) {
1390-
var el = document.querySelector(to.hash)
1418+
if (isObject && shouldScroll.selector) {
1419+
var el = document.querySelector(shouldScroll.selector)
13911420
if (el) {
1392-
var docTop = document.documentElement.getBoundingClientRect().top
1393-
var elTop = el.getBoundingClientRect().top
1394-
position = {
1395-
x: window.scrollX,
1396-
y: elTop - docTop
1397-
}
1421+
position = getElementPosition(el)
1422+
} else if (isValidPosition(shouldScroll)) {
1423+
position = normalizePosotion(shouldScroll)
13981424
}
1425+
} else if (isObject && isValidPosition(shouldScroll)) {
1426+
position = normalizePosotion(shouldScroll)
13991427
}
14001428

14011429
if (position) {
@@ -1426,7 +1454,7 @@
14261454
_key = genKey()
14271455
history.pushState({ key: _key }, '', url)
14281456
}
1429-
saveScrollPosition()
1457+
saveScrollPosition(_key)
14301458
} catch (e) {
14311459
window.location[replace ? 'assign' : 'replace'](url)
14321460
}
@@ -1436,19 +1464,6 @@
14361464
pushState(url, true)
14371465
}
14381466

1439-
function saveScrollPosition () {
1440-
if (!_key) return
1441-
window.sessionStorage.setItem(_key, JSON.stringify({
1442-
x: window.pageXOffset,
1443-
y: window.pageYOffset
1444-
}))
1445-
}
1446-
1447-
function getScrollPosition () {
1448-
if (!_key) return
1449-
return JSON.parse(window.sessionStorage.getItem(_key))
1450-
}
1451-
14521467
var HashHistory = (function (History) {
14531468
function HashHistory (router , base , fallback ) {
14541469
var this$1 = this;
@@ -1595,28 +1610,38 @@
15951610
}(History));
15961611

15971612
var VueRouter = function VueRouter (options) {
1598-
var this$1 = this;
15991613
if ( options === void 0 ) options = {};
16001614

1601-
assert(
1602-
install.installed,
1603-
"not installed. Make sure to call `Vue.use(VueRouter)` " +
1604-
"before mounting root instance."
1605-
)
1606-
16071615
this.app = null
16081616
this.options = options
1617+
this.beforeHooks = []
1618+
this.afterHooks = []
16091619
this.match = createMatcher(options.routes || [])
16101620

16111621
var mode = options.mode || 'hash'
1612-
var fallback = mode === 'history' && !supportsHistory
1613-
if (fallback) {
1622+
this.fallback = mode === 'history' && !supportsHistory
1623+
if (this.fallback) {
16141624
mode = 'hash'
16151625
}
16161626
if (!inBrowser) {
16171627
mode = 'abstract'
16181628
}
1629+
this.mode = mode
1630+
};
1631+
1632+
VueRouter.prototype.init = function init (app /* Vue component instance */) {
1633+
var this$1 = this;
1634+
1635+
assert(
1636+
install.installed,
1637+
"not installed. Make sure to call `Vue.use(VueRouter)` " +
1638+
"before creating root instance."
1639+
)
16191640

1641+
var ref = this;
1642+
var mode = ref.mode;
1643+
var options = ref.options;
1644+
var fallback = ref.fallback;
16201645
switch (mode) {
16211646
case 'history':
16221647
this.history = new HTML5History(this, options.base)
@@ -1631,26 +1656,33 @@
16311656
assert(false, ("invalid mode: " + mode))
16321657
}
16331658

1634-
this.mode = mode
1635-
1659+
this.app = app
16361660
this.history.listen(function (route) {
16371661
this$1.app._route = route
16381662
})
16391663
};
16401664

1665+
VueRouter.prototype.beforeEach = function beforeEach (fn ) {
1666+
this.beforeHooks.push(fn)
1667+
};
1668+
1669+
VueRouter.prototype.afterEach = function afterEach (fn ) {
1670+
this.afterHooks.push(fn)
1671+
};
1672+
16411673
VueRouter.prototype.push = function push (location ) {
16421674
this.history.push(location)
16431675
};
16441676

16451677
VueRouter.prototype.replace = function replace (location ) {
16461678
this.history.replace(location)
1647-
};
1679+
};
16481680

1649-
VueRouter.prototype.go = function go (n ) {
1650-
this.history.go(n)
1651-
};
1681+
VueRouter.prototype.go = function go (n ) {
1682+
this.history.go(n)
1683+
};
16521684

1653-
VueRouter.prototype.back = function back () {
1685+
VueRouter.prototype.back = function back () {
16541686
this.go(-1)
16551687
};
16561688

0 commit comments

Comments
 (0)