-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathregistry.js
More file actions
285 lines (257 loc) · 9.47 KB
/
registry.js
File metadata and controls
285 lines (257 loc) · 9.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/**
* Patterns registry - Central registry and scan logic for patterns
*
* Copyright 2012-2013 Simplon B.V.
* Copyright 2012-2013 Florian Friesdorf
* Copyright 2013 Marko Durkovic
* Copyright 2013 Rok Garbas
* Copyright 2014-2015 Syslab.com GmBH, JC Brand
*/
/*
* changes to previous patterns.register/scan mechanism
* - if you want initialised class, do it in init
* - init returns set of elements actually initialised
* - handle once within init
* - no turnstile anymore
* - set pattern.jquery_plugin if you want it
*/
import $ from "jquery";
import dom from "./dom";
import logging from "./logging";
import utils from "./utils";
const log = logging.getLogger("registry");
const disable_re = /patterns-disable=([^&]+)/g;
const dont_catch_re = /patterns-dont-catch/g;
const disabled = {};
let dont_catch = false;
let match;
while ((match = disable_re.exec(window.location.search)) !== null) {
disabled[match[1]] = true;
log.info("Pattern disabled via url config:", match[1]);
}
while ((match = dont_catch_re.exec(window.location.search)) !== null) {
dont_catch = true;
log.info("I will not catch init exceptions");
}
/**
* Global pattern registry.
*
* This is a singleton and shared among any instance of the Patternslib
* registry since Patternslib version 8.
*
* You normally don't need this as the registry handles it for you.
*/
if (typeof window.__patternslib_registry === "undefined") {
window.__patternslib_registry = {};
}
export const PATTERN_REGISTRY = window.__patternslib_registry;
if (typeof window.__patternslib_registry_initialized === "undefined") {
window.__patternslib_registry_initialized = false;
}
const registry = {
patterns: PATTERN_REGISTRY, // reference to global patterns registry
// as long as the registry is not initialized, pattern
// registration just registers a pattern. Once init is called,
// the DOM is scanned. After that registering a new pattern
// results in rescanning the DOM only for this pattern.
init(patterns) {
// Extend this registries patterns object.
// This is a way to inject lazy loading patterns.
if (typeof patterns === "object") {
this.patterns = { ...this.patterns, ...patterns };
}
dom.document_ready(() => {
if (window.__patternslib_registry_initialized) {
// Do not reinitialize a already initialized registry.
return;
}
window.__patternslib_registry_initialized = true;
log.debug("Loaded: " + Object.keys(registry.patterns).sort().join(", "));
registry.scan(document.body);
log.debug("Finished initial scan.");
});
},
clear() {
// Removes all patterns from the registry. Currently only being
// used in tests.
for (const name in registry.patterns) {
delete registry.patterns[name];
}
},
transformPattern(name, content) {
/* Call the transform method on the pattern with the given name, if
* it exists.
*/
if (disabled[name]) {
log.debug(`Skipping disabled pattern: ${name}.`);
return;
}
const pattern = registry.patterns[name];
const transform = pattern.transform || pattern.prototype?.transform;
if (transform) {
try {
transform($(content));
} catch (e) {
if (dont_catch) {
throw e;
}
log.error(`Transform error for pattern ${name}.`, e);
}
}
},
initPattern(name, el, trigger) {
/* Initialize the pattern with the provided name and in the context
* of the passed in DOM element.
*/
const pattern = registry.patterns[name];
if (el.matches(pattern.trigger)) {
if (pattern.importer) {
const observer = new IntersectionObserver(async (entries) => {
for (const entry of entries) {
if (entry.isIntersecting) {
const _pattern = await pattern.importer();
this.patterns[name] = _pattern;
this.initPattern__initializer(_pattern, name, el, trigger);
observer.unobserve(el);
}
}
});
observer.observe(el);
} else {
this.initPattern__initializer(pattern, name, el, trigger);
}
}
},
initPattern__initializer(pattern, name, el, trigger) {
const $el = $(el);
const logger = logging.getLogger(`pat.${name}`);
logger.debug("Initialising.", el);
try {
if (pattern.init) {
// old style initialisation
pattern.init($el, null, trigger);
} else {
// class based pattern initialisation
new pattern($el, null, trigger);
}
logger.debug("done.");
} catch (e) {
if (dont_catch) {
throw e;
}
logger.error("Caught error:", e);
}
},
orderPatterns(patterns) {
// Always add pat-validation as first pattern, so that it can prevent
// other patterns from reacting to submit events if form validation
// fails.
if (patterns.includes("validation")) {
patterns.splice(patterns.indexOf("validation"), 1);
patterns.unshift("validation");
}
// Add clone-code to the very beginning - we want to copy the markup
// before any other patterns changed the markup.
if (patterns.includes("clone-code")) {
patterns.splice(patterns.indexOf("clone-code"), 1);
patterns.unshift("clone-code");
}
return patterns;
},
scan(content, patterns, trigger) {
if (!content) {
return;
}
if (typeof content === "string") {
content = document.querySelector(content);
} else if (content instanceof Text) {
// No need to scan a TextNode.
return;
} else if (content.jquery) {
content = content[0];
}
const selectors = [];
patterns = this.orderPatterns(patterns || Object.keys(registry.patterns));
for (const name of patterns) {
this.transformPattern(name, content);
const pattern = registry.patterns[name];
if (pattern.trigger) {
selectors.unshift(pattern.trigger);
}
}
// Clean up selectors:
// - Remove whitespace,
// - Remove trailing commas,
// - Join to selector string.
const selector_string = selectors
.map((selector) => selector.trim().replace(/,$/, ""))
.join(",");
// Exit, if no selector.
if (!selector_string) {
return;
}
let matches = dom.querySelectorAllAndMe(content, selector_string);
matches = matches.filter((el) => {
// Filter out patterns:
// - with class ``.disable-patterns`` or wrapped within.
// - wrapped in ``<pre>`` elements
// - wrapped in ``<template>`` elements (not reachable anyways)
return (
!el?.closest?.(".disable-patterns") &&
!el?.parentNode?.closest?.("pre") &&
// BBB. TODO: Remove with next major version.
!el?.closest?.(".cant-touch-this")
);
});
// walk list backwards and initialize patterns inside-out.
for (const el of matches.reverse()) {
for (const name of patterns) {
this.initPattern(name, el, trigger);
}
}
document.body.classList.add("patterns-loaded");
},
register(pattern, name) {
name = name || pattern.name;
if (!name) {
log.error("Pattern lacks a name.", pattern);
return false;
}
// Do not register blacklisted patterns.
let BLACKLIST = window.__patternslib_patterns_blacklist;
if (!Array.isArray(BLACKLIST)) {
BLACKLIST = [];
}
if (BLACKLIST.includes(name)) {
log.warn(`Pattern name ${name} is blacklisted.`);
return false;
}
if (registry.patterns[name]) {
log.debug(`Already have a pattern called ${name}.`);
return false;
}
// register pattern to be used for scanning new content
registry.patterns[name] = pattern;
// register pattern as jquery plugin
if (pattern.jquery_plugin) {
const plugin_name = ("pat-" + name).replace(
/-([a-zA-Z])/g,
function (match, p1) {
return p1.toUpperCase();
}
);
$.fn[plugin_name] = utils.jqueryPlugin(pattern);
// BBB 2012-12-10 and also for Mockup patterns.
$.fn[plugin_name.replace(/^pat/, "pattern")] = $.fn[plugin_name];
}
log.debug(`Registered pattern ${name}`, pattern);
if (window.__patternslib_registry_initialized) {
// Once the first initialization has been done, do only scan for
// newly registered patterns.
registry.scan(document.body, [name]);
log.debug(`Re-scanned dom with newly registered pattern ${name}.`);
}
return true;
},
};
export default registry;