Prefill public completion form with current timer if present, and allow refresh
This commit is contained in:
@@ -48,19 +48,26 @@
|
|||||||
= form.check_box :completed, class: "form-check-input"
|
= form.check_box :completed, class: "form-check-input"
|
||||||
= form.label :completed
|
= form.label :completed
|
||||||
javascript:
|
javascript:
|
||||||
|
savetime = "";
|
||||||
|
ispatch = #{method == :patch};
|
||||||
completedEl = document.getElementById('completion_completed');
|
completedEl = document.getElementById('completion_completed');
|
||||||
completedEl.addEventListener('change', (e) => {
|
completedEl.addEventListener('change', (e) => {
|
||||||
const timeEl = document.getElementById('time');
|
const timeEl = document.getElementById('time');
|
||||||
const missingPiecesEl = document.getElementById('missing_pieces');
|
const missingPiecesEl = document.getElementById('missing_pieces');
|
||||||
const remainingPiecesEl = document.getElementById('remaining_pieces');
|
const remainingPiecesEl = document.getElementById('remaining_pieces');
|
||||||
|
const refreshEl = document.getElementById('refresh');
|
||||||
if (e.target.checked) {
|
if (e.target.checked) {
|
||||||
timeEl.value = '#{@completion.display_time_from_start}';
|
if (ispatch) timeEl.value = '#{@completion.display_time_from_start}';
|
||||||
|
else timeEl.value = savetime;
|
||||||
missingPiecesEl.style.display = 'block';
|
missingPiecesEl.style.display = 'block';
|
||||||
remainingPiecesEl.style.display = 'none';
|
remainingPiecesEl.style.display = 'none';
|
||||||
|
if (refreshEl) refreshEl.style.display = '';
|
||||||
} else {
|
} else {
|
||||||
|
savetime = timeEl.value;
|
||||||
timeEl.value = '#{display_time(@contest.duration_seconds)}';
|
timeEl.value = '#{display_time(@contest.duration_seconds)}';
|
||||||
missingPiecesEl.style.display = 'none';
|
missingPiecesEl.style.display = 'none';
|
||||||
remainingPiecesEl.style.display = 'block';
|
remainingPiecesEl.style.display = 'block';
|
||||||
|
if (refreshEl) refreshEl.style.display = 'none';
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.row.mb-3
|
.row.mb-3
|
||||||
@@ -68,8 +75,32 @@
|
|||||||
.form-floating
|
.form-floating
|
||||||
= form.text_field :display_time_from_start, autocomplete: "off", class: "form-control", id: "time"
|
= form.text_field :display_time_from_start, autocomplete: "off", class: "form-control", id: "time"
|
||||||
= form.label :display_time_from_start, class: "required"
|
= form.label :display_time_from_start, class: "required"
|
||||||
|
- unless method == :patch
|
||||||
|
.col.mt-2
|
||||||
|
a.btn.btn-primary id="refresh"
|
||||||
|
= t("helpers.buttons.refresh")
|
||||||
.form-text
|
.form-text
|
||||||
= t("activerecord.attributes.completion.display_time_from_start_description")
|
= t("activerecord.attributes.completion.display_time_from_start_description")
|
||||||
|
- if @contest.start_time.present? && method != :patch
|
||||||
|
javascript:
|
||||||
|
startTime = #{@contest.start_time.present? ? @contest.start_time.to_i : "null"};
|
||||||
|
pauseTime = #{@contest.pause_time.present? ? @contest.pause_time.to_i : "null"};
|
||||||
|
function updateTime() {
|
||||||
|
const timeEl = document.getElementById('time');
|
||||||
|
if (timeEl) {
|
||||||
|
if (startTime) {
|
||||||
|
let s = Math.floor((Date.now() - 1000 * startTime) / 1000);
|
||||||
|
if (pauseTime) s = Math.floor(pauseTime - startTime);
|
||||||
|
let ss = s % 60;
|
||||||
|
let mm = Math.floor(s / 60) % 60;
|
||||||
|
let hh = Math.floor(s / 3600);
|
||||||
|
timeEl.value = `${hh}:${mm < 10 ? `0${mm}` : mm}:${ss < 10 ? `0${ss}` : ss}`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
refreshButton = document.getElementById("refresh");
|
||||||
|
refreshButton?.addEventListener('click', updateTime);
|
||||||
|
updateTime();
|
||||||
.row.mb-3 id="missing_pieces"
|
.row.mb-3 id="missing_pieces"
|
||||||
.col
|
.col
|
||||||
.form-floating
|
.form-floating
|
||||||
|
|||||||
Reference in New Issue
Block a user