-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodule-url.js
34 lines (29 loc) · 1001 Bytes
/
module-url.js
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
const attr = ( el, attr )=> el.getAttribute( attr );
export class ModuleUrl extends HTMLElement
{
static observedAttributes=
[ 'slice'
, 'src' // module path, relative or absolute URL
];
sliceInit()
{ let path = attr(this,'src');
try
{ const url = '.' === path.charAt(0)
? new URL(path, this.closest('[base]')?.getAttribute('base') || location.href).href
: import.meta.resolve(path);
this.setAttribute('value',this.value = url );
}catch( er )
{ this.setAttribute('error', er.message);
this.setAttribute('value', path);
console.error(er.message ?? er, path);
}
this.dispatchEvent( new Event('change') );
}
attributeChangedCallback( name, oldValue, newValue )
{
if( 'src'=== name )
this.sliceInit();
}
}
window.customElements.define( 'module-url', ModuleUrl );
export default ModuleUrl;