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

Enabling more formats to input hours and minutes in preferences #1020

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
4 changes: 2 additions & 2 deletions src/preferences.html
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When one inputs something wrong, the html message asks to follow the requested format, but that is not explicit.
Is there a way to customize that html message?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, consider if it's possible to implement extra tests for this behavior to ensure it's always working.

Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@
</div>
<div class="flex-box">
<p data-i18n="$Preferences.hoursPerDay">Hours per day</p>
<input data-i18n="[placeholder]$Preferences.hours-per-day;[oninvalid]$Generic.hours-on-invalid" type="text" name="hours-per-day" id="hours-per-day" maxlength=5 pattern="^(0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$" value="08:00" size=5 required oninput="this.setCustomValidity('');this.reportValidity()" onblur="this.value = this.checkValidity() ? this.value : '08:00';this.setCustomValidity('')">
<input data-i18n="[placeholder]$Preferences.hours-per-day;[oninvalid]$Generic.hours-on-invalid" type="text" name="hours-per-day" id="hours-per-day" maxlength=5 pattern="^((0|1)?[0-9]|2[0-3])(\.[0-9][0-9]?|:[0-5][0-9])?$" value="08:00" size=5 required oninput="this.setCustomValidity('');this.reportValidity()" onblur="this.value = this.checkValidity() ? this.value : '08:00';this.setCustomValidity('')">
</div>
<div class="flex-box">
<p><i class="fas fa-utensils"></i><span data-i18n="$Preferences.enablePrefillBreakTime">Enable prefilling of break time</span></p>
<label class="switch"><input type="checkbox" id='enable-prefill-break-time' name="enable-prefill-break-time"><span class="slider round"></span></label>
</div>
<div class="flex-box">
<p data-i18n="$Preferences.breakTimeInterval">Break time interval</p>
<input data-i18n="[placeholder]$Preferences.hours-per-day" type="text" name="break-time-interval" id="break-time-interval" maxlength=5 pattern="^(0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$" value="00:30" size=5 required oninput="this.reportValidity()" onblur="this.value = this.checkValidity() ? this.value : '00:30'">
<input data-i18n="[placeholder]$Preferences.hours-per-day" type="text" name="break-time-interval" id="break-time-interval" maxlength=5 pattern="^((0|1)?[0-9]|2[0-3])(\.[0-9][0-9]?|:[0-5][0-9])?$" value="00:30" size=5 required oninput="this.reportValidity()" onblur="this.value = this.checkValidity() ? this.value : '00:30'">
</div>

</section>
Expand Down
41 changes: 40 additions & 1 deletion src/preferences.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,46 @@
/* istanbul ignore else */
if (this.checkValidity() === true)
{
changeValue(this.name, this.value);
let entry = this.value;
araujoarthur0 marked this conversation as resolved.
Show resolved Hide resolved
const colonIdx = entry.indexOf(':');
const periodIdx = entry.indexOf('.');
if (colonIdx !== -1)
{
// contains :
araujoarthur0 marked this conversation as resolved.
Show resolved Hide resolved
/* istanbul ignore else */
araujoarthur0 marked this conversation as resolved.
Show resolved Hide resolved
if (colonIdx <= 1)
{
// only 1 digit before :
entry = '0'.concat(entry);

Check warning on line 114 in src/preferences.js

View check run for this annotation

Codecov / codecov/patch

src/preferences.js#L114

Added line #L114 was not covered by tests
}
}
else if (periodIdx !== -1)

Check warning on line 117 in src/preferences.js

View check run for this annotation

Codecov / codecov/patch

src/preferences.js#L117

Added line #L117 was not covered by tests
{
// contains .
let n = parseFloat('0'.concat(entry.substring(periodIdx)));
araujoarthur0 marked this conversation as resolved.
Show resolved Hide resolved
n *= 60;
n = Math.floor(n).toString();
n = n.length < 2 ? '0'.concat(n) : n.substring(0, 2);
entry = entry.substring(0, periodIdx).concat(':').concat(n);

Check warning on line 124 in src/preferences.js

View check run for this annotation

Codecov / codecov/patch

src/preferences.js#L120-L124

Added lines #L120 - L124 were not covered by tests
/* istanbul ignore else */
if (periodIdx <= 1)

Check warning on line 126 in src/preferences.js

View check run for this annotation

Codecov / codecov/patch

src/preferences.js#L126

Added line #L126 was not covered by tests
{
// only 1 digit before .
entry = '0'.concat(entry);

Check warning on line 129 in src/preferences.js

View check run for this annotation

Codecov / codecov/patch

src/preferences.js#L129

Added line #L129 was not covered by tests
}
}
else
{

Check warning on line 133 in src/preferences.js

View check run for this annotation

Codecov / codecov/patch

src/preferences.js#L133

Added line #L133 was not covered by tests
// no . or :
/* istanbul ignore else */
if (entry.length < 2)

Check warning on line 136 in src/preferences.js

View check run for this annotation

Codecov / codecov/patch

src/preferences.js#L136

Added line #L136 was not covered by tests
{
entry = '0'.concat(entry);

Check warning on line 138 in src/preferences.js

View check run for this annotation

Codecov / codecov/patch

src/preferences.js#L138

Added line #L138 was not covered by tests
}
entry = entry.concat(':00');

Check warning on line 140 in src/preferences.js

View check run for this annotation

Codecov / codecov/patch

src/preferences.js#L140

Added line #L140 was not covered by tests
}
this.value = entry;
changeValue(this.name, entry);
}
});

Expand Down