jquery-mobile/js/jquery.mobile.dialog.js
scottjehl 9b57c46349 Refactored urlStack and updated dialog and page plugins to match. jQuery Mobile's internal history now attempts to follow the history menu when urls change from hashchange, even for changes that go multiple steps forward or back. The internal history stack is now pruned based on whether a user goes back and then changes direction, whereas before a back-button click would result in a pop off the history. Instead we now maintain an active index number in the history stack, which allows us to maintain references to transitions that are saved on pages reached through clicking the forward button as well. This fixes #636.
In the process, some other small changes should be noted:
urlStack is now urlHistory, a hash of methods and properties used for history stack management (stack, activeIndex, getActive, getPrev, getNext, addNew, clearForward, and listening Enabled). All these are documented inline and exposed on $.mobile.urlHistory (I'm not sure these will be publicly documented, but just exposed internally for plugins for now).

$.changePage has two argument changes: the "back" argument is now called "reverse"; this results in no change from an end-user standpoint, but reflects the fact that it only reverses the direction of a transition without affecting the internal history stack, and second, a new argument at the end defines whether changePage was called from a hashChange which makes that url open to history menu guessing.
2011-01-23 17:33:36 -05:00

84 lines
No EOL
2.5 KiB
JavaScript

/*
* jQuery Mobile Framework : "dialog" plugin.
* Copyright (c) jQuery Project
* Dual licensed under the MIT (MIT-LICENSE.txt) and GPL (GPL-LICENSE.txt) licenses.
* Note: Code is in draft form and is subject to change
*/
(function($, undefined ) {
$.widget( "mobile.dialog", $.mobile.widget, {
options: {},
_create: function(){
var self = this,
$el = self.element,
$closeBtn = $('<a href="#" data-icon="delete" data-iconpos="notext">Close</a>'),
dialogClickHandler = function(e){
var $target = $(e.target);
// fixes issues with target links in dialogs breaking
// page transitions by reseting the active page below
if( $.mobile.path.isExternal( $target.closest("a").attr("href") ) ||
$target.closest("a[target]").length ||
$target.is( "[rel='external']" ) ) {
return;
}
if( e.type == "click" && ( $(e.target).closest('[data-back]')[0] || this==$closeBtn[0] ) ){
self.close();
return false;
}
//otherwise, assume we're headed somewhere new. set activepage to dialog so the transition will work
$.mobile.activePage = self.element;
};
// NOTE avoid click handler in the case of an external resource
// TODO add function in navigation to handle external check
$el.delegate("a", "click", dialogClickHandler);
$el.delegate("form", "submit", dialogClickHandler);
this.element
.bind("pageshow",function(){
self.thisPage = $.mobile.urlHistory.getActive();
self.prevPage = $.mobile.urlHistory.getPrev();
return false;
})
.bind("pagehide", function(){
$.mobile.urlHistory.stack = $.mobile.urlHistory.stack.slice(0,$.mobile.urlHistory.stack.length-2);
$.mobile.urlHistory.activeIndex = $.mobile.urlHistory.stack.length -1;
})
//add ARIA role
.attr("role","dialog")
.addClass('ui-page ui-dialog ui-body-a')
.find('[data-role=header]')
.addClass('ui-corner-top ui-overlay-shadow')
.prepend( $closeBtn )
.end()
.find('.ui-content:not([class*="ui-body-"])')
.addClass('ui-body-c')
.end()
.find('.ui-content,[data-role=footer]')
.last()
.addClass('ui-corner-bottom ui-overlay-shadow');
$(window).bind('hashchange',function(){
$.mobile.urlHistory.listeningEnabled = true;
if( $el.is('.ui-page-active') ){
self.close();
$el.bind('pagehide',function(){
$.mobile.urlHistory.listeningEnabled = false;
$.mobile.updateHash( self.prevPage.url );
});
}
});
},
close: function(){
$.mobile.changePage([this.element, $.mobile.activePage], this.thisPage.transition, true, true, true );
}
});
})( jQuery );