/
discourse-location.js.es6
227 lines (172 loc) · 4.46 KB
/
discourse-location.js.es6
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/**
@module Discourse
*/
const get = Ember.get, set = Ember.set;
let popstateFired = false;
const supportsHistoryState = window.history && 'state' in window.history;
const popstateCallbacks = [];
/**
`Ember.DiscourseLocation` implements the location API using the browser's
`history.pushState` API.
@class DiscourseLocation
@namespace Discourse
@extends Ember.Object
*/
const DiscourseLocation = Ember.Object.extend({
init() {
set(this, 'location', get(this, 'location') || window.location);
this.initState();
},
/**
@private
Used to set state on first call to setURL
@method initState
*/
initState() {
const history = get(this, 'history') || window.history;
if (history && history.scrollRestoration) {
history.scrollRestoration = "manual";
}
set(this, 'history', history);
let url = this.formatURL(this.getURL());
const loc = get(this, 'location');
if (loc && loc.hash) {
url += loc.hash;
}
this.replaceState(url);
},
/**
Will be pre-pended to path upon state change
@property rootURL
@default '/'
*/
rootURL: '/',
/**
@private
Returns the current `location.pathname` without rootURL
@method getURL
*/
getURL() {
const location = get(this, 'location');
let url = location.pathname;
url = url.replace(Discourse.BaseUri, '');
const search = location.search || '';
url += search;
return url;
},
/**
@private
Uses `history.pushState` to update the url without a page reload.
@method setURL
@param path {String}
*/
setURL(path) {
const state = this.getState();
path = this.formatURL(path);
if (state && state.path !== path) {
this.pushState(path);
}
},
/**
@private
Uses `history.replaceState` to update the url without a page reload
or history modification.
@method replaceURL
@param path {String}
*/
replaceURL(path) {
const state = this.getState();
path = this.formatURL(path);
if (state && state.path !== path) {
this.replaceState(path);
}
},
/**
@private
Get the current `history.state`
Polyfill checks for native browser support and falls back to retrieving
from a private _historyState constiable
@method getState
*/
getState() {
return supportsHistoryState ? get(this, 'history').state : this._historyState;
},
/**
@private
Pushes a new state
@method pushState
@param path {String}
*/
pushState(path) {
const state = { path: path };
// store state if browser doesn't support `history.state`
if (!supportsHistoryState) {
this._historyState = state;
} else {
get(this, 'history').pushState(state, null, path);
}
// used for webkit workaround
this._previousURL = this.getURL();
},
/**
@private
Replaces the current state
@method replaceState
@param path {String}
*/
replaceState(path) {
const state = { path: path };
// store state if browser doesn't support `history.state`
if (!supportsHistoryState) {
this._historyState = state;
} else {
get(this, 'history').replaceState(state, null, path);
}
// used for webkit workaround
this._previousURL = this.getURL();
},
/**
@private
Register a callback to be invoked whenever the browser
history changes, including using forward and back buttons.
@method onUpdateURL
@param callback {Function}
*/
onUpdateURL(callback) {
const guid = Ember.guidFor(this),
self = this;
Ember.$(window).on('popstate.ember-location-'+guid, function() {
// Ignore initial page load popstate event in Chrome
if (!popstateFired) {
popstateFired = true;
if (self.getURL() === self._previousURL) { return; }
}
const url = self.getURL();
popstateCallbacks.forEach(function(cb) {
cb(url);
});
callback(url);
});
},
/**
@private
Used when using `{{action}}` helper. The url is always appended to the rootURL.
@method formatURL
@param url {String}
*/
formatURL(url) {
let rootURL = get(this, 'rootURL');
if (url !== '') {
rootURL = rootURL.replace(/\/$/, '');
if (rootURL.length > 0 && url.indexOf(rootURL + "/") === 0){
rootURL = "";
}
}
return rootURL + url;
},
willDestroy() {
const guid = Ember.guidFor(this);
Ember.$(window).off('popstate.ember-location-'+guid);
}
});
export default DiscourseLocation;