Updated home page examples, added init with sort

Commented all options
pull/56/head
Glen Cheney 11 years ago
parent 615691c8ea
commit 4d38115f4d

@ -4,7 +4,7 @@
<h3>Example</h3>
<pre rel="JavaScript"><code class="language-javascript">// Filters elements with a data-title attribute with less than 10 characters
$('#grid').shuffle(function($el, shuffle) {
$('#grid').shuffle('shuffle', function($el, shuffle) {
return $el.data('title').length &lt; 10;
});</code></pre>
@ -12,15 +12,16 @@ $('#grid').shuffle(function($el, shuffle) {
<pre rel="JavaScript"><code class="language-javascript">// Advanced filtering
$('.js-shuffle-search').on('keyup change', function() {
var val = this.value.toLowerCase();
$grid.shuffle(function($el, shuffle) {
$grid.shuffle('shuffle', function($el, shuffle) {
// Only search elements in the current group
if (shuffle.group !== 'all' &amp;&amp; $.inArray(shuffle.group, $el.data('groups')) === -1) {
return false;
}
// Only search elements in the current group
if (shuffle.group !== 'all' &amp;&amp; $.inArray(shuffle.group, $el.data('groups')) === -1) {
return false;
}
var text = $.trim( $el.find('.picture-item__title').text() ).toLowerCase();
return text.indexOf(val) != -1;
var text = $.trim( $el.find('.picture-item__title').text() ).toLowerCase();
return text.indexOf(val) !== -1;
});
});</code></pre>
<p>For another example of advanced filters, check out the <a href="responsive.html">compounded filters demo</a>.</p>
<p>For another example of advanced filters, check out the <a href="{% post_url demos/2013-05-02-adaptive %}">compounded filters demo</a>.</p>

@ -1,2 +1,23 @@
<h2>Events</h2>
<p>These events will be triggered at their respective times: <code>'shrink.shuffle'</code>, <code>'shrunk.shuffle'</code>, <code>'filter.shuffle'</code>, <code>'filtered.shuffle'</code>, and <code>'sorted.shuffle'</code>.</p>
<p>A list of events shuffle triggers:</p>
<ul>
<li><code class="language-javascript">'loading.shuffle'</code></li>
<li><code class="language-javascript">'done.shuffle'</code></li>
<li><code class="language-javascript">'shrink.shuffle'</code></li>
<li><code class="language-javascript">'shrunk.shuffle'</code></li>
<li><code class="language-javascript">'filter.shuffle'</code></li>
<li><code class="language-javascript">'filtered.shuffle'</code></li>
<li><code class="language-javascript">'layout.shuffle'</code></li>
<li><code class="language-javascript">'removed.shuffle'</code></li>
</ul>
<h3>Get notified when shuffle is done with setup</h3>
<pre rel="JavaScript"><code class="language-javascript">$grid.on('done.shuffle', function() {
console.log('Finished initializing shuffle!');
});</code></pre>
<h3>Do something when an item is removed</h3>
<pre rel="JavaScript"><code class="language-javascript">$grid.on('removed.shuffle', function( evt, $collection, shuffle ) {
console.log( this, evt, $collection, shuffle );
});</code></pre>

@ -31,6 +31,7 @@
<!-- Prefetch DNS for external assets -->
<link rel="dns-prefetch" href="//fonts.googleapis.com">
<link rel="dns-prefetch" href="//themes.googleusercontent.com">
<link rel="dns-prefetch" href="//www.google-analytics.com">
<link rel="dns-prefetch" href="//ajax.googleapis.com">

@ -2,13 +2,21 @@
<p>Settings you can change (these are the defaults)</p>
<pre rel="JavaScript"><code class="language-javascript">// Overrideable options
$.fn.shuffle.options = {
group : 'all', // Which category to show
speed : 600, // Speed of the transition (in milliseconds). 600 = .6 seconds
easing : 'ease-out', // css easing function to use
gutterWidth : 0, // space between columns. Can be a function or number
columnWidth : 0 // can be a function or number or calculated by plugin
};
</code></pre>
Shuffle.options = {
group: 'all', // Filter group
speed: 250, // Transition/animation speed (milliseconds)
easing: 'ease-out', // css easing function to use
itemSelector: '', // e.g. '.picture-item'
sizer: null, // sizer element. Can be anything columnWidth is
gutterWidth: 0, // a static number or function that tells the plugin how wide the gutters between columns are (in pixels)
columnWidth: 0, // a static number or function that returns a number which tells the plugin how wide the columns are (in pixels)
delimeter: null, // if your group is not json, and is comma delimeted, you could set delimeter to ','
buffer: 0, // useful for percentage based heights when they might not always be exactly the same (in pixels)
initialSort: null, // Shuffle can be initialized with a sort object. It is the same object given to the sort method
throttle: $.throttle || null, // By default, shuffle will try to throttle the resize event. This option will change the method it uses
throttleTime: 300, // How often shuffle can be called on resize (in milliseconds)
sequentialFadeDelay: 150, // Delay between each item that fades in when adding items
supported: Modernizr.csstransforms &amp;&amp; Modernizr.csstransitions // supports transitions and transforms
};</code></pre>
<p>The easing function is one of <code>'default'</code>, <code>'linear'</code>, <code>'ease-in'</code>, <code>'ease-out'</code>, <code>'ease-in-out'</code>, or <code>'cubic-bezier'</code>.</p>
<p>No options <em>need</em> to be specified, but <code>itemSelector</code> should be used. Other common options to change are <code>speed</code>, <code>easing</code>, <code>gutterWidth</code>, and <code>columnWidth</code> (or <code>sizer</code>).</p>

@ -1,29 +1,30 @@
<h2>Sorting</h2>
<p>You can order the elements based off a function you supply. In the example above, each item has a <code>data-date-created</code> and <code>data-title</code> attribute. The filter buttons have a <code>data-sort</code> attribute with the value of the item&rsquo;s attribute. Then, with some JavaScript, we can get the correct attribute and provide a function to sort by.</p>
<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>
<pre rel="HTML"><code class="language-markup">&lt;li data-sort="title"&gt;Title&lt;/li&gt;</code></pre>
<pre rel="HTML"><code class="language-markup">&lt;div class="item" data-title="Baseball"&gt;&hellip;&lt;/div&gt;</code></pre>
<pre rel="HTML"><code class="language-markup">&lt;select class="sort-options"&gt;
&lt;option value=""&gt;Default&lt;/option&gt;
&lt;option value="title"&gt;Title&lt;/option&gt;
&lt;option value="date-created"&gt;Date Created&lt;/option&gt;
&lt;/select&gt;</code></pre>
<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>
<pre rel="JavaScript"><code class="language-javascript">// Sorting options
$('.sort-options button').on('click', function() {
var $this = $(this),
sort = $this.data('sort'),
$('.sort-options').on('change', function() {
var sort = this.value,
opts = {};
// Hide current label, show current label in title
$('.sort-options .active').removeClass('active');
$this.addClass('active');
// We're given the element wrapped in jQuery
if (sort === 'date-created') {
if ( sort === 'date-created' ) {
opts = {
reverse: true,
by: function($el) {
return $el.data('date-created');
}
}
} else if (sort === 'title') {
};
} else if ( sort === 'title' ) {
opts = {
by: function($el) {
return $el.data('title').toLowerCase();

@ -39,26 +39,22 @@
&lt;/div&gt;</code></pre>
<h3 id="columns">How column widths work</h3>
<p>The <code>columnWidth</code> option is used to calculate the column width. This option <strong>can be a function</strong> for responsive layouts. If it's not a function, it uses jQuery's <code class="language-javascript">outerWidth(true)</code> to calculate the column width (this includes margins). If the <code>columnWidth</code> option isn't specified, the width will be the width of the first item.</p>
<p>The <code>columnWidth</code> option is used to calculate the column width. You have several options:</p>
<ol>
<li>Use a <strong>sizer</strong> element. This is the easest way to specify column and gutter widths. You can use an element or an element wrapped in jQuery. The column and gutter widths will be measured using this element. The <code>sizer</code> option is an alias for <code>columnWidth</code>. See <a href="{% post_url demos/2013-05-01-basic %}">a demo</a> using a sizer element or <a href="{{ site.url }}/js/demos/homepage.js">look at js file</a> for the sizer demo</a>.</li>
<li>Use a <strong>function</strong>. When a function is used, its first parameter will be the width of the shuffle element. You need to return the column width for shuffle to use (in pixels).</li>
<li>A <strong>number</strong>. This will explicitly set the column width to your number (in pixels).</li>
<li>By default, shuffle will use jQuery's <code class="language-javascript">outerWidth(true)</code> to calculate the column width of the first item and use that value.</li>
</ol>
<h3>A basic setup example</h3>
<p>Sets up the button clicks and runs shuffle</p>
<pre rel="jQuery"><code class="language-javascript">
$(document).ready(function(){
// Set up button clicks
$('.filter-options li').on('click', function() {
var $this = $(this),
$grid = $('#grid');
// Hide current label, show current label in title
$('.filter-options .active').removeClass('active');
$this.addClass('active');
// Filter elements
$grid.shuffle($this.data('group'));
});
// instantiate the plugin
$('#grid').shuffle();
<pre rel="JavaScript"><code class="language-javascript">$(document).ready(function() {
var $grid = $('#grid'),
$sizer = $grid.find('.shuffle__sizer');
$grid.shuffle({
itemSelector: '.picture-item',
sizer: $sizer
});
});</code></pre>

@ -40,7 +40,7 @@ prism: true
</div>
</div>
<div class="span3">
<div class="pusll-right">
<div class="pull-right">
<p class="filter__label">Sort:</p>
<select class="sort-options">
<option value>Default</option>

@ -13,7 +13,7 @@ var DEMO = (function( $ ) {
// instantiate the plugin
$grid.shuffle({
itemSelector: '.picture-item',
columnWidth: $sizer
sizer: $sizer
});
// Destroy it! o_O
@ -57,13 +57,14 @@ var DEMO = (function( $ ) {
opts = {};
// We're given the element wrapped in jQuery
if (sort === 'date-created') {
if ( sort === 'date-created' ) {
opts = {
reverse: true,
by: function($el) {
return $el.data('date-created');
}
};
} else if (sort === 'title') {
} else if ( sort === 'title' ) {
opts = {
by: function($el) {
return $el.data('title').toLowerCase();

@ -144,7 +144,8 @@ Shuffle.prototype = {
var self = this,
containerCSS,
resizeFunction = $.proxy( self._onResize, self ),
debouncedResize = self.throttle ? self.throttle( self.throttleTime, resizeFunction ) : resizeFunction;
debouncedResize = self.throttle ? self.throttle( self.throttleTime, resizeFunction ) : resizeFunction,
sort = self.initialSort ? self.initialSort : null;
// Save variables needed later then add some classes
self._setVars();
@ -161,7 +162,7 @@ Shuffle.prototype = {
// Bind resize events (http://stackoverflow.com/questions/1852751/window-resize-event-firing-in-internet-explorer)
self.$window.on('resize.shuffle.' + self.unique, debouncedResize);
// Get container css all in one request
// Get container css all in one request. Causes reflow
containerCSS = self.$container.css(['paddingLeft', 'paddingRight', 'position', 'width']);
// Position cannot be static.
@ -181,7 +182,7 @@ Shuffle.prototype = {
self._setColumns( parseInt( containerCSS.width, 10 ) );
// Kick off!
self.shuffle( self.group );
self.shuffle( self.group, sort );
// The shuffle items haven't had transitions set on them yet
// so the user doesn't see the first layout. Set them now that the first layout is done.
@ -438,13 +439,6 @@ Shuffle.prototype = {
self.cols = Math.floor( calculatedColumns );
self.cols = Math.max( self.cols, 1 );
// This can happen when .shuffle is called on something hidden (e.g. display:none for tabs)
if ( !self.colWidth || isNaN( self.cols ) || !containerWidth ) {
self.needsUpdate = true;
} else {
self.needsUpdate = false;
}
self.containerWidth = containerWidth;
},
@ -579,11 +573,11 @@ Shuffle.prototype = {
scale: 1
};
if ( !isOnlyPosition ) {
if ( isOnlyPosition ) {
transitionObj.skipTransition = true;
} else {
transitionObj.opacity = 1;
transitionObj.callback = callback;
} else {
transitionObj.skipTransition = true;
}
if ( isHide ) {
@ -603,7 +597,7 @@ Shuffle.prototype = {
callback = fn || self.shrinkEnd;
// Abort if no items
if ($concealed.length === 0) {
if ( !$concealed.length ) {
return;
}
@ -851,6 +845,7 @@ Shuffle.prototype = {
}, self.revealAppendedDelay);
},
/**
* Public Methods
*/
@ -1015,17 +1010,30 @@ Shuffle.prototype = {
return self;
},
/**
* Destroys shuffle, removes events, styles, and classes
*/
destroy: function() {
var self = this;
// If there is more than one shuffle instance on the page,
// removing the resize handler from the window would remove them
// all. This is why a unique value is needed.
self.$window.off('.' + self.unique);
// Reset container styles
self.$container
.removeClass('shuffle')
.removeAttr('style')
.removeData('shuffle');
// Reset individual item styles
self.$items
.removeAttr('style')
.removeClass('concealed filtered shuffle-item');
// Set a flag so if a debounced resize has been triggered,
// it can first check if it is actually destroyed and not doing anything
self.destroyed = true;
}
@ -1033,19 +1041,19 @@ Shuffle.prototype = {
// Overrideable options
Shuffle.options = {
group : 'all', // Filter group
speed : 250, // Transition/animation speed (milliseconds)
easing : 'ease-out', // css easing function to use
itemSelector: '', // e.g. '.gallery-item'
group: 'all', // Filter group
speed: 250, // Transition/animation speed (milliseconds)
easing: 'ease-out', // css easing function to use
itemSelector: '', // e.g. '.picture-item'
sizer: null, // sizer element. Can be anything columnWidth is
gutterWidth : 0, // a static number or function that tells the plugin how wide the gutters between columns are (in pixels)
columnWidth : 0,// a static number or function that returns a number which tells the plugin how wide the columns are (in pixels)
showInitialTransition : false, // If set to false, the shuffle-items will only have a transition applied to them after the first layout
delimeter : null, // if your group is not json, and is comma delimeted, you could set delimeter to ','
gutterWidth: 0, // a static number or function that tells the plugin how wide the gutters between columns are (in pixels)
columnWidth: 0, // a static number or function that returns a number which tells the plugin how wide the columns are (in pixels)
delimeter: null, // if your group is not json, and is comma delimeted, you could set delimeter to ','
buffer: 0, // useful for percentage based heights when they might not always be exactly the same (in pixels)
throttle: $.throttle || null,
throttleTime: 300,
sequentialFadeDelay: 150,
initialSort: null, // Shuffle can be initialized with a sort object. It is the same object given to the sort method
throttle: $.throttle || null, // By default, shuffle will try to throttle the resize event. This option will change the method it uses
throttleTime: 300, // How often shuffle can be called on resize (in milliseconds)
sequentialFadeDelay: 150, // Delay between each item that fades in when adding items
supported: Modernizr.csstransforms && Modernizr.csstransitions // supports transitions and transforms
};
@ -1072,13 +1080,13 @@ Shuffle.settings = {
$.fn.shuffle = function( opts ) {
var args = Array.prototype.slice.call( arguments, 1 );
return this.each(function() {
var $this = $(this),
shuffle = $this.data('shuffle');
var $this = $( this ),
shuffle = $this.data( 'shuffle' );
// If we don't have a stored shuffle, make a new one and save it
if ( !shuffle ) {
shuffle = new Shuffle($this, opts);
$this.data('shuffle', shuffle);
shuffle = new Shuffle( $this, opts );
$this.data( 'shuffle', shuffle );
}
if ( typeof opts === 'string' && shuffle[ opts ] ) {

@ -2,13 +2,8 @@
This is a large improvement to shuffle. Most notably, the ability for [masonry](http://masonry.desandro.com) layouts. Other additions include adding/removing items, enabling/disabling, multiple instances on a page, and more!
## Todo before launch
* Update index code examples
* Solve margin top issue
* init with a sort
* Consistent naming of public vs private functions
## Other improvements
## Improvements
* Use Deferred objects for callbacks
* Custom animations
* Horizontal layout

Loading…
Cancel
Save