Skip to content

Instantly share code, notes, and snippets.

@Vesely
Last active September 6, 2018 14:45
Show Gist options
  • Save Vesely/83b517d1a4f2f3b644849763d3b86729 to your computer and use it in GitHub Desktop.
Save Vesely/83b517d1a4f2f3b644849763d3b86729 to your computer and use it in GitHub Desktop.
Trello - Get last ID
window.liff = window.liff || {};
window.liff.calcLastId = () => {
if (!document.querySelector('.js-last-id')) {
const headerBtns = document.querySelector('.board-header-btns.mod-left');
if (headerBtns) {
const lastIdElement = document.createElement("div");
lastIdElement.className = "js-last-id board-header-btn";
headerBtns.appendChild(lastIdElement);
} else {
return;
}
}
const titleIds = [];
document.querySelectorAll('.list-card .list-card-title').forEach((title) => {
let titleText;
if (title.childNodes.length === 2) {
titleText = title.childNodes[1].textContent;
} else {
titleText = title.childNodes[0].textContent;
}
if (titleText[0] === '#') {
const id = +(titleText.substr(1, titleText.indexOf(' ') - 1));
if (Number.isInteger(id)) {
titleIds.push(id);
}
}
});
if (titleIds.length !== 0) {
const maxId = Math.max.apply(null, titleIds);
window.liff.lastId = maxId;
document.querySelector('.js-last-id').innerHTML = `Last ID: <strong>#${maxId}</strong> &nbsp;`;
}
}
window.liff.calcLastId();
window.liff.calcLastIdInterval = setInterval(() => window.liff.calcLastId(), 5000);
window.liff.prefillCardTitle = () => {
if (window.liff.lastId) {
setTimeout(() => {
const cardTitle = document.querySelector('.js-card-title');
cardTitle.value = `#${+window.liff.lastId + 1} `;
cardTitle.addEventListener('keydown', (event) => {
if (event.key == 'Enter') {
window.liff.lastId = +window.liff.lastId + 1;
window.liff.prefillCardTitle();
}
});
}, 100);
}
};
document.querySelectorAll('.js-open-card-composer').forEach((openCardComposer) => {
openCardComposer.addEventListener('click', window.liff.prefillCardTitle);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment