05.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. $(document).ready(function () {
  2. 'use strict';
  3. // Use attr() to add an id, rel, and title.
  4. $('div.chapter a[href*="wikipedia"]').attr({
  5. rel: 'external',
  6. title: function () {
  7. return 'Learn more about ' + $(this).text() + ' at Wikipedia.';
  8. },
  9. id: function (index, oldValue) {
  10. return 'wikilink-' + index;
  11. }
  12. });
  13. // Add "back to top" links.
  14. $('div.chapter p').slice(4).after($('<a href="#top">back to top</a>'));
  15. $('<a id="top"></a>').prependTo('body');
  16. // insertBefore <elem> prependTo <children /> appendTo </elem> insertAfter
  17. // Create footnotes.
  18. var $notes = $('<ol id="notes"></ol>').insertBefore('#footer');
  19. $('span.footnote').each (function (index) {
  20. $(this)
  21. .before([
  22. '<a href="#footnote-',
  23. index + 1,
  24. '" id="context-',
  25. index + 1,
  26. '" class="context">',
  27. '<sup>',
  28. index + 1,
  29. '</sup></a>'
  30. ].join(''))
  31. .appendTo($notes)
  32. .append([
  33. '&nbsp;(<a href="#context-',
  34. index + 1,
  35. '">context</a>)'
  36. ].join(''))
  37. .wrap('<li id="footnote-' + (index + 1) + '"></li>');
  38. });
  39. // Style pull quotes.
  40. $('span.pull-quote').each(function (index) {
  41. var $parentParagraph = $(this).parent('p');
  42. $parentParagraph.css('position', 'relative');
  43. var $clonedCopy = $(this).clone();
  44. $clonedCopy
  45. .addClass('pulled')
  46. .find('span.drop')
  47. .html('&hellip;')
  48. .end()
  49. .text($clonedCopy.text())
  50. .prependTo($parentParagraph);
  51. });
  52. $('a[href="#top"]').click(function (event) {
  53. var $youWereHere = $(this).after($('<p>You were here</p>')).next('p');
  54. setTimeout(function () {
  55. $youWereHere.remove();
  56. }, 3000)
  57. });
  58. $('#f-author').click(function () {
  59. var $this = $(this);
  60. var $b = $('b', this);
  61. if ($b.length) {
  62. $this.text($b.text());
  63. }
  64. else {
  65. $this.wrapInner('<b/>');
  66. }
  67. });
  68. $('p').each(function () {
  69. var $this = $(this);
  70. var sClass = $this.attr('class') ? $this.attr('class') : '';
  71. var aClass = sClass.split(' ');
  72. // In a more general case, we would also need to :
  73. // - remove multiple spacers
  74. // - order and deduplicate existing classes.
  75. aClass.push('inhabitants');
  76. sClass = aClass.join(' ').trim();
  77. $this.attr('class', sClass);
  78. });
  79. });