2008-08-09 05:54:55 +02:00
|
|
|
/**
|
|
|
|
* Javascript handlers for generic model admin.
|
|
|
|
*
|
|
|
|
* Most of the work being done here is intercepting clicks on form submits,
|
|
|
|
* and managing the loading and sequencing of data between the different panels of
|
|
|
|
* the CMS interface.
|
|
|
|
*
|
|
|
|
* @todo add live query to manage application of events to DOM refreshes
|
|
|
|
* @todo alias the $ function instead of literal jQuery
|
|
|
|
*/
|
2008-09-13 06:45:30 +02:00
|
|
|
(function($) {
|
|
|
|
$(document).ready(function() {
|
2010-01-14 22:45:32 +01:00
|
|
|
|
2008-11-06 00:14:00 +01:00
|
|
|
/**
|
|
|
|
* Add class ajaxActions class to the parent of Add button of AddForm
|
|
|
|
* so it float to the right
|
|
|
|
*/
|
|
|
|
$('#Form_AddForm_action_doCreate').livequery(function(){
|
|
|
|
$(this).parent().addClass('ajaxActions');
|
|
|
|
});
|
|
|
|
|
2008-09-13 06:45:30 +02:00
|
|
|
/*
|
|
|
|
* Highlight buttons on click
|
|
|
|
*/
|
2008-09-13 08:19:17 +02:00
|
|
|
$('input[type=submit]').livequery('click', function() {
|
2008-09-13 06:45:30 +02:00
|
|
|
$(this).addClass('loading');
|
|
|
|
});
|
2008-10-02 02:31:33 +02:00
|
|
|
|
|
|
|
$("#right").scroll( function () {
|
|
|
|
positionActionArea();
|
|
|
|
});
|
|
|
|
|
|
|
|
$(window).resize( function() {
|
|
|
|
positionActionArea();
|
|
|
|
});
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Make the status message and ajax action button fixed
|
|
|
|
*/
|
|
|
|
function positionActionArea() {
|
|
|
|
if ( $.browser.msie && $.browser.version.indexOf("6.", 0)==0 ) {
|
|
|
|
newTopValue = $("#right").scrollTop()+$(window).height()-139;
|
|
|
|
$('.ajaxActions').css('top', newTopValue);
|
|
|
|
$('#statusMessage').css('top', newTopValue);
|
|
|
|
}
|
|
|
|
}
|
2008-09-12 01:18:21 +02:00
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
// Search form
|
|
|
|
//////////////////////////////////////////////////////////////////
|
2008-09-13 06:45:30 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* If a dropdown is used to choose between the classes, it is handled by this code
|
|
|
|
*/
|
|
|
|
$('#ModelClassSelector select')
|
|
|
|
// Set up an onchange function to show the applicable form and hide all others
|
|
|
|
.change(function() {
|
|
|
|
var $selector = $(this);
|
|
|
|
$('option', this).each(function() {
|
|
|
|
var $form = $('#'+$(this).val());
|
|
|
|
if($selector.val() == $(this).val()) $form.show();
|
|
|
|
else $form.hide();
|
|
|
|
});
|
|
|
|
})
|
|
|
|
// Initialise the form by calling this onchange event straight away
|
|
|
|
.change();
|
2008-09-12 01:18:21 +02:00
|
|
|
|
2008-08-09 07:57:44 +02:00
|
|
|
/**
|
|
|
|
* Stores a jQuery reference to the last submitted search form.
|
|
|
|
*/
|
|
|
|
__lastSearch = null;
|
2008-08-09 04:16:46 +02:00
|
|
|
|
2008-08-09 05:54:55 +02:00
|
|
|
/**
|
2008-09-12 01:18:21 +02:00
|
|
|
* Submits a search filter query and attaches event handlers
|
2008-11-05 09:55:07 +01:00
|
|
|
* to the response table, excluding the import form because
|
|
|
|
* file ($_FILES) submission doesn't work using AJAX
|
2008-09-12 01:18:21 +02:00
|
|
|
*
|
2009-05-20 06:27:26 +02:00
|
|
|
* Note: This is used for Form_CreateForm too
|
|
|
|
*
|
2008-09-12 01:18:21 +02:00
|
|
|
* @todo use livequery to manage ResultTable click handlers
|
2008-08-09 05:54:55 +02:00
|
|
|
*/
|
2008-11-05 09:55:07 +01:00
|
|
|
$('#SearchForm_holder .tab form:not(#Form_ImportForm)').submit(function () {
|
2008-09-13 06:45:30 +02:00
|
|
|
var $form = $(this);
|
2008-11-18 00:07:46 +01:00
|
|
|
|
2010-04-22 23:00:19 +02:00
|
|
|
$('#right form textarea.htmleditor').each(function(){
|
2010-01-14 22:45:32 +01:00
|
|
|
tinyMCE.execCommand('mceRemoveControl', false, $(this).attr('id'));
|
|
|
|
});
|
|
|
|
|
2008-11-18 00:07:46 +01:00
|
|
|
$('#ModelAdminPanel').fn('startHistory', $(this).attr('action'), $(this).formToArray());
|
2008-09-13 06:45:30 +02:00
|
|
|
$('#ModelAdminPanel').load($(this).attr('action'), $(this).formToArray(), standardStatusHandler(function(result) {
|
2008-11-18 00:07:46 +01:00
|
|
|
if(!this.future || !this.future.length) {
|
|
|
|
$('#Form_EditForm_action_goForward, #Form_ResultsForm_action_goForward').hide();
|
|
|
|
}
|
|
|
|
if(!this.history || this.history.length <= 1) {
|
|
|
|
$('#Form_EditForm_action_goBack, #Form_ResultsForm_action_goBack').hide();
|
|
|
|
}
|
|
|
|
|
2008-09-13 06:45:30 +02:00
|
|
|
$('#form_actions_right').remove();
|
|
|
|
Behaviour.apply();
|
2010-01-14 22:45:32 +01:00
|
|
|
|
2009-05-20 06:27:26 +02:00
|
|
|
if(window.onresize) window.onresize();
|
2008-09-13 06:45:30 +02:00
|
|
|
// Remove the loading indicators from the buttons
|
|
|
|
$('input[type=submit]', $form).removeClass('loading');
|
|
|
|
},
|
|
|
|
// Failure handler - we should still remove loading indicator
|
|
|
|
function () {
|
|
|
|
$('input[type=submit]', $form).removeClass('loading');
|
|
|
|
}));
|
|
|
|
return false;
|
2008-09-12 01:18:21 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clear search button
|
|
|
|
*/
|
2008-09-13 06:45:30 +02:00
|
|
|
$('#SearchForm_holder button[name=action_clearsearch]').click(function(e) {
|
2008-11-25 06:13:10 +01:00
|
|
|
$(this.form).resetForm();
|
2008-09-12 01:18:21 +02:00
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Column selection in search form
|
|
|
|
*/
|
2008-09-13 06:45:30 +02:00
|
|
|
$('a.form_frontend_function.toggle_result_assembly').click(function(){
|
|
|
|
var toggleElement = $(this).next();
|
2008-09-12 01:18:21 +02:00
|
|
|
toggleElement.toggle();
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
2008-09-13 06:45:30 +02:00
|
|
|
$('a.form_frontend_function.tick_all_result_assembly').click(function(){
|
2009-02-10 01:53:54 +01:00
|
|
|
var resultAssembly = $(this).prevAll('div#ResultAssembly').find('ul li input');
|
2008-09-12 01:18:21 +02:00
|
|
|
resultAssembly.attr('checked', 'checked');
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
2008-09-13 06:45:30 +02:00
|
|
|
$('a.form_frontend_function.untick_all_result_assembly').click(function(){
|
2009-02-10 01:53:54 +01:00
|
|
|
var resultAssembly = $(this).prevAll('div#ResultAssembly').find('ul li input');
|
2008-09-12 01:18:21 +02:00
|
|
|
resultAssembly.removeAttr('checked');
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
// Results list
|
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Table record handler for search result record
|
2008-09-13 08:19:17 +02:00
|
|
|
* @todo: Shouldn't this be part of TableListField?
|
2008-09-12 01:18:21 +02:00
|
|
|
*/
|
2009-01-14 21:44:55 +01:00
|
|
|
$('#right #Form_ResultsForm tbody td a:not(.deletelink,.downloadlink)')
|
2008-09-12 01:18:21 +02:00
|
|
|
.livequery('click', function(){
|
2008-09-13 06:45:30 +02:00
|
|
|
$(this).parent().parent().addClass('loading');
|
|
|
|
var el = $(this);
|
2008-11-18 00:07:46 +01:00
|
|
|
$('#ModelAdminPanel').fn('addHistory', el.attr('href'));
|
2008-09-13 08:19:17 +02:00
|
|
|
$('#ModelAdminPanel').fn('loadForm', el.attr('href'));
|
2008-09-12 01:18:21 +02:00
|
|
|
return false;
|
2008-09-13 08:19:17 +02:00
|
|
|
});
|
2008-08-09 08:18:32 +02:00
|
|
|
|
2008-09-12 01:18:21 +02:00
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
// RHS detail form
|
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
/**
|
|
|
|
* RHS panel Back button
|
|
|
|
*/
|
2008-11-18 00:07:46 +01:00
|
|
|
$('#Form_EditForm_action_goBack, #Form_ResultsForm_action_goBack').livequery('click', function() {
|
|
|
|
$('#ModelAdminPanel').fn('goBack');
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* RHS panel Back button
|
|
|
|
*/
|
|
|
|
$('#Form_ResultsForm_action_goForward').livequery('click', function() {
|
|
|
|
$('#ModelAdminPanel').fn('goForward');
|
2008-08-09 08:18:32 +02:00
|
|
|
return false;
|
|
|
|
});
|
2008-08-09 04:16:46 +02:00
|
|
|
|
2008-08-09 05:54:55 +02:00
|
|
|
/**
|
2008-09-12 01:18:21 +02:00
|
|
|
* RHS panel Save button
|
2008-08-09 05:54:55 +02:00
|
|
|
*/
|
2010-01-14 22:45:32 +01:00
|
|
|
|
|
|
|
$('#right #form_actions_right input[name=action_doSave], #right #form_actions_right input[name=action_doCreate]').livequery('click', function(){
|
2008-09-13 06:45:30 +02:00
|
|
|
var form = $('#right form');
|
|
|
|
var formAction = form.attr('action') + '?' + $(this).fieldSerialize();
|
2010-01-14 22:45:32 +01:00
|
|
|
|
2010-04-25 02:50:52 +02:00
|
|
|
$('#right form textarea.htmleditor').each(function(){
|
2010-01-14 22:45:32 +01:00
|
|
|
tinyMCE.execCommand('mceRemoveControl', false, $(this).attr('id'));
|
|
|
|
});
|
|
|
|
|
2008-09-12 01:18:21 +02:00
|
|
|
// Post the data to save
|
2008-09-13 08:19:17 +02:00
|
|
|
$.post(formAction, form.formToArray(), function(result){
|
2008-09-13 06:45:30 +02:00
|
|
|
$('#right #ModelAdminPanel').html(result);
|
2008-08-11 02:14:48 +02:00
|
|
|
|
2008-10-10 14:15:31 +02:00
|
|
|
statusMessage(ss.i18n._t('ModelAdmin.SAVED'));
|
2008-08-09 06:38:44 +02:00
|
|
|
|
2010-01-14 22:45:32 +01:00
|
|
|
// refresh and reapply the JS to trigger textmate
|
|
|
|
Behaviour.apply();
|
|
|
|
|
2009-05-20 06:27:26 +02:00
|
|
|
if(window.onresize) window.onresize();
|
|
|
|
}, 'html');
|
2010-01-14 22:45:32 +01:00
|
|
|
|
2008-09-12 01:18:21 +02:00
|
|
|
|
|
|
|
return false;
|
|
|
|
});
|
2008-08-09 05:54:55 +02:00
|
|
|
|
2008-08-11 02:21:44 +02:00
|
|
|
/**
|
2008-09-12 01:18:21 +02:00
|
|
|
* RHS panel Delete button
|
2008-08-11 02:21:44 +02:00
|
|
|
*/
|
2008-09-13 06:45:30 +02:00
|
|
|
$('#right #form_actions_right input[name=action_doDelete]').livequery('click', function(){
|
2008-10-10 14:15:31 +02:00
|
|
|
var confirmed = confirm(ss.i18n._t('ModelAdmin.REALLYDELETE'));
|
2009-02-10 04:41:37 +01:00
|
|
|
if(!confirmed) {
|
|
|
|
$(this).removeClass('loading')
|
|
|
|
return false;
|
|
|
|
}
|
2010-01-14 22:45:32 +01:00
|
|
|
|
2010-04-25 02:50:52 +02:00
|
|
|
$('#right form textarea.htmleditor').each(function(){
|
2010-01-14 22:45:32 +01:00
|
|
|
tinyMCE.execCommand('mceRemoveControl', false, $(this).attr('id'));
|
|
|
|
});
|
2008-09-13 08:19:17 +02:00
|
|
|
|
2008-09-13 06:45:30 +02:00
|
|
|
var form = $('#right form');
|
|
|
|
var formAction = form.attr('action') + '?' + $(this).fieldSerialize();
|
2008-09-12 01:18:21 +02:00
|
|
|
|
|
|
|
// The POST actually handles the delete
|
2008-09-13 08:19:17 +02:00
|
|
|
$.post(formAction, form.formToArray(), function(result){
|
2008-09-12 01:18:21 +02:00
|
|
|
// On success, the panel is refreshed and a status message shown.
|
2008-09-13 06:45:30 +02:00
|
|
|
$('#right #ModelAdminPanel').html(result);
|
2008-08-11 02:21:44 +02:00
|
|
|
|
2008-10-10 14:15:31 +02:00
|
|
|
statusMessage(ss.i18n._t('ModelAdmin.DELETED'));
|
2008-09-13 06:45:30 +02:00
|
|
|
$('#form_actions_right').remove();
|
2008-09-13 08:19:17 +02:00
|
|
|
|
|
|
|
// To do - convert everything to jQuery so that this isn't needed
|
2008-08-11 02:21:44 +02:00
|
|
|
Behaviour.apply(); // refreshes ComplexTableField
|
|
|
|
});
|
2008-09-12 01:18:21 +02:00
|
|
|
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
// Import/Add form
|
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
|
2008-08-09 05:54:55 +02:00
|
|
|
/**
|
2008-09-12 01:18:21 +02:00
|
|
|
* Add object button
|
2008-08-09 05:54:55 +02:00
|
|
|
*/
|
2008-09-13 06:45:30 +02:00
|
|
|
$('#Form_ManagedModelsSelect').submit(function(){
|
|
|
|
className = $('select option:selected', this).val();
|
|
|
|
requestPath = $(this).attr('action').replace('ManagedModelsSelect', className + '/add');
|
2008-09-29 21:02:44 +02:00
|
|
|
var $button = $(':submit', this);
|
|
|
|
$('#ModelAdminPanel').fn(
|
|
|
|
'loadForm',
|
|
|
|
requestPath,
|
|
|
|
function() {
|
|
|
|
$button.removeClass('loading');
|
|
|
|
$button = null;
|
|
|
|
}
|
|
|
|
);
|
2008-09-18 23:25:49 +02:00
|
|
|
$('#form_actions_right').remove();
|
2008-08-09 04:16:46 +02:00
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
2008-08-09 05:54:55 +02:00
|
|
|
/**
|
2008-09-12 01:18:21 +02:00
|
|
|
* Toggle import specifications
|
2008-08-11 02:21:44 +02:00
|
|
|
*/
|
2008-11-05 09:55:07 +01:00
|
|
|
$('.importSpec .details').hide();
|
|
|
|
$('.importSpec a.detailsLink').click(function() {
|
2008-09-13 06:45:30 +02:00
|
|
|
$('#' + $(this).attr('href').replace(/.*#/,'')).toggle();
|
2008-08-11 02:03:57 +02:00
|
|
|
return false;
|
|
|
|
});
|
2008-09-12 01:18:21 +02:00
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
// Helper functions
|
|
|
|
//////////////////////////////////////////////////////////////////
|
2008-08-09 04:16:46 +02:00
|
|
|
|
2008-09-13 08:19:17 +02:00
|
|
|
$('#ModelAdminPanel').fn({
|
|
|
|
/**
|
|
|
|
* Load a detail editing form into the main edit panel
|
|
|
|
* @todo Convert everything to jQuery so that the built-in load method can be used with this instead
|
|
|
|
*/
|
2008-09-29 21:02:44 +02:00
|
|
|
loadForm: function(url, successCallback) {
|
2008-09-13 08:19:17 +02:00
|
|
|
$('#right #ModelAdminPanel').load(url, standardStatusHandler(function(result) {
|
2008-09-29 21:02:44 +02:00
|
|
|
if(typeof(successCallback) == 'function') successCallback.apply();
|
2010-01-14 22:45:32 +01:00
|
|
|
|
|
|
|
var forward = $('#Form_EditForm_action_goForward, #Form_ResultsForm_action_goForward');
|
|
|
|
var back = $('#Form_EditForm_action_goBack, #Form_ResultsForm_action_goBack');
|
|
|
|
|
|
|
|
if(!this.future || !this.future.length) forward.hide();
|
|
|
|
else forward.show();
|
|
|
|
|
|
|
|
if(!this.history || this.history.length <= 1) back.hide();
|
|
|
|
else back.show();
|
|
|
|
|
2008-11-18 00:07:46 +01:00
|
|
|
|
2008-09-13 08:19:17 +02:00
|
|
|
Behaviour.apply(); // refreshes ComplexTableField
|
2009-04-27 09:20:52 +02:00
|
|
|
if(window.onresize) window.onresize();
|
2008-09-13 08:19:17 +02:00
|
|
|
}));
|
2008-11-18 00:07:46 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
startHistory: function(url, data) {
|
|
|
|
this.history = [];
|
|
|
|
$(this).fn('addHistory', url, data);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add an item to the history, to be accessed by goBack and goForward
|
|
|
|
*/
|
|
|
|
addHistory: function(url, data) {
|
|
|
|
// Combine data into URL
|
|
|
|
if(data) {
|
|
|
|
if(url.indexOf('?') == -1) url += '?' + $.param(data);
|
|
|
|
else url += '&' + $.param(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add to history
|
|
|
|
if(this.history == null) this.history = [];
|
|
|
|
this.history.push(url);
|
|
|
|
|
|
|
|
// Reset future
|
|
|
|
this.future = [];
|
|
|
|
},
|
|
|
|
|
|
|
|
goBack: function() {
|
2010-04-25 02:50:52 +02:00
|
|
|
$('#right form textarea.htmleditor').each(function(){
|
2010-01-14 22:45:32 +01:00
|
|
|
tinyMCE.execCommand('mceRemoveControl', false, $(this).attr('id'));
|
|
|
|
});
|
|
|
|
|
2008-11-18 00:07:46 +01:00
|
|
|
if(this.history && this.history.length) {
|
|
|
|
if(this.future == null) this.future = [];
|
|
|
|
|
|
|
|
var currentPage = this.history.pop();
|
|
|
|
var previousPage = this.history[this.history.length-1];
|
|
|
|
|
|
|
|
this.future.push(currentPage);
|
|
|
|
$(this).fn('loadForm', previousPage);
|
|
|
|
}
|
2010-01-14 22:45:32 +01:00
|
|
|
|
2008-11-18 00:07:46 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
goForward: function() {
|
2010-04-25 02:50:52 +02:00
|
|
|
$('#right form textarea.htmleditor').each(function(){
|
2010-01-14 22:45:32 +01:00
|
|
|
tinyMCE.execCommand('mceRemoveControl', false, $(this).attr('id'));
|
|
|
|
});
|
|
|
|
|
2008-11-18 00:07:46 +01:00
|
|
|
if(this.future && this.future.length) {
|
|
|
|
if(this.future == null) this.future = [];
|
|
|
|
|
|
|
|
var nextPage = this.future.pop();
|
|
|
|
|
|
|
|
this.history.push(nextPage);
|
|
|
|
$(this).fn('loadForm', nextPage);
|
|
|
|
}
|
2008-09-13 08:19:17 +02:00
|
|
|
}
|
2008-11-18 00:07:46 +01:00
|
|
|
|
2008-09-13 08:19:17 +02:00
|
|
|
});
|
2008-09-12 03:50:08 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Standard SilverStripe status handler for ajax responses
|
|
|
|
* It will generate a status message out of the response, and only call the callback for successful responses
|
|
|
|
*
|
|
|
|
* To use:
|
|
|
|
* Instead of passing your callback function as:
|
|
|
|
* function(response) { ... }
|
|
|
|
*
|
|
|
|
* Pass it as this:
|
|
|
|
* standardStatusHandler(function(response) { ... })
|
|
|
|
*/
|
2008-09-13 06:45:30 +02:00
|
|
|
function standardStatusHandler(callback, failureCallback) {
|
2008-09-12 03:50:08 +02:00
|
|
|
return function(response, status, xhr) {
|
2008-09-13 06:45:30 +02:00
|
|
|
// If the response is takne from $.ajax's complete handler, then swap the variables around
|
|
|
|
if(response.status) {
|
|
|
|
xhr = response;
|
|
|
|
response = xhr.responseText;
|
|
|
|
}
|
|
|
|
|
2008-09-12 03:50:08 +02:00
|
|
|
if(status == 'success') {
|
|
|
|
statusMessage(xhr.statusText, "good");
|
2008-09-13 06:45:30 +02:00
|
|
|
$(this).each(callback, [response, status, xhr]);
|
2008-09-12 03:50:08 +02:00
|
|
|
} else {
|
2008-10-07 05:00:25 +02:00
|
|
|
errorMessage(xhr.statusText);
|
2008-09-13 06:45:30 +02:00
|
|
|
if(failureCallback) $(this).each(failureCallback, [response, status, xhr]);
|
2008-09-12 03:50:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-13 06:45:30 +02:00
|
|
|
})
|
|
|
|
})(jQuery);
|
2008-08-09 07:00:42 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @todo Terrible HACK, but thats the cms UI...
|
|
|
|
*/
|
|
|
|
function fixHeight_left() {
|
2008-10-28 00:13:56 +01:00
|
|
|
//fitToParent('LeftPane');
|
2008-08-09 07:00:42 +02:00
|
|
|
fitToParent('Search_holder',12);
|
|
|
|
fitToParent('ResultTable_holder',12);
|
2008-08-09 08:29:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function prepareAjaxActions(actions, formName, tabName) {
|
|
|
|
// @todo HACK Overwrites LeftAndMain.js version of this method to avoid double form actions
|
|
|
|
// (by new jQuery and legacy prototype)
|
|
|
|
return false;
|
2009-05-20 06:27:26 +02:00
|
|
|
}
|