REFACTOR: discourse-location lib (#7700)
This commit is contained in:
parent
32f878db69
commit
80459d83a4
|
@ -1,14 +1,6 @@
|
|||
import { defaultHomepage } from "discourse/lib/utilities";
|
||||
|
||||
/**
|
||||
@module Discourse
|
||||
*/
|
||||
|
||||
const get = Ember.get,
|
||||
set = Ember.set;
|
||||
let popstateFired = false;
|
||||
const supportsHistoryState = window.history && "state" in window.history;
|
||||
|
||||
const popstateCallbacks = [];
|
||||
|
||||
/**
|
||||
|
@ -21,7 +13,9 @@ const popstateCallbacks = [];
|
|||
*/
|
||||
const DiscourseLocation = Ember.Object.extend({
|
||||
init() {
|
||||
set(this, "location", get(this, "location") || window.location);
|
||||
this._super(...arguments);
|
||||
|
||||
this.set("location", this.location || window.location);
|
||||
this.initState();
|
||||
},
|
||||
|
||||
|
@ -33,18 +27,17 @@ const DiscourseLocation = Ember.Object.extend({
|
|||
@method initState
|
||||
*/
|
||||
initState() {
|
||||
const history = get(this, "history") || window.history;
|
||||
const history = this.history || window.history;
|
||||
if (history && history.scrollRestoration) {
|
||||
history.scrollRestoration = "manual";
|
||||
}
|
||||
|
||||
set(this, "history", history);
|
||||
this.set("history", history);
|
||||
|
||||
let url = this.formatURL(this.getURL());
|
||||
const loc = get(this, "location");
|
||||
|
||||
if (loc && loc.hash) {
|
||||
url += loc.hash;
|
||||
if (this.location && this.location.hash) {
|
||||
url += this.location.hash;
|
||||
}
|
||||
|
||||
this.replaceState(url);
|
||||
|
@ -66,12 +59,11 @@ const DiscourseLocation = Ember.Object.extend({
|
|||
@method getURL
|
||||
*/
|
||||
getURL() {
|
||||
const location = get(this, "location");
|
||||
let url = location.pathname;
|
||||
let url = this.location.pathname;
|
||||
|
||||
url = url.replace(new RegExp(`^${Discourse.BaseUri}`), "");
|
||||
|
||||
const search = location.search || "";
|
||||
const search = this.location.search || "";
|
||||
url += search;
|
||||
return url;
|
||||
},
|
||||
|
@ -124,9 +116,7 @@ const DiscourseLocation = Ember.Object.extend({
|
|||
@method getState
|
||||
*/
|
||||
getState() {
|
||||
return supportsHistoryState
|
||||
? get(this, "history").state
|
||||
: this._historyState;
|
||||
return supportsHistoryState ? this.history.state : this._historyState;
|
||||
},
|
||||
|
||||
/**
|
||||
|
@ -138,13 +128,13 @@ const DiscourseLocation = Ember.Object.extend({
|
|||
@param path {String}
|
||||
*/
|
||||
pushState(path) {
|
||||
const state = { path: path };
|
||||
const state = { path };
|
||||
|
||||
// store state if browser doesn't support `history.state`
|
||||
if (!supportsHistoryState) {
|
||||
this._historyState = state;
|
||||
} else {
|
||||
get(this, "history").pushState(state, null, path);
|
||||
this.history.pushState(state, null, path);
|
||||
}
|
||||
|
||||
// used for webkit workaround
|
||||
|
@ -160,13 +150,13 @@ const DiscourseLocation = Ember.Object.extend({
|
|||
@param path {String}
|
||||
*/
|
||||
replaceState(path) {
|
||||
const state = { path: path };
|
||||
const state = { path };
|
||||
|
||||
// store state if browser doesn't support `history.state`
|
||||
if (!supportsHistoryState) {
|
||||
this._historyState = state;
|
||||
} else {
|
||||
get(this, "history").replaceState(state, null, path);
|
||||
this.history.replaceState(state, null, path);
|
||||
}
|
||||
|
||||
// used for webkit workaround
|
||||
|
@ -183,21 +173,18 @@ const DiscourseLocation = Ember.Object.extend({
|
|||
@param callback {Function}
|
||||
*/
|
||||
onUpdateURL(callback) {
|
||||
const guid = Ember.guidFor(this),
|
||||
self = this;
|
||||
const guid = Ember.guidFor(this);
|
||||
|
||||
$(window).on(`popstate.ember-location-${guid}`, () => {
|
||||
const url = this.getURL();
|
||||
|
||||
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;
|
||||
}
|
||||
if (url === this._previousURL) return;
|
||||
}
|
||||
const url = self.getURL();
|
||||
popstateCallbacks.forEach(function(cb) {
|
||||
cb(url);
|
||||
});
|
||||
|
||||
popstateCallbacks.forEach(cb => cb(url));
|
||||
callback(url);
|
||||
});
|
||||
},
|
||||
|
@ -211,7 +198,7 @@ const DiscourseLocation = Ember.Object.extend({
|
|||
@param url {String}
|
||||
*/
|
||||
formatURL(url) {
|
||||
let rootURL = get(this, "rootURL");
|
||||
let rootURL = this.rootURL;
|
||||
|
||||
if (url !== "") {
|
||||
rootURL = rootURL.replace(/\/$/, "");
|
||||
|
@ -225,9 +212,10 @@ const DiscourseLocation = Ember.Object.extend({
|
|||
},
|
||||
|
||||
willDestroy() {
|
||||
const guid = Ember.guidFor(this);
|
||||
this._super(...arguments);
|
||||
|
||||
Ember.$(window).off("popstate.ember-location-" + guid);
|
||||
const guid = Ember.guidFor(this);
|
||||
$(window).off(`popstate.ember-location-${guid}`);
|
||||
}
|
||||
});
|
||||
|
||||
|
|
Loading…
Reference in New Issue