anchorific
Advanced tools
Comparing version 0.1.2 to 0.1.3
@@ -1,2 +0,1 @@ | ||
/*! anchorific 2019-08-17 */ | ||
"function"!=typeof Object.create&&(Object.create=function(a){function b(){}return b.prototype=a,new b}),function(a,b,c,d){"use strict";var e={init:function(b,c){var d=this;d.elem=c,d.$elem=a(c),d.opt=a.extend({},this.opt,b),d.headers=d.$elem.find(d.opt.headers),d.previous=0,0!==d.headers.length&&(d.first=parseInt(d.headers.prop("nodeName").substring(1),null)),d.build()},opt:{navigation:".anchorific",headers:"h1, h2, h3, h4, h5, h6",speed:200,anchorClass:"anchor",anchorText:"#",top:".top",spy:!0,position:"append",spyOffset:!0},build:function(){var b,c=this,d=function(){};c.opt.navigation&&(a(c.opt.navigation).append("<ul />"),c.previous=a(c.opt.navigation).find("ul").last(),d=function(a){return c.navigations(a)});for(var e=0;e<c.headers.length;e++)b=c.headers.eq(e),d(b),c.anchor(b);c.opt.spy&&c.spy(),c.opt.top&&c.back()},navigations:function(b){var c,e,f,g=this,h=g.name(b);b.attr("id")!==d&&(h=b.attr("id")),c=a("<a />").attr("href","#"+h).text(b.text()),e=a("<li />").append(c),f=parseInt(b.prop("nodeName").substring(1),null),e.attr("data-tag",f),g.subheadings(f,e),g.first=f},subheadings:function(b,c){var d=this,e=(a(d.opt.navigation).find("ul"),a(d.opt.navigation).find("li"));b===d.first?d.previous.append(c):b>d.first?(e.last().append("<ul />"),a(d.opt.navigation).find("ul").last().append(c),d.previous=c.parent()):(a("li[data-tag="+b+"]").last().parent().append(c),d.previous=c.parent())},name:function(a){var b=a.text().replace(/[^\w\s]/gi,"").replace(/\s+/g,"-").toLowerCase();return b},anchor:function(b){var c,e,f=this,g=f.name(b),h=f.opt.anchorText,i=f.opt.anchorClass;b.attr("id")===d&&b.attr("id",g),e=b.attr("id"),c=a("<a />").attr("href","#"+e).html(h).addClass(i),"append"===f.opt.position?b.append(c):b.prepend(c)},back:function(){var b=this,c=a("body, html"),d=a(b.opt.top);d.on("click",function(a){a.preventDefault(),c.animate({scrollTop:0},b.opt.speed)})},top:function(b){var c,d=this,e=d.opt.top;e!==!1&&(c=a(b).scrollTop()>200?a(e).fadeIn():a(e).fadeOut())},spy:function(){var c,e,f,g=this;a(b).scroll(function(h){g.top(this),c=g.headers.map(function(c){return a(this).offset().top-a(b).scrollTop()<g.opt.spyOffset?this:void 0}),c=a(c).eq(c.length-1),c&&c.length&&(e=a('li:has(a[href="#'+c.attr("id")+'"])'),f!==d&&f.removeClass("active"),e.addClass("active"),f=e)})}};a.fn.anchorific=function(b){return this.each(function(){if(!a.data(this,"anchorific")){var c=Object.create(e);c.init(b,this),a.data(this,"anchorific",c)}})}}(jQuery,window,document); | ||
if(typeof Object.create!=="function"){Object.create=function(t){function n(){}n.prototype=t;return new n}}(function(s,r,t,p){"use strict";var e={init:function(t,n){var e=this;e.elem=n;e.$elem=s(n);e.opt=s.extend({},this.opt,t);e.headers=e.$elem.find(e.opt.headers);e.previous=0;if(e.headers.length!==0){e.first=parseInt(e.headers.prop("nodeName").substring(1),null)}e.build();for(var a=0;a<e.opt.navElements.length;a++){var i=e.opt.navElements[a];s(e.opt.navigation).children().clone(true).appendTo(i)}},opt:{navigation:".anchorific",headers:"h1, h2, h3, h4, h5, h6",speed:200,anchorClass:"anchor",anchorText:"#",top:".top",spy:true,position:"append",spyOffset:!0,navElements:[]},build:function(){var n=this,t,e=function(){};if(n.opt.navigation){s(n.opt.navigation).append("<ul />");n.previous=s(n.opt.navigation).find("ul").last();e=function(t){return n.navigations(t)}}for(var a=0;a<n.headers.length;a++){t=n.headers.eq(a);e(t);n.anchor(t)}if(n.opt.spy)n.spy();if(n.opt.top)n.back()},navigations:function(t){var n=this,e,a,i,o=n.name(t);if(t.attr("id")!==p)o=t.attr("id");e=s("<a />").attr("href","#"+o).text(t.text());a=s("<li />").append(e);i=parseInt(t.prop("nodeName").substring(1),null);a.attr("data-tag",i);n.subheadings(i,a);n.first=i},subheadings:function(t,n){var e=this,a=s(e.opt.navigation).find("ul"),i=s(e.opt.navigation).find("li");if(t===e.first){e.previous.append(n)}else if(t>e.first){i.last().append("<ul />");s(e.opt.navigation).find("ul").last().append(n);e.previous=n.parent()}else{s("li[data-tag="+t+"]").last().parent().append(n);e.previous=n.parent()}},name:function(t){var n=t.text().replace(/[^\w\s]/gi,"").replace(/\s+/g,"-").toLowerCase();return n},anchor:function(t){var n=this,e=n.name(t),a,i=n.opt.anchorText,o=n.opt.anchorClass,r;if(t.attr("id")===p)t.attr("id",e);r=t.attr("id");a=s("<a />").attr("href","#"+r).html(i).addClass(o);if(n.opt.position==="append"){t.append(a)}else{t.prepend(a)}},back:function(){var n=this,e=s("body, html"),t=s(n.opt.top);t.on("click",function(t){t.preventDefault();e.animate({scrollTop:0},n.opt.speed)})},top:function(t){var n=this,e=n.opt.top,a;if(e!==false){a=s(t).scrollTop()>200?s(e).fadeIn():s(e).fadeOut()}},spy:function(){var n=this,t,e,a,i,o;s(r).scroll(function(t){n.top(this);e=n.headers.map(function(t){if(s(this).offset().top-s(r).scrollTop()<n.opt.spyOffset){return this}});e=s(e).eq(e.length-1);if(e&&e.length){a=s('li:has(a[href="#'+e.attr("id")+'"])');if(o!==p){o.removeClass("active")}a.addClass("active");o=a}})}};s.fn.anchorific=function(n){return this.each(function(){if(!s.data(this,"anchorific")){var t=Object.create(e);t.init(n,this);s.data(this,"anchorific",t)}})}})(jQuery,window,document); |
{ | ||
"name": "anchorific", | ||
"version": "0.1.2", | ||
"version": "0.1.3", | ||
"description": "Automatically generate anchored headings and nested navigations based on header tags", | ||
"main": "anchorific.js", | ||
"main": "./src/anchorific.js", | ||
"scripts": { | ||
"test": "echo \"No test specified\"" | ||
"test": "node-qunit-phantomjs ./tests/index.html", | ||
"build": "uglifyjs ./src/anchorific.js -o ./min/anchorific.min.js --mangle" | ||
}, | ||
@@ -22,4 +23,9 @@ "repository": { | ||
], | ||
"author": "renettarenula <ren.aysha@gmail.com>", | ||
"license": "MIT ", | ||
"husky": { | ||
"hooks": { | ||
"pre-commit": "pretty-quick --staged" | ||
} | ||
}, | ||
"author": "renettarenula <ayshaa@hey.com>", | ||
"license": "MIT", | ||
"bugs": { | ||
@@ -29,9 +35,9 @@ "url": "https://github.com/renettarenula/anchorific/issues" | ||
"devDependencies": { | ||
"grunt": "~0.4.1", | ||
"grunt-contrib-connect": "^0.8.0", | ||
"grunt-contrib-jshint": "~0.6.3", | ||
"grunt-contrib-nodeunit": "~0.2.0", | ||
"grunt-contrib-qunit": "0.3.0", | ||
"grunt-contrib-uglify": "~0.2.2" | ||
"husky": "^4.2.5", | ||
"node-qunit-phantomjs": "^2.1.1", | ||
"prettier": "2.0.5", | ||
"pretty-quick": "^2.0.1", | ||
"qunit": "^2.10.1", | ||
"uglify-js": "^3.10.0" | ||
} | ||
} |
# Anchorific.js | ||
Anchorific is a jQuery plugin that automatically generates anchored headings and nested navigations based on header tags. My intention is for it to be used in single-page project documentation. | ||
Project page and demo here: [renaysha.me/anchorific-js](http://renaysha.me/anchorific-js) | ||
Project page and demo here: [https://anchorific.netlify.app/](https://anchorific.netlify.app/) | ||
@@ -127,3 +127,3 @@ ## Installation | ||
```javascript | ||
$(".content").anchorific(); | ||
$('.content').anchorific(); | ||
``` | ||
@@ -134,12 +134,13 @@ | ||
```javascript | ||
$(".content").anchorific({ | ||
navigation: ".anchorific", // position of navigation | ||
headers: "h1, h2, h3, h4, h5, h6", // headers that you wish to target | ||
$('.content').anchorific({ | ||
navigation: '.anchorific', // position of navigation | ||
headers: 'h1, h2, h3, h4, h5, h6', // headers that you wish to target | ||
speed: 200, // speed of sliding back to top | ||
anchorClass: "anchor", // class of anchor links | ||
anchorText: "#", // prepended or appended to anchor headings | ||
top: ".top", // back to top button or link class | ||
anchorClass: 'anchor', // class of anchor links | ||
anchorText: '#', // prepended or appended to anchor headings | ||
top: '.top', // back to top button or link class | ||
spy: true, // scroll spy | ||
position: "append", // position of anchor text | ||
position: 'append', // position of anchor text | ||
spyOffset: 0, // specify heading offset for spy scrolling | ||
navElements: [], // if there are other elements that should act as navigation, add classes here | ||
}); | ||
@@ -164,7 +165,19 @@ ``` | ||
Anchorific.js uses Grunt CLI via NodeJS for linting, providing minified of production file, and executing the unit tests of the plugin. Check out the guide on [Grunt's getting started page](http://gruntjs.com/getting-started) to install it. | ||
Anchorific.js good old npm scripts to run test and build the code. | ||
Run test: | ||
```bash | ||
$ npm test | ||
``` | ||
Build code: | ||
```bash | ||
$ npm run build | ||
``` | ||
### Setup | ||
Once you have grunt CLI installed, CD to the project folder and type: | ||
CD to the project folder and type: | ||
@@ -175,10 +188,14 @@ ```bash | ||
Execute lint, minification, and unit tests: | ||
Run test | ||
```bash | ||
$ grunt | ||
npm test | ||
``` | ||
## Contributing | ||
In order to contribute, open a pull request and ensure that new tests are written in order to test out your contributions. Ensure that previous test pass by running `npm test`. Running `/tests/index.html` directly in the browser will show you some failed tests but this is because those tests are testing the scroll functionality which it doesn't support. Running `npm test` is more reliable. | ||
## License | ||
[MIT License](http://opensource.org/licenses/MIT) |
/* | ||
The MIT License (MIT) | ||
Copyright (c) <2013> <Ren Aysha> | ||
Copyright (c) <2020> <Aysha Anggraini> | ||
@@ -25,194 +25,228 @@ Permission is hereby granted, free of charge, to any person obtaining a copy | ||
if ( typeof Object.create !== 'function' ) { | ||
Object.create = function( obj ) { | ||
function F() {} | ||
F.prototype = obj; | ||
return new F(); | ||
}; | ||
if (typeof Object.create !== 'function') { | ||
Object.create = function (obj) { | ||
function F() {} | ||
F.prototype = obj; | ||
return new F(); | ||
}; | ||
} | ||
(function( $, window, document, undefined ) { | ||
"use strict"; | ||
(function ($, window, document, undefined) { | ||
'use strict'; | ||
var Anchorific = { | ||
var Anchorific = { | ||
init: function (options, elem) { | ||
var self = this; | ||
init: function( options, elem ) { | ||
var self = this; | ||
self.elem = elem; | ||
self.$elem = $(elem); | ||
self.elem = elem; | ||
self.$elem = $( elem ); | ||
self.opt = $.extend({}, this.opt, options); | ||
self.opt = $.extend( {}, this.opt, options ); | ||
self.headers = self.$elem.find(self.opt.headers); | ||
self.previous = 0; | ||
self.headers = self.$elem.find( self.opt.headers ); | ||
self.previous = 0; | ||
// Fix bug #1 | ||
if (self.headers.length !== 0) { | ||
self.first = parseInt(self.headers.prop('nodeName').substring(1), null); | ||
} | ||
// Fix bug #1 | ||
if ( self.headers.length !== 0 ) { | ||
self.first = parseInt( self.headers.prop( 'nodeName' ).substring( 1 ), null ); | ||
} | ||
self.build(); | ||
self.build(); | ||
}, | ||
for (var i = 0; i < self.opt.navElements.length; i++) { | ||
var nav = self.opt.navElements[i]; | ||
$(self.opt.navigation).children().clone(true).appendTo(nav); | ||
} | ||
}, | ||
opt: { | ||
navigation: '.anchorific', // position of navigation | ||
headers: 'h1, h2, h3, h4, h5, h6', // custom headers selector | ||
speed: 200, // speed of sliding back to top | ||
anchorClass: 'anchor', // class of anchor links | ||
anchorText: '#', // prepended or appended to anchor headings | ||
top: '.top', // back to top button or link class | ||
spy: true, // scroll spy | ||
position: 'append', // position of anchor text | ||
spyOffset: !0 // specify heading offset for spy scrolling | ||
}, | ||
opt: { | ||
navigation: '.anchorific', // position of navigation | ||
headers: 'h1, h2, h3, h4, h5, h6', // custom headers selector | ||
speed: 200, // speed of sliding back to top | ||
anchorClass: 'anchor', // class of anchor links | ||
anchorText: '#', // prepended or appended to anchor headings | ||
top: '.top', // back to top button or link class | ||
spy: true, // scroll spy | ||
position: 'append', // position of anchor text | ||
spyOffset: !0, // specify heading offset for spy scrolling | ||
navElements: [], // if there are other elements that should act as navigation, add classes here | ||
}, | ||
build: function() { | ||
var self = this, obj, navigations = function() {}; | ||
// when navigation configuration is set | ||
if ( self.opt.navigation ) { | ||
$( self.opt.navigation ).append( '<ul />' ); | ||
self.previous = $( self.opt.navigation ).find( 'ul' ).last(); | ||
navigations = function( obj ) { | ||
return self.navigations( obj ); | ||
}; | ||
} | ||
build: function () { | ||
var self = this, | ||
obj, | ||
navigations = function () {}; | ||
// when navigation configuration is set | ||
if (self.opt.navigation) { | ||
$(self.opt.navigation).append('<ul />'); | ||
self.previous = $(self.opt.navigation).find('ul').last(); | ||
navigations = function (obj) { | ||
return self.navigations(obj); | ||
}; | ||
} | ||
for( var i = 0; i < self.headers.length; i++ ) { | ||
obj = self.headers.eq( i ); | ||
navigations( obj ); | ||
self.anchor( obj ); | ||
} | ||
for (var i = 0; i < self.headers.length; i++) { | ||
obj = self.headers.eq(i); | ||
navigations(obj); | ||
self.anchor(obj); | ||
} | ||
if ( self.opt.spy ) | ||
self.spy(); | ||
if (self.opt.spy) self.spy(); | ||
if ( self.opt.top ) | ||
self.back(); | ||
}, | ||
if (self.opt.top) self.back(); | ||
}, | ||
navigations: function( obj ) { | ||
var self = this, link, list, which, name = self.name( obj ); | ||
navigations: function (obj) { | ||
var self = this, | ||
link, | ||
list, | ||
which, | ||
name = self.name(obj); | ||
if ( obj.attr( 'id' ) !== undefined ) | ||
name = obj.attr( 'id' ); | ||
if (obj.attr('id') !== undefined) name = obj.attr('id'); | ||
link = $( '<a />' ).attr( 'href', '#' + name ).text( obj.text() ); | ||
list = $( '<li />' ).append( link ); | ||
link = $('<a />') | ||
.attr('href', '#' + name) | ||
.text(obj.text()); | ||
list = $('<li />').append(link); | ||
which = parseInt( obj.prop( 'nodeName' ).substring( 1 ), null ); | ||
list.attr( 'data-tag', which ); | ||
which = parseInt(obj.prop('nodeName').substring(1), null); | ||
list.attr('data-tag', which); | ||
self.subheadings( which, list ); | ||
self.subheadings(which, list); | ||
self.first = which; | ||
}, | ||
self.first = which; | ||
}, | ||
subheadings: function( which, a ) { | ||
var self = this, ul = $( self.opt.navigation ).find( 'ul' ), | ||
li = $( self.opt.navigation ).find( 'li' ); | ||
subheadings: function (which, a) { | ||
var self = this, | ||
ul = $(self.opt.navigation).find('ul'), | ||
li = $(self.opt.navigation).find('li'); | ||
if ( which === self.first ) { | ||
self.previous.append( a ); | ||
} else if ( which > self.first ) { | ||
li.last().append( '<ul />' ); | ||
// can't use cache ul; need to find ul once more | ||
$( self.opt.navigation ).find( 'ul' ).last().append( a ); | ||
self.previous = a.parent(); | ||
} else { | ||
$( 'li[data-tag=' + which + ']' ).last().parent().append( a ); | ||
self.previous = a.parent(); | ||
} | ||
}, | ||
if (which === self.first) { | ||
self.previous.append(a); | ||
} else if (which > self.first) { | ||
li.last().append('<ul />'); | ||
// can't use cache ul; need to find ul once more | ||
$(self.opt.navigation).find('ul').last().append(a); | ||
self.previous = a.parent(); | ||
} else { | ||
$('li[data-tag=' + which + ']') | ||
.last() | ||
.parent() | ||
.append(a); | ||
self.previous = a.parent(); | ||
} | ||
}, | ||
name: function( obj ) { | ||
var name = obj.text().replace( /[^\w\s]/gi, '' ) | ||
.replace( /\s+/g, '-' ) | ||
.toLowerCase(); | ||
name: function (obj) { | ||
var name = obj | ||
.text() | ||
.replace(/[^\w\s]/gi, '') | ||
.replace(/\s+/g, '-') | ||
.toLowerCase(); | ||
return name; | ||
}, | ||
return name; | ||
}, | ||
anchor: function( obj ) { | ||
var self = this, name = self.name( obj ), anchor, text = self.opt.anchorText, | ||
klass = self.opt.anchorClass, id; | ||
anchor: function (obj) { | ||
var self = this, | ||
name = self.name(obj), | ||
anchor, | ||
text = self.opt.anchorText, | ||
klass = self.opt.anchorClass, | ||
id; | ||
if ( obj.attr( 'id' ) === undefined ) | ||
obj.attr( 'id', name ); | ||
if (obj.attr('id') === undefined) obj.attr('id', name); | ||
id = obj.attr( 'id' ); | ||
id = obj.attr('id'); | ||
anchor = $( '<a />' ).attr( 'href', '#' + id ).html( text ).addClass( klass ); | ||
anchor = $('<a />') | ||
.attr('href', '#' + id) | ||
.html(text) | ||
.addClass(klass); | ||
if ( self.opt.position === 'append' ) { | ||
obj.append( anchor ); | ||
} else { | ||
obj.prepend( anchor ); | ||
} | ||
}, | ||
if (self.opt.position === 'append') { | ||
obj.append(anchor); | ||
} else { | ||
obj.prepend(anchor); | ||
} | ||
}, | ||
back: function() { | ||
var self = this, body = $( 'body, html' ), top = $( self.opt.top ); | ||
back: function () { | ||
var self = this, | ||
body = $('body, html'), | ||
top = $(self.opt.top); | ||
top.on( 'click', function( e ) { | ||
e.preventDefault(); | ||
top.on('click', function (e) { | ||
e.preventDefault(); | ||
body.animate({ | ||
'scrollTop': 0 | ||
}, self.opt.speed ); | ||
}); | ||
}, | ||
body.animate( | ||
{ | ||
scrollTop: 0, | ||
}, | ||
self.opt.speed | ||
); | ||
}); | ||
}, | ||
top: function( that ) { | ||
var self = this, top = self.opt.top, back; | ||
top: function (that) { | ||
var self = this, | ||
top = self.opt.top, | ||
back; | ||
if ( top !== false ) { | ||
back = ( $( that ).scrollTop() > 200 ) ? | ||
$( top ).fadeIn() : | ||
$( top ).fadeOut(); | ||
} | ||
}, | ||
if (top !== false) { | ||
back = $(that).scrollTop() > 200 ? $(top).fadeIn() : $(top).fadeOut(); | ||
} | ||
}, | ||
spy: function() { | ||
var self = this, previous, current, list, top, prev; | ||
spy: function () { | ||
var self = this, | ||
previous, | ||
current, | ||
list, | ||
top, | ||
prev; | ||
$( window ).scroll( function( e ) { | ||
// show links back to top | ||
self.top( this ); | ||
// get all the header on top of the viewport | ||
current = self.headers.map( function( e ) { | ||
if ( ( $( this ).offset().top - $( window ).scrollTop() ) < self.opt.spyOffset ) { | ||
return this; | ||
} | ||
}); | ||
// get only the latest header on the viewport | ||
current = $( current ).eq( current.length - 1 ); | ||
$(window).scroll(function (e) { | ||
// show links back to top | ||
self.top(this); | ||
// get all the header on top of the viewport | ||
current = self.headers.map(function (e) { | ||
if ( | ||
$(this).offset().top - $(window).scrollTop() < | ||
self.opt.spyOffset | ||
) { | ||
return this; | ||
} | ||
}); | ||
// get only the latest header on the viewport | ||
current = $(current).eq(current.length - 1); | ||
if ( current && current.length ) { | ||
// get all li tag that contains href of # ( all the parents ) | ||
list = $( 'li:has(a[href="#' + current.attr( 'id' ) + '"])' ); | ||
if (current && current.length) { | ||
// get all li tag that contains href of # ( all the parents ) | ||
list = $('li:has(a[href="#' + current.attr('id') + '"])'); | ||
if ( prev !== undefined ) { | ||
prev.removeClass( 'active' ); | ||
} | ||
if (prev !== undefined) { | ||
prev.removeClass('active'); | ||
} | ||
list.addClass( 'active' ); | ||
prev = list; | ||
} | ||
}); | ||
} | ||
}; | ||
list.addClass('active'); | ||
prev = list; | ||
} | ||
}); | ||
}, | ||
}; | ||
$.fn.anchorific = function( options ) { | ||
return this.each(function() { | ||
if ( ! $.data( this, 'anchorific' ) ) { | ||
var anchor = Object.create( Anchorific ); | ||
$.fn.anchorific = function (options) { | ||
return this.each(function () { | ||
if (!$.data(this, 'anchorific')) { | ||
var anchor = Object.create(Anchorific); | ||
anchor.init( options, this ); | ||
anchor.init(options, this); | ||
$.data( this, 'anchorific', anchor ); | ||
} | ||
}); | ||
}; | ||
})( jQuery, window, document ); | ||
$.data(this, 'anchorific', anchor); | ||
} | ||
}); | ||
}; | ||
})(jQuery, window, document); |
$(function () { | ||
module('Anchorific'); | ||
module( 'Anchorific' ); | ||
var subject = '#qunit-fixture > #default', | ||
$el = $('h1, h2, h3, h4, h5, h6', subject); | ||
var subject = '#qunit-fixture > #default', | ||
$el = $( 'h1, h2, h3, h4, h5, h6', subject ); | ||
test('ID is correctly generated', function () { | ||
var results = ['hello-world', 'existing-id']; | ||
test( 'ID is correctly generated', function() { | ||
var results = [ 'hello-world', 'existing-id' ]; | ||
$(subject).anchorific({ | ||
navigation: false, | ||
spy: false, | ||
}); | ||
$( subject ).anchorific({ | ||
navigation: false, | ||
spy: false | ||
}); | ||
equal($el.eq(0).attr('id'), results[0], 'Correct ID is generated'); | ||
equal($el.eq(1).attr('id'), results[1], 'Existing ID is used instead'); | ||
}); | ||
equal( $el.eq( 0 ).attr( 'id' ), results[ 0 ], 'Correct ID is generated' ); | ||
equal( $el.eq( 1 ).attr( 'id' ), results[ 1 ], 'Existing ID is used instead'); | ||
}); | ||
test('Anchor link is correctly generated', function () { | ||
var results = ['#hello-world', '#existing-id'], | ||
actual, | ||
anchor; | ||
test( 'Anchor link is correctly generated', function() { | ||
var results = [ '#hello-world', '#existing-id' ], actual, anchor; | ||
$(subject).anchorific({ | ||
navigation: false, | ||
spy: false, | ||
}); | ||
$( subject ).anchorific({ | ||
navigation: false, | ||
spy: false | ||
}); | ||
actual = function (eq) { | ||
return $el.eq(eq).find('.anchor').attr('href'); | ||
}; | ||
actual = function( eq ) { | ||
return $el.eq( eq ).find( '.anchor' ).attr( 'href' ); | ||
}; | ||
anchor = function (eq) { | ||
return $el.eq(eq).find('.anchor'); | ||
}; | ||
anchor = function( eq ) { | ||
return $el.eq( eq ).find( '.anchor'); | ||
}; | ||
equal( | ||
$el.eq(0).text(), | ||
'Hello World!#', | ||
'Anchor link is appended correctly' | ||
); | ||
equal(anchor(0).text(), '#', 'Link text is correctly generated'); | ||
equal(actual(0), results[0], 'href is generated based on the ids'); | ||
equal(actual(1), results[1], 'href is generated based on the existing ids'); | ||
}); | ||
equal( $el.eq( 0 ).text(), 'Hello World!#', 'Anchor link is appended correctly' ); | ||
equal( anchor( 0 ).text(), '#', 'Link text is correctly generated' ); | ||
equal( actual( 0 ), results[ 0 ], 'href is generated based on the ids' ); | ||
equal( actual( 1 ), results[ 1 ], 'href is generated based on the existing ids' ); | ||
}); | ||
test('Anchor link is correctly generated based on options specified', function () { | ||
var subject = '#qunit-fixture > #default', | ||
$el = $('h1, h2, h3, h4, h5, h6', subject), | ||
actual = 0; | ||
test( 'Anchor link is correctly generated based on options specified', function() { | ||
var subject = '#qunit-fixture > #default', | ||
$el = $( 'h1, h2, h3, h4, h5, h6', subject ), actual = 0; | ||
$(subject).anchorific({ | ||
position: 'prepend', | ||
navigation: false, | ||
anchorText: '¶', | ||
anchorClass: 'newclass', | ||
spy: false, | ||
}); | ||
$( subject ).anchorific({ | ||
position: 'prepend', | ||
navigation: false, | ||
anchorText: '¶', | ||
anchorClass: 'newclass', | ||
spy: false | ||
}); | ||
actual = function (eq, me) { | ||
if (me !== undefined) return $el.eq(eq).find(me); | ||
else return $el.eq(eq); | ||
}; | ||
actual = function( eq, me ) { | ||
if ( me !== undefined ) | ||
return $el.eq( eq ).find( me ); | ||
else | ||
return $el.eq( eq ); | ||
}; | ||
equal( | ||
actual(0, 'a').attr('class'), | ||
'newclass', | ||
'Class uses option specified' | ||
); | ||
equal( | ||
actual(0).text(), | ||
'¶Hello World!', | ||
'Option prepend works as expected' | ||
); | ||
equal(actual(0, '.newclass').text(), '¶', 'Text uses option specified'); | ||
}); | ||
equal( actual( 0, 'a' ).attr( 'class' ), 'newclass', 'Class uses option specified' ); | ||
equal( actual( 0 ).text(), '¶Hello World!', 'Option prepend works as expected' ); | ||
equal( actual( 0, '.newclass' ).text(), '¶', 'Text uses option specified' ); | ||
}); | ||
test('Navigation links are generated as expected', function () { | ||
var $el = $('.anchor-default', subject), | ||
tags = $('h1, h2, h3, h4, h5, h6', subject), | ||
actual = 0, | ||
text = 0; | ||
test( 'Navigation links are generated as expected', function() { | ||
var $el = $( '.anchor-default', subject ), | ||
tags = $( 'h1, h2, h3, h4, h5, h6', subject ), | ||
actual = 0, text = 0; | ||
$(subject).anchorific({ | ||
navigation: '#qunit-fixture > #default > .anchor-default', | ||
}); | ||
$( subject ).anchorific({ | ||
navigation: '#qunit-fixture > #default > .anchor-default' | ||
}); | ||
actual = function (eq) { | ||
return $el.find('a').eq(eq); | ||
}; | ||
actual = function( eq ) { | ||
return $el.find( 'a' ).eq( eq ); | ||
}; | ||
text = function (eq) { | ||
return tags.eq(eq).text().replace('#', ''); | ||
}; | ||
text = function( eq ) { | ||
return tags.eq( eq ).text().replace( '#', '' ); | ||
}; | ||
equal(actual(0).attr('href'), '#hello-world', 'href is correct for no id'); | ||
equal( | ||
actual(0).text(), | ||
text(0), | ||
'link text is correct for non-existing id' | ||
); | ||
equal( | ||
actual(1).attr('href'), | ||
'#existing-id', | ||
'href is correct when id exist' | ||
); | ||
equal(actual(1).text(), text(1), 'link text is correct for existing id'); | ||
}); | ||
equal( actual( 0 ).attr( 'href' ), '#hello-world', 'href is correct for no id' ); | ||
equal( actual( 0 ).text(), text( 0 ), 'link text is correct for non-existing id' ); | ||
equal( actual( 1 ).attr( 'href' ), '#existing-id', 'href is correct when id exist' ); | ||
equal( actual( 1 ).text(), text( 1 ), 'link text is correct for existing id' ); | ||
}); | ||
test('Navigation link is correctly generated based on given headers', function () { | ||
var subject = '#qunit-fixture > #navigation', | ||
$el = $('.anchorific', subject), | ||
expected = $.trim($('#expected').html()); | ||
test( 'Navigation link is correctly generated based on given headers', function() { | ||
var subject = '#qunit-fixture > #navigation', | ||
$el = $( '.anchorific', subject ), | ||
expected = $.trim( $( '#expected' ).html() ); | ||
$(subject).anchorific({ | ||
navigation: '#qunit-fixture > #navigation > .anchorific', | ||
}); | ||
$( subject ).anchorific({ | ||
navigation: '#qunit-fixture > #navigation > .anchorific' | ||
}); | ||
equal($el.html(), expected, 'Navigation is generated as expected'); | ||
}); | ||
equal( $el.html(), expected, 'Navigation is generated as expected' ); | ||
}); | ||
test('Elements listed under navElements option will have the same content as the one listed under navigation option', function () { | ||
var subject = '#qunit-fixture > #navigation', | ||
$el = $('.anchorific', subject), | ||
$navEl = $('.anchorific-2', subject), | ||
expected = $.trim($('#expected').html()); | ||
asyncTest( 'Scrollspy highlights links correctly when scroll', function() { | ||
setTimeout(function() { | ||
var s = $( '#scroll' ).contents().find( '.anchorific' ).find( 'li.active' ); | ||
equal( s.length, 6, "No of links highlighted is correct when scroll to h6" ); | ||
start(); | ||
}, 100 ); | ||
}); | ||
$(subject).anchorific({ | ||
navigation: '#qunit-fixture > #navigation > .anchorific', | ||
navElements: ['.anchorific-2'], | ||
}); | ||
asyncTest( 'Back to top link is visible when configured', function() { | ||
setTimeout(function() { | ||
var s = $( '#scroll' ).contents().find( '.top' ); | ||
equal( s.is( ':visible' ), true, 'Top button is visible' ); | ||
start(); | ||
}, 100 ); | ||
}); | ||
equal($navEl.html(), expected, 'Nav elements is generated as expected'); | ||
}); | ||
asyncTest( 'Clicking back to top link works as expected', function() { | ||
var fixture = 0, test = 0, actions = []; | ||
// simulate click for top button | ||
fixture = function() { | ||
var s = $( '#scroll' ).contents().find( '.top' ); | ||
s.trigger( "click" ); | ||
}; | ||
// ensure that it moves the scroll back to top | ||
test = function() { | ||
equal( $( '#scroll' ).contents().scrollTop(), 0, 'Back to top works' ); | ||
start(); | ||
}; | ||
asyncTest('Scrollspy highlights links correctly when scroll', function () { | ||
setTimeout(function () { | ||
var s = $('#scroll').contents().find('.anchorific').find('li.active'); | ||
equal( | ||
s.length, | ||
6, | ||
'No of links highlighted is correct when scroll to h6' | ||
); | ||
start(); | ||
}, 100); | ||
}); | ||
actions = [ fixture(), test() ]; | ||
asyncTest('Back to top link is visible when configured', function () { | ||
setTimeout(function () { | ||
var s = $('#scroll').contents().find('.top'); | ||
equal(s.is(':visible'), true, 'Top button is visible'); | ||
start(); | ||
}, 100); | ||
}); | ||
actions.map(function( action, index ) { | ||
setTimeout( action, 100 + index * 100 ); | ||
}); | ||
}); | ||
}); | ||
asyncTest('Clicking back to top link works as expected', function () { | ||
var fixture = 0, | ||
test = 0, | ||
actions = []; | ||
// simulate click for top button | ||
fixture = function () { | ||
var s = $('#scroll').contents().find('.top'); | ||
s.trigger('click'); | ||
}; | ||
// ensure that it moves the scroll back to top | ||
test = function () { | ||
equal($('#scroll').contents().scrollTop(), 0, 'Back to top works'); | ||
start(); | ||
}; | ||
actions = [fixture(), test()]; | ||
actions.map(function (action, index) { | ||
setTimeout(action, 100 + index * 100); | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
385639
17
3448
2
198