-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevents.js
113 lines (94 loc) · 2.96 KB
/
events.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
Coltan.Events = {}
/**
* Pass an existing `Backbone.Events`-based object or create a new one.
* Assign it methods within the `createProxy` closure so they retain access
* to the proxy object even when bound to other objects as event handlers.
*
* This is probably ridiculously inefficient - better to create the closure
* only around the `handle` function and assign the remaining ones as methods
* on the prototype.
*/
Coltan.Events.createProxy = function(proxy){
if(!proxy){
proxy = {};
_.extend(proxy,Backbone.Events);
};
/**
* Handle an event by proxying it to the `proxy`
* object
*/
proxy.handle = function(event){
proxy.trigger(event.type,event);
};
/**
* Bind the `proxy.handle` function to an elements for
* a list of event names. If no list is supplied and
* the `proxy` object has an `events` property (i.e.,
* it's a `View` instance), use only the ones included there
* (the first word of each key). If neither is available, use
* all of them.
*
* **TO DO:** *selectively* bind the handler to UI
* elements matching selectors
*/
proxy.handleElement = function(element,eventNames){
if(!eventNames && proxy.events){
eventNames = _(proxy.events).map(function(method,evName){
return evName.split(' ')[0];
});
} else if(!eventNames){
eventNames = ALL_EVENT_NAMES;
}
_(eventNames).each(function(eventName){
element.addEventListener(eventName,proxy.handle);
});
return element;
};
/**
* Create a special handler for to bind in special situations:
*
* ```
* button.addEventListener('click',proxy.createSpecialHandler({type:'buttonClick'}));
* ```
*
* Will send a `buttonClick` event to the `proxy` object instead of a `click`.
*/
proxy.createSpecialHandler = function(options){
return function(event){
event.handlerOptions = options;
proxy.trigger(options.type || event.type,event);
}
};
/**
* Convinience method for creating a UI element and assigning it
* to the event proxy
*/
proxy.createHandledElement = function(tagName,attributes,eventNames){
return handleElement(createByTagName(tagName,properties),eventNames);
};
/**
* The central delegate method, binds hanlder methods
* to proxied events
*
* **TO DO:**
* - Rename
* - Smarter selectors (className);
*/
proxy.handleDelegate = function(selector,eventName,method){
proxy.bind(eventName,function(event){
Ti.API.debug(event);
if(event.source && event.source.name === selector) method(event);
});
};
return proxy;
};
/**
* All the event names, for when it's important to catch everything?
*/
var ALL_EVENT_NAMES = Coltan.Events._ALL_EVENT_NAMES = [
'blur','change','click','close','complete',
'dblclick','doubletap','focus',
'open','regionChange','return','selected',
'singletap','swipe','touchcancel','touchend',
'touchmove','touchstart','twofingertap'
];