Created
June 10, 2011 15:43
-
-
Save seutje/1019095 to your computer and use it in GitHub Desktop.
Drupal.behaviors.printLinks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function($) { | |
/** | |
* Put numbers after all links and list em at the bottom of the body | |
*/ | |
Drupal.behaviors.printLinks = function(context) { | |
var printLinks = Drupal.printLinks, | |
exceptions = ([ | |
'ul.menu a' | |
]).join(', '); | |
if (printLinks.wrapper === false) { | |
printLinks.init(); | |
} | |
$('a') | |
.not(exceptions) | |
.not('.printLinks-processed') | |
.addClass('printLinks-processed') | |
.each(function() { | |
var $this = $(this); | |
if (printLinks.checkLink($this)) { | |
printLinks.links.push($this); | |
} | |
}); | |
// Only act if we have new links to add. | |
if (printLinks.wrapper && printLinks.wrapper.find('li').length < printLinks.links.length) { | |
var am = printLinks.links.length, | |
$lis = printLinks.wrapper.find('li'), | |
lis = $lis.length, | |
arr = []; | |
for (; lis < am; lis++) { | |
arr.push('<li>' + printLinks.links[lis][0].href + '</li>'); | |
// People often have an inner span in their links | |
if (printLinks.links[lis].find('span').length) { | |
printLinks.links[lis].find('span').append('<sup class="printLinks-number print-only">' + (lis+1) + '</sup>'); | |
} | |
else { | |
printLinks.links[lis].append('<sup class="printLinks-number print-only">' + (lis+1) + '</sup>'); | |
} | |
} | |
if (arr.length) { | |
printLinks.wrapper.append(arr.join('')); | |
} | |
} | |
} | |
Drupal.printLinks = { | |
links: [], | |
init : function() { | |
this.wrapper = $('<div id="printlinks-wrapper" class="print-only"><h2>' + Drupal.t('Links') + '</h2><ol></ol></div>').appendTo('body').find('ol'); | |
}, | |
wrapper: false, | |
checkLink : function($el) { | |
var at = $el.attr('href'); | |
// returns false for links with no href, with only a hash, mailto links and invisible links | |
return at && at != $el[0].hash && at != '#' && !$el.is('[href^="mailto:"]') && !$el.is(':hidden'); | |
} | |
} | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
should really just fake print state and capture links then