Permalink
Browse files

Autoinit: Add wrapper to $.widget to inject default initSelector to w…

…idgets, add enhanceWithin function to helpers
  • Loading branch information...
arschmitz committed Jul 30, 2013
1 parent 36bb67f commit 0e61e08a9f7fd4543a1ed1c2d7b7b75a80b53c8c
Showing with 107 additions and 25 deletions.
  1. +78 −25 js/jquery.mobile.helpers.js
  2. +2 −0 js/jquery.ui.widget.js
  3. +27 −0 js/widgets/page.js
View
@@ -127,42 +127,95 @@ define( [ "jquery", "./jquery.mobile.ns", "./jquery.ui.core", "json!../package.j
}
});
$.fn.removeWithDependents = function() {
$.removeWithDependents( this );
};
$.removeWithDependents = function( elem ) {
var $elem = $( elem );
( $elem.jqmData( "dependents" ) || $() ).remove();
$elem.remove();
};
$.fn.addDependents = function( newDependents ) {
$.addDependents( this , newDependents );
};
$.addDependents = function( elem, newDependents ) {
var $elem = $( elem ),
dependents = $elem.jqmData( "dependents" ) || $();
$elem.jqmData( "dependents", $( dependents ).add( newDependents ) );
};
// plugins
$.fn.extend({
removeWithDependents: function() {
$.removeWithDependents( this );
},
// note that this helper doesn't attempt to handle the callback
// or setting of an html element's text, its only purpose is
// to return the html encoded version of the text in all cases. (thus the name)
$.fn.getEncodedText = function() {
return $( "<a>" ).text( $( this ).text() ).html();
};
//enhance child elements
enhanceWithin: function() {
var widgetElements,
that = this;
//Add no js class to elements
if( $.mobile.nojs ) {
$.mobile.nojs( this );
}
//bind links for ajax nav
if( $.mobile.links ) {
$.mobile.links( this );
}
//degrade inputs for styleing
if( $.mobile.degradeInputsWithin ){
$.mobile.degradeInputsWithin( this );
}
//run buttonmarkup
if( $.mobile.enhanceWithButtonMarkup ){
$( "a:jqmData(role='button'), .ui-bar > a, .ui-bar > :jqmData(role='controlgroup') > a, button", this ).each( $.mobile.enhanceWithButtonMarkup );
}
//add classes for fieldContain
if( $.fn.fieldcontain ) {
$( ":jqmData(role='fieldcontain')", this ).jqmEnhanceable().fieldcontain();
}
//enhance widgets
$.each( $.mobile.widgets, function( name, constructor ) {
//filter elements that should not be enhanced based on parents
widgetElements = $.mobile.enhanceable( that.find( constructor.initSelector ) );
//if any matching elements remain filter ones with keepNativeSelector
if ( widgetElements.length ) {
//$.mobile.page.prototype.keepNativeSelector is deprecated this is just for backcompt
//switch to $.mobile.keepNativeSelector in 1.5 which is just a value not a function
widgetElements = widgetElements.not( $.mobile.page.prototype.keepNativeSelector() );
}
//enhance whatever is left
widgetElements[ constructor.prototype.widgetName ]();
});
return this;
},
addDependents: function( newDependents ) {
$.addDependents( this , newDependents );
},
// note that this helper doesn't attempt to handle the callback
// or setting of an html element's text, its only purpose is
// to return the html encoded version of the text in all cases. (thus the name)
getEncodedText: function() {
return $( "<a>" ).text( $( this ).text() ).html();
},
// fluent helper function for the mobile namespaced equivalent
jqmEnhanceable: function() {
return $.mobile.enhanceable( this );
},
jqmHijackable: function() {
return $.mobile.hijackable( this );
}
});
$.removeWithDependents = function( nativeElement ) {
var element = $( nativeElement );
// fluent helper function for the mobile namespaced equivalent
$.fn.jqmEnhanceable = function() {
return $.mobile.enhanceable( this );
( element.jqmData( "dependents" ) || $() ).remove();
element.remove();
};
$.addDependents = function( nativeElement, newDependents ) {
var element = $( nativeElement ),
dependents = element.jqmData( "dependents" ) || $();
$.fn.jqmHijackable = function() {
return $.mobile.hijackable( this );
element.jqmData( "dependents", $( dependents ).add( newDependents ) );
};
$.find.matches = function( expr, set ) {
View
@@ -134,6 +134,8 @@ $.widget = function( name, base, prototype ) {
}
$.widget.bridge( name, constructor );
return constructor;
};
$.widget.extend = function( target ) {
View
@@ -6,6 +6,33 @@
define( [ "jquery", "../jquery.mobile.widget", "../jquery.mobile.core", "../jquery.mobile.registry" ], function( jQuery ) {
//>>excludeEnd("jqmBuildExclude");
(function( $, undefined ) {
$.mobile.widgets = {};
var originalWidget = $.widget;
$.widget = (function( orig ) {
return function() {
var constructor = orig.apply( this, arguments ),
name = constructor.prototype.widgetName;
constructor.initSelector = ( constructor.prototype.initSelector ? constructor.prototype.initSelector : ":jqmData(role='" + name + "')" );
$.mobile.widgets[ name ] = constructor;
return constructor;
};
})( $.widget );
//make sure $.widget still has bridge and extend methods
$.extend( $.widget, {
extend: originalWidget.extend,
bridge: originalWidget.bridge
});
//for backcompat remove in 1.5
$.mobile.document.on( "create", function( event ){
$.mobile.enhanceWithin( event.target );
});
$.widget( "mobile.page", {
options: {

0 comments on commit 0e61e08

Please sign in to comment.