Sha256: d516c36ee1081649f9dde2206581de2a4a0cbd429dde68e2f912e669699bf5a9

Contents?: true

Size: 953 Bytes

Versions: 27

Compression:

Stored size: 953 Bytes

Contents

/**
 * Module dependencies.
 */

var delegate = require('delegate');
var url = require('url');

/**
 * Handle link delegation on `el` or the document,
 * and invoke `fn(e)` when clickable.
 *
 * @param {Element|Function} el or fn
 * @param {Function} [fn]
 * @api public
 */

module.exports = function(el, fn){
  // default to document
  if ('function' == typeof el) {
    fn = el;
    el = document;
  }

  delegate.bind(el, 'a', 'click', function(e){
    if (clickable(e)) fn(e);
  });
};

/**
 * Check if `e` is clickable.
 */

function clickable(e) {
  if (1 != which(e)) return;
  if (e.metaKey || e.ctrlKey || e.shiftKey) return;
  if (e.defaultPrevented) return;

  // target
  var el = e.target;

  // check target
  if (el.target) return;

  // x-origin
  if (url.isCrossDomain(el.href)) return;

  return true;
}

/**
 * Event button.
 */

function which(e) {
  e = e || window.event;
  return null == e.which
    ? e.button
    : e.which;
}

Version data entries

27 entries across 14 versions & 1 rubygems

Version Path
ende-0.2.3 vendor/assets/components/component-link-delegate/index.js
ende-0.2.3 vendor/components/component-link-delegate/index.js
ende-0.2.2 vendor/assets/components/component-link-delegate/index.js
ende-0.2.2 vendor/components/component-link-delegate/index.js
ende-0.2.1 vendor/assets/components/component-link-delegate/index.js
ende-0.2.1 vendor/components/component-link-delegate/index.js
ende-0.2.0 vendor/components/component-link-delegate/index.js
ende-0.2.0 vendor/assets/components/component-link-delegate/index.js
ende-0.1.14 vendor/assets/components/component-link-delegate/index.js
ende-0.1.14 vendor/components/component-link-delegate/index.js
ende-0.1.13 vendor/components/component-link-delegate/index.js
ende-0.1.13 vendor/assets/components/component-link-delegate/index.js
ende-0.1.12 vendor/components/component-link-delegate/index.js
ende-0.1.12 vendor/assets/components/component-link-delegate/index.js
ende-0.1.11 vendor/components/component-link-delegate/index.js
ende-0.1.11 vendor/assets/components/component-link-delegate/index.js
ende-0.1.10 vendor/assets/components/component-link-delegate/index.js
ende-0.1.10 vendor/components/component-link-delegate/index.js
ende-0.1.9 vendor/components/component-link-delegate/index.js
ende-0.1.9 vendor/assets/components/component-link-delegate/index.js