forked from bitovi/jquerypp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdefault.js
More file actions
119 lines (107 loc) · 3.41 KB
/
default.js
File metadata and controls
119 lines (107 loc) · 3.41 KB
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
steal('jquery', function($){
/**
* @function jQuery.fn.triggerAsync
* @parent jQuery.event.pause
* @plugin jquerypp/event/default
*
* `jQuery.fn.triggerAsync(type, [data], [success], [prevented]` triggers an event and calls success
* when the event has finished propagating through the DOM and no other handler
* called `event.preventDefault()` or returned `false`.
*
* $('#panel').triggerAsync('show', function() {
* $('#panel').show();
* });
*
* You can also provide a callback that gets called if `event.preventDefault()` was called on the event:
*
* $('panel').triggerAsync('show', function(){
* $('#panel').show();
* },function(){
* $('#other').addClass('error');
* });
*
* @param {String} type The type of event
* @param {Object} data The data for the event
* @param {Function} success a callback function which occurs upon success
* @param {Function} prevented a callback function which occurs if preventDefault was called
*/
$.fn.triggerAsync = function(type, data, success, prevented){
if(typeof data == 'function'){
prevented=success;
success = data;
data = undefined;
}
if ( this.length ) {
var el=this;
// Trigger the event with the success callback as the success handler
// when triggerAsync called within another triggerAsync,it's the same tick time so we should use timeout
// http://javascriptweblog.wordpress.com/2010/06/28/understanding-javascript-timers/
setTimeout(function(){
el.trigger( {type: type, _success: success,_prevented:prevented}, data);
},0);
} else{
// If we have no elements call the success callback right away
if(success)
success.call(this);
}
return this;
}
/**
* @add jQuery.event.special
*/
//cache default types for performance
var types = {}, rnamespaces= /\.(.*)$/, $event = $.event;
/**
* @attribute default
* @parent specialevents
* @plugin jquerypp/event/default
* @download http://jmvcsite.heroku.com/pluginify?plugins[]=jquerypp/event/default/default.js
* @test jquerypp/event/default/qunit.html
*
*/
$event.special["default"] = {
add: function( handleObj ) {
//save the type
types[handleObj.namespace.replace(rnamespaces,"")] = true;
},
setup: function() {return true}
}
// overwrite trigger to allow default types
var oldTrigger = $event.trigger;
$event.trigger = function defaultTriggerer( event, data, elem, onlyHandlers){
// Event object or event type
var type = event.type || event,
// Caller can pass in an Event, Object, or just an event type string
event = typeof event === "object" ?
// jQuery.Event object
event[ $.expando ] ? event :
// Object literal
new $.Event( type, event ) :
// Just the event type (string)
new $.Event( type),
res=oldTrigger.call($.event,event, data, elem, onlyHandlers),
paused=event.isPaused && event.isPaused();
if(!onlyHandlers && !event.isDefaultPrevented() && event.type.indexOf("default") !== 0) {
// Trigger the default. event
oldTrigger("default."+event.type, data, elem)
if(event._success){
event._success(event)
}
}
if(!onlyHandlers && event.isDefaultPrevented() && event.type.indexOf("default") !== 0 && !paused ){
if(event._prevented){
event._prevented(event);
}
}
// code for paused
if( paused ){
// set back original stuff
event.isDefaultPrevented =
event.pausedState.isDefaultPrevented;
event.isPropagationStopped =
event.pausedState.isPropagationStopped;
}
return res;
};
return $;
});