Skip to content

Commit

Permalink
Update button state when pasting from menu, autoselect item if exists (
Browse files Browse the repository at this point in the history
  • Loading branch information
stephanmahieu committed Jan 14, 2020
1 parent 7c21e0d commit 45f42a6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
13 changes: 13 additions & 0 deletions popup/options/options-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,10 @@ function setListButtonsState(selectId, inputId, btnAddId, btnModId, btnDelId) {
const lstSelect = document.querySelector(selectId);
const inputValue = document.querySelector(inputId).value;

if (lstSelect.selectedIndex < 0 && inputValue && _listItemExist(lstSelect, inputValue)) {
_selectItemInList(lstSelect, inputValue);
}

if (lstSelect.selectedIndex < 0 || !inputValue) {
if (inputValue) {
btnAdd.removeAttribute("disabled");
Expand Down Expand Up @@ -183,3 +187,12 @@ function _listItemExist(selectElm, optionValue) {
}
return exist;
}

function _selectItemInList(selectElm, optionValue) {
const options = selectElm.options;
for(let i = 0; i < options.length; i++) {
if (options[i].textContent === optionValue) {
selectElm.selectedIndex = i;
}
}
}
8 changes: 8 additions & 0 deletions popup/options/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,12 @@ document.addEventListener("DOMContentLoaded", function() {
});
document.querySelector('#domainlist').addEventListener("change", domainlistChanged);
document.querySelector('#domainListItem').addEventListener("keyup", domainlistInputChanged);
document.querySelector('#domainListItem').addEventListener("paste", domainlistInputPasted);
document.querySelectorAll('.domainbutton').forEach(btn => {btn.addEventListener("click", listButtonClicked)});

document.querySelector('#fieldlist').addEventListener("change", fieldlistChanged);
document.querySelector('#fieldListItem').addEventListener("keyup", fieldlistInputChanged);
document.querySelector('#fieldListItem').addEventListener("paste", fieldlistInputPasted);

document.querySelector("#buttonClose").addEventListener("click", closeThisPopup);
document.addEventListener("keyup", onKeyClicked);
Expand Down Expand Up @@ -357,6 +359,9 @@ function domainlistChanged() {
function domainlistInputChanged() {
setListButtonsState("#domainlist", "#domainListItem", "#listAdd", "#listModify", "#listDelete");
}
function domainlistInputPasted(event) {
window.setTimeout(() => {domainlistInputChanged(); }, 10);
}

function fieldlistChanged() {
copySelectedItemToInput("#fieldlist", "#fieldListItem");
Expand All @@ -366,6 +371,9 @@ function fieldlistChanged() {
function fieldlistInputChanged() {
setListButtonsState("#fieldlist", "#fieldListItem", "#fieldAdd", "#fieldModify", "#fieldDelete");
}
function fieldlistInputPasted(event) {
window.setTimeout(() => {fieldlistInputChanged(); }, 10);
}

function shortcutKeyEnableChanged(event) {
const commandName = event.target.getAttribute('data-cmd');
Expand Down

0 comments on commit 45f42a6

Please sign in to comment.