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 :/
|
|
|
|
*/
|
|
|
|
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();
|
2011-03-02 05:14:47 +01:00
|
|
|
var self = this;
|
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 ) {
|
2011-04-19 12:47:56 +02:00
|
|
|
jQuery(row).fadeOut();
|
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
|
|
|
|
2012-07-16 05:18:31 +02:00
|
|
|
// TODO ajaxErrorHandler and loading-image are dependent on cms, but formfield is in framework
|
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){
|
2012-07-16 05:18:31 +02:00
|
|
|
img.setAttribute("src",'framework/images/network-save.gif'); // TODO doesn't work
|
2011-03-02 05:14:47 +01:00
|
|
|
jQuery.ajax({
|
|
|
|
'url': link.getAttribute("href"),
|
|
|
|
'method': 'post',
|
2012-02-16 12:27:47 +01:00
|
|
|
'data': {ajax: 1, 'SecurityID': document.getElementById('SecurityID') ? document.getElementById('SecurityID').value : null},
|
2011-03-02 05:14:47 +01:00
|
|
|
'success': function(response){
|
2011-04-19 12:47:56 +02:00
|
|
|
jQuery(row).fadeOut('fast', function() {
|
|
|
|
// remove row from DOM
|
|
|
|
this.element.parentNode.removeChild(obj.element);
|
|
|
|
// recalculate summary if needed (assumes that TableListField.js is present)
|
|
|
|
// TODO Proper inheritance
|
|
|
|
if(self._summarise) self._summarise();
|
|
|
|
// custom callback
|
|
|
|
if(self.callback_deleteRecord) self.callback_deleteRecord(e);
|
|
|
|
});
|
2011-03-02 05:14:47 +01:00
|
|
|
},
|
|
|
|
'error': ajaxErrorHandler
|
|
|
|
});
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|