-
Notifications
You must be signed in to change notification settings - Fork 159
/
event.js
180 lines (145 loc) · 5.22 KB
/
event.js
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/**
Event
=====
A good old fashioned event handling system.
*/
xui.events = {}; var cache = {};
xui.extend({
/**
on
--
Registers a callback function to a DOM event on the element collection.
For more information see:
- http://developer.apple.com/webapps/docs/documentation/AppleApplications/Reference/SafariWebContent/HandlingEvents/chapter_7_section_1.html#//apple_ref/doc/uid/TP40006511-SW1
### syntax ###
x$('button').on( 'click', function(e){ alert('hey that tickles!') });
or...
x$('a.save').click(function(e){ alert('tee hee!') });
### arguments ###
- type:string the event to subscribe to click|load|etc
- fn:function a callback function to execute when the event is fired
### example ###
x$(window).load(function(e){
x$('.save').touchstart( function(evt){ alert('tee hee!') }).css(background:'grey');
});
*/
/*on: function(type, fn) {
return this.each(function(el) {
if (window.addEventListener) {
el.addEventListener(type, fn, false);
}
});
},*/
on: function(type, fn, details) {
return this.each(function (el) {
if (xui.events[type]) {
var id = _getEventID(el),
responders = _getRespondersForEvent(id, type);
details = details || {};
details.handler = function (event, data) {
xui.fn.fire.call(xui(this), type, data);
};
// trigger the initialiser - only happens the first time around
if (!responders.length) {
xui.events[type].call(el, details);
}
}
el.addEventListener(type, _createResponder(el, type, fn), false);
});
},
un: function(type, fn) {
return this.each(function (el) {
var id = _getEventID(el), responders = _getRespondersForEvent(id, type), i = responders.length;
while (i--) {
if (fn === undefined || fn.guid === responders[i].guid) {
el.removeEventListener(type, responders[i], false);
removex(cache[id][type], i, 1);
}
}
if (cache[id][type].length === 0) delete cache[id][type];
for (var t in cache[id]) {
return;
}
delete cache[id];
});
},
fire: function (type, data) {
return this.each(function (el) {
if (el == document && !el.dispatchEvent)
el = document.documentElement;
var event = document.createEvent('HTMLEvents');
event.initEvent(type, true, true);
event.data = data || {};
event.eventName = type;
el.dispatchEvent(event);
});
}
// --
});
/**
Events
------
A good new skool fashioned event handling system.
- click
- load
- touchstart
- touchmove
- touchend
- touchcancel
- gesturestart
- gesturechange
- gestureend
- orientationchange
*/
"click load submit touchstart touchmove touchend touchcancel gesturestart gesturechange gestureend orientationchange".split(' ').forEach(function (event) {
xui.fn[event] = function(action) { return function (fn) { return fn ? this.on(action, fn) : this.fire(action); }; }(event);
});
// patched orientation support - Andriod 1 doesn't have native onorientationchange events
xui(window).on('load', function() {
if (!('onorientationchange' in document.body)) {
(function () {
var w = window.innerWidth, h = window.innerHeight;
xui(window).on('resize', function () {
var portraitSwitch = (window.innerWidth < w && window.innerHeight > h) && (window.innerWidth < window.innerHeight),
landscapeSwitch = (window.innerWidth > w && window.innerHeight < h) && (window.innerWidth > window.innerHeight);
if (portraitSwitch || landscapeSwitch) {
window.orientation = portraitSwitch ? 0 : 90; // what about -90? Some support is better than none
xui('body').fire('orientationchange'); // will this bubble up?
w = window.innerWidth;
h = window.innerHeight;
}
});
})();
}
});
// this doesn't belong on the prototype, it belongs as a property on the xui object
xui.touch = (function () {
try{
return !!(document.createEvent("TouchEvent").initTouchEvent)
} catch(e) {
return false;
};
})();
// lifted from Prototype's (big P) event model
function _getEventID(element) {
if (element._xuiEventID) return element._xuiEventID;
return element._xuiEventID = ++_getEventID.id;
}
_getEventID.id = 1;
function _getRespondersForEvent(id, eventName) {
var c = cache[id] = cache[id] || {};
return c[eventName] = c[eventName] || [];
}
function _createResponder(element, eventName, handler) {
var id = _getEventID(element), r = _getRespondersForEvent(id, eventName);
var responder = function(event) {
if (handler.call(element, event) === false) {
event.preventDefault();
event.stopPropagation();
}
};
responder.guid = handler.guid = handler.guid || ++_getEventID.id;
responder.handler = handler;
r.push(responder);
return responder;
}