Blame view

framework/thirdparty/jquery-entwine/src/jquery.entwine.eventcapture.js 3.19 KB
0084d336   Administrator   Importers CRUD
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
  (function($) {
  
  	$.entwine.Namespace.addMethods({
  		bind_capture: function(selector, event, name, capture) {
  			var store  = this.captures || (this.captures = {});
  			var rulelists = store[event] || (store[event] = {});
  			var rulelist = rulelists[name] || (rulelists[name] = $.entwine.RuleList());
  
  			rule = rulelist.addRule(selector, event);
  			rule.handler = name;
  
  			this.bind_proxy(selector, name, capture);
  		}
  	});
  
  	var bindings = $.entwine.capture_bindings = {};
  
  	var event_proxy = function(event) {
  		return function(e) {
  			var namespace, capturelists, forevent, capturelist, rule, handler, sel;
  
  			for (var k in $.entwine.namespaces) {
  				namespace = $.entwine.namespaces[k];
  				capturelists = namespace.captures;
  
  				if (capturelists && (forevent = capturelists[event])) {
  					for (var k in forevent) {
  						var capturelist = forevent[k];
  						var triggered = namespace.$([]);
  
  						// Stepping through each selector from most to least specific
  						var j = capturelist.length;
  						while (j--) {
  							rule = capturelist[j];
  							handler = rule.handler;
  							sel = rule.selector.selector;
  
  							var matching = namespace.$(sel).not(triggered);
  							matching[handler].apply(matching, arguments);
  
  							triggered = triggered.add(matching);
  						}
  					}
  				}
  			}
  		}
  	};
  
  	var selector_proxy = function(selector, handler, includechildren) {
  		var matcher = $.selector(selector);
  		return function(e){
  			if (matcher.matches(e.target)) return handler.apply(this, arguments);
  		}
  	};
  
  	var document_proxy = function(selector, handler, includechildren) {
  		return function(e){
  			if (e.target === document) return handler.apply(this, arguments);
  		}
  	};
  
  	var window_proxy = function(selector, handler, includechildren) {
  		return function(e){
  			if (e.target === window) return handler.apply(this, arguments);
  		}
  	};
  
  	var property_proxy = function(property, handler, includechildren) {
  		var matcher;
  
  		return function(e){
  			var match = this['get'+property]();
  
  			if (typeof(match) == 'string') {
  				var matcher = (matcher && match == matcher.selector) ? matcher : $.selector(match);
  				if (matcher.matches(e.target)) return handler.apply(this, arguments);
  			}
  			else {
  				if ($.inArray(e.target, match) !== -1) return handler.apply(this, arguments);
  			}
  		}
  	};
  
  	$.entwine.Namespace.addHandler({
  		order: 10,
  
  		bind: function(selector, k, v) {
  			var match;
  			if ($.isPlainObject(v) && (match = k.match(/^from\s*(.*)/))) {
  				var from = match[1];
  				var proxyGen;
  
  				if (from.match(/[^\w]/)) proxyGen = selector_proxy;
  				else if (from == 'Window' || from == 'window') proxyGen = window_proxy;
  				else if (from == 'Document' || from == 'document') proxyGen = document_proxy;
  				else proxyGen = property_proxy;
  
  				for (var onevent in v) {
  					var handler = v[onevent];
  					match = onevent.match(/^on(.*)/);
  					var event = match[1];
  
  					this.bind_capture(selector, event, k + '_' + event, proxyGen(from, handler));
  
  					if (!bindings[event]) {
  						var namespaced = event.replace(/(\s+|$)/g, '.entwine$1');
  						bindings[event] = event_proxy(event);
  
  						$(proxyGen == window_proxy ? window : document).bind(namespaced, bindings[event]);
  					}
  				}
  
  				return true;
  			}
  		}
  	});
  
  })(jQuery);