choc.__version__
- currently-loaded library versionchoc.__version__
is a string consisting of three parts, major/minor/revision,
such as "1.6.4"
. See What’s New for details on what changed in
each version.
choc
- Construct a DOM elementelement = choc("TAG", attributes, contents);
xmlns_xlat
below), or include the full namespace URI.attributes
- optional object mapping attribute names to their desired
values. If omitted/null/empty, no attributes will be set.
element.setAttribute
or by direct assignment
where this is more suitable.{".foo": 42}
to set element.foo = 42
{"@onclick": "hello?"}
volume
, value
, disabled
, checked
.htmlFor
and for
have the same effect, as do class
and className
.contents
- optional collection of children. See set_content
below.
Calling choc("TAG", attr, contents)
is broadly equivalent to calling
set_content(choc("TAG", attr), contents)
(but see ‘Special Cases’ below).In additional to the above syntax, this notation is also supported:
element = choc.TAG(attributes, contents);
A destructuring import makes this more convenient:
const {TAG} = choc;
element = TAG(attributes, contents);
This is the recommended idiom, and can be managed by use of the chocimport script.
DOM
- Locate a DOM elementelement = DOM(selector);
undefined
.Note that this is very similar - but not identical - to both document.querySelector
and jQuery’s selection. Their behaviours differ when zero or multiple elements match:
document.querySelector
will return the first match, ignoring any others.set_content
- Populate a DOM elementelement = set_content(elem_or_sel, contents);
elem_or_sel
is either a DOM element or a selector. Passing a selector is
equivalent to calling element = set_content(DOM(sel), contents);
but will
throw if there is no such element.contents
may be any of the following:
null
, undefined
, ""
, or 0
) - no content at allNode
(including an Element as constructed by choc()
) - moves the
node from its current location in the document (if any) into hereon
- Respond to DOM eventsidx = on(event, selector, callback, options);
event
- name of an event such as "click"
or "paste"
selector
- CSS selector to identify matching elements. The elements do
not have to exist at the time the event handler is established.callback
- JavaScript function to call when the event occurs.options
- normally omit this, but if additional options are needed,
they can be provided. Not compatible with multiple uses of the same
event handler.The event will be attached to the document. Elements rendered in the document are capable of triggering these events.
Inside the callback, the event object is exactly the one given by the browser, with one additional attribute:
match
- the element which matched the selectorFor example, on("click", ".thing", e => set_content(e.match, "Hi!"));
will change the contents of the element clicked on, regardless of the
presence of other elements with the same selector.
This is similar to e.currentTarget
but due to technical limitations,
currentTarget
will always be document
during these event handlers.
NOTE: Asynchronous event handlers are fully supported; however, as
the event object is not reconstructed for each handler, the match
attribute is only available during the initial, synchronous, part of the
function. For example:
on("click", "button", async e => {
console.log(e.match); //Works
const self = e.match;
await something();
console.log(e.match); //Is now null
console.log(self); //This will work though
});
//the same is true for other forms of asynchronicity:
on("click", "button", e => {
console.log(e.match); //Works
setTimeout(() => {
console.log(e.match); //Is now null
}, 1000);
});
apply_fixes
- Provide extra functionality and compatibility for dialogsapply_fixes({
close_selector: selector,
click_outside: false | true | "formless",
});
Improves support for the <dialog>
element in browsers that lack it, such as
older versions of Firefox. Regardless of the options provided, this will add
the showModal
and close
methods to all dialog elements, with a basic level
of functionality. Additional features can be selected, all are optional:
close_selector
- a CSS selector for clickable elements which should cause
a dialog to be immediately closed. For example, set a CSS class on all your
close buttons, and quickly and easily close the correct dialog with one
event handler.click_outside
- if true, clicking outside any dialog (on the shaded
background where it’s blocking access to the rest of the document) will close
the dialog. If the string "formless"
, this applies only when the dialog
doesn’t contain a form
element.methods
- if true, will add methods to all DOM elements. Use value of 1,
as future expansion may give different meaning to higher numbers. See below
for methods added.Note that this function can be imported and called under the name fix_dialogs
for compatibility with older versions of the Chocolate Factory.
apply_fixes({methods: 1})
will add the following method to all DOM elements:
elem.closest_data("foo")
is approximately equivalent to elem.closest("[data-foo]").dataset.foo
but will return null
if no parent element has this data attribute.lindt
- Construct an element templatetemplate = lindt("TAG", attributes, contents);
Analogous to choc() but for templated usage.
lindt.TAG(attributes, contents)
and is thus
suitable for destructuring imports.lindt
recognizes a special attribute key
which can guide the template
matching (see below), and can construct pseudo-element groupings with no tag
such as lindt({key: "some_key"}, contents)
. These are equivalent to arrays
but have keys associated with them.The lindt
function is useful only in conjunction with replace_content
.
replace_content
- Apply an element templateelement = replace_content(elem_or_sel, template);
Analogous to set_content() but for templated usage.
elem_or_sel
is either a DOM element or a selector. Passing a selector is
equivalent to calling element = replace_content(DOM(sel), template);
.
Passing null
will coalesce the template into an actual DOM element and
return it, allowing it to be used in other ways. Note that this requires
a non-array template.template
may be any of the following:
null
, undefined
, ""
, or 0
) - no content at allNode
(including an Element as constructed by choc()
) - moves the
node from its current location in the document (if any) into herelindt()
When using templates, replace_content
will attempt to reuse DOM elements
as much as possible. Repeatedly calling replace_content
on the same element
with similar templates will apply only those changes, keeping other elements
unchanged.
Recommended best practice:
xmlns_xlat
- Translate namespace aliases into URIs(New in 1.7.0)
xmlns_xlat.math = "http://www.w3.org/1998/Math/MathML";
This mapping of aliases to URIs may be externally mutated to create new aliases
as needed. It comes prepopulated with the alias svg
for http://www.w3.org/2000/svg.
The five primary functions choc
, set_content
, on
, DOM
, and fix_dialogs
are made available on the window object as pseudo-globals, for the convenience
of non-module code (where an import
statement is not available). These may
therefore also be used from the browser console.
Though Chocolate Factory tries its best to be generic and handle everything the same way, there are occasionally quirks that are best handled in their own way.
value
attribute will reapply the value after adding
the children, since valid values for a drop-down list are defined by its
children.