Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Polling frontend topic tools option #41

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
99 changes: 99 additions & 0 deletions public/src/client/topic/poll.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
'use strict';

define('forum/topic/poll', ['components', 'translator', 'alerts'], function (components, translator, alerts) {
const Poll = {};

Check failure on line 4 in public/src/client/topic/poll.js

View workflow job for this annotation

GitHub Actions / test

Expected indentation of 1 tab but found 4 spaces

let pollModal;

Check failure on line 6 in public/src/client/topic/poll.js

View workflow job for this annotation

GitHub Actions / test

Expected indentation of 1 tab but found 4 spaces
let pollOptions = [];

Check failure on line 7 in public/src/client/topic/poll.js

View workflow job for this annotation

GitHub Actions / test

Expected indentation of 1 tab but found 4 spaces

Poll.init = function () {

Check failure on line 9 in public/src/client/topic/poll.js

View workflow job for this annotation

GitHub Actions / test

Expected indentation of 1 tab but found 4 spaces
if (pollModal) {

Check failure on line 10 in public/src/client/topic/poll.js

View workflow job for this annotation

GitHub Actions / test

Expected indentation of 2 tabs but found 8 spaces
return;

Check failure on line 11 in public/src/client/topic/poll.js

View workflow job for this annotation

GitHub Actions / test

Expected indentation of 3 tabs but found 12 spaces
}

Check failure on line 12 in public/src/client/topic/poll.js

View workflow job for this annotation

GitHub Actions / test

Expected indentation of 2 tabs but found 8 spaces

app.parseAndTranslate('modals/create-poll', {}, function (html) {

Check failure on line 14 in public/src/client/topic/poll.js

View workflow job for this annotation

GitHub Actions / test

Expected indentation of 2 tabs but found 8 spaces
pollModal = html;

Check failure on line 15 in public/src/client/topic/poll.js

View workflow job for this annotation

GitHub Actions / test

Expected indentation of 3 tabs but found 12 spaces
$('body').append(pollModal);

Check failure on line 16 in public/src/client/topic/poll.js

View workflow job for this annotation

GitHub Actions / test

Expected indentation of 3 tabs but found 12 spaces

const pollNameInput = pollModal.find('#pollName');
const pollOptionInput = pollModal.find('#pollOption');
const addOptionButton = pollModal.find('#addOption');
const createPollButton = pollModal.find('#createPoll');
const closePollButton = pollModal.find('#closePollModal');
const pollOptionsContainer = pollModal.find('#pollOptionsContainer');
const pollOptionsList = pollModal.find('#pollOptionsList');
const scrollIndicator = pollModal.find('#scrollIndicator');

// Add option when button is clicked
addOptionButton.on('click', function () {
const optionText = pollOptionInput.val().trim();
if (optionText) {
pollOptions.push(optionText);
pollOptionInput.val('');

// Add option to the list
const optionItem = $('<li class="list-group-item d-flex justify-content-between align-items-center"></li>')
.text(optionText)
.append($('<button class="btn btn-sm btn-danger">X</button>')
.on('click', function () {
pollOptions = pollOptions.filter(opt => opt !== optionText);
optionItem.remove();
checkScrollIndicator();
})
);
pollOptionsList.append(optionItem);
checkScrollIndicator();
}
});

// Check if scrollbar should be visible
function checkScrollIndicator() {
if (pollOptionsContainer[0].scrollHeight > pollOptionsContainer.innerHeight()) {
scrollIndicator.show();
} else {
scrollIndicator.hide();
}
}

// Listen for scrolling to hide indicator
pollOptionsContainer.on('scroll', function () {
if (pollOptionsContainer.scrollTop() + pollOptionsContainer.innerHeight() >= pollOptionsContainer[0].scrollHeight) {
scrollIndicator.fadeOut();
} else {
scrollIndicator.fadeIn();
}
});

// Close modal
closePollButton.on('click', function () {
pollModal.remove();
pollModal = null;
});

// Create Poll
createPollButton.on('click', function () {
const pollName = pollNameInput.val().trim();
if (!pollName) {
return alerts.error('Please enter a poll name.');
}
if (pollOptions.length < 2) {
return alerts.error('Please add at least two options.');
}

const pollData = {
name: pollName,
options: pollOptions
};

console.log('Poll Created:', pollData);
alerts.success('Poll Created Successfully!');

// Close modal after creating poll
pollModal.remove();
pollModal = null;
});
});
};

return Poll;
});
8 changes: 8 additions & 0 deletions public/src/client/topic/threadTools.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,14 @@ define('forum/topic/threadTools', [
});
});

//create topic modal
topicContainer.on('click', '[component="topic/create-poll"]', function () {
require(['forum/topic/poll'], function (createPoll) {
createPoll.init();
});
});


topicContainer.on('click', '[component="topic/following"]', function () {
changeWatching('follow');
});
Expand Down
36 changes: 36 additions & 0 deletions src/views/modals/create-poll.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<div class="card tool-modal shadow" style="width: 800px; height: 650px;">
<h5 class="card-header">
Create Poll
</h5>

<!-- Modal Body (Scrollable) -->
<div class="card-body" style="overflow-y: auto; max-height: 550px;">
<!-- Poll Name -->
<p><strong>Poll Name:</strong></p>
<input id="pollName" type="text" class="form-control mb-3" placeholder="Enter poll name">

<!-- Poll Options -->
<p>Write your options below:</p>
<div>
<label class="form-label" for="pollOption"><strong>Options</strong></label>
<input id="pollOption" type="text" class="form-control">
</div>
<button class="btn btn-primary btn-sm mt-2" id="addOption">Add Option</button>

<!-- Scrollable Container for Options -->
<div id="pollOptionsContainer" class="border rounded p-2 mt-3 position-relative"
style="max-height: 250px; overflow-y: scroll; scrollbar-width: thin; scrollbar-color: #555 #eee;">
<ul id="pollOptionsList" class="list-group" style="margin-bottom: 0;"></ul>
<!-- Scroll Indicator -->
<div id="scrollIndicator" class="position-absolute bottom-0 start-0 w-100"
style="height: 20px; background: linear-gradient(to bottom, transparent, rgba(0, 0, 0, 0.1));">
</div>
</div>
</div>

<!-- Footer (Static) -->
<div class="card-footer text-end" style="position: absolute; bottom: 0; width: 100%; background: white;">
<button class="btn btn-link btn-sm" id="closePollModal">[[global:buttons.close]]</button>
<button class="btn btn-primary btn-sm" id="createPoll">Create Poll</button>
</div>
</div>
Loading