Extract logic to add buttons to the bulk actions bar
In the future, we will want to add many buttons to the bulk actions bar
in thread lists. This CL prepares for that by moving the logic which
detects whether the button has already been inserted into the bar to a
common file, so it can be reused by features other than the "batch lock"
feature.
Bug: twpowertools:74
Change-Id: Ic98cb68e5a8d0ca97e24578050b53cff6fdde41c
diff --git a/src/contentScripts/communityConsole/batchLock.js b/src/contentScripts/communityConsole/batchLock.js
index 86a7057..db08a06 100644
--- a/src/contentScripts/communityConsole/batchLock.js
+++ b/src/contentScripts/communityConsole/batchLock.js
@@ -1,19 +1,12 @@
import {isOptionEnabled} from '../../common/optionsUtils.js';
-import {addButtonToThreadListActions, removeChildNodes} from './utils/common.js';
+import {addButtonToThreadListActions, removeChildNodes, shouldAddBtnToActionBar} from './utils/common.js';
+
+const kLockDebugId = 'twpt-lock';
export var batchLock = {
- nodeIsReadToggleBtn(node) {
- return ('tagName' in node) && node.tagName == 'MATERIAL-BUTTON' &&
- node.getAttribute('debugid') !== null &&
- (node.getAttribute('debugid') == 'mark-read-button' ||
- node.getAttribute('debugid') == 'mark-unread-button') &&
- ('parentNode' in node) && node.parentNode !== null &&
- ('parentNode' in node.parentNode) &&
- node.parentNode.querySelector('[debugid="twpt-lock"]') === null &&
- node.parentNode.parentNode !== null &&
- ('tagName' in node.parentNode.parentNode) &&
- node.parentNode.parentNode.tagName == 'EC-BULK-ACTIONS';
+ shouldAddButton(node) {
+ return shouldAddBtnToActionBar(kLockDebugId, node);
},
createDialog() {
var modal = document.querySelector('.pane[pane-id="default-1"]');
@@ -119,7 +112,7 @@
if (isEnabled) {
let tooltip = chrome.i18n.getMessage('inject_lockbtn');
let btn = addButtonToThreadListActions(
- readToggle, 'lock', 'twpt-lock', tooltip);
+ readToggle, 'lock', kLockDebugId, tooltip);
btn.addEventListener('click', () => {
this.createDialog();
});
diff --git a/src/contentScripts/communityConsole/main.js b/src/contentScripts/communityConsole/main.js
index 34b0f1d..95045a4 100644
--- a/src/contentScripts/communityConsole/main.js
+++ b/src/contentScripts/communityConsole/main.js
@@ -130,7 +130,7 @@
// Inject the batch lock button in the thread list if the option is
// currently enabled.
- if (batchLock.nodeIsReadToggleBtn(node)) {
+ if (batchLock.shouldAddButton(node)) {
batchLock.addButtonIfEnabled(node);
}
diff --git a/src/contentScripts/communityConsole/utils/common.js b/src/contentScripts/communityConsole/utils/common.js
index ac4db4e..2e33f99 100644
--- a/src/contentScripts/communityConsole/utils/common.js
+++ b/src/contentScripts/communityConsole/utils/common.js
@@ -61,6 +61,17 @@
return clone;
}
+// Returns true if |node| is the "mark as read/unread" button, the parent of the
+// parent of |node| is the actions bar of the thread list, and the button with
+// debugid |debugid| is NOT part of the actions bar.
+export function shouldAddBtnToActionBar(debugid, node) {
+ return node?.tagName == 'MATERIAL-BUTTON' &&
+ (node.getAttribute?.('debugid') == 'mark-read-button' ||
+ node.getAttribute?.('debugid') == 'mark-unread-button') &&
+ node.parentNode?.querySelector('[debugid="' + debugid + '"]') === null &&
+ node.parentNode?.parentNode?.tagName == 'EC-BULK-ACTIONS';
+}
+
// Returns the display language set by the user.
export function getDisplayLanguage() {
var startup =