3b9640bafb1afc522060b750033e206234b382fe.svn-base 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*!
  2. * AnchorJS - v1.0.1 - 2015-05-15
  3. * https://github.com/bryanbraun/anchorjs
  4. * Copyright (c) 2015 Bryan Braun; Licensed MIT
  5. */
  6. function AnchorJS(options) {
  7. 'use strict';
  8. this.options = options || {};
  9. this._applyRemainingDefaultOptions = function(opts) {
  10. this.options.icon = this.options.hasOwnProperty('icon') ? opts.icon : '&#xe9cb'; // Accepts characters (and also URLs?), like '#', '¶', '❡', or '§'.
  11. this.options.visible = this.options.hasOwnProperty('visible') ? opts.visible : 'hover'; // Also accepts 'always'
  12. this.options.placement = this.options.hasOwnProperty('placement') ? opts.placement : 'right'; // Also accepts 'left'
  13. this.options.class = this.options.hasOwnProperty('class') ? opts.class : ''; // Accepts any class name.
  14. };
  15. this._applyRemainingDefaultOptions(options);
  16. this.add = function(selector) {
  17. var elements,
  18. elsWithIds,
  19. idList,
  20. elementID,
  21. i,
  22. roughText,
  23. tidyText,
  24. index,
  25. count,
  26. newTidyText,
  27. readableID,
  28. anchor,
  29. div,
  30. anchorNodes;
  31. this._applyRemainingDefaultOptions(this.options);
  32. // Provide a sensible default selector, if none is given.
  33. if (!selector) {
  34. selector = 'h1, h2, h3, h4, h5, h6';
  35. } else if (typeof selector !== 'string') {
  36. throw new Error('The selector provided to AnchorJS was invalid.');
  37. }
  38. elements = document.querySelectorAll(selector);
  39. if (elements.length === 0) {
  40. return false;
  41. }
  42. this._addBaselineStyles();
  43. // We produce a list of existing IDs so we don't generate a duplicate.
  44. elsWithIds = document.querySelectorAll('[id]');
  45. idList = [].map.call(elsWithIds, function assign(el) {
  46. return el.id;
  47. });
  48. for (i = 0; i < elements.length; i++) {
  49. if (elements[i].hasAttribute('id')) {
  50. elementID = elements[i].getAttribute('id');
  51. } else {
  52. roughText = elements[i].textContent;
  53. // Refine it so it makes a good ID. Strip out non-safe characters, replace
  54. // spaces with hyphens, truncate to 32 characters, and make toLowerCase.
  55. //
  56. // Example string: // '⚡⚡⚡ Unicode icons are cool--but don't belong in a URL.'
  57. tidyText = roughText.replace(/[^\w\s-]/gi, '') // ' Unicode icons are cool--but dont belong in a URL'
  58. .replace(/\s+/g, '-') // '-Unicode-icons-are-cool--but-dont-belong-in-a-URL'
  59. .replace(/-{2,}/g, '-') // '-Unicode-icons-are-cool-but-dont-belong-in-a-URL'
  60. .substring(0, 32) // '-Unicode-icons-are-cool-but-dont'
  61. .replace(/^-+|-+$/gm, '') // 'Unicode-icons-are-cool-but-dont'
  62. .toLowerCase(); // 'unicode-icons-are-cool-but-dont'
  63. // Compare our generated ID to existing IDs (and increment it if needed)
  64. // before we add it to the page.
  65. newTidyText = tidyText;
  66. count = 0;
  67. do {
  68. if (index !== undefined) {
  69. newTidyText = tidyText + '-' + count;
  70. }
  71. // .indexOf is supported in IE9+.
  72. index = idList.indexOf(newTidyText);
  73. count += 1;
  74. } while (index !== -1);
  75. index = undefined;
  76. idList.push(newTidyText);
  77. // Assign it to our element.
  78. // Currently the setAttribute element is only supported in IE9 and above.
  79. elements[i].setAttribute('id', newTidyText);
  80. elementID = newTidyText;
  81. }
  82. readableID = elementID.replace(/-/g, ' ');
  83. anchor = '<a class="anchorjs-link ' + this.options.class + '" href="#' + elementID + '" aria-label="Anchor link for: ' + readableID + '" data-anchorjs-icon="' + this.options.icon + '"></a>';
  84. div = document.createElement('div');
  85. div.innerHTML = anchor;
  86. anchorNodes = div.childNodes;
  87. if (this.options.visible === 'always') {
  88. anchorNodes[0].style.opacity = '1';
  89. }
  90. if (this.options.icon === '&#xe9cb') {
  91. anchorNodes[0].style.fontFamily = 'anchorjs-icons';
  92. anchorNodes[0].style.fontStyle = 'normal';
  93. anchorNodes[0].style.fontVariant = 'normal';
  94. anchorNodes[0].style.fontWeight = 'normal';
  95. }
  96. if (this.options.placement === 'left') {
  97. anchorNodes[0].style.position = 'absolute';
  98. anchorNodes[0].style.marginLeft = '-1em';
  99. anchorNodes[0].style.paddingRight = '0.5em';
  100. elements[i].insertBefore(anchorNodes[0], elements[i].firstChild);
  101. } else { // if the option provided is `right` (or anything else).
  102. anchorNodes[0].style.paddingLeft = '0.375em';
  103. elements[i].appendChild(anchorNodes[0]);
  104. }
  105. }
  106. return this;
  107. };
  108. this.remove = function(selector) {
  109. var domAnchor,
  110. elements = document.querySelectorAll(selector);
  111. for (var i = 0; i < elements.length; i++) {
  112. domAnchor = elements[i].querySelector('.anchorjs-link');
  113. if (domAnchor) {
  114. elements[i].removeChild(domAnchor);
  115. }
  116. }
  117. return this;
  118. };
  119. this._addBaselineStyles = function() {
  120. // We don't want to add global baseline styles if they've been added before.
  121. if (document.head.querySelector('style.anchorjs') !== null) {
  122. return;
  123. }
  124. var style = document.createElement('style'),
  125. linkRule =
  126. ' .anchorjs-link {' +
  127. ' opacity: 0;' +
  128. ' text-decoration: none;' +
  129. ' -webkit-font-smoothing: antialiased;' +
  130. ' -moz-osx-font-smoothing: grayscale;' +
  131. ' }',
  132. hoverRule =
  133. ' *:hover > .anchorjs-link,' +
  134. ' .anchorjs-link:focus {' +
  135. ' opacity: 1;' +
  136. ' }',
  137. anchorjsLinkFontFace =
  138. ' @font-face {' +
  139. ' font-family: "anchorjs-icons";' +
  140. ' font-style: normal;' +
  141. ' font-weight: normal;' + // Icon from icomoon; 10px wide & 10px tall; 2 empty below & 4 above
  142. ' src: url(data:application/x-font-ttf;charset=utf-8;base64,AAEAAAALAIAAAwAwT1MvMg8SBTUAAAC8AAAAYGNtYXAWi9QdAAABHAAAAFRnYXNwAAAAEAAAAXAAAAAIZ2x5Zgq29TcAAAF4AAABNGhlYWQEZM3pAAACrAAAADZoaGVhBhUDxgAAAuQAAAAkaG10eASAADEAAA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AAEAHAAOAAMAAQQJAAIADgCmAAMAAQQJAAMAHABZAAMAAQQJAAQAHADCAAMAAQQJAAUAFgA1AAMAAQQJAAYAHACDAAMAAQQJAAoANAD4YW5jaG9yanMtaWNvbnMAYQBuAGMAaABvAHIAagBzAC0AaQBjAG8AbgBzVmVyc2lvbiAxLjAAVgBlAHIAcwBpAG8AbgAgADEALgAwYW5jaG9yanMtaWNvbnMAYQBuAGMAaABvAHIAagBzAC0AaQBjAG8AbgBzYW5jaG9yanMtaWNvbnMAYQBuAGMAaABvAHIAagBzAC0AaQBjAG8AbgBzUmVndWxhcgBSAGUAZwB1AGwAYQByYW5jaG9yanMtaWNvbnMAYQBuAGMAaABvAHIAagBzAC0AaQBjAG8AbgBzRm9udCBnZW5lcmF0ZWQgYnkgSWNvTW9vbi4ARgBvAG4AdAAgAGcAZQBuAGUAcgBhAHQAZQBkACAAYgB5ACAASQBjAG8ATQBvAG8AbgAuAAAAAwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA==) format("truetype");' +
  143. ' }',
  144. pseudoElContent =
  145. ' [data-anchorjs-icon]::after {' +
  146. ' content: attr(data-anchorjs-icon);' +
  147. ' }',
  148. firstStyleEl;
  149. style.className = 'anchorjs';
  150. style.appendChild(document.createTextNode('')); // Necessary for Webkit.
  151. // We place it in the head with the other style tags, if possible, so as to
  152. // not look out of place. We insert before the others so these styles can be
  153. // overridden if necessary.
  154. firstStyleEl = document.head.querySelector('[rel="stylesheet"], style');
  155. if (firstStyleEl === undefined) {
  156. document.head.appendChild(style);
  157. } else {
  158. document.head.insertBefore(style, firstStyleEl);
  159. }
  160. style.sheet.insertRule(linkRule, style.sheet.cssRules.length);
  161. style.sheet.insertRule(hoverRule, style.sheet.cssRules.length);
  162. style.sheet.insertRule(pseudoElContent, style.sheet.cssRules.length);
  163. style.sheet.insertRule(anchorjsLinkFontFace, style.sheet.cssRules.length);
  164. };
  165. }
  166. var anchors = new AnchorJS();