diff --git a/_includes/adding-removing.html b/_includes/adding-removing.html index 2dcb768..a764c8e 100644 --- a/_includes/adding-removing.html +++ b/_includes/adding-removing.html @@ -1,21 +1,30 @@
You can add and remove elements from shuffle after it has been created. This also works for infinite scrolling.
-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.
// Adds 2 <div>s to an existing Shuffle instance.
-var $item1 = $('<div class="gallery-item item1">')
-var $item2 = $('<div class="gallery-item item2">')
-var $items = $item1.add($item2);
-$('#my-shuffle').append($items);
-$('#my-shuffle').shuffle('appended', $items);
+ /**
+ * 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);
+};
Shuffle will animate the element away and then remove it from the DOM once it's finished. It will then emit the Shuffle.EventType.REMOVED
custom event with the array of elements in event.detail.collection
.
// Remove the 2 <div>s which were just added.
-var $items = $('.gallery-item');
-$('#my-shuffle').shuffle('remove', $items);
+ this.shuffle.remove([element1, element2]);
Check out the demo.
+Check out the adding and removing demo.
diff --git a/_includes/advanced-filters.html b/_includes/advanced-filters.html index 13a999d..c4bf1e2 100644 --- a/_includes/advanced-filters.html +++ b/_includes/advanced-filters.html @@ -1,31 +1,33 @@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 true
to keep the element or false
to hide it.
By passing a function to filter
, you can fully customize filtering items. Shuffle will iterate over each item and give your function the element and the shuffle instance. Return true
to keep the element or false
to hide it.
// Filters elements with a data-title attribute with less than 10 characters
-$('#grid').shuffle('shuffle', function($el, shuffle) {
- return $el.data('title').length < 10;
+instance.filter(function (element) {
+ return element.getAttribute('data-title').length < 10;
});
// 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' && $.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;
});
-});
+};
Check out the compounded filters demo.
diff --git a/_includes/public-methods.html b/_includes/public-methods.html index beed951..8eb7ab1 100644 --- a/_includes/public-methods.html +++ b/_includes/public-methods.html @@ -1,14 +1,15 @@A list of the methods available to you and what they do.
-shuffle
- Filters all the shuffle items.sort
- Sorts the currently filtered shuffle items.update
- Repositions everything. Useful for when dimensions (like the window size) change.layout
- Use this instead of update()
if you don't need the columns and gutters updated. Maybe an image loaded and now has a height.appended
- New items have been appended to the shuffle container.disable
- Disables Shuffle from updating dimensions and layout on resize.enable
- Enables Shuffle again.remove
- Remove one or more shuffle items.destroy
- Destroys Shuffle, removes events, styles, classes, and references.filter(category, sortObject)
- Filters all the shuffle items and then sorts them. category
can be a string, array of strings, or a function. The sort object is optional and will use the last-used sort object.sort(sortObject)
- Sorts the currently filtered shuffle items.update()
- Repositions everything. Useful for when dimensions (like the window size) change.layout()
- Use this instead of update()
if you don't need the columns and gutters updated. Maybe an image loaded and now has a height.add(newItems)
- New items have been appended to the shuffle container. newItems
is an array of elements.disable()
- Disables Shuffle from updating dimensions and layout on resize.enable()
- Enables Shuffle again.remove()
- Remove one or more shuffle items.getItemByElement(element)
- Retrieve a ShuffleItem
by its element.destroy()
- Destroys Shuffle, removes events, styles, classes, and references.You can order the elements based off a function you supply. In the demo above, each item has a data-date-created
and data-title
attribute. When the select option menu changes, a sort object is passed to shuffle.
You can order the elements with a function you supply. In the demo above, each item has a data-date-created
and data-title
attribute. When the select option menu changes, a sort object is passed to shuffle.
<figure class="picture-item" data-groups='["photography"]' data-date-created="2010-09-14" data-title="Baseball">…</figure>
+<select class="sort-options">
@@ -12,36 +15,43 @@
<figure class="picture-item" data-groups='["photography"]' data-date-created="2010-09-14" data-title="Baseball">…</figure>
-Demo.prototype.addSorting = function () {
+ document.querySelector('.sort-options').addEventListener('change', this._handleSortChange.bind(this));
+};
-
- // 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);
-});
+ this.shuffle.sort(options);
+};
-The opts
parameter can contain two properties. reverse
, a boolean which will reverse the array. by
is a function that is passed the element wrapped in jQuery. In the case above, we’re returning the value of the data-date-created or data-title attributes.
+The options
object can contain three properties:
+
+ reverse
: a boolean which will reverse the resulting order.
+ by
: a function with an element as the parameter. Above, we’re returning the value of the data-date-created
or data-title
attribute.
+ randomize
: Make the order random.
+
+Returning undefined
from the by
function will reset the order to DOM order.
Calling sort with an empty object will reset the elements to DOM order.
Check out the demo.
diff --git a/_posts/2013-06-19-adding-removing.html b/_posts/2013-06-19-adding-removing.html
index c5d1fb9..eb79dfb 100644
--- a/_posts/2013-06-19-adding-removing.html
+++ b/_posts/2013-06-19-adding-removing.html
@@ -120,11 +120,11 @@ prism: true
Adding elements
- 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.
+ 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.
- // Earlier…
-document.querySelector('#append').addEventListener('click', this.onAppendBoxes.bind(this));
-// …
+ 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
diff --git a/index.html b/index.html
index ef602cb..299e4a3 100644
--- a/index.html
+++ b/index.html
@@ -188,7 +188,7 @@ prism: true
Dependencies
- Shuffle assumes Promise
is available globally. If you care about IE11, use a polyfill. Current support.
+ Shuffle assumes Promise
is available globally. If you care about IE11, use a polyfill. Can I use Promises?
Shuffle's other dependencies are bundled with the dist file.
diff --git a/js/demos/adding-removing.js b/js/demos/adding-removing.js
index 43b15a8..2cd673a 100644
--- a/js/demos/adding-removing.js
+++ b/js/demos/adding-removing.js
@@ -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.
diff --git a/js/demos/homepage.js b/js/demos/homepage.js
index 60ae901..1a33f74 100644
--- a/js/demos/homepage.js
+++ b/js/demos/homepage.js
@@ -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;
});