Skip to content

Commit

Permalink
feat: Improve support for input elements outside form.
Browse files Browse the repository at this point in the history
  • Loading branch information
thet committed Nov 5, 2023
1 parent 295e83d commit 6c6d630
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 12 deletions.
28 changes: 28 additions & 0 deletions src/core/testmutationobserverdestroy.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width" />
<title>test mutation observer destroy</title>
<link rel="stylesheet" href="/style/common.css" />
src="/bundle.min.js"></script> src="/bundle.min.js"></script>
</head>
<body>
<div id="el"><p></p></div>
src="/bundle.min.js"></script>>
const el = document.getElementById('el');
const observer = new MutationObserver((rec) => {
debugger;
console.log("mutation");
});
observer.observe(el, {
attributeFilter: ["class"],
childList: true,
});

//debugger;

//observer.disconnect();
</script>
</body>
</html>
6 changes: 2 additions & 4 deletions src/core/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,7 @@ function parseLength(length, reference_length = null) {

// Return a jQuery object with elements related to an input element.
function findRelatives(el) {
el = jqToNode(el);
const $el = $(el);
let $relatives = $(el);
let $label = $();
Expand All @@ -505,10 +506,7 @@ function findRelatives(el) {
$label = $(`label[for="${el.id}"]`);
}
if (!$label.length) {
let $form = $el.closest("form");
if (!$form.length) {
$form = $(document.body);
}
const $form = $(el.form || el.closest("form") || document.body);
$label = $form.find(`label[for="${el.name}"]`);
}
$relatives = $relatives.add($label);
Expand Down
8 changes: 4 additions & 4 deletions src/lib/dependshandler.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
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
}

Expand Down Expand Up @@ -53,7 +53,7 @@ DependsHandler.prototype = {
case "NOT":
return !this._evaluate(node.children[0]);
case "AND":
for (const child of node.children.length) {
for (const child of node.children) {
if (!this._evaluate(child)) {
return false;
}
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
28 changes: 28 additions & 0 deletions src/pat/checklist/index-infinite.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<script src="/bundle.min.js"></script>
<title>Infinite scrolling</title>
</head>
<body>

<form class="pat-checklist">
<label><input type="checkbox" class="toggle-all" />Toggle checkboxes</label><br>
<fieldset id="infinite-boxes">
<label><input type="checkbox" checked="checked" /> Option one</label><br>
<label><input type="checkbox" /> Option two</label><br>
<label><input type="checkbox" /> Option three</label><br>
<label><input type="checkbox" /> Option four</label><br>

<a
href="./index-infinite.html#infinite-boxes"
class="pat-inject"
data-pat-inject="trigger: autoload-visible; target:self::element"
>Loading...</a>

</fieldset>
</form>

</body>
</html>
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
15 changes: 15 additions & 0 deletions src/pat/inject/test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width" />
<title>Test</title>
<link rel="stylesheet" href="/style/common.css" />
<script src="/bundle.min.js"></script>
</head>
<body>
<form class="pat-form pat-inject">
<button formaction="test.html" class="pat-button" type="submit">Save button</button>
</form>
</body>
</html>
1 change: 1 addition & 0 deletions src/pat/push/tools/local-quaivedemo.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
./py/bin/python send.py quaivedemo push_marker message_counter
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

0 comments on commit 6c6d630

Please sign in to comment.