// 2.3.1 (function($) { // Animate scrolling to section location var scroll_to = function(value, speed, offset, animated) { if ( $(value).length > 0 ) { var destination = $(value).offset().top; speed = animated ? speed : 0; $('html:not(:animated),body:not(:animated)') .animate({scrollTop: destination - offset }, speed ); } }; // Get url hash if one exists var get_hash = function() { return window.location.hash; }; var S = { classes: { loading: 'sn-loading', failed: 'sn-failed', success: 'sn-active' }, defaults: { sections: 'h2', subSections: false, sectionElem: 'section', className: 'scroll-nav', showHeadline: true, headlineText: 'Scroll To', showTopLink: true, topLinkText: 'Top', fixedMargin: 40, scrollOffset: 40, animated: true, speed: 500, insertLocation: 'insertBefore', arrowKeys: false, onInit: null, onRender: null, onDestroy: null }, _set_body_class: function(state) { // Set and swap our loading hooks to the body var $body = $('body'); if (state === 'loading') { $body.addClass(S.classes.loading); } else if (state === 'success') { $body.removeClass(S.classes.loading).addClass(S.classes.success); } else { $body.removeClass(S.classes.loading).addClass(S.classes.failed); } }, _find_sections: function($el) { // Find the html for each section var target_elems = S.settings.sections; var raw_html = []; if (S.settings.showTopLink) { var $firstElem = $el.children().first(); if ( !$firstElem.is(target_elems) ) { raw_html.push( $firstElem.nextUntil(target_elems).andSelf() ); } } $el.find(target_elems).each(function() { raw_html.push( $(this).nextUntil(target_elems).andSelf() ); }); S.sections = { raw: raw_html }; }, _setup_sections: function(sections) { // Wrap each section and add it's details to the section array var section_data = []; $(sections).each(function(i) { var sub_data = []; var $this_section = $(this); var section_id = 'scrollNav-' + (i + 1); var isFirst = function() { return i === 0; }; var hasHeading = function() { return !$this_section.eq(0).is(S.settings.sections); }; var text = ( S.settings.showTopLink && isFirst() && hasHeading() ) ? S.settings.topLinkText : $this_section.filter(S.settings.sections).text(); $this_section.wrapAll('<' + S.settings.sectionElem + ' id="' + section_id + '" class="' + S.settings.className + '__section" />'); if (S.settings.subSections) { var $sub_sections = $this_section.filter(S.settings.subSections); if ($sub_sections.length > 0) { $sub_sections.each(function(i) { var sub_id = section_id + '-' + (i + 1); var sub_text = $(this).text(); var $this_sub = $this_section.filter($(this).nextUntil($sub_sections).andSelf()); $this_sub.wrapAll('
'); sub_data.push( {id: sub_id, text: sub_text} ); }); } } section_data.push( {id: section_id, text: text, sub_sections: sub_data} ); }); S.sections.data = section_data; }, _tear_down_sections: function(sections) { $(sections).each(function() { var sub_sections = this.sub_sections; $('#' + this.id).children().unwrap(); if (sub_sections.length > 0) { $(sub_sections).each(function() { $('#' + this.id).children().unwrap(); }); } }); }, _setup_nav: function(sections) { // Populate an ordered list from the section array we built var $headline = $('', {'class': S.settings.className + '__heading', text: S.settings.headlineText}); var $wrapper = $('
', {'class': S.settings.className + '__wrapper'}); var $nav = $('