2008-07-09 11:09:40 +00:00
|
|
|
/*
|
|
|
|
* jQuery UI History
|
|
|
|
*
|
|
|
|
* Copyright (c) 2008 Klaus Hartl (stilbuero.de)
|
|
|
|
* Dual licensed under the MIT (MIT-LICENSE.txt)
|
|
|
|
* and GPL (GPL-LICENSE.txt) licenses.
|
|
|
|
*
|
|
|
|
* http://docs.jquery.com/UI/History
|
|
|
|
*
|
2008-07-10 02:50:51 +00:00
|
|
|
* Depends:
|
|
|
|
* ui.core.js
|
2008-07-09 11:09:40 +00:00
|
|
|
*/
|
2008-07-10 02:50:51 +00:00
|
|
|
(function($) {
|
2008-07-09 11:09:40 +00:00
|
|
|
|
2008-07-10 02:50:51 +00:00
|
|
|
// TODO lazy loading singleton
|
|
|
|
$.ui.hmanager = new function() {
|
|
|
|
var states = {}, def = function() {};
|
2008-07-09 11:09:40 +00:00
|
|
|
|
2008-07-10 02:50:51 +00:00
|
|
|
var $window = $(window), hash = location.hash;
|
2008-07-09 11:09:40 +00:00
|
|
|
|
2008-07-10 02:50:51 +00:00
|
|
|
function getState() {
|
|
|
|
return hash.replace('#', '');
|
|
|
|
}
|
|
|
|
|
|
|
|
var iframe;
|
|
|
|
// var keepHistoryIn = iframe || window;
|
|
|
|
|
|
|
|
return {
|
2008-07-09 11:09:40 +00:00
|
|
|
|
2008-07-10 02:50:51 +00:00
|
|
|
enable: function() {
|
|
|
|
|
|
|
|
if ($.browser.msie && parseInt($.browser.version) < 8) {
|
|
|
|
$(function() {
|
|
|
|
// create hidden iframe for hash change tracking
|
|
|
|
iframe = $('<iframe id="ui-history-iframe" style="display: none;"></iframe>').
|
|
|
|
prependTo(document.body)[0];
|
|
|
|
|
|
|
|
// create initial history entry
|
|
|
|
iframe.contentWindow.document.open();
|
|
|
|
iframe.contentWindow.document.close();
|
2008-07-09 11:09:40 +00:00
|
|
|
|
|
|
|
if (getState())
|
2008-07-10 02:50:51 +00:00
|
|
|
iframe.contentWindow.document.location.hash = getState();
|
|
|
|
|
2008-07-09 11:09:40 +00:00
|
|
|
});
|
2008-07-10 02:50:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$window.bind('hashchange', function(e) {
|
|
|
|
// Prevent IE 8 from fireing an event twice,
|
|
|
|
// one from true event, one from trigger...
|
|
|
|
if (!iframe && hash == location.hash || iframe && hash == iframe.contentWindow.document.location.hash)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if ($.browser.msie && parseInt($.browser.version) < 8) {
|
|
|
|
hash = iframe.contentWindow.document.location.hash;
|
2008-07-09 11:09:40 +00:00
|
|
|
}
|
2008-07-10 02:50:51 +00:00
|
|
|
else
|
|
|
|
hash = location.hash;
|
2008-07-09 11:09:40 +00:00
|
|
|
|
2008-07-10 02:50:51 +00:00
|
|
|
if (getState())
|
|
|
|
states[getState()]();
|
|
|
|
else
|
|
|
|
// TODO invoke default
|
|
|
|
;
|
|
|
|
});
|
2008-07-09 11:09:40 +00:00
|
|
|
|
2008-07-10 02:50:51 +00:00
|
|
|
if (!($.browser.msie && parseInt($.browser.version) >= 8)) {
|
|
|
|
setInterval(
|
|
|
|
($.browser.msie ?
|
|
|
|
function() {
|
|
|
|
if (hash != iframe.contentWindow.document.location.hash)
|
|
|
|
$window.trigger('hashchange');
|
|
|
|
} :
|
|
|
|
function() {
|
|
|
|
if (hash != location.hash)
|
|
|
|
$window.trigger('hashchange');
|
|
|
|
else
|
|
|
|
// Do the history.length check hack for Safari 2
|
|
|
|
;
|
|
|
|
}
|
|
|
|
)
|
|
|
|
, 200
|
|
|
|
);
|
2008-07-09 11:09:40 +00:00
|
|
|
}
|
2008-07-10 02:50:51 +00:00
|
|
|
},
|
2008-07-09 11:09:40 +00:00
|
|
|
|
2008-07-10 02:50:51 +00:00
|
|
|
add: function(state, handler) {
|
|
|
|
states[state] = handler;
|
|
|
|
},
|
2008-07-09 11:09:40 +00:00
|
|
|
|
2008-07-10 02:50:51 +00:00
|
|
|
go: function(state) {
|
|
|
|
if (state) {
|
|
|
|
if ($.browser.msie && parseInt($.browser.version) < 8) {
|
|
|
|
iframe.contentWindow.document.open();
|
|
|
|
iframe.contentWindow.document.close();
|
|
|
|
iframe.contentWindow.document.location.hash = state;
|
|
|
|
}
|
|
|
|
location.hash = state;
|
|
|
|
$window.trigger('hashchange');
|
|
|
|
}
|
|
|
|
else
|
|
|
|
console.log('TODO do default state');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
$.ui.history = function() {
|
|
|
|
var args = Array.prototype.slice.call(arguments, 1);
|
|
|
|
$.ui.hmanager[arguments[0]].apply($.ui.hmanager, args);
|
|
|
|
};
|
2008-07-09 11:09:40 +00:00
|
|
|
|
|
|
|
})(jQuery);
|