removed mass of duplication from core of functionality that was moved to init, fixed init tests

This commit is contained in:
John Bender 2011-03-22 00:26:24 -07:00
parent 7df9e245f4
commit 018609fa32
5 changed files with 35 additions and 242 deletions

View file

@ -11,7 +11,7 @@
//jQuery.mobile configurable options
$.extend( $.mobile, {
//namespace used framework-wide for data-attrs. Default is no namespace
ns: "",
@ -52,13 +52,13 @@
loadingMessage: "loading",
//configure meta viewport tag's content attr:
//note: this feature is deprecated in A4 in favor of adding
//note: this feature is deprecated in A4 in favor of adding
//the meta viewport element directly in the markup
metaViewportContent: "width=device-width, minimum-scale=1, maximum-scale=1",
//support conditions that must be met in order to proceed
//default enhanced qualifications are media query support OR IE 7+
gradeA: function(){
gradeA: function(){
return $.support.mediaquery || $.mobile.browser.ie && $.mobile.browser.ie >= 7;
},
@ -96,28 +96,31 @@
TAB: 9,
UP: 38,
WINDOWS: 91 // COMMAND
},
//scroll page vertically: scroll to 0 to hide iOS address bar, or pass a Y value
silentScroll: function( ypos ) {
ypos = ypos || 0;
// prevent scrollstart and scrollstop events
$.event.special.scrollstart.enabled = false;
setTimeout(function() {
window.scrollTo( 0, ypos );
$(document).trigger( "silentscroll", { x: 0, y: ypos });
},20);
setTimeout(function() {
$.event.special.scrollstart.enabled = true;
}, 150 );
}
});
//trigger mobileinit event - useful hook for configuring $.mobile settings before they're used
$( window.document ).trigger( "mobileinit" );
//support conditions
//if device support condition(s) aren't met, leave things as they are -> a basic, usable experience,
//otherwise, proceed with the enhancements
if ( !$.mobile.gradeA() ) {
return;
}
//mobile version of data() method
//treats namespaced data-attrs the same as non-namespaced ones
$.fn.mobileData = function( prop, value ){
var pUndef = prop === undefined,
vUndef = value === undefined;
if( pUndef || vUndef ){
var ret,
nsret;
@ -142,102 +145,8 @@
}
}
return ret;
}
}
};
//define vars for interal use
var $window = $(window),
$html = $( "html" ),
$head = $( "head" ),
//loading div which appears during Ajax requests
//will not appear if $.mobile.loadingMessage is false
$loader = $.mobile.loadingMessage ?
$( "<div class='ui-loader ui-body-a ui-corner-all'>" +
"<span class='ui-icon ui-icon-loading spin'></span>" +
"<h1>" + $.mobile.loadingMessage + "</h1>" +
"</div>" )
: undefined;
//add mobile, initial load "rendering" classes to docEl
$html.addClass( "ui-mobile ui-mobile-rendering" );
//define & prepend meta viewport tag, if content is defined
$.mobile.metaViewportContent ? $( "<meta>", { name: "viewport", content: $.mobile.metaViewportContent}).prependTo( $head ) : undefined;
//expose some core utilities
$.extend($.mobile, {
// turn on/off page loading message.
pageLoading: function ( done ) {
if ( done ) {
$html.removeClass( "ui-loading" );
} else {
if( $.mobile.loadingMessage ){
var activeBtn =$( "." + $.mobile.activeBtnClass ).first();
$loader
.appendTo( $.mobile.pageContainer )
//position at y center (if scrollTop supported), above the activeBtn (if defined), or just 100px from top
.css( {
top: $.support.scrollTop && $(window).scrollTop() + $(window).height() / 2 ||
activeBtn.length && activeBtn.offset().top || 100
} );
}
$html.addClass( "ui-loading" );
}
},
//scroll page vertically: scroll to 0 to hide iOS address bar, or pass a Y value
silentScroll: function( ypos ) {
ypos = ypos || 0;
// prevent scrollstart and scrollstop events
$.event.special.scrollstart.enabled = false;
setTimeout(function() {
window.scrollTo( 0, ypos );
$(document).trigger( "silentscroll", { x: 0, y: ypos });
},20);
setTimeout(function() {
$.event.special.scrollstart.enabled = true;
}, 150 );
},
// find and enhance the pages in the dom and transition to the first page.
initializePage: function(){
//find present pages
var $pages = $( "[data-" + $.mobile.ns + "role='page']" );
//add dialogs, set data-url attrs
$pages.add( "[data-" + $.mobile.ns + "role='dialog']" ).each(function(){
$(this).attr( "data-" + $.mobile.ns + "url", $(this).attr( "id" ));
});
//define first page in dom case one backs out to the directory root (not always the first page visited, but defined as fallback)
$.mobile.firstPage = $pages.first();
//define page container
$.mobile.pageContainer = $pages.first().parent().addClass( "ui-mobile-viewport" );
//cue page loading message
$.mobile.pageLoading();
// if hashchange listening is disabled or there's no hash deeplink, change to the first page in the DOM
if( !$.mobile.hashListeningEnabled || !$.mobile.path.stripHash( location.hash ) ){
$.mobile.changePage( $.mobile.firstPage, false, true, false, true );
}
// otherwise, trigger a hashchange to load a deeplink
else {
$window.trigger( "hashchange", [ true ] );
}
}
});
// Monkey-patching Sizzle to filter the :jqdata selector
var oldFind = jQuery.find;
@ -249,11 +158,4 @@
};
jQuery.extend( jQuery.find, oldFind );
//dom-ready inits
$($.mobile.initializePage);
//window load event
//hide iOS browser chrome on load
$window.load( $.mobile.silentScroll );
})( jQuery, this );

View file

@ -4,7 +4,7 @@
(function($){
var libName = "jquery.mobile.core.js",
setGradeA = function(value, version) {
setGradeA = function(value, version) {
$.support.mediaquery = value;
$.mobile.browser.ie = version;
},
@ -33,119 +33,5 @@
$.testHelper.reloadLib(libName);
ok($.mobile.gradeA());
});
test( "loading the core library triggers mobilinit on the document", function(){
expect( 1 );
$(window.document).bind('mobileinit', function(event){
ok(true);
});
$.testHelper.reloadLib(libName);
});
test( "enhancments are skipped when the browser is not grade A", function(){
setGradeA(false);
$.testHelper.reloadLib(libName);
//NOTE easiest way to check for enhancements, not the most obvious
ok(!$("html").hasClass("ui-mobile"));
});
test( "enhancments are added when the browser is grade A", function(){
setGradeA(true);
$.testHelper.reloadLib(libName);
ok($("html").hasClass("ui-mobile"));
});
//TODO lots of duplication
test( "pageLoading doesn't add the dialog to the page when loading message is false", function(){
$.testHelper.alterExtend({loadingMessage: false});
$.testHelper.reloadLib(libName);
$.mobile.pageLoading(false);
ok(!$(".ui-loader").length);
});
test( "pageLoading doesn't add the dialog to the page when done is passed as true", function(){
$.testHelper.alterExtend({loadingMessage: true});
$.testHelper.reloadLib(libName);
// TODO add post reload callback
$('.ui-loader').remove();
$.mobile.pageLoading(true);
ok(!$(".ui-loader").length);
});
test( "pageLoading adds the dialog to the page when done is true", function(){
$.testHelper.alterExtend({loadingMessage: true});
$.testHelper.reloadLib(libName);
$.mobile.pageLoading(false);
ok($(".ui-loader").length);
});
var metaViewportSelector = "head meta[name=viewport]",
setViewPortContent = function(value){
$(metaViewportSelector).remove();
$.testHelper.alterExtend({metaViewportContent: value});
$.testHelper.reloadLib(libName);
};
test( "meta view port element is added to head when defined on mobile", function(){
setViewPortContent("width=device-width");
same($(metaViewportSelector).length, 1);
});
test( "meta view port element not added to head when not defined on mobile", function(){
setViewPortContent(false);
same($(metaViewportSelector).length, 0);
});
var findFirstPage = function() {
return $("[ data-"+ $.mobile.ns + "role='page']").first();
};
test( "active page and start page should be set to the fist page in the selected set", function(){
$.testHelper.reloadLib(libName);
var firstPage = findFirstPage();
same($.mobile.firstPage[0], firstPage[0]);
same($.mobile.activePage[0], firstPage[0]);
});
test( "mobile viewport class is defined on the first page's parent", function(){
var firstPage = findFirstPage();
$.testHelper.reloadLib(libName);
ok(firstPage.parent().hasClass('ui-mobile-viewport'));
});
test( "mobile page container is the first page's parent", function(){
var firstPage = findFirstPage();
$.testHelper.reloadLib(libName);
same($.mobile.pageContainer[0], firstPage.parent()[0]);
});
test( "page loading is called on document ready", function(){
$.testHelper.alterExtend({ pageLoading: function(){
start();
ok("called");
}});
stop();
$.testHelper.reloadLib(libName);
});
test( "hashchange triggered on document ready with single argument: true", function(){
$(window).bind("hashchange", function(ev, arg){
same(arg, true);
start();
});
stop();
$.testHelper.reloadLib(libName);
});
});
})(jQuery);

View file

@ -10,7 +10,6 @@
<script type="text/javascript" src="../../../external/qunit.js"></script>
<script type="text/javascript" src="../../../js/"></script>
<!-- added explicitly for library reloading (see testHelper ) -->
<script type="text/javascript" src="../../../js/jquery.mobile.core.js"></script>
<script type="text/javascript" src="../../jquery.testHelper.js"></script>
<link rel="stylesheet" href="../../../themes/default" />
<link rel="stylesheet" href="../../../external/qunit.css" type="text/css"/>

View file

@ -11,8 +11,8 @@
<script type="text/javascript" src="../../jquery.testHelper.js"></script>
<script type="text/javascript" src="init_core.js"></script>
<!-- added explicitly for library reloading (see testHelper ) -->
<script type="text/javascript" src="../../../js/jquery.mobile.init.js"></script>
<script type="text/javascript" src="../../../js/jquery.mobile.core.js"></script>
<script type="text/javascript" src="../../../js/jquery.mobile.init.js"></script>
<link rel="stylesheet" href="../../../themes/default" />
<link rel="stylesheet" href="../../../external/qunit.css" type="text/css"/>
</head>

View file

@ -6,8 +6,15 @@
mobileSelect = undefined,
metaViewportContentDefault = $.mobile.metaViewportContent,
libName = 'jquery.mobile.init.js',
coreLib = 'jquery.mobile.core.js',
extendFn = $.extend,
setGradeA = function(value) { $.mobile.gradeA = function(){ return value; }; },
extendFn = $.extend;
reloadCoreNSandInit = function(){
$.testHelper.reloadLib(coreLib);
$.testHelper.reloadLib("jquery.setNamespace.js");
$.testHelper.reloadLib(libName);
};
module(libName, {
setup: function(){
@ -35,6 +42,7 @@
mobileSelect = $.mobile.selectmenu;
});
// NOTE for the following two tests see index html for the binding
test( "mobile.page is available when mobile init is fired", function(){
ok(mobilePage !== undefined, "$.mobile.page is defined");
});
@ -172,11 +180,9 @@
}, 500);
});
var coreLib = 'jquery.mobile.core.js';
asyncTest( "pageLoading adds the loading class to the page when done is false", function(){
$.testHelper.alterExtend({loadingMessage: true});
$.testHelper.reloadLib(coreLib);
reloadCoreNSandInit();
$.mobile.pageLoading(false);
setTimeout(function(){
@ -186,7 +192,7 @@
});
asyncTest( "page loading should contain default loading message", function(){
$.testHelper.reloadLib(coreLib);
reloadCoreNSandInit();
$.mobile.pageLoading(false);
setTimeout(function(){
@ -200,7 +206,7 @@
$.mobile.loadingMessage = "foo";
});
$.testHelper.reloadLib(coreLib);
reloadCoreNSandInit();
$.mobile.pageLoading(false);
setTimeout(function(){