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

Support Input outside form #1188

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions src/core/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -494,18 +494,20 @@ function parseLength(length, reference_length = null) {

// Return a jQuery object with elements related to an input element.
function findRelatives(el) {
var $el = $(el),
$relatives = $(el),
$label = $();
el = jqToNode(el);
const $el = $(el);
let $relatives = $(el);
let $label = $();

$relatives = $relatives.add($el.closest("label"));
$relatives = $relatives.add($el.closest("fieldset"));

if (el.id) $label = $("label[for='" + el.id + "']");
if (el.id) {
$label = $(`label[for="${el.id}"]`);
}
if (!$label.length) {
var $form = $el.closest("form");
if (!$form.length) $form = $(document.body);
$label = $form.find("label[for='" + el.name + "']");
const $form = $(el.form || el.closest("form") || document.body);
$label = $form.find(`label[for="${el.name}"]`);
}
$relatives = $relatives.add($label);
return $relatives;
Expand Down
62 changes: 39 additions & 23 deletions src/lib/dependshandler.js
Original file line number Diff line number Diff line change
@@ -1,58 +1,70 @@
import $ from "jquery";
import parser from "./depends_parse";
import utils from "../core/utils";

function DependsHandler($el, expression) {
var $context = $el.closest("form");
if (!$context.length) $context = $(document);
const el = utils.jqToNode($el);
this.$el = $el;
this.$context = $context;
this.$context = $(el.form || el.closest("form") || document);
this.ast = parser.parse(expression); // TODO: handle parse exceptions here
}

DependsHandler.prototype = {
_findInputs: function (name) {
var $input = this.$context.find(":input[name='" + name + "']");
if (!$input.length) $input = $("#" + name);
let $input = this.$context.find(":input[name='" + name + "']"); // TODO input outside form
if (!$input.length) {
$input = $("#" + name);
}
return $input;
},

_getValue: function (name) {
var $input = this._findInputs(name);
if (!$input.length) return null;

if ($input.attr("type") === "radio" || $input.attr("type") === "checkbox")
const $input = this._findInputs(name);
if (!$input.length) {
return null;
}
if ($input.attr("type") === "radio" || $input.attr("type") === "checkbox") {
return $input.filter(":checked").val() || null;
else return $input.val();
}
return $input.val();
},

getAllInputs: function () {
var todo = [this.ast],
$inputs = $(),
node;
const todo = [this.ast];
let $inputs = $();
let node;

while (todo.length) {
node = todo.shift();
if (node.input) $inputs = $inputs.add(this._findInputs(node.input));
if (node.children && node.children.length)
if (node.input) {
$inputs = $inputs.add(this._findInputs(node.input));
}
if (node.children && node.children.length) {
todo.push.apply(todo, node.children);
}
}
return $inputs;
},

_evaluate: function (node) {
var value = node.input ? this._getValue(node.input) : null,
i;
const value = node.input ? this._getValue(node.input) : null;

switch (node.type) {
case "NOT":
return !this._evaluate(node.children[0]);
case "AND":
for (i = 0; i < node.children.length; i++)
if (!this._evaluate(node.children[i])) return false;
for (const child of node.children) {
if (!this._evaluate(child)) {
return false;
}
}
return true;
case "OR":
for (i = 0; i < node.children.length; i++)
if (this._evaluate(node.children[i])) return true;
for (const child of node.children) {
if (this._evaluate(child)) {
return true;
}
}
return false;
case "comparison":
switch (node.operator) {
Expand All @@ -69,10 +81,14 @@ DependsHandler.prototype = {
case ">=":
return value >= node.value;
case "~=":
if (value === null) return false;
if (value === null) {
return false;
}
return value.indexOf(node.value) != -1;
case "=~":
if (value === null || !node.value) return false;
if (value === null || !node.value) {
return false;
}
return node.value.indexOf(value) != -1;
}
break;
Expand Down
2 changes: 1 addition & 1 deletion src/pat/auto-suggest/auto-suggest.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ export default Base.extend({

// Clear values on reset.
events.add_event_listener(
this.el.closest("form"),
this.el.form || this.el.closest("form"), // TODO: `closest` necessary?
"reset",
"pat-auto-suggest--reset",
() => this.$el.select2("val", "")
Expand Down
2 changes: 1 addition & 1 deletion src/pat/close-panel/close-panel.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export default Base.extend({
if (
e.target.matches(":not([formnovalidate])") &&
e.target.matches("[type=submit], button:not([type=button])") &&
this.el.closest("form")?.checkValidity() === false
this.el.form?.checkValidity() === false
) {
// Prevent closing an invalid form when submitting.
return;
Expand Down
2 changes: 1 addition & 1 deletion src/pat/image-crop/image-crop.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ var _ = {
// Set the form ID
if (opts.formId.length === 0) {
// no form ID supplied. Look for the closest parent form element
data.form = $this.closest("form");
data.form = $(this.form || this.closest("form"));
if (data.form.length === 0) {
log.error("No form specified or found");
return;
Expand Down
2 changes: 1 addition & 1 deletion src/pat/sortable/sortable.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export default Base.extend({
if (window.__patternslib_import_styles) {
import("./_sortable.scss");
}
this.$form = this.$el.closest("form");
this.$form = $(this.el.form || this.el.closest("form"));
this.options = parser.parse(this.$el, false);
this.recordPositions().initScrolling();
this.$el.on("pat-update", this.onPatternUpdate.bind(this));
Expand Down