2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Javascript for TableField. allows the deletion of records via
|
|
|
|
* AJAX, and the addition of rows via javasript.
|
|
|
|
*
|
|
|
|
* TODO relies on include-order at the moment to override actions :/
|
|
|
|
*/
|
|
|
|
Effect.FadeOut = function(element,callback) {
|
|
|
|
element = $(element);
|
|
|
|
var oldOpacity = Element.getInlineOpacity(element);
|
|
|
|
var options = Object.extend({
|
|
|
|
from: Element.getOpacity(element) || 1.0,
|
|
|
|
to: 0.0,
|
|
|
|
afterFinishInternal: function(effect) {
|
|
|
|
effect.element.parentNode.removeChild(effect.element);
|
|
|
|
}
|
|
|
|
}, arguments[1] || {});
|
|
|
|
return new Effect.Opacity(element,options);
|
|
|
|
}
|
|
|
|
|
|
|
|
TableField = Class.create();
|
2008-10-16 02:57:06 +02:00
|
|
|
TableField.prototype = {
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
newRowID: 1,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Applies behaviour to the delete button for deleting objects via ajax.
|
|
|
|
*/
|
|
|
|
initialize: function() {
|
|
|
|
var rules = {};
|
|
|
|
|
|
|
|
rules['#'+this.id+' table.data a.deletelink'] = {
|
|
|
|
onclick: this.deleteRecord.bind(this)
|
|
|
|
};
|
|
|
|
|
|
|
|
rules['#'+this.id+' table.data a.addrow'] = {
|
|
|
|
onclick: this.addRow.bind(this)
|
|
|
|
};
|
|
|
|
|
2008-08-12 06:50:00 +02:00
|
|
|
Behaviour.register('TableField_'+this.id,rules);
|
2007-07-19 12:40:28 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deletes the given dataobject record via an ajax request. If the record doesn't have any
|
|
|
|
* information in it, it just removes it from the form.
|
|
|
|
* to tablefield->Delete()
|
|
|
|
* @param {Object} e
|
|
|
|
*/
|
|
|
|
deleteRecord: function(e) {
|
|
|
|
var img = Event.element(e);
|
|
|
|
var link = Event.findElement(e,"a");
|
|
|
|
var row = Event.findElement(e,"tr");
|
|
|
|
var params = link.getAttribute("href").toQueryParams();
|
|
|
|
var isEmpty = true;
|
2009-07-17 01:58:01 +02:00
|
|
|
var recordID = row.getRecordId();
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
// Check to see if there is a dataobject to delete first, otherwise remove the row.
|
|
|
|
// or: Check if a childID is set (not present on new items)
|
|
|
|
if(
|
|
|
|
(this.hasNoValues(row,"input") && this.hasNoValues(row,"select") && this.hasNoValues(row,"textarea"))
|
2009-07-17 01:58:01 +02:00
|
|
|
|| params["childID"] <= 0 || (recordID <= 0 || recordID == false)
|
2007-07-19 12:40:28 +02:00
|
|
|
){
|
|
|
|
if( row.parentNode.getElementsByTagName('tr').length > 1 ) {
|
2010-03-28 22:06:01 +02:00
|
|
|
Effect.FadeOut(row);
|
2007-07-19 12:40:28 +02:00
|
|
|
} else {
|
|
|
|
// clear all fields in the row
|
|
|
|
var fields = row.getElementsByTagName('input');
|
|
|
|
if( fields )
|
|
|
|
for( var i = 0; i < fields.length; i++ ) {
|
|
|
|
fields[i].value = '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Event.stop(e);
|
|
|
|
return false;
|
|
|
|
}
|
2009-10-16 00:11:13 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
// TODO ajaxErrorHandler and loading-image are dependent on cms, but formfield is in sapphire
|
2009-03-10 23:08:52 +01:00
|
|
|
var confirmed = confirm(ss.i18n._t('TABLEFIELD.DELETECONFIRMMESSAGE', 'Are you sure you want to delete this record?'));
|
2007-07-19 12:40:28 +02:00
|
|
|
if(confirmed){
|
|
|
|
img.setAttribute("src",'cms/images/network-save.gif'); // TODO doesn't work
|
|
|
|
new Ajax.Request(
|
|
|
|
link.getAttribute("href"),
|
|
|
|
{
|
|
|
|
method: 'post',
|
2007-11-08 03:23:26 +01:00
|
|
|
postBody: 'ajax=1' + ($('SecurityID') ? '&SecurityID=' + $('SecurityID').value : ''),
|
2007-07-19 12:40:28 +02:00
|
|
|
onComplete: function(response){
|
2008-11-03 06:00:43 +01:00
|
|
|
Effect.Fade(
|
|
|
|
row,
|
|
|
|
{
|
|
|
|
afterFinish: function(obj) {
|
|
|
|
// remove row from DOM
|
|
|
|
obj.element.parentNode.removeChild(obj.element);
|
|
|
|
// recalculate summary if needed (assumes that TableListField.js is present)
|
|
|
|
// TODO Proper inheritance
|
|
|
|
if(this._summarise) this._summarise();
|
|
|
|
// custom callback
|
|
|
|
if(this.callback_deleteRecord) this.callback_deleteRecord(e);
|
|
|
|
}.bind(this)
|
|
|
|
}
|
|
|
|
);
|
2007-07-19 12:40:28 +02:00
|
|
|
}.bind(this),
|
|
|
|
onFailure: ajaxErrorHandler
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
Event.stop(e);
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @param {Object} element
|
|
|
|
* @param {Object} tagName
|
|
|
|
*/
|
|
|
|
hasNoValues: function(element,tagName){
|
|
|
|
elements = element.getElementsByTagName(tagName);
|
|
|
|
|
|
|
|
if(elements.length >= 1){
|
|
|
|
var isEmpty = true;
|
|
|
|
for(var i = 0; i < elements.length;i++){
|
|
|
|
if(elements[i].type != "hidden"){
|
|
|
|
if(elements[i].value != null && elements[i].value != ""){
|
|
|
|
isEmpty = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return isEmpty;
|
|
|
|
}else{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Appends a new row to the DOM.
|
|
|
|
*
|
|
|
|
* @param {Object} tableid
|
|
|
|
*/
|
|
|
|
addRow: function (e){
|
|
|
|
|
|
|
|
var table = Event.findElement(e,"table");
|
|
|
|
if(table){
|
|
|
|
// Clone the last TR
|
|
|
|
var tbody = table.tBodies[0];
|
|
|
|
var numRows = tbody.rows.length;
|
|
|
|
var newRow = tbody.rows[0].cloneNode(true);
|
|
|
|
|
|
|
|
// Get the input elements in this new row
|
|
|
|
var inputs = newRow.getElementsByTagName('input');
|
|
|
|
// For every input, set it's value to blank if it is not hidden
|
|
|
|
for(var i = 0; i < inputs.length; i++) {
|
|
|
|
if(inputs[i].type != 'hidden') {
|
|
|
|
inputs[i].value = ""
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
this.newRowID++;
|
|
|
|
|
|
|
|
if(newRow.id != "new"){
|
|
|
|
this.resetNames(newRow);
|
|
|
|
}
|
2009-04-29 02:07:39 +02:00
|
|
|
|
|
|
|
// Make sure all inputs have unique IDs
|
|
|
|
for(var i = 0; i < inputs.length; i++) {
|
|
|
|
inputs[i].id += "-" + this.newRowID;
|
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
// Change the ID to a unique one
|
|
|
|
newRow.id = "New_" + this.newRowID;
|
|
|
|
|
|
|
|
// Append the new row to the DOM
|
|
|
|
table.tBodies[0].appendChild(newRow);
|
|
|
|
Behaviour.apply(table);
|
|
|
|
}
|
|
|
|
Event.stop(e);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* resets the names for all elements inside a row.
|
|
|
|
* @param {Object} row
|
|
|
|
*/
|
|
|
|
resetNames: function(row){
|
|
|
|
|
|
|
|
// Support for addressing the ID's appropriately.
|
|
|
|
for(i = 0; i < row.cells.length;i++){
|
|
|
|
for(b=0; b < row.cells[i].childNodes.length;b++){
|
|
|
|
inputElement = row.cells[i].childNodes[b];
|
|
|
|
if(inputElement.type != 'hidden') inputElement.value = "";
|
|
|
|
if(inputElement.name != null){
|
|
|
|
if(inputElement.name.substr(inputElement.name.length - 2,inputElement.name.length) != "[]"){
|
|
|
|
inputElement.name =
|
|
|
|
inputElement.name.substr(0,inputElement.name.indexOf('[')+1) + "new" +
|
|
|
|
inputElement.name.substr(inputElement.name.indexOf(']'),inputElement.name.length) + "[]";
|
|
|
|
}else{
|
|
|
|
inputElement.name =
|
|
|
|
inputElement.name.substr(0,inputElement.name.indexOf('[')+1) + "new" +
|
|
|
|
inputElement.name.substr(inputElement.name.indexOf(']'),inputElement.name.length);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-10-16 02:57:06 +02:00
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
TableField.applyTo('div.TableField');
|
|
|
|
if(typeof ajaxErrorHandler == 'undefined'){
|
|
|
|
ajaxErrorHandler = function(response) {
|
|
|
|
alert(response.responseText);
|
|
|
|
}
|
|
|
|
}
|