Middle-click in popup on a style name to open the editor

This commit is contained in:
tophf 2017-03-20 00:24:29 +03:00
parent b2e18177c3
commit ba8301fdce

116
popup.js
View File

@ -91,44 +91,67 @@ function showStyles(styles) {
} }
function createStyleElement(style) { function createStyleElement(style) {
var e = template.style.cloneNode(true); // reuse event function references
var checkbox = e.querySelector(".checker"); createStyleElement.events = createStyleElement.events || {
checkbox.id = "style-" + style.id; checkboxClick() {
checkbox.checked = style.enabled; enableStyle(getClickedStyleId(), this.checked);
},
e.setAttribute("class", "entry " + (style.enabled ? "enabled" : "disabled")); styleNameClick() {
e.setAttribute("style-id", style.id); this.checkbox.click();
var styleName = e.querySelector(".style-name"); window.event.preventDefault();
styleName.appendChild(document.createTextNode(style.name)); },
styleName.setAttribute("for", "style-" + style.id); toggleClick() {
styleName.checkbox = checkbox; enableStyle(getClickedStyleId(), this.matches('.enable'));
var editLink = e.querySelector(".style-edit-link"); },
editLink.setAttribute("href", editLink.getAttribute("href") + style.id); deleteClick() {
editLink.addEventListener("click", openLinkInTabOrWindow, false); doDelete();
styleName.addEventListener("click", function() { this.checkbox.click(); event.preventDefault(); });
// clicking the checkbox will toggle it, and this will run after that happens
checkbox.addEventListener("click", function() { enable(event, event.target.checked); }, false);
e.querySelector(".enable").addEventListener("click", function() { enable(event, true); }, false);
e.querySelector(".disable").addEventListener("click", function() { enable(event, false); }, false);
e.querySelector(".delete").addEventListener("click", function() { doDelete(event, false); }, false);
return e;
} }
};
const entry = template.style.cloneNode(true);
entry.setAttribute('style-id', style.id);
Object.assign(entry, {
styleId: style.id,
className: ['entry', style.enabled ? 'enabled' : 'disabled'].join(' '),
onmousedown: openEditorOnMiddleclick,
onauxclick: openEditorOnMiddleclick,
});
function enable(event, enabled) { const checkbox = entry.querySelector('.checker');
var id = getId(event); Object.assign(checkbox, {
enableStyle(id, enabled); id: 'style-' + style.id,
checked: style.enabled,
onclick: createStyleElement.events.checkboxClick,
});
const editLink = entry.querySelector('.style-edit-link');
Object.assign(editLink, {
href: editLink.getAttribute('href') + style.id,
onclick: openLinkInTabOrWindow,
});
const styleName = entry.querySelector('.style-name');
Object.assign(styleName, {
htmlFor: 'style-' + style.id,
onclick: createStyleElement.events.styleNameClick,
});
styleName.checkbox = checkbox;
styleName.appendChild(document.createTextNode(style.name));
entry.querySelector('.enable').onclick = createStyleElement.events.toggleClick;
entry.querySelector('.disable').onclick = createStyleElement.events.toggleClick;
entry.querySelector('.delete').onclick = createStyleElement.events.deleteClick;
return entry;
} }
function doDelete() { function doDelete() {
document.getElementById('confirm').dataset.display = true; document.getElementById('confirm').dataset.display = true;
let id = getId(event); const id = getClickedStyleId();
document.querySelector('#confirm b').textContent = document.querySelector('#confirm b').textContent =
document.querySelector(`[style-id="${id}"] label`).textContent; document.querySelector(`[style-id="${id}"] label`).textContent;
document.getElementById('confirm').dataset.id = id; document.getElementById('confirm').dataset.id = id;
} }
document.getElementById('confirm').addEventListener('click', e => { document.getElementById('confirm').addEventListener('click', e => {
let cmd = e.target.dataset.cmd; let cmd = e.target.dataset.cmd;
if (cmd === 'ok') { if (cmd === 'ok') {
@ -145,22 +168,9 @@ document.getElementById('confirm').addEventListener('click', e => {
} }
}); });
function getBrowser() { function getClickedStyleId() {
if (navigator.userAgent.indexOf("OPR") > -1) { const entry = window.event.target.closest('.entry');
return "Opera"; return entry ? entry.styleId : null;
}
return "Chrome";
}
function getId(event) {
var e = event.target;
while (e) {
if (e.hasAttribute("style-id")) {
return e.getAttribute("style-id");
}
e = e.parentNode;
}
return null;
} }
function openLinkInTabOrWindow(event) { function openLinkInTabOrWindow(event) {
@ -176,6 +186,24 @@ function openLinkInTabOrWindow(event) {
close(); close();
} }
function openEditorOnMiddleclick(event) {
if (event.button != 1) {
return;
}
// open an editor on middleclick
if (event.target.matches('.entry, .style-name, .style-edit-link')) {
this.querySelector('.style-edit-link').click();
event.preventDefault();
return;
}
// prevent the popup being opened in a background tab
// when an irrelevant link was accidentally clicked
if (event.target.closest('a')) {
event.preventDefault();
return;
}
}
function openLink(event) { function openLink(event) {
event.preventDefault(); event.preventDefault();
chrome.runtime.sendMessage({method: "openURL", url: event.target.href}); chrome.runtime.sendMessage({method: "openURL", url: event.target.href});