Browse Source

demos : hash history : commented, jsHinted

pull/96/head
David DeSandro 14 years ago
parent
commit
6cb338e3dd
  1. 31
      _posts/demos/2011-06-13-hash-history.html

31
_posts/demos/2011-06-13-hash-history.html

@ -69,13 +69,14 @@ $(function(){
var $container = $('#container'), var $container = $('#container'),
// object that will keep track of options // object that will keep track of options
isotopeOptions = {},
// defaults, used if not explicitly set in hash
defaultOptions = { defaultOptions = {
filter: '*', filter: '*',
sortBy: 'original-order', sortBy: 'original-order',
sortAscending: true, sortAscending: true,
layoutMode: 'masonry' layoutMode: 'masonry'
}, };
isotopeOptions = {};
// hacky way of adding random size classes // hacky way of adding random size classes
@ -88,8 +89,7 @@ $(function(){
} }
}); });
// set up Isotope var setupOptions = $.extend( defaultOptions, {
$container.isotope({
itemSelector : '.element', itemSelector : '.element',
masonry : { masonry : {
columnWidth : 120 columnWidth : 120
@ -117,8 +117,10 @@ $(function(){
} }
}); });
// set up Isotope
$container.isotope( setupOptions );
var $optionSets = $('#options').find('.option-set'), var $optionSets = $('#options').find('.option-set'),
$optionLinks = $optionSets.find('a'),
isOptionLinkClicked = false; isOptionLinkClicked = false;
// switches selected class on buttons // switches selected class on buttons
@ -130,27 +132,23 @@ $(function(){
} }
$optionLinks.click(function(){ $optionSets.find('a').click(function(){
var $this = $(this); var $this = $(this);
// don't proceed if already selected // don't proceed if already selected
if ( $this.hasClass('selected') ) { if ( $this.hasClass('selected') ) {
return; return;
} }
changeSelectedLink( $this ); changeSelectedLink( $this );
// get href attr, remove leading # // get href attr, remove leading #
var href = $this.attr('href').replace( /^#/, '' ), var href = $this.attr('href').replace( /^#/, '' ),
// convert href into object // convert href into object
// i.e. 'filter=inner-transition' -> { filter: 'inner-transition' } // i.e. 'filter=inner-transition' -> { filter: 'inner-transition' }
option = $.deparam( href, true ); option = $.deparam( href, true );
// apply new option to previous
$.extend( isotopeOptions, option ); $.extend( isotopeOptions, option );
// set hash, triggers hashchange on window // set hash, triggers hashchange on window
$.bbq.pushState( isotopeOptions ); $.bbq.pushState( isotopeOptions );
isOptionLinkClicked = true; isOptionLinkClicked = true;
return false; return false;
}); });
@ -158,13 +156,12 @@ $(function(){
$(window).bind( 'hashchange', function( event ){ $(window).bind( 'hashchange', function( event ){
// get options object from hash // get options object from hash
var hashOptions = window.location.hash ? $.deparam.fragment( window.location.hash, true ) : {}, var hashOptions = window.location.hash ? $.deparam.fragment( window.location.hash, true ) : {},
options = {}; // apply defaults where no option was specified
options = $.extend( {}, defaultOptions, hashOptions );
$.extend( options, defaultOptions, hashOptions );
isotopeOptions = hashOptions;
// apply options from hash // apply options from hash
$container.isotope( options ); $container.isotope( options );
// save options
isotopeOptions = hashOptions;
// if option link was not clicked // if option link was not clicked
// then we'll need to update selected links // then we'll need to update selected links
@ -176,7 +173,7 @@ $(function(){
hrefObj[ key ] = options[ key ]; hrefObj[ key ] = options[ key ];
// convert object into parameter string // convert object into parameter string
// i.e. { filter: 'inner-transition' } -> 'filter=inner-transition' // i.e. { filter: 'inner-transition' } -> 'filter=inner-transition'
hrefValue = $.param( hrefObj ), hrefValue = $.param( hrefObj );
// get matching link // get matching link
$selectedLink = $optionSets.find('a[href="#' + hrefValue + '"]'); $selectedLink = $optionSets.find('a[href="#' + hrefValue + '"]');
changeSelectedLink( $selectedLink ); changeSelectedLink( $selectedLink );

Loading…
Cancel
Save