1/* 2 The MIT License (MIT) 3 4 Copyright (c) <2013> <Ren Aysha> 5 6 Permission is hereby granted, free of charge, to any person obtaining a copy 7 of this software and associated documentation files (the "Software"), to deal 8 in the Software without restriction, including without limitation the rights 9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 copies of the Software, and to permit persons to whom the Software is 11 furnished to do so, subject to the following conditions: 12 13 The above copyright notice and this permission notice shall be included in 14 all copies or substantial portions of the Software. 15 16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 THE SOFTWARE. 23*/ 24 25if ( typeof Object.create !== 'function' ) { 26 Object.create = function( obj ) { 27 function F() { 28 } 29 30 F.prototype = obj; 31 return new F(); 32 }; 33} 34 35(function( $, window, document, undefined ) { 36 "use strict"; 37 38 var Anchorific = { 39 40 init: function( options, elem ) { 41 var self = this; 42 43 self.elem = elem; 44 self.$elem = $( elem ); 45 46 self.opt = $.extend( {}, this.opt, options ); 47 48 self.headers = self.$elem.find( self.opt.headers ); 49 self.previous = 0; 50 51 // Fix bug #1 52 if ( self.headers.length !== 0 ) { 53 self.first = parseInt( self.headers.prop( 'nodeName' ).substring( 1 ), null ); 54 } 55 56 self.build(); 57 }, 58 59 opt: { 60 navigation: '.anchorific', // position of navigation 61 headers: 'h1, h2, h3, h4, h5, h6', // custom headers selector 62 speed: 200, // speed of sliding back to top 63 anchorClass: 'anchor', // class of anchor links 64 anchorText: '#', // prepended or appended to anchor headings 65 top: '.top', // back to top button or link class 66 spy: true, // scroll spy 67 position: 'append', // position of anchor text 68 spyOffset: ! 0 // specify heading offset for spy scrolling 69 }, 70 71 build: function() { 72 var self = this, obj, navigations = function() { 73 }; 74 // when navigation configuration is set 75 if ( self.opt.navigation ) { 76 $( self.opt.navigation ).append( '<ul />' ); 77 self.previous = $( self.opt.navigation ).find( 'ul' ).last(); 78 navigations = function( obj ) { 79 return self.navigations( obj ); 80 }; 81 } 82 83 for ( var i = 0; i < self.headers.length; i ++ ) { 84 obj = self.headers.eq( i ); 85 navigations( obj ); 86 self.anchor( obj ); 87 } 88 89 if ( self.opt.spy ) { 90 self.spy(); 91 } 92 93 if ( self.opt.top ) { 94 self.back(); 95 } 96 }, 97 98 navigations: function( obj ) { 99 var self = this, link, list, which, name = self.name( obj ); 100 101 if ( obj.attr( 'id' ) !== undefined ) { 102 name = obj.attr( 'id' ); 103 } 104 105 link = $( '<a />' ).attr( 'href', '#' + name ).text( obj.text() ); 106 list = $( '<li />' ).append( link ); 107 108 which = parseInt( obj.prop( 'nodeName' ).substring( 1 ), null ); 109 list.attr( 'data-tag', which ); 110 111 self.subheadings( which, list ); 112 113 self.first = which; 114 }, 115 116 subheadings: function( which, a ) { 117 var self = this, ul = $( self.opt.navigation ).find( 'ul' ), 118 li = $( self.opt.navigation ).find( 'li' ); 119 120 if ( which === self.first ) { 121 self.previous.append( a ); 122 } else if ( which > self.first ) { 123 li.last().append( '<ul />' ); 124 // can't use cache ul; need to find ul once more 125 $( self.opt.navigation ).find( 'ul' ).last().append( a ); 126 self.previous = a.parent(); 127 } else { 128 $( 'li[data-tag=' + which + ']' ).last().parent().append( a ); 129 self.previous = a.parent(); 130 } 131 }, 132 133 name: function( obj ) { 134 var name = obj.text().replace( /[^\w\s]/gi, '' ) 135 .replace( /\s+/g, '-' ) 136 .toLowerCase(); 137 138 return name; 139 }, 140 141 anchor: function( obj ) { 142 var self = this, name = self.name( obj ), anchor, text = self.opt.anchorText, 143 klass = self.opt.anchorClass, id; 144 145 if ( obj.attr( 'id' ) === undefined ) { 146 obj.attr( 'id', name ); 147 } 148 149 id = obj.attr( 'id' ); 150 151 anchor = $( '<a />' ).attr( 'href', '#' + id ).html( text ).addClass( klass ); 152 153 if ( self.opt.position === 'append' ) { 154 obj.append( anchor ); 155 } else { 156 obj.prepend( anchor ); 157 } 158 }, 159 160 back: function() { 161 var self = this, body = $( 'body, html' ), top = $( self.opt.top ); 162 163 top.on( 'click', function( e ) { 164 e.preventDefault(); 165 166 body.animate( { 167 'scrollTop': 0 168 }, self.opt.speed ); 169 } ); 170 }, 171 172 top: function( that ) { 173 var self = this, top = self.opt.top, back; 174 175 if ( top !== false ) { 176 back = ( $( that ).scrollTop() > 200 ) ? 177 $( top ).fadeIn() : 178 $( top ).fadeOut(); 179 } 180 }, 181 182 spy: function() { 183 var self = this, previous, current, list, top, prev; 184 185 $( window ).scroll( function( e ) { 186 // show links back to top 187 self.top( this ); 188 // get all the header on top of the viewport 189 current = self.headers.map( function( e ) { 190 if ( ( $( this ).offset().top - $( window ).scrollTop() ) < self.opt.spyOffset ) { 191 return this; 192 } 193 } ); 194 // get only the latest header on the viewport 195 current = $( current ).eq( current.length - 1 ); 196 197 if ( current && current.length ) { 198 // get all li tag that contains href of # ( all the parents ) 199 list = $( 'li:has(a[href="#' + current.attr( 'id' ) + '"])' ); 200 201 if ( prev !== undefined ) { 202 prev.removeClass( 'active' ); 203 } 204 205 list.addClass( 'active' ); 206 prev = list; 207 } 208 } ); 209 } 210 }; 211 212 $.fn.anchorific = function( options ) { 213 return this.each( function() { 214 if ( ! $.data( this, 'anchorific' ) ) { 215 var anchor = Object.create( Anchorific ); 216 217 anchor.init( options, this ); 218 219 $.data( this, 'anchorific', anchor ); 220 } 221 } ); 222 }; 223 224})( jQuery, window, document );
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.