|
1 | | -import $ from "jquery"; |
2 | | -import Base from "@patternslib/patternslib/src/core/base"; |
| 1 | +import { BasePattern } from "@patternslib/patternslib/src/core/basepattern"; |
| 2 | +import events from "@patternslib/patternslib/src/core/events"; |
3 | 3 | import Parser from "@patternslib/patternslib/src/core/parser"; |
| 4 | +import registry from "@patternslib/patternslib/src/core/registry"; |
4 | 5 |
|
5 | 6 | const parser = new Parser("content-mirror"); |
6 | 7 | parser.addArgument("target", "p.content-mirror .text"); |
7 | 8 |
|
8 | | -export default Base.extend({ |
9 | | - name: "content-mirror", |
10 | | - trigger: ".pat-content-mirror", |
| 9 | +class Pattern extends BasePattern { |
| 10 | + static name = "content-mirror"; |
| 11 | + static trigger = ".pat-content-mirror"; |
| 12 | + static parser = parser; |
11 | 13 |
|
12 | | - init: function content_mirror_init($el) { |
13 | | - this.options = parser.parse(this.el, this.options); |
14 | | - const $mirror = $(this.options.target).parents("p.content-mirror").first(); |
15 | | - $el.on( |
16 | | - "input propertychange", |
17 | | - $.proxy(this.updateMirror, this, this.options.target) |
| 14 | + async init() { |
| 15 | + this.target = document.querySelector(this.options.target); |
| 16 | + events.add_event_listener( |
| 17 | + this.el, |
| 18 | + "input", |
| 19 | + "pat-content-mirror--update-mirror", |
| 20 | + this.update_mirror.bind(this) |
18 | 21 | ); |
19 | | - $el.parents("form") |
20 | | - .first() |
21 | | - .on("reset", function () { |
22 | | - $el.val(""); |
23 | | - $mirror.html($mirror.html()); |
24 | | - }); |
25 | | - $(".placeholder", this.options.target).text($el.attr("placeholder") || ""); |
26 | | - }, |
27 | | - |
28 | | - updateMirror: function updateMirror(target, ev) { |
29 | | - const $el = $(ev.target); |
30 | | - const the_mirror = $(target); |
31 | | - the_mirror.text($el.val()); |
32 | | - if (!$el.val().length) { |
33 | | - const placeholder = $el.attr("placeholder"); |
| 22 | + |
| 23 | + const form = this.el.form || this.el.closest("form"); |
| 24 | + events.add_event_listener( |
| 25 | + form, |
| 26 | + "reset", |
| 27 | + `pat-content-mirror--reset--${this.uuid}`, |
| 28 | + () => { |
| 29 | + this.el.value = ""; |
| 30 | + this.el.dispatchEvent(events.input_event()); |
| 31 | + } |
| 32 | + ); |
| 33 | + |
| 34 | + const placeholder = this.target.querySelector(".placeholder"); |
| 35 | + if (placeholder) { |
| 36 | + placeholder.textContent = this.el.getAttribute("placeholder") || ""; |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + update_mirror(ev) { |
| 41 | + const el = ev.target; |
| 42 | + const the_mirror = this.target; |
| 43 | + const value = el.value; |
| 44 | + the_mirror.textContent = value; |
| 45 | + if (!value) { |
| 46 | + const placeholder = this.el.getAttribute("placeholder"); |
34 | 47 | if (placeholder) { |
35 | | - the_mirror.html('<em class="placeholder">' + placeholder + "</em>"); |
| 48 | + the_mirror.innerHTML = `<em class="placeholder">${placeholder}</em>`; |
36 | 49 | } |
37 | 50 | } |
38 | | - }, |
39 | | -}); |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +registry.register(Pattern); |
| 55 | + |
| 56 | +export default Pattern; |
| 57 | +export { Pattern }; |
0 commit comments