Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Replaced jQuery with vanilla javascript.
  • Loading branch information
KN4CK3R authored and silverwind committed May 21, 2021
commit a9a85a33d8a4e70409959d05a6da782b2efbbeb3
36 changes: 18 additions & 18 deletions web_src/js/markup/tasklist.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,44 +4,44 @@
* On success it updates the raw-content on error it resets the checkbox to its original value.
*/
export default function initMarkupTasklist() {
$(`.render-content.markup[data-can-edit='true']`).parent().each((_, container) => {
const $container = $(container);
const $checkboxes = $container.find(`.task-list-item input:checkbox`);
document.querySelectorAll(`.render-content.markup[data-can-edit='true']`).forEach((el) => {
const container = el.parentNode;
const checkboxes = container.querySelectorAll(`.task-list-item input[type=checkbox]`);

$checkboxes.on('change', async (ev) => {
const $checkbox = $(ev.target);
const checkboxCharacter = $checkbox.is(':checked') ? 'x' : ' ';
const position = parseInt($checkbox.data('source-position')) + 1;
checkboxes.forEach((cb) => cb.addEventListener('change', async (ev) => {
const checkbox = ev.target;
const checkboxCharacter = checkbox.checked ? 'x' : ' ';
const position = parseInt(checkbox.dataset.sourcePosition) + 1;

const $rawContent = $container.find('.raw-content');
const oldContent = $rawContent.text();
const rawContent = container.querySelector('.raw-content');
const oldContent = rawContent.textContent;
const newContent = oldContent.substring(0, position) + checkboxCharacter + oldContent.substring(position + 1);

if (newContent !== oldContent) {
$checkboxes.prop('disabled', true);
checkboxes.forEach((cb) => cb.disabled = true);

try {
const $contentZone = $container.find('.edit-content-zone');
const url = $contentZone.data('update-url');
const context = $contentZone.data('context');
const contentZone = container.querySelector('.edit-content-zone');
const url = contentZone.dataset.updateUrl;
const context = contentZone.dataset.context;

await $.post(url, {
_csrf: window.config.csrf,
content: newContent,
context,
});

$rawContent.text(newContent);
rawContent.textContent = newContent;
} catch (e) {
$checkbox.prop('checked', !$checkbox.is(':checked'));
checkbox.checked = !checkbox.checked;

console.error(e);
} finally {
$checkboxes.prop('disabled', false);
checkboxes.forEach((cb) => cb.disabled = false);
}
}
});
}));

$checkboxes.prop('disabled', false);
checkboxes.forEach((cb) => cb.disabled = false);
});
}