Skip to content

Commit

Permalink
Implement container reordering
Browse files Browse the repository at this point in the history
  • Loading branch information
sryze committed Jan 21, 2021
1 parent f4bcd30 commit 89719f7
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 4 deletions.
20 changes: 20 additions & 0 deletions src/css/popup.css
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,10 @@ h3.title {
max-inline-size: 300px;
}

.menu-item.drag-over td {
border-block-start: 2px solid var(--text-normal-color);
}

.disabled-menu-item {
color: grey;
cursor: default;
Expand Down Expand Up @@ -932,6 +936,22 @@ tr:hover > td > .trash-button {
display: block;
}

.move-button {
cursor: move;
display: inline-block;
height: 100%;
inline-size: 16px;
margin-block-end: 4px;
margin-block-start: 4px;
margin-inline-end: 10px;
margin-inline-start: auto;
text-align: center;
}

.move-button > img {
height: 16px;
}

@media (prefers-color-scheme: dark) {
:root {
--title-text-color: #fff;
Expand Down
13 changes: 13 additions & 0 deletions src/img/container-move.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
73 changes: 69 additions & 4 deletions src/js/popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ const DEFAULT_ICON = "circle";
const NEW_CONTAINER_ID = "new";

const ONBOARDING_STORAGE_KEY = "onboarding-stage";
const CONTAINER_ORDER_STORAGE_KEY = "container-order";
const CONTAINER_DRAG_DATA_TYPE = "firefox-container";

// List of panels
const P_ONBOARDING_1 = "onboarding1";
Expand Down Expand Up @@ -192,16 +194,29 @@ const Logic = {
elementToEnable.classList.remove("disabled-menu-item");
},

async saveContainerOrder(rows) {
const containerOrder = {};
rows.forEach((node, index) => {
return containerOrder[node.dataset.containerId] = index;
});
await browser.storage.local.set({
[CONTAINER_ORDER_STORAGE_KEY]: containerOrder
});
},

async refreshIdentities() {
const [identities, state] = await Promise.all([
const [identities, state, containerOrderStorage] = await Promise.all([
browser.contextualIdentities.query({}),
browser.runtime.sendMessage({
method: "queryIdentitiesState",
message: {
windowId: browser.windows.WINDOW_ID_CURRENT
}
})
}),
browser.storage.local.get([CONTAINER_ORDER_STORAGE_KEY])
]);
const containerOrder =
containerOrderStorage && containerOrderStorage[CONTAINER_ORDER_STORAGE_KEY];
this._identities = identities.map((identity) => {
const stateObject = state[identity.cookieStoreId];
if (stateObject) {
Expand All @@ -211,8 +226,11 @@ const Logic = {
identity.numberOfOpenTabs = stateObject.numberOfOpenTabs;
identity.isIsolated = stateObject.isIsolated;
}
if (containerOrder) {
identity.order = containerOrder[identity.cookieStoreId];
}
return identity;
});
}).sort((i1, i2) => i1.order - i2.order);
},

getPanelSelector(panel) {
Expand Down Expand Up @@ -1006,12 +1024,59 @@ Logic.registerPanel(MANAGE_CONTAINERS_PICKER, {
data-identity-color="${identity.color}">
</div>
</div>
<span class="menu-text">${identity.name}</span>`;
<span class="menu-text">${identity.name}</span>
<span class="move-button">
<img
class="pop-button-image"
src="/img/container-move.svg"
/>
</span>`;

fragment.appendChild(tr);

tr.appendChild(td);

tr.draggable = true;
tr.dataset.containerId = identity.cookieStoreId;
tr.addEventListener("dragstart", (e) => {
e.dataTransfer.setData(CONTAINER_DRAG_DATA_TYPE, identity.cookieStoreId);
});
tr.addEventListener("dragover", (e) => {
if (e.dataTransfer.types.includes(CONTAINER_DRAG_DATA_TYPE)) {
tr.classList.add("drag-over");
e.preventDefault();
}
});
tr.addEventListener("dragenter", (e) => {
if (e.dataTransfer.types.includes(CONTAINER_DRAG_DATA_TYPE)) {
e.preventDefault();
tr.classList.add("drag-over");
}
});
tr.addEventListener("dragleave", (e) => {
if (e.dataTransfer.types.includes(CONTAINER_DRAG_DATA_TYPE)) {
e.preventDefault();
tr.classList.remove("drag-over");
}
});
tr.addEventListener("drop", async (e) => {
e.preventDefault();
const parent = tr.parentNode;
const containerId = e.dataTransfer.getData(CONTAINER_DRAG_DATA_TYPE);
let droppedElement;
parent.childNodes.forEach((node) => {
if (node.dataset.containerId === containerId) {
droppedElement = node;
}
});
if (droppedElement && droppedElement !== tr) {
tr.classList.remove("drag-over");
parent.insertBefore(droppedElement, tr);
await Logic.saveContainerOrder(parent.childNodes);
await Logic.refreshIdentities();
}
});

Utils.addEnterHandler(tr, () => {
pickedFunction(identity);
});
Expand Down

0 comments on commit 89719f7

Please sign in to comment.