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
196 lines (175 loc) · 6.59 KB
/
default.js
File metadata and controls
196 lines (175 loc) · 6.59 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
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/**
* @add jQuery.event.special static
*/
steal.plugins('jquery/event').then(function($){
//cache default types for performance
var types = {}, rnamespaces= /\.(.*)$/;
/**
* @attribute default
* @parent specialevents
* @plugin jquery/event/default
* @download jquery/dist/jquery.event.default.js
* @test jquery/event/default/qunit.html
* Allows you to perform default actions as a result of an event.
* <p>
* Event based APIs are a powerful way of exposing functionality of your widgets. It also fits in
* quite nicely with how the DOM works.
* </p>
* <p>
* Like default events in normal functions (e.g. submitting a form), synthetic default events run after
* all event handlers have been triggered and no event handler has called
* preventDefault or returned false.
* </p>
* <p>To listen for a default event, just prefix the event with default.</p>
* @codestart
* $("div").bind("default.show", function(ev){ ... });
* $("ul").delegate("li","default.activate", function(ev){ ... });
* @codeend
* <p>
* The default plugin also adds the [jQuery.fn.triggerDefault triggerDefault] and [jQuery.fn.triggerDefaults triggerDefaults] methods. These are used to trigger
* an event and report back whether preventDefault was called on the event. The only difference is [jQuery.fn.triggerDefault triggerDefault]
* doesn't bubble.
* </p>
* <h2>Example</h2>
* <p>Lets look at how you could build a simple tabs widget with default events.
* First with just jQuery:</p>
* <p>
* Default events are useful in cases where you want to provide an event based
* API for users of your widgets. Users can simply listen to your synthetic events and
* prevent your default functionality by calling preventDefault.
* </p>
* <p>
* In the example below, the tabs widget provides a show event. Users of the
* tabs widget simply listen for show, and if they wish for some reason, call preventDefault
* to avoid showing the tab.
* </p>
* <p>
* In this case, the application developer doesn't want to show the second
* tab until the checkbox is checked.
* </p>
* @demo jquery/event/default/defaultjquery.html
* <p>Lets see how we would build this with JavaScriptMVC:</p>
* @demo jquery/event/default/default.html
*/
$.event.special["default"] = {
add: function( handleObj){
//save the type
types[handleObj.namespace.replace(rnamespaces,"")] = true;
//move the handler ...
var origHandler = handleObj.handler;
handleObj.origHandler = origHandler;
handleObj.handler = function(ev, data){
if(!ev._defaultActions) ev._defaultActions = [];
ev._defaultActions.push({element: this, handler: origHandler, event: ev, data: data, currentTarget: ev.currentTarget})
}
},
setup : function(){return true}
}
// overwrite trigger to allow default types
var oldTrigger = $.event.trigger;
$.event.trigger = function defaultTriggerer( event, data, elem, bubbling){
//always need to convert here so we know if we have default actions
var type = event.type || event
//should need to trigger just on this event
//shortcut if we never listened for a default of this type
//if(!types[type]){
// return oldTrigger.call($.event, event, data, elem, bubbling)
//}
if ( !bubbling ) {
event = typeof event === "object" ?
// jQuery.Event object
event[$.expando] ? event :
// Object literal
jQuery.extend( jQuery.Event(type), event ) :
// Just the event type (string)
jQuery.Event(type);
if ( type.indexOf("!") >= 0 ) {
event.type = type = type.slice(0, -1);
event.exclusive = true;
}
event._defaultActions = []; //set depth for possibly reused events
}
var defaultGetter = jQuery.Event("default."+event.type),
res;
$.extend(defaultGetter,{
target: elem,
_defaultActions: event._defaultActions,
exclusive : true
});
defaultGetter.stopPropagation();
//default events only work on elements
if(elem){
oldTrigger.call($.event, defaultGetter, [defaultGetter, data], elem, true);
}
//fire old trigger, this will call back here
res = oldTrigger.call($.event, event, data, elem, bubbling);
//fire if there are default actions to run &&
// we have not prevented default &&
// propagation has been stopped or we are at the document element
// we have reached the document
if (!event.isDefaultPrevented() &&
event._defaultActions &&
( ( event.isPropagationStopped() ) ||
( !elem.parentNode && !elem.ownerDocument ) )
) {
// put event back
event.namespace= event.type;
event.type = "default";
event.liveFired = null;
// call each event handler
for(var i = 0 ; i < event._defaultActions.length; i++){
var a = event._defaultActions[i];
event.currentTarget = a.currentTarget;
a.handler.call(a.element, event, a.data)
}
event._defaultActions = null; //set to null so everyone else on this element ignores it
}
}
/**
* @add jQuery.fn
*/
$.fn.
/**
* Triggers the event, stops the event from propagating through the DOM, and
* returns whether or not the event's default action was prevented.
* If true, the default action was not prevented. If false, the
* default action was prevented. This is the same as triggerDefaults, but
* the event doesn't bubble. Use these methods to easily determine if default was
* prevented, and proceed accordingly.
*
* <p>Widget developers might use this method to perform additional logic if an event
* handler doesn't prevent the default action. For example, a tabs widget might
* hide the currently shown tab if the application developer doesn't prevent default.</p>
* @param {Object} type The type of event to trigger.
* @param {Object} data Some data to pass to callbacks listening to this
* event.
*/
triggerDefault = function(type, data){
if ( this[0] ) {
var event = $.Event( type );
event.stopPropagation();
jQuery.event.trigger( event, data, this[0] );
return !event.isDefaultPrevented();
}
return true;
}
$.fn.
/**
* Triggers the event and returns whether or not the event's
* default action was prevented. If true, the default action was not
* prevented. If false, the default action was prevented. This is the same
* as triggerDefault, but the event bubbles. Use these methods to easily determine if default was
* prevented, and proceed accordingly.
* @param {Object} type The type of event to trigger.
* @param {Object} data Some data to pass to callbacks listening to this
* event.
*/
triggerDefaults = function(type, data){
if ( this[0] ) {
var event = $.Event( type );
jQuery.event.trigger( event, data, this[0] );
return !event.isDefaultPrevented();
}
return true;
}
});