Skip to content

Commit

Permalink
refactor(content): function decomposition
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelo-lourenco committed Jun 21, 2024
1 parent f2b3c29 commit d41a497
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 101 deletions.
226 changes: 130 additions & 96 deletions content.js
Original file line number Diff line number Diff line change
@@ -1,122 +1,156 @@
// Function to add the button and configure the collapse behavior
function addCollapseButton() {
try {
// issue details
const containerRight = document.querySelector('[data-testid="issue.views.issue-details.issue-layout.container-right"]');
const resizerElement = document.querySelector('[data-testid="flex-resizer.ui.handle.resizer"]');
// Function to add the 'icon-collapse' class to the span element
function addIconCollapse(spanCollapseOpen) {
spanCollapseOpen.classList.remove('icon-open');
spanCollapseOpen.classList.add('icon-collapse');
}

if (containerRight && resizerElement && !document.getElementById('collapse-icon')) {
resizerElement.classList.add('resizer-width');
// Function to add the 'icon-open' class to the span element
function addIconOpen(spanCollapseOpen) {
spanCollapseOpen.classList.remove('icon-collapse');
spanCollapseOpen.classList.add('icon-open');
}

// Function to add the 'icon-shrink' class to the span element
function addIconShrink(spanShrinkExpand) {
spanShrinkExpand.classList.remove('icon-expand');
spanShrinkExpand.classList.add('icon-shrink');
}

// Function to add the 'icon-expand' class to the span element
function addIconExpand(spanShrinkExpand) {
spanShrinkExpand.classList.remove('icon-shrink');
spanShrinkExpand.classList.add('icon-expand');
}

// Function to handle the collapse/open behavior of the right container
function fnCollapseOpen(containerRight, spanCollapseOpen, modalIssueDetailsDialog, modalIssueDetailsDialogPositioner) {
if (containerRight.style.display === 'none') {
// If the container is hidden, show it and update the icon
addIconCollapse(spanCollapseOpen);
containerRight.style.display = 'block';

const collapseIcon = document.createElement('span');
collapseIcon.id = 'collapse-icon';
collapseIcon.classList.add('icon-close');
collapseIcon.style.cursor = 'pointer';

// issue details
const modalIssueDetailsDialogPositioner = document.querySelector('[data-testid="issue.views.issue-details.issue-modal.modal-dialog--positioner"]');
const modalIssueDetailsDialog = document.querySelector('[data-testid="issue.views.issue-details.issue-modal.modal-dialog"]');

// Add click behavior to icon
collapseIcon.addEventListener('click', function() {
if (containerRight.style.display === 'none') {

collapseIcon.classList.remove('icon-open');
collapseIcon.classList.add('icon-close');

containerRight.style.display = 'block';

if (modalIssueDetailsDialog){
modalIssueDetailsDialog.style.width = '';
Object.assign(modalIssueDetailsDialogPositioner.style, {
width: "", maxWidth: "", maxHeight: "", insetBlockStart: ""
});
}


} else {
collapseIcon.classList.remove('icon-close');
collapseIcon.classList.add('icon-open');

containerRight.style.display = 'none';

if (modalIssueDetailsDialog){
modalIssueDetailsDialog.style.width = '100%'
Object.assign(modalIssueDetailsDialogPositioner.style, {
width: "100%", maxWidth: "100%", maxHeight: "100%", insetBlockStart: "0px"
});
}

}
// If the modal dialog exists, reset its width and position
if (modalIssueDetailsDialog) {
modalIssueDetailsDialog.style.width = '';
Object.assign(modalIssueDetailsDialogPositioner.style, {
width: "", maxWidth: "", maxHeight: "", insetBlockStart: ""
});

// Add the button to the document
resizerElement.appendChild(collapseIcon);
}
} else {
// If the container is visible, hide it and update the icon
addIconOpen(spanCollapseOpen);
containerRight.style.display = 'none';

} catch (error) {
console.error('Error adding collapse button:', error);
// If the modal dialog exists, set its width and position to 100%
if (modalIssueDetailsDialog) {
modalIssueDetailsDialog.style.width = '100%';
Object.assign(modalIssueDetailsDialogPositioner.style, {
width: "100%", maxWidth: "100%", maxHeight: "100%", insetBlockStart: "0px"
});
}
}
}

// Function to handle the shrink/expand behavior of the issue create modal
function fnShrinkExpand(modalIssueCreate, spanShrinkExpand, modalIssueCreatePositioner) {
if (modalIssueCreate.style.width !== '100%') {
// If the modal is not full width, expand it and update the icon
addIconShrink(spanShrinkExpand);
modalIssueCreate.style.width = '100%';
Object.assign(modalIssueCreatePositioner.style, {
width: "100%", maxWidth: "100%", maxHeight: "100%", insetBlockStart: "0px"
});
} else {
// If the modal is full width, shrink it and update the icon
addIconExpand(spanShrinkExpand);
modalIssueCreate.style.width = "";
Object.assign(modalIssueCreatePositioner.style, {
width: "", maxWidth: "", maxHeight: "", insetBlockStart: "60px"
});
}
}

// Function to add the expand button to the issue create modal
function addExpandButton() {
try {
// issue create
// Get the necessary elements
const modalIssueCreatePositioner = document.querySelector('[data-testid="issue-create.ui.modal.modal-wrapper.modal--positioner"]');
const modalIssueCreate = document.querySelector('[data-testid="issue-create.ui.modal.modal-wrapper.modal"]');
const modalIcons = document.querySelector('[data-testid="minimizable-modal.ui.modal-container.modal-header.view-changer-wrapper"]');

if (modalIssueCreate && modalIcons && !document.getElementById('expand-icon')) {
const expandIcon = document.createElement('span');
expandIcon.id = 'expand-icon';
expandIcon.classList.add('icon-expand');
expandIcon.style.cursor = 'pointer';

// Add click behavior to icon
expandIcon.addEventListener('click', function() {
if (modalIssueCreate.style.width !== '100%') {

expandIcon.classList.remove('icon-expand');
expandIcon.classList.add('icon-shrink');

modalIssueCreate.style.width = '100%'
Object.assign(modalIssueCreatePositioner.style, {
width: "100%", maxWidth: "100%", maxHeight: "100%", insetBlockStart: "0px"
});
} else {
expandIcon.classList.remove('icon-shrink');
expandIcon.classList.add('icon-expand');

modalIssueCreate.style.width = "";
Object.assign(modalIssueCreatePositioner.style, {
width: "", maxWidth: "", maxHeight: "", insetBlockStart: "60px"
});
}
// If all elements are found and the button doesn't exist yet
if (modalIssueCreate && modalIcons && !document.getElementById('span-shrink-expand')) {
// Create the button element
const spanShrinkExpand = document.createElement('span');
spanShrinkExpand.id = 'span-shrink-expand';
addIconExpand(spanShrinkExpand);

// Start the modal in expanded state
fnShrinkExpand(modalIssueCreate, spanShrinkExpand, modalIssueCreatePositioner);

// Add click event listener to the button
spanShrinkExpand.addEventListener('click', function () {
fnShrinkExpand(modalIssueCreate, spanShrinkExpand, modalIssueCreatePositioner);
});
// Add the button to the document
modalIcons.appendChild(expandIcon);

// Append the button to the modal icons container
modalIcons.appendChild(spanShrinkExpand);
}

} catch (error) {
console.error('Error adding expand button:', error);
}
}

// Add button when loading page and when changing route in Jira
// Function to add the collapse button to the issue details container
function addCollapseButton() {
try {
// Get the necessary elements
const containerRight = document.querySelector('[data-testid="issue.views.issue-details.issue-layout.container-right"]');
const resizerElement = document.querySelector('[data-testid="flex-resizer.ui.handle.resizer"]');

// If all elements are found and the button doesn't exist yet
if (containerRight && resizerElement && !document.getElementById('span-collapse-open')) {
// Add a class to the resizer element
resizerElement.classList.add('resizer-width');

// Create the button element
const spanCollapseOpen = document.createElement('span');
spanCollapseOpen.id = 'span-collapse-open';
spanCollapseOpen.classList.add('icon-collapse');

// Get the modal dialog elements
const modalIssueDetailsDialogPositioner = document.querySelector('[data-testid="issue.views.issue-details.issue-modal.modal-dialog--positioner"]');
const modalIssueDetailsDialog = document.querySelector('[data-testid="issue.views.issue-details.issue-modal.modal-dialog"]');

// Start the container in maximized state
fnCollapseOpen(containerRight, spanCollapseOpen, modalIssueDetailsDialog, modalIssueDetailsDialogPositioner);

// Add click event listener to the button
spanCollapseOpen.addEventListener('click', function () {
fnCollapseOpen(containerRight, spanCollapseOpen, modalIssueDetailsDialog, modalIssueDetailsDialogPositioner);
});

// Append the button to the resizer element
resizerElement.appendChild(spanCollapseOpen);
}
} catch (error) {
console.error('Error adding collapse button:', error);
}
}

// Function to check if the necessary elements exist and add the buttons
function checkAndAddButtons() {
addExpandButton();
addCollapseButton();
}

// Add buttons when the page loads or when the route changes in Jira
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', () => {
addCollapseButton();
addExpandButton();
});
document.addEventListener('DOMContentLoaded', checkAndAddButtons);
} else {
addCollapseButton();
addExpandButton();
checkAndAddButtons();
}

// Observer for DOM changes
const observer = new MutationObserver(() => {
addCollapseButton();
addExpandButton();
});
// Observe for DOM changes and add buttons if necessary
const observer = new MutationObserver(checkAndAddButtons);
observer.observe(document.body, { childList: true, subtree: true });
Binary file removed icon64.png
Binary file not shown.
3 changes: 1 addition & 2 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"manifest_version": 3,
"version": "1.1",
"version": "1.1.1",
"default_locale": "en_US",
"description": "__MSG_extDesc__",
"name": "__MSG_extName__",
Expand All @@ -16,7 +16,6 @@
"icons": {
"16": "icon16.png",
"48": "icon48.png",
"64": "icon64.png",
"128": "icon128.png"
}
}
10 changes: 7 additions & 3 deletions styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
width: 30px;
}

#expand-icon {
#span-shrink-expand {
display: inline-block;
width: 24px;
height: 24px;
margin-top: -15px;
margin-right: -15px;
}

#collapse-icon {
#span-collapse-open {
display: inline-block;
width: 32px;
height: 32px;
Expand All @@ -24,15 +24,17 @@
background-repeat: no-repeat;
background-size: 100% 100%;
background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 32 32'%3E%3Cpath fill='%230052cc' d='M2 6v20a2 2 0 0 0 2 2h24a2 2 0 0 0 2-2V6a2 2 0 0 0-2-2H4a2 2 0 0 0-2 2m2 0h16v9H9.83l3.58-3.59L12 10l-6 6l6 6l1.41-1.41L9.83 17H20v9H4z'/%3E%3C/svg%3E");
cursor: pointer;
}

.icon-close {
.icon-collapse {
display: inline-block;
width: 32px;
height: 32px;
background-repeat: no-repeat;
background-size: 100% 100%;
background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 32 32'%3E%3Cpath fill='%230052cc' d='M2 6v20a2 2 0 0 0 2 2h24a2 2 0 0 0 2-2V6a2 2 0 0 0-2-2H4a2 2 0 0 0-2 2m2 0h16v20H4v-9h10.17l-3.58 3.59L12 22l6-6l-6-6l-1.41 1.41L14.17 15H4z'/%3E%3C/svg%3E");
cursor: pointer;
}

.icon-shrink {
Expand All @@ -42,6 +44,7 @@
background-repeat: no-repeat;
background-size: 100% 100%;
background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 14 14'%3E%3Cpath fill='none' stroke='%230052cc' stroke-linecap='round' stroke-linejoin='round' d='m.5 13.5l4-4M1 9.5h3.5V13m9 .5l-4-4m3.5 0H9.5V13M.5.5l4 4M1 4.5h3.5V1m9-.5l-4 4m3.5 0H9.5V1'/%3E%3C/svg%3E");
cursor: pointer;
}

.icon-expand {
Expand All @@ -51,4 +54,5 @@
background-repeat: no-repeat;
background-size: 100% 100%;
background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 14 14'%3E%3Cpath fill='none' stroke='%230052cc' stroke-linecap='round' stroke-linejoin='round' d='m8.5 5.5l5-5m-4 0h4v4m-8 1l-5-5m4 0h-4v4m8 4l5 5m-4 0h4v-4m-8-1l-5 5m4 0h-4v-4'/%3E%3C/svg%3E");
cursor: pointer;
}

0 comments on commit d41a497

Please sign in to comment.