Skip to content

Commit c32bf26

Browse files
authored
Merge pull request #1109 from Patternslib/classbased-patterns
Classbased patterns
2 parents 10d4e08 + e218af2 commit c32bf26

File tree

5 files changed

+120
-102
lines changed

5 files changed

+120
-102
lines changed

.npmignore

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
1-
*.css.map
21
*.log
32
*.map
43
*.swo
54
*.swp
5+
*.zip
66
*~
77
.DS_Store
88
.bundle
99
.env
1010
.sass-cache
11-
/paternslib.sublime-project
12-
/paternslib.sublime-workspace
1311
/stamp-yarn
1412
Gemfile.lock
1513
cache/
1614
coverage/
15+
dist-*
1716
node_modules/
1817
src/pat/**/*.css
1918
stats.html

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,8 @@ describe("pat-autosuggest", function () {
464464
const form = document.querySelector("form");
465465
const input = document.querySelector("input");
466466

467-
new pattern_validation(form);
467+
const instance_validation = new pattern_validation(form);
468+
await events.await_pattern_init(instance_validation);
468469
new pattern(input);
469470
await utils.timeout(1); // wait a tick for async to settle.
470471

@@ -497,7 +498,8 @@ describe("pat-autosuggest", function () {
497498
const form = document.querySelector("form");
498499
const input = document.querySelector("input");
499500

500-
new pattern_validation(form);
501+
const instance_validation = new pattern_validation(form);
502+
await events.await_pattern_init(instance_validation);
501503
new pattern(input);
502504
await utils.timeout(1); // wait a tick for async to settle.
503505

src/pat/date-picker/date-picker.test.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import $ from "jquery";
2+
import events from "../../core/events";
23
import pattern from "./date-picker";
34
import pattern_auto_submit from "../auto-submit/auto-submit";
45
import utils from "../../core/utils";
@@ -604,8 +605,9 @@ describe("pat-date-picker", function () {
604605
const el = document.querySelector("input[type=date]");
605606

606607
const pattern_validation = (await import("../validation/validation")).default;
608+
const instance_validation = new pattern_validation(form);
609+
await events.await_pattern_init(instance_validation);
607610

608-
new pattern_validation(form);
609611
new pattern(el);
610612
await utils.timeout(1); // wait a tick for async to settle.
611613

src/pat/validation/validation.js

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
// Patterns validate - Form vlidation
22
import "../../core/polyfills"; // SubmitEvent.submitter for Safari < 15.4 and jsDOM
33
import $ from "jquery";
4-
import Base from "../../core/base";
4+
import { BasePattern } from "../../core/basepattern";
55
import Parser from "../../core/parser";
66
import dom from "../../core/dom";
77
import events from "../../core/events";
88
import logging from "../../core/logging";
99
import utils from "../../core/utils";
10+
import registry from "../../core/registry";
1011

1112
const logger = logging.getLogger("pat-validation");
1213
//logger.setLevel(logging.Level.DEBUG);
@@ -34,12 +35,12 @@ parser.addArgument("error-template");
3435
const KEY_ERROR_EL = "__patternslib__input__error__el";
3536
const KEY_ERROR_MSG = "__patternslib__input__error__msg";
3637

37-
export default Base.extend({
38-
name: "validation",
39-
trigger: "form.pat-validation",
38+
class Pattern extends BasePattern {
39+
static name = "validation";
40+
static trigger = "form.pat-validation";
41+
static parser = parser;
4042

4143
init() {
42-
this.options = parser.parse(this.el, this.options);
4344
events.add_event_listener(
4445
this.el,
4546
"submit",
@@ -52,7 +53,11 @@ export default Base.extend({
5253
logger.debug("Checking input for submit", input, e);
5354
this.check_input({ input: input, event: e });
5455
}
55-
}
56+
},
57+
// Make sure this event handler is run early, in the capturing
58+
// phase in order to be able to cancel later non-capturing submit
59+
// events.
60+
{ capture: true }
5661
);
5762

5863
this.initialize_inputs();
@@ -63,7 +68,7 @@ export default Base.extend({
6368
// Set ``novalidate`` attribute to disable the browser's validation
6469
// bubbles but not disable the validation API.
6570
this.el.setAttribute("novalidate", "");
66-
},
71+
}
6772

6873
initialize_inputs() {
6974
this.inputs = [
@@ -99,7 +104,7 @@ export default Base.extend({
99104
(e) => debouncer(e)
100105
);
101106
}
102-
},
107+
}
103108

104109
check_input({ input, event, stop = false }) {
105110
if (input.disabled) {
@@ -320,7 +325,7 @@ export default Base.extend({
320325
event.stopImmediatePropagation();
321326
}
322327
this.set_error_message(input);
323-
},
328+
}
324329

325330
set_validity({ input, msg, attribute = null, min = null, max = null }) {
326331
// Replace some variables, as like validate.js
@@ -340,7 +345,7 @@ export default Base.extend({
340345
// Hidden inputs do not participate in validation but we need this
341346
// (e.g. styled date input).
342347
input[KEY_ERROR_MSG] = msg;
343-
},
348+
}
344349

345350
remove_error(input, all_of_group = false) {
346351
// Remove error message and related referencesfrom input.
@@ -365,7 +370,7 @@ export default Base.extend({
365370
}
366371
}
367372
}
368-
},
373+
}
369374

370375
set_error_message(input) {
371376
this.remove_error(input);
@@ -416,10 +421,15 @@ export default Base.extend({
416421
this.check_input({ input: _input, stop: true });
417422
}
418423
}
419-
},
424+
}
420425

421426
error_template(message) {
422427
// Template for the validation message
423428
return `<em class="validation warning message">${message}</em>`;
424-
},
425-
});
429+
}
430+
}
431+
432+
registry.register(Pattern);
433+
434+
export default Pattern;
435+
export { Pattern };

0 commit comments

Comments
 (0)