diff annotator_files/lib/class.js @ 3:6356e78ccf5c

new version contains Annotator JS files to be used with FilesystemSite.
author casties
date Thu, 05 Apr 2012 19:37:27 +0200
parents
children 6979313586cf
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/annotator_files/lib/class.js	Thu Apr 05 19:37:27 2012 +0200
@@ -0,0 +1,95 @@
+var Delegator,
+  __slice = Array.prototype.slice,
+  __hasProp = Object.prototype.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, _ref2, _results;
+    _ref = this.events;
+    _results = [];
+    for (sel in _ref) {
+      functionName = _ref[sel];
+      _ref2 = sel.split(' '), selector = 2 <= _ref2.length ? __slice.call(_ref2, 0, _i = _ref2.length - 1) : (_i = 0, []), event = _ref2[_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);
+})();