Refactored sections JS to use widget factory scheme; continued work on fetchlinks/inline loader.

This commit is contained in:
Mat Marquis 2011-12-07 19:49:54 -05:00
parent 612d1be179
commit f02ad3d67b
4 changed files with 103 additions and 92 deletions

View file

@ -18,10 +18,9 @@
<div data-role="header" data-theme="d">
<h1>Dialog</h1>
</div>
<div data-role="content" data-theme="c" class="test">
<div data-role="content" data-theme="c">
<h1>Delete page?</h1>
<p>This is a regular page, styled as a dialog. To create a dialog, just link to a normal page and include a transition and <code>data-rel="dialog"</code> attribute.</p>
<a href="docs-dialogs.html" data-role="button" data-rel="back" data-theme="b">Sounds good</a>

View file

@ -26,9 +26,10 @@
<pre><code>&lt;a href=&quot;dialog.html&quot; <strong>data-rel=&quot;dialog&quot;</strong>&gt;Link&lt;/a&gt;
</code></pre>
<p>Note: The below example temporary contains a <code>data-fragment</code> attribute.</p>
<p>A <a data-role="fetchlink" data-target=".quote" href="dialog.html" data-fragment=".test">fetch link</a> is created by adding the <code>data-target</code> attribute to a link. This tells the framework to <strong>not</strong> change pages and instead load the <code>href</code> into the <code>target</code> DOM element on the current page when the link is clicked. The target can be any jQuery selector.</p>
<p>A <a data-role="fetchlink" data-target=".quote" href="dialog.html">fetch link</a> is created by adding the <code>data-target</code> attribute to a link. This tells the framework to <strong>not</strong> change pages and instead load the <code>href</code> into the target DOM element on the current page when the link is clicked. The target can be any jQuery selector (or restrict to ID only?). </p>
>>>>>>> Refactored sections JS to use widget factory scheme; continued work on fetchlinks/inline loader.
<pre><code>&lt;a href=&quot;dialog.html&quot; <strong>data-target=&quot;.quote&quot;</strong>&gt;
</code></pre>
@ -61,14 +62,12 @@
<address>Arthur Conan Doyle</address>
<cite>Sherlock Holmes: The Sign of the Four</cite>
</blockquote>
<div class="quote" style="border: 2px solid red;">
<p>To be replaced.</p>
</div>
</div>
<div class="quote">
<p>To be replaced.</p>
</div>
</div>

View file

@ -5,19 +5,14 @@ $.widget( "mobile.fetchlink", $.mobile.widget, {
initSelector: ":jqmData(role='fetchlink')"
},
_create: function() {
// Prototyping.
// $( this.element.data( 'fragment' ) ).hide();
console.log( $(":jqmData(role='page')") );
$( this.element ).click(function() {
var el = $( this ),
url = el.attr( 'href' ),
url = el.attr( "href" ),
target = el.data( "target" ),
targetEl = target && $( target ) || el,
load = el.data( "fragment" ) || $(":jqmData(role='page')") /* Needs a proper default (page, most likely). */,
fragment = el.data( "fragment" ),
load = fragment || ":jqmData(role='page')",
threshold = screen.width > parseInt( el.data( "breakpoint" ) || 0 ),
methods = [ "append", "prepend", "replace", "before", "after" ],
method = "html",
@ -41,17 +36,21 @@ $.widget( "mobile.fetchlink", $.mobile.widget, {
});
$.get( url, function( data ) {
/* Swiped from the jQuery core; $.get() should really be replaced by .load() */
var rscript = /<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi,
responseEl = $( load ? $("<div/>").append( data.replace( rscript, "" ) ).find( load ) : data );
data = $( $("<div/>").append( data.replace( rscript, "" ) ).find( load ) )
responseEl = !fragment ? $( data.html() ) : data;
setTimeout(function() {
targetEl[ method ]( responseEl );
responseEl
.trigger( "create" )
.trigger( "fetchlink", { target : targetEl, data: responseEl } );
}, 1500);
responseEl
.find('[data-role="header"]')
.trigger( "create" )
}, 2000 );
});
}
@ -66,7 +65,6 @@ $( document ).bind( "inlineLoader", function( e ){
$( e.target ).html( "<div class='ui-loader-inline'><span class='ui-icon ui-icon-loading spin'></span></div>" );
});
//auto self-init widgets
$( document ).bind( "pagecreate create", function( e ){
$( $.mobile.fetchlink.prototype.options.initSelector, e.target ).fetchlink();

View file

@ -4,84 +4,99 @@
(function( $, undefined ) {
$.mobile.page.prototype.options.backBtnText = "Back";
$.mobile.page.prototype.options.addBackBtn = false;
$.mobile.page.prototype.options.backBtnTheme = null;
$.mobile.page.prototype.options.headerTheme = "a";
$.mobile.page.prototype.options.footerTheme = "a";
$.mobile.page.prototype.options.contentTheme = null;
$( document ).delegate( ":jqmData(role='page'), :jqmData(role='dialog')", "pagecreate", function( e ) {
$.widget( "mobile.sections", $.mobile.widget, {
options: {
initSelector : ":jqmData(role='page'), :jqmData(role='dialog')",
sectionSelector : ":jqmData(role='header'), :jqmData(role='footer'), :jqmData(role='content')",
backBtnText : "top",
addBackBtn : null,
backBtnTheme : ":jqmData(role='navbar')",
headerTheme : "a",
footerTheme : "a",
contentTheme : null
},
var $page = $( this ),
o = $page.data( "page" ).options,
pageRole = $page.jqmData( "role" ),
pageTheme = o.theme;
$( ":jqmData(role='header'), :jqmData(role='footer'), :jqmData(role='content')", this ).each(function() {
var $this = $( this ),
role = $this.jqmData( "role" ),
theme = $this.jqmData( "theme" ),
contentTheme = theme || o.contentTheme || ( pageRole === "dialog" && pageTheme ),
$headeranchors,
leftbtn,
rightbtn,
backBtn;
$this.addClass( "ui-" + role );
_create: function() {
var self = this;
//apply theming and markup modifications to page,header,content,footer
if ( role === "header" || role === "footer" ) {
var thisTheme = theme || ( role === "header" ? o.headerTheme : o.footerTheme ) || pageTheme;
$( self.options.initSelector ).each(function() {
var $page = $( this ),
o = self.options,
pageRole = $page.jqmData( "role" ),
pageTheme = o.theme;
$( o.sectionSelector, this ).each(function() {
var $this = $( this ),
role = $this.jqmData( "role" ),
theme = $this.jqmData( "theme" ),
contentTheme = theme || o.contentTheme || ( pageRole === "dialog" && pageTheme ),
$headeranchors,
leftbtn,
rightbtn,
backBtn;
$this.addClass( "ui-" + role );
$this
//add theme class
.addClass( "ui-bar-" + thisTheme )
// Add ARIA role
.attr( "role", role === "header" ? "banner" : "contentinfo" );
//apply theming and markup modifications to page,header,content,footer
if ( role === "header" || role === "footer" ) {
// Right,left buttons
$headeranchors = $this.children( "a" );
leftbtn = $headeranchors.hasClass( "ui-btn-left" );
rightbtn = $headeranchors.hasClass( "ui-btn-right" );
var thisTheme = theme || ( role === "header" ? o.headerTheme : o.footerTheme ) || pageTheme;
leftbtn = leftbtn || $headeranchors.eq( 0 ).not( ".ui-btn-right" ).addClass( "ui-btn-left" ).length;
rightbtn = rightbtn || $headeranchors.eq( 1 ).addClass( "ui-btn-right" ).length;
// Auto-add back btn on pages beyond first view
if ( o.addBackBtn &&
role === "header" &&
$( ".ui-page" ).length > 1 &&
$this.jqmData( "url" ) !== $.mobile.path.stripHash( location.hash ) &&
!leftbtn ) {
$this
//add theme class
.addClass( "ui-bar-" + thisTheme )
// Add ARIA role
.attr( "role", role === "header" ? "banner" : "contentinfo" );
backBtn = $( "<a href='#' class='ui-btn-left' data-"+ $.mobile.ns +"rel='back' data-"+ $.mobile.ns +"icon='arrow-l'>"+ o.backBtnText +"</a>" )
// If theme is provided, override default inheritance
.attr( "data-"+ $.mobile.ns +"theme", o.backBtnTheme || thisTheme )
.prependTo( $this );
}
// Right,left buttons
$headeranchors = $this.children( "a" );
leftbtn = $headeranchors.hasClass( "ui-btn-left" );
rightbtn = $headeranchors.hasClass( "ui-btn-right" );
// Page title
$this.children( "h1, h2, h3, h4, h5, h6" )
.addClass( "ui-title" )
// Regardless of h element number in src, it becomes h1 for the enhanced page
.attr({
"tabindex": "0",
"role": "heading",
"aria-level": "1"
});
leftbtn = leftbtn || $headeranchors.eq( 0 ).not( ".ui-btn-right" ).addClass( "ui-btn-left" ).length;
} else if ( role === "content" ) {
if ( contentTheme ) {
$this.addClass( "ui-body-" + ( contentTheme ) );
}
rightbtn = rightbtn || $headeranchors.eq( 1 ).addClass( "ui-btn-right" ).length;
// Add ARIA role
$this.attr( "role", "main" );
}
});
// Auto-add back btn on pages beyond first view
if ( o.addBackBtn &&
role === "header" &&
$( ".ui-page" ).length > 1 &&
$this.jqmData( "url" ) !== $.mobile.path.stripHash( location.hash ) &&
!leftbtn ) {
backBtn = $( "<a href='#' class='ui-btn-left' data-"+ $.mobile.ns +"rel='back' data-"+ $.mobile.ns +"icon='arrow-l'>"+ o.backBtnText +"</a>" )
// If theme is provided, override default inheritance
.attr( "data-"+ $.mobile.ns +"theme", o.backBtnTheme || thisTheme )
.prependTo( $this );
}
// Page title
$this.children( "h1, h2, h3, h4, h5, h6" )
.addClass( "ui-title" )
// Regardless of h element number in src, it becomes h1 for the enhanced page
.attr({
"tabindex": "0",
"role": "heading",
"aria-level": "1"
});
} else if ( role === "content" ) {
if ( contentTheme ) {
$this.addClass( "ui-body-" + ( contentTheme ) );
}
// Add ARIA role
$this.attr( "role", "main" );
}
});
});
}
});
$( document ).bind( "pagecreate create", function( e ) {
$( e.target ).sections();
});
})( jQuery );