Skip to content

Commit

Permalink
refactor code in client.js to fix indentation errors during lint
Browse files Browse the repository at this point in the history
  • Loading branch information
bobofishbo committed Feb 26, 2025
1 parent f6bf5e2 commit 9cd5318
Showing 1 changed file with 130 additions and 132 deletions.
262 changes: 130 additions & 132 deletions nodebb-plugin-composer-custom/static/lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,128 +3,128 @@
const bootstrap = require('bootstrap');

$(document).ready(function () {
console.log('Poll Script Loaded!');

// === Composer Initialization ===
$(window).on('action:app.load', function () {
require(['composer/drafts'], function (drafts) {
drafts.migrateGuest();
drafts.loadOpen();
});
});

$(window).on('action:composer.topic.new', function (ev, data) {
if (config['composer-default'].composeRouteEnabled !== 'on') {
require(['composer'], function (composer) {
composer.newTopic({
cid: data.cid,
title: data.title || '',
body: data.body || '',
tags: data.tags || [],
});
});
} else {
ajaxify.go(
'compose?cid=' + data.cid +
(data.title ? '&title=' + encodeURIComponent(data.title) : '') +
(data.body ? '&body=' + encodeURIComponent(data.body) : '')
);
}
});

$(window).on('action:composer.post.edit', function (ev, data) {
if (config['composer-default'].composeRouteEnabled !== 'on') {
require(['composer'], function (composer) {
composer.editPost({ pid: data.pid });
});
} else {
ajaxify.go('compose?pid=' + data.pid);
}
});

$(window).on('action:composer.post.new', function (ev, data) {
data.body = data.body || data.text;
data.title = data.title || data.topicName;
if (config['composer-default'].composeRouteEnabled !== 'on') {
require(['composer'], function (composer) {
composer.newReply({
tid: data.tid,
toPid: data.pid,
title: data.title,
body: data.body,
});
});
} else {
ajaxify.go(
'compose?tid=' + data.tid +
(data.pid ? '&toPid=' + data.pid : '') +
(data.title ? '&title=' + encodeURIComponent(data.title) : '') +
(data.body ? '&body=' + encodeURIComponent(data.body) : '')
);
}
});

$(window).on('action:composer.addQuote', function (ev, data) {
data.body = data.body || data.text;
data.title = data.title || data.topicName;
if (config['composer-default'].composeRouteEnabled !== 'on') {
require(['composer'], function (composer) {
var topicUUID = composer.findByTid(data.tid);
composer.addQuote({
tid: data.tid,
toPid: data.pid,
selectedPid: data.selectedPid,
title: data.title,
username: data.username,
body: data.body,
uuid: topicUUID,
});
});
} else {
ajaxify.go('compose?tid=' + data.tid + '&toPid=' + data.pid + '&quoted=1&username=' + data.username);
}
});

$(window).on('action:composer.enhance', function (ev, data) {
require(['composer'], function (composer) {
composer.enhance(data.container);
});
});

// === Bootstrap Modal Fix ===
const pollModalElement = document.getElementById('pollModal');
let pollModalInstance;
if (pollModalElement) {
pollModalInstance = new bootstrap.Modal(pollModalElement);
// Reset the poll form when the modal is closed
pollModalElement.addEventListener('hidden.bs.modal', function () {
document.getElementById('poll-form').reset();
$('#poll-options .input-group:gt(1)').remove(); // Keep the first two options
});
} else {
console.warn('Poll modal not found in DOM.');
}

// === Add Poll Option ===
$(document).on('click', '#add-option', function (e) {
e.preventDefault();
const newOption = $(`
<div class="input-group mb-2">
<input type="text" class="form-control poll-option" placeholder="New Option" required>
<button class="btn btn-danger remove-option" type="button">&times;</button>
</div>
`);
$('#poll-options').append(newOption);
});

// === Remove Poll Option ===
$(document).on('click', '.remove-option', function (e) {
$(this).closest('.input-group').remove();
});

// === Save Poll ===
console.log('Poll Script Loaded!');

// === Composer Initialization ===
$(window).on('action:app.load', function () {
require(['composer/drafts'], function (drafts) {
drafts.migrateGuest();
drafts.loadOpen();
});
});

$(window).on('action:composer.topic.new', function (ev, data) {
if (config['composer-default'].composeRouteEnabled !== 'on') {
require(['composer'], function (composer) {
composer.newTopic({
cid: data.cid,
title: data.title || '',
body: data.body || '',
tags: data.tags || [],
});
});
} else {
ajaxify.go(
'compose?cid=' + data.cid +
(data.title ? '&title=' + encodeURIComponent(data.title) : '') +
(data.body ? '&body=' + encodeURIComponent(data.body) : '')
);
}
});

$(window).on('action:composer.post.edit', function (ev, data) {
if (config['composer-default'].composeRouteEnabled !== 'on') {
require(['composer'], function (composer) {
composer.editPost({ pid: data.pid });
});
} else {
ajaxify.go('compose?pid=' + data.pid);
}
});

$(window).on('action:composer.post.new', function (ev, data) {
data.body = data.body || data.text;
data.title = data.title || data.topicName;
if (config['composer-default'].composeRouteEnabled !== 'on') {
require(['composer'], function (composer) {
composer.newReply({
tid: data.tid,
toPid: data.pid,
title: data.title,
body: data.body,
});
});
} else {
ajaxify.go(
'compose?tid=' + data.tid +
(data.pid ? '&toPid=' + data.pid : '') +
(data.title ? '&title=' + encodeURIComponent(data.title) : '') +
(data.body ? '&body=' + encodeURIComponent(data.body) : '')
);
}
});

$(window).on('action:composer.addQuote', function (ev, data) {
data.body = data.body || data.text;
data.title = data.title || data.topicName;
if (config['composer-default'].composeRouteEnabled !== 'on') {
require(['composer'], function (composer) {
var topicUUID = composer.findByTid(data.tid);
composer.addQuote({
tid: data.tid,
toPid: data.pid,
selectedPid: data.selectedPid,
title: data.title,
username: data.username,
body: data.body,
uuid: topicUUID,
});
});
} else {
ajaxify.go('compose?tid=' + data.tid + '&toPid=' + data.pid + '&quoted=1&username=' + data.username);
}
});

$(window).on('action:composer.enhance', function (ev, data) {
require(['composer'], function (composer) {
composer.enhance(data.container);
});
});

// === Bootstrap Modal Fix ===
const pollModalElement = document.getElementById('pollModal');
let pollModalInstance;

Check failure on line 97 in nodebb-plugin-composer-custom/static/lib/client.js

View workflow job for this annotation

GitHub Actions / test

Trailing spaces not allowed
if (pollModalElement) {
pollModalInstance = new bootstrap.Modal(pollModalElement);

Check failure on line 99 in nodebb-plugin-composer-custom/static/lib/client.js

View workflow job for this annotation

GitHub Actions / test

'pollModalInstance' is assigned a value but never used

Check failure on line 100 in nodebb-plugin-composer-custom/static/lib/client.js

View workflow job for this annotation

GitHub Actions / test

Trailing spaces not allowed
// Reset the poll form when the modal is closed
pollModalElement.addEventListener('hidden.bs.modal', function () {
document.getElementById('poll-form').reset();
$('#poll-options .input-group:gt(1)').remove(); // Keep the first two options
});
} else {
console.warn('Poll modal not found in DOM.');
}

// === Add Poll Option ===
$(document).on('click', '#add-option', function (e) {
e.preventDefault();
const newOption = $(`
<div class="input-group mb-2">
<input type="text" class="form-control poll-option" placeholder="New Option" required>
<button class="btn btn-danger remove-option" type="button">&times;</button>
</div>
`);
$('#poll-options').append(newOption);
});

// === Remove Poll Option ===
$(document).on('click', '.remove-option', function (e) {

Check failure on line 123 in nodebb-plugin-composer-custom/static/lib/client.js

View workflow job for this annotation

GitHub Actions / test

Expected error to be handled

Check failure on line 123 in nodebb-plugin-composer-custom/static/lib/client.js

View workflow job for this annotation

GitHub Actions / test

'e' is defined but never used
$(this).closest('.input-group').remove();
});

// === Save Poll ===
$(document).on('click', '#save-poll', function (e) {
e.preventDefault();

Expand Down Expand Up @@ -168,20 +168,20 @@ $(document).ready(function () {
});


// === Update Poll Preview in Composer ===
$(window).on('action:composer.preview', function () {
// === Update Poll Preview in Composer ===
$(window).on('action:composer.preview', function () {
const textareaValue = $('.composer .write').val().trim();
let pollContainer = $('#poll-preview');

if (pollContainer.length === 0) {
console.warn('Poll preview container not found. Creating it...');
pollContainer = $('<div id="poll-preview" class="poll-preview alert alert-light mt-3 p-3"></div>');
$('.composer .preview-container').prepend(pollContainer);
}

const pollRegex = /\*\*(.*?)\*\*\n\n(- .+(\n- .+)*)/s;
const match = textareaValue.match(pollRegex);

if (match && match[1] && match[2]) {
const pollTitle = match[1];
const pollOptions = match[2]
Expand All @@ -193,7 +193,7 @@ $(document).ready(function () {
</div>
`)
.join('');

pollContainer.html(`
<div class="poll-card">
<h4 class="poll-title">${pollTitle}</h4>
Expand All @@ -202,7 +202,7 @@ $(document).ready(function () {
</div>
`);
pollContainer.removeClass('d-none');

// Enable vote button when an option is selected
$('.poll-radio').on('change', function () {
$('.poll-vote-btn').prop('disabled', false);
Expand All @@ -211,6 +211,4 @@ $(document).ready(function () {
pollContainer.addClass('d-none').html('');
}
});


});
});

Check failure on line 214 in nodebb-plugin-composer-custom/static/lib/client.js

View workflow job for this annotation

GitHub Actions / test

Newline required at end of file but not found

0 comments on commit 9cd5318

Please sign in to comment.