-
Notifications
You must be signed in to change notification settings - Fork 1
/
arbiter.js
100 lines (89 loc) · 2.74 KB
/
arbiter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*! arbiter v0.0.2 https://github.com/iamdustan/arbiter | MIT License */
(function (name, definition) {
if (typeof module !== 'undefined') module.exports = definition()
else if (typeof define === 'function' && define.amd) define(name, definition)
else this[name] = definition()
}('arbiter', function () {
if (!(!!(window.history && history.pushState))) return false
var $ = window.ender
, arbiter = {}
, pageState = {}
, merge = function (o, o2) {
for (var k in o2) if (k.isOwnPropery(o2)) o[k] = o2[k]
return o
}
, defaults = function (o, o2) {
for (var k in o2) if (!o[k]) o[k] = o2[k]
return o
}
, setPrevious = function(options) {
// update previous transition to be next transition
if (pageState.state) pageState.state.transition = options.type
else {
pageState = {
state: { page: location.pathname, transition: undefined, reverse: undefined }
, title: ''
, url: location.pathname
}
}
history.replaceState(pageState.state, pageState.title, pageState.url)
}
, setState = function(options) {
// keep state details for next time
pageState = {
state: {
page: options.page
, transition: options.type
, reverse: options.reverse
}
, title: ''
, url: options.page
}
history.pushState(pageState.state, pageState.title, pageState.url)
}
, changePage = function (page, options) {
// set callback function to getPage if none are built in
var success;
if (typeof options === 'function') success = options
options = options || {}
defaults(options, {
type: null,
reverse: null,
page: page,
success: success
})
setPrevious(options)
setState(options)
getPage(options)
}
, getPage = function (ajax) {
ajax = defaults(ajax || {}, {
url: pageState.url,
type: 'html',
method: 'get',
success: function (data) {
$('#slider').html($(data).find('#slider').html())
}
})
$.ajax(ajax)
}
if ('addEventListener' in window) {
window.addEventListener('popstate', function (e) {
// ignore popstate thrown on page load
if (!e.state) { return }
//transition(e.state.page, e.state.transition, !e.state.reverse)
pageState = {
state: {
page: e.state.page
, transition: e.state.transition
, reverse: e.state.reverse
}
, title: ''
, url: e.state.page
}
getPage()
}, false)
}
arbiter.change = changePage;
return arbiter
}));