Skip to content

Commit

Permalink
Redo Chrome workaround with a style-hidden text <input>.
Browse files Browse the repository at this point in the history
This is slightly cleaner than the history-state mechanism. The
style-hidden text <input> is restored by Chrome on navigation.

* When saving the value, change the control's defaultValue. This
  causes Chrome to not generate a change event on navigation. But
  Chrome forgets the new defaultValue on navigation. So....
* Record the new defaultValue in an ____updates____ input, which
  cannot be type=hidden (Chrome forgets the values of hidden
  inputs on navigation) but can be type=text with style=hidden.
  On load, read the defaultValues from ____updates____ into the
  relevant controls. Ugh.
  • Loading branch information
kohler committed Aug 31, 2021
1 parent 6af3505 commit 6dde969
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 44 deletions.
3 changes: 2 additions & 1 deletion conflictassign.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@
$tr = $paperlist->table_render();
if (!$tr->is_empty()) {
if (!$any) {
echo Ht::form(hoturl("conflictassign")),
echo Ht::form($Conf->hoturl("conflictassign")),
Ht::entry("____updates____", "", ["class" => "hidden ignore-diff"]),
$tr->table_start,
Ht::unstash(),
($tr->thead ? : ""),
Expand Down
3 changes: 2 additions & 1 deletion manualassign.php
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,8 @@ function show_ass_elements($pl) {
$pl->apply_view_qreq();
echo Ht::form($Conf->hoturl_post("manualassign", ["reviewer" => $reviewer->email, "sort" => $Qreq->sort]), ["class" => "assignpc ignore-diff"]),
Ht::hidden("t", $Qreq->t),
Ht::hidden("q", $Qreq->q);
Ht::hidden("q", $Qreq->q),
Ht::entry("____updates____", "", ["class" => "hidden ignore-diff"]);
$rev_rounds = $Conf->round_selector_options(false);
$expected_round = $Conf->assignment_round_option(false);

Expand Down
1 change: 1 addition & 0 deletions reviewprefs.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ function show_pref_element($pl, $name, $text, $extra = []) {
}
echo Ht::form($Conf->hoturl_post("reviewprefs", $hoturl_args), ["id" => "sel", "class" => "ui-submit js-submit-paperlist assignpc"]),
Ht::hidden("defaultfn", ""),
Ht::entry("____updates____", "", ["class" => "hidden ignore-diff"]),
Ht::hidden_default_submit("default", 1);
echo "<div class=\"pltable-fullw-container\">\n",
'<noscript><div style="text-align:center">', Ht::submit("fn", "Save changes", ["value" => "saveprefs", "class" => "btn-primary"]), '</div></noscript>';
Expand Down
99 changes: 57 additions & 42 deletions scripts/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -1063,18 +1063,6 @@ function hoturl_absolute_base() {
return siteinfo.absolute_base;
}

function hidden_input(name, value, attr) {
var input = document.createElement("input");
input.type = "hidden";
input.name = name;
input.value = value;
if (attr) {
for (var k in attr)
input.setAttribute(k, attr[k]);
}
return input;
}

function hoturl_post_go(page, options) {
var form = document.createElement("form");
form.setAttribute("method", "post");
Expand Down Expand Up @@ -1247,10 +1235,25 @@ function input_default_value(elt) {
}

function input_set_default_value(elt, val) {
if (input_is_checkboxlike(elt)) {
elt.setAttribute("data-default-checked", val == "" ? "false" : "true");
var cb = input_is_checkboxlike(elt), upd, j;
if (cb) {
elt.removeAttribute("data-default-checked");
elt.defaultChecked = val == "";
} else {
elt.setAttribute("data-default-value", val);
elt.removeAttribute("data-default-value");
elt.defaultValue = val;
}
// 2021 Chrome workaround
if (elt.name && elt.form && (upd = elt.form.elements.____updates____)) {
try {
j = JSON.parse(upd.value || "{}");
} catch (e) {
j = {};
}
if (elt.type === "radio" && !elt.checked)
val = elt.form.elements[elt.name].value;
j[elt.name] = val || "";
upd.value = JSON.stringify(j);
}
}

Expand Down Expand Up @@ -1291,6 +1294,42 @@ function form_highlight(form, elt) {
}
}

function hidden_input(name, value, attr) {
var input = document.createElement("input");
input.type = "hidden";
input.name = name;
input.value = value;
if (attr) {
for (var k in attr)
input.setAttribute(k, attr[k]);
}
return input;
}

$(function () {
$("form").each(function () {
var upd = this.elements.____updates____, j, n, e, e2, i;
if (upd && upd.value) {
try {
j = JSON.parse(upd.value);
for (n in j)
if ((e = this.elements[n])) {
if (e.type === "checkbox")
e.defaultChecked = e.value === j[n];
else if (e instanceof RadioNodeList) {
for (i = 0; i !== e.length; ++i) {
e2 = e.item(i);
e2.defaultChecked = e2.value === j[n];
}
} else
e.defaultValue = j[n];
}
} catch (e) {
}
}
});
});

function hiliter_children(form) {
form = $(form)[0];
addClass(form, "want-diff-alert");
Expand Down Expand Up @@ -6112,7 +6151,7 @@ function comment_completion_q(elt) {

// review preferences
var add_revpref_ajax = (function () {
var blurred_at = 0, posted_revpref;
var blurred_at = 0;

function rp(selector, on_unload) {
var $e = $(selector);
Expand All @@ -6135,17 +6174,6 @@ var add_revpref_ajax = (function () {
blurred_at = now_msec();
}

function rp_init_collecting() {
$(window).on("beforeunload", function () {
push_history_state();
});
$(document).on("collectState", function (event, state) {
state.initial_load = hotcrp_status.initial_load;
state.posted_revpref = posted_revpref;
});
return {};
}

function rp_change(event) {
var self = this, pid = this.name.substr(7), cid = null, pos;
if ((pos = pid.indexOf("u")) > 0) {
Expand All @@ -6157,24 +6185,13 @@ var add_revpref_ajax = (function () {
success: function (rv) {
minifeedback(self, rv);
if (rv && rv.ok && rv.value != null) {
posted_revpref = posted_revpref || rp_init_collecting();
posted_revpref[self.name] = self.value = self.defaultValue =
rv.value === "0" ? "" : rv.value;
self.value = rv.value === "0" ? "" : rv.value;
input_set_default_value(self, self.value);
}
}, trackOutstanding: true
});
}

function rp_load() {
if ((history.state && history.state.initial_load) === hotcrp_status.initial_load) {
var i, rp = history.state.posted_revpref || {};
for (i in rp) {
var e = $("input.revpref[name=" + i + "]")[0];
e && (e.defaultValue = rp[i]);
}
}
}

function rp_unload() {
if ((blurred_at && now_msec() - blurred_at < 1000)
|| $(":focus").is("input.revpref"))
Expand All @@ -6192,8 +6209,6 @@ var add_revpref_ajax = (function () {
rp_change.call(this, event);
});

$(window).on("load", rp_load);

return rp;
})();

Expand Down
1 change: 1 addition & 0 deletions search.php
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,7 @@ function echo_request_as_hidden_inputs($specialscore) {
echo Ht::form($Conf->selfurl($Qreq, ["post" => post_value(), "forceShow" => null]), ["id" => "sel", "class" => "ui-submit js-submit-paperlist"]),
Ht::hidden("defaultfn", ""),
Ht::hidden("forceShow", (string) $Qreq->forceShow, ["id" => "forceShow"]),
Ht::entry("____updates____", "", ["class" => "hidden ignore-diff"]),
Ht::hidden_default_submit("default", 1);
}

Expand Down

0 comments on commit 6dde969

Please sign in to comment.