Skip to content

Commit

Permalink
ENHANCEMENT Support for fake "redirects" in ajax responses via Histor…
Browse files Browse the repository at this point in the history
…y.replaceState() (used e.g. for an add form loading the record for editing afterwards, under its own URL)
  • Loading branch information
chillu committed Dec 20, 2011
1 parent 068fcb2 commit ac96375
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
15 changes: 10 additions & 5 deletions admin/javascript/LeftAndMain.Menu.js
Expand Up @@ -57,15 +57,20 @@

editPageItem[editPageItem.is('.current') ? 'show' : 'hide']();

// update the menu links to reflect the page ID if the page has changed the URL.
// Update the menu links to reflect the page ID if the page has changed the URL.
var currentID = $('.cms-content input[name=ID]').val();

// only handling the sub menu as the other items are global in context
var itemsWithPageContext = [
'#Menu-CMSPageAddController',
'#Menu-CMSPageSettingsController',
'#Menu-CMSPageHistoryController',
'#Menu-CMSPageEditController'
];
if(currentID) {
$(this).find("ul a").each(function(i, elem) {
var links = $(this).find(itemsWithPageContext.join(',')).find('a');
links.each(function(i, elem) {
var href = $(elem).attr("href").split('/');
href[href.length -1] = currentID;

// Assumes that current ID will always be the last URL segment (and not a query parameter)
$(elem).attr('href', href.join('/'));
});
}
Expand Down
14 changes: 13 additions & 1 deletion admin/javascript/LeftAndMain.js
Expand Up @@ -70,7 +70,13 @@
self.redraw();
});

$('.cms-edit-form').live('reloadeditform', function() {
$('.cms-edit-form').live('reloadeditform', function(e, data) {
// Simulates a redirect on an ajax response - just exchange the URL without re-requesting it
if(window.History.enabled) {
var url = data.xmlhttp.getResponseHeader('X-ControllerURL');
if(url) window.history.replaceState({}, '', url);
}

self.redraw()
});

Expand Down Expand Up @@ -222,6 +228,12 @@
self.redraw();
newContentEl.css('visibility', 'visible');
newContentEl.removeClass('loading');

// Simulates a redirect on an ajax response - just exchange the URL without re-requesting it
if(window.History.enabled) {
var url = xhr.getResponseHeader('X-ControllerURL');
if(url) window.history.replaceState({}, '', url);
}

self.trigger('afterstatechange', {data: data, status: status, xhr: xhr, element: newContentEl});
},
Expand Down

0 comments on commit ac96375

Please sign in to comment.