Skip to content

Commit 6c6d630

Browse files
committed
feat: Improve support for input elements outside form.
1 parent 295e83d commit 6c6d630

File tree

10 files changed

+82
-12
lines changed

10 files changed

+82
-12
lines changed
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width" />
6+
<title>test mutation observer destroy</title>
7+
<link rel="stylesheet" href="/style/common.css" />
8+
src="/bundle.min.js"></script> src="/bundle.min.js"></script>
9+
</head>
10+
<body>
11+
<div id="el"><p></p></div>
12+
src="/bundle.min.js"></script>>
13+
const el = document.getElementById('el');
14+
const observer = new MutationObserver((rec) => {
15+
debugger;
16+
console.log("mutation");
17+
});
18+
observer.observe(el, {
19+
attributeFilter: ["class"],
20+
childList: true,
21+
});
22+
23+
//debugger;
24+
25+
//observer.disconnect();
26+
</script>
27+
</body>
28+
</html>

src/core/utils.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,7 @@ function parseLength(length, reference_length = null) {
494494

495495
// Return a jQuery object with elements related to an input element.
496496
function findRelatives(el) {
497+
el = jqToNode(el);
497498
const $el = $(el);
498499
let $relatives = $(el);
499500
let $label = $();
@@ -505,10 +506,7 @@ function findRelatives(el) {
505506
$label = $(`label[for="${el.id}"]`);
506507
}
507508
if (!$label.length) {
508-
let $form = $el.closest("form");
509-
if (!$form.length) {
510-
$form = $(document.body);
511-
}
509+
const $form = $(el.form || el.closest("form") || document.body);
512510
$label = $form.find(`label[for="${el.name}"]`);
513511
}
514512
$relatives = $relatives.add($label);

src/lib/dependshandler.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import $ from "jquery";
22
import parser from "./depends_parse";
3+
import utils from "../core/utils";
34

45
function DependsHandler($el, expression) {
5-
var $context = $el.closest("form");
6-
if (!$context.length) $context = $(document);
6+
const el = utils.jqToNode($el);
77
this.$el = $el;
8-
this.$context = $context;
8+
this.$context = $(el.form || el.closest("form") || document);
99
this.ast = parser.parse(expression); // TODO: handle parse exceptions here
1010
}
1111

@@ -53,7 +53,7 @@ DependsHandler.prototype = {
5353
case "NOT":
5454
return !this._evaluate(node.children[0]);
5555
case "AND":
56-
for (const child of node.children.length) {
56+
for (const child of node.children) {
5757
if (!this._evaluate(child)) {
5858
return false;
5959
}

src/pat/auto-suggest/auto-suggest.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ export default Base.extend({
132132

133133
// Clear values on reset.
134134
events.add_event_listener(
135-
this.el.closest("form"),
135+
this.el.form || this.el.closest("form"), // TODO: `closest` necessary?
136136
"reset",
137137
"pat-auto-suggest--reset",
138138
() => this.$el.select2("val", "")

src/pat/checklist/index-infinite.html

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="UTF-8" />
5+
<script src="/bundle.min.js"></script>
6+
<title>Infinite scrolling</title>
7+
</head>
8+
<body>
9+
10+
<form class="pat-checklist">
11+
<label><input type="checkbox" class="toggle-all" />Toggle checkboxes</label><br>
12+
<fieldset id="infinite-boxes">
13+
<label><input type="checkbox" checked="checked" /> Option one</label><br>
14+
<label><input type="checkbox" /> Option two</label><br>
15+
<label><input type="checkbox" /> Option three</label><br>
16+
<label><input type="checkbox" /> Option four</label><br>
17+
18+
<a
19+
href="./index-infinite.html#infinite-boxes"
20+
class="pat-inject"
21+
data-pat-inject="trigger: autoload-visible; target:self::element"
22+
>Loading...</a>
23+
24+
</fieldset>
25+
</form>
26+
27+
</body>
28+
</html>

src/pat/close-panel/close-panel.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export default Base.extend({
2727
if (
2828
e.target.matches(":not([formnovalidate])") &&
2929
e.target.matches("[type=submit], button:not([type=button])") &&
30-
this.el.closest("form")?.checkValidity() === false
30+
this.el.form?.checkValidity() === false
3131
) {
3232
// Prevent closing an invalid form when submitting.
3333
return;

src/pat/image-crop/image-crop.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ var _ = {
5252
// Set the form ID
5353
if (opts.formId.length === 0) {
5454
// no form ID supplied. Look for the closest parent form element
55-
data.form = $this.closest("form");
55+
data.form = $(this.form || this.closest("form"));
5656
if (data.form.length === 0) {
5757
log.error("No form specified or found");
5858
return;

src/pat/inject/test.html

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width" />
6+
<title>Test</title>
7+
<link rel="stylesheet" href="/style/common.css" />
8+
<script src="/bundle.min.js"></script>
9+
</head>
10+
<body>
11+
<form class="pat-form pat-inject">
12+
<button formaction="test.html" class="pat-button" type="submit">Save button</button>
13+
</form>
14+
</body>
15+
</html>
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
./py/bin/python send.py quaivedemo push_marker message_counter

src/pat/sortable/sortable.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export default Base.extend({
2020
if (window.__patternslib_import_styles) {
2121
import("./_sortable.scss");
2222
}
23-
this.$form = this.$el.closest("form");
23+
this.$form = $(this.el.form || this.el.closest("form"));
2424
this.options = parser.parse(this.$el, false);
2525
this.recordPositions().initScrolling();
2626
this.$el.on("pat-update", this.onPatternUpdate.bind(this));

0 commit comments

Comments
 (0)