-
Notifications
You must be signed in to change notification settings - Fork 20
/
history.js
149 lines (132 loc) · 3.52 KB
/
history.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/**
* BEM wrap for History API.
* @module history
*/
modules.define('history', ['inherit', 'events', 'jquery', 'uri'], function(provide, inherit, events, $, Uri) {
/**
* @class BEMHistory
* @augments events:Emitter
* @exports history
*/
var BEMHistory = inherit(events.Emitter, /** @lends BEMHistory.prototype */{
/**
* @constructor
*/
__constructor : function() {
this
._resetUrl()
._bindEvents()
._syncState();
},
/**
* Adds new state to browsing history.
*
* @param {Object} data New state data.
* @param {String} title Document title.
* @param {String} [url] Location url.
* @returns {Object}
*/
pushState : function(data, title, url) {
return this.changeState('push', this._normalizeState(data, title, url));
},
/**
* Replaces current state.
*
* @param {Object} data New state data
* @param {String} title Document title.
* @param {String} [url] Location url.
* @returns {Object}
*/
replaceState : function(data, title, url) {
return this.changeState('replace', this._normalizeState(data, title, url));
},
/**
* Base method for an events binding.
* Method have to be extended in modificators.
*
* @private
* @returns {Object}
*/
_bindEvents : function() {
return this;
},
/**
* Base method for url check and reset.
* Method have to be extended in modificators.
*
* @private
* @returns {Object}
*/
_resetUrl : function() {
return this;
},
/**
* Removes hashbang from url.
*
* @private
* @example
* /?p=1#!/?p=2 => /?p=2.
*
* @param {String} url
* @returns {String}
*/
_removeHashbang : function(url) {
var parsedUri = Uri.parse(url),
hash = parsedUri.getAnchor();
if(hash === '' || hash[0] !== '!') { return url; }
var hashbangUri = Uri.parse(hash.replace(/^!/, ''));
parsedUri
.setAnchor('')
.setQuery(hashbangUri.getQuery());
return parsedUri.build();
},
/**
* Base method for the state syncing with global history state.
* Method may be extended in modificators.
*
* @private
* @returns {Object}
*/
_syncState : function() {
this.state = this._normalizeState(undefined, document.title, window.location.href);
return this;
},
/**
* Normalizes state to the appropriate form.
*
* @private
* @param {Object} data
* @param {String} title
* @param {String} url
* @returns {Object} normalized state
*/
_normalizeState : function(data, title, url) {
// null -> default state, which we don't want to listen
// so that data have to be undefined or an empty object
// but not null
return {
data : data === null ? undefined : data,
title : title,
url : url
};
},
/**
* Changes current state.
* By default it performs simple page redirect.
* Method may be extended in modificators.
*
* @param {String} method Push or Replace method.
* @param {Object} state
* @returns {Object}
*/
changeState : function(method, state) {
try {
window.location.assign(state.url);
} catch (e) {}
}
});
BEMHistory.hasNativeAPI = function() {
return (window.history && 'pushState' in window.history);
};
provide(BEMHistory);
});