MCPcopy Create free account
hub / github.com/SpringRoll/SpringRoll / AnchorJS

Function AnchorJS

docs/assets/anchor.js:26–347  ·  view source on GitHub ↗
(options)

Source from the content-addressed store, hash-verified

24})(this, function () {
25 'use strict';
26 function AnchorJS(options) {
27 this.options = options || {};
28 this.elements = [];
29
30 /**
31 * Assigns options to the internal options object, and provides defaults.
32 * @param {Object} opts - Options object
33 */
34 function _applyRemainingDefaultOptions(opts) {
35 opts.icon = opts.hasOwnProperty('icon') ? opts.icon : '\ue9cb'; // Accepts characters (and also URLs?), like '#', '¶', '❡', or '§'.
36 opts.visible = opts.hasOwnProperty('visible') ? opts.visible : 'hover'; // Also accepts 'always' & 'touch'
37 opts.placement = opts.hasOwnProperty('placement')
38 ? opts.placement
39 : 'right'; // Also accepts 'left'
40 opts.class = opts.hasOwnProperty('class') ? opts.class : ''; // Accepts any class name.
41 // Using Math.floor here will ensure the value is Number-cast and an integer.
42 opts.truncate = opts.hasOwnProperty('truncate')
43 ? Math.floor(opts.truncate)
44 : 64; // Accepts any value that can be typecast to a number.
45 }
46
47 _applyRemainingDefaultOptions(this.options);
48
49 /**
50 * Checks to see if this device supports touch. Uses criteria pulled from Modernizr:
51 * https://github.com/Modernizr/Modernizr/blob/da22eb27631fc4957f67607fe6042e85c0a84656/feature-detects/touchevents.js#L40
52 * @returns {Boolean} - true if the current device supports touch.
53 */
54 this.isTouchDevice = function () {
55 return !!(
56 'ontouchstart' in window ||
57 (window.DocumentTouch && document instanceof DocumentTouch)
58 );
59 };
60
61 /**
62 * Add anchor links to page elements.
63 * @param {String|Array|Nodelist} selector - A CSS selector for targeting the elements you wish to add anchor links
64 * to. Also accepts an array or nodeList containing the relavant elements.
65 * @returns {this} - The AnchorJS object
66 */
67 this.add = function (selector) {
68 var elements,
69 elsWithIds,
70 idList,
71 elementID,
72 i,
73 index,
74 count,
75 tidyText,
76 newTidyText,
77 readableID,
78 anchor,
79 visibleOptionToUse,
80 indexesToDrop = [];
81
82 // We reapply options here because somebody may have overwritten the default options object when setting options.
83 // For example, this overwrites all options but visible:

Callers

nothing calls this directly

Calls 4

_getElementsFunction · 0.85
_addBaselineStylesFunction · 0.85
removeMethod · 0.80

Tested by

no test coverage detected