From 6141af3025eef8435f931638fde67f986e7f0ac7 Mon Sep 17 00:00:00 2001 From: John Rayes Date: Sun, 24 Dec 2023 17:01:05 -0700 Subject: [PATCH] extract poll js Signed-off-by: John Rayes --- Sources/Actions/Post.php | 4 +--- Sources/Poll.php | 10 ++-------- Themes/default/Poll.template.php | 25 +------------------------ Themes/default/Post.template.php | 26 +------------------------- Themes/default/scripts/post.js | 25 +++++++++++++++++++++++++ 5 files changed, 30 insertions(+), 60 deletions(-) create mode 100644 Themes/default/scripts/post.js diff --git a/Sources/Actions/Post.php b/Sources/Actions/Post.php index 6315ddd292d..c6339c6f8a8 100644 --- a/Sources/Actions/Post.php +++ b/Sources/Actions/Post.php @@ -375,7 +375,7 @@ public function show(): void Theme::loadJavaScriptFile('drafts.js', ['defer' => false, 'minimize' => true], 'smf_drafts'); } - // quotedText.js + Theme::loadJavaScriptFile('post.js', ['defer' => true, 'minimize' => true], 'smf_post'); Theme::loadJavaScriptFile('quotedText.js', ['defer' => true, 'minimize' => true], 'smf_quotedText'); // Knowing the current board ID might be handy. @@ -954,8 +954,6 @@ protected function showPreview(): void 'is_last' => false, ]; } - Utils::$context['last_choice_id'] = $choice_id; - Utils::$context['choices'][count(Utils::$context['choices']) - 1]['is_last'] = true; } // Are you... a guest? diff --git a/Sources/Poll.php b/Sources/Poll.php index d6a0438fef8..44e62592090 100644 --- a/Sources/Poll.php +++ b/Sources/Poll.php @@ -1196,10 +1196,8 @@ public static function edit(): void Utils::$context['poll'] = $poll->format(); Utils::$context['choices'] = &Utils::$context['poll']['choices']; - Utils::$context['last_choice_id'] = array_key_last(Utils::$context['poll']['choices']); - Utils::$context['poll']['choices'][Utils::$context['last_choice_id']]['is_last'] = true; - Utils::$context['page_title'] = Utils::$context['is_edit'] ? Lang::$txt['poll_edit'] : Lang::$txt['add_poll']; + Theme::loadJavaScriptFile('post.js', ['defer' => true, 'minimize' => true], 'smf_post'); // Build the link tree. Utils::$context['linktree'][] = [ @@ -1473,15 +1471,11 @@ protected function initNewPoll(): void 'guest_vote' => !empty($_POST['poll_guest_vote']), ]); - // Make all five poll choices empty. - Utils::$context['last_choice_id'] = 4; - - for ($i = 0; $i <= Utils::$context['last_choice_id']; $i++) { + for ($i = 0; $i <= 4; $i++) { $this->addChoice([ 'id' => $i, 'number' => $i + 1, 'label' => '', - 'is_last' => $i === Utils::$context['last_choice_id'], ], true); } } diff --git a/Themes/default/Poll.template.php b/Themes/default/Poll.template.php index 9320a54c3fa..6ab9079a61e 100644 --- a/Themes/default/Poll.template.php +++ b/Themes/default/Poll.template.php @@ -19,27 +19,6 @@ */ function template_main() { - // Some javascript for adding more options. - echo ' - '; - if (!empty(Utils::$context['poll_error']['messages'])) echo '
@@ -67,7 +46,7 @@ function addPollOption()
', Lang::$txt['poll_question'], ': -
+
', Lang::$txt['poll_question'], ':
'; @@ -89,9 +68,7 @@ function addPollOption() } echo ' -

- (', Lang::$txt['poll_add_option'], ')
', Lang::$txt['poll_options'], ': diff --git a/Themes/default/Post.template.php b/Themes/default/Post.template.php index 8316b1a4af1..78108db89e9 100644 --- a/Themes/default/Post.template.php +++ b/Themes/default/Post.template.php @@ -42,28 +42,6 @@ function template_main() echo ' };'; - // If this is a poll - use some javascript to ensure the user doesn't create a poll with illegal option combinations. - if (Utils::$context['make_poll']) - echo ' - var pollOptionNum = 0, pollTabIndex; - var pollOptionId = ', Utils::$context['last_choice_id'], '; - function addPollOption() - { - if (pollOptionNum == 0) - { - for (var i = 0, n = document.forms.postmodify.elements.length; i < n; i++) - if (document.forms.postmodify.elements[i].id.substr(0, 8) == \'options-\') - { - pollOptionNum++; - pollTabIndex = document.forms.postmodify.elements[i].tabIndex; - } - } - pollOptionNum++ - pollOptionId++ - - setOuterHTML(document.getElementById(\'pollMoreOptions\'), ', Utils::JavaScriptEscape('
:

'), '); - }'; - // If we are making a calendar event we want to ensure we show the current days in a month etc... this is done here. if (Utils::$context['make_event']) echo ' @@ -204,7 +182,7 @@ function addPollOption()
', Lang::$txt['poll_question'], ' -
+
', Lang::$txt['poll_question'], '
@@ -221,9 +199,7 @@ function addPollOption()
'; echo ' -

- (', Lang::$txt['poll_add_option'], ')
', Lang::$txt['poll_options'], ' diff --git a/Themes/default/scripts/post.js b/Themes/default/scripts/post.js new file mode 100644 index 00000000000..fad0853a707 --- /dev/null +++ b/Themes/default/scripts/post.js @@ -0,0 +1,25 @@ +function addPollOption(container, e) { + var pollOptionNum = 0; + var pollOptionId = 0; + + for (const el of e.target.form.elements) { + if (el.id.substr(0, 8) == 'options-') { + pollOptionNum++; + pollOptionId = el.id.match(/\d+/)[0]; + } + } + + pollOptionNum++ + pollOptionId++ + + container.insertAdjacentHTML('beforeend', '
:
'); +} + +window.addEventListener('load', function () { + const el = document.forms.postmodify.poll_main.children[1]; + const addMoreButton = document.createElement('button'); + addMoreButton.textContent = el.dataset.moreTxt; + addMoreButton.className = 'button'; + addMoreButton.addEventListener('click', addPollOption.bind(null, el)); + el.after(addMoreButton); +}); \ No newline at end of file