2011-04-28 12:36:28 +02:00
|
|
|
(function($) {
|
|
|
|
$.entwine('ss', function($){
|
2011-05-15 05:43:21 +02:00
|
|
|
|
2011-07-15 10:35:41 +02:00
|
|
|
/**
|
2011-12-17 04:06:56 +01:00
|
|
|
* Shows a previewable website state alongside its editable version in backend UI,
|
|
|
|
* typically a page. This allows CMS users to seamlessly switch between preview and
|
|
|
|
* edit mode in the same browser window. The preview panel is embedded in the layout
|
|
|
|
* of the backend UI, and loads its content via an iframe.
|
2011-07-15 10:35:41 +02:00
|
|
|
*
|
|
|
|
* The admin UI itself is collapsible, leaving most screen space to this panel.
|
2011-12-17 04:06:56 +01:00
|
|
|
*
|
|
|
|
* Relies on the server responses to indicate if a preview URL is available for the
|
|
|
|
* currently loaded admin interface. If no preview is available, the panel is "blocked"
|
|
|
|
* automatically.
|
2011-07-15 10:35:41 +02:00
|
|
|
*
|
2011-07-21 20:14:33 +02:00
|
|
|
* Internal links within the preview iframe trigger a refresh of the admin panel as well,
|
|
|
|
* while all external links are disabled (via JavaScript).
|
2011-07-15 10:35:41 +02:00
|
|
|
*/
|
2011-07-05 14:34:31 +02:00
|
|
|
$('.cms-preview').entwine({
|
2011-04-28 12:36:28 +02:00
|
|
|
|
2011-05-15 05:43:21 +02:00
|
|
|
// Minimum width to keep the CMS operational
|
2011-05-02 01:43:51 +02:00
|
|
|
SharedWidth: null,
|
|
|
|
|
2012-06-14 23:44:22 +02:00
|
|
|
onadd: function() {
|
2011-04-28 12:36:28 +02:00
|
|
|
var self = this, layoutContainer = this.parent();
|
|
|
|
// this.resizable({
|
|
|
|
// handles: 'w',
|
|
|
|
// stop: function(e, ui) {
|
|
|
|
// $('.cms-container').layout({resize: false});
|
|
|
|
// }
|
|
|
|
// });
|
|
|
|
|
2011-05-02 01:43:51 +02:00
|
|
|
// TODO Compute dynamically
|
|
|
|
this.setSharedWidth(500);
|
|
|
|
|
2011-04-28 12:36:28 +02:00
|
|
|
// Create layout and controls
|
|
|
|
this.find('iframe').addClass('center');
|
2011-05-15 05:43:21 +02:00
|
|
|
this.find('iframe').bind('load', function() {
|
|
|
|
self._fixIframeLinks();
|
2011-12-19 15:06:04 +01:00
|
|
|
|
|
|
|
// Load edit view for new page, but only if the preview is activated at the moment.
|
|
|
|
// This avoids e.g. force-redirections of the edit view on RedirectorPage instances.
|
|
|
|
if(!self.is('.is-collapsed')) self.loadCurrentPage();
|
2011-05-15 05:43:21 +02:00
|
|
|
});
|
2011-04-28 12:36:28 +02:00
|
|
|
|
2011-05-15 05:43:21 +02:00
|
|
|
if(this.hasClass('is-expanded')) this.expand();
|
|
|
|
else this.collapse();
|
2012-05-16 15:32:25 +02:00
|
|
|
this.data('cms-preview-initialized', true);
|
2011-07-15 10:35:41 +02:00
|
|
|
|
|
|
|
// Preview might not be available in all admin interfaces - block/disable when necessary
|
2011-09-29 06:09:36 +02:00
|
|
|
this.append('<div class="cms-preview-overlay ui-widget-overlay-light"></div>');
|
|
|
|
this.find('.cms-preview-overlay-light').hide();
|
2012-01-03 14:42:26 +01:00
|
|
|
$('.cms-preview-toggle-link')[this.canPreview() ? 'show' : 'hide']();
|
2011-12-12 21:30:16 +01:00
|
|
|
|
|
|
|
self._fixIframeLinks();
|
2011-04-28 12:36:28 +02:00
|
|
|
|
|
|
|
this._super();
|
|
|
|
},
|
2012-05-14 01:43:36 +02:00
|
|
|
loadUrl: function(url) {
|
2011-04-28 12:36:28 +02:00
|
|
|
this.find('iframe').attr('src', url);
|
|
|
|
},
|
2011-07-21 20:14:33 +02:00
|
|
|
|
2012-06-12 12:48:08 +02:00
|
|
|
updateAfterXhr: function(){
|
|
|
|
$('.cms-preview-toggle-link')[this.canPreview() ? 'show' : 'hide']();
|
|
|
|
|
|
|
|
// Only load when panel is visible (see details in iframe load event handler).
|
|
|
|
if(this.is('.is-collapsed')) return;
|
|
|
|
|
|
|
|
// var url = ui.xmlhttp.getResponseHeader('x-frontend-url');
|
2012-08-28 18:31:15 +02:00
|
|
|
var url = $('.cms-edit-form').choosePreviewLink();
|
2012-06-12 12:48:08 +02:00
|
|
|
if(url) {
|
|
|
|
this.loadUrl(url);
|
|
|
|
this.unblock();
|
|
|
|
} else {
|
|
|
|
this.block();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
'from .cms-container': {
|
|
|
|
onaftersubmitform: function(){
|
|
|
|
this.updateAfterXhr();
|
|
|
|
},
|
|
|
|
onafterstatechange: function(){
|
|
|
|
this.updateAfterXhr();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
// Toggle preview when new menu entry is selected.
|
|
|
|
// Only do this when preview is actually shown,
|
|
|
|
// to avoid auto-expanding the menu in normal CMS mode
|
|
|
|
'from .cms-menu-list li': {
|
|
|
|
onselect: function(){
|
|
|
|
if(!this.hasClass('is-collapsed')) this.collapse();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2011-07-21 20:14:33 +02:00
|
|
|
/**
|
|
|
|
* Loads the matching edit form for a page viewed in the preview iframe,
|
|
|
|
* based on metadata sent along with this document.
|
|
|
|
*/
|
|
|
|
loadCurrentPage: function() {
|
2012-01-03 14:42:26 +01:00
|
|
|
var doc = this.find('iframe')[0].contentDocument, containerEl = this.getLayoutContainer();
|
2011-05-15 05:43:21 +02:00
|
|
|
|
2012-01-03 14:42:26 +01:00
|
|
|
if(!this.canPreview()) return;
|
2011-05-15 05:43:21 +02:00
|
|
|
|
|
|
|
// Load this page in the admin interface if appropriate
|
2011-12-17 04:06:56 +01:00
|
|
|
var id = $(doc).find('meta[name=x-page-id]').attr('content');
|
|
|
|
var editLink = $(doc).find('meta[name=x-cms-edit-link]').attr('content');
|
|
|
|
var contentPanel = $('.cms-content');
|
|
|
|
|
2011-06-09 03:49:52 +02:00
|
|
|
if(id && contentPanel.find(':input[name=ID]').val() != id) {
|
2011-12-17 04:06:56 +01:00
|
|
|
// Ignore behaviour without history support (as we need ajax loading
|
|
|
|
// for the new form to load in the background)
|
|
|
|
if(window.History.enabled)
|
|
|
|
$('.cms-container').loadPanel(editLink);
|
2011-06-09 03:49:52 +02:00
|
|
|
}
|
2011-05-15 05:43:21 +02:00
|
|
|
},
|
2012-01-03 14:42:26 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines if the current interface is capable of previewing its managed record.
|
|
|
|
*
|
|
|
|
* Returns: {boolean}
|
|
|
|
*/
|
|
|
|
canPreview: function() {
|
|
|
|
var contentEl = this.getLayoutContainer().find('.cms-content');
|
|
|
|
// Only load if we're in the "edit page" view
|
|
|
|
var blockedClasses = ['CMSPagesController', 'CMSSettingsController', 'CMSPageHistoryController'];
|
|
|
|
return !(contentEl.is('.' + blockedClasses.join(',.')));
|
|
|
|
},
|
2011-05-15 05:43:21 +02:00
|
|
|
|
2011-04-28 12:36:28 +02:00
|
|
|
_fixIframeLinks: function() {
|
|
|
|
var doc = this.find('iframe')[0].contentDocument;
|
2012-04-10 21:08:31 +02:00
|
|
|
if(!doc) return;
|
2011-04-28 12:36:28 +02:00
|
|
|
|
|
|
|
// Block outside links from going anywhere
|
|
|
|
var links = doc.getElementsByTagName('A');
|
|
|
|
for (var i = 0; i < links.length; i++) {
|
|
|
|
var href = links[i].getAttribute('href');
|
2011-07-15 10:35:41 +02:00
|
|
|
if(!href) continue;
|
|
|
|
|
2012-08-27 14:40:11 +02:00
|
|
|
// Open external links in new window to avoid "escaping" the
|
|
|
|
// internal page context in the preview iframe,
|
|
|
|
// which is important to stay in for the CMS logic.
|
|
|
|
if (href.match(/^http:\/\//)) links[i].setAttribute('target', '_blank');
|
2011-04-28 12:36:28 +02:00
|
|
|
}
|
2012-08-27 14:40:11 +02:00
|
|
|
|
|
|
|
// Hide duplicate navigator, as it replicates existing UI in the CMS
|
|
|
|
var navi = doc.getElementById('SilverStripeNavigator');
|
|
|
|
if(navi) navi.style.display = 'none';
|
|
|
|
var naviMsg = doc.getElementById('SilverStripeNavigatorMessage');
|
|
|
|
if(naviMsg) naviMsg.style.display = 'none';
|
2011-05-15 05:43:21 +02:00
|
|
|
},
|
|
|
|
|
2012-05-16 15:32:25 +02:00
|
|
|
expand: function(inclMenu) {
|
2011-05-15 05:43:21 +02:00
|
|
|
var self = this, containerEl = this.getLayoutContainer(), contentEl = containerEl.find('.cms-content');
|
2012-06-06 21:02:13 +02:00
|
|
|
this.show();
|
2011-05-15 05:43:21 +02:00
|
|
|
this.removeClass('east').addClass('center').removeClass('is-collapsed');
|
|
|
|
// this.css('overflow', 'auto');
|
|
|
|
contentEl.removeClass('center').hide();
|
|
|
|
this.find('iframe').show();
|
|
|
|
this.find('.cms-preview-toggle a').html('»');
|
2011-07-21 20:14:33 +02:00
|
|
|
this.find('.cms-preview-controls').show();
|
2012-05-16 15:32:25 +02:00
|
|
|
|
|
|
|
if(this.data('cms-preview-initialized')) {
|
|
|
|
containerEl.find('.cms-menu').collapsePanel();
|
|
|
|
}
|
2011-07-21 20:14:33 +02:00
|
|
|
|
2012-06-06 21:02:13 +02:00
|
|
|
containerEl.redraw();
|
2011-05-15 05:43:21 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
collapse: function() {
|
|
|
|
var self = this, containerEl = this.getLayoutContainer(), contentEl = containerEl.find('.cms-content');
|
|
|
|
this.addClass('east').removeClass('center').addClass('is-collapsed').width(10);
|
|
|
|
// this.css('overflow', 'hidden');
|
2012-08-27 16:50:37 +02:00
|
|
|
contentEl.addClass('center').show().css('visibility', 'visible');
|
2011-05-15 05:43:21 +02:00
|
|
|
this.find('iframe').hide();
|
|
|
|
this.find('.cms-preview-toggle a').html('«');
|
2011-07-21 20:14:33 +02:00
|
|
|
this.find('.cms-preview-controls').hide();
|
2012-05-16 15:32:25 +02:00
|
|
|
|
|
|
|
if(this.data('cms-preview-initialized')) {
|
|
|
|
containerEl.find('.cms-menu').expandPanel();
|
|
|
|
}
|
2011-07-21 20:14:33 +02:00
|
|
|
|
2012-06-06 21:02:13 +02:00
|
|
|
containerEl.redraw();
|
2011-05-15 05:43:21 +02:00
|
|
|
},
|
|
|
|
|
2011-07-15 10:35:41 +02:00
|
|
|
block: function() {
|
|
|
|
this.addClass('blocked');
|
|
|
|
},
|
|
|
|
|
|
|
|
unblock: function() {
|
|
|
|
this.removeClass('blocked');
|
|
|
|
},
|
|
|
|
|
2011-05-15 05:43:21 +02:00
|
|
|
getLayoutContainer: function() {
|
2011-07-05 14:34:31 +02:00
|
|
|
return this.parents('.cms-container');
|
2011-04-28 12:36:28 +02:00
|
|
|
},
|
|
|
|
|
2011-05-02 01:43:51 +02:00
|
|
|
toggle: function(bool) {
|
2011-05-15 05:43:21 +02:00
|
|
|
this[this.hasClass('is-collapsed') ? 'expand' : 'collapse']();
|
2011-07-21 20:14:33 +02:00
|
|
|
},
|
|
|
|
redraw: function() {
|
2012-05-30 20:57:18 +02:00
|
|
|
if(window.debug) console.log('redraw', this.attr('class'), this.get(0));
|
|
|
|
|
2012-06-06 21:02:13 +02:00
|
|
|
this.layout({resize: false});
|
2011-04-28 12:36:28 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2011-07-05 14:34:31 +02:00
|
|
|
$('.cms-preview.collapsed').entwine({
|
2011-04-28 12:36:28 +02:00
|
|
|
onmatch: function() {
|
|
|
|
this.find('a').text('<');
|
2012-05-11 06:07:07 +02:00
|
|
|
this._super();
|
2012-05-14 01:43:36 +02:00
|
|
|
},
|
|
|
|
onunmatch: function() {
|
|
|
|
this._super();
|
2011-04-28 12:36:28 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2011-07-15 10:35:41 +02:00
|
|
|
$('.cms-preview.blocked').entwine({
|
|
|
|
onmatch: function() {
|
|
|
|
this.find('.cms-preview-overlay').show();
|
2012-05-11 06:07:07 +02:00
|
|
|
this._super();
|
2011-07-15 10:35:41 +02:00
|
|
|
},
|
|
|
|
onunmatch: function() {
|
|
|
|
this.find('.cms-preview-overlay').hide();
|
2012-05-11 06:07:07 +02:00
|
|
|
this._super();
|
2011-07-15 10:35:41 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2011-07-05 14:34:31 +02:00
|
|
|
$('.cms-preview.expanded').entwine({
|
2011-04-28 12:36:28 +02:00
|
|
|
onmatch: function() {
|
|
|
|
this.find('a').text('>');
|
2012-05-11 06:07:07 +02:00
|
|
|
this._super();
|
2012-05-14 01:43:36 +02:00
|
|
|
},
|
|
|
|
onunmatch: function() {
|
|
|
|
this._super();
|
2011-04-28 12:36:28 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2011-07-05 14:34:31 +02:00
|
|
|
$('.cms-preview .cms-preview-toggle').entwine({
|
2011-04-28 12:36:28 +02:00
|
|
|
onclick: function(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
this.parents('.cms-preview').toggle();
|
|
|
|
}
|
|
|
|
});
|
2011-05-02 01:43:51 +02:00
|
|
|
|
2011-07-05 14:34:31 +02:00
|
|
|
$('.cms-switch-view a').entwine({
|
2011-05-02 01:43:51 +02:00
|
|
|
onclick: function(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
var preview = $('.cms-preview');
|
|
|
|
preview.toggle(true);
|
|
|
|
preview.loadUrl($(e.target).attr('href'));
|
|
|
|
}
|
|
|
|
});
|
2011-05-15 05:43:21 +02:00
|
|
|
|
2011-07-05 14:34:31 +02:00
|
|
|
$('.cms-menu li').entwine({
|
2011-05-15 05:43:21 +02:00
|
|
|
onclick: function(e) {
|
|
|
|
// Prevent reloading of interface when opening the edit panel
|
|
|
|
if(this.hasClass('Menu-CMSMain')) {
|
|
|
|
var preview = $('.cms-preview');
|
|
|
|
preview.toggle(true);
|
|
|
|
e.preventDefault();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2012-05-04 01:29:50 +02:00
|
|
|
|
2011-07-21 20:14:33 +02:00
|
|
|
$('.cms-preview .cms-preview-states').entwine({
|
|
|
|
onmatch: function() {
|
2012-05-04 03:51:06 +02:00
|
|
|
this.find('a').addClass('disabled');
|
|
|
|
this.find('.active a').removeClass('disabled');
|
2012-05-04 01:29:50 +02:00
|
|
|
this.find('.cms-preview-watermark').show();
|
|
|
|
this.find('.active .cms-preview-watermark').hide();
|
2012-05-11 06:07:07 +02:00
|
|
|
this._super();
|
2012-05-14 01:43:36 +02:00
|
|
|
},
|
|
|
|
onunmatch: function() {
|
|
|
|
this._super();
|
2011-07-21 20:14:33 +02:00
|
|
|
}
|
|
|
|
});
|
2012-05-04 01:29:50 +02:00
|
|
|
|
2011-07-21 20:14:33 +02:00
|
|
|
$('.cms-preview .cms-preview-states a').entwine({
|
|
|
|
onclick: function(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
this.parents('.cms-preview').loadUrl(this.attr('href'));
|
2012-05-04 03:51:06 +02:00
|
|
|
this.addClass('disabled');
|
|
|
|
this.parents('.cms-preview-states').find('a').not(this).removeClass('disabled');
|
2012-05-04 01:29:50 +02:00
|
|
|
//This hides all watermarks
|
|
|
|
this.parents('.cms-preview-states').find('.cms-preview-watermark').hide();
|
|
|
|
//Show the watermark for the current state
|
|
|
|
this.siblings('.cms-preview-watermark').show();
|
2011-07-21 20:14:33 +02:00
|
|
|
}
|
|
|
|
});
|
2012-05-04 01:29:50 +02:00
|
|
|
|
2011-07-21 20:14:33 +02:00
|
|
|
$('.cms-preview-toggle-link').entwine({
|
|
|
|
onclick: function(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
|
2012-08-28 18:31:15 +02:00
|
|
|
var preview = $('.cms-preview'),
|
|
|
|
url = $('.cms-edit-form').choosePreviewLink();
|
|
|
|
|
2011-12-19 15:06:04 +01:00
|
|
|
if(url) {
|
|
|
|
preview.loadUrl(url);
|
|
|
|
preview.unblock();
|
|
|
|
preview.toggle();
|
|
|
|
}
|
2011-07-21 20:14:33 +02:00
|
|
|
}
|
|
|
|
});
|
2012-08-28 18:31:15 +02:00
|
|
|
|
|
|
|
$('.cms-edit-form').entwine({
|
|
|
|
/**
|
|
|
|
* Choose applicable preview link based on form data,
|
|
|
|
* in a fixed order of priority: The PreviewURL field is used as an override,
|
|
|
|
* which falls back to stage or live URLs.
|
|
|
|
*
|
|
|
|
* @return String Absolute URL
|
|
|
|
*/
|
|
|
|
choosePreviewLink: function() {
|
|
|
|
var self = this, urls = $.map(['PreviewURL', 'StageLink', 'LiveLink'], function(name) {
|
|
|
|
var val = self.find(':input[name=' + name + ']').val();
|
|
|
|
return val ? val : null;
|
|
|
|
});
|
|
|
|
return urls ? urls[0] : false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2011-04-28 12:36:28 +02:00
|
|
|
});
|
2012-07-16 12:05:48 +02:00
|
|
|
}(jQuery));
|