Skip to content

Commit

Permalink
Evaluate edit conditions differently.
Browse files Browse the repository at this point in the history
  • Loading branch information
kohler committed Aug 31, 2023
1 parent a2e883c commit ae02f50
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
30 changes: 21 additions & 9 deletions scripts/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -11059,7 +11059,7 @@ handle_ui.on("pspcard-fold", function (evt) {
});

(function ($) {
var edit_conditions = {};
var edit_conditions = {}, edit_conditions_scheduled = false;

function prepare_paper_select() {
var self = this,
Expand Down Expand Up @@ -11383,15 +11383,27 @@ edit_conditions.pc_conflict = function (ec, form) {
return evaluate_compar(n, ec.compar, ec.value);
};

function run_edit_conditions() {
var f = this.closest("form"),
function run_edit_condition() {
const f = this.closest("form"),
ec = JSON.parse(this.getAttribute("data-edit-condition")),
off = !evaluate_edit_condition(ec, f),
link = navsidebar.get(this);
toggleClass(this, "hidden", off);
link && toggleClass(link.element, "hidden", off);
}

function run_all_edit_conditions() {
$("#f-paper").find(".has-edit-condition").each(run_edit_condition);
edit_conditions_scheduled = false;
}

function schedule_all_edit_conditions() {
if (!edit_conditions_scheduled) {
edit_conditions_scheduled = true;
queueMicrotask(run_all_edit_conditions);
}
}

function header_text(hdr) {
var x = hdr.firstChild;
while (x && x.nodeType !== 3) {
Expand Down Expand Up @@ -11475,6 +11487,7 @@ handle_ui.on("submit.js-submit-paper", function (evt) {
});

function fieldchange(evt) {
$(this.form).trigger({type: "fieldchange", changeTarget: evt.target});
$(this.form).find(".want-fieldchange")
.trigger({type: "fieldchange", changeTarget: evt.target});
}
Expand Down Expand Up @@ -11547,6 +11560,10 @@ hotcrp.load_editable_paper = function () {
$("#f-paper .btn-savepaper").first().trigger({type: "click", sidebarTarget: this});
});
$(f).on("change", "input, select, textarea", fieldchange);
if (f.querySelector(".has-edit-condition")) {
run_all_edit_conditions();
$(f).on("fieldchange", schedule_all_edit_conditions);
}
};

hotcrp.load_editable_review = function () {
Expand Down Expand Up @@ -11600,11 +11617,6 @@ hotcrp.replace_editable_field = function (field, elt) {
add_pslitem_pfe.call(pfe);
};

hotcrp.paper_edit_conditions = function () {
$("#f-paper").on("fieldchange", ".has-edit-condition", run_edit_conditions)
.find(".has-edit-condition").each(run_edit_conditions);
};

hotcrp.evaluate_edit_condition = function (ec, form) {
return evaluate_edit_condition(typeof ec === "string" ? JSON.parse(ec) : ec, form || $$("f-paper"));
};
Expand Down Expand Up @@ -12950,7 +12962,7 @@ Object.assign(window.hotcrp, {
// load_paper_sidebar
// make_review_field
// onload
// paper_edit_conditions
paper_edit_conditions: function () {}, // XXX
popup_skeleton: popup_skeleton,
// render_list
render_text: render_text,
Expand Down
3 changes: 1 addition & 2 deletions src/papertable.php
Original file line number Diff line number Diff line change
Expand Up @@ -493,8 +493,7 @@ function print_editable_option_papt(PaperOption $opt, $heading = null, $rest = [
if ($opt->has_complex_exists_condition()
&& !$this->settings_mode
&& $input) {
echo ' want-fieldchange has-edit-condition" data-edit-condition="', htmlspecialchars(json_encode_browser($opt->exists_script_expression($this->prow)));
Ht::stash_script('$(hotcrp.paper_edit_conditions)', 'edit_condition');
echo ' has-edit-condition" data-edit-condition="', htmlspecialchars(json_encode_browser($opt->exists_script_expression($this->prow)));
}
echo '"><h3 class="', $this->control_class($opt->formid, "pfehead");
if ($for === "checkbox") {
Expand Down

0 comments on commit ae02f50

Please sign in to comment.