Merge pull request #90 from mateusz/previews-by-id

Change the preview iframe to use injected SubsiteID GET/POST params.
This commit is contained in:
Sean Harvey 2013-05-25 18:29:59 -07:00
commit 1538e49e1b
2 changed files with 71 additions and 3 deletions

View File

@ -222,6 +222,29 @@ class SiteTreeSubsites extends DataExtension {
return $url;
}
/**
* Use the CMS domain for iframed CMS previews to prevent single-origin violations
* and SSL cert problems.
*/
function alternatePreviewLink($action = null) {
$url = Director::absoluteURL($this->owner->Link());
if($this->owner->SubsiteID) {
$url = HTTP::setGetVar('SubsiteID', $this->owner->SubsiteID, $url);
}
return $url;
}
/**
* Inject the subsite ID into the content so it can be used by frontend scripts.
*/
function MetaTags(&$tags) {
if($this->owner->SubsiteID) {
$tags .= "<meta name=\"x-subsite-id\" content=\"" . $this->owner->SubsiteID . "\" />\n";
}
return $tags;
}
function augmentSyncLinkTracking() {
// Set LinkTracking appropriately
$links = HTTP::getLinksIn($this->owner->Content);

View File

@ -57,4 +57,49 @@
});
});
$.entwine('ss.preview', function($){
$('.cms-preview').entwine({
/**
* Update links and forms with GET/POST SubsiteID param, so we remaing on the current subsite.
* The initial link for the iframe comes from SiteTreeSubsites::alternatePreviewLink.
*
* This is done so we can use the CMS domain for displaying previews so we prevent single-origin
* violations and SSL cert problems that come up when iframing content from a different URL.
*/
onafterIframeAdjustedForPreview: function(event, doc) {
var subsiteId = $(doc).find('meta[name=x-subsite-id]').attr('content');
if (!subsiteId) return;
// Inject the SubsiteID into internal links.
$(doc).find('a').each(function() {
var href = $(this).attr('href');
if (!href.match(/^http:\/\//)) {
$(this).attr('href', $.path.addSearchParams(href, {
'SubsiteID': subsiteId
}));
}
});
// Inject the SubsiteID as a hidden input into all forms submitting to the local site.
$(doc).find('form').each(function() {
if (!$(this).attr('action').match(/^http:\/\//)) {
$(doc).find('form').append('<input type=hidden name="SubsiteID" value="' + subsiteId + '" >');
}
});
}
});
});
})(jQuery);