[feature/new-tz-handling] Fix javascript in prosilver

Use new coding guidelines, wrap code with (function($)) and use phpbb. prefix

PHPBB3-9558
This commit is contained in:
Joas Schilling 2012-06-19 15:41:18 +02:00
parent 72c3148cfe
commit 79a0390970
2 changed files with 50 additions and 54 deletions

View file

@ -1,35 +1,30 @@
(function($) { // Avoid conflicts with other libraries
/** /**
* Hide the optgroups that are not the selected timezone * Hide the optgroups that are not the selected timezone
* *
* @param bool keep_selection Shall we keep the value selected, or shall the user be forced to repick one. * @param bool keep_selection Shall we keep the value selected, or shall the user be forced to repick one.
*/ */
function phpbb_switch_tz_date(keep_selection) phpbb.timezone_switch_date = function(keep_selection) {
{ $('#timezone > optgroup').css('display', 'none');
$('#timezone > optgroup').css("display", "none"); $("#timezone > optgroup[label='" + $('#tz_date').val() + "']").css('display', 'block');
$("#timezone > optgroup[label='" + $('#tz_date').val() + "']").css("display", "block");
if ($("#timezone > optgroup[label='" + $('#tz_date').val() + "'] > option").size() == 1) if ($("#timezone > optgroup[label='" + $('#tz_date').val() + "'] > option").size() == 1) {
{
// If there is only one timezone for the selected date, we just select that automatically. // If there is only one timezone for the selected date, we just select that automatically.
$("#timezone > optgroup[label='" + $('#tz_date').val() + "'] > option:first").attr("selected", true); $("#timezone > optgroup[label='" + $('#tz_date').val() + "'] > option:first").attr('selected', true);
keep_selection = true; keep_selection = true;
} }
if (typeof keep_selection !== 'undefined') if (typeof keep_selection !== 'undefined' && !keep_selection) {
{ $('#timezone > option:first').attr('selected', true);
if (!keep_selection)
{
$('#timezone > option:first').attr("selected", true);
}
} }
} }
/** /**
* Display the date/time select * Display the date/time select
*/ */
function phpbb_enable_tz_dates() phpbb.timezone_enable_date_selection = function() {
{ $('#tz_select_date').css('display', 'block');
$('#tz_select_date').css("display", "block");
} }
/** /**
@ -37,72 +32,73 @@ function phpbb_enable_tz_dates()
* *
* @param bool force_selector Shall we select the suggestion? * @param bool force_selector Shall we select the suggestion?
*/ */
function phpbb_preselect_tz_select(force_selector) phpbb.timezone_preselect_select = function(force_selector) {
{
// The offset returned here is in minutes and negated. // The offset returned here is in minutes and negated.
// http://www.w3schools.com/jsref/jsref_getTimezoneOffset.asp // http://www.w3schools.com/jsref/jsref_getTimezoneOffset.asp
var offset = (new Date()).getTimezoneOffset(); var offset = (new Date()).getTimezoneOffset();
if (offset < 0)
{ if (offset < 0) {
var sign = '+'; var sign = '+';
offset = -offset; offset = -offset;
} } else {
else
{
var sign = '-'; var sign = '-';
} }
var minutes = offset % 60; var minutes = offset % 60;
var hours = (offset - minutes) / 60; var hours = (offset - minutes) / 60;
if (hours < 10)
{ if (hours < 10) {
hours = '0' + hours.toString(); hours = '0' + hours.toString();
} } else {
else
{
hours = hours.toString(); hours = hours.toString();
} }
if (minutes < 10)
{ if (minutes < 10) {
minutes = '0' + minutes.toString(); minutes = '0' + minutes.toString();
} } else {
else
{
minutes = minutes.toString(); minutes = minutes.toString();
} }
var prefix = 'GMT' + sign + hours + ':' + minutes; var prefix = 'GMT' + sign + hours + ':' + minutes;
var prefix_length = prefix.length; var prefix_length = prefix.length;
var selector_options = $('#tz_date > option'); var selector_options = $('#tz_date > option');
for (var i = 0; i < selector_options.length; ++i)
{ for (var i = 0; i < selector_options.length; ++i) {
var option = selector_options[i]; var option = selector_options[i];
if (option.value.substring(0, prefix_length) == prefix)
{ if (option.value.substring(0, prefix_length) == prefix) {
if ($('#tz_date').val() != option.value && !force_selector) if ($('#tz_date').val() != option.value && !force_selector) {
{
// We do not select the option for the user, but notify him, // We do not select the option for the user, but notify him,
// that we would suggest a different setting. // that we would suggest a different setting.
$('#tz_select_date_suggest').css("display", "inline"); $('#tz_select_date_suggest').css('display', 'inline');
$('#tz_select_date_suggest').attr("title", $('#tz_select_date_suggest').attr('data-l-suggestion').replace("%s", option.innerHTML)); $('#tz_select_date_suggest').attr('title', $('#tz_select_date_suggest').attr('data-l-suggestion').replace("%s", option.innerHTML));
$('#tz_select_date_suggest').attr("value", $('#tz_select_date_suggest').attr('data-l-suggestion').replace("%s", option.innerHTML.substring(0, 9))); $('#tz_select_date_suggest').attr('value', $('#tz_select_date_suggest').attr('data-l-suggestion').replace("%s", option.innerHTML.substring(0, 9)));
phpbb_switch_tz_date(true); phpbb.timezone_switch_date(true);
} } else {
else
{
option.selected = true; option.selected = true;
phpbb_switch_tz_date(!force_selector); phpbb.timezone_switch_date(!force_selector);
$('#tz_select_date_suggest').css("display", "none"); $('#tz_select_date_suggest').css('display', 'none');
} }
break; break;
} }
} }
} }
$('#tz_select_date_suggest').click(function(){ })(jQuery); // Avoid conflicts with other libraries
phpbb_preselect_tz_select(true, '');
$('#tz_date').change(function() {
phpbb.timezone_switch_date(false);
}); });
phpbb_enable_tz_dates(); $('#tz_select_date_suggest').click(function(){
phpbb_preselect_tz_select($('#tz_select_date_suggest').attr('data-is-registration') == "true"); phpbb.timezone_preselect_select(true);
});
$(document).ready(
phpbb.timezone_enable_date_selection
);
$(document).ready(
phpbb.timezone_preselect_select($('#tz_select_date_suggest').attr('data-is-registration') == 'true')
);

View file

@ -2,7 +2,7 @@
<dt><label for="timezone">{L_BOARD_TIMEZONE}:</label></dt> <dt><label for="timezone">{L_BOARD_TIMEZONE}:</label></dt>
<!-- IF S_TZ_DATE_OPTIONS --> <!-- IF S_TZ_DATE_OPTIONS -->
<dd id="tz_select_date" style="display: none;"> <dd id="tz_select_date" style="display: none;">
<select name="tz_date" id="tz_date" class="autowidth tz_select" onchange="phpbb_switch_tz_date(false);"> <select name="tz_date" id="tz_date" class="autowidth tz_select">
<option value="">{L_SELECT_CURRENT_TIME}</option> <option value="">{L_SELECT_CURRENT_TIME}</option>
{S_TZ_DATE_OPTIONS} {S_TZ_DATE_OPTIONS}
</select> </select>