Skip to content

Commit b50415b

Browse files
committed
core registry: Modernize code - Remove underscore and jquery-ext, use more regular js
1 parent ee09d31 commit b50415b

File tree

1 file changed

+25
-23
lines changed

1 file changed

+25
-23
lines changed

src/core/registry.js

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,10 @@
1717
* - set pattern.jquery_plugin if you want it
1818
*/
1919
import $ from "jquery";
20-
import _ from "underscore";
20+
import dom from "./dom";
2121
import logging from "./logging";
2222
import utils from "./utils";
2323

24-
// below here modules that are only loaded
25-
import "./jquery-ext";
26-
2724
const log = logging.getLogger("registry");
2825
const disable_re = /patterns-disable=([^&]+)/g;
2926
const dont_catch_re = /patterns-dont-catch/g;
@@ -117,44 +114,49 @@ const registry = {
117114
// sure here, that it appears first. Not sure what would be
118115
// the best solution. Perhaps some kind of way to register
119116
// patterns "before" or "after" other patterns.
120-
if (
121-
_.contains(patterns, "validation") &&
122-
_.contains(patterns, "inject")
123-
) {
117+
if (patterns.includes("validation") && patterns.includes("inject")) {
124118
patterns.splice(patterns.indexOf("validation"), 1);
125119
patterns.unshift("validation");
126120
}
127121
return patterns;
128122
},
129123

130124
scan(content, patterns, trigger) {
125+
if (typeof content === "string") {
126+
content = document.querySelector(content);
127+
} else if (content.jquery) {
128+
content = content[0];
129+
}
130+
131131
const selectors = [];
132-
let $match;
133132
patterns = this.orderPatterns(
134133
patterns || Object.keys(registry.patterns)
135134
);
136-
patterns.forEach(_.partial(this.transformPattern, _, content));
137-
patterns = _.each(patterns, (name) => {
135+
for (const name of patterns) {
136+
this.transformPattern(name, content);
138137
const pattern = registry.patterns[name];
139138
if (pattern.trigger) {
140139
selectors.unshift(pattern.trigger);
141140
}
142-
});
143-
$match = $(content).findInclusive(selectors.join(",")); // Find all DOM elements belonging to a pattern
144-
$match = $match.filter(function () {
141+
}
142+
143+
let matches = dom.querySelectorAllAndMe(content, selectors.join(","));
144+
matches = matches.filter((el) => {
145145
// Filter out code examples wrapped in <pre> elements.
146-
return $(this).parents("pre").length === 0;
146+
// Also filter special class ``.cant-touch-this``
147+
return (
148+
dom.find_parents(el, "pre").length === 0 &&
149+
!el.matches(".cant-touch-this")
150+
);
147151
});
148-
$match = $match.filter(":not(.cant-touch-this)");
149152

150153
// walk list backwards and initialize patterns inside-out.
151-
$match.toArray().reduceRight(
152-
function registryInitPattern(acc, el) {
153-
patterns.forEach(_.partial(this.initPattern, _, el, trigger));
154-
}.bind(this),
155-
null
156-
);
157-
$("body").addClass("patterns-loaded");
154+
for (const el of matches.reverse()) {
155+
for (const name of patterns) {
156+
this.initPattern(name, el, trigger);
157+
}
158+
}
159+
document.body.classList.add("patterns-loaded");
158160
},
159161

160162
register(pattern, name) {

0 commit comments

Comments
 (0)