From 67fe43e9a9691bf88ea0edd570d5076152d77974 Mon Sep 17 00:00:00 2001 From: tophf Date: Wed, 10 Jun 2015 15:55:32 +0300 Subject: [PATCH] Editor: use history back for Back-To-Manage when appropriate --- edit.js | 31 +++++++++++++++++++++++-------- manage.js | 10 +++++++--- 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/edit.js b/edit.js index 5a239699..c516828b 100644 --- a/edit.js +++ b/edit.js @@ -4,6 +4,7 @@ var styleId = null; var dirty = {}; // only the actually dirty items here var editors = []; // array of all CodeMirror instances var saveSizeOnClose; +var useHistoryBack; // use browser history back when "back to manage" is clicked // direct & reverse mapping of @-moz-document keywords and internal property names var propertyToCss = {urls: "url", urlPrefixes: "url-prefix", domains: "domain", regexps: "regexp"}; @@ -404,21 +405,34 @@ document.addEventListener("wheel", function(event) { } }); -if (prefs.getPref("openEditInWindow")) { - chrome.tabs.query({currentWindow: true}, function(tabs) { - var windowId = tabs[0].windowId; +chrome.tabs.query({currentWindow: true}, function(tabs) { + var windowId = tabs[0].windowId; + if (prefs.getPref("openEditInWindow")) { if (tabs.length == 1 && window.history.length == 1) { sessionStorageHash("saveSizeOnClose").set(windowId, true); saveSizeOnClose = true; } else { saveSizeOnClose = sessionStorageHash("saveSizeOnClose").value[windowId]; } - chrome.tabs.onRemoved.addListener(function(tabId, info) { - if (info.windowId == windowId && info.isWindowClosing) { - sessionStorageHash("saveSizeOnClose").unset(windowId); - } - }); + } + chrome.tabs.onRemoved.addListener(function(tabId, info) { + sessionStorageHash("manageStylesHistory").unset(tabId); + if (info.windowId == windowId && info.isWindowClosing) { + sessionStorageHash("saveSizeOnClose").unset(windowId); + } }); +}); + +getActiveTab(function(tab) { + useHistoryBack = sessionStorageHash("manageStylesHistory").value[tab.id] == location.href; +}); + +function goBackToManage(event) { + if (useHistoryBack) { + event.stopPropagation(); + event.preventDefault(); + history.back(); + } } window.onbeforeunload = function() { @@ -791,6 +805,7 @@ function initHooks() { document.getElementById("save-button").addEventListener("click", save, false); document.getElementById("sections-help").addEventListener("click", showSectionHelp, false); document.getElementById("keyMap-help").addEventListener("click", showKeyMapHelp, false); + document.getElementById("cancel-button").addEventListener("click", goBackToManage); setupGlobalSearch(); setCleanGlobal(); diff --git a/manage.js b/manage.js index 67fbb0f7..3e67b9d4 100644 --- a/manage.js +++ b/manage.js @@ -121,10 +121,10 @@ function createStyleElement(style) { var openWindow = left && shift && !ctrl; var openBackgroundTab = (middle && !shift) || (left && ctrl && !shift); var openForegroundTab = (middle && shift) || (left && ctrl && shift); + var url = event.target.href || event.target.parentNode.href; + event.preventDefault(); + event.stopPropagation(); if (openWindow || openBackgroundTab || openForegroundTab) { - event.preventDefault(); - event.stopPropagation(); - var url = event.target.href || event.target.parentNode.href; if (openWindow) { var options = prefs.getPref('windowPosition', {}); options.url = url; @@ -138,6 +138,10 @@ function createStyleElement(style) { } } else { history.replaceState({scrollY: window.scrollY}, document.title); + getActiveTab(function(tab) { + sessionStorageHash("manageStylesHistory").set(tab.id, url); + location.href = url; + }); } } });