2012-02-16 12:27:47 +01:00
|
|
|
jQuery.noConflict();
|
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
/**
|
|
|
|
* File: LeftAndMain.js
|
|
|
|
*/
|
|
|
|
(function($) {
|
2012-11-22 02:14:11 +01:00
|
|
|
|
2015-08-24 02:02:20 +02:00
|
|
|
window.ss = window.ss || {};
|
|
|
|
|
2013-06-13 17:31:28 +02:00
|
|
|
var windowWidth, windowHeight;
|
2015-08-24 02:02:20 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @func debounce
|
|
|
|
* @param func {function} - The callback to invoke after `wait` milliseconds.
|
|
|
|
* @param wait {number} - Milliseconds to wait.
|
|
|
|
* @param immediate {boolean} - If true the callback will be invoked at the start rather than the end.
|
|
|
|
* @return {function}
|
|
|
|
* @desc Returns a function that will not be called until it hasn't been invoked for `wait` seconds.
|
|
|
|
*/
|
|
|
|
window.ss.debounce = function (func, wait, immediate) {
|
|
|
|
var timeout, context, args;
|
|
|
|
|
|
|
|
var later = function() {
|
|
|
|
timeout = null;
|
|
|
|
if (!immediate) func.apply(context, args);
|
|
|
|
};
|
|
|
|
|
|
|
|
return function() {
|
|
|
|
var callNow = immediate && !timeout;
|
|
|
|
|
|
|
|
context = this;
|
|
|
|
args = arguments;
|
|
|
|
|
|
|
|
clearTimeout(timeout);
|
|
|
|
timeout = setTimeout(later, wait);
|
|
|
|
|
|
|
|
if (callNow) {
|
|
|
|
func.apply(context, args);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2013-06-13 17:31:28 +02:00
|
|
|
$(window).bind('resize.leftandmain', function(e) {
|
2012-12-16 02:33:05 +01:00
|
|
|
// Entwine's 'fromWindow::onresize' does not trigger on IE8. Use synthetic event.
|
2013-06-13 17:31:28 +02:00
|
|
|
var cb = function() {$('.cms-container').trigger('windowresize');};
|
|
|
|
|
2015-03-07 13:32:04 +01:00
|
|
|
// Workaround to avoid IE8 infinite loops when elements are resized as a result of this event
|
2013-06-13 17:31:28 +02:00
|
|
|
if($.browser.msie && parseInt($.browser.version, 10) < 9) {
|
|
|
|
var newWindowWidth = $(window).width(), newWindowHeight = $(window).height();
|
|
|
|
if(newWindowWidth != windowWidth || newWindowHeight != windowHeight) {
|
|
|
|
windowWidth = newWindowWidth;
|
|
|
|
windowHeight = newWindowHeight;
|
|
|
|
cb();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
cb();
|
|
|
|
}
|
|
|
|
});
|
2012-12-16 02:33:05 +01:00
|
|
|
|
2011-12-17 04:06:56 +01:00
|
|
|
// setup jquery.entwine
|
|
|
|
$.entwine.warningLevel = $.entwine.WARN_LEVEL_BESTPRACTISE;
|
|
|
|
$.entwine('ss', function($) {
|
2015-03-07 13:32:04 +01:00
|
|
|
|
2014-10-06 05:01:33 +02:00
|
|
|
/*
|
|
|
|
* Handle messages sent via nested iframes
|
|
|
|
* Messages should be raised via postMessage with an object with the 'type' parameter given.
|
|
|
|
* An optional 'target' and 'data' parameter can also be specified. If no target is specified
|
|
|
|
* events will be sent to the window instead.
|
|
|
|
* type should be one of:
|
|
|
|
* - 'event' - Will trigger the given event (specified by 'event') on the target
|
|
|
|
* - 'callback' - Will call the given method (specified by 'callback') on the target
|
2015-03-07 13:32:04 +01:00
|
|
|
*/
|
2014-10-06 05:01:33 +02:00
|
|
|
$(window).on("message", function(e) {
|
|
|
|
var target,
|
|
|
|
event = e.originalEvent,
|
|
|
|
data = JSON.parse(event.data);
|
2015-03-07 13:32:04 +01:00
|
|
|
|
2014-10-06 05:01:33 +02:00
|
|
|
// Reject messages outside of the same origin
|
|
|
|
if($.path.parseUrl(window.location.href).domain !== $.path.parseUrl(event.origin).domain) return;
|
2015-03-07 13:32:04 +01:00
|
|
|
|
2014-10-06 05:01:33 +02:00
|
|
|
// Get target of this action
|
|
|
|
target = typeof(data.target) === 'undefined'
|
|
|
|
? $(window)
|
|
|
|
: $(data.target);
|
2015-03-07 13:32:04 +01:00
|
|
|
|
2014-10-06 05:01:33 +02:00
|
|
|
// Determine action
|
|
|
|
switch(data.type) {
|
|
|
|
case 'event':
|
|
|
|
target.trigger(data.event, data.data);
|
|
|
|
break;
|
|
|
|
case 'callback':
|
|
|
|
target[data.callback].call(target, data.data);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
2015-03-07 13:32:04 +01:00
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
/**
|
|
|
|
* Position the loading spinner animation below the ss logo
|
2015-03-07 13:32:04 +01:00
|
|
|
*/
|
2011-03-23 10:51:00 +01:00
|
|
|
var positionLoadingSpinner = function() {
|
|
|
|
var offset = 120; // offset from the ss logo
|
2015-03-07 13:32:04 +01:00
|
|
|
var spinner = $('.ss-loading-screen .loading-animation');
|
2011-03-23 10:51:00 +01:00
|
|
|
var top = ($(window).height() - spinner.height()) / 2;
|
|
|
|
spinner.css('top', top + offset);
|
|
|
|
spinner.show();
|
2012-02-21 17:46:58 +01:00
|
|
|
};
|
2015-03-07 13:32:04 +01:00
|
|
|
|
2012-03-27 17:06:58 +02:00
|
|
|
// apply an select element only when it is ready, ie. when it is rendered into a template
|
|
|
|
// with css applied and got a width value.
|
2012-07-30 17:14:02 +02:00
|
|
|
var applyChosen = function(el) {
|
|
|
|
if(el.is(':visible')) {
|
|
|
|
el.addClass('has-chzn').chosen({
|
|
|
|
allow_single_deselect: true,
|
|
|
|
disable_search_threshold: 20
|
|
|
|
});
|
|
|
|
|
2012-12-17 11:31:39 +01:00
|
|
|
var title = el.prop('title');
|
2012-07-30 17:14:02 +02:00
|
|
|
|
|
|
|
if(title) {
|
|
|
|
el.siblings('.chzn-container').prop('title', title);
|
|
|
|
}
|
2012-03-27 17:06:58 +02:00
|
|
|
} else {
|
2012-09-03 08:47:25 +02:00
|
|
|
setTimeout(function() {
|
|
|
|
// Make sure it's visible before applying the ui
|
|
|
|
el.show();
|
2015-03-07 13:32:04 +01:00
|
|
|
applyChosen(el); },
|
2012-09-03 08:47:25 +02:00
|
|
|
500);
|
2012-03-27 17:06:58 +02:00
|
|
|
}
|
|
|
|
};
|
2012-08-15 21:22:15 +02:00
|
|
|
|
|
|
|
/**
|
2015-03-07 13:32:04 +01:00
|
|
|
* Compare URLs, but normalize trailing slashes in
|
2012-08-15 21:22:15 +02:00
|
|
|
* URL to work around routing weirdnesses in SS_HTTPRequest.
|
|
|
|
* Also normalizes relative URLs by prefixing them with the <base>.
|
|
|
|
*/
|
|
|
|
var isSameUrl = function(url1, url2) {
|
|
|
|
var baseUrl = $('base').attr('href');
|
|
|
|
url1 = $.path.isAbsoluteUrl(url1) ? url1 : $.path.makeUrlAbsolute(url1, baseUrl),
|
|
|
|
url2 = $.path.isAbsoluteUrl(url2) ? url2 : $.path.makeUrlAbsolute(url2, baseUrl);
|
|
|
|
var url1parts = $.path.parseUrl(url1), url2parts = $.path.parseUrl(url2);
|
|
|
|
return (
|
2015-03-07 13:32:04 +01:00
|
|
|
url1parts.pathname.replace(/\/*$/, '') == url2parts.pathname.replace(/\/*$/, '') &&
|
2012-08-15 21:22:15 +02:00
|
|
|
url1parts.search == url2parts.search
|
|
|
|
);
|
|
|
|
};
|
2015-03-07 13:32:04 +01:00
|
|
|
|
2015-08-24 02:02:20 +02:00
|
|
|
var ajaxCompleteEvent = window.ss.debounce(function () {
|
2015-05-19 03:43:43 +02:00
|
|
|
$(window).trigger('ajaxComplete');
|
|
|
|
}, 1000, true);
|
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
$(window).bind('resize', positionLoadingSpinner).trigger('resize');
|
2011-12-13 11:35:00 +01:00
|
|
|
|
2012-03-30 18:30:01 +02:00
|
|
|
// global ajax handlers
|
2012-03-24 03:19:02 +01:00
|
|
|
$(document).ajaxComplete(function(e, xhr, settings) {
|
|
|
|
// Simulates a redirect on an ajax response.
|
|
|
|
if(window.History.enabled) {
|
2014-10-06 05:01:33 +02:00
|
|
|
var url = xhr.getResponseHeader('X-ControllerURL'),
|
2012-08-15 21:22:15 +02:00
|
|
|
// TODO Replaces trailing slashes added by History after locale (e.g. admin/?locale=en/)
|
|
|
|
origUrl = History.getPageUrl().replace(/\/$/, ''),
|
2014-10-06 05:01:33 +02:00
|
|
|
destUrl = settings.url,
|
|
|
|
opts;
|
2012-08-15 21:22:15 +02:00
|
|
|
|
2014-10-06 05:01:33 +02:00
|
|
|
// Only redirect if controller url differs to the requested or current one
|
|
|
|
if(url !== null &&
|
|
|
|
(!isSameUrl(origUrl, url) || !isSameUrl(destUrl, url))
|
|
|
|
) {
|
|
|
|
opts = {
|
|
|
|
// Ensure that redirections are followed through by history API by handing it a unique ID
|
|
|
|
id: (new Date()).getTime() + String(Math.random()).replace(/\D/g,''),
|
|
|
|
pjax: xhr.getResponseHeader('X-Pjax')
|
|
|
|
? xhr.getResponseHeader('X-Pjax')
|
|
|
|
: settings.headers['X-Pjax']
|
|
|
|
};
|
2012-03-24 03:19:02 +01:00
|
|
|
window.History.pushState(opts, '', url);
|
2012-03-24 04:35:18 +01:00
|
|
|
}
|
2011-03-23 10:51:00 +01:00
|
|
|
}
|
2012-04-17 17:12:15 +02:00
|
|
|
|
2012-05-10 10:44:21 +02:00
|
|
|
// Handle custom status message headers
|
|
|
|
var msg = (xhr.getResponseHeader('X-Status')) ? xhr.getResponseHeader('X-Status') : xhr.statusText,
|
2014-10-06 05:01:33 +02:00
|
|
|
reathenticate = xhr.getResponseHeader('X-Reauthenticate'),
|
2012-05-10 10:44:21 +02:00
|
|
|
msgType = (xhr.status < 200 || xhr.status > 399) ? 'bad' : 'good',
|
2016-07-14 16:35:51 +02:00
|
|
|
ignoredMessages = ['OK', 'success', 'HTTP/2.0 200'];
|
2015-03-07 13:32:04 +01:00
|
|
|
|
2014-10-06 05:01:33 +02:00
|
|
|
// Enable reauthenticate dialog if requested
|
|
|
|
if(reathenticate) {
|
|
|
|
$('.cms-container').showLoginDialog();
|
|
|
|
return;
|
|
|
|
}
|
2012-05-10 10:44:21 +02:00
|
|
|
|
|
|
|
// Show message (but ignore aborted requests)
|
2016-05-02 08:15:39 +02:00
|
|
|
if(xhr.status !== 0 && msg && $.inArray(msg, ignoredMessages) === -1) {
|
2012-05-14 15:13:49 +02:00
|
|
|
// Decode into UTF-8, HTTP headers don't allow multibyte
|
|
|
|
statusMessage(decodeURIComponent(msg), msgType);
|
2012-03-24 03:19:02 +01:00
|
|
|
}
|
2015-05-19 03:43:43 +02:00
|
|
|
|
|
|
|
ajaxCompleteEvent(this);
|
2012-03-24 03:19:02 +01:00
|
|
|
});
|
2013-08-12 04:18:18 +02:00
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
/**
|
|
|
|
* Main LeftAndMain interface with some control panel and an edit form.
|
2015-03-07 13:32:04 +01:00
|
|
|
*
|
2011-03-23 10:51:00 +01:00
|
|
|
* Events:
|
|
|
|
* ajaxsubmit - ...
|
|
|
|
* validate - ...
|
2012-06-12 17:13:36 +02:00
|
|
|
* aftersubmitform - ...
|
2011-03-23 10:51:00 +01:00
|
|
|
*/
|
2011-07-05 14:34:31 +02:00
|
|
|
$('.cms-container').entwine({
|
2015-03-07 13:32:04 +01:00
|
|
|
|
2013-08-22 04:12:31 +02:00
|
|
|
/**
|
|
|
|
* Tracks current panel request.
|
|
|
|
*/
|
|
|
|
StateChangeXHR: null,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tracks current fragment-only parallel PJAX requests.
|
|
|
|
*/
|
|
|
|
FragmentXHR: {},
|
2012-06-14 18:23:54 +02:00
|
|
|
|
|
|
|
StateChangeCount: 0,
|
2015-03-07 13:32:04 +01:00
|
|
|
|
2012-11-26 05:26:06 +01:00
|
|
|
/**
|
|
|
|
* Options for the threeColumnCompressor layout algorithm.
|
|
|
|
*
|
|
|
|
* See LeftAndMain.Layout.js for description of these options.
|
|
|
|
*/
|
|
|
|
LayoutOptions: {
|
2015-05-25 06:06:47 +02:00
|
|
|
minContentWidth: 940,
|
2012-11-26 05:26:06 +01:00
|
|
|
minPreviewWidth: 400,
|
2012-12-04 06:10:20 +01:00
|
|
|
mode: 'content'
|
2012-11-26 05:26:06 +01:00
|
|
|
},
|
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
/**
|
|
|
|
* Constructor: onmatch
|
|
|
|
*/
|
2012-06-12 12:48:08 +02:00
|
|
|
onadd: function() {
|
2011-03-23 10:51:00 +01:00
|
|
|
var self = this;
|
2011-06-09 03:49:52 +02:00
|
|
|
|
2011-04-24 02:31:11 +02:00
|
|
|
// Browser detection
|
2013-06-15 11:58:35 +02:00
|
|
|
if($.browser.msie && parseInt($.browser.version, 10) < 8) {
|
2011-04-24 02:31:11 +02:00
|
|
|
$('.ss-loading-screen').append(
|
2015-03-07 13:32:04 +01:00
|
|
|
'<p class="ss-loading-incompat-warning"><span class="notice">' +
|
2013-06-15 11:58:35 +02:00
|
|
|
'Your browser is not compatible with the CMS interface. Please use Internet Explorer 8+, Google Chrome or Mozilla Firefox.' +
|
2011-04-24 02:31:11 +02:00
|
|
|
'</span></p>'
|
2011-12-15 16:54:33 +01:00
|
|
|
).css('z-index', $('.ss-loading-screen').css('z-index')+1);
|
|
|
|
$('.loading-animation').remove();
|
2012-05-11 06:07:07 +02:00
|
|
|
|
|
|
|
this._super();
|
2011-04-24 02:31:11 +02:00
|
|
|
return;
|
|
|
|
}
|
2015-03-07 13:32:04 +01:00
|
|
|
|
2011-07-21 18:39:26 +02:00
|
|
|
// Initialize layouts
|
2011-05-15 05:43:21 +02:00
|
|
|
this.redraw();
|
2011-07-21 20:14:33 +02:00
|
|
|
|
2011-04-15 00:35:09 +02:00
|
|
|
// Remove loading screen
|
|
|
|
$('.ss-loading-screen').hide();
|
|
|
|
$('body').removeClass('loading');
|
|
|
|
$(window).unbind('resize', positionLoadingSpinner);
|
2013-03-14 16:22:32 +01:00
|
|
|
this.restoreTabState();
|
2011-03-23 10:51:00 +01:00
|
|
|
this._super();
|
|
|
|
},
|
2012-06-12 12:48:08 +02:00
|
|
|
|
|
|
|
fromWindow: {
|
2015-07-31 00:16:18 +02:00
|
|
|
onstatechange: function(e){
|
|
|
|
this.handleStateChange(e);
|
|
|
|
}
|
2012-12-16 02:33:05 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
'onwindowresize': function() {
|
|
|
|
this.redraw();
|
2012-05-14 01:43:36 +02:00
|
|
|
},
|
2012-06-12 12:48:08 +02:00
|
|
|
|
|
|
|
'from .cms-panel': {
|
|
|
|
ontoggle: function(){ this.redraw(); }
|
2012-06-12 17:13:36 +02:00
|
|
|
},
|
2012-06-12 12:48:08 +02:00
|
|
|
|
|
|
|
'from .cms-container': {
|
|
|
|
onaftersubmitform: function(){ this.redraw(); }
|
|
|
|
},
|
|
|
|
|
2012-11-26 05:26:06 +01:00
|
|
|
/**
|
|
|
|
* Ensure the user can see the requested section - restore the default view.
|
|
|
|
*/
|
|
|
|
'from .cms-menu-list li a': {
|
2012-12-18 11:44:12 +01:00
|
|
|
onclick: function(e) {
|
2013-03-01 12:42:34 +01:00
|
|
|
var href = $(e.target).attr('href');
|
|
|
|
if(e.which > 1 || href == this._tabStateUrl()) return;
|
2012-11-26 05:26:06 +01:00
|
|
|
this.splitViewMode();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2013-08-12 04:18:18 +02:00
|
|
|
* Change the options of the threeColumnCompressor layout, and trigger layouting if needed.
|
|
|
|
* You can provide any or all options. The remaining options will not be changed.
|
2012-11-26 05:26:06 +01:00
|
|
|
*/
|
|
|
|
updateLayoutOptions: function(newSpec) {
|
|
|
|
var spec = this.getLayoutOptions();
|
2013-08-12 04:18:18 +02:00
|
|
|
|
|
|
|
var dirty = false;
|
|
|
|
|
|
|
|
for (var k in newSpec) {
|
|
|
|
if (spec[k] !== newSpec[k]) {
|
|
|
|
spec[k] = newSpec[k];
|
|
|
|
dirty = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dirty) this.redraw();
|
2012-11-26 05:26:06 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enable the split view - with content on the left and preview on the right.
|
|
|
|
*/
|
|
|
|
splitViewMode: function() {
|
|
|
|
this.updateLayoutOptions({
|
|
|
|
mode: 'split'
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Content only.
|
|
|
|
*/
|
|
|
|
contentViewMode: function() {
|
|
|
|
this.updateLayoutOptions({
|
|
|
|
mode: 'content'
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Preview only.
|
|
|
|
*/
|
|
|
|
previewMode: function() {
|
|
|
|
this.updateLayoutOptions({
|
|
|
|
mode: 'preview'
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2013-08-12 04:18:18 +02:00
|
|
|
RedrawSuppression: false,
|
|
|
|
|
2011-05-15 05:43:21 +02:00
|
|
|
redraw: function() {
|
2013-08-12 04:18:18 +02:00
|
|
|
if (this.getRedrawSuppression()) return;
|
|
|
|
|
2012-05-30 20:57:18 +02:00
|
|
|
if(window.debug) console.log('redraw', this.attr('class'), this.get(0));
|
|
|
|
|
2012-11-26 05:26:06 +01:00
|
|
|
// Reset the algorithm.
|
|
|
|
this.data('jlayout', jLayout.threeColumnCompressor(
|
|
|
|
{
|
|
|
|
menu: this.children('.cms-menu'),
|
|
|
|
content: this.children('.cms-content'),
|
|
|
|
preview: this.children('.cms-preview')
|
|
|
|
},
|
|
|
|
this.getLayoutOptions()
|
2012-11-22 02:14:11 +01:00
|
|
|
));
|
2013-06-19 14:03:43 +02:00
|
|
|
|
2012-11-15 00:14:06 +01:00
|
|
|
// Trigger layout algorithm once at the top. This also lays out children - we move from outside to
|
|
|
|
// inside, resizing to fit the parent.
|
2012-11-22 02:14:11 +01:00
|
|
|
this.layout();
|
2012-06-06 21:02:13 +02:00
|
|
|
|
2012-11-22 02:14:11 +01:00
|
|
|
// Redraw on all the children that need it
|
2013-08-12 04:18:18 +02:00
|
|
|
this.find('.cms-panel-layout').redraw();
|
|
|
|
this.find('.cms-content-fields[data-layout-type]').redraw();
|
|
|
|
this.find('.cms-edit-form[data-layout-type]').redraw();
|
2012-02-17 15:58:15 +01:00
|
|
|
this.find('.cms-preview').redraw();
|
2012-06-06 21:02:13 +02:00
|
|
|
this.find('.cms-content').redraw();
|
2011-06-09 03:49:52 +02:00
|
|
|
},
|
2011-12-15 12:16:54 +01:00
|
|
|
|
2015-07-31 00:16:18 +02:00
|
|
|
/**
|
|
|
|
* Confirm whether the current user can navigate away from this page
|
|
|
|
*
|
|
|
|
* @param {array} selectors Optional list of selectors
|
|
|
|
* @returns {boolean} True if the navigation can proceed
|
|
|
|
*/
|
|
|
|
checkCanNavigate: function(selectors) {
|
|
|
|
// Check change tracking (can't use events as we need a way to cancel the current state change)
|
|
|
|
var contentEls = this._findFragments(selectors || ['Content']),
|
|
|
|
trackedEls = contentEls
|
|
|
|
.find(':data(changetracker)')
|
|
|
|
.add(contentEls.filter(':data(changetracker)')),
|
|
|
|
safe = true;
|
|
|
|
|
|
|
|
if(!trackedEls.length) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
trackedEls.each(function() {
|
|
|
|
// See LeftAndMain.EditForm.js
|
|
|
|
if(!$(this).confirmUnsavedChanges()) {
|
|
|
|
safe = false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return safe;
|
|
|
|
},
|
2011-12-15 12:16:54 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Proxy around History.pushState() which handles non-HTML5 fallbacks,
|
|
|
|
* as well as global change tracking. Change tracking needs to be synchronous rather than event/callback
|
|
|
|
* based because the user needs to be able to abort the action completely.
|
2015-03-07 13:32:04 +01:00
|
|
|
*
|
2011-12-15 12:16:54 +01:00
|
|
|
* See handleStateChange() for more details.
|
2015-03-07 13:32:04 +01:00
|
|
|
*
|
2011-12-15 12:16:54 +01:00
|
|
|
* Parameters:
|
|
|
|
* - {String} url
|
|
|
|
* - {String} title New window title
|
|
|
|
* - {Object} data Any additional data passed through to History.pushState()
|
2012-07-16 01:54:10 +02:00
|
|
|
* - {boolean} forceReload Forces the replacement of the current history state, even if the URL is the same, i.e. allows reloading.
|
2011-12-15 12:16:54 +01:00
|
|
|
*/
|
2013-11-05 11:36:46 +01:00
|
|
|
loadPanel: function(url, title, data, forceReload, forceReferer) {
|
2012-02-21 17:46:58 +01:00
|
|
|
if(!data) data = {};
|
2012-03-24 02:20:33 +01:00
|
|
|
if(!title) title = "";
|
2013-11-05 11:36:46 +01:00
|
|
|
if (!forceReferer) forceReferer = History.getState().url;
|
2012-05-30 15:01:40 +02:00
|
|
|
|
2015-07-31 00:16:18 +02:00
|
|
|
// Check for unsaved changes
|
|
|
|
if(!this.checkCanNavigate(data.pjax ? data.pjax.split(',') : ['Content'])) {
|
|
|
|
return;
|
2011-12-15 12:23:32 +01:00
|
|
|
}
|
2012-07-13 16:46:23 +02:00
|
|
|
|
|
|
|
// Save tab selections so we can restore them later
|
|
|
|
this.saveTabState();
|
2015-03-07 13:32:04 +01:00
|
|
|
|
2011-12-15 12:16:54 +01:00
|
|
|
if(window.History.enabled) {
|
2013-11-05 11:36:46 +01:00
|
|
|
$.extend(data, {__forceReferer: forceReferer});
|
2011-12-15 12:16:54 +01:00
|
|
|
// Active menu item is set based on X-Controller ajax header,
|
|
|
|
// which matches one class on the menu
|
2012-07-16 01:54:10 +02:00
|
|
|
if(forceReload) {
|
|
|
|
// Add a parameter to make sure the page gets reloaded even if the URL is the same.
|
|
|
|
$.extend(data, {__forceReload: Math.random()});
|
|
|
|
window.History.replaceState(data, title, url);
|
|
|
|
} else {
|
|
|
|
window.History.pushState(data, title, url);
|
|
|
|
}
|
2011-12-15 12:16:54 +01:00
|
|
|
} else {
|
2011-12-14 15:37:42 +01:00
|
|
|
window.location = $.path.makeUrlAbsolute(url, $('base').attr('href'));
|
2011-12-15 12:16:54 +01:00
|
|
|
}
|
|
|
|
},
|
2012-05-30 11:57:49 +02:00
|
|
|
|
2012-07-16 01:54:10 +02:00
|
|
|
/**
|
|
|
|
* Nice wrapper for reloading current history state.
|
|
|
|
*/
|
|
|
|
reloadCurrentPanel: function() {
|
|
|
|
this.loadPanel(window.History.getState().url, null, null, true);
|
|
|
|
},
|
|
|
|
|
2012-05-30 11:57:49 +02:00
|
|
|
/**
|
|
|
|
* Function: submitForm
|
2015-03-07 13:32:04 +01:00
|
|
|
*
|
2012-05-30 11:57:49 +02:00
|
|
|
* Parameters:
|
|
|
|
* {DOMElement} form - The form to be submitted. Needs to be passed
|
|
|
|
* in to avoid entwine methods/context being removed through replacing the node itself.
|
|
|
|
* {DOMElement} button - The pressed button (optional)
|
|
|
|
* {Function} callback - Called in complete() handler of jQuery.ajax()
|
|
|
|
* {Object} ajaxOptions - Object literal to merge into $.ajax() call
|
2015-03-07 13:32:04 +01:00
|
|
|
*
|
2012-05-30 11:57:49 +02:00
|
|
|
* Returns:
|
|
|
|
* (boolean)
|
|
|
|
*/
|
|
|
|
submitForm: function(form, button, callback, ajaxOptions) {
|
|
|
|
var self = this;
|
2015-03-07 13:32:04 +01:00
|
|
|
|
2012-05-30 11:57:49 +02:00
|
|
|
// look for save button
|
|
|
|
if(!button) button = this.find('.Actions :submit[name=action_save]');
|
|
|
|
// default to first button if none given - simulates browser behaviour
|
|
|
|
if(!button) button = this.find('.Actions :submit:first');
|
2015-03-07 13:32:04 +01:00
|
|
|
|
2012-06-12 17:13:36 +02:00
|
|
|
form.trigger('beforesubmitform');
|
2012-05-30 11:57:49 +02:00
|
|
|
this.trigger('submitform', {form: form, button: button});
|
2015-03-07 13:32:04 +01:00
|
|
|
|
2012-05-30 11:57:49 +02:00
|
|
|
// set button to "submitting" state
|
|
|
|
$(button).addClass('loading');
|
2015-03-07 13:32:04 +01:00
|
|
|
|
2012-05-30 11:57:49 +02:00
|
|
|
// validate if required
|
2012-11-18 23:27:15 +01:00
|
|
|
var validationResult = form.validate();
|
|
|
|
if(typeof validationResult!=='undefined' && !validationResult) {
|
2012-05-30 11:57:49 +02:00
|
|
|
// TODO Automatically switch to the tab/position of the first error
|
|
|
|
statusMessage("Validation failed.", "bad");
|
|
|
|
|
|
|
|
$(button).removeClass('loading');
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2015-03-07 13:32:04 +01:00
|
|
|
|
2012-05-30 11:57:49 +02:00
|
|
|
// get all data from the form
|
|
|
|
var formData = form.serializeArray();
|
|
|
|
// add button action
|
|
|
|
formData.push({name: $(button).attr('name'), value:'1'});
|
2015-03-07 13:32:04 +01:00
|
|
|
// Artificial HTTP referer, IE doesn't submit them via ajax.
|
2012-05-30 11:57:49 +02:00
|
|
|
// Also rewrites anchors to their page counterparts, which is important
|
|
|
|
// as automatic browser ajax response redirects seem to discard the hash/fragment.
|
2014-08-15 05:17:39 +02:00
|
|
|
// TODO Replaces trailing slashes added by History after locale (e.g. admin/?locale=en/)
|
|
|
|
formData.push({name: 'BackURL', value:History.getPageUrl().replace(/\/$/, '')});
|
2012-05-30 11:57:49 +02:00
|
|
|
|
2012-07-13 16:46:23 +02:00
|
|
|
// Save tab selections so we can restore them later
|
|
|
|
this.saveTabState();
|
|
|
|
|
2012-05-30 11:57:49 +02:00
|
|
|
// Standard Pjax behaviour is to replace the submitted form with new content.
|
|
|
|
// The returned view isn't always decided upon when the request
|
|
|
|
// is fired, so the server might decide to change it based on its own logic,
|
|
|
|
// sending back different `X-Pjax` headers and content
|
|
|
|
jQuery.ajax(jQuery.extend({
|
|
|
|
headers: {"X-Pjax" : "CurrentForm,Breadcrumbs"},
|
2015-03-07 13:32:04 +01:00
|
|
|
url: form.attr('action'),
|
2012-05-30 11:57:49 +02:00
|
|
|
data: formData,
|
|
|
|
type: 'POST',
|
|
|
|
complete: function() {
|
|
|
|
$(button).removeClass('loading');
|
|
|
|
},
|
|
|
|
success: function(data, status, xhr) {
|
|
|
|
form.removeClass('changed'); // TODO This should be using the plugin API
|
2012-06-11 13:33:41 +02:00
|
|
|
if(callback) callback(data, status, xhr);
|
2012-05-30 11:57:49 +02:00
|
|
|
|
|
|
|
var newContentEls = self.handleAjaxResponse(data, status, xhr);
|
|
|
|
if(!newContentEls) return;
|
|
|
|
|
2012-07-13 16:46:23 +02:00
|
|
|
newContentEls.filter('form').trigger('aftersubmitform', {status: status, xhr: xhr, formData: formData});
|
2012-07-12 17:10:50 +02:00
|
|
|
}
|
2012-05-30 11:57:49 +02:00
|
|
|
}, ajaxOptions));
|
2015-03-07 13:32:04 +01:00
|
|
|
|
2012-05-30 11:57:49 +02:00
|
|
|
return false;
|
|
|
|
},
|
2012-07-13 16:46:23 +02:00
|
|
|
|
2015-07-31 00:16:18 +02:00
|
|
|
/**
|
|
|
|
* Last html5 history state
|
|
|
|
*/
|
|
|
|
LastState: null,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Flag to pause handleStateChange
|
|
|
|
*/
|
|
|
|
PauseState: false,
|
2012-07-13 16:46:23 +02:00
|
|
|
|
2011-06-09 03:49:52 +02:00
|
|
|
/**
|
|
|
|
* Handles ajax loading of new panels through the window.History object.
|
|
|
|
* To trigger loading, pass a new URL to window.History.pushState().
|
2011-12-15 12:16:54 +01:00
|
|
|
* Use loadPanel() as a pushState() wrapper as it provides some additional functionality
|
|
|
|
* like global changetracking and user aborts.
|
2015-03-07 13:32:04 +01:00
|
|
|
*
|
2011-06-09 03:49:52 +02:00
|
|
|
* Due to the nature of history management, no callbacks are allowed.
|
|
|
|
* Use the 'beforestatechange' and 'afterstatechange' events instead,
|
2015-03-07 13:32:04 +01:00
|
|
|
* or overwrite the beforeLoad() and afterLoad() methods on the
|
2011-06-09 03:49:52 +02:00
|
|
|
* DOM element you're loading the new content into.
|
2015-03-07 13:32:04 +01:00
|
|
|
* Although you can pass data into pushState(), it shouldn't contain
|
2011-06-09 03:49:52 +02:00
|
|
|
* DOM elements or callback closures.
|
2015-03-07 13:32:04 +01:00
|
|
|
*
|
2011-06-09 03:49:52 +02:00
|
|
|
* The passed URL should allow reconstructing important interface state
|
|
|
|
* without additional parameters, in the following use cases:
|
|
|
|
* - Explicit loading through History.pushState()
|
|
|
|
* - Implicit loading through browser navigation event triggered by the user (forward or back)
|
|
|
|
* - Full window refresh without ajax
|
|
|
|
* For example, a ModelAdmin search event should contain the search terms
|
2015-03-07 13:32:04 +01:00
|
|
|
* as URL parameters, and the result display should automatically appear
|
2011-06-09 03:49:52 +02:00
|
|
|
* if the URL is loaded without ajax.
|
|
|
|
*/
|
|
|
|
handleStateChange: function() {
|
2015-07-31 00:16:18 +02:00
|
|
|
if(this.getPauseState()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-06-09 03:49:52 +02:00
|
|
|
// Don't allow parallel loading to avoid edge cases
|
2013-08-22 04:12:31 +02:00
|
|
|
if(this.getStateChangeXHR()) this.getStateChangeXHR().abort();
|
2012-04-18 22:11:40 +02:00
|
|
|
|
|
|
|
var self = this, h = window.History, state = h.getState(),
|
|
|
|
fragments = state.data.pjax || 'Content', headers = {},
|
2012-07-13 10:19:24 +02:00
|
|
|
fragmentsArr = fragments.split(','),
|
|
|
|
contentEls = this._findFragments(fragmentsArr);
|
2012-06-14 18:23:54 +02:00
|
|
|
|
|
|
|
// For legacy IE versions (IE7 and IE8), reload without ajax
|
|
|
|
// as a crude way to fix memory leaks through whole window refreshes.
|
|
|
|
this.setStateChangeCount(this.getStateChangeCount() + 1);
|
|
|
|
var isLegacyIE = ($.browser.msie && parseInt($.browser.version, 10) < 9);
|
|
|
|
if(isLegacyIE && this.getStateChangeCount() > 20) {
|
|
|
|
document.location.href = state.url;
|
|
|
|
return;
|
|
|
|
}
|
2012-07-13 10:19:24 +02:00
|
|
|
|
2015-07-31 00:16:18 +02:00
|
|
|
if(!this.checkCanNavigate()) {
|
|
|
|
// If history is emulated (ie8 or below) disable attempting to restore
|
|
|
|
if(h.emulated.pushState) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var lastState = this.getLastState();
|
|
|
|
|
|
|
|
// Suppress panel loading while resetting state
|
|
|
|
this.setPauseState(true);
|
|
|
|
|
|
|
|
// Restore best last state
|
|
|
|
if(lastState) {
|
|
|
|
h.pushState(lastState.id, lastState.title, lastState.url);
|
|
|
|
} else {
|
|
|
|
h.back();
|
|
|
|
}
|
|
|
|
this.setPauseState(false);
|
|
|
|
|
|
|
|
// Abort loading of this panel
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.setLastState(state);
|
|
|
|
|
2012-07-13 10:19:24 +02:00
|
|
|
// If any of the requested Pjax fragments don't exist in the current view,
|
|
|
|
// fetch the "Content" view instead, which is the "outermost" fragment
|
|
|
|
// that can be reloaded without reloading the whole window.
|
|
|
|
if(contentEls.length < fragmentsArr.length) {
|
|
|
|
fragments = 'Content', fragmentsArr = ['Content'];
|
2015-03-07 13:32:04 +01:00
|
|
|
contentEls = this._findFragments(fragmentsArr);
|
2012-07-13 10:19:24 +02:00
|
|
|
}
|
2015-03-07 13:32:04 +01:00
|
|
|
|
2012-05-30 15:01:40 +02:00
|
|
|
this.trigger('beforestatechange', {state: state, element: contentEls});
|
2011-07-15 10:37:46 +02:00
|
|
|
|
2012-05-29 11:34:47 +02:00
|
|
|
// Set Pjax headers, which can declare a preference for the returned view.
|
|
|
|
// The actually returned view isn't always decided upon when the request
|
|
|
|
// is fired, so the server might decide to change it based on its own logic.
|
2012-04-18 22:11:40 +02:00
|
|
|
headers['X-Pjax'] = fragments;
|
2015-03-07 13:32:04 +01:00
|
|
|
|
2013-11-05 11:36:46 +01:00
|
|
|
// Set 'fake' referer - we call pushState() before making the AJAX request, so we have to
|
|
|
|
// set our own referer here
|
|
|
|
if (typeof state.data.__forceReferer !== 'undefined') {
|
2014-10-16 13:24:10 +02:00
|
|
|
// Ensure query string is properly encoded if present
|
|
|
|
var url = state.data.__forceReferer;
|
2015-03-07 13:32:04 +01:00
|
|
|
|
2014-10-16 13:24:10 +02:00
|
|
|
try {
|
|
|
|
// Prevent double-encoding by attempting to decode
|
|
|
|
url = decodeURI(url);
|
|
|
|
} catch(e) {
|
|
|
|
// URL not encoded, or was encoded incorrectly, so do nothing
|
|
|
|
} finally {
|
|
|
|
// Set our referer header to the encoded URL
|
|
|
|
headers['X-Backurl'] = encodeURI(url);
|
|
|
|
}
|
2013-11-05 11:36:46 +01:00
|
|
|
}
|
2015-03-07 13:32:04 +01:00
|
|
|
|
2012-04-18 22:11:40 +02:00
|
|
|
contentEls.addClass('loading');
|
2011-06-09 03:49:52 +02:00
|
|
|
var xhr = $.ajax({
|
2012-03-24 03:19:02 +01:00
|
|
|
headers: headers,
|
2011-06-09 03:49:52 +02:00
|
|
|
url: state.url,
|
2012-05-30 15:01:40 +02:00
|
|
|
complete: function() {
|
2013-08-22 04:12:31 +02:00
|
|
|
self.setStateChangeXHR(null);
|
2012-04-18 22:11:40 +02:00
|
|
|
// Remove loading indication from old content els (regardless of which are replaced)
|
|
|
|
contentEls.removeClass('loading');
|
2011-07-15 10:37:46 +02:00
|
|
|
},
|
2012-05-30 15:01:40 +02:00
|
|
|
success: function(data, status, xhr) {
|
2013-03-14 17:28:53 +01:00
|
|
|
var els = self.handleAjaxResponse(data, status, xhr, state);
|
|
|
|
self.trigger('afterstatechange', {data: data, status: status, xhr: xhr, element: els, state: state});
|
2011-06-09 03:49:52 +02:00
|
|
|
}
|
|
|
|
});
|
2015-03-07 13:32:04 +01:00
|
|
|
|
2013-08-22 04:12:31 +02:00
|
|
|
this.setStateChangeXHR(xhr);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ALternative to loadPanel/submitForm.
|
|
|
|
*
|
|
|
|
* Triggers a parallel-fetch of a PJAX fragment, which is a separate request to the
|
|
|
|
* state change requests. There could be any amount of these fetches going on in the background,
|
|
|
|
* and they don't register as a HTML5 history states.
|
|
|
|
*
|
|
|
|
* This is meant for updating a PJAX areas that are not complete panel/form reloads. These you'd
|
|
|
|
* normally do via submitForm or loadPanel which have a lot of automation built in.
|
|
|
|
*
|
|
|
|
* On receiving successful response, the framework will update the element tagged with appropriate
|
|
|
|
* data-pjax-fragment attribute (e.g. data-pjax-fragment="<pjax-fragment-name>"). Make sure this element
|
|
|
|
* is available.
|
|
|
|
*
|
|
|
|
* Example usage:
|
|
|
|
* $('.cms-container').loadFragment('admin/foobar/', 'FragmentName');
|
|
|
|
*
|
|
|
|
* @param url string Relative or absolute url of the controller.
|
|
|
|
* @param pjaxFragments string PJAX fragment(s), comma separated.
|
|
|
|
*/
|
|
|
|
loadFragment: function(url, pjaxFragments) {
|
|
|
|
|
|
|
|
var self = this,
|
|
|
|
xhr,
|
|
|
|
headers = {},
|
|
|
|
baseUrl = $('base').attr('href'),
|
|
|
|
fragmentXHR = this.getFragmentXHR();
|
|
|
|
|
|
|
|
// Make sure only one XHR for a specific fragment is currently in progress.
|
|
|
|
if(
|
|
|
|
typeof fragmentXHR[pjaxFragments]!=='undefined' &&
|
|
|
|
fragmentXHR[pjaxFragments]!==null
|
|
|
|
) {
|
|
|
|
fragmentXHR[pjaxFragments].abort();
|
|
|
|
fragmentXHR[pjaxFragments] = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
url = $.path.isAbsoluteUrl(url) ? url : $.path.makeUrlAbsolute(url, baseUrl);
|
|
|
|
headers['X-Pjax'] = pjaxFragments;
|
|
|
|
|
|
|
|
xhr = $.ajax({
|
|
|
|
headers: headers,
|
|
|
|
url: url,
|
|
|
|
success: function(data, status, xhr) {
|
|
|
|
var elements = self.handleAjaxResponse(data, status, xhr, null);
|
|
|
|
|
|
|
|
// We are fully done now, make it possible for others to hook in here.
|
|
|
|
self.trigger('afterloadfragment', { data: data, status: status, xhr: xhr, elements: elements });
|
|
|
|
},
|
|
|
|
error: function(xhr, status, error) {
|
|
|
|
self.trigger('loadfragmenterror', { xhr: xhr, status: status, error: error });
|
|
|
|
},
|
|
|
|
complete: function() {
|
|
|
|
// Reset the current XHR in tracking object.
|
|
|
|
var fragmentXHR = self.getFragmentXHR();
|
|
|
|
if(
|
|
|
|
typeof fragmentXHR[pjaxFragments]!=='undefined' &&
|
|
|
|
fragmentXHR[pjaxFragments]!==null
|
|
|
|
) {
|
|
|
|
fragmentXHR[pjaxFragments] = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Store the fragment request so we can abort later, should we get a duplicate request.
|
|
|
|
fragmentXHR[pjaxFragments] = xhr;
|
2013-08-22 23:39:38 +02:00
|
|
|
|
|
|
|
return xhr;
|
2012-03-24 02:20:33 +01:00
|
|
|
},
|
2012-04-18 22:11:40 +02:00
|
|
|
|
2012-05-30 15:01:40 +02:00
|
|
|
/**
|
|
|
|
* Handles ajax responses containing plain HTML, or mulitple
|
|
|
|
* PJAX fragments wrapped in JSON (see PjaxResponseNegotiator PHP class).
|
|
|
|
* Can be hooked into an ajax 'success' callback.
|
2013-03-14 17:28:53 +01:00
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* (Object) data
|
|
|
|
* (String) status
|
|
|
|
* (XMLHTTPRequest) xhr
|
|
|
|
* (Object) state The original history state which the request was initiated with
|
2012-05-30 15:01:40 +02:00
|
|
|
*/
|
2013-03-14 17:28:53 +01:00
|
|
|
handleAjaxResponse: function(data, status, xhr, state) {
|
2012-11-21 22:01:02 +01:00
|
|
|
var self = this, url, selectedTabs, guessFragment;
|
2012-05-30 15:01:40 +02:00
|
|
|
|
2013-01-03 20:43:25 +01:00
|
|
|
// Support a full reload
|
|
|
|
if(xhr.getResponseHeader('X-Reload') && xhr.getResponseHeader('X-ControllerURL')) {
|
2015-04-27 16:38:45 +02:00
|
|
|
var baseUrl = $('base').attr('href'),
|
|
|
|
rawURL = xhr.getResponseHeader('X-ControllerURL'),
|
|
|
|
url = $.path.isAbsoluteUrl(rawURL) ? rawURL : $.path.makeUrlAbsolute(rawURL, baseUrl);
|
|
|
|
|
|
|
|
document.location.href = url;
|
2013-01-03 20:43:25 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-05-30 15:01:40 +02:00
|
|
|
// Pseudo-redirects via X-ControllerURL might return empty data, in which
|
|
|
|
// case we'll ignore the response
|
|
|
|
if(!data) return;
|
|
|
|
|
|
|
|
// Update title
|
|
|
|
var title = xhr.getResponseHeader('X-Title');
|
2012-12-17 10:41:07 +01:00
|
|
|
if(title) document.title = decodeURIComponent(title.replace(/\+/g, ' '));
|
2012-05-30 15:01:40 +02:00
|
|
|
|
2012-06-12 17:13:36 +02:00
|
|
|
var newFragments = {}, newContentEls;
|
2012-09-30 05:32:22 +02:00
|
|
|
// If content type is text/json (ignoring charset and other parameters)
|
2014-08-10 23:23:52 +02:00
|
|
|
if(xhr.getResponseHeader('Content-Type').match(/^((text)|(application))\/json[ \t]*;?/i)) {
|
2012-05-30 15:01:40 +02:00
|
|
|
newFragments = data;
|
|
|
|
} else {
|
2015-03-07 13:32:04 +01:00
|
|
|
|
2012-05-30 15:01:40 +02:00
|
|
|
// Fall back to replacing the content fragment if HTML is returned
|
2014-08-10 23:23:52 +02:00
|
|
|
var fragment = document.createDocumentFragment();
|
|
|
|
jQuery.clean( [ data ], document, fragment, [] );
|
|
|
|
$data = $(jQuery.merge( [], fragment.childNodes ));
|
2012-07-27 01:13:23 +02:00
|
|
|
|
|
|
|
// Try and guess the fragment if none is provided
|
|
|
|
// TODO: data-pjax-fragment might actually give us the fragment. For now we just check most common case
|
|
|
|
guessFragment = 'Content';
|
|
|
|
if ($data.is('form') && !$data.is('[data-pjax-fragment~=Content]')) guessFragment = 'CurrentForm';
|
|
|
|
|
2012-07-18 16:49:03 +02:00
|
|
|
newFragments[guessFragment] = $data;
|
2012-05-30 15:01:40 +02:00
|
|
|
}
|
|
|
|
|
2013-08-12 04:18:18 +02:00
|
|
|
this.setRedrawSuppression(true);
|
|
|
|
try {
|
|
|
|
// Replace each fragment individually
|
|
|
|
$.each(newFragments, function(newFragment, html) {
|
|
|
|
var contentEl = $('[data-pjax-fragment]').filter(function() {
|
|
|
|
return $.inArray(newFragment, $(this).data('pjaxFragment').split(' ')) != -1;
|
|
|
|
}), newContentEl = $(html);
|
|
|
|
|
|
|
|
// Add to result collection
|
|
|
|
if(newContentEls) newContentEls.add(newContentEl);
|
|
|
|
else newContentEls = newContentEl;
|
|
|
|
|
|
|
|
// Update panels
|
|
|
|
if(newContentEl.find('.cms-container').length) {
|
|
|
|
throw 'Content loaded via ajax is not allowed to contain tags matching the ".cms-container" selector to avoid infinite loops';
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set loading state and store element state
|
|
|
|
var origStyle = contentEl.attr('style');
|
|
|
|
var origParent = contentEl.parent();
|
|
|
|
var origParentLayoutApplied = (typeof origParent.data('jlayout')!=='undefined');
|
|
|
|
var layoutClasses = ['east', 'west', 'center', 'north', 'south', 'column-hidden'];
|
|
|
|
var elemClasses = contentEl.attr('class');
|
|
|
|
var origLayoutClasses = [];
|
|
|
|
if(elemClasses) {
|
|
|
|
origLayoutClasses = $.grep(
|
|
|
|
elemClasses.split(' '),
|
|
|
|
function(val) { return ($.inArray(val, layoutClasses) >= 0);}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
newContentEl
|
|
|
|
.removeClass(layoutClasses.join(' '))
|
|
|
|
.addClass(origLayoutClasses.join(' '));
|
|
|
|
if(origStyle) newContentEl.attr('style', origStyle);
|
|
|
|
|
|
|
|
// Allow injection of inline styles, as they're not allowed in the document body.
|
|
|
|
// Not handling this through jQuery.ondemand to avoid parsing the DOM twice.
|
|
|
|
var styles = newContentEl.find('style').detach();
|
|
|
|
if(styles.length) $(document).find('head').append(styles);
|
|
|
|
|
|
|
|
// Replace panel completely (we need to override the "layout" attribute, so can't replace the child instead)
|
|
|
|
contentEl.replaceWith(newContentEl);
|
|
|
|
|
|
|
|
// Force jlayout to rebuild internal hierarchy to point to the new elements.
|
|
|
|
// This is only necessary for elements that are at least 3 levels deep. 2nd level elements will
|
|
|
|
// be taken care of when we lay out the top level element (.cms-container).
|
|
|
|
if (!origParent.is('.cms-container') && origParentLayoutApplied) {
|
|
|
|
origParent.layout();
|
|
|
|
}
|
|
|
|
});
|
2012-05-30 15:01:40 +02:00
|
|
|
|
2013-08-12 04:18:18 +02:00
|
|
|
// Re-init tabs (in case the form tag itself is a tabset)
|
|
|
|
var newForm = newContentEls.filter('form');
|
|
|
|
if(newForm.hasClass('cms-tabset')) newForm.removeClass('cms-tabset').addClass('cms-tabset');
|
|
|
|
}
|
|
|
|
finally {
|
|
|
|
this.setRedrawSuppression(false);
|
|
|
|
}
|
2012-07-13 16:46:23 +02:00
|
|
|
|
2012-05-30 15:01:40 +02:00
|
|
|
this.redraw();
|
2013-04-11 18:25:15 +02:00
|
|
|
this.restoreTabState((state && typeof state.data.tabState !== 'undefined') ? state.data.tabState : null);
|
2012-07-13 16:46:23 +02:00
|
|
|
|
2012-05-30 15:01:40 +02:00
|
|
|
return newContentEls;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2015-03-07 13:32:04 +01:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* Parameters:
|
2012-05-30 15:01:40 +02:00
|
|
|
* - fragments {Array}
|
|
|
|
* Returns: jQuery collection
|
|
|
|
*/
|
|
|
|
_findFragments: function(fragments) {
|
|
|
|
return $('[data-pjax-fragment]').filter(function() {
|
|
|
|
// Allows for more than one fragment per node
|
|
|
|
var i, nodeFragments = $(this).data('pjaxFragment').split(' ');
|
|
|
|
for(i in fragments) {
|
|
|
|
if($.inArray(fragments[i], nodeFragments) != -1) return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2012-03-24 02:20:33 +01:00
|
|
|
/**
|
|
|
|
* Function: refresh
|
2015-03-07 13:32:04 +01:00
|
|
|
*
|
2012-03-24 02:20:33 +01:00
|
|
|
* Updates the container based on the current url
|
|
|
|
*
|
|
|
|
* Returns: void
|
|
|
|
*/
|
|
|
|
refresh: function() {
|
|
|
|
$(window).trigger('statechange');
|
2015-03-07 13:32:04 +01:00
|
|
|
|
2012-03-24 02:20:33 +01:00
|
|
|
$(this).redraw();
|
2012-07-13 16:46:23 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Save tab selections in order to reconstruct them later.
|
|
|
|
* Requires HTML5 sessionStorage support.
|
|
|
|
*/
|
|
|
|
saveTabState: function() {
|
2012-12-17 11:31:39 +01:00
|
|
|
if(typeof(window.sessionStorage)=="undefined" || window.sessionStorage === null) return;
|
2012-07-13 16:46:23 +02:00
|
|
|
|
2012-11-21 22:01:02 +01:00
|
|
|
var selectedTabs = [], url = this._tabStateUrl();
|
2012-07-13 16:46:23 +02:00
|
|
|
this.find('.cms-tabset,.ss-tabset').each(function(i, el) {
|
|
|
|
var id = $(el).attr('id');
|
|
|
|
if(!id) return; // we need a unique reference
|
2012-07-13 17:34:55 +02:00
|
|
|
if(!$(el).data('tabs')) return; // don't act on uninit'ed controls
|
2012-12-10 23:30:06 +01:00
|
|
|
|
|
|
|
// Allow opt-out via data element or entwine property.
|
|
|
|
if($(el).data('ignoreTabState') || $(el).getIgnoreTabState()) return;
|
|
|
|
|
2012-11-21 22:01:02 +01:00
|
|
|
selectedTabs.push({id:id, selected:$(el).tabs('option', 'selected')});
|
2012-07-13 16:46:23 +02:00
|
|
|
});
|
2012-11-30 03:32:03 +01:00
|
|
|
|
2012-11-21 22:01:02 +01:00
|
|
|
if(selectedTabs) {
|
2012-11-30 03:32:03 +01:00
|
|
|
var tabsUrl = 'tabs-' + url;
|
|
|
|
try {
|
2012-11-21 22:01:02 +01:00
|
|
|
window.sessionStorage.setItem(tabsUrl, JSON.stringify(selectedTabs));
|
2012-11-30 03:32:03 +01:00
|
|
|
} catch(err) {
|
|
|
|
if (err.code === DOMException.QUOTA_EXCEEDED_ERR && window.sessionStorage.length === 0) {
|
2015-03-07 13:32:04 +01:00
|
|
|
// If this fails we ignore the error as the only issue is that it
|
2012-11-30 03:32:03 +01:00
|
|
|
// does not remember the tab state.
|
|
|
|
// This is a Safari bug which happens when private browsing is enabled.
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-07-13 16:46:23 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Re-select previously saved tabs.
|
|
|
|
* Requires HTML5 sessionStorage support.
|
2013-03-14 17:28:53 +01:00
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* (Object) Map of tab container selectors to tab selectors.
|
|
|
|
* Used to mark a specific tab as active regardless of the previously saved options.
|
2012-07-13 16:46:23 +02:00
|
|
|
*/
|
2013-03-14 17:28:53 +01:00
|
|
|
restoreTabState: function(overrideStates) {
|
2012-07-13 16:46:23 +02:00
|
|
|
var self = this, url = this._tabStateUrl(),
|
2013-03-14 17:28:53 +01:00
|
|
|
hasSessionStorage = (typeof(window.sessionStorage)!=="undefined" && window.sessionStorage),
|
|
|
|
sessionData = hasSessionStorage ? window.sessionStorage.getItem('tabs-' + url) : null,
|
|
|
|
sessionStates = sessionData ? JSON.parse(sessionData) : false;
|
|
|
|
|
2013-10-11 16:09:31 +02:00
|
|
|
this.find('.cms-tabset, .ss-tabset').each(function() {
|
2015-01-29 01:18:39 +01:00
|
|
|
var index,
|
|
|
|
tabset = $(this),
|
|
|
|
tabsetId = tabset.attr('id'),
|
|
|
|
tab,
|
|
|
|
forcedTab = tabset.children('ul').children('li.ss-tabs-force-active');
|
2013-03-14 17:28:53 +01:00
|
|
|
|
2014-10-26 02:21:47 +01:00
|
|
|
if(!tabset.data('tabs')){
|
|
|
|
return; // don't act on uninit'ed controls
|
|
|
|
}
|
2013-03-14 17:28:53 +01:00
|
|
|
|
2013-05-16 01:12:53 +02:00
|
|
|
// The tabs may have changed, notify the widget that it should update its internal state.
|
|
|
|
tabset.tabs('refresh');
|
|
|
|
|
2015-01-29 01:18:39 +01:00
|
|
|
// Make sure the intended tab is selected. Only force the tab on the correct tabset though
|
2013-03-14 17:28:53 +01:00
|
|
|
if(forcedTab.length) {
|
2015-01-29 01:18:39 +01:00
|
|
|
index = forcedTab.first().index();
|
2013-03-14 17:28:53 +01:00
|
|
|
} else if(overrideStates && overrideStates[tabsetId]) {
|
|
|
|
tab = tabset.find(overrideStates[tabsetId].tabSelector);
|
2014-10-26 02:21:47 +01:00
|
|
|
if(tab.length){
|
|
|
|
index = tab.index();
|
|
|
|
}
|
2013-03-14 17:28:53 +01:00
|
|
|
} else if(sessionStates) {
|
2015-01-29 01:18:39 +01:00
|
|
|
$.each(sessionStates, function(i, state) {
|
2016-04-26 01:09:33 +02:00
|
|
|
if(tabsetId == state.id){
|
|
|
|
index = state.selected;
|
2014-10-26 02:21:47 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if(index !== null){
|
|
|
|
tabset.tabs('option', 'active', index);
|
|
|
|
self.trigger('tabstaterestored');
|
|
|
|
}
|
2013-03-14 17:28:53 +01:00
|
|
|
});
|
2012-07-13 16:46:23 +02:00
|
|
|
},
|
|
|
|
|
2012-07-18 16:49:23 +02:00
|
|
|
/**
|
|
|
|
* Remove any previously saved state.
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* (String) url Optional (sanitized) URL to clear a specific state.
|
|
|
|
*/
|
|
|
|
clearTabState: function(url) {
|
|
|
|
if(typeof(window.sessionStorage)=="undefined") return;
|
|
|
|
|
|
|
|
var s = window.sessionStorage;
|
|
|
|
if(url) {
|
2015-03-07 13:32:04 +01:00
|
|
|
s.removeItem('tabs-' + url);
|
2012-07-18 16:49:23 +02:00
|
|
|
} else {
|
2013-06-15 11:16:11 +02:00
|
|
|
for(var i=0;i<s.length;i++) {
|
|
|
|
if(s.key(i).match(/^tabs-/)) s.removeItem(s.key(i));
|
2013-06-19 14:03:43 +02:00
|
|
|
}
|
2012-07-18 16:49:23 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove tab state for the current URL.
|
|
|
|
*/
|
|
|
|
clearCurrentTabState: function() {
|
|
|
|
this.clearTabState(this._tabStateUrl());
|
|
|
|
},
|
|
|
|
|
2012-07-13 16:46:23 +02:00
|
|
|
_tabStateUrl: function() {
|
|
|
|
return History.getState().url
|
|
|
|
.replace(/\?.*/, '')
|
|
|
|
.replace(/#.*/, '')
|
|
|
|
.replace($('base').attr('href'), '');
|
2014-10-06 05:01:33 +02:00
|
|
|
},
|
2015-03-07 13:32:04 +01:00
|
|
|
|
2014-10-06 05:01:33 +02:00
|
|
|
showLoginDialog: function() {
|
|
|
|
var tempid = $('body').data('member-tempid'),
|
|
|
|
dialog = $('.leftandmain-logindialog'),
|
|
|
|
url = 'CMSSecurity/login';
|
|
|
|
|
|
|
|
// Force regeneration of any existing dialog
|
|
|
|
if(dialog.length) dialog.remove();
|
2015-03-07 13:32:04 +01:00
|
|
|
|
2014-10-06 05:01:33 +02:00
|
|
|
// Join url params
|
|
|
|
url = $.path.addSearchParams(url, {
|
|
|
|
'tempid': tempid,
|
|
|
|
'BackURL': window.location.href
|
|
|
|
});
|
2015-03-07 13:32:04 +01:00
|
|
|
|
2014-10-06 05:01:33 +02:00
|
|
|
// Show a placeholder for instant feedback. Will be replaced with actual
|
|
|
|
// form dialog once its loaded.
|
|
|
|
dialog = $('<div class="leftandmain-logindialog"></div>');
|
|
|
|
dialog.attr('id', new Date().getTime());
|
|
|
|
dialog.data('url', url);
|
|
|
|
$('body').append(dialog);
|
|
|
|
}
|
|
|
|
});
|
2015-03-07 13:32:04 +01:00
|
|
|
|
2014-10-06 05:01:33 +02:00
|
|
|
// Login dialog page
|
|
|
|
$('.leftandmain-logindialog').entwine({
|
|
|
|
onmatch: function() {
|
|
|
|
this._super();
|
2015-03-07 13:32:04 +01:00
|
|
|
|
2014-10-06 05:01:33 +02:00
|
|
|
// Create jQuery dialog
|
|
|
|
this.ssdialog({
|
|
|
|
iframeUrl: this.data('url'),
|
|
|
|
dialogClass: "leftandmain-logindialog-dialog",
|
|
|
|
autoOpen: true,
|
|
|
|
minWidth: 500,
|
|
|
|
maxWidth: 500,
|
|
|
|
minHeight: 370,
|
|
|
|
maxHeight: 400,
|
|
|
|
closeOnEscape: false,
|
|
|
|
open: function() {
|
|
|
|
$('.ui-widget-overlay').addClass('leftandmain-logindialog-overlay');
|
|
|
|
},
|
|
|
|
close: function() {
|
|
|
|
$('.ui-widget-overlay').removeClass('leftandmain-logindialog-overlay');
|
2015-03-07 13:32:04 +01:00
|
|
|
}
|
2014-10-06 05:01:33 +02:00
|
|
|
});
|
|
|
|
},
|
|
|
|
onunmatch: function() {
|
|
|
|
this._super();
|
|
|
|
},
|
|
|
|
open: function() {
|
|
|
|
this.ssdialog('open');
|
|
|
|
},
|
|
|
|
close: function() {
|
|
|
|
this.ssdialog('close');
|
|
|
|
},
|
|
|
|
toggle: function(bool) {
|
|
|
|
if(this.is(':visible')) this.close();
|
|
|
|
else this.open();
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Callback activated by CMSSecurity_success.ss
|
|
|
|
*/
|
|
|
|
reauthenticate: function(data) {
|
|
|
|
// Replace all SecurityID fields with the given value
|
|
|
|
if(typeof(data.SecurityID) !== 'undefined') {
|
|
|
|
$(':input[name=SecurityID]').val(data.SecurityID);
|
|
|
|
}
|
|
|
|
// Update TempID for current user
|
|
|
|
if(typeof(data.TempID) !== 'undefined') {
|
|
|
|
$('body').data('member-tempid', data.TempID);
|
|
|
|
}
|
|
|
|
this.close();
|
2011-03-23 10:51:00 +01:00
|
|
|
}
|
|
|
|
});
|
2015-03-07 13:32:04 +01:00
|
|
|
|
2012-05-30 15:38:08 +02:00
|
|
|
/**
|
|
|
|
* Add loading overlay to selected regions in the CMS automatically.
|
|
|
|
* Not applied to all "*.loading" elements to avoid secondary regions
|
|
|
|
* like the breadcrumbs showing unnecessary loading status.
|
|
|
|
*/
|
|
|
|
$('form.loading,.cms-content.loading,.cms-content-fields.loading,.cms-content-view.loading').entwine({
|
|
|
|
onmatch: function() {
|
|
|
|
this.append('<div class="cms-content-loading-overlay ui-widget-overlay-light"></div><div class="cms-content-loading-spinner"></div>');
|
|
|
|
this._super();
|
|
|
|
},
|
|
|
|
onunmatch: function() {
|
|
|
|
this.find('.cms-content-loading-overlay,.cms-content-loading-spinner').remove();
|
|
|
|
this._super();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2012-06-15 00:05:41 +02:00
|
|
|
/** Make all buttons "hoverable" with jQuery theming. */
|
|
|
|
$('.cms input[type="submit"], .cms button, .cms input[type="reset"], .cms .ss-ui-button').entwine({
|
2012-06-12 12:48:08 +02:00
|
|
|
onadd: function() {
|
2012-06-15 00:05:41 +02:00
|
|
|
this.addClass('ss-ui-button');
|
2012-02-17 00:35:10 +01:00
|
|
|
if(!this.data('button')) this.button();
|
2012-05-14 01:43:36 +02:00
|
|
|
this._super();
|
|
|
|
},
|
2012-06-12 12:48:08 +02:00
|
|
|
onremove: function() {
|
2016-09-16 15:21:16 +02:00
|
|
|
if(this.data('button')) this.button('destroy');
|
2012-02-17 00:35:10 +01:00
|
|
|
this._super();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2012-03-07 17:17:18 +01:00
|
|
|
/**
|
|
|
|
* Loads the link's 'href' attribute into a panel via ajax,
|
|
|
|
* as opposed to triggering a full page reload.
|
|
|
|
* Little helper to avoid repetition, and make it easy to
|
|
|
|
* "opt in" to panel loading, while by default links still exhibit their default behaviour.
|
2012-05-30 15:01:40 +02:00
|
|
|
* The PJAX target can be specified via a 'data-pjax-target' attribute.
|
2012-03-07 17:17:18 +01:00
|
|
|
*/
|
2012-04-10 08:16:03 +02:00
|
|
|
$('.cms .cms-panel-link').entwine({
|
2012-03-07 17:17:18 +01:00
|
|
|
onclick: function(e) {
|
2013-12-21 04:58:16 +01:00
|
|
|
if($(this).hasClass('external-link')) {
|
|
|
|
e.stopPropagation();
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-03-07 13:32:04 +01:00
|
|
|
var href = this.attr('href'),
|
2012-04-17 22:28:36 +02:00
|
|
|
url = (href && !href.match(/^#/)) ? href : this.data('href'),
|
2012-05-30 15:01:40 +02:00
|
|
|
data = {pjax: this.data('pjaxTarget')};
|
2012-04-10 08:16:03 +02:00
|
|
|
|
2012-03-07 17:17:18 +01:00
|
|
|
$('.cms-container').loadPanel(url, null, data);
|
|
|
|
e.preventDefault();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Does an ajax loads of the link's 'href' attribute via ajax and displays any FormResponse messages from the CMS.
|
|
|
|
* Little helper to avoid repetition, and make it easy to trigger actions via a link,
|
|
|
|
* without reloading the page, changing the URL, or loading in any new panel content.
|
|
|
|
*/
|
2012-03-24 02:20:33 +01:00
|
|
|
$('.cms .ss-ui-button-ajax').entwine({
|
2012-03-07 17:17:18 +01:00
|
|
|
onclick: function(e) {
|
2012-03-24 02:20:33 +01:00
|
|
|
$(this).removeClass('ui-button-text-only');
|
|
|
|
$(this).addClass('ss-ui-button-loading ui-button-text-icons');
|
2015-03-07 13:32:04 +01:00
|
|
|
|
2012-03-24 02:20:33 +01:00
|
|
|
var loading = $(this).find(".ss-ui-loading-icon");
|
2015-03-07 13:32:04 +01:00
|
|
|
|
2012-03-24 02:20:33 +01:00
|
|
|
if(loading.length < 1) {
|
|
|
|
loading = $("<span></span>").addClass('ss-ui-loading-icon ui-button-icon-primary ui-icon');
|
2015-03-07 13:32:04 +01:00
|
|
|
|
2012-03-24 02:20:33 +01:00
|
|
|
$(this).prepend(loading);
|
|
|
|
}
|
2015-03-07 13:32:04 +01:00
|
|
|
|
2012-03-24 02:20:33 +01:00
|
|
|
loading.show();
|
2015-03-07 13:32:04 +01:00
|
|
|
|
2012-03-07 17:17:18 +01:00
|
|
|
var href = this.attr('href'), url = href ? href : this.data('href');
|
|
|
|
|
|
|
|
jQuery.ajax({
|
|
|
|
url: url,
|
|
|
|
// Ensure that form view is loaded (rather than whole "Content" template)
|
|
|
|
complete: function(xmlhttp, status) {
|
|
|
|
var msg = (xmlhttp.getResponseHeader('X-Status')) ? xmlhttp.getResponseHeader('X-Status') : xmlhttp.responseText;
|
2015-03-07 13:32:04 +01:00
|
|
|
|
2012-03-24 02:20:33 +01:00
|
|
|
try {
|
2012-12-17 11:31:39 +01:00
|
|
|
if (typeof msg != "undefined" && msg !== null) eval(msg);
|
2012-03-24 02:20:33 +01:00
|
|
|
}
|
|
|
|
catch(e) {}
|
2015-03-07 13:32:04 +01:00
|
|
|
|
2012-03-24 02:20:33 +01:00
|
|
|
loading.hide();
|
2015-03-07 13:32:04 +01:00
|
|
|
|
2012-03-24 02:20:33 +01:00
|
|
|
$(".cms-container").refresh();
|
2015-03-07 13:32:04 +01:00
|
|
|
|
2012-03-24 02:20:33 +01:00
|
|
|
$(this).removeClass('ss-ui-button-loading ui-button-text-icons');
|
|
|
|
$(this).addClass('ui-button-text-only');
|
2012-03-07 17:17:18 +01:00
|
|
|
},
|
|
|
|
dataType: 'html'
|
|
|
|
});
|
|
|
|
e.preventDefault();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
/**
|
2012-02-08 15:34:06 +01:00
|
|
|
* Trigger dialogs with iframe based on the links href attribute (see ssui-core.js).
|
2011-03-23 10:51:00 +01:00
|
|
|
*/
|
2012-03-01 11:59:28 +01:00
|
|
|
$('.cms .ss-ui-dialog-link').entwine({
|
2012-02-08 15:34:06 +01:00
|
|
|
UUID: null,
|
2011-03-23 10:51:00 +01:00
|
|
|
onmatch: function() {
|
2012-02-08 15:34:06 +01:00
|
|
|
this._super();
|
|
|
|
this.setUUID(new Date().getTime());
|
2011-12-17 00:51:32 +01:00
|
|
|
},
|
2012-05-14 01:43:36 +02:00
|
|
|
onunmatch: function() {
|
|
|
|
this._super();
|
|
|
|
},
|
2012-02-08 15:34:06 +01:00
|
|
|
onclick: function() {
|
|
|
|
this._super();
|
2011-12-17 00:51:32 +01:00
|
|
|
|
2012-02-08 15:34:06 +01:00
|
|
|
var self = this, id = 'ss-ui-dialog-' + this.getUUID();
|
|
|
|
var dialog = $('#' + id);
|
|
|
|
if(!dialog.length) {
|
|
|
|
dialog = $('<div class="ss-ui-dialog" id="' + id + '" />');
|
|
|
|
$('body').append(dialog);
|
|
|
|
}
|
2015-03-07 13:32:04 +01:00
|
|
|
|
2012-04-03 02:48:43 +02:00
|
|
|
var extraClass = this.data('popupclass')?this.data('popupclass'):'';
|
2015-03-07 13:32:04 +01:00
|
|
|
|
2012-04-03 02:48:43 +02:00
|
|
|
dialog.ssdialog({iframeUrl: this.attr('href'), autoOpen: true, dialogExtraClass: extraClass});
|
2011-03-23 10:51:00 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
2015-03-07 13:32:04 +01:00
|
|
|
|
2012-01-23 09:01:30 +01:00
|
|
|
/**
|
|
|
|
* Add styling to all contained buttons, and create buttonsets if required.
|
|
|
|
*/
|
2012-06-06 21:02:13 +02:00
|
|
|
$('.cms-content .Actions').entwine({
|
2012-05-11 05:31:12 +02:00
|
|
|
onmatch: function() {
|
|
|
|
this.find('.ss-ui-button').click(function() {
|
|
|
|
var form = this.form;
|
2013-05-25 04:05:30 +02:00
|
|
|
|
2012-05-11 05:31:12 +02:00
|
|
|
// forms don't natively store the button they've been triggered with
|
|
|
|
if(form) {
|
|
|
|
form.clickedButton = this;
|
|
|
|
// Reset the clicked button shortly after the onsubmit handlers
|
|
|
|
// have fired on the form
|
2013-05-25 04:05:30 +02:00
|
|
|
setTimeout(function() {
|
|
|
|
form.clickedButton = null;
|
|
|
|
}, 10);
|
2014-07-14 01:15:38 +02:00
|
|
|
}
|
|
|
|
});
|
2012-01-23 09:01:30 +01:00
|
|
|
|
2012-05-11 05:31:12 +02:00
|
|
|
this.redraw();
|
|
|
|
this._super();
|
|
|
|
},
|
2012-05-14 01:43:36 +02:00
|
|
|
onunmatch: function() {
|
|
|
|
this._super();
|
|
|
|
},
|
2012-05-11 05:31:12 +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-05-11 05:31:12 +02:00
|
|
|
// Remove whitespace to avoid gaps with inline elements
|
2015-03-07 13:32:04 +01:00
|
|
|
this.contents().filter(function() {
|
|
|
|
return (this.nodeType == 3 && !/\S/.test(this.nodeValue));
|
2012-05-11 05:31:12 +02:00
|
|
|
}).remove();
|
|
|
|
|
|
|
|
// Init buttons if required
|
|
|
|
this.find('.ss-ui-button').each(function() {
|
|
|
|
if(!$(this).data('button')) $(this).button();
|
|
|
|
});
|
2015-03-07 13:32:04 +01:00
|
|
|
|
2012-05-11 05:31:12 +02:00
|
|
|
// Mark up buttonsets
|
|
|
|
this.find('.ss-ui-buttonset').buttonset();
|
|
|
|
}
|
|
|
|
});
|
2015-03-07 13:32:04 +01:00
|
|
|
|
2011-04-30 08:34:14 +02:00
|
|
|
/**
|
|
|
|
* Duplicates functionality in DateField.js, but due to using entwine we can match
|
|
|
|
* the DOM element on creation, rather than onclick - which allows us to decorate
|
|
|
|
* the field with a calendar icon
|
|
|
|
*/
|
2012-03-01 11:59:28 +01:00
|
|
|
$('.cms .field.date input.text').entwine({
|
2011-04-30 08:34:14 +02:00
|
|
|
onmatch: function() {
|
2012-02-16 17:13:33 +01:00
|
|
|
var holder = $(this).parents('.field.date:first'), config = holder.data();
|
2012-05-11 06:07:07 +02:00
|
|
|
if(!config.showcalendar) {
|
|
|
|
this._super();
|
|
|
|
return;
|
|
|
|
}
|
2011-04-30 08:34:14 +02:00
|
|
|
|
|
|
|
config.showOn = 'button';
|
|
|
|
if(config.locale && $.datepicker.regional[config.locale]) {
|
|
|
|
config = $.extend(config, $.datepicker.regional[config.locale], {});
|
|
|
|
}
|
|
|
|
|
|
|
|
$(this).datepicker(config);
|
|
|
|
// // Unfortunately jQuery UI only allows configuration of icon images, not sprites
|
|
|
|
// this.next('button').button('option', 'icons', {primary : 'ui-icon-calendar'});
|
2015-03-07 13:32:04 +01:00
|
|
|
|
2012-05-14 01:43:36 +02:00
|
|
|
this._super();
|
|
|
|
},
|
|
|
|
onunmatch: function() {
|
2011-04-30 08:34:14 +02:00
|
|
|
this._super();
|
|
|
|
}
|
2011-08-05 05:46:57 +02:00
|
|
|
});
|
2015-03-07 13:32:04 +01:00
|
|
|
|
2011-08-05 05:46:57 +02:00
|
|
|
/**
|
|
|
|
* Styled dropdown select fields via chosen. Allows things like search and optgroup
|
2015-03-07 13:32:04 +01:00
|
|
|
* selection support. Rather than manually adding classes to selects we want
|
2011-08-05 05:46:57 +02:00
|
|
|
* styled, we style everything but the ones we tell it not to.
|
|
|
|
*
|
2015-03-07 13:32:04 +01:00
|
|
|
* For the CMS we also need to tell the parent div that it has a select so
|
2011-08-05 05:46:57 +02:00
|
|
|
* we can fix the height cropping.
|
|
|
|
*/
|
2015-03-07 13:32:04 +01:00
|
|
|
|
2013-07-26 20:25:27 +02:00
|
|
|
$('.cms .field.dropdown select, .cms .field select[multiple], .fieldholder-small select.dropdown').entwine({
|
2011-08-05 05:46:57 +02:00
|
|
|
onmatch: function() {
|
2012-05-11 06:07:07 +02:00
|
|
|
if(this.is('.no-chzn')) {
|
|
|
|
this._super();
|
|
|
|
return;
|
|
|
|
}
|
2012-03-05 09:21:24 +01:00
|
|
|
|
2012-03-05 16:26:28 +01:00
|
|
|
// Explicitly disable default placeholder if no custom one is defined
|
|
|
|
if(!this.data('placeholder')) this.data('placeholder', ' ');
|
|
|
|
|
2012-05-11 06:06:25 +02:00
|
|
|
// We could've gotten stale classes and DOM elements from deferred cache.
|
|
|
|
this.removeClass('has-chzn chzn-done');
|
|
|
|
this.siblings('.chzn-container').remove();
|
|
|
|
|
2012-03-27 17:06:58 +02:00
|
|
|
// Apply Chosen
|
|
|
|
applyChosen(this);
|
2015-03-07 13:32:04 +01:00
|
|
|
|
2012-05-14 01:43:36 +02:00
|
|
|
this._super();
|
|
|
|
},
|
|
|
|
onunmatch: function() {
|
2011-08-05 05:46:57 +02:00
|
|
|
this._super();
|
|
|
|
}
|
2012-05-14 01:43:36 +02:00
|
|
|
});
|
2015-03-07 13:32:04 +01:00
|
|
|
|
2011-10-29 02:01:06 +02:00
|
|
|
$(".cms-panel-layout").entwine({
|
|
|
|
redraw: function() {
|
2012-05-30 20:57:18 +02:00
|
|
|
if(window.debug) console.log('redraw', this.attr('class'), this.get(0));
|
2011-10-29 02:01:06 +02:00
|
|
|
}
|
|
|
|
});
|
2015-03-07 13:32:04 +01:00
|
|
|
|
2012-05-11 05:31:12 +02:00
|
|
|
/**
|
|
|
|
* Overload the default GridField behaviour (open a new URL in the browser)
|
|
|
|
* with the CMS-specific ajax loading.
|
|
|
|
*/
|
|
|
|
$('.cms .ss-gridfield').entwine({
|
|
|
|
showDetailView: function(url) {
|
|
|
|
// Include any GET parameters from the current URL, as the view state might depend on it.
|
|
|
|
// For example, a list prefiltered through external search criteria might be passed to GridField.
|
2012-07-13 17:33:48 +02:00
|
|
|
var params = window.location.search.replace(/^\?/, '');
|
|
|
|
if(params) url = $.path.addSearchParams(url, params);
|
2012-05-11 05:31:12 +02:00
|
|
|
$('.cms-container').loadPanel(url);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2012-02-17 00:38:21 +01:00
|
|
|
|
2012-05-11 05:31:12 +02:00
|
|
|
/**
|
|
|
|
* Generic search form in the CMS, often hooked up to a GridField results display.
|
2015-03-07 13:32:04 +01:00
|
|
|
*/
|
2012-05-11 05:31:12 +02:00
|
|
|
$('.cms-search-form').entwine({
|
2013-05-25 04:05:30 +02:00
|
|
|
onsubmit: function(e) {
|
2012-05-11 05:31:12 +02:00
|
|
|
// Remove empty elements and make the URL prettier
|
2013-05-25 04:05:30 +02:00
|
|
|
var nonEmptyInputs,
|
|
|
|
url;
|
|
|
|
|
|
|
|
nonEmptyInputs = this.find(':input:not(:submit)').filter(function() {
|
2012-05-11 05:31:12 +02:00
|
|
|
// Use fieldValue() from jQuery.form plugin rather than jQuery.val(),
|
|
|
|
// as it handles checkbox values more consistently
|
|
|
|
var vals = $.grep($(this).fieldValue(), function(val) { return (val);});
|
|
|
|
return (vals.length);
|
|
|
|
});
|
2013-05-25 04:05:30 +02:00
|
|
|
|
|
|
|
url = this.attr('action');
|
|
|
|
|
|
|
|
if(nonEmptyInputs.length) {
|
|
|
|
url = $.path.addSearchParams(url, nonEmptyInputs.serialize());
|
|
|
|
}
|
2012-02-17 00:38:21 +01:00
|
|
|
|
2012-05-11 05:31:12 +02:00
|
|
|
var container = this.closest('.cms-container');
|
|
|
|
container.find('.cms-edit-form').tabs('select',0); //always switch to the first tab (list view) when searching
|
2013-05-25 04:05:30 +02:00
|
|
|
container.loadPanel(url, "", {}, true);
|
2012-05-11 05:31:12 +02:00
|
|
|
|
2013-05-25 04:05:30 +02:00
|
|
|
return false;
|
2012-05-11 05:31:12 +02:00
|
|
|
}
|
|
|
|
});
|
2012-02-17 00:38:21 +01:00
|
|
|
|
2013-05-25 04:05:30 +02:00
|
|
|
/**
|
|
|
|
* Reset button handler. IE8 does not bubble reset events to
|
|
|
|
*/
|
2013-06-13 00:45:41 +02:00
|
|
|
$(".cms-search-form button[type=reset], .cms-search-form input[type=reset]").entwine({
|
2013-05-25 04:05:30 +02:00
|
|
|
onclick: function(e) {
|
|
|
|
e.preventDefault();
|
2012-03-02 15:20:17 +01:00
|
|
|
|
2013-05-25 04:05:30 +02:00
|
|
|
var form = $(this).parents('form');
|
|
|
|
|
|
|
|
form.clearForm();
|
2013-06-13 00:45:41 +02:00
|
|
|
form.find(".dropdown select").prop('selectedIndex', 0).trigger("liszt:updated"); // Reset chosen.js
|
2013-05-25 04:05:30 +02:00
|
|
|
form.submit();
|
2013-02-19 17:10:19 +01:00
|
|
|
}
|
2015-06-26 02:45:02 +02:00
|
|
|
});
|
2013-05-25 04:05:30 +02:00
|
|
|
|
2012-05-11 05:31:12 +02:00
|
|
|
/**
|
|
|
|
* Allows to lazy load a panel, by leaving it empty
|
|
|
|
* and declaring a URL to load its content via a 'url' HTML5 data attribute.
|
|
|
|
* The loaded HTML is cached, with cache key being the 'url' attribute.
|
|
|
|
* In order for this to work consistently, we assume that the responses are stateless.
|
|
|
|
* To avoid caching, add a 'deferred-no-cache' to the node.
|
|
|
|
*/
|
|
|
|
window._panelDeferredCache = {};
|
|
|
|
$('.cms-panel-deferred').entwine({
|
2012-06-12 12:48:08 +02:00
|
|
|
onadd: function() {
|
2012-05-11 05:31:12 +02:00
|
|
|
this._super();
|
|
|
|
this.redraw();
|
|
|
|
},
|
2012-06-12 12:48:08 +02:00
|
|
|
onremove: function() {
|
2012-06-14 10:08:16 +02:00
|
|
|
if(window.debug) console.log('saving', this.data('url'), this);
|
2015-03-07 13:32:04 +01:00
|
|
|
|
2012-05-11 05:31:12 +02:00
|
|
|
// Save the HTML state at the last possible moment.
|
|
|
|
// Don't store the DOM to avoid memory leaks.
|
|
|
|
if(!this.data('deferredNoCache')) window._panelDeferredCache[this.data('url')] = this.html();
|
|
|
|
this._super();
|
|
|
|
},
|
|
|
|
redraw: function() {
|
2012-05-30 20:57:18 +02:00
|
|
|
if(window.debug) console.log('redraw', this.attr('class'), this.get(0));
|
|
|
|
|
2012-05-11 05:31:12 +02:00
|
|
|
var self = this, url = this.data('url');
|
|
|
|
if(!url) throw 'Elements of class .cms-panel-deferred need a "data-url" attribute';
|
2012-04-17 17:17:45 +02:00
|
|
|
|
2012-05-11 05:31:12 +02:00
|
|
|
this._super();
|
|
|
|
|
|
|
|
// If the node is empty, try to either load it from cache or via ajax.
|
|
|
|
if(!this.children().length) {
|
|
|
|
if(!this.data('deferredNoCache') && typeof window._panelDeferredCache[url] !== 'undefined') {
|
|
|
|
this.html(window._panelDeferredCache[url]);
|
|
|
|
} else {
|
|
|
|
this.addClass('loading');
|
|
|
|
$.ajax({
|
|
|
|
url: url,
|
|
|
|
complete: function() {
|
|
|
|
self.removeClass('loading');
|
|
|
|
},
|
|
|
|
success: function(data, status, xhr) {
|
|
|
|
self.html(data);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2012-04-17 17:17:45 +02:00
|
|
|
}
|
|
|
|
}
|
2012-05-11 05:31:12 +02:00
|
|
|
});
|
2012-04-17 22:29:45 +02:00
|
|
|
|
|
|
|
/**
|
2012-05-11 05:31:12 +02:00
|
|
|
* Lightweight wrapper around jQuery UI tabs.
|
|
|
|
* Ensures that anchor links are set properly,
|
|
|
|
* and any nested tabs are scrolled if they have
|
|
|
|
* their height explicitly set. This is important
|
|
|
|
* for forms inside the CMS layout.
|
2012-04-17 22:29:45 +02:00
|
|
|
*/
|
2012-05-11 05:31:12 +02:00
|
|
|
$('.cms-tabset').entwine({
|
2012-06-12 12:48:08 +02:00
|
|
|
onadd: function() {
|
2012-05-11 05:31:12 +02:00
|
|
|
// Can't name redraw() as it clashes with other CMS entwine classes
|
|
|
|
this.redrawTabs();
|
|
|
|
this._super();
|
|
|
|
},
|
2012-06-12 12:48:08 +02:00
|
|
|
onremove: function() {
|
2013-04-09 12:21:54 +02:00
|
|
|
if (this.data('tabs')) this.tabs('destroy');
|
2012-05-11 07:36:18 +02:00
|
|
|
this._super();
|
|
|
|
},
|
2012-05-11 05:31:12 +02:00
|
|
|
redrawTabs: function() {
|
|
|
|
this.rewriteHashlinks();
|
|
|
|
|
2012-11-06 22:53:34 +01:00
|
|
|
var id = this.attr('id'), activeTab = this.find('ul:first .ui-tabs-active');
|
2012-05-11 05:31:12 +02:00
|
|
|
|
2012-11-06 22:53:34 +01:00
|
|
|
if(!this.data('uiTabs')) this.tabs({
|
|
|
|
active: (activeTab.index() != -1) ? activeTab.index() : 0,
|
2012-11-12 04:38:18 +01:00
|
|
|
beforeLoad: function(e, ui) {
|
2015-03-07 13:32:04 +01:00
|
|
|
// Disable automatic ajax loading of tabs without matching DOM elements,
|
2012-12-04 14:26:19 +01:00
|
|
|
// determining if the current URL differs from the tab URL is too error prone.
|
2012-11-06 15:28:09 +01:00
|
|
|
return false;
|
2012-05-11 05:31:12 +02:00
|
|
|
},
|
2012-11-06 22:53:34 +01:00
|
|
|
activate: function(e, ui) {
|
2013-06-15 12:23:51 +02:00
|
|
|
// Accessibility: Simulate click to trigger panel load when tab is focused
|
|
|
|
// by a keyboard navigation event rather than a click
|
|
|
|
if(ui.newTab) {
|
|
|
|
ui.newTab.find('.cms-panel-link').click();
|
|
|
|
}
|
|
|
|
|
2012-09-28 16:32:16 +02:00
|
|
|
// Usability: Hide actions for "readonly" tabs (which don't contain any editable fields)
|
|
|
|
var actions = $(this).closest('form').find('.Actions');
|
2012-11-21 22:01:02 +01:00
|
|
|
if($(ui.newTab).closest('li').hasClass('readonly')) {
|
2012-09-28 16:32:16 +02:00
|
|
|
actions.fadeOut();
|
|
|
|
} else {
|
|
|
|
actions.show();
|
|
|
|
}
|
|
|
|
}
|
2012-05-11 05:31:12 +02:00
|
|
|
});
|
2016-04-28 05:57:58 +02:00
|
|
|
|
|
|
|
this.trigger('afterredrawtabs');
|
2012-05-11 05:31:12 +02:00
|
|
|
},
|
2015-03-07 13:32:04 +01:00
|
|
|
|
2012-05-11 05:31:12 +02:00
|
|
|
/**
|
2012-11-06 15:28:09 +01:00
|
|
|
* Ensure hash links are prefixed with the current page URL,
|
|
|
|
* otherwise jQuery interprets them as being external.
|
2012-05-11 05:31:12 +02:00
|
|
|
*/
|
|
|
|
rewriteHashlinks: function() {
|
|
|
|
$(this).find('ul a').each(function() {
|
2012-12-04 05:44:13 +01:00
|
|
|
if (!$(this).attr('href')) return;
|
2012-11-06 15:28:09 +01:00
|
|
|
var matches = $(this).attr('href').match(/#.*/);
|
|
|
|
if(!matches) return;
|
|
|
|
$(this).attr('href', document.location.href.replace(/#.*/, '') + matches[0]);
|
2012-05-11 05:31:12 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2015-06-16 00:52:42 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* CMS content filters
|
|
|
|
*/
|
|
|
|
$('#filters-button').entwine({
|
|
|
|
onmatch: function () {
|
|
|
|
this._super();
|
|
|
|
|
2015-06-26 02:45:02 +02:00
|
|
|
this.data('collapsed', true); // The current collapsed state of the element.
|
2015-06-16 00:52:42 +02:00
|
|
|
this.data('animating', false); // True if the element is currently animating.
|
|
|
|
},
|
|
|
|
onunmatch: function () {
|
|
|
|
this._super();
|
|
|
|
},
|
|
|
|
showHide: function () {
|
|
|
|
var self = this,
|
|
|
|
$filters = $('.cms-content-filters').first(),
|
|
|
|
collapsed = this.data('collapsed');
|
|
|
|
|
|
|
|
// Prevent the user from spamming the UI with animation requests.
|
|
|
|
if (this.data('animating')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-06-26 02:45:02 +02:00
|
|
|
this.toggleClass('active');
|
|
|
|
this.data('animating', true);
|
|
|
|
|
2015-06-16 00:52:42 +02:00
|
|
|
// Slide the element down / up based on it's current collapsed state.
|
|
|
|
$filters[collapsed ? 'slideDown' : 'slideUp']({
|
|
|
|
complete: function () {
|
|
|
|
// Update the element's state.
|
|
|
|
self.data('collapsed', !collapsed);
|
|
|
|
self.data('animating', false);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
onclick: function () {
|
|
|
|
this.showHide();
|
|
|
|
}
|
|
|
|
});
|
2012-04-17 22:29:45 +02:00
|
|
|
});
|
2015-03-07 13:32:04 +01:00
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
}(jQuery));
|
|
|
|
|
|
|
|
var statusMessage = function(text, type) {
|
2013-02-19 09:20:29 +01:00
|
|
|
text = jQuery('<div/>').text(text).html(); // Escape HTML entities in text
|
2015-09-02 00:12:45 +02:00
|
|
|
jQuery.noticeAdd({text: text, type: type, stayTime: 5000, inEffect: {left: '0', opacity: 'show'}});
|
2011-03-23 10:51:00 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
var errorMessage = function(text) {
|
2015-09-02 00:12:45 +02:00
|
|
|
jQuery.noticeAdd({text: text, type: 'error', stayTime: 5000, inEffect: {left: '0', opacity: 'show'}});
|
2012-04-02 04:29:02 +02:00
|
|
|
};
|