Skip to content
This repository was archived by the owner on Oct 8, 2021. It is now read-only.

Commit 0e61e08

Browse files
committed
Autoinit: Add wrapper to $.widget to inject default initSelector to widgets, add enhanceWithin function to helpers
1 parent 36bb67f commit 0e61e08

File tree

3 files changed

+107
-25
lines changed

3 files changed

+107
-25
lines changed

js/jquery.mobile.helpers.js

Lines changed: 78 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -127,42 +127,95 @@ define( [ "jquery", "./jquery.mobile.ns", "./jquery.ui.core", "json!../package.j
127127
}
128128
});
129129

130-
$.fn.removeWithDependents = function() {
131-
$.removeWithDependents( this );
132-
};
133-
134-
$.removeWithDependents = function( elem ) {
135-
var $elem = $( elem );
136-
137-
( $elem.jqmData( "dependents" ) || $() ).remove();
138-
$elem.remove();
139-
};
140-
141-
$.fn.addDependents = function( newDependents ) {
142-
$.addDependents( this , newDependents );
143-
};
130+
144131

145132
$.addDependents = function( elem, newDependents ) {
146133
var $elem = $( elem ),
147134
dependents = $elem.jqmData( "dependents" ) || $();
148135

149136
$elem.jqmData( "dependents", $( dependents ).add( newDependents ) );
150137
};
138+
// plugins
139+
$.fn.extend({
140+
removeWithDependents: function() {
141+
$.removeWithDependents( this );
142+
},
151143

152-
// note that this helper doesn't attempt to handle the callback
153-
// or setting of an html element's text, its only purpose is
154-
// to return the html encoded version of the text in all cases. (thus the name)
155-
$.fn.getEncodedText = function() {
156-
return $( "<a>" ).text( $( this ).text() ).html();
157-
};
144+
//enhance child elements
145+
enhanceWithin: function() {
146+
var widgetElements,
147+
that = this;
148+
149+
//Add no js class to elements
150+
if( $.mobile.nojs ) {
151+
$.mobile.nojs( this );
152+
}
153+
//bind links for ajax nav
154+
if( $.mobile.links ) {
155+
$.mobile.links( this );
156+
}
157+
//degrade inputs for styleing
158+
if( $.mobile.degradeInputsWithin ){
159+
$.mobile.degradeInputsWithin( this );
160+
}
161+
//run buttonmarkup
162+
if( $.mobile.enhanceWithButtonMarkup ){
163+
$( "a:jqmData(role='button'), .ui-bar > a, .ui-bar > :jqmData(role='controlgroup') > a, button", this ).each( $.mobile.enhanceWithButtonMarkup );
164+
}
165+
//add classes for fieldContain
166+
if( $.fn.fieldcontain ) {
167+
168+
$( ":jqmData(role='fieldcontain')", this ).jqmEnhanceable().fieldcontain();
169+
}
170+
//enhance widgets
171+
$.each( $.mobile.widgets, function( name, constructor ) {
172+
//filter elements that should not be enhanced based on parents
173+
widgetElements = $.mobile.enhanceable( that.find( constructor.initSelector ) );
174+
//if any matching elements remain filter ones with keepNativeSelector
175+
if ( widgetElements.length ) {
176+
//$.mobile.page.prototype.keepNativeSelector is deprecated this is just for backcompt
177+
//switch to $.mobile.keepNativeSelector in 1.5 which is just a value not a function
178+
widgetElements = widgetElements.not( $.mobile.page.prototype.keepNativeSelector() );
179+
}
180+
//enhance whatever is left
181+
widgetElements[ constructor.prototype.widgetName ]();
182+
});
183+
184+
return this;
185+
},
186+
187+
addDependents: function( newDependents ) {
188+
$.addDependents( this , newDependents );
189+
},
190+
191+
// note that this helper doesn't attempt to handle the callback
192+
// or setting of an html element's text, its only purpose is
193+
// to return the html encoded version of the text in all cases. (thus the name)
194+
getEncodedText: function() {
195+
return $( "<a>" ).text( $( this ).text() ).html();
196+
},
197+
198+
// fluent helper function for the mobile namespaced equivalent
199+
jqmEnhanceable: function() {
200+
return $.mobile.enhanceable( this );
201+
},
202+
203+
jqmHijackable: function() {
204+
return $.mobile.hijackable( this );
205+
}
206+
});
207+
208+
$.removeWithDependents = function( nativeElement ) {
209+
var element = $( nativeElement );
158210

159-
// fluent helper function for the mobile namespaced equivalent
160-
$.fn.jqmEnhanceable = function() {
161-
return $.mobile.enhanceable( this );
211+
( element.jqmData( "dependents" ) || $() ).remove();
212+
element.remove();
162213
};
214+
$.addDependents = function( nativeElement, newDependents ) {
215+
var element = $( nativeElement ),
216+
dependents = element.jqmData( "dependents" ) || $();
163217

164-
$.fn.jqmHijackable = function() {
165-
return $.mobile.hijackable( this );
218+
element.jqmData( "dependents", $( dependents ).add( newDependents ) );
166219
};
167220

168221
$.find.matches = function( expr, set ) {

js/jquery.ui.widget.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@ $.widget = function( name, base, prototype ) {
134134
}
135135

136136
$.widget.bridge( name, constructor );
137+
138+
return constructor;
137139
};
138140

139141
$.widget.extend = function( target ) {

js/widgets/page.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,33 @@
66
define( [ "jquery", "../jquery.mobile.widget", "../jquery.mobile.core", "../jquery.mobile.registry" ], function( jQuery ) {
77
//>>excludeEnd("jqmBuildExclude");
88
(function( $, undefined ) {
9+
$.mobile.widgets = {};
10+
11+
var originalWidget = $.widget;
12+
13+
$.widget = (function( orig ) {
14+
return function() {
15+
var constructor = orig.apply( this, arguments ),
16+
name = constructor.prototype.widgetName;
17+
18+
constructor.initSelector = ( constructor.prototype.initSelector ? constructor.prototype.initSelector : ":jqmData(role='" + name + "')" );
19+
20+
$.mobile.widgets[ name ] = constructor;
21+
22+
return constructor;
23+
};
24+
})( $.widget );
25+
26+
//make sure $.widget still has bridge and extend methods
27+
$.extend( $.widget, {
28+
extend: originalWidget.extend,
29+
bridge: originalWidget.bridge
30+
});
31+
32+
//for backcompat remove in 1.5
33+
$.mobile.document.on( "create", function( event ){
34+
$.mobile.enhanceWithin( event.target );
35+
});
936

1037
$.widget( "mobile.page", {
1138
options: {

0 commit comments

Comments
 (0)