Skip to content

Commit

Permalink
Fix for cross-window dragging issue. Fixes #6
Browse files Browse the repository at this point in the history
  • Loading branch information
Bill13579 committed Apr 26, 2018
1 parent b064f86 commit 7f0bf93
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 4 deletions.
11 changes: 11 additions & 0 deletions src/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ var rightToWrong = {};

browser.tabs.onAttached.addListener(fixOnAttached);
browser.tabs.onRemoved.addListener(fixOnRemoved);
browser.runtime.onMessage.addListener(onMessage);

function fixOnAttached(tabId, attachInfo) {
browser.tabs.get(tabId).then(function (tab){
Expand All @@ -52,6 +53,16 @@ function getCorrectTabId(tabId) {
return wrongToRight[tabId] || tabId;
}

function onMessage(request, sender, sendResponse) {
switch (request.msg) {
case "WRONG_TO_RIGHT_GET":
sendResponse({
wrongToRight: wrongToRight
});
break;
}
}

// Watch out for any changes in tabs
browser.tabs.onUpdated.addListener(tabUpdated);
browser.tabs.onActivated.addListener(tabActivated);
Expand Down
43 changes: 39 additions & 4 deletions src/popup/popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,20 @@ if (!browser.runtime.onMessage.hasListener(onMessage)) {
browser.runtime.onMessage.addListener(onMessage);
}

// Function to send a message
function sendMessage(msg, details) {
return browser.runtime.sendMessage({
msg: msg,
details: details
});
}

function getWrongToRight() {
return sendMessage("WRONG_TO_RIGHT_GET", {}).then(function (response){
return response.wrongToRight;
});
}

function onMessage(request, sender, sendResponse) {
switch (request.msg) {
case "ACTIVE_TAB_CHANGED":
Expand Down Expand Up @@ -75,6 +89,13 @@ function getFavIconFromTabEntry(entry) {
return entry.querySelector(".tab-entry-favicon")[0];
}

// Find correct tab entry by tab id
function findCorrectTabEntryById(tabId) {
return getWrongToRight().then(function (wrongToRight){
return findTabEntryById(getCorrectTabId(tabId, wrongToRight));
});
}

// Find tab entry by tab id
function findTabEntryById(tabId) {
return document.querySelector(".tab-entry[data-tab_id=\"" + tabId + "\"]");
Expand Down Expand Up @@ -125,7 +146,9 @@ function removeTab(tabId, windowId) {
active: true,
windowId: windowId
}).then(function (tabs){
findTabEntryById(tabs[0].id).classList.add("current-tab");
findCorrectTabEntryById(tabs[0].id).then(function (tab){
tab.classList.add("current-tab");
});
});
}

Expand Down Expand Up @@ -157,8 +180,20 @@ function removeWindow(windowId) {
});
}

// Update tabs initiator
function updateTabsStarter(windows) {
getWrongToRight().then(function (wrongToRight){
updateTabs(windows, wrongToRight);
});
}

// Function to get correct tab id
function getCorrectTabId(tabId, wrongToRight) {
return wrongToRight[tabId] || tabId;
}

// Update tabs
function updateTabs(windows) {
function updateTabs(windows, wrongToRight) {
tabs_list.innerHTML = "";
let windowEntry;
let currentWindowEntry;
Expand Down Expand Up @@ -279,7 +314,7 @@ function updateTabs(windows) {
buttons.appendChild(closeBtn);

// Set tab entry tab id
tabEntry.setAttribute("data-tab_id", tab.id);
tabEntry.setAttribute("data-tab_id", getCorrectTabId(tab.id, wrongToRight));
tabEntry.appendChild(buttons);
if (favicon !== null) {
tabEntry.appendChild(favicon);
Expand Down Expand Up @@ -312,7 +347,7 @@ function updateTabs(windows) {
// Add tabs to list
function addTabs() {
getWindows().then(function (windows) {
updateTabs(windows);
updateTabsStarter(windows);
});
}

Expand Down

0 comments on commit 7f0bf93

Please sign in to comment.