diff options
author | Vincent Petry <pvince81@owncloud.com> | 2014-05-08 22:06:30 +0200 |
---|---|---|
committer | Vincent Petry <pvince81@owncloud.com> | 2014-05-15 17:51:04 +0200 |
commit | 9d38e3602b2faf37d861729c52690ce51b8fee97 (patch) | |
tree | 5da63d26db4a4e8ec356dee45fc8f7804c6fe38a /core/js/js.js | |
parent | fb10bf4048aaf5b2a9665fc9dff217c790efe005 (diff) | |
download | nextcloud-server-9d38e3602b2faf37d861729c52690ce51b8fee97.tar.gz nextcloud-server-9d38e3602b2faf37d861729c52690ce51b8fee97.zip |
Namespacing for FileList, FileActions and trashbin app
- FileList is now an instantiable class
- FileActions is now in namespace
- added App class for trashbin app
- moved trashbin overrides into classes extending FileList
- replaced many static calls with "this." or "self." to make the classes
reusable/extendable
- new URL parameter "view" to specify which view is shown, for example
"files" or "trashbin"
- added OC.Util.History utility class in core for handling history
- moved URL handling/routing to OCA.Files.App
- popstate will correctly update the current view and notify the view of
the URL change so it can update the current dir
- added JS unitt tests for the trashbin app
- fixed public app to work with the new namespaces
Diffstat (limited to 'core/js/js.js')
-rw-r--r-- | core/js/js.js | 108 |
1 files changed, 108 insertions, 0 deletions
diff --git a/core/js/js.js b/core/js/js.js index 93f4196f056..38b97590430 100644 --- a/core/js/js.js +++ b/core/js/js.js @@ -1319,6 +1319,114 @@ OC.Util = { }; /** + * Utility class for the history API, + * includes fallback to using the URL hash when + * the browser doesn't support the history API. + */ +OC.Util.History = { + _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 params to append to the URL, can be either a string + * or a map + */ + pushState: function(params) { + var strParams; + if (typeof(params) === 'string') { + strParams = params; + } + else { + strParams = OC.buildQueryString(params); + } + if (window.history.pushState) { + var url = location.pathname + '?' + strParams; + window.history.pushState(params, '', url); + } + // use URL hash for IE8 + else { + window.location.hash = '?' + strParams; + // inhibit next onhashchange that just added itself + // to the event queue + this._cancelPop = true; + } + }, + + /** + * Add a popstate handler + * + * @param handler function + */ + addOnPopStateHandler: function(handler) { + this._handlers.push(handler); + }, + + /** + * Parse a query string from the hash part of the URL. + * (workaround for IE8 / IE9) + */ + _parseHashQuery: function() { + var hash = window.location.hash, + pos = hash.indexOf('?'); + if (pos >= 0) { + return hash.substr(pos + 1); + } + return ''; + }, + + _decodeQuery: function(query) { + return query.replace(/\+/g, ' '); + }, + + /** + * Parse the query/search part of the URL. + * Also try and parse it from the URL hash (for IE8) + * + * @return map of parameters + */ + parseUrlQuery: function() { + var query = this._parseHashQuery(), + params; + // try and parse from URL hash first + if (query) { + params = OC.parseQueryString(this._decodeQuery(query)); + } + // else read from query attributes + if (!params) { + params = OC.parseQueryString(this._decodeQuery(location.search)); + } + return params || {}; + }, + + _onPopState: function(e) { + if (this._cancelPop) { + this._cancelPop = false; + return; + } + var params; + if (!this._handlers.length) { + return; + } + params = (e && e.state) || this.parseUrlQuery() || {}; + for (var i = 0; i < this._handlers.length; i++) { + this._handlers[i](params); + } + } +}; + +// fallback to hashchange when no history support +if (window.history.pushState) { + window.onpopstate = _.bind(OC.Util.History._onPopState, OC.Util.History); +} +else { + $(window).on('hashchange', _.bind(OC.Util.History._onPopState, OC.Util.History)); +} + +/** * Get a variable by name * @param {string} name * @return {*} |