comparison 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
comparison
equal deleted inserted replaced
2:4c6c8835fc5c 3:6356e78ccf5c
1 var Delegator,
2 __slice = Array.prototype.slice,
3 __hasProp = Object.prototype.hasOwnProperty;
4
5 Delegator = (function() {
6
7 Delegator.prototype.events = {};
8
9 Delegator.prototype.options = {};
10
11 Delegator.prototype.element = null;
12
13 function Delegator(element, options) {
14 this.options = $.extend(true, {}, this.options, options);
15 this.element = $(element);
16 this.on = this.subscribe;
17 this.addEvents();
18 }
19
20 Delegator.prototype.addEvents = function() {
21 var event, functionName, sel, selector, _i, _ref, _ref2, _results;
22 _ref = this.events;
23 _results = [];
24 for (sel in _ref) {
25 functionName = _ref[sel];
26 _ref2 = sel.split(' '), selector = 2 <= _ref2.length ? __slice.call(_ref2, 0, _i = _ref2.length - 1) : (_i = 0, []), event = _ref2[_i++];
27 _results.push(this.addEvent(selector.join(' '), event, functionName));
28 }
29 return _results;
30 };
31
32 Delegator.prototype.addEvent = function(bindTo, event, functionName) {
33 var closure, isBlankSelector,
34 _this = this;
35 closure = function() {
36 return _this[functionName].apply(_this, arguments);
37 };
38 isBlankSelector = typeof bindTo === 'string' && bindTo.replace(/\s+/g, '') === '';
39 if (isBlankSelector) bindTo = this.element;
40 if (typeof bindTo === 'string') {
41 this.element.delegate(bindTo, event, closure);
42 } else {
43 if (this.isCustomEvent(event)) {
44 this.subscribe(event, closure);
45 } else {
46 $(bindTo).bind(event, closure);
47 }
48 }
49 return this;
50 };
51
52 Delegator.prototype.isCustomEvent = function(event) {
53 event = event.split('.')[0];
54 return $.inArray(event, Delegator.natives) === -1;
55 };
56
57 Delegator.prototype.publish = function() {
58 this.element.triggerHandler.apply(this.element, arguments);
59 return this;
60 };
61
62 Delegator.prototype.subscribe = function(event, callback) {
63 var closure;
64 closure = function() {
65 return callback.apply(this, [].slice.call(arguments, 1));
66 };
67 closure.guid = callback.guid = ($.guid += 1);
68 this.element.bind(event, closure);
69 return this;
70 };
71
72 Delegator.prototype.unsubscribe = function() {
73 this.element.unbind.apply(this.element, arguments);
74 return this;
75 };
76
77 return Delegator;
78
79 })();
80
81 Delegator.natives = (function() {
82 var key, specials, val;
83 specials = (function() {
84 var _ref, _results;
85 _ref = jQuery.event.special;
86 _results = [];
87 for (key in _ref) {
88 if (!__hasProp.call(_ref, key)) continue;
89 val = _ref[key];
90 _results.push(key);
91 }
92 return _results;
93 })();
94 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);
95 })();