Skip to content

Commit 6141af3

Browse files
committed
extract poll js
Signed-off-by: John Rayes <[email protected]>
1 parent 98d7f00 commit 6141af3

File tree

5 files changed

+30
-60
lines changed

5 files changed

+30
-60
lines changed

Sources/Actions/Post.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ public function show(): void
375375
Theme::loadJavaScriptFile('drafts.js', ['defer' => false, 'minimize' => true], 'smf_drafts');
376376
}
377377

378-
// quotedText.js
378+
Theme::loadJavaScriptFile('post.js', ['defer' => true, 'minimize' => true], 'smf_post');
379379
Theme::loadJavaScriptFile('quotedText.js', ['defer' => true, 'minimize' => true], 'smf_quotedText');
380380

381381
// Knowing the current board ID might be handy.
@@ -954,8 +954,6 @@ protected function showPreview(): void
954954
'is_last' => false,
955955
];
956956
}
957-
Utils::$context['last_choice_id'] = $choice_id;
958-
Utils::$context['choices'][count(Utils::$context['choices']) - 1]['is_last'] = true;
959957
}
960958

961959
// Are you... a guest?

Sources/Poll.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1196,10 +1196,8 @@ public static function edit(): void
11961196
Utils::$context['poll'] = $poll->format();
11971197
Utils::$context['choices'] = &Utils::$context['poll']['choices'];
11981198

1199-
Utils::$context['last_choice_id'] = array_key_last(Utils::$context['poll']['choices']);
1200-
Utils::$context['poll']['choices'][Utils::$context['last_choice_id']]['is_last'] = true;
1201-
12021199
Utils::$context['page_title'] = Utils::$context['is_edit'] ? Lang::$txt['poll_edit'] : Lang::$txt['add_poll'];
1200+
Theme::loadJavaScriptFile('post.js', ['defer' => true, 'minimize' => true], 'smf_post');
12031201

12041202
// Build the link tree.
12051203
Utils::$context['linktree'][] = [
@@ -1473,15 +1471,11 @@ protected function initNewPoll(): void
14731471
'guest_vote' => !empty($_POST['poll_guest_vote']),
14741472
]);
14751473

1476-
// Make all five poll choices empty.
1477-
Utils::$context['last_choice_id'] = 4;
1478-
1479-
for ($i = 0; $i <= Utils::$context['last_choice_id']; $i++) {
1474+
for ($i = 0; $i <= 4; $i++) {
14801475
$this->addChoice([
14811476
'id' => $i,
14821477
'number' => $i + 1,
14831478
'label' => '',
1484-
'is_last' => $i === Utils::$context['last_choice_id'],
14851479
], true);
14861480
}
14871481
}

Themes/default/Poll.template.php

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,27 +19,6 @@
1919
*/
2020
function template_main()
2121
{
22-
// Some javascript for adding more options.
23-
echo '
24-
<script>
25-
var pollOptionNum = 0;
26-
var pollOptionId = ', Utils::$context['last_choice_id'], ';
27-
28-
function addPollOption()
29-
{
30-
if (pollOptionNum == 0)
31-
{
32-
for (var i = 0; i < document.forms.postmodify.elements.length; i++)
33-
if (document.forms.postmodify.elements[i].id.substr(0, 8) == "options-")
34-
pollOptionNum++;
35-
}
36-
pollOptionNum++
37-
pollOptionId++
38-
39-
setOuterHTML(document.getElementById("pollMoreOptions"), \'<dt><label for="options-\' + pollOptionId + \'" ', (isset(Utils::$context['poll_error']['no_question']) ? ' class="error"' : ''), '>', Lang::$txt['option'], ' \' + pollOptionNum + \'</label>:</dt><dd><input type="text" name="options[\' + (pollOptionId) + \']" id="options-\' + (pollOptionId) + \'" value="" size="80" maxlength="255"></dd><p id="pollMoreOptions"></p\');
40-
}
41-
</script>';
42-
4322
if (!empty(Utils::$context['poll_error']['messages']))
4423
echo '
4524
<div class="errorbox">
@@ -67,7 +46,7 @@ function addPollOption()
6746
<input type="hidden" name="poll" value="', Utils::$context['poll']['id'], '">
6847
<fieldset id="poll_main">
6948
<legend><span ', (isset(Utils::$context['poll_error']['no_question']) ? ' class="error"' : ''), '>', Lang::$txt['poll_question'], ':</span></legend>
70-
<dl class="settings poll_options">
49+
<dl class="settings poll_options" data-more-txt="', Lang::$txt['poll_add_option'], '" data-option-txt="', Lang::$txt['option'], '">
7150
<dt>', Lang::$txt['poll_question'], ':</dt>
7251
<dd><input type="text" name="question" size="80" value="', Utils::$context['poll']['question'], '"></dd>';
7352

@@ -89,9 +68,7 @@ function addPollOption()
8968
}
9069

9170
echo '
92-
<p id="pollMoreOptions"></p>
9371
</dl>
94-
<strong><a href="javascript:addPollOption(); void(0);">(', Lang::$txt['poll_add_option'], ')</a></strong>
9572
</fieldset>
9673
<fieldset id="poll_options">
9774
<legend>', Lang::$txt['poll_options'], ':</legend>

Themes/default/Post.template.php

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -42,28 +42,6 @@ function template_main()
4242
echo '
4343
};';
4444

45-
// If this is a poll - use some javascript to ensure the user doesn't create a poll with illegal option combinations.
46-
if (Utils::$context['make_poll'])
47-
echo '
48-
var pollOptionNum = 0, pollTabIndex;
49-
var pollOptionId = ', Utils::$context['last_choice_id'], ';
50-
function addPollOption()
51-
{
52-
if (pollOptionNum == 0)
53-
{
54-
for (var i = 0, n = document.forms.postmodify.elements.length; i < n; i++)
55-
if (document.forms.postmodify.elements[i].id.substr(0, 8) == \'options-\')
56-
{
57-
pollOptionNum++;
58-
pollTabIndex = document.forms.postmodify.elements[i].tabIndex;
59-
}
60-
}
61-
pollOptionNum++
62-
pollOptionId++
63-
64-
setOuterHTML(document.getElementById(\'pollMoreOptions\'), ', Utils::JavaScriptEscape('<dt><label for="options-'), ' + pollOptionId + ', Utils::JavaScriptEscape('">' . Lang::$txt['option'] . ' '), ' + pollOptionNum + ', Utils::JavaScriptEscape('</label>:</dt><dd><input type="text" name="options['), ' + pollOptionId + ', Utils::JavaScriptEscape(']" id="options-'), ' + pollOptionId + ', Utils::JavaScriptEscape('" value="" size="80" maxlength="255" tabindex="'), ' + pollTabIndex + ', Utils::JavaScriptEscape('"></dd><p id="pollMoreOptions"></p>'), ');
65-
}';
66-
6745
// If we are making a calendar event we want to ensure we show the current days in a month etc... this is done here.
6846
if (Utils::$context['make_event'])
6947
echo '
@@ -204,7 +182,7 @@ function addPollOption()
204182
<div id="edit_poll">
205183
<fieldset id="poll_main">
206184
<legend><span ', (isset(Utils::$context['poll_error']['no_question']) ? ' class="error"' : ''), '>', Lang::$txt['poll_question'], '</span></legend>
207-
<dl class="settings poll_options">
185+
<dl class="settings poll_options" data-more-txt="', Lang::$txt['poll_add_option'], '" data-option-txt="', Lang::$txt['option'], '">
208186
<dt>', Lang::$txt['poll_question'], '</dt>
209187
<dd>
210188
<input type="text" name="question" value="', isset(Utils::$context['question']) ? Utils::$context['question'] : '', '" tabindex="', Utils::$context['tabindex']++, '" size="80">
@@ -221,9 +199,7 @@ function addPollOption()
221199
</dd>';
222200

223201
echo '
224-
<p id="pollMoreOptions"></p>
225202
</dl>
226-
<strong><a href="javascript:addPollOption(); void(0);">(', Lang::$txt['poll_add_option'], ')</a></strong>
227203
</fieldset>
228204
<fieldset id="poll_options">
229205
<legend>', Lang::$txt['poll_options'], '</legend>

Themes/default/scripts/post.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
function addPollOption(container, e) {
2+
var pollOptionNum = 0;
3+
var pollOptionId = 0;
4+
5+
for (const el of e.target.form.elements) {
6+
if (el.id.substr(0, 8) == 'options-') {
7+
pollOptionNum++;
8+
pollOptionId = el.id.match(/\d+/)[0];
9+
}
10+
}
11+
12+
pollOptionNum++
13+
pollOptionId++
14+
15+
container.insertAdjacentHTML('beforeend', '<dt><label for="options-' + pollOptionId + '">' + container.dataset.optionTxt + ' ' + pollOptionNum + '</label>:</dt><dd><input type="text" name="options[' + pollOptionId + ']" id="options-' + pollOptionId + '" value="" size="80" maxlength="255"></dd>');
16+
}
17+
18+
window.addEventListener('load', function () {
19+
const el = document.forms.postmodify.poll_main.children[1];
20+
const addMoreButton = document.createElement('button');
21+
addMoreButton.textContent = el.dataset.moreTxt;
22+
addMoreButton.className = 'button';
23+
addMoreButton.addEventListener('click', addPollOption.bind(null, el));
24+
el.after(addMoreButton);
25+
});

0 commit comments

Comments
 (0)