Skip to content

Commit

Permalink
move more js to dedicated files
Browse files Browse the repository at this point in the history
  • Loading branch information
live627 committed Dec 17, 2024
1 parent 70a5392 commit df4b491
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 136 deletions.
1 change: 1 addition & 0 deletions Sources/Actions/Admin/Subscriptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -846,6 +846,7 @@ public function modifyUser(): void
// Setup the template.
Utils::$context['sub_template'] = 'modify_user_subscription';
Utils::$context['page_title'] = Lang::$txt[Utils::$context['action_type'] . '_subscriber'];
Theme::loadJavaScriptFile('paidsubs.js', ['defer' => true, 'minimize' => true], 'smf_paidsubs');

// If we haven't been passed the subscription ID get it.
if (Utils::$context['log_id'] && !Utils::$context['sub_id']) {
Expand Down
34 changes: 3 additions & 31 deletions Themes/default/Admin.template.php
Original file line number Diff line number Diff line change
Expand Up @@ -725,7 +725,7 @@ function template_not_done()
<form action="', Config::$scripturl, Utils::$context['continue_get_data'], '" method="post" accept-charset="', Utils::$context['character_set'], '" name="autoSubmit" id="autoSubmit">';

// Do we have a token?
if (isset(Utils::$context['not_done_token']) && isset(Utils::$context[Utils::$context['not_done_token'] . '_token'], Utils::$context[Utils::$context['not_done_token'] . '_token_var']))
if (isset(Utils::$context['not_done_token'], Utils::$context[Utils::$context['not_done_token'] . '_token'], Utils::$context[Utils::$context['not_done_token'] . '_token_var']))
echo '
<input type="hidden" name="', Utils::$context[Utils::$context['not_done_token'] . '_token_var'], '" value="', Utils::$context[Utils::$context['not_done_token'] . '_token'], '">';

Expand All @@ -735,21 +735,7 @@ function template_not_done()
</form>
</div><!-- .windowbg -->
<script>
var countdown = ', Utils::$context['continue_countdown'], ';
doAutoSubmit();
function doAutoSubmit()
{
if (countdown == 0)
document.forms.autoSubmit.submit();
else if (countdown == -1)
return;
document.forms.autoSubmit.cont.value = "', Lang::$txt['not_done_continue'], ' (" + countdown + ")";
countdown--;
setTimeout(doAutoSubmit, 1000);
}
doAutoSubmit(', Utils::$context['continue_countdown'], ', ', Lang::$txt['not_done_continue'], ');
</script>';
}

Expand Down Expand Up @@ -1526,21 +1512,7 @@ function template_repair_boards()
{
echo '
<script>
var countdown = 5;
doAutoSubmit();
function doAutoSubmit()
{
if (countdown == 0)
document.forms.recount_form.submit();
else if (countdown == -1)
return;
document.forms.recount_form.recount_now.value = "', Lang::$txt['errors_recount_now'], ' (" + countdown + ")";
countdown--;
setTimeout(doAutoSubmit, 1000);
}
doAutoSubmit(5, ', Utils::escapeJavaScript(Lang::$txt['errors_recount_now']), ', "recount_form", "recount_now");
</script>';
}
}
Expand Down
22 changes: 4 additions & 18 deletions Themes/default/ManageNews.template.php
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ function template_email_members_send()
<div class="bar" style="width: ', Utils::$context['percentage_done'], '%;"></div>
</div>
<hr>
<input type="submit" name="b" value="', Lang::$txt['email_continue'], '" class="button">
<input type="submit" name="cont" value="', Lang::$txt['email_continue'], '" class="button">
<input type="hidden" name="', Utils::$context['session_var'], '" value="', Utils::$context['session_id'], '">
<input type="hidden" name="subject" value="', Utils::$context['subject'], '">
<input type="hidden" name="message" value="', Utils::$context['message'], '">
Expand All @@ -404,23 +404,9 @@ function template_email_members_send()
</div><!-- .windowbg -->
</form>
<script>
var countdown = 2;
doAutoSubmit();
function doAutoSubmit()
{
if (countdown == 0)
document.forms.autoSubmit.submit();
else if (countdown == -1)
return;
document.forms.autoSubmit.b.value = "', Lang::$txt['email_continue'], ' (" + countdown + ")";
countdown--;
setTimeout("doAutoSubmit();", 1000);
}
</script>';
<script>
doAutoSubmit(2, ', Utils::escapeJavaScript(Lang::$txt['email_continue']), ');
</script>';
}

/**
Expand Down
18 changes: 8 additions & 10 deletions Themes/default/ManagePaid.template.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,12 +211,6 @@ function template_delete_subscription()
*/
function template_modify_user_subscription()
{
// Some quickly stolen javascript from Post, could do with being more efficient :)
echo '
<script>
var monthLength = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
</script>';

echo '
<form action="', Config::$scripturl, '?action=admin;area=paidsubscribe;sa=modifyuser;sid=', Utils::$context['sub_id'], ';lid=', Utils::$context['log_id'], '" method="post">
<div class="cat_bar">
Expand Down Expand Up @@ -257,7 +251,7 @@ function template_modify_user_subscription()
</dl>
<fieldset>
<legend>', Lang::$txt['start_date_and_time'], '</legend>
<select name="year" id="year" onchange="generateDays();">';
<select name="year" id="year">';

// Show a list of all the years we allow...
for ($year = 2005; $year <= 2030; $year++)
Expand All @@ -266,7 +260,7 @@ function template_modify_user_subscription()

echo '
</select>
<select name="month" id="month" onchange="generateDays();">';
<select name="month" id="month">';

// There are 12 months per year - ensure that they all get listed.
for ($month = 1; $month <= 12; $month++)
Expand All @@ -291,7 +285,7 @@ function template_modify_user_subscription()
</fieldset>
<fieldset>
<legend>', Lang::$txt['end_date_and_time'], '</legend>
<select name="yearend" id="yearend" onchange="generateDays(\'end\');">';
<select name="yearend" id="yearend">';

// Show a list of all the years we allow...
for ($year = 2005; $year <= 2030; $year++)
Expand All @@ -300,7 +294,7 @@ function template_modify_user_subscription()

echo '
</select>
<select name="monthend" id="monthend" onchange="generateDays(\'end\');">';
<select name="monthend" id="monthend">';

// There are 12 months per year - ensure that they all get listed.
for ($month = 1; $month <= 12; $month++)
Expand Down Expand Up @@ -336,6 +330,10 @@ function template_modify_user_subscription()
sSearchType: \'member\',
sTextDeleteItem: \'', Lang::$txt['autosuggest_delete_item'], '\',
});
document.getElementById("year").addEventListener("change", generateDays);
document.getElementById("month").addEventListener("change", generateDays);
document.getElementById("yearend").addEventListener("change", generateDays.bind(null, "end"));
document.getElementById("monthend").addEventListener("change", generateDays.bind(null, "end"));
</script>';

if (!empty(Utils::$context['pending_payments']))
Expand Down
19 changes: 2 additions & 17 deletions Themes/default/ManageSearch.template.php
Original file line number Diff line number Diff line change
Expand Up @@ -310,31 +310,16 @@ function template_create_index_progress()
</div>
</div>
<hr>
<input type="submit" name="b" value="', Lang::$txt['search_create_index_continue'], '" class="button">
<input type="submit" name="cont" value="', Lang::$txt['search_create_index_continue'], '" class="button">
</div>
<input type="hidden" name="step" value="', Utils::$context['step'], '">
<input type="hidden" name="start" value="', Utils::$context['start'], '">
<input type="hidden" name="bytes_per_word" value="', Utils::$context['index_settings']['bytes_per_word'], '">
<input type="hidden" name="', Utils::$context['session_var'], '" value="', Utils::$context['session_id'], '">
</form>
<script>
var countdown = 10;
doAutoSubmit();
function doAutoSubmit()
{
if (countdown == 0)
document.forms.autoSubmit.submit();
else if (countdown == -1)
return;
document.forms.autoSubmit.b.value = "', Lang::$txt['search_create_index_continue'], ' (" + countdown + ")";
countdown--;
setTimeout(doAutoSubmit, 1000);
}
doAutoSubmit(10, ', Utils::escapeJavaScript(Lang::$txt['search_create_index_continue']), ');
</script>';

}

/**
Expand Down
16 changes: 1 addition & 15 deletions Themes/default/Packages.template.php
Original file line number Diff line number Diff line change
Expand Up @@ -1834,21 +1834,7 @@ function template_action_permissions()
// Just the countdown stuff
echo '
<script>
var countdown = ', $countDown, ';
doAutoSubmit();
function doAutoSubmit()
{
if (countdown == 0)
document.forms.perm_submit.submit();
else if (countdown == -1)
return;
document.getElementById(\'cont\').value = "', Lang::$txt['not_done_continue'], ' (" + countdown + ")";
countdown--;
setTimeout(doAutoSubmit, 1000);
}
doAutoSubmit(', $countDown, ', ', Utils::escapeJavaScript(Lang::$txt['not_done_continue']), '"perm_submit", "go");
</script>';
}

Expand Down
18 changes: 2 additions & 16 deletions Themes/default/Post.template.php
Original file line number Diff line number Diff line change
Expand Up @@ -822,7 +822,7 @@ function template_announcement_send()
</div>
<hr>
<div id="confirm_buttons">
<input type="submit" name="b" value="', Lang::$txt['announce_continue'], '" class="button">
<input type="submit" name="cont" value="', Lang::$txt['announce_continue'], '" class="button">
<input type="hidden" name="', Utils::$context['session_var'], '" value="', Utils::$context['session_id'], '">
<input type="hidden" name="topic" value="', Utils::$context['current_topic'], '">
<input type="hidden" name="move" value="', Utils::$context['move'], '">
Expand All @@ -836,21 +836,7 @@ function template_announcement_send()
</div><!-- #announcement -->
<br>
<script>
var countdown = 2;
doAutoSubmit();
function doAutoSubmit()
{
if (countdown == 0)
document.forms.autoSubmit.submit();
else if (countdown == -1)
return;
document.forms.autoSubmit.b.value = "', Lang::$txt['announce_continue'], ' (" + countdown + ")";
countdown--;
setTimeout(doAutoSubmit, 1000);
}
doAutoSubmit(2, ', Utils::escapeJavaScript(Lang::$txt['announce_continue']), ');
</script>';
}

Expand Down
41 changes: 41 additions & 0 deletions Themes/default/scripts/paidsubs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
function generateDays(offset = '') {
// Get the DOM elements
const dayElement = document.getElementById('day' + offset);
const yearElement = document.getElementById('year' + offset);
const monthElement = document.getElementById('month' + offset);

// Validate the existence of elements
if (!dayElement || !yearElement || !monthElement) {
console.error('One or more elements are missing. Ensure the IDs are correct.');
return;
}

// Month lengths (default February to 28 days)
const monthLengths = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

// Adjust February for leap years
const year = parseInt(yearElement.options[yearElement.selectedIndex]?.value || 0, 10);
if ((year % 4 === 0 && year % 100 !== 0) || (year % 400 === 0)) {
monthLengths[1] = 29;
}

// Get selected day before updating options
const selectedDay = dayElement.selectedIndex;

// Clear current options
dayElement.innerHTML = '';

// Get the number of days in the selected month
const daysInMonth = monthLengths[parseInt(monthElement.value, 10) - 1] || 31;

// Populate day options
for (let i = 1; i <= daysInMonth; i++) {
const option = document.createElement('option');
option.value = i;
option.textContent = i;
dayElement.appendChild(option);
}

// Restore the previously selected day if valid
dayElement.selectedIndex = Math.min(selectedDay, daysInMonth - 1);
}
58 changes: 29 additions & 29 deletions Themes/default/scripts/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -1485,35 +1485,6 @@ function expandThumb(thumbID)
return false;
}

function generateDays(offset)
{
// Work around JavaScript's lack of support for default values...
offset = typeof(offset) != 'undefined' ? offset : '';

var days = 0, selected = 0;
var dayElement = document.getElementById("day" + offset), yearElement = document.getElementById("year" + offset), monthElement = document.getElementById("month" + offset);

var monthLength = [
31, 28, 31, 30,
31, 30, 31, 31,
30, 31, 30, 31
];
if (yearElement.options[yearElement.selectedIndex].value % 4 == 0)
monthLength[1] = 29;

selected = dayElement.selectedIndex;
while (dayElement.options.length)
dayElement.options[0] = null;

days = monthLength[monthElement.value - 1];

for (i = 1; i <= days; i++)
dayElement.options[dayElement.length] = new Option(i, i);

if (selected < days)
dayElement.selectedIndex = selected;
}

function initSearch()
{
if (document.forms.searchform.search.value.indexOf("%u") != -1)
Expand Down Expand Up @@ -1992,4 +1963,33 @@ smc_preview_post.prototype.onDocSent = function (XMLDoc)
}

location.hash = '#' + this.opts.sPreviewSectionContainerID;
}

function doAutoSubmit(countdown, txtMessage, formName = 'autoSubmit', fieldName = 'cont') {
const form = document.forms[formName];

// Ensure the form exists
if (!form) {
console.error('Form with name "' + formName + '" not found.');
return;
}

// Handle the countdown completion
if (countdown <= 0) {
form.submit();
return;
}

// Update the field if it exists
const contField = form.elements[fieldName];
if (contField) {
contField.value = txtMessage + ' (' + countdown + ')';
} else {
console.warn('Field "' + fieldName + '" not found in form "' + formName + '".');
}

// Schedule the next countdown tick
setTimeout(() => {
doAutoSubmit(countdown - 1, txtMessage, formName, fieldName);
}, 1000);
}

0 comments on commit df4b491

Please sign in to comment.