Skip to content

Commit

Permalink
Converted to ES6, detabified
Browse files Browse the repository at this point in the history
  • Loading branch information
Vojtech Jasny committed Oct 10, 2015
1 parent 840d39d commit c44de33
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 88 deletions.
11 changes: 0 additions & 11 deletions about.html

This file was deleted.

131 changes: 66 additions & 65 deletions js/context_menu.js
@@ -1,131 +1,132 @@
function ContextMenu(chromeObj) {
if (chromeObj) {
this.chrome = chromeObj;
} else if (typeof chrome !== 'undefined') {
this.chrome = chrome;
}
'use strict';

class ContextMenu {
constructor(chromeObj) {
if (chromeObj) {
this.chrome = chromeObj;
} else if (typeof chrome !== 'undefined') {
this.chrome = chrome;
}

this.contexts = ['all'];
this.contextMenuId = null;
}
this.contexts = ['all'];
this.contextMenuId = null;

this.tabTests = {
tabsToTheLeft(currentTab, testedTab) {
return testedTab.tab.index < currentTab.tab.index;
},

ContextMenu.prototype = {
tabsToTheRight(currentTab, testedTab) {
return testedTab.tab.index > currentTab.tab.index;
},

init: function() {
otherTabs(currentTab, testedTab) {
return testedTab.tab.index !== currentTab.tab.index;
},

tabsFromDomain(currentTab, testedTab) {
return currentTab.domain === testedTab.domain;
},

tabsFromOtherDomain(currentTab, testedTab) {
return testedTab.domain !== currentTab.domain;
},

otherTabsFromDomain(currentTab, testedTab) {
return testedTab.domain === currentTab.domain && testedTab.tab.index !== currentTab.tab.index;
}
};
}

init() {
this.initContextMenu();
},
}

getDomain: function(url) {
getDomain(url) {
return url.split(/\/+/g)[1];
},
}

enrichTab: function(tab) {
enrichTab(tab) {
return {
tab: tab,
domain: this.getDomain(tab.url)
};
},
}

getWindowTabs: function(callback) {
getWindowTabs(callback) {
var params = { windowId: this.chrome.windows.WINDOW_ID_CURRENT };
this.chrome.tabs.query(params, function(tabs) {
this.chrome.tabs.query(params, (tabs) => {
var result = tabs.map(this.enrichTab, this);
callback(result);
}.bind(this));
},

tabTests: {
tabsToTheLeft: function(currentTab, testedTab) {
return testedTab.tab.index < currentTab.tab.index;
},

tabsToTheRight: function(currentTab, testedTab) {
return testedTab.tab.index > currentTab.tab.index;
},

otherTabs: function(currentTab, testedTab) {
return testedTab.tab.index !== currentTab.tab.index;
},

tabsFromDomain: function(currentTab, testedTab) {
return currentTab.domain === testedTab.domain;
},

tabsFromOtherDomain: function(currentTab, testedTab) {
return testedTab.domain !== currentTab.domain;
},

otherTabsFromDomain: function(currentTab, testedTab) {
return testedTab.domain === currentTab.domain && testedTab.tab.index !== currentTab.tab.index;
}
},
});
}

/**
* Determines what tabs should be closed using supplied test function and
* returns their ids.
*/
getTabsToClose: function(currentTab, allTabs, tabTest) {
getTabsToClose(currentTab, allTabs, tabTest) {
var tabsToClose = allTabs.filter(function(testedTab) {
return tabTest(currentTab, testedTab);
});

tabsToClose = tabsToClose.map(function(tab) { return tab.tab.id; });

return tabsToClose;
},
}

_closeTabs: function(tabIds) {
_closeTabs(tabIds) {
this.chrome.tabs.remove(tabIds);
},
}

/**
* Given a tab test function returns a context menu click handler item which runs
* that test against all tabs in the current window and closes those tabs for
* which the test function returns true.
*/
getClickHandler: function(tabTest, callback) {
var clickHandler = function(info, tab) {
var execute = function(allTabs) {
getClickHandler(tabTest, callback) {
var clickHandler = (info, tab) => {
var execute = (allTabs) => {
var currentTab = this.enrichTab(tab);

var tabsToClose = this.getTabsToClose(currentTab, allTabs, tabTest);
this._closeTabs(tabsToClose);

callback && callback();
}.bind(this);
};

this.getWindowTabs(execute);
}.bind(this);
};

return clickHandler;
},
}

addItem: function(title, tabTest) {
addItem(title, tabTest) {
this.chrome.contextMenus.create({
title: title,
title,
contexts: this.contexts,
parentId: this.contextMenuId,
onclick: this.getClickHandler(tabTest).bind(this)
onclick: callback => this.getClickHandler(tabTest, callback)
});
},
}

addSeparator: function() {
addSeparator() {
this.chrome.contextMenus.create({
type: 'separator',
parentId: this.contextMenuId,
contexts: this.contexts
});
},
}

initContextMenu: function(tab) {
initContextMenu(tab) {
if (this.contextMenuId) return;

this.contextMenuId = this.chrome.contextMenus.create({
title: 'Close tabs',
contexts: this.contexts
});

this.addItem('Other', this.tabTests.otherTabs);
this.addItem('Other in this window', this.tabTests.otherTabs);
this.addItem('To the ←', this.tabTests.tabsToTheLeft);
this.addItem('To the →', this.tabTests.tabsToTheRight);
this.addSeparator();
Expand All @@ -134,7 +135,7 @@ ContextMenu.prototype = {
this.addItem('All other from site', this.tabTests.otherTabsFromDomain);
this.addItem('All from other site', this.tabTests.tabsFromOtherDomain);
}
};
}

// only instantiate when in chrome environment
if (typeof chrome !== 'undefined') {
Expand Down
4 changes: 2 additions & 2 deletions manifest.json
Expand Up @@ -3,9 +3,9 @@
"version": "2.0",
"name": "Close Tabs (Enhanced Edition)",
"description": "Context menu to close tabs in various clever ways.",
"homepage_url": "https://github.com/voy/chrome-close-tabs/",
"homepage_url": "https://github.com/voy/chrome-close-tabs/",
"background": {
"scripts": ["js/context_menu.js"]
"scripts": ["js/context_menu.js"]
},
"icons": {
"128": "img/close-tabs-128.png",
Expand Down
20 changes: 10 additions & 10 deletions package.json
@@ -1,12 +1,12 @@
{
"name": "chrome-close-tabs",
"version": "0.0.0",
"scripts": {
"test": "mocha"
},
"devDependencies": {
"mocha": ">= 1.5.0",
"sinon": ">= 1.4.0",
"chai": ">= 1.3.0"
}
"name": "chrome-close-tabs",
"version": "2.0.0",
"scripts": {
"test": "mocha"
},
"devDependencies": {
"mocha": ">= 1.5.0",
"sinon": ">= 1.4.0",
"chai": ">= 1.3.0"
}
}

0 comments on commit c44de33

Please sign in to comment.