Core API
Class Decorators
- @Component
Utilities
- createDecorator
- EventEmitter
Constants
- INIT_CLASS
- INIT_SELECTOR
- VERSION
Component API
Property Decorators
- @OnInit
- @Evt
- @El
Properties / DOM
- $element
- $data
Properties / Composition
- mixins
Methods / Lifecycle
- $teardown
Methods / Events
- $emit
- $on
- $off
Lifecycle / Hooks
- beforeInit
- init
- beforeDestroy
- destroy
DOM API
Traversing
- find
- children
- closest
- parent
- eq
- first
- get
- array
Filtering
- filter
- map
- each
- is
Manipulation
- clone
- append
- prepend
- remove
- text
- html
- attr
- prop
- data
- addClass
- removeClass
- toggleClass
Events
- trigger
- on
- off
Miscellaneous
- index
Core API
Class Decorators
- @Component
Utilities
- createDecorator
- EventEmitter
Constants
- INIT_CLASS
- INIT_SELECTOR
- VERSION
Component API
Property Decorators
- @OnInit
- @Evt
- @El
Properties / DOM
- $element
- $data
Properties / Composition
- mixins
Methods / Lifecycle
- $teardown
Methods / Events
- $emit
- $on
- $off
Lifecycle / Hooks
- beforeInit
- init
- beforeDestroy
- destroy
DOM API
Traversing
- find
- children
- closest
- parent
- eq
- first
- get
- array
Filtering
- filter
- map
- each
- is
Manipulation
- clone
- append
- prepend
- remove
- text
- html
- attr
- prop
- data
- addClass
- removeClass
- toggleClass
Events
- trigger
- on
- off
Miscellaneous
- index
Core API
Class Decorators
@Component
Type:
DecoratorArguments:
{String} CSS selector
Details:
Registers new component that will be instantiated for all occurrences of selector on a page. When a new HTML is added to a page new occurences will be found.
Usage:
@Component('.foo')
class Foo { }
Utilities
createDecorator
Type:
FunctionArguments:
{*} any (optional)
Details:
Creates a custom decorator that can be used for both classes and properties.
Custom decorators are run during component initialisation so they can extend the component capability.
Usage:
const Decorator = createDecorator((component, property) => {
/* Decorator logic */
});
const DecoratorWithNoParams = createDecorator((component, property) => {
/* Decorator logic */
})();
@Decorator('argument')
method() {}
@DecoratorWithNoParams
otherMethod() {}
EventEmitter
Type:
ObjectDetails:
Utility class that extending allows to communicate any module with Strudel event system.
Usage:
import { EventEmitter } from 'strudel';
class Notifier extends EventEmitter {
constructor() {
this.$emit('notify');
}
}
Constants
INIT_CLASS
Type:
StringDefault:
strudel-initDetails:
Provides the value of class that is added to an element when Strudel initializes.
Usage:
import { INIT_CLASS } from 'strudel';
INIT_SELECTOR
Type:
StringDefault:
.strudel-initDetails:
Provides the value of a selector that can be used to select elements that have been initialized.
Usage:
import { INIT_SELECTOR } from 'strudel';
VERSION
Type:
StringDetails:
Provides the used version of Strudel as string.
Usage:
import { VERSION } from 'strudel';
const version = Number(VERSION.split('.')[0]);
if (version === 1) {
// Version v1.x.x
} else {
// Version v0.x.x
}
Component API
Property Decorators
@OnInit
Type:
DecoratorDetails:
Runs decorated class method on Component init lifecycle hook
Usage:
@OnInit
render() { }
@Evt
Type:
DecoratorArguments:
{String} event descriptor{String} CSS selector (optional)
Details:
Adds DOM event handler (using .on() from DOM API) for specific descriptor and makes decorated class method a callback. Second parameter can be provided to delegate the event to a descendant element by selector.
Usage:
@Evt('click')
onElementClick() { }
@Evt('click', '.bar')
onChildClick() { }
@El
Type:
DecoratorArguments:
{String} CSS selector
Details:
Finds element by selector (like .find() from DOM API) on component initialisation and substitute for variable. Searched for within component DOM tree.
Usage:
@El('.bar')
bar
Properties / DOM
$element
Type:
ElementDetails:
Gets the DOM element correlated with the component instance.
Usage:
init() {
this.$element.text('Hello world')
}
$data
Type:
ObjectDetails:
Reference to the
dataattributes map for DOM element correlated with component instance.Usage:
init() {
console.log(this.$data.attribute);
}
Properties / Composition
mixins
Type:
Array<Object>Details:
The
mixinsoptions accepts an array of mixin objects. These mixin objects can contain methods and properties and they will be merged against the component.Mixins can have
initmethod specificied which will be called before the component owninit.Usage:
const mixin = {
init() { console.log(1) }
}
class Component {
mixins: [mixin]
init() { console.log(2) }
}
// => 1
// => 2
Methods / Lifecycle
$teardown
Type:
FunctionDetails:
Triggers destroy of the component - unbinds event listeners and binding to DOM
Usage:
this.$teardown()
Methods / Events
Instance events are handled by EventEmitter custom event implementation not using DOM events.$emit
Type:
FunctionArguments:
{String} event descriptor
Details:
Emits event with provided descriptor and invokes all the handlers registered under that descriptor.
Usage:
this.$emit('eventName');
$on
Type:
FunctionArguments:
{String} event descriptor{Function} callback
Details:
Registers
callbackwhich will fire when event with matchingevent descriptoris emitted.Usage:
this.$on('eventName', function (data) {
console.log(data);
});
$off
Type:
FunctionArguments:
{String} event descriptor{Function} callback
Details:
Unregisters
callbackregistered for providedevent descriptor.Usage:
this.$off('eventName', callback);
Lifecycle Hooks
beforeInit
Type:
FunctionDetails:
Called synchronously before the
initand binding of events and elements. This can be used to fetch the data or manipulate the dom.
init
Type:
FunctionDetails:
Called synchronously after
DOMContentLoadedevent or when new component is discovered.
beforeDestroy
Type:
FunctionDetails:
Called synchronously after
$teardown()function is invoked on component instance before event listeners and DOM detach.
destroy
Type:
FunctionDetails:
Called synchronously after
$teardown()function is invoked on component instance when everything is destroyed.
DOM API
Note: Element is internal class for handling DOM manipulation that reimplements jQuery API. In componentsthis.$elementand all elements found using@Elproperty decorator are Element instances.
Element
Type:
ObjectArguments:
{String} CSS selector{Node} context
Details:
Creates instance of
Elementclass for theselector, can also have context being provided to find elements within Node.Usage:
Element('div');
Element('.list-item', document.querySelector('ul'));
Traversing
find
Type:
FunctionArguments:
{String} CSS selector
Details:
Finds element specified by selector in the element context
Usage:
Element('body').find('.className');
children
Type:
FunctionArguments:
{String} CSS selector (optional)
Details:
Get the direct children of all of the nodes with an optional filter
Usage:
Element('body').children();
Element('body').children('<div>');
closest
Type:
FunctionArguments:
{String} CSS selector (optional)
Details:
Find first ancestor that matches the selector
Usage:
Element('a').closest();
parent
Type:
FunctionArguments:
{String} CSS selector (optional)
Details:
Travel the matched elements one node up.
Usage:
Element('a').parent();
eq
Type:
FunctionArguments:
{number} index
Details:
Reduce the set of matched elements to the one at the specified index.
Usage:
Element('.list-items').eq(3);
first
Type:
FunctionDetails:
Reduce the set of matched elements to the first in the set. Returns native HTML Element.
Usage:
Element('body').find('.className').first();
get
Type:
FunctionArguments:
{number} index (optional)
Details:
Reduce the set of matched elements to the item at specified index. Returns native HTML Element.
Usage:
Element('body').find('.className').get();
Element('body').find('.className').get(0); // Same as first
array
Type:
FunctionArguments:
{Function} callback (optional)
Details:
Extracts structured data from the DOM. Can have callback passed to return different format of data.
Usage:
Element('ul li').array();
// ['Item 1', 'Item 2', 'Item 3']
Filtering
filter
Type:
FunctionArguments:
{string|Element|Function} filter
Details:
Remove all the nodes that doesn’t match the criteria.
Usage:
Element('ul').filter('li')
Element('nav').filter(Element('a'))
Element('nav').filter((node) => Element(node).is('a'))
map
Type:
FunctionArguments:
{Function} callback
Details:
Change the content of the current instance by looping each element
Usage:
Element('ul li').map((node, i) => '<a>' + Element(node).text() + '</a>');
each
Type:
FunctionArguments:
{Function} callback
Details:
Loop through all of the nodes and execute a callback for each
Usage:
Element('ul li').each((node, i) => Element(node).attr('href', '#'));
is
Type:
FunctionArguments:
{string|Element|Function} filter
Details:
Check whether any of the nodes matches the selector
Usage:
Element('nav').is('.is-active');
Manipulation
clone
Type:
FunctionDetails:
Clone element with all it’s attributes and descendants
Usage:
Element("ul").clone();
append
Type:
FunctionArguments:
{string|Element} html
Details:
Insert content, specified by the argument, to the end of each element in the set of matched elements. Additional data can be provided, which will be used for populating the html
Usage:
Element("ul").append("<li>Item 1</li>");
prepend
Type:
FunctionArguments:
{string|Element} html
Details:
Insert content, specified by the argument, to the beginning of each element in the set of matched elements. Additional data can be provided, which will be used for populating the html
- Usage:
Element("ul").prepend("<li>Item 1</li>");
remove
Type:
FunctionDetails:
Remove the set of matched elements from the DOM.
Usage:
Element("ul").remove();
text
Type:
FunctionArguments:
{string} text (optional)
Details:
Gets the text contents of the first element in a set. When argument is provided set the text contents of each element in the set.
Usage:
Element("span").text();
Element("span").text("Hello world!");
html
Type:
FunctionArguments:
{string} htmlString (optional)
Details:
Gets the HTML contents of the first element in a set. When argument is provided set the HTML contents of each element in the set.
Usage:
Element("div").html();
Element("div").html("<span>Hello world!</span>");
attr
Type:
FunctionArguments:
{string|object} attrName{string|null} value
Details:
Gets the value of an attribute of the first element in the set. When second argument is provided sets the attributes contents of each element in the set.
Usage:
Element("a").attr("href");
Element("a").attr("href", "#");
Element("a").attr({"href": "#", "target": "_blank"});
Element("a").attr("href", null); // Remove attribute
prop
Type:
FunctionArguments:
{string|object} propName{string} value
Details:
Gets the value of a property for the each element in the set of matched elements or set one or more properties for every matched element.
Usage:
Element("input[type='checkbox']").prop("checked");
Element("input[type='text']").prop("value", "5");
data
Type:
FunctionArguments:
{string|object} attrName{string} value
Details:
Gets the value of an data attribute for the each element in the set of matched elements or set one or more attributes for every matched element.
Usage:
Element("div").data().msg;
Element("div").data("msg");
Element("div").data("msg", "Hello world!");
Element("div").data({"msg": "Hello world!", "value": "200"});
addClass
Type:
FunctionArguments:
{...String} class(es)
Details:
Adds the specified class(es) to each element in the set of matched elements.
Usage:
Element('.button').addClass('is-active');
removeClass
Type:
FunctionArguments:
{...String} class(es)
Details:
Remove the specified class(es) to each element in the set of matched elements.
Usage:
Element('.modal').removeClass('is-visible');
toggleClass
Type:
FunctionArguments:
{...String} class(es)
Details:
Toggles the specified class(es) to each element in the set of matched elements.
Usage:
Element('.main-nav').toggleClass('is-expanded');
Events
trigger
Type:
FunctionArguments:
{String} events
Details:
Execute all handlers attached to the event type.
Usage:
Element('button').trigger('click');
on
Type:
FunctionArguments:
{String} event{string|Function} CSS selector or listener{Function} listener (only when selector provided)
Details:
Attach DOM event handlers to element or element descendants.
Usage:
Element('button').on('click', callback);
Element('div').on('click', 'button', callback);
off
Type:
FunctionArguments:
{String|[String]} events{Function} callback
Details:
Remove DOM event handler(s) from element.
Usage:
Element('button').off(); // Removes all handlers from element
Element('button').off('click'); // Removes all handlers from element's click event
Element('button').off('click', callback); // Removes callback from element's click event
Miscellaneous
index
Type:
FunctionArguments:
{Element|HTML Element} node
Details:
Get an integer indicating the position of the passed element relative to parent.
Usage:
Element('nav').index(Element('a.is-active'));
Element('ul').index(Element('li.is-hidden').first());