Update docs for adding, removing, advanced filters, sorting, and public methods.

pull/111/head
Glen Cheney 8 years ago
parent d7e4f4b720
commit 20823d49af

@ -1,21 +1,30 @@
<h2>Adding and removing items<a href="#adding-removing"></a></h2>
<p>You can add and remove elements from shuffle after it has been created. This also works for infinite scrolling.</p>
<h3>Add a collection</h3>
<h3>Adding elements</h3>
<p>Wherever you add the element in the DOM is where it will show up in the grid (assuming you&rsquo;re using the default sort-by-dom-order). With this in mind, you can append, prepend, or insert elements wherever you need to get them to show up in the right order.</p>
<div class="code-block">
<pre rel="JavaScript"><code class="language-javascript">// Adds 2 &lt;div&gt;s to an existing Shuffle instance.
var $item1 = $('&lt;div class="gallery-item item1"&gt;')
var $item2 = $('&lt;div class="gallery-item item2"&gt;')
var $items = $item1.add($item2);
$('#my-shuffle').append($items);
$('#my-shuffle').shuffle('appended', $items);</code></pre>
<pre rel="JavaScript"><code class="language-javascript">/**
* Create some DOM elements, append them to the shuffle container, then notify
* shuffle about the new items. You could also insert the HTML as a string.
*/
Demo.prototype.onAppendBoxes = function () {
var items = this._getArrayOfElementsToAdd();
items.forEach(function (item) {
this.shuffle.element.appendChild(item);
}, this);
// Tell shuffle items have been appended.
// It expects an array of elements as the parameter.
this.shuffle.add(items);
};</code></pre>
</div>
<h3>Remove a collection</h3>
<h3>Removing elements</h3>
<p>Shuffle will animate the element away and then remove it from the DOM once it's finished. It will then emit the <code>Shuffle.EventType.REMOVED</code> custom event with the array of elements in <code>event.detail.collection</code>.</p>
<div class="code-block">
<pre rel="JavaScript"><code class="language-javascript">// Remove the 2 &lt;div&gt;s which were just added.
var $items = $('.gallery-item');
$('#my-shuffle').shuffle('remove', $items);</code></pre>
<pre rel="JavaScript"><code class="language-javascript">this.shuffle.remove([element1, element2]);</code></pre>
</div>
<p class="demo-link-container">Check out the <a href="{{ site.baseurl }}{% post_url 2013-06-19-adding-removing %}">demo</a>.</p>
<p class="demo-link-container">Check out the <a href="{{ site.baseurl }}{% post_url 2013-06-19-adding-removing %}">adding and removing demo</a>.</p>

@ -1,31 +1,33 @@
<h2>Advanced Filtering<a href="#advanced-filters"></a></h2>
<p>By passing a function to shuffle, you can customize the filtering to your hearts content. Shuffle will iterate over each item and give your function the element wrapped in jQuery and the shuffle instance. Return <code>true</code> to keep the element or <code>false</code> to hide it.</p>
<p>By passing a function to <code>filter</code>, you can fully customize filtering items. Shuffle will iterate over each item and give your function the element and the shuffle instance. Return <code>true</code> to keep the element or <code>false</code> to hide it.</p>
<h3>Example</h3>
<div class="code-block">
<pre rel="JavaScript"><code class="language-javascript">// Filters elements with a data-title attribute with less than 10 characters
$('#grid').shuffle('shuffle', function($el, shuffle) {
return $el.data('title').length &lt; 10;
instance.filter(function (element) {
return element.getAttribute('data-title').length &lt; 10;
});</code></pre>
</div>
<h3>Searching</h3>
<div class="code-block">
<pre rel="JavaScript"><code class="language-javascript">// Advanced filtering
$('.js-shuffle-search').on('keyup change', function() {
var val = this.value.toLowerCase();
$grid.shuffle('shuffle', function($el, shuffle) {
Demo.prototype.addSearchFilter = function () {
document.querySelector('.js-shuffle-search').addEventListener('keyup', this._handleSearchKeyup.bind(this));
};
// Only search elements in the current group
if (shuffle.group !== 'all' &amp;&amp; $.inArray(shuffle.group, $el.data('groups')) === -1) {
return false;
}
// Filter the shuffle instance by items with a title that matches the search input.
Demo.prototype._handleSearchKeyup = function (evt) {
var searchText = evt.target.value.toLowerCase();
var text = $.trim( $el.find('.picture-item__title').text() ).toLowerCase();
return text.indexOf(val) !== -1;
this.shuffle.filter(function (element, shuffle) {
var titleElement = element.querySelector('.picture-item__title');
var titleText = titleElement.textContent.toLowerCase().trim();
return titleText.indexOf(searchText) !== -1;
});
});</code></pre>
};</code></pre>
</div>
<p class="demo-link-container">Check out the <a href="{{ site.baseurl }}{% post_url 2013-05-02-compound-filters %}">compounded filters demo</a>.</p>

@ -1,14 +1,15 @@
<h2>Public Methods<a href="#adding-removing"></a></h2>
<p>A list of the methods available to you and what they do.</p>
<ul class="breathable-list">
<li><code>shuffle</code> - Filters all the shuffle items.</li>
<li><code>sort</code> - Sorts the currently filtered shuffle items.</li>
<li><code>update</code> - Repositions everything. Useful for when dimensions (like the window size) change.</li>
<li><code>layout</code> - Use this instead of <code>update()</code> if you don't need the columns and gutters updated. Maybe an image loaded and now has a height.</li>
<li><code>appended</code> - New items have been appended to the shuffle container.</li>
<li><code>disable</code> - Disables Shuffle from updating dimensions and layout on resize.</li>
<li><code>enable</code> - Enables Shuffle again.</li>
<li><code>remove</code> - Remove one or more shuffle items.</li>
<li><code>destroy</code> - Destroys Shuffle, removes events, styles, classes, and references.</li>
<ul>
<li><code>filter(category, sortObject)</code> - Filters all the shuffle items and then sorts them. <code>category</code> can be a string, array of strings, or a function. The sort object is optional and will use the last-used sort object.</li>
<li><code>sort(sortObject)</code> - Sorts the currently filtered shuffle items.</li>
<li><code>update()</code> - Repositions everything. Useful for when dimensions (like the window size) change.</li>
<li><code>layout()</code> - Use this instead of <code>update()</code> if you don't need the columns and gutters updated. Maybe an image loaded and now has a height.</li>
<li><code>add(newItems)</code> - New items have been appended to the shuffle container. <code>newItems</code> is an array of elements.</li>
<li><code>disable()</code> - Disables Shuffle from updating dimensions and layout on resize.</li>
<li><code>enable()</code> - Enables Shuffle again.</li>
<li><code>remove()</code> - Remove one or more shuffle items.</li>
<li><code>getItemByElement(element)</code> - Retrieve a <code>ShuffleItem</code> by its element.</li>
<li><code>destroy()</code> - Destroys Shuffle, removes events, styles, classes, and references.</li>
</ul>

@ -1,7 +1,10 @@
<h2>Sorting<a href="#sorting"></a></h2>
<p>You can order the elements based off a function you supply. In the demo above, each item has a <code>data-date-created</code> and <code>data-title</code> attribute. When the select option menu changes, a sort object is passed to shuffle.</p>
<p>You can order the elements with a function you supply. In the demo above, each item has a <code>data-date-created</code> and <code>data-title</code> attribute. When the select option menu changes, a sort object is passed to shuffle.</p>
<div class="code-block">
<pre rel="HTML"><code class="language-markup">&lt;figure class="picture-item" data-groups='[&quot;photography&quot;]' data-date-created="2010-09-14" data-title="Baseball"&gt;&hellip;&lt;/figure&gt;</code></pre>
</div>
<div class="code-block">
<pre rel="HTML"><code class="language-markup">&lt;select class="sort-options"&gt;
@ -12,36 +15,43 @@
</div>
<div class="code-block">
<pre rel="HTML"><code class="language-markup">&lt;figure class="picture-item" data-groups='[&quot;photography&quot;]' data-date-created="2010-09-14" data-title="Baseball"&gt;&hellip;&lt;/figure&gt;</code></pre>
</div>
<pre rel="JavaScript"><code class="language-javascript">Demo.prototype.addSorting = function () {
document.querySelector('.sort-options').addEventListener('change', this._handleSortChange.bind(this));
};
<div class="code-block">
<pre rel="JavaScript"><code class="language-javascript">// Sorting options
$('.sort-options').on('change', function() {
var sort = this.value,
opts = {};
// We're given the element wrapped in jQuery
if ( sort === 'date-created' ) {
opts = {
Demo.prototype._handleSortChange = function (evt) {
var value = evt.target.value;
var options = {};
function sortByDate(element) {
return element.getAttribute('data-created');
}
function sortByTitle(element) {
return element.getAttribute('data-title').toLowerCase();
}
if (value === 'date-created') {
options = {
reverse: true,
by: function($el) {
return $el.data('date-created');
}
by: sortByDate,
};
} else if ( sort === 'title' ) {
opts = {
by: function($el) {
return $el.data('title').toLowerCase();
}
} else if (value === 'title') {
options = {
by: sortByTitle,
};
}
// Filter elements
$grid.shuffle('sort', opts);
});</code></pre>
this.shuffle.sort(options);
};</code>
</div>
<p>The <code class="language-javascript">opts</code> parameter can contain two properties. <code class="language-javascript">reverse</code>, a boolean which will reverse the array. <code class="language-javascript">by</code> is a function that is passed the element wrapped in jQuery. In the case above, we&rsquo;re returning the value of the data-date-created or data-title attributes.</p>
<p>The <code class="language-javascript">options</code> object can contain three properties:</p>
<ul>
<li><code class="language-javascript">reverse</code>: a boolean which will reverse the resulting order.</li>
<li><code class="language-javascript">by</code>: a function with an element as the parameter. Above, we&rsquo;re returning the value of the <code class="language-markup">data-date-created</code> or <code class="language-markup">data-title</code> attribute.</li>
<li><code class="language-javascript">randomize</code>: Make the order random.</li>
</ul>
<p>Returning <code class="language-javascript">undefined</code> from the <code class="language-javascript">by</code> function will reset the order to DOM order.</p>
<p>Calling sort with an empty object will reset the elements to DOM order.</p>
<p class="demo-link-container">Check out the <a href="#demo">demo</a>.</p>

@ -120,11 +120,11 @@ prism: true
<div class="row">
<div class="col-12@sm">
<h3>Adding elements</h3>
<p>Wherever you add the element in the DOM is where it will show up in the grid (assuming you're using the default sort-by-dom-order). With this in mind, you can append, prepend, or insert elements wherever you need to get them to show up in the right order.</p>
<p>Wherever you add the element in the DOM is where it will show up in the grid (assuming you&rsquo;re using the default sort-by-dom-order). With this in mind, you can append, prepend, or insert elements wherever you need to get them to show up in the right order.</p>
<div class="code-block">
<pre rel="JavaScript"><code class="language-javascript">// Earlier&hellip;
document.querySelector('#append').addEventListener('click', this.onAppendBoxes.bind(this));
// &hellip;
<pre rel="JavaScript"><code class="language-javascript">Demo.prototype.setupEvents = function () {
document.querySelector('#append').addEventListener('click', this.onAppendBoxes.bind(this));
};
/**
* Create some DOM elements, append them to the shuffle container, then notify

@ -188,7 +188,7 @@ prism: true
<div class="row">
<div class="col-12@sm">
<h2>Dependencies<a href="#dependencies"></a></h2>
<p>Shuffle assumes <code>Promise</code> is available globally. If you care about IE11, use a <a href="https://github.com/stefanpenner/es6-promise">polyfill</a>. <a href="http://caniuse.com/#feat=promises">Current support</a>.</p>
<p>Shuffle assumes <code>Promise</code> is available globally. If you care about IE11, use a <a href="https://github.com/stefanpenner/es6-promise">polyfill</a>. <a href="http://caniuse.com/#feat=promises">Can I use Promises?</a></p>
<p>Shuffle's <a href="{{ site.baseurl }}/package.json">other dependencies</a> are bundled with the dist file.</p>
</div>
</div>

@ -99,7 +99,7 @@ Demo.prototype.onAppendBoxes = function () {
var items = this._getArrayOfElementsToAdd();
items.forEach(function (item) {
this.element.appendChild(item);
this.shuffle.element.appendChild(item);
}, this);
// Tell shuffle items have been appended.

@ -157,20 +157,29 @@ Demo.prototype.addSearchFilter = function () {
searchInput.addEventListener('keyup', this._handleSearchKeyup.bind(this));
};
/**
* Filter the shuffle instance by items with a title that matches the search input.
* @param {Event} evt Event object.
*/
Demo.prototype._handleSearchKeyup = function (evt) {
var searchText = evt.target.value.toLowerCase();
this.shuffle.filter(function (element, shuffle) {
// Get the item's groups.
var groups = JSON.parse(element.getAttribute('data-groups'));
// Only search elements in the current group
if (shuffle.options.group !== 'all' && groups.indexOf(shuffle.options.group) === -1) {
return false;
// If there is a current filter applied, ignore elements that don't match it.
if (shuffle.options.group !== Shuffle.ALL_ITEMS) {
// Get the item's groups.
var groups = JSON.parse(element.getAttribute('data-groups'));
var isElementInCurrentGroup = groups.indexOf(shuffle.options.group) !== -1;
// Only search elements in the current group
if (!isElementInCurrentGroup) {
return false;
}
}
var title = element.querySelector('.picture-item__title');
var titleText = title.textContent.toLowerCase().trim();
var titleElement = element.querySelector('.picture-item__title');
var titleText = titleElement.textContent.toLowerCase().trim();
return titleText.indexOf(searchText) !== -1;
});

Loading…
Cancel
Save