Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

util-history.js 5.4KB

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