Skip to content

Commit

Permalink
Add function to delete window or tab on saved session
Browse files Browse the repository at this point in the history
  • Loading branch information
sienori committed Apr 1, 2018
1 parent 99da5ee commit 12b592a
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 7 deletions.
3 changes: 3 additions & 0 deletions Tab-Session-Manager/background/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ function onMessageListener(request, sender, sendResponse) {
case "rename":
renameSession(request.id, request.name);
break;
case "update":
updateSession(request.session, false);
break;
case "import":
importSessions(request.importSessions);
break;
Expand Down
4 changes: 2 additions & 2 deletions Tab-Session-Manager/background/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,10 @@ async function removeSession(id, isTemp = false) {
} catch (e) {}
}

async function updateSession(session) {
async function updateSession(session, isSend = true) {
try {
await Sessions.put(session);
sendMessage('updateSession', session.id);
if (isSend) sendMessage('updateSession', session.id);
} catch (e) {}
}

Expand Down
37 changes: 36 additions & 1 deletion Tab-Session-Manager/popup/popup.css
Original file line number Diff line number Diff line change
Expand Up @@ -641,13 +641,47 @@ option {

.windowTitle {
padding-left: 8px;
cursor: pointer
cursor: pointer;
flex-grow: 1;
}

.windowTitle:hover {
text-decoration: underline;
}

.windowDeleteButton,
.tabDeleteButton {
display: flex;
align-items: center;
justify-content: center;
height: 16px;
width: 16px;
cursor: pointer;
visibility: hidden;
opacity: 0;
transition: all 100ms;
}

.windowTitleContainer:hover .windowDeleteButton,
.tabContainer:hover .tabDeleteButton {
visibility: visible;
opacity: 1;
}

.windowDeleteButton svg,
.tabDeleteButton svg {
width: 12px;
height: 12px;
fill: var(--sub-text);
transform: rotate(45deg);
transition: all 200ms;
}

.windowDeleteButton:hover svg,
.tabDeleteButton:hover svg {
fill: var(--confirm);
}

.tabContainer {
display: flex;
flex-direction: row;
Expand Down Expand Up @@ -682,6 +716,7 @@ option {

.tabTitle {
padding-left: 8px;
flex-grow: 1;
text-overflow: ellipsis;
white-space: nowrap;
overflow: hidden;
Expand Down
75 changes: 71 additions & 4 deletions Tab-Session-Manager/popup/popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -461,22 +461,27 @@ function showSessions(sessions, isInit = true) {
sortChange();
}

async function showDetail(e) {
async function showDetail(e, overwrite = false) {
const sessionId = getParentSessionId(e.target);
const detail = document.getElementById(sessionId).getElementsByClassName("detailItems")[0];
const session = await getSessions(sessionId);
if (session == undefined) return;

if (detail.classList.contains("hidden")) {
if (detail.classList.contains("hidden") || overwrite) {
detail.innerHTML = "";
let i = 0;
for (let win in session.windows) {
i++;
detail.insertAdjacentHTML('beforeend',
`<ul class="windowContainer">
<li class="windowTitleContainer hidden">
<li class="windowTitleContainer ${overwrite? '':'hidden'}">
<div class="windowIcon"></div>
<span class=windowTitle data-windowid="${win}" title="${Labels.open}">${Labels.windowLabel} ${i}</span>
<div class=windowDeleteButton data-windowid="${win}" title="${Labels.remove}">
<svg>
<use xlink:href="#plusSvg"></use>
</svg>
</div>
</li>
</ul>`);

Expand All @@ -495,11 +500,16 @@ async function showDetail(e) {
if (tabFavIconUrl == undefined || tabFavIconUrl.match(/^chrome:\/\//))
tabFavIconUrl = "/icons/favicon.png";
const tabHtml =
`<li class="tabContainer hidden">
`<li class="tabContainer ${overwrite? '':'hidden'}">
<div class=fav style="background-image:url(${sanitaize.encode(tabFavIconUrl)})"></div>
<div class=tabTitle data-url="${sanitaize.encode(tabUrl)}" title="${sanitaize.encode(tabTitle)}&#10;${sanitaize.encode(tabUrl)}">
${sanitaize.encode(tabTitle)}
</div>
<div class=tabDeleteButton data-windowid="${win}" data-tabid="${tab}" title="${Labels.remove}">
<svg>
<use xlink:href="#plusSvg"></use>
</svg>
</div>
</li>`;

windowContainer.insertAdjacentHTML('beforeend', tabHtml);
Expand Down Expand Up @@ -737,6 +747,57 @@ function openUrl(url, title = '') {
});
}

async function deleteWindowTab(e, target) {
const id = getParentSessionId(e.target);
let session = await getSessions(id);
const winId = e.target.dataset.windowid;

switch (target) {
case 'window':
const deletedWindow = session.windows[winId];
delete session.windows[winId];
if (session.windowsInfo != undefined) delete session.windowsInfo[winId];

session.windowsNumber--;
session.tabsNumber -= Object.keys(deletedWindow).length;
break;
case 'tab':
const tabId = e.target.dataset.tabid;
const deletedTab = session.windows[winId][tabId];
delete session.windows[winId][tabId];
if (session.windowsInfo != undefined) delete session.windowsInfo[winId][tabId];

if (Object.keys(session.windows[winId]).length == 0) {
deleteWindowTab(e, 'window');
return;
}

const window = session.windows[deletedTab.windowId];
for (let tab in window) {
//openerTabIdを削除
if (window[tab].openerTabId != undefined) {
if (window[tab].openerTabId == deletedTab.id) delete window[tab].openerTabId;
}
//indexを変更
if (window[tab].index > deletedTab.index) window[tab].index--;
}
session.tabsNumber--;
break;
}

if (session.tabsNumber == 0) return;

browser.runtime.sendMessage({
message: 'update',
session: session
});

const detail = document.getElementById(id).getElementsByClassName('detail')[0];
const detailText = `${session.windowsNumber} ${(session.windowsNumber==1)?Labels.windowLabel:Labels.windowsLabel} - ${session.tabsNumber} ${(session.tabsNumber==1)?Labels.tabLabel:Labels.tabsLabel}`;
detail.innerText = detailText;
showDetail(e, true);
}

document.addEventListener('click', async function (e) {
hideAllPopupMenu(e);
switch (e.target.id) {
Expand Down Expand Up @@ -778,6 +839,12 @@ document.addEventListener('click', async function (e) {
const windowId = e.target.dataset.windowid;
sendOpenMessage(e, "openInNewWindow", windowId);
break;
case "windowDeleteButton":
deleteWindowTab(e, 'window');
break;
case 'tabDeleteButton':
deleteWindowTab(e, 'tab');
break;
case "tabTitle":
const url = e.target.dataset.url;
const title = e.target.innerText;
Expand Down

0 comments on commit 12b592a

Please sign in to comment.