blissfuljs
Version:
Lightweight helper library for modern browsers.
1,324 lines (1,002 loc) • 60.6 kB
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Bliss.js — Documentation</title>
<link rel="shortcut icon" href="style/twitter-avatar.png" />
<link rel="stylesheet" href="style/prism.css" />
<link rel="stylesheet" href="style/style.css" />
<link rel="stylesheet" href="style/docs.css" />
</head>
<body class="language-javascript" id="docs">
<header>
<a href="index.html">
<h1>Bliss</h1>
<p>Heavenly JavaScript</p>
</a>
</header>
<nav id="toc">
<h1>Contents</h1>
<ol></ol>
</nav>
<section id="basics">
<h1>Overview</h1>
<p>Bliss includes several static methods (on the <code>Bliss</code> or <code>$</code> object). For example, to copy all properties of an object onto another object, you would call <a href="$.extend"><code>$.extend()</code></a>:</p>
<pre><code>var yolo = $.extend({foo: 1}, {bar: 2}); // or Bliss.extend(…)
// yolo is {foo: 1, bar: 2}</code></pre>
<p>Many of Bliss’ methods take an element or an array of elements as their first argument. For example, to set both the <code>width</code> and <code>padding</code> of an element to 0, you could use the <a href="#$.style"><code>$.style()</code></a> method:</p>
<pre><code>$.style(element, {width: 0, padding: 0});</code></pre>
<p>These types of methods are also available on elements and arrays, for convenience. However, since adding convenience methods to elements and arrays directly would be a JS Mortal Sin™, Bliss only <a href="http://lea.verou.me/2015/04/idea-extending-native-dom-prototypes-without-collisions">adds a <code>_</code> property on elements & arrays</a>, on which it hangs all its methods, to avoid conflicts. The previous example would be written as:</p>
<pre><code>element._.style({
"width": 0,
"padding": 0
});</code></pre>
<aside><p>The <code>._.</code> sequence of characters that appears all too often when coding with Bliss is where Bliss gets its logo from. However, <a href="#configuration">the property can be customized to anything you want</a>.</p></aside>
<p>Methods that are available on elements like this will have an <a class="tag element">Element</a> tag in these docs.</p>
<p>If you wanted to set the width and padding of multiple elements to 0, you could use an array:</p>
<pre><code>myArray._.style({
"width": 0,
"padding": 0
});</code></pre>
<p>Methods that are available on arrays like this will have an <a class="tag array">Array</a> tag in these docs.</p>
<p>For example, assume that in addition to these CSS changes you also wanted to add a <code>hidden</code> attribute to this element. Bliss methods that don’t return a value return the element or array they are called on, so you could call more element methods on them, including native ones:</p>
<pre><code>element._.style({
"width": 0,
"padding": 0
}).setAttribute("hidden", "");</code></pre>
<p>Now assume you also wanted to set a second attribute: The "class" attribute to "foo". The native <code>setAttribute()</code> method is not chainable, it returns <code>undefined</code>. However, all native element methods are also available on the almighty <code>_</code> property, and there they are also chainable:</p>
<pre><code>element._.style({
"width": 0,
"padding": 0
})._.setAttribute("hidden", "").setAttribute("class", "foo");</code></pre>
<p>This works, but it’s a bit unwieldy. Thankfully, Bliss offers an <code>$.attributes()</code> method for setting multiple attributes at once:</p>
<pre><code>element._.style({
"width": 0,
"padding": 0
})._.attributes({
"hidden": "",
"class": "foo"
});</code></pre>
<p>This is better and more readable, but still a bit awkward. Turns out there is a special <a href="#$.set"><code>$.set()</code></a> method to do both at once:</p>
<pre><code>element._.set({
attributes: {
"hidden": "",
"class": "foo"
},
style: {
"width": 0,
"padding": 0
}
});</code></pre>
<p>Because <code>$.attributes()</code> and <code>$.style()</code> are also available for <code>$.set()</code> parameters, they will have the special <a class="tag set">$.set()</a> tag in these docs.</p>
<p>Note that you don’t actually need <code>attributes: {…}</code> in <code>$.set()</code> at all: if there are any unrecognized properties in the parameter object, Bliss will first check if there is a property with that name on the element and if not, set an attribute. So you could rewrite the example above as:</p>
<pre><code>element._.set({
"hidden": "",
"class": "foo", // or "className": "foo" to use the property
style: {
"width": 0,
"padding": 0
}
});</code></pre>
</section>
<section id="vanilla">
<h1>Vanilla Methods</h1>
<p>You don’t need Bliss or any library for any of these. All the following are 100% pure Vanilla JS! Click on the code for documentation and browser support info.</p>
<table>
<thead>
<tr>
<th>Action</th>
<th>Vanilla JS</th>
</tr>
</thead>
<tbody>
<tr>
<td>Adding the class "my-class" on <code>element</code></td>
<td><a href="https://developer.mozilla.org/en-US/docs/Web/API/Element/classList"><pre><code>element.classList.add("my-class");</code></pre></a></td>
</tr>
<tr>
<td>Removing the class "my-class" from <code>element</code></td>
<td><a href="https://developer.mozilla.org/en-US/docs/Web/API/Element/classList"><pre><code>element.classList.remove("my-class");</code></pre></a></td>
</tr>
<tr>
<td>Adding the class "my-class" on <code>element</code> if it doesn’t already have it and removing it if it does:</td>
<td><a href="https://developer.mozilla.org/en-US/docs/Web/API/Element/classList"><pre><code>element.classList.toggle("my-class");</code></pre></a></td>
</tr>
<tr>
<td>Checking if <code>element</code> contains the class "my-class"</td>
<td>
<a href="https://developer.mozilla.org/en-US/docs/Web/API/Element/classList"><pre><code>element.classList.contains("my-class")</code></pre></a>
</td>
</tr>
<tr>
<td>Removing <code>element</code> from the DOM</td>
<td><a href="https://developer.mozilla.org/en-US/docs/Web/API/ChildNode/remove"><pre><code>element.remove();</code></pre></a></td>
</tr>
<tr>
<td>Checking if <code>element</code> contains <code>otherElement</code></td>
<td>
<a href="https://developer.mozilla.org/en-US/docs/Web/API/Node/contains"><pre><code>element.contains(otherElement)</code></pre></a>
</td>
</tr>
<tr>
<td>Check if <code>element</code> matches <code>selector</code></td>
<td><a href="https://developer.mozilla.org/en-US/docs/Web/API/Element/matches">
<pre><code>element.matches(selector)</code></pre>
</a></td>
</tr>
<tr>
<td>Find the closest ancestor (or self) of <code>element</code> that matches <code>selector</code></td>
<td>
<a href="https://developer.mozilla.org/en-US/docs/Web/API/Element/closest">
<pre><code>element.closest(selector)</code></pre>
</a>
</td>
</tr>
<tr>
<td>Find next sibling of <code>element</code> that is a real element (not a text node or comment node):</td>
<td><a href="https://developer.mozilla.org/en-US/docs/Web/API/NonDocumentTypeChildNode/nextElementSibling">
<pre><code>element.nextElementSibling</code></pre>
</a></td>
</tr>
<tr>
<td>Get all children of <code>element</code> which are real elements</td>
<td><a href="https://developer.mozilla.org/en-US/docs/Web/API/ParentNode/children">
<pre><code>element.children</code></pre>
</a></td>
</tr>
<!-- tr>td+td>a[href]>pre>code -->
</tbody>
</table>
<p>Note that all vanilla <strong>methods</strong> from elements (specifically, from <code>HTMLElement</code>) are also available on <code>$</code> as well as the <code>_</code> property for both elements and arrays, so for example, to remove all divs on a page, you could write either of the following:</p>
<pre><code>$$("div")._.remove();</code></pre>
<pre><code>$.remove($$("div"));</code></pre>
</section>
<section>
<h1>DOM</h1>
<p><strong>Note:</strong> The <code>$</code> is just an alias to <code>Bliss</code>. If another library that uses <code>$</code> is included before Bliss, then Bliss will not use <code>$</code>. You can always alias <code>$</code> to <code>Bliss</code> manually, by putting this before any of your code (but after including Bliss):</p>
<pre><code>self.$ = Bliss;</code></pre>
<p>Similarly, <code>$$()</code> is an alias to Bliss’ <code>$.$()</code> or <code>Bliss.$()</code> function. If it’s already defined by the time Bliss is called, Bliss will not use it. You can do so manually with this line:</p>
<pre><code>self.$$ = Bliss.$;</code></pre>
<p>When writing Bliss plugins, if you want to use <code>$()</code> and <code>$$()</code> it’s good practice to make local variables with them, in case they’re not available:</p>
<pre><code>(function($, $$){
// Plugin code here
})(Bliss, Bliss.$)</code></pre>
<article>
<h1 class="transform-ignore">$()</h1>
<p class="description">Select an element by selector. Mainly a shortcut for <code>element.querySelector()</code>.</p>
<pre><code>var element = $(selector [, context])</code></pre>
<dl class="args">
<dt class="string">selector</dt>
<dd>The CSS selector to use.</dd>
<dt class="element">context</dt>
<dd>Returned element needs to be a descendant of this element. <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/%3Ascope"><code>:scope</code></a> inside <code>selector</code> refers to this element (for supporting browsers).</dd>
<dt class="element">element</dt>
<dd>The matched element or <code>null</code> if none found.</dd>
</dl>
<pre class="examples"><code>// return the first element with a class of .foo
// that is inside the first element with a class of .bar
var ret = $(".foo", $(".bar"));</code></pre>
<pre><code>// Return the first element with a class of .foo
// that is inside any element with a class of .bar
var ret = $(".bar .foo");</code></pre>
<pre><code>// Get the first element with a class of .foo
// and set its title attribute to "yolo"
// If there is no such element, this will throw an exception!
$(".foo").setAttribute("title", "yolo");</code></pre>
<ul class="notes">
<li>If the element is not found, it will return <code>null</code>. This could cause an exception if methods are called on the result.
You might previously check if the element exists by caching the node in a variable
into an <code>if</code> statement:</li>
</ul>
<pre><code>// Check if the .foo element exists
// and set an attribute by using a variable "foo"
// as a reference of the element
var foo = $(".foo");
if (foo) {
foo.setAttribute("title", "yolo");
}</code></pre>
<a href="http://api.jquery.com/jQuery/#jQuery1" class="jq">$</a>
</article>
<article>
<h1 class="transform-ignore">$$()</h1>
<p class="description">Get all elements that match a given selector as an array. Similar to <code>element.querySelectorAll()</code> but returns an Array instead of a NodeList, so it has all of the convenience methods we’ve come to love on arrays.</p>
<pre><code>var array = $$(selector [, context])</code></pre>
<dl class="args">
<dt class="string">selector</dt>
<dd>The CSS selector to use.</dd>
<dt class="element">context</dt>
<dd>Returned elements need to be a descendants of this element. <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/%3Ascope"><code>:scope</code></a> inside <code>selector</code> refers to this element (for supporting browsers).</dd>
<dt class="array">array</dt>
<dd>The matched elements as an array.</dd>
</dl>
<pre class="def"><code>var array = $$(collection)</code></pre>
<dl class="args">
<dt>collection</dt>
<dd class="type">Type: Array-like</dd>
<dd>Any array-like object, such as a NodeList, a function’s <code>arguments</code> object or an elements <a href="https://developer.mozilla.org/en-US/docs/Web/API/Element/attributes"><code>element.attributes</code></a> collection.</dd>
<dt class="array">array</dt>
<dd>The collection converted to an array.</dd>
</dl>
<pre class="examples"><code>// Add an id to all <h1> headings that don’t already have one
$$("h1:not([id])").forEach(function(h1){
h1.id = h1.textContent.replace(/\W/g, "");
});</code></pre>
<pre><code>// Get an array with all ids on the page
var ids = $$("[id]").map(function(element){
return element.id;
});</code></pre>
<pre><code>// Get all of an element’s attributes starting with data-bliss-
$$(element.attributes).filter(function(attribute){
return attribute.name.indexOf("data-bliss-") === 0;
}).map(function(attribute){
return attribute.name;
});</code></pre>
</article>
<article>
<h1>create</h1>
<p class="description">Create an element.</p>
<pre><code>var element = $.create([tag,] [options]);</code></pre>
<dl class="args">
<dt class="string">tag</dt>
<dd>The tag name of the element to create.</dd>
<dt class="object">options</dt>
<dd>An object with several options for the new element (properties, attributes, events etc). For details about what options this object accepts, see <code>$.set()</code>. If <code>tag</code> is omitted, you need to provide it in the <code>options</code> object.</dd>
<dt class="element">element</dt>
<dd>The newly created element.</dd>
</dl>
<pre><code>$.create("ul", {
className: "nav",
contents: [
"Navigation: ",
{tag: "li",
contents: {tag: "a",
href: "index.html",
textContent: "Home"
}
},
{tag: "li",
contents: {tag: "a",
href: "contact.html",
textContent: "Contact",
target: "_blank"
}}
]
});</code></pre>
<pre><code>var paragraph = $.create("p");</code></pre>
<pre><code>var div = $.create();</code></pre>
<ul class="notes">
<li>If you’re only creating an element without setting any options on it, just use the native <code>document.createElement(tag)</code></li>
<li><code>$.create(tag, options)</code> is equivalent to:
<pre><code>$.set(document.createElement(tag), options)</code></pre> This is also the recommended syntax if you need a different <code>document</code> than the current one.</li>
</ul>
</article>
<article>
<h1 class="element array">set</h1>
<p class="description">Set multiple traits of an element (properties, attributes, events, contents etc). One of the most useful functions in Bliss.</p>
<pre><code>subject = $.set(subject, options);</code></pre>
<pre><code>subject = subject._.set(options);</code></pre>
<dl class="args">
<dt class="element array">subject</dt>
<dd>The element to apply the options on.</dd>
<dt class="object">options</dt>
<dd>An object with various options, including:
<dl>
<dt>*</dt>
<dd>Any remaining properties will be added as properties or attributes on the element (Bliss first checks if there is a property with that name, and if not, it sets it as an attribute.</dd>
</dl>
</dd>
</dl>
<pre><code>$.set(document.createElement("nav"), {
style: {
color: "red"
},
events: {
click: function(evt) {
console.log("YOLO");
}
},
contents: ["Navigation: ", {tag: "ul",
className: "buttons",
delegate: {
click: {
li: function() {
console.log("A list item was clicked");
}
}
},
contents: [{tag: "li",
contents: {tag: "a",
href: "index.html",
textContent: "Home"
}
}, {tag: "li",
contents: {tag: "a",
href: "docs.html",
textContent: "Docs"
}
}
]
}],
inside: $("body > header")
});</code></pre>
<ul class="notes">
<li><strong>Tip:</strong> Add more specially handled properties of your own by just adding a function to <code>$.setProps</code>.</li>
<li><strong>Warning:</strong> If setting SVG attributes, use the explicit <code>attributes</code> property, not just properties directly on <code>options</code>. Otherwise, they will be added as properties, and the API for SVG DOM properties is the stuff of nightmares.</li>
</ul>
</article>
<article>
<h1 class="element array set">contents</h1>
<p class="description">Append existing or newly created element(s) and text nodes to an element.</p>
<pre><code>subject = $.contents(subject, elements)</code></pre>
<pre><code>subject = subject._.contents(elements)</code></pre>
<pre><code>subject = subject._.set({contents: elements})</code></pre>
<dl class="args">
<dt class="element array">subject</dt>
<dd>The element(s) to append the elements to.</dd>
<dt class="object array string number node">elements</dt>
<dd>If it’s an object, it will be passed through <code>$.create()</code>, to be converted into an element. If it’s a string or number, it will be converted to a text node. If it’s a <code>Node</code> it will be used as-is. If it’s an array, its elements will be processed according to the aforementioned rules. Note that this lets you recursively create entire subtrees, since these objects can also have a <code>contents</code> property of their own, as seen in the example below.</dd>
</dl>
<pre><code>nav._.contents(["Navigation: ", {tag: "ul",
className: "buttons",
delegate: {
click: {
li: function() {
console.log("A list item was clicked")
}
}
},
contents: [{tag: "li",
contents: {tag: "a",
href: "index.html",
textContent: "Home"
}
}, {tag: "li",
contents: {tag: "a",
href: "docs.html",
textContent: "Docs"
}
}
]
}])</code></pre>
<a class="jq">jQuery.fn.html</a>
</article>
<article class="full">
<h1 class="element full">clone</h1>
<p class="description">Clone an element including its descendants, with events and data. <strong>This function is deprecated and will be removed in the next version of Bliss</strong></p>
<pre><code>var clone = $.clone(subject)</code></pre>
<pre><code>var clone = subject._.clone()</code></pre>
<dl class="args">
<dt class="element">subject</dt>
<dd>The element to be cloned.</dd>
<dt class="element">clone</dt>
<dd>The cloned element.</dd>
</dl>
<pre><code>var button = $("button");
button.addEventListener("click", function() { console.log("Click from listener!"); });
button.onclick = function() { console.log("Click from inline event!"); };
var button2 = button._.clone();
// If clicked, button2 will print both messages</code></pre>
<ul class="notes">
<li>If you don’t care about copying events and data, just use the native <a href="https://developer.mozilla.org/en-US/docs/Web/API/Node/cloneNode"><code>element.cloneNode()</code></a></li>
</ul>
<a class="jq">jQuery.fn.clone</a>
</article>
<article>
<h1 class="element array set">after</h1>
<p class="description">Insert an element after another.</p>
<pre><code>subject = $.after(subject, element)</code></pre>
<pre><code>subject = subject._.after(element)</code></pre>
<pre><code>subject = subject._.set({after: element})</code></pre>
<dl class="args">
<dt class="element">subject</dt>
<dd>The element to be inserted.</dd>
<dt class="element">element</dt>
<dd>The element to insert after.</dd>
</dl>
<p class="needs example"></p>
<a class="jq">jQuery.fn.after</a>
</article>
<article>
<h1 class="element set">around</h1>
<p class="description">Wrap an element around another.</p>
<pre><code>subject = $.around(subject, element)</code></pre>
<pre><code>subject = subject._.around(element)</code></pre>
<pre><code>subject = subject._.set({around: element})</code></pre>
<dl class="args">
<dt class="element">subject</dt>
<dd>The element to wrap with.</dd>
<dt class="element">element</dt>
<dd>The element to be wrapped.</dd>
</dl>
<pre><code>// Wrap headings with a link to their section
$$("section[id] > h1, article[id] > h1").forEach(function(h1){
$.create("a", {
href: "#" + h1.parentNode.id,
around: h1
});
});</code></pre>
<a class="jq">jQuery.fn.wrap</a>
</article>
<article>
<h1 class="element array set">attributes</h1>
<p class="description">Set multiple attributes on one or more elements.</p>
<pre><code>subject = $.attributes(subject, attrs)</code></pre>
<pre><code>subject = subject._.attributes(attrs)</code></pre>
<pre><code>subject = subject._.set({attributes: attrs})</code></pre>
<dl class="args">
<dt class="element array">subject</dt>
<dd>The element(s) to set the attributes on.</dd>
<dt class="object">attrs</dt>
<dd>An object with the attributes and values to set.</dd>
</dl>
<p class="needs example"></p>
<ul class="notes">
<li>If only setting one attribute on one element, consider using the native <a href="https://developer.mozilla.org/en-US/docs/Web/API/Element/setAttribute"><code>element.setAttribute(name, value)</code></a>. If setting one attribute on multiple elements, you can use <code>myArray._.setAttribute(name, value)</code>.</li>
</ul>
</article>
<article>
<h1 class="element array">toggleAttribute</h1>
<p class="description">Set or remove an attribute based on a test</p>
<pre><code>subject = $.toggleAttribute(subject, name, value [, test])</code></pre>
<pre><code>subject = subject._.toggleAttribute(attrs)</code></pre>
<dl class="args">
<dt class="element array">subject</dt>
<dd>The element(s) to set the attributes on.</dd>
<dt class="string">name</dt>
<dd>The attribute name</dd>
<dt class="string">value</dt>
<dd>The attribute value</dd>
<dt>test</dt>
<dd>The test. If truthy, set the attribute. If falsy, remove it. If not present, it’s false when <code>value</code> is <code>null</code> and true otherwise.</dd>
</dl>
<p class="needs example"></p>
</article>
<article>
<h1 class="element array set">before</h1>
<p class="description">Insert the element before another.</p>
<pre><code>subject = $.before(subject, element)</code></pre>
<pre><code>subject = subject._.before(element)</code></pre>
<pre><code>subject = subject._.set({before: element})</code></pre>
<dl class="args">
<dt class="element">subject</dt>
<dd>The element to be inserted.</dd>
<dt class="element">element</dt>
<dd>The element to insert before.</dd>
</dl>
<p class="needs examples"></p>
<a class="jq">jQuery.fn.before</a>
</article>
<article>
<h1 class="element array set">inside</h1>
<p class="description">Insert the element inside another, after any existing contents.</p>
<pre><code>subject = $.inside(subject, element)</code></pre>
<pre><code>subject = subject._.inside(element)</code></pre>
<pre><code>subject = subject._.set({inside: element})</code></pre>
<ul class="notes">Mainly useful inside <code>$.set()</code>, otherwise just use the native <code>element.appendChild(subject)</code>.</ul>
<a class="jq">jQuery.fn.append</a>
</article>
<article>
<h1 class="element array set">properties</h1>
<p class="description">Set a number of properties on an element.</p>
<pre><code>subject = $.properties(subject, props)</code></pre>
<pre><code>subject = subject._.properties(props)</code></pre>
<pre><code>subject = subject._.set({properties: props})</code></pre>
<dl class="args">
<dt class="element array">subject</dt>
<dd>The element(s) to set the properties on.</dd>
<dt class="object">props</dt>
<dd>The properties to set on the element.</dd>
</dl>
<pre><code>document.createElement("button")._.properties({
className: "continue",
textContent: "Next Step",
onclick: function() { MyApp.next() }
});</code></pre>
<ul class="notes">
<li>If only setting one property on one element, just use <code>element[property] = value;</code>. If setting multiple properties on one element, you can use <code>$.extend(subject, props)</code> instead.</li>
</ul>
<a class="jq">jQuery.fn.prop</a>
</article>
<article>
<h1 class="element array set">start</h1>
<p class="description">Insert an element inside another, before its existing contents.</p>
<pre><code>subject = $.start(subject, element)</code></pre>
<pre><code>subject = subject._.start(element)</code></pre>
<pre><code>subject = subject._.set({start: element})</code></pre>
<a class="jq">jQuery.fn.prepend</a>
</article>
<article>
<h1 class="element array set">style</h1>
<p class="description">Set multiple CSS properties on one or more elements. Both camelCase and hyphen-case attributes are allowed.</p>
<pre><code>subject = $.style(subject, properties)</code></pre>
<pre><code>subject = subject._.style(properties)</code></pre>
<pre><code>subject = subject._.set({style: properties})</code></pre>
<dl class="args">
<dt class="element array">subject</dt>
<dd>The element(s) to apply the CSS properties to.</dd>
<dt class="object">properties</dt>
<dd>The CSS properties to apply, in camelCase format</dd>
</dl>
<pre class="examples"><code>document.body._.style({
color: "white",
backgroundColor: "red",
cssFloat: "left",
"--my-variable": 5
});</code></pre>
<ul class="notes">
<li>If only setting one property on one element, you don’t need this. Just use the native <code>element.style.propertyName = value;</code> syntax.</li>
</ul>
<a class="jq">jQuery.fn.css</a>
</article>
<article>
<h1 class="element">transition</h1>
<p class="description">Set multiple CSS properties with a transition and execute code after the transition is finished.</p>
<pre><code>var promise = $.transition(subject, properties [, duration])</code></pre>
<pre><code>var promise = subject._.transition(properties [, duration])</code></pre>
<dl class="args">
<dt class="element array">subject</dt>
<dd>The element(s) to apply the transitions to.</dd>
<dt class="object">properties</dt>
<dd>The CSS properties to apply, in camelCase format</dd>
<dt class="number">duration</dt>
<dd>The duration, in milliseconds. Defaults to 400.</dd>
<dt class="promise">promise</dt>
<dd>Promise that gets resolved after the transition is done, or immediately <a href="http://caniuse.com/#feat=css-transitions">if CSS transitions are not supported</a>.</dd>
</dl>
<pre class="examples"><code>// Fade out an element then remove it from the DOM
$.transition(element, {opacity: 0}).then($.remove);</code></pre>
<pre><code>// Fade out and shrink all <div>s on a page,
// then remove them from the DOM
Promise.all($$("div")._.transition({
opacity: 0,
transform: "scale(0)"
})).then($.remove);</code></pre>
<ul class="notes">
<li>Uses CSS transitions, not custom interpolation code. In browsers that do not support CSS transitions, the style is applied immediately and the promise returned is resolved.</li>
<li>The properties applied remain after the transition has ended.</li>
</ul>
<a class="jq">jQuery.fn.animate</a>
</article>
</section>
<section>
<h1>Events</h1>
<article>
<h1 class="element array set">delegate</h1>
<p class="description">Helper for event delegation. Helps you register events for children at an ancestor, so that when you add children that match the provided selector, they also trigger the event.</p>
<pre><code>subject = $.delegate(subject, type, selector, callback)</code></pre>
<pre><code>subject = subject._.delegate(type, selector, callback)</code></pre>
<dl class="args">
<dt class="element array">subject</dt>
<dd>The delegation root. Only elements inside this will fire the event.</dd>
<dt class="string">type</dt>
<dd>The event type.</dd>
<dt class="string">selector</dt>
<dd>The selector the target of the event needs to match.</dd>
</dl>
<pre class="def"><code>subject = $.delegate(subject, type, selectorsToCallbacks)</code></pre>
<pre class="def"><code>subject = subject._.delegate(type, selectorsToCallbacks)</code></pre>
<dl class="args">
<dt class="element array">subject</dt>
<dd>The delegation root. Only elements inside this will fire the event.</dd>
<dt class="string">type</dt>
<dd>The event type.</dd>
<dt class="object">selectorsToCallbacks</dt>
<dd>An object of the form <code>{selector1: callback1, …, selectorN: callbackN}</code>.</dd>
</dl>
<pre class="def"><code>subject = $.delegate(subject, typesToSelectorsToCallbacks)</code></pre>
<pre class="def"><code>subject = subject._.delegate(typesToSelectorsToCallbacks)</code></pre>
<pre class="def"><code>subject = subject._.set({delegate: typesToSelectorsToCallbacks})</code></pre>
<dl class="args">
<dt class="element array">subject</dt>
<dd>The delegation root. Only elements inside this will fire the event.</dd>
<dt class="object">typesToSelectorsToCallbacks</dt>
<dd>An object of the form <pre><code>{
type1: {selector11: callback11, …, selector1N: callback1N},
…
typeM: {selectorM1: callbackM1, …, selectorMK: callbackMK}
}</code></pre>.</dd>
</dl>
<a class="jq">jQuery.fn.delegate</a>
</article>
<article>
<h1 class="element array">bind</h1>
<p class="description">Set multiple event listeners on one or more elements.</p>
<pre><code>subject = $.bind(subject, handlers)</code></pre>
<pre><code>subject = subject._.bind(handlers)</code></pre>
<dl class="args">
<dt class="element array">subject</dt>
<dd>The element(s) to set the event listeners on.</dd>
<dt class="object">handlers</dt>
<dd>An object whose keys are the events and the values the listeners.
For details on the keys, see <code>types</code> below.
For details on the values, see <code>callback</code> and <code>options</code> below.</dd>
</dl>
<pre><code>$$('input[type="range"]')._.bind({
"input change": function(evt) { this.title = this.value},
"focus.className": function(evt) { console.log(evt.type); }
})</code></pre>
<pre class="def"><code>subject = $.bind(subject, types, [callback], [options])</code></pre>
<pre class="def"><code>subject = subject._.bind(types, [callback], [options])</code></pre>
<dl class="args">
<dt class="element array">subject</dt>
<dd>The element(s) to set the event listeners on.</dd>
<dt class="string">types</dt>
<dd>The event type. You can include multiple event types by space-separating them.
You can add a “class” on each event type by using a period (<code>click.myclass</code>), which you can later use in <code>$.unbind()</code> for unbinding all events with a certain class at once.</dd>
<dt class="function">callback</dt>
<dd>The function to execute. Passed directly to <code>addEventListener</code>.</dd>
<dt class="boolean object">options</dt>
<dd>Capture or options object to pass to <code>addEventListener</code>.
For details on what options are available, refer to the documentation for <a href="https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener"><code>addEventListener</code></a>
You can also combine the <code>options</code> and <code>callback</code> parameters, by including a <code>callback</code> property.
This is useful when using the syntax above for multiple handlers if you also want to pass options.
</dd>
</dl>
<ul class="notes">
<li>If only setting one event listener on one element and you are keeping a reference to the callback (or are using Bliss Full), just use the native <code>element.addEventListener(type, handler, useCapture)</code> syntax.
</li>
<li>You can use classes in your event names, like <code>click.foo</code>. The event that will be bound in that case is without the class name (<code>click</code>), but you can use the class name to unbind via <code>$.unbind()</code></li>
</ul>
<a class="jq">jQuery.fn.bind</a>
</article>
<article>
<h1 class="element array set">events</h1>
<p class="description">Just like <code>$.bind()</code>, but also lets you copy events from another element.
The latter is the only syntax described here, for the <code>$.bind()</code> syntax look above.</p>
<pre><code>subject = $.events(subject, element)</code></pre>
<pre><code>subject = subject._.events(element)</code></pre>
<pre><code>subject = subject = subject._.set({events: element})</code></pre>
<dl class="args">
<dt class="element array">subject</dt>
<dd>The element(s) to set the event listeners on.</dd>
<dt class="object element">element</dt>
<dd>The element to copy listeners from. Inline events set via HTML or <code>on*</code> properties are also copied.</dd>
</dl>
<ul class="notes">
<li>If using Bliss Shy, only listeners added via Bliss methods will be copied.</li>
</ul>
</article>
<article>
<h1 class="element array">unbind</h1>
<p class="description">Unbind event listeners en masse.</p>
<pre><code>subject = $.unbind(subject, handlers)</code></pre>
<pre><code>subject = subject._.unbind(handlers)</code></pre>
<dl class="args">
<dt class="element array">subject</dt>
<dd>The element(s) to unbind the event listeners from.</dd>
<dt class="object">handlers</dt>
<dd>An object whose keys are the event types and the values the callbacks.
For details about what these can be, read about the <code>type</code> and <code>callback</code> arguments below.</dd>
</dl>
<pre><code>$$('input[type="range"]')._.unbind({
"input change": function(evt) { this.title = this.value},
})</code></pre>
<pre class="def"><code>subject = $.unbind(subject, [type[, callback]])</code></pre>
<pre class="def"><code>subject = subject._.unbind([type[, callback]])</code></pre>
<dl class="args">
<dt class="element array">subject</dt>
<dd>The element(s) to unbind the event listeners from.</dd>
<dt class="string">type</dt>
<dd>The event type. You can include multiple event types by space-separating them.
<strong>If using Bliss Full</strong>:
You can also unbind by class (e.g. <code>click.foo</code> or even just <code>.foo</code> to unbind all events with that class).
You can also unbind all listeners on the element, by not providing <strong>any</strong> arguments. This will remove all listeners with capture option set to either true or false.
This works for Bliss Shy and anonymous functions too, if the listeners were added using Bliss bind method. Bliss (both Full and Shy) keeps track of anonymous functions so these listeners may be removed later.
</dd>
<dt class="function">callback</dt>
<dd>The callback. <strong>If using Bliss Full</strong>, this is optional. If not provided, all callbacks matching the event type and/or class specified by <code>type</code> will be unbound.</dd>
</dl>
<pre><code>document.body._.events({
"click.foo mousedown.foo": function(e) { console.log("foo", e.type)},
"click.bar mousedown.bar": function(e) { console.log("bar", e.type)},
});
// Clicking on the body now prints foo mousedown, bar mousedown, foo click, bar click
document.body._.unbind(".foo");
// Clicking on the body now prints bar mousedown, bar click
document.body._.unbind("click");
// Clicking on the body now prints only bar mousedown
document.body._.unbind(); // unbind ALL events
// Clicking on the body now does nothing
$.unbind(elem)
// Removing all listeners from elem, regardless of event capture setting.
// If using Bliss Shy, $.unbind only works for the listeners added using $.bind method
$.unbind(elem, '', false)
// Removing all listeners from elem, with capture option set to false (the default in most browsers)
$.unbind(elem, '', true)
// Removing all listeners from elem, with capture option set to true explicitly
</code></pre>
<ul class="notes">
<li>If only unbinding one event listener and you have a reference to the callback used, just use the native <code>element.removeEventListener(type, handler, useCapture)</code> syntax.
</li>
<li>If using Bliss Shy, only listeners bound via Bliss methods can be removed without a reference to the callback.</li>
<li>Avoid using overly liberal unbinding queries (e.g. no arguments or just a type) in code meant to be used in environments you don’t control.
You don’t want to conflict with other people’s code! Use a class instead.</li>
</ul>
<a class="jq">jQuery.fn.unbind</a>
</article>
<article>
<h1 class="element array">fire</h1>
<p class="description">Fire a synthesized event.</p>
<pre><code>subject = $.fire(subject, type [, properties])</code></pre>
<pre><code>subject = subject._.fire(type [, properties])</code></pre>
<dl class="args">
<dt class="element array">subject</dt>
<dd>The element(s) to fire the synthesized event on.</dd>
<dt class="string">type</dt>
<dd>The event type.</dd>
<dt class="object">properties</dt>
<dd>If provided, these properties will be added to the event object.</dd>
</dl>
<pre class="examples"><code>// Fire a custom event on a map widget
myMap._.fire("locationchange", {
location: [42.361667, -71.092751]
});</code></pre>
<pre><code>// Fire a fake input event
myInput._.fire("input");</code></pre>
<ul class="notes">
<li>If you are writing a library meant to be run on websites you don’t control, it is recommended that you namespace your events by adding the library name before them, like <code>"mylibrary-locationchange"</code> or <code>"mylibrary:locationchange"</code>, to avoid collisions.</li>
</ul>
<a class="jq">jQuery.fn.trigger</a>
</article>
<article>
<h1 class="element array set">once</h1>
<p class="description">Set event listeners that will be fired once per callback per element.</p>
<pre><code>subject = $.once(subject, handlers)</code></pre>
<pre><code>subject = subject._.once(handlers)</code></pre>
<pre><code>subject = subject = subject._.set({once: handlers})</code></pre>
<dl class="args">
<dt class="element array">subject</dt>
<dd>The element(s) to set the event listeners on.</dd>
<dt class="object element">handlers</dt>
<dd>An object whose keys are the events and the values the listeners. You can include multiple event types by space-separating them.</dd>
</dl>
<a class="jq">jQuery.fn.one</a>
</article>
<article>
<h1>ready</h1>
<p class="description">Execute code after the DOM is ready.</p>
<pre><code>var promise = $.ready([context], [callback])</code></pre>
<dl class="args">
<dt class="document">context</dt>
<dd>The document whose …readiness we’re interested in.</dd>
<dt class="function">callback</dt>
<dd>A callback that gets executed synchronously if the DOM is already ready or on <code>DOMContentLoaded</code> if not.</dd>
<dt class="promise">promise</dt>
<dd>A promise that gets resolved immediately if the DOM is already ready, or on <code>DOMContentLoaded</code> if not.</dd>
</dl>
<pre class="examples"><code>// Add a red border to all divs on a page
$.ready().then(function(){
$$("div")._.style({ border: "1px solid red" });
});</code></pre>
<a class="jq">jQuery.fn.ready</a>
</article>
<article>
<h1 class="element">when</h1>
<p class="description">Defer code until an event fires.</p>
<pre><code>var promise = $.when(subject, type [, test])</code></pre>
<pre><code>var promise = subject._.when(type [, test])</code></pre>
<dl class="args">
<dt class="element">subject</dt>
<dd>The element to listen for the event on.</dd>
<dt class="string">type</dt>
<dd>The event type.</dd>
<dt class="function">test</dt>
<dd>If provided, this test will also need to pass for the promise to resolve.</dd>
<dt class="promise">promise</dt>
<dd>A promise that gets resolved once BOTH the event is fired and the test (if one is present) passes.</dd>
</dl>
<pre class="examples"><code>// Defer code until Esc is pressed for the first time
$.when(document, "keyup", evt => evt.key === "Escape").then(evt => console.log("Esc pressed!", evt));</code></pre>
<ul class="notes">
<li>If the event fires multiple times, your code will only run the first time.</li>
<li>If the event fires but the test doesn’t pass, the promise will resolve once it does.</li>
</ul>
</article>
</section>
<section>
<h1>Objects & Arrays</h1>
<article>
<h1 class="array">all</h1>
<p class="description">Run a method on all elements of an array, get the results as an array.</p>
<pre><code>var ret = $.all(array, method [, args...])</code></pre>
<pre><code>var ret = array._.all(method [, args...])</code></pre>
<dl class="args">
<dt class="array">array</dt>
<dd>The array whose every element we want to run a method on.</dd>
<dt class="string">method</dt>
<dd>The method name</dd>
<dt>args</dt>
<dd>Any arguments to pass to the method</dd>
<dt class="array">ret</dt>
<dd>The array of results. If the method returns no results, this will be the same as <code>array</code>.</dd>
</dl>
<pre><code>// Uppercase all strings in an array
["Foo", "bar"]._.all("toUpperCase"); // Returns ["FOO", "BAR"]</code></pre>
<ul class="notes">
<li>Basically syntactic sugar over: <pre><code>array.map(function(a) {
return a[method](args...);
});</code></pre></li>
</ul>
</article>
<article>
<h1>Class</h1>
<p class="description">Helper for defining OOP-like “classes”, for those who have been irreparably damaged by Java-like languages.</p>
<pre><code>var myClass = $.Class(options);</code></pre>
<dl class="args">
<dt class="object">options</dt>
<dd>An object with the following options:
<dl class="options">
<dt class="function">constructor</dt>
<dd>The constructor.</dd>
<dt class="function">extends</dt>
<dd>The “class” it inherits from.</dd>
<dt class="boolean">abstract</dt>
<dd>If <code>true</code>, “classes” can inherit from it, but it will throw an error if called with the <code>new</code> operator directly. Sorry Java folks, I know this is not what you hoped for.</dd>
<dt class="object">lazy</dt>
<dd>Lazily evaluated properties. See <code>$.lazy()</code>.</dd>
<dt class="object">live</dt>
<dd>See <code>$.live()</code>.</dd>
<dt class="object">static</dt>
<dd>Any static methods, as a shortcut to <code>$.extend(myClass, static)</code></dd>
<dt>*</dt>
<dd>Any remaining properties will be added on <code>myClass.prototype</code> as instance methods.</dd>
</dl>
</dd>
</dl>
<ul class="notes">
<li>To enable more properties with “special” handling, like <code>lazy</code> and <code>live</code>, add them to the <code>$.classProps</code> object.</li>
</ul>
</article>
<article>
<h1>each</h1>
<p class="description">Loop over properties of an object and map them onto another object.</p>
<pre><code>var ret = $.each(obj, callback [, ret]);</code></pre>
<dl class="args">
<dt class="object">obj</dt>
<dd>The object to loop over</dd>
<dt class="function">callback</dt>
<dd>The function to be executed for every property. It will be called with <code>obj</code> as its context and the property and value as its arguments.</dd>
<dt class="object">ret</dt>
<dd>The returned object. If not provided, a new object will be created. Provide the same object as <code>obj</code> to overwrite.</dd>
</dl>
<ul class="notes">
<li>Inherited properties will be included in the loop.</li>
</ul>
</article>
<article>
<h1>extend</h1>
<p class="description">Copy properties of an object onto another.</p>
<pre><code>target = $.extend(target, source [, whitelist])</code></pre>
<dl class="args">
<dt class="object">target</dt>
<dd>The object that will receive the new properties.</dd>
<dt class="object">source</dt>
<dd>An object containing the additional properties to merge in.</dd>
<dt class="array string function regExp">whitelist</dt>
<dd>If array or string, a whitelist of property names. If function, it is called on each property and only the properties it returns a truthy value on will be copied. If it’s a regular expression, only matching property names will be copied.</dd>
</dl>
<pre><code>var o1 = {foo: 1, bar:2}
o2 = $.extend(o1, {foo: 3, baz: 4});
// o2 is {foo: 3, bar: 2, baz: 4}</code></pre>
<pre><code>// Get typography-related computed style on <body>
var type = $.extend({},
getComputedStyle(document.body),
/^font|^lineHeight$/);</code></pre>
<ul class="notes">
<li>Overwrites are allowed.</li>
<li>Inherited properties are included (i.e. <code>Object.hasOwnProperty()</code> is not used), unless <code>whitelist</code> is <code>"own"</code>.</li>
<li>It modifies <code>target</code>, it doesn’t create a new object.</li>
<li>If <code>source</code> contains accessors <a href="http://lea.verou.me/2015/08/copying-properties-the-robust-way">it copies the accessors not the result of running the getter</a>. This is unlike similar functions in other libraries or the native <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign"><code>Object.assign()</code></a> in ES6.</li>
</ul>
<a class="jq">jQuery.extend</a>
</article>
<article>
<h1>lazy</h1>
<p class="description">Define lazily evaluated properties on an object.</p>
<pre><code>object = $.lazy(object, property, getter)</code></pre>
<dl class="args">
<dt class="object">object</dt>
<dd>The object to define the property on.</dd>
<dt class="string">property</dt>
<dd>The name of the property to define.</dd>
<dt class="function">getter</dt>
<dd>A function that returns the value of the property. After the first time the property is used, it will be replaced with the return value of this function.</dd>
</dl>
<pre class="def"><code>object = $.lazy(object, properties)</code></pre>
<dl class="args">
<dt class="object">object</dt>
<dd>The object to define the properties on.</dd>
<dt class="object">properties</dt>
<dd>An object where the keys are the property names and the values are the getters.</dd>
</dl>
</article>
<article>
<h1>live</h1>
<p class="description">Define properties that behave like normal properties but also execute code upon getting/setting.</p>
<pre><code>object = $.live(object, property, descriptor)</code></pre>
<dl class="args">
<dt class="object">object</dt>
<dd>The object to define the property on.</dd>
<dt class="string">property</dt>
<dd>The name of the property to define.</dd>
<dt class="object function">descriptor</dt>
<dd>If an object, an <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty#Description">accessor descriptor</a> of the property. If a function, it will be assumed to be executed upon <strong>setting</strong> the property.</dd>
</dl>
<pre class="def"><code>$.live(object, properties)</code></pre>
<dl class="args">
<dt class="object">object</dt>
<dd>The object to define the properties on.</dd>
<dt class="object">properties</dt>
<dd>An object where the keys are the property names and the values are the descriptors.</dd>
</dl>
</article>
<article>
<h1>type</h1>
<p class="description">Determine the internal JavaScript [[Class]] of an object.</p>
<pre><code>var type = $.type(object)</code></pre>
<dl class="args">
<dt class="object">object</dt>
<dd>The object whose [[Class]] we want to get.</dd>
<dt class="string">type</dt>
<dd>The internal [[Class]] of the object, lowercased.</dd>
</dl>
<pre><code>// Check if the second argument of a function is a regexp
if ($.type(arguments[1]) == "regexp") {
// ...
}</code></pre>
<a class="jq">jQuery.type</a>
</article>
<article>
<h1>value</h1>
<p class="description">Get the value of a nested property reference if it exists, and <code>undefined</code> without any errors if not.</p>
<pre><code>var val = $.value([obj, ] property1, [property2 [,...[, propertyN]]])</code></pre>
<dl class="args">
<dt class="object">obj</dt>
<dd>The object to use as the root of the property chain. If not provided, <code>self</code> will be used</dd>
<dt class="string">property1, ..., propertyN</dt>
<dd>The properties to resolve on the root object. Order denotes hierarchy.</dd>
</dl>
<pre><code>$.value(document, "body", "nodeType"); // 1
$.value(document, "body", "foo", "bar", "baz"); // undefined, no errors
$.value("document", "body", "nodeType"); // 1, no root, starting from self</code></pre>
</article>
</section>
<section>
<h1>Async</h1>
<article>
<h1>fetch</h1>
<p class="description">Helper for AJAX calls, inspired by the new <a href="https://fetch.spec.whatwg.org" target="_blank">Fetch API</a>.</p>
<pre><code>var promise = $.fetch(url, options)</code></pre>
<dl class="args">
<dt class="string">url</dt>
<dd>The URL to which the request is sent.</dd>
<dt class="object">options</dt>
<dd>An object with options, including:
<dl class="options">
<dt class="string">method</dt>
<dd>The HTTP method, such as <code>"GET"</code> or <code>"POST"</code>.</dd>
<dt class="string">data</dt>
<dd>Any data to send, as a URLencoded parameter string.</dd>
<dt class="object">headers</dt>
<dd>Object with any extra headers to set.</dd>
<dt>*</dt>
<dd>Any remaining properties will be set on the <code>XMLHttpObject</code> directly.</dd>
</dl>
</dd>
<dt class="promise">promise</dt>
<dd>A promise that is resolved when the resource is successfully fetched and rejected if there is any error.
When the request is successful, the promise resolves with the XHR object.
When the request fails, the promise rejects with an Error whose message is a description of the error.
The error object also contains two properties: <code>xhr</code> which points to the XHR object, and
<code>status</code> which returns the status code (e.g. 404). In case you need to abort the request,
the xhr is returned in a field on the promise, so you can: <code>promise.xhr.abort()</code>.</dd>
</dl>
<pre><code>$.fetch("/api/create", {
method: "POST",
responseType: "json"
}).then(function(){
alert("success!");
}).catch(function(error){
console.error(error, "code: " + error.status);
});</code></pre>
<p><strong>Hooks:</strong> <