From 7475e05f997c780c07111681a78d510930ad2e9a Mon Sep 17 00:00:00 2001 From: Cesar G Date: Mon, 5 May 2014 15:08:18 -0700 Subject: [PATCH 1/2] [ticket/12440] Set browser URL to point to specific post when using view=unread PHPBB3-12440 --- phpBB/assets/javascript/core.js | 16 ++++++++++++++++ .../prosilver/template/viewtopic_body.html | 2 +- .../subsilver2/template/viewtopic_body.html | 4 ++-- 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/phpBB/assets/javascript/core.js b/phpBB/assets/javascript/core.js index f461d5a175..0b46a1e937 100644 --- a/phpBB/assets/javascript/core.js +++ b/phpBB/assets/javascript/core.js @@ -1425,6 +1425,20 @@ phpbb.getFunctionByName = function (functionName) { return context[func]; }; +/** +* Update browser history URL to point to specific post in viewtopic.php +* when using view=unread#unread link. +* +* @return undefined +*/ +phpbb.setUnreadUrl = function() { + var url = $('#unread[data-url]').data('url'); + + if (url && typeof history !== 'undefined' && typeof history.replaceState !== 'undefined') { + history.replaceState(null, document.title, url); + } +}; + /** * Apply code editor to all textarea elements with data-bbcode attribute */ @@ -1444,6 +1458,8 @@ $(document).ready(function() { $('#color_palette_placeholder').each(function() { phpbb.registerPalette($(this)); }); + + phpbb.setUnreadUrl(); }); })(jQuery); // Avoid conflicts with other libraries diff --git a/phpBB/styles/prosilver/template/viewtopic_body.html b/phpBB/styles/prosilver/template/viewtopic_body.html index ed45835da6..f2c553d015 100644 --- a/phpBB/styles/prosilver/template/viewtopic_body.html +++ b/phpBB/styles/prosilver/template/viewtopic_body.html @@ -115,7 +115,7 @@ - +
diff --git a/phpBB/styles/subsilver2/template/viewtopic_body.html b/phpBB/styles/subsilver2/template/viewtopic_body.html index 6ac4e0ea33..f5dab205f0 100644 --- a/phpBB/styles/subsilver2/template/viewtopic_body.html +++ b/phpBB/styles/subsilver2/template/viewtopic_body.html @@ -141,7 +141,7 @@ - + @@ -155,7 +155,7 @@ - + From ed14d12d148b1aa7e726121afa9ad79718745e1c Mon Sep 17 00:00:00 2001 From: Cesar G Date: Tue, 6 May 2014 13:34:12 -0700 Subject: [PATCH 2/2] [ticket/12440] Use a more generic approach for replacing the URL. PHPBB3-12440 --- phpBB/assets/javascript/core.js | 86 +++++++++++++++++++++++++++------ 1 file changed, 71 insertions(+), 15 deletions(-) diff --git a/phpBB/assets/javascript/core.js b/phpBB/assets/javascript/core.js index 0b46a1e937..ae8583ddbf 100644 --- a/phpBB/assets/javascript/core.js +++ b/phpBB/assets/javascript/core.js @@ -694,6 +694,74 @@ $('#phpbb').click(function(e) { } }); +phpbb.history = {}; + +/** +* Check whether a method in the native history object is supported. +* +* @param string fn Method name. +* @return bool Returns true if the method is supported. +*/ +phpbb.history.isSupported = function(fn) { + if (typeof history === 'undefined' || typeof history[fn] === 'undefined') { + return false; + } + return true; +}; + +/** +* Wrapper for the pushState and replaceState methods of the +* native history object. +* +* @param string mode Mode. Either push or replace. +* @param string url New URL. +* @param string title Optional page title. +* @patam object obj Optional state object. +* +* @return undefined +*/ +phpbb.history.alterUrl = function(mode, url, title, obj) { + var fn = mode + 'State'; + + if (!url || !phpbb.history.isSupported(fn)) { + return; + } + if (!title) { + title = document.title; + } + if (!obj) { + obj = null; + } + + history[fn](obj, title, url); +}; + +/** +* Wrapper for the native history.replaceState method. +* +* @param string url New URL. +* @param string title Optional page title. +* @patam object obj Optional state object. +* +* @return undefined +*/ +phpbb.history.replaceUrl = function(url, title, obj) { + phpbb.history.alterUrl('replace', url, title, obj); +}; + +/** +* Wrapper for the native history.pushState method. +* +* @param string url New URL. +* @param string title Optional page title. +* @patam object obj Optional state object. +* +* @return undefined +*/ +phpbb.history.pushUrl = function(url, title, obj) { + phpbb.history.alterUrl('push', url, title, obj); +}; + /** * Hide the optgroups that are not the selected timezone * @@ -1425,20 +1493,6 @@ phpbb.getFunctionByName = function (functionName) { return context[func]; }; -/** -* Update browser history URL to point to specific post in viewtopic.php -* when using view=unread#unread link. -* -* @return undefined -*/ -phpbb.setUnreadUrl = function() { - var url = $('#unread[data-url]').data('url'); - - if (url && typeof history !== 'undefined' && typeof history.replaceState !== 'undefined') { - history.replaceState(null, document.title, url); - } -}; - /** * Apply code editor to all textarea elements with data-bbcode attribute */ @@ -1459,7 +1513,9 @@ $(document).ready(function() { phpbb.registerPalette($(this)); }); - phpbb.setUnreadUrl(); + // Update browser history URL to point to specific post in viewtopic.php + // when using view=unread#unread link. + phpbb.history.replaceUrl($('#unread[data-url]').data('url')); }); })(jQuery); // Avoid conflicts with other libraries