2011-03-23 10:51:00 +01:00
|
|
|
/**
|
|
|
|
* File: LeftAndMain.EditForm.js
|
|
|
|
*/
|
|
|
|
(function($) {
|
2012-03-04 21:58:00 +01:00
|
|
|
|
|
|
|
// Can't bind this through jQuery
|
|
|
|
window.onbeforeunload = function(e) {
|
|
|
|
var form = $('.cms-edit-form');
|
2012-06-12 17:13:36 +02:00
|
|
|
form.trigger('beforesubmitform');
|
2012-03-04 21:58:00 +01:00
|
|
|
if(form.is('.changed')) return ss.i18n._t('LeftAndMain.CONFIRMUNSAVEDSHORT');
|
|
|
|
};
|
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
$.entwine('ss', function($){
|
|
|
|
|
|
|
|
/**
|
2011-03-31 10:52:29 +02:00
|
|
|
* Class: .cms-edit-form
|
2011-03-23 10:51:00 +01:00
|
|
|
*
|
|
|
|
* Base edit form, provides ajaxified saving
|
|
|
|
* and reloading itself through the ajax return values.
|
|
|
|
* Takes care of resizing tabsets within the layout container.
|
2012-05-06 04:45:25 +02:00
|
|
|
*
|
|
|
|
* Change tracking is enabled on all fields within the form. If you want
|
|
|
|
* to disable change tracking for a specific field, add a "no-change-track"
|
|
|
|
* class to it.
|
|
|
|
*
|
2011-03-23 10:51:00 +01:00
|
|
|
* @name ss.Form_EditForm
|
|
|
|
* @require jquery.changetracker
|
|
|
|
*
|
|
|
|
* Events:
|
|
|
|
* ajaxsubmit - Form is about to be submitted through ajax
|
|
|
|
* validate - Contains validation result
|
|
|
|
* load - Form is about to be loaded through ajax
|
|
|
|
*/
|
2011-03-31 10:52:29 +02:00
|
|
|
$('.cms-edit-form').entwine(/** @lends ss.Form_EditForm */{
|
2011-03-23 10:51:00 +01:00
|
|
|
/**
|
|
|
|
* Variable: PlaceholderHtml
|
|
|
|
* (String_ HTML text to show when no form content is chosen.
|
|
|
|
* Will show inside the <form> tag.
|
|
|
|
*/
|
|
|
|
PlaceholderHtml: '',
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Variable: ChangeTrackerOptions
|
|
|
|
* (Object)
|
|
|
|
*/
|
2012-02-03 00:59:40 +01:00
|
|
|
ChangeTrackerOptions: {
|
2012-12-04 04:50:35 +01:00
|
|
|
ignoreFieldSelector: '.no-change-track, .ss-upload :input, .cms-navigator :input'
|
2012-02-03 00:59:40 +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-12-15 11:38:29 +01:00
|
|
|
|
|
|
|
// Turn off autocomplete to fix the access tab randomly switching radio buttons in Firefox
|
|
|
|
// when refresh the page with an anchor tag in the URL. E.g: /admin#Root_Access.
|
|
|
|
// Autocomplete in the CMS also causes strangeness in other browsers,
|
|
|
|
// filling out sections of the form that the user does not want to be filled out,
|
|
|
|
// so this turns it off for all browsers.
|
|
|
|
// See the following page for demo and explanation of the Firefox bug:
|
|
|
|
// http://www.ryancramer.com/journal/entries/radio_buttons_firefox/
|
|
|
|
this.attr("autocomplete", "off");
|
2011-03-23 10:51:00 +01:00
|
|
|
|
|
|
|
this._setupChangeTracker();
|
|
|
|
|
2011-12-15 11:38:29 +01:00
|
|
|
// Catch navigation events before they reach handleStateChange(),
|
|
|
|
// in order to avoid changing the menu state if the action is cancelled by the user
|
2012-02-21 17:46:58 +01:00
|
|
|
// $('.cms-menu')
|
2011-06-08 01:35:56 +02:00
|
|
|
|
|
|
|
// Optionally get the form attributes from embedded fields, see Form->formHtmlContent()
|
|
|
|
for(var overrideAttr in {'action':true,'method':true,'enctype':true,'name':true}) {
|
|
|
|
var el = this.find(':input[name='+ '_form_' + overrideAttr + ']');
|
|
|
|
if(el) {
|
|
|
|
this.attr(overrideAttr, el.val());
|
|
|
|
el.remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO
|
|
|
|
// // Rewrite # links
|
|
|
|
// html = html.replace(/(<a[^>]+href *= *")#/g, '$1' + window.location.href.replace(/#.*$/,'') + '#');
|
|
|
|
//
|
|
|
|
// // Rewrite iframe links (for IE)
|
|
|
|
// html = html.replace(/(<iframe[^>]*src=")([^"]+)("[^>]*>)/g, '$1' + $('base').attr('href') + '$2$3');
|
|
|
|
|
|
|
|
// Show validation errors if necessary
|
|
|
|
if(this.hasClass('validationerror')) {
|
2012-07-18 16:49:23 +02:00
|
|
|
// Ensure the first validation error is visible
|
2014-10-26 02:21:47 +01:00
|
|
|
var tabError = this.find('.message.validation, .message.required').first().closest('.tab');
|
2012-07-18 16:49:23 +02:00
|
|
|
$('.cms-container').clearCurrentTabState(); // clear state to avoid override later on
|
2014-10-26 02:21:47 +01:00
|
|
|
tabError.closest('.ss-tabset').tabs('option', 'active', tabError.index('.tab'));
|
2011-06-08 01:35:56 +02:00
|
|
|
}
|
2014-10-26 02:21:47 +01:00
|
|
|
|
2011-06-08 01:35:56 +02:00
|
|
|
this._super();
|
|
|
|
},
|
2012-06-12 12:48:08 +02:00
|
|
|
onremove: function() {
|
2012-06-13 23:15:47 +02:00
|
|
|
this.changetracker('destroy');
|
2012-05-11 07:36:18 +02:00
|
|
|
this._super();
|
|
|
|
},
|
2012-06-14 18:49:17 +02:00
|
|
|
onmatch: function() {
|
|
|
|
this._super();
|
|
|
|
},
|
|
|
|
onunmatch: function() {
|
|
|
|
this._super();
|
|
|
|
},
|
2011-07-21 18:39:26 +02:00
|
|
|
redraw: function() {
|
2012-06-06 21:02:13 +02:00
|
|
|
if(window.debug) console.log('redraw', this.attr('class'), this.get(0));
|
|
|
|
|
2011-12-15 20:37:20 +01:00
|
|
|
// Force initialization of tabsets to avoid layout glitches
|
2012-04-17 17:40:37 +02:00
|
|
|
this.add(this.find('.cms-tabset')).redrawTabs();
|
2012-06-06 14:35:07 +02:00
|
|
|
this.find('.cms-content-header').redraw();
|
2011-07-21 18:39:26 +02:00
|
|
|
},
|
2011-09-19 21:01:17 +02:00
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
/**
|
|
|
|
* Function: _setupChangeTracker
|
|
|
|
*/
|
|
|
|
_setupChangeTracker: function() {
|
|
|
|
// Don't bind any events here, as we dont replace the
|
|
|
|
// full <form> tag by any ajax updates they won't automatically reapply
|
|
|
|
this.changetracker(this.getChangeTrackerOptions());
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2011-12-15 12:23:32 +01:00
|
|
|
* Function: confirmUnsavedChanges
|
2011-03-23 10:51:00 +01:00
|
|
|
*
|
2011-12-15 12:23:32 +01:00
|
|
|
* Checks the jquery.changetracker plugin status for this form,
|
|
|
|
* and asks the user for confirmation via a browser dialog if changes are detected.
|
|
|
|
* Doesn't cancel any unload or form removal events, you'll need to implement this based on the return
|
|
|
|
* value of this message.
|
2011-03-23 10:51:00 +01:00
|
|
|
*
|
2015-07-31 00:16:18 +02:00
|
|
|
* If changes are confirmed for discard, the 'changed' flag is reset.
|
|
|
|
*
|
2011-03-23 10:51:00 +01:00
|
|
|
* Returns:
|
2011-12-15 12:23:32 +01:00
|
|
|
* (Boolean) FALSE if the user wants to abort with changes present, TRUE if no changes are detected
|
|
|
|
* or the user wants to discard them.
|
2011-03-23 10:51:00 +01:00
|
|
|
*/
|
2011-12-15 12:23:32 +01:00
|
|
|
confirmUnsavedChanges: function() {
|
2012-06-12 17:13:36 +02:00
|
|
|
this.trigger('beforesubmitform');
|
2015-07-31 00:16:18 +02:00
|
|
|
if(!this.is('.changed')) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
var confirmed = confirm(ss.i18n._t('LeftAndMain.CONFIRMUNSAVED'));
|
|
|
|
if(confirmed) {
|
|
|
|
// confirm discard changes
|
|
|
|
this.removeClass('changed');
|
|
|
|
}
|
|
|
|
return confirmed;
|
2011-03-23 10:51:00 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function: onsubmit
|
|
|
|
*
|
|
|
|
* Suppress submission unless it is handled through ajaxSubmit().
|
|
|
|
*/
|
2012-04-05 22:12:00 +02:00
|
|
|
onsubmit: function(e, button) {
|
2012-05-01 23:40:21 +02:00
|
|
|
// Only submit if a button is present.
|
|
|
|
// This supressed submits from ENTER keys in input fields,
|
|
|
|
// which means the browser auto-selects the first available form button.
|
|
|
|
// This might be an unrelated button of the form field,
|
|
|
|
// or a destructive action (if "save" is not available, or not on first position).
|
2012-07-17 08:34:01 +02:00
|
|
|
if(this.prop("target") != "_blank") {
|
|
|
|
if(button) this.closest('.cms-container').submitForm(this, button);
|
|
|
|
return false;
|
|
|
|
}
|
2011-03-23 10:51:00 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function: validate
|
|
|
|
*
|
|
|
|
* Hook in (optional) validation routines.
|
|
|
|
* Currently clientside validation is not supported out of the box in the CMS.
|
|
|
|
*
|
|
|
|
* Todo:
|
|
|
|
* Placeholder implementation
|
|
|
|
*
|
|
|
|
* Returns:
|
|
|
|
* {boolean}
|
|
|
|
*/
|
|
|
|
validate: function() {
|
|
|
|
var isValid = true;
|
|
|
|
this.trigger('validate', {isValid: isValid});
|
|
|
|
|
|
|
|
return isValid;
|
2014-10-26 02:21:47 +01:00
|
|
|
},
|
|
|
|
/*
|
|
|
|
* Track focus on htmleditor fields
|
|
|
|
*/
|
|
|
|
'from .htmleditor': {
|
|
|
|
oneditorinit: function(e){
|
|
|
|
var self = this,
|
|
|
|
field = $(e.target).closest('.field.htmleditor'),
|
|
|
|
editor = field.find('textarea.htmleditor').getEditor().getInstance();
|
|
|
|
|
|
|
|
// TinyMCE 4 will add a focus event, but for now, use click
|
|
|
|
editor.onClick.add(function(e){
|
|
|
|
self.saveFieldFocus(field.attr('id'));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
/*
|
|
|
|
* Track focus on inputs
|
|
|
|
*/
|
|
|
|
'from .cms-edit-form :input:not(:submit)': {
|
|
|
|
onclick: function(e){
|
|
|
|
this.saveFieldFocus($(e.target).attr('id'));
|
|
|
|
},
|
|
|
|
onfocus: function(e){
|
|
|
|
this.saveFieldFocus($(e.target).attr('id'));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
/*
|
|
|
|
* Track focus on treedropdownfields.
|
|
|
|
*/
|
|
|
|
'from .cms-edit-form .treedropdown *': {
|
|
|
|
onfocusin: function(e){
|
|
|
|
var field = $(e.target).closest('.field.treedropdown');
|
|
|
|
this.saveFieldFocus(field.attr('id'));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
/*
|
|
|
|
* Track focus on chosen selects
|
|
|
|
*/
|
|
|
|
'from .cms-edit-form .dropdown .chzn-container a': {
|
|
|
|
onfocusin: function(e){
|
|
|
|
var field = $(e.target).closest('.field.dropdown');
|
|
|
|
this.saveFieldFocus(field.attr('id'));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
/*
|
|
|
|
* Restore fields after tabs are restored
|
|
|
|
*/
|
|
|
|
'from .cms-container': {
|
|
|
|
ontabstaterestored: function(e){
|
|
|
|
this.restoreFieldFocus();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
/*
|
|
|
|
* Saves focus in Window session storage so it that can be restored on page load
|
|
|
|
*/
|
|
|
|
saveFieldFocus: function(selected){
|
|
|
|
if(typeof(window.sessionStorage)=="undefined" || window.sessionStorage === null) return;
|
|
|
|
|
|
|
|
var id = $(this).attr('id'),
|
|
|
|
focusElements = [];
|
|
|
|
|
|
|
|
focusElements.push({
|
|
|
|
id:id,
|
|
|
|
selected:selected
|
|
|
|
});
|
|
|
|
|
|
|
|
if(focusElements) {
|
|
|
|
try {
|
|
|
|
window.sessionStorage.setItem(id, JSON.stringify(focusElements));
|
|
|
|
} catch(err) {
|
|
|
|
if (err.code === DOMException.QUOTA_EXCEEDED_ERR && window.sessionStorage.length === 0) {
|
|
|
|
// If this fails we ignore the error as the only issue is that it
|
|
|
|
// does not remember the focus state.
|
|
|
|
// This is a Safari bug which happens when private browsing is enabled.
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Set focus or window to previously saved fields.
|
|
|
|
* Requires HTML5 sessionStorage support.
|
|
|
|
*
|
|
|
|
* Must follow tab restoration, as reliant on active tab
|
|
|
|
*/
|
|
|
|
restoreFieldFocus: function(){
|
|
|
|
if(typeof(window.sessionStorage)=="undefined" || window.sessionStorage === null) return;
|
|
|
|
|
|
|
|
var self = this,
|
|
|
|
hasSessionStorage = (typeof(window.sessionStorage)!=="undefined" && window.sessionStorage),
|
|
|
|
sessionData = hasSessionStorage ? window.sessionStorage.getItem(this.attr('id')) : null,
|
|
|
|
sessionStates = sessionData ? JSON.parse(sessionData) : false,
|
|
|
|
elementID,
|
2015-09-23 16:19:24 +02:00
|
|
|
tabbed = (this.find('.ss-tabset').length !== 0),
|
2014-10-26 02:21:47 +01:00
|
|
|
activeTab,
|
|
|
|
elementTab,
|
|
|
|
toggleComposite,
|
|
|
|
scrollY;
|
|
|
|
|
|
|
|
if(hasSessionStorage && sessionStates.length > 0){
|
|
|
|
$.each(sessionStates, function(i, sessionState) {
|
|
|
|
if(self.is('#' + sessionState.id)){
|
|
|
|
elementID = $('#' + sessionState.selected);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-09-23 16:19:24 +02:00
|
|
|
// If the element IDs saved in session states don't match up to anything in this particular form
|
|
|
|
// that probably means we haven't encountered this form yet, so focus on the first input
|
2014-10-26 02:21:47 +01:00
|
|
|
if($(elementID).length < 1){
|
2015-09-23 16:19:24 +02:00
|
|
|
this.focusFirstInput();
|
2014-10-26 02:21:47 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
activeTab = $(elementID).closest('.ss-tabset').find('.ui-tabs-nav .ui-tabs-active .ui-tabs-anchor').attr('id');
|
|
|
|
elementTab = 'tab-' + $(elementID).closest('.ss-tabset .ui-tabs-panel').attr('id');
|
|
|
|
|
|
|
|
// Last focussed element differs to last selected tab, do nothing
|
|
|
|
if(tabbed && elementTab !== activeTab){
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
toggleComposite = $(elementID).closest('.togglecomposite');
|
|
|
|
|
|
|
|
//Reopen toggle fields
|
|
|
|
if(toggleComposite.length > 0){
|
|
|
|
toggleComposite.accordion('activate', toggleComposite.find('.ui-accordion-header'));
|
|
|
|
}
|
|
|
|
|
|
|
|
//Calculate position for scroll
|
|
|
|
scrollY = $(elementID).position().top;
|
|
|
|
|
|
|
|
//Fall back to nearest visible element if hidden (for select type fields)
|
|
|
|
if(!$(elementID).is(':visible')){
|
2016-05-31 16:09:40 +02:00
|
|
|
elementID = '#' + $(elementID).closest('.field:visible').attr('id');
|
2014-10-26 02:21:47 +01:00
|
|
|
scrollY = $(elementID).position().top;
|
|
|
|
}
|
|
|
|
|
|
|
|
//set focus to focus variable if element focusable
|
|
|
|
$(elementID).focus();
|
|
|
|
|
|
|
|
// Scroll fallback when element is not focusable
|
|
|
|
// Only scroll if element at least half way down window
|
|
|
|
if(scrollY > $(window).height() / 2){
|
|
|
|
self.find('.cms-content-fields').scrollTop(scrollY);
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
2015-09-23 16:19:24 +02:00
|
|
|
// If session storage is not supported or there is nothing stored yet, focus on the first input
|
|
|
|
this.focusFirstInput();
|
2014-10-26 02:21:47 +01:00
|
|
|
}
|
2015-09-23 16:19:24 +02:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Skip if an element in the form is already focused. Exclude elements which specifically
|
|
|
|
* opt-out of this behaviour via "data-skip-autofocus". This opt-out is useful if the
|
|
|
|
* first visible field is shown far down a scrollable area, for example for the pagination
|
|
|
|
* input field after a long GridField listing.
|
|
|
|
*/
|
|
|
|
focusFirstInput: function() {
|
|
|
|
this.find(':input:not(:submit)[data-skip-autofocus!="true"]').filter(':visible:first').focus();
|
2011-03-23 10:51:00 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
2011-03-31 10:52:29 +02:00
|
|
|
* Class: .cms-edit-form .Actions :submit
|
2011-03-23 10:51:00 +01:00
|
|
|
*
|
|
|
|
* All buttons in the right CMS form go through here by default.
|
|
|
|
* We need this onclick overloading because we can't get to the
|
|
|
|
* clicked button from a form.onsubmit event.
|
|
|
|
*/
|
2012-03-09 03:40:24 +01:00
|
|
|
$('.cms-edit-form .Actions input.action[type=submit], .cms-edit-form .Actions button.action').entwine({
|
2011-03-23 10:51:00 +01:00
|
|
|
/**
|
|
|
|
* Function: onclick
|
|
|
|
*/
|
|
|
|
onclick: function(e) {
|
2013-08-16 15:24:50 +02:00
|
|
|
// Confirmation on delete.
|
|
|
|
if(
|
|
|
|
this.hasClass('gridfield-button-delete')
|
|
|
|
&& !confirm(ss.i18n._t('TABLEFIELD.DELETECONFIRMMESSAGE'))
|
|
|
|
) {
|
|
|
|
e.preventDefault();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-10-08 09:27:38 +02:00
|
|
|
if(!this.is(':disabled')) {
|
|
|
|
this.parents('form').trigger('submit', [this]);
|
|
|
|
}
|
2012-03-08 18:21:13 +01:00
|
|
|
e.preventDefault();
|
2011-03-23 10:51:00 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
2012-01-09 17:17:31 +01:00
|
|
|
|
2013-11-22 12:45:39 +01:00
|
|
|
/**
|
|
|
|
* If we've a history state to go back to, go back, otherwise fall back to
|
|
|
|
* submitting the form with the 'doCancel' action.
|
|
|
|
*/
|
|
|
|
$('.cms-edit-form .Actions input.action[type=submit].ss-ui-action-cancel, .cms-edit-form .Actions button.action.ss-ui-action-cancel').entwine({
|
|
|
|
onclick: function(e) {
|
|
|
|
if (History.getStateByIndex(1)) {
|
|
|
|
History.back();
|
|
|
|
} else {
|
|
|
|
this.parents('form').trigger('submit', [this]);
|
|
|
|
}
|
|
|
|
e.preventDefault();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2012-05-09 17:19:20 +02:00
|
|
|
/**
|
2012-11-21 22:01:02 +01:00
|
|
|
* Hide tabs when only one is available.
|
|
|
|
* Special case is actiontabs - tabs between buttons, where we want to have
|
|
|
|
* extra options hidden within a tab (even if only one) by default.
|
2012-05-09 17:19:20 +02:00
|
|
|
*/
|
|
|
|
$('.cms-edit-form .ss-tabset').entwine({
|
|
|
|
onmatch: function() {
|
2012-11-21 22:01:02 +01:00
|
|
|
if (!this.hasClass('ss-ui-action-tabset')) {
|
|
|
|
var tabs = this.find("> ul:first");
|
2012-06-13 05:46:00 +02:00
|
|
|
|
2012-11-21 22:01:02 +01:00
|
|
|
if(tabs.children("li").length == 1) {
|
|
|
|
tabs.hide().parent().addClass("ss-tabset-tabshidden");
|
|
|
|
}
|
2012-06-13 05:46:00 +02:00
|
|
|
}
|
|
|
|
|
2012-05-09 17:19:20 +02:00
|
|
|
this._super();
|
2012-05-14 01:43:36 +02:00
|
|
|
},
|
|
|
|
onunmatch: function() {
|
|
|
|
this._super();
|
2012-05-09 17:19:20 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
});
|
2011-12-13 14:56:35 +01:00
|
|
|
|
2012-05-11 07:36:18 +02:00
|
|
|
}(jQuery));
|