2017-11-13 01:23:39 -05:00
|
|
|
'use strict';
|
|
|
|
|
2024-01-24 12:12:08 -05:00
|
|
|
import Module, { GenericModule } from 'utilities/module';
|
2017-11-13 01:23:39 -05:00
|
|
|
|
|
|
|
let last_site = 0;
|
|
|
|
let last_call = 0;
|
|
|
|
|
|
|
|
export default class BaseSite extends Module {
|
2024-01-24 12:12:08 -05:00
|
|
|
|
|
|
|
constructor(name, parent) {
|
|
|
|
super(name, parent);
|
2017-11-13 01:23:39 -05:00
|
|
|
this._id = `_ffz$${last_site++}`;
|
|
|
|
|
2020-07-22 21:31:41 -04:00
|
|
|
//this.inject('settings');
|
2017-11-13 01:23:39 -05:00
|
|
|
|
|
|
|
this.log.info(`Using: ${this.constructor.name}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ========================================================================
|
|
|
|
// DOM Manipulation
|
|
|
|
// ========================================================================
|
|
|
|
|
2023-11-02 21:39:52 -04:00
|
|
|
getReact() {
|
|
|
|
if ( this._react )
|
|
|
|
return this._react;
|
|
|
|
|
|
|
|
let react;
|
|
|
|
try {
|
|
|
|
react = this.getCore?.()?.intl?.react;
|
|
|
|
} catch(err) { /* no-op */ }
|
|
|
|
|
|
|
|
if ( react?.Component && react.createElement )
|
|
|
|
return this._react = react;
|
|
|
|
|
2024-01-24 12:12:08 -05:00
|
|
|
react = this.resolve('site.web_munch')?.getModule?.('react');
|
2023-11-02 21:39:52 -04:00
|
|
|
if ( react?.Component && react.createElement )
|
|
|
|
return this._react = react;
|
|
|
|
}
|
|
|
|
|
2024-10-22 17:19:03 -04:00
|
|
|
getReactDom() {
|
|
|
|
if ( this._reactDom )
|
|
|
|
return this._reactDom;
|
|
|
|
|
2025-06-07 16:09:55 -06:00
|
|
|
const reactDom = this.resolve('site.web_munch')?.getModule?.('react-dom');
|
2024-10-22 17:19:03 -04:00
|
|
|
if ( reactDom?.createPortal )
|
|
|
|
return this._reactDom = reactDom;
|
|
|
|
}
|
|
|
|
|
2023-11-02 21:39:52 -04:00
|
|
|
findReact() {
|
|
|
|
const react = this.getReact();
|
|
|
|
if ( react )
|
|
|
|
return Promise.resolve(react);
|
|
|
|
|
2024-01-24 12:12:08 -05:00
|
|
|
const munch = this.resolve('site.web_munch');
|
|
|
|
if ( munch )
|
|
|
|
return munch.findModule('react');
|
|
|
|
|
|
|
|
return this.waitFor('site.web_munch:registered')
|
|
|
|
.then(() => this.findReact());
|
2023-11-02 21:39:52 -04:00
|
|
|
}
|
|
|
|
|
2017-11-13 01:23:39 -05:00
|
|
|
awaitElement(selector, parent, timeout = 60000) {
|
|
|
|
if ( ! parent )
|
|
|
|
parent = document.documentElement;
|
|
|
|
|
|
|
|
const el = parent.querySelector(selector);
|
|
|
|
if ( el )
|
|
|
|
return Promise.resolve(el);
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const observer_name = `${this._id}$observer`,
|
|
|
|
data = parent[observer_name],
|
|
|
|
call_id = last_call++,
|
|
|
|
|
|
|
|
timer = timeout && setTimeout(() => {
|
|
|
|
const data = parent[observer_name];
|
|
|
|
if ( ! data )
|
|
|
|
return;
|
|
|
|
|
|
|
|
const [observer, selectors] = data;
|
|
|
|
|
|
|
|
for(let i=0; i < selectors.length; i++) {
|
|
|
|
const d = selectors[i];
|
|
|
|
if ( d[0] === call_id ) {
|
|
|
|
selectors.splice(i, 1);
|
|
|
|
d[3]('Timed out');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! selectors.length ) {
|
|
|
|
observer.disconnect();
|
|
|
|
parent[observer_name] = null;
|
|
|
|
}
|
|
|
|
}, timeout);
|
|
|
|
|
|
|
|
if ( data ) {
|
|
|
|
data[1].push([call_id, selector, resolve, reject, timer]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const observer = new MutationObserver(() => {
|
|
|
|
const data = parent[observer_name];
|
|
|
|
if ( ! data ) {
|
|
|
|
observer.disconnect();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const selectors = data[1];
|
|
|
|
for(let i=0; i < selectors.length; i++) {
|
|
|
|
const d = selectors[i];
|
|
|
|
const el = parent.querySelector(d[1]);
|
|
|
|
if ( el ) {
|
|
|
|
selectors.splice(i, 1);
|
|
|
|
i--;
|
|
|
|
|
|
|
|
if ( d[4] )
|
|
|
|
clearTimeout(d[4]);
|
|
|
|
|
|
|
|
d[2](el);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! selectors.length ) {
|
|
|
|
observer.disconnect();
|
|
|
|
parent[observer_name] = null;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
parent[observer_name] = [observer, [[call_id, selector, resolve, reject, timer]]];
|
|
|
|
observer.observe(parent, {
|
|
|
|
childList: true,
|
|
|
|
attributes: true,
|
|
|
|
characterData: true,
|
|
|
|
subtree: true
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2024-01-24 12:12:08 -05:00
|
|
|
}
|