Convenience aliases and functions to minimize the boilerplate of using the DOM API.
Typical usage would be to import as $
and to create an H
for the HTML tags the current module would be using:
import * as $ from 'vpweb/browser';
const H = $.H('input', 'label', 'div', 'span');
Alias of HTMLElement.prototype.getAttribute()
Alias of Node.nextElementSibling
Alias of Node.parentElement
Alias of Node.previousElementSibling
Set multiple element attributes at once. Wraps HTMLElement.setAttribute()
.
Parameters:
el
—HTMLElement
— Elementattrs
—Object
— All attributes to set
Set multiple style properties at once. Wraps CSSStyleDeclaration.setProperty()
.
Parameters:
el
—HTMLElement
— Elementattrs
—Object
— All attributes to set
Create text node
Parameters:
str
—string
— String
Returns: Text
— Text node
Alias of HTMLElement.removeAttribute()
Remove all children of a node.
Parameters:
node
—Node
— Node
Insert node(s) before another. Node arrays may contain sub-arrays which will be crawled recursively.
Parameters:
orig
—Node
— Originaladd
—Node|Node[]|NodeList|HTMLCollection
— New node(s) to insert
Append node(s) after another. Node arrays may contain sub-arrays which will be crawled recursively.
Parameters:
parent
—Node
— Parentadd
—Node|Node[]|NodeList|HTMLCollection
— Node(s) to insert
Remove all children of a node and append others into it. Shortcut to empty()
and append()
.
Parameters:
parent
—Node
— Parentadd
—Node|Node[]|NodeList|HTMLCollection
— Node(s) to insert
Prepend node(s) before another. Node arrays may contain sub-arrays which will be crawled recursively.
Parameters:
parent
—Node
— Parentadd
—Node|Node[]|NodeList|HTMLCollection
— Node(s) to insert
Create an element. Note that while attrs
is fully optional, children
must be specified in order to specify shadow
.
Parameters:
tag
—string
— Tag name (i.e. "div")[attrs]
—Object
— Attributes to set (usesset()
)[children]
—string|Node|Node[]|NodeList|HTMLCollection
— Children to add[shadow]
—string|Node|Node[]|NodeList|HTMLCollection
— Children to add as a shadow DOM
Returns: HTMLElement
— Element
Create a module of element creators. For example:
const H = Browser.H('div','span');
...would return an object with div()
and span()
being aliases to h()
with the first argument curried to their tag name. Creating an empty element then becomes i.e. H.div()
.
Parameters:
tag
—...string
— Tag names
Returns: Object
— Module
Wrapper for querySelector()
which makes the subject optional.
Parameters:
[node]
—Node
— Node (default:document
)sel
—string
— Selector
Returns: HTMLElement|null
— Result
Wrapper for querySelectorAll()
which makes the subject optional.
Parameters:
[node]
—Node
— Node (default:document
)sel
—string
— Selector
Returns: NodeList
— Results (static)
Iterate over all matching elements.
Parameters:
[base]
—Node
— Node (default:document
)sel
—string
— Selectorf
—function(HTMLElement):void
— Operation to perform
Add event listener. Note that even though opts
is optional, it must be specified in order to specify vpo
. Valid vpo
parameters are:
- mute: ms to wait until we can be triggered again (debouncer)
- prevent: set true to
preventDefault()
automatically - stop: set true to
stopPropagation()
automatically - stopi: set true to
stopImmediatePropagation()
automatically
Parameters:
[node]
—Node
— Node (default:document
)name
—string|string[]
— Name(s) of the event(s)f
—function(event):void
— Operation to perform[opts]
—Object
— Options to pass toaddEventListener()
[vpo]
—Object
— Additional options
Trigger an event using dispatchEvent(customEvent())
.
Parameters:
[el]
—HTMLElement
— Element (default:document
)name
—string
— Event name[detail]
—*
— Details to pass toCustomEvent()
Delay execution until document is loaded, or now if it already is.
Parameters:
f
—function():void
— Task to perform exactly once
Iterate over all matching elements now and in the future as they appear in the DOM. Similar to forall()
but adds a mutation observer.
Parameters:
[base]
—Node
— Base (default:document
)sel
—string
— Selectorf
—function(el):void
— Operation to perform
Returns: MutationObserver
— The observer which you may cancel later
Perform async HTTP request. Valid args
parameters are:
- headers: Object with additional HTTP headers to send
- username: HTTP Auth username
- password: HTTP Auth password
Parameters:
method
—string
— HTTP method (usually "GET" or "POST")url
—string
— URL[body]
—string|Object
— Content[ctype]
—string
— Content-Type ofbody
[rtype]
—string
— Type expected in response ("text", "json", "document")[args]
—Object
— Additional options[ok]
—function(XMLHttpRequest):void
— Callback on success[err]
—function(XMLHttpRequest):void
— Callback on failure
Simple async HTTP "GET" request. Wraps ajax()
.
Parameters:
url
—string
— URL[rtype]
—string
— Type expected in response ("text", "json", "document")[args]
—Object
— Additional options (seeajax()
)[ok]
—function(XMLHttpRequest):void
— Callback on success[err]
—function(XMLHttpRequest):void
— Callback on failure
Simple async HTTP "POST" request. Wraps ajax()
.
If body
is present and an Object
, it will be serialized as JSON, the content type will be set accordingly and the response type will default to "json" instead of "text".
Parameters:
url
—string
— URLbody
—string|Object
— Content[rtype]
—string
— Type expected in response ("text", "json", "document")[args]
—Object
— Additional options (seeajax()
)[ok]
—function(XMLHttpRequest):void
— Callback on success[err]
—function(XMLHttpRequest):void
— Callback on failure
Load a JSON resource asynchronously. If the script has an src
attribute, the URL is loaded and the result parsed as JSON. Otherwise, the content of the script block is parsed as JSON.
Parameters:
el
—HTMLElement
— Script[args]
—Object
— Additional options (seeajax()
)[ok]
—function(Object):void
— Callback on success[err]
—function(XMLHttpRequest):void
— Callback on failure