view annotator_files/lib/class.js @ 4:6979313586cf

new version of annotator.
author casties
date Mon, 27 Aug 2012 19:05:38 +0200
parents 6356e78ccf5c
children
line wrap: on
line source

// Generated by CoffeeScript 1.3.3
var Delegator,
  __slice = [].slice,
  __hasProp = {}.hasOwnProperty;

Delegator = (function() {

  Delegator.prototype.events = {};

  Delegator.prototype.options = {};

  Delegator.prototype.element = null;

  function Delegator(element, options) {
    this.options = $.extend(true, {}, this.options, options);
    this.element = $(element);
    this.on = this.subscribe;
    this.addEvents();
  }

  Delegator.prototype.addEvents = function() {
    var event, functionName, sel, selector, _i, _ref, _ref1, _results;
    _ref = this.events;
    _results = [];
    for (sel in _ref) {
      functionName = _ref[sel];
      _ref1 = sel.split(' '), selector = 2 <= _ref1.length ? __slice.call(_ref1, 0, _i = _ref1.length - 1) : (_i = 0, []), event = _ref1[_i++];
      _results.push(this.addEvent(selector.join(' '), event, functionName));
    }
    return _results;
  };

  Delegator.prototype.addEvent = function(bindTo, event, functionName) {
    var closure, isBlankSelector,
      _this = this;
    closure = function() {
      return _this[functionName].apply(_this, arguments);
    };
    isBlankSelector = typeof bindTo === 'string' && bindTo.replace(/\s+/g, '') === '';
    if (isBlankSelector) {
      bindTo = this.element;
    }
    if (typeof bindTo === 'string') {
      this.element.delegate(bindTo, event, closure);
    } else {
      if (this.isCustomEvent(event)) {
        this.subscribe(event, closure);
      } else {
        $(bindTo).bind(event, closure);
      }
    }
    return this;
  };

  Delegator.prototype.isCustomEvent = function(event) {
    event = event.split('.')[0];
    return $.inArray(event, Delegator.natives) === -1;
  };

  Delegator.prototype.publish = function() {
    this.element.triggerHandler.apply(this.element, arguments);
    return this;
  };

  Delegator.prototype.subscribe = function(event, callback) {
    var closure;
    closure = function() {
      return callback.apply(this, [].slice.call(arguments, 1));
    };
    closure.guid = callback.guid = ($.guid += 1);
    this.element.bind(event, closure);
    return this;
  };

  Delegator.prototype.unsubscribe = function() {
    this.element.unbind.apply(this.element, arguments);
    return this;
  };

  return Delegator;

})();

Delegator.natives = (function() {
  var key, specials, val;
  specials = (function() {
    var _ref, _results;
    _ref = jQuery.event.special;
    _results = [];
    for (key in _ref) {
      if (!__hasProp.call(_ref, key)) continue;
      val = _ref[key];
      _results.push(key);
    }
    return _results;
  })();
  return "blur focus focusin focusout load resize scroll unload click dblclick\nmousedown mouseup mousemove mouseover mouseout mouseenter mouseleave\nchange select submit keydown keypress keyup error".split(/[^a-z]+/).concat(specials);
})();