Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for not closeable tabs #45

Open
wants to merge 4 commits into
base: gh-pages
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,10 @@
<div class="buttons">
<button data-theme-toggle>Toggle dark theme</button>
<button data-add-tab>Add new tab</button>
<button data-add-not-closeable-tab>Add new not closeable tab</button>
<button data-add-background-tab>Add tab in the background</button>
<button data-remove-tab>Remove active tab</button>
<button data-remove-all>Remove all tabs</button>
</div>
</div>
</div>
Expand Down Expand Up @@ -108,9 +110,25 @@
})
})

document.querySelector('button[data-add-not-closeable-tab]').addEventListener('click', _ => {
chromeTabs.addTab({
title: 'New Tab',
favicon: false
}, {
closeable: false
})
})

document.querySelector('button[data-remove-tab]').addEventListener('click', _ => {
chromeTabs.removeTab(chromeTabs.activeTabEl)
})


document.querySelector('button[data-remove-all]').addEventListener('click', _ => {
chromeTabs.tabEls.forEach(element => {
chromeTabs.removeTab(element)
});
})

document.querySelector('button[data-theme-toggle]').addEventListener('click', _ => {
if (el.classList.contains('chrome-tabs-dark-theme')) {
Expand Down
15 changes: 11 additions & 4 deletions js/chrome-tabs.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,14 +179,17 @@
this.styleEl.innerHTML = styleHTML
}

createNewTabEl() {
createNewTabEl(closeable) {
const div = document.createElement('div')
div.innerHTML = tabTemplate
if(!closeable) {
div.querySelector('.chrome-tab-content').removeChild(div.querySelector('.chrome-tab-close'))
}
return div.firstElementChild
}

addTab(tabProperties, { animate = true, background = false } = {}) {
const tabEl = this.createNewTabEl()
addTab(tabProperties, { animate = true, background = false, closeable = true } = {}) {
const tabEl = this.createNewTabEl(closeable)

if (animate) {
tabEl.classList.add('chrome-tab-was-just-added')
Expand All @@ -202,10 +205,14 @@
this.cleanUpPreviouslyDraggedTabs()
this.layoutTabs()
this.setupDraggabilly()
return tabEl
}

setTabCloseEventListener(tabEl) {
tabEl.querySelector('.chrome-tab-close').addEventListener('click', _ => this.removeTab(tabEl))
const tab = tabEl.querySelector('.chrome-tab-close')
if(tab !== null){
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please conform to the existing code style, e.g. if(tab !== null){if (tab !== null) {.

tab.addEventListener('click', _ => this.removeTab(tabEl))
}
}

get activeTabEl() {
Expand Down