You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

util-history.js 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /**
  2. * @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at>
  3. *
  4. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  5. * @author John Molakvoæ <skjnldsv@protonmail.com>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. import _ from 'underscore'
  24. import OC from './index'
  25. /**
  26. * Utility class for the history API,
  27. * includes fallback to using the URL hash when
  28. * the browser doesn't support the history API.
  29. *
  30. * @namespace OC.Util.History
  31. */
  32. export default {
  33. _handlers: [],
  34. /**
  35. * Push the current URL parameters to the history stack
  36. * and change the visible URL.
  37. * Note: this includes a workaround for IE8/IE9 that uses
  38. * the hash part instead of the search part.
  39. *
  40. * @param {object | string} params to append to the URL, can be either a string
  41. * or a map
  42. * @param {string} [url] URL to be used, otherwise the current URL will be used,
  43. * using the params as query string
  44. * @param {boolean} [replace=false] whether to replace instead of pushing
  45. */
  46. _pushState(params, url, replace) {
  47. let strParams
  48. if (typeof (params) === 'string') {
  49. strParams = params
  50. } else {
  51. strParams = OC.buildQueryString(params)
  52. }
  53. if (window.history.pushState) {
  54. url = url || location.pathname + '?' + strParams
  55. // Workaround for bug with SVG and window.history.pushState on Firefox < 51
  56. // https://bugzilla.mozilla.org/show_bug.cgi?id=652991
  57. const isFirefox = navigator.userAgent.toLowerCase().indexOf('firefox') > -1
  58. if (isFirefox && parseInt(navigator.userAgent.split('/').pop()) < 51) {
  59. const patterns = document.querySelectorAll('[fill^="url(#"], [stroke^="url(#"], [filter^="url(#invert"]')
  60. for (let i = 0, ii = patterns.length, pattern; i < ii; i++) {
  61. pattern = patterns[i]
  62. // eslint-disable-next-line no-self-assign
  63. pattern.style.fill = pattern.style.fill
  64. // eslint-disable-next-line no-self-assign
  65. pattern.style.stroke = pattern.style.stroke
  66. pattern.removeAttribute('filter')
  67. pattern.setAttribute('filter', 'url(#invert)')
  68. }
  69. }
  70. if (replace) {
  71. window.history.replaceState(params, '', url)
  72. } else {
  73. window.history.pushState(params, '', url)
  74. }
  75. } else {
  76. // use URL hash for IE8
  77. window.location.hash = '?' + strParams
  78. // inhibit next onhashchange that just added itself
  79. // to the event queue
  80. this._cancelPop = true
  81. }
  82. },
  83. /**
  84. * Push the current URL parameters to the history stack
  85. * and change the visible URL.
  86. * Note: this includes a workaround for IE8/IE9 that uses
  87. * the hash part instead of the search part.
  88. *
  89. * @param {object | string} params to append to the URL, can be either a string or a map
  90. * @param {string} [url] URL to be used, otherwise the current URL will be used, using the params as query string
  91. */
  92. pushState(params, url) {
  93. this._pushState(params, url, false)
  94. },
  95. /**
  96. * Push the current URL parameters to the history stack
  97. * and change the visible URL.
  98. * Note: this includes a workaround for IE8/IE9 that uses
  99. * the hash part instead of the search part.
  100. *
  101. * @param {object | string} params to append to the URL, can be either a string
  102. * or a map
  103. * @param {string} [url] URL to be used, otherwise the current URL will be used,
  104. * using the params as query string
  105. */
  106. replaceState(params, url) {
  107. this._pushState(params, url, true)
  108. },
  109. /**
  110. * Add a popstate handler
  111. *
  112. * @param {Function} handler handler
  113. */
  114. addOnPopStateHandler(handler) {
  115. this._handlers.push(handler)
  116. },
  117. /**
  118. * Parse a query string from the hash part of the URL.
  119. * (workaround for IE8 / IE9)
  120. *
  121. * @return {string}
  122. */
  123. _parseHashQuery() {
  124. const hash = window.location.hash
  125. const pos = hash.indexOf('?')
  126. if (pos >= 0) {
  127. return hash.substr(pos + 1)
  128. }
  129. if (hash.length) {
  130. // remove hash sign
  131. return hash.substr(1)
  132. }
  133. return ''
  134. },
  135. _decodeQuery(query) {
  136. return query.replace(/\+/g, ' ')
  137. },
  138. /**
  139. * Parse the query/search part of the URL.
  140. * Also try and parse it from the URL hash (for IE8)
  141. *
  142. * @return {object} map of parameters
  143. */
  144. parseUrlQuery() {
  145. const query = this._parseHashQuery()
  146. let params
  147. // try and parse from URL hash first
  148. if (query) {
  149. params = OC.parseQueryString(this._decodeQuery(query))
  150. }
  151. // else read from query attributes
  152. params = _.extend(params || {}, OC.parseQueryString(this._decodeQuery(location.search)))
  153. return params || {}
  154. },
  155. _onPopState(e) {
  156. if (this._cancelPop) {
  157. this._cancelPop = false
  158. return
  159. }
  160. let params
  161. if (!this._handlers.length) {
  162. return
  163. }
  164. params = (e && e.state)
  165. if (_.isString(params)) {
  166. params = OC.parseQueryString(params)
  167. } else if (!params) {
  168. params = this.parseUrlQuery() || {}
  169. }
  170. for (let i = 0; i < this._handlers.length; i++) {
  171. this._handlers[i](params)
  172. }
  173. },
  174. }