2019-09-25 19:19:42 +03:00
|
|
|
/**
|
2019-01-31 20:50:59 +03:00
|
|
|
* @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at>
|
|
|
|
*
|
|
|
|
* @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at>
|
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import _ from 'underscore'
|
|
|
|
import OC from './index'
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Utility class for the history API,
|
|
|
|
* includes fallback to using the URL hash when
|
|
|
|
* the browser doesn't support the history API.
|
|
|
|
*
|
|
|
|
* @namespace OC.Util.History
|
|
|
|
*/
|
|
|
|
export default {
|
|
|
|
|
|
|
|
_handlers: [],
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Push the current URL parameters to the history stack
|
|
|
|
* and change the visible URL.
|
|
|
|
* Note: this includes a workaround for IE8/IE9 that uses
|
|
|
|
* the hash part instead of the search part.
|
|
|
|
*
|
|
|
|
* @param {Object|string} params to append to the URL, can be either a string
|
|
|
|
* or a map
|
|
|
|
* @param {string} [url] URL to be used, otherwise the current URL will be used,
|
|
|
|
* using the params as query string
|
|
|
|
* @param {boolean} [replace=false] whether to replace instead of pushing
|
|
|
|
*/
|
2019-09-25 19:19:42 +03:00
|
|
|
_pushState: function(params, url, replace) {
|
2019-11-13 15:05:10 +03:00
|
|
|
let strParams
|
2019-01-31 20:50:59 +03:00
|
|
|
if (typeof (params) === 'string') {
|
2019-09-25 19:19:42 +03:00
|
|
|
strParams = params
|
2019-01-31 20:50:59 +03:00
|
|
|
} else {
|
2019-09-25 19:19:42 +03:00
|
|
|
strParams = OC.buildQueryString(params)
|
2019-01-31 20:50:59 +03:00
|
|
|
}
|
2019-09-25 19:19:42 +03:00
|
|
|
|
2019-01-31 20:50:59 +03:00
|
|
|
if (window.history.pushState) {
|
2019-09-25 19:19:42 +03:00
|
|
|
url = url || location.pathname + '?' + strParams
|
2019-01-31 20:50:59 +03:00
|
|
|
// Workaround for bug with SVG and window.history.pushState on Firefox < 51
|
|
|
|
// https://bugzilla.mozilla.org/show_bug.cgi?id=652991
|
2019-11-13 15:05:10 +03:00
|
|
|
const isFirefox = navigator.userAgent.toLowerCase().indexOf('firefox') > -1
|
2019-01-31 20:50:59 +03:00
|
|
|
if (isFirefox && parseInt(navigator.userAgent.split('/').pop()) < 51) {
|
2019-11-13 15:05:10 +03:00
|
|
|
const patterns = document.querySelectorAll('[fill^="url(#"], [stroke^="url(#"], [filter^="url(#invert"]')
|
|
|
|
for (let i = 0, ii = patterns.length, pattern; i < ii; i++) {
|
2019-09-25 19:19:42 +03:00
|
|
|
pattern = patterns[i]
|
2019-10-02 14:18:17 +03:00
|
|
|
// eslint-disable-next-line no-self-assign
|
|
|
|
pattern.style.fill = pattern.style.fill
|
|
|
|
// eslint-disable-next-line no-self-assign
|
|
|
|
pattern.style.stroke = pattern.style.stroke
|
2019-09-25 19:19:42 +03:00
|
|
|
pattern.removeAttribute('filter')
|
|
|
|
pattern.setAttribute('filter', 'url(#invert)')
|
2019-01-31 20:50:59 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (replace) {
|
2019-09-25 19:19:42 +03:00
|
|
|
window.history.replaceState(params, '', url)
|
2019-01-31 20:50:59 +03:00
|
|
|
} else {
|
2019-09-25 19:19:42 +03:00
|
|
|
window.history.pushState(params, '', url)
|
2019-01-31 20:50:59 +03:00
|
|
|
}
|
2019-09-25 19:19:42 +03:00
|
|
|
} else {
|
|
|
|
// use URL hash for IE8
|
|
|
|
window.location.hash = '?' + strParams
|
2019-01-31 20:50:59 +03:00
|
|
|
// inhibit next onhashchange that just added itself
|
|
|
|
// to the event queue
|
2019-09-25 19:19:42 +03:00
|
|
|
this._cancelPop = true
|
2019-01-31 20:50:59 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Push the current URL parameters to the history stack
|
|
|
|
* and change the visible URL.
|
|
|
|
* Note: this includes a workaround for IE8/IE9 that uses
|
|
|
|
* the hash part instead of the search part.
|
|
|
|
*
|
2019-09-25 19:19:42 +03:00
|
|
|
* @param {Object|string} params to append to the URL, can be either a string or a map
|
|
|
|
* @param {string} [url] URL to be used, otherwise the current URL will be used, using the params as query string
|
2019-01-31 20:50:59 +03:00
|
|
|
*/
|
2019-09-25 19:19:42 +03:00
|
|
|
pushState: function(params, url) {
|
|
|
|
this._pushState(params, url, false)
|
2019-01-31 20:50:59 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Push the current URL parameters to the history stack
|
|
|
|
* and change the visible URL.
|
|
|
|
* Note: this includes a workaround for IE8/IE9 that uses
|
|
|
|
* the hash part instead of the search part.
|
|
|
|
*
|
|
|
|
* @param {Object|string} params to append to the URL, can be either a string
|
|
|
|
* or a map
|
|
|
|
* @param {string} [url] URL to be used, otherwise the current URL will be used,
|
|
|
|
* using the params as query string
|
|
|
|
*/
|
2019-09-25 19:19:42 +03:00
|
|
|
replaceState: function(params, url) {
|
|
|
|
this._pushState(params, url, true)
|
2019-01-31 20:50:59 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a popstate handler
|
|
|
|
*
|
2019-09-25 19:19:42 +03:00
|
|
|
* @param {Function} handler handler
|
2019-01-31 20:50:59 +03:00
|
|
|
*/
|
2019-09-25 19:19:42 +03:00
|
|
|
addOnPopStateHandler: function(handler) {
|
|
|
|
this._handlers.push(handler)
|
2019-01-31 20:50:59 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse a query string from the hash part of the URL.
|
|
|
|
* (workaround for IE8 / IE9)
|
2019-09-25 19:19:42 +03:00
|
|
|
* @returns {string}
|
2019-01-31 20:50:59 +03:00
|
|
|
*/
|
2019-09-25 19:19:42 +03:00
|
|
|
_parseHashQuery: function() {
|
2019-11-13 15:05:10 +03:00
|
|
|
const hash = window.location.hash
|
|
|
|
const pos = hash.indexOf('?')
|
2019-01-31 20:50:59 +03:00
|
|
|
if (pos >= 0) {
|
2019-09-25 19:19:42 +03:00
|
|
|
return hash.substr(pos + 1)
|
2019-01-31 20:50:59 +03:00
|
|
|
}
|
|
|
|
if (hash.length) {
|
|
|
|
// remove hash sign
|
2019-09-25 19:19:42 +03:00
|
|
|
return hash.substr(1)
|
2019-01-31 20:50:59 +03:00
|
|
|
}
|
2019-09-25 19:19:42 +03:00
|
|
|
return ''
|
2019-01-31 20:50:59 +03:00
|
|
|
},
|
|
|
|
|
2019-09-25 19:19:42 +03:00
|
|
|
_decodeQuery: function(query) {
|
|
|
|
return query.replace(/\+/g, ' ')
|
2019-01-31 20:50:59 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse the query/search part of the URL.
|
|
|
|
* Also try and parse it from the URL hash (for IE8)
|
|
|
|
*
|
2019-09-25 19:19:42 +03:00
|
|
|
* @returns {Object} map of parameters
|
2019-01-31 20:50:59 +03:00
|
|
|
*/
|
2019-09-25 19:19:42 +03:00
|
|
|
parseUrlQuery: function() {
|
2019-11-13 15:05:10 +03:00
|
|
|
const query = this._parseHashQuery()
|
|
|
|
let params
|
2019-01-31 20:50:59 +03:00
|
|
|
// try and parse from URL hash first
|
|
|
|
if (query) {
|
2019-09-25 19:19:42 +03:00
|
|
|
params = OC.parseQueryString(this._decodeQuery(query))
|
2019-01-31 20:50:59 +03:00
|
|
|
}
|
|
|
|
// else read from query attributes
|
2019-09-25 19:19:42 +03:00
|
|
|
params = _.extend(params || {}, OC.parseQueryString(this._decodeQuery(location.search)))
|
|
|
|
return params || {}
|
2019-01-31 20:50:59 +03:00
|
|
|
},
|
|
|
|
|
2019-09-25 19:19:42 +03:00
|
|
|
_onPopState: function(e) {
|
2019-01-31 20:50:59 +03:00
|
|
|
if (this._cancelPop) {
|
2019-09-25 19:19:42 +03:00
|
|
|
this._cancelPop = false
|
|
|
|
return
|
2019-01-31 20:50:59 +03:00
|
|
|
}
|
2019-11-13 15:05:10 +03:00
|
|
|
let params
|
2019-01-31 20:50:59 +03:00
|
|
|
if (!this._handlers.length) {
|
2019-09-25 19:19:42 +03:00
|
|
|
return
|
2019-01-31 20:50:59 +03:00
|
|
|
}
|
2019-09-25 19:19:42 +03:00
|
|
|
params = (e && e.state)
|
2019-01-31 20:50:59 +03:00
|
|
|
if (_.isString(params)) {
|
2019-09-25 19:19:42 +03:00
|
|
|
params = OC.parseQueryString(params)
|
2019-01-31 20:50:59 +03:00
|
|
|
} else if (!params) {
|
2019-09-25 19:19:42 +03:00
|
|
|
params = this.parseUrlQuery() || {}
|
2019-01-31 20:50:59 +03:00
|
|
|
}
|
2019-11-13 15:05:10 +03:00
|
|
|
for (let i = 0; i < this._handlers.length; i++) {
|
2019-09-25 19:19:42 +03:00
|
|
|
this._handlers[i](params)
|
2019-01-31 20:50:59 +03:00
|
|
|
}
|
2019-11-13 15:05:10 +03:00
|
|
|
},
|
2019-01-31 20:50:59 +03:00
|
|
|
}
|