2012-02-16 12:27:47 +01:00
|
|
|
// Shortcut-function (until we update to Prototye v1.5)
|
|
|
|
if(typeof $$ != "Function") $$ = document.getElementsBySelector;
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
TableListField = Class.create();
|
|
|
|
TableListField.prototype = {
|
|
|
|
|
2007-10-17 04:29:42 +02:00
|
|
|
errorMessage: "Error talking to server",
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
initialize: function() {
|
|
|
|
var rules = {};
|
2010-10-15 04:27:59 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
rules['#'+this.id+' table.data a.deletelink'] = {
|
|
|
|
onclick: this.deleteRecord.bind(this)
|
|
|
|
};
|
|
|
|
|
2008-02-25 03:10:37 +01:00
|
|
|
rules['#'+this.id+' th a'] = {
|
2008-08-11 04:25:44 +02:00
|
|
|
onclick: this.refresh.bind(this)
|
2008-02-25 03:10:37 +01:00
|
|
|
};
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
rules['#'+this.id+' th'] = {
|
|
|
|
initialize: function() {
|
|
|
|
var sortLinks = $$('span.sortLinkHidden a', this);
|
2010-04-13 04:13:12 +02:00
|
|
|
if(sortLinks) sortLinks[0].style.visibility = 'hidden';
|
2007-07-19 12:40:28 +02:00
|
|
|
},
|
|
|
|
onmouseover: function(e) {
|
|
|
|
var sortLinks = $$('span.sortLinkHidden a', this);
|
2010-04-13 04:13:12 +02:00
|
|
|
if(sortLinks) sortLinks[0].style.visibility = 'visible';
|
2007-07-19 12:40:28 +02:00
|
|
|
},
|
|
|
|
onmouseout: function(e) {
|
|
|
|
var sortLinks = $$('span.sortLinkHidden a', this);
|
2010-04-13 04:13:12 +02:00
|
|
|
if(sortLinks) sortLinks[0].style.visibility = 'hidden';
|
2007-10-17 04:29:42 +02:00
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
};
|
|
|
|
|
2008-08-11 04:25:44 +02:00
|
|
|
rules['#'+this.id+' div.PageControls a'] = {onclick: this.refresh.bind(this)};
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2007-09-15 02:17:11 +02:00
|
|
|
rules['#'+this.id+' table.data tr td.markingcheckbox'] = {
|
|
|
|
onclick : function(e) {
|
|
|
|
// do nothing for clicks in marking box cells (e.g. if checkbox is missed)
|
|
|
|
}
|
|
|
|
};
|
2010-10-15 04:27:59 +02:00
|
|
|
|
|
|
|
// rules for selection options on click event
|
|
|
|
rules['#'+this.id+' .selectOptions a'] = {
|
|
|
|
onclick: this.markRecords.bind(this)
|
|
|
|
};
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
// initialize summary (if needed)
|
|
|
|
// TODO Breaks with nested divs
|
|
|
|
var summaryCols = $$('tfoot tr.summary td', this);
|
2007-10-17 04:29:42 +02:00
|
|
|
this._summaryDefs = [];
|
2008-10-10 05:21:30 +02:00
|
|
|
|
2008-10-10 18:07:01 +02:00
|
|
|
//should check summaryCols.length, because summaryCols will always be an array, though its length could be 0.
|
|
|
|
if(summaryCols && summaryCols.length) {
|
2007-07-19 12:40:28 +02:00
|
|
|
rules['#'+this.id+' table.data tbody input'] = {
|
|
|
|
onchange: function(e) {
|
|
|
|
if (!e) e = window.event; // stupid IE
|
|
|
|
// workaround for wrong scope with bind(this) and applyTo()
|
|
|
|
var root = Event.findElement(e,'div');
|
|
|
|
// TODO Fix slow $$()-calls and re-enable clientside summaries
|
|
|
|
//root._summarise();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
rules['#'+this.id+' table.data tbody select'] = {
|
|
|
|
onchange: function(e) {
|
|
|
|
if (!e) e = window.event; // stupid IE
|
|
|
|
// workaround for wrong scope with bind(this) and applyTo()
|
|
|
|
var root = Event.findElement(e,'div');
|
|
|
|
// TODO Fix slow $$()-calls and re-enable clientside summaries
|
|
|
|
//root._summarise();
|
|
|
|
}.bind(this)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2008-08-12 06:50:00 +02:00
|
|
|
Behaviour.register('TableListField_'+this.id,rules);
|
2008-10-10 05:21:30 +02:00
|
|
|
|
2008-10-10 18:07:01 +02:00
|
|
|
/*
|
2008-10-10 05:21:30 +02:00
|
|
|
if(summaryCols.length) {
|
2008-10-10 18:07:01 +02:00
|
|
|
this._getSummaryDefs(summaryCols);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2008-10-10 18:07:01 +02:00
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2008-08-11 04:25:44 +02:00
|
|
|
* Deletes the given dataobject record via an ajax request
|
|
|
|
* to complextablefield->Delete()
|
|
|
|
* @param {Object} e
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
deleteRecord: function(e) {
|
|
|
|
var img = Event.element(e);
|
|
|
|
var link = Event.findElement(e,"a");
|
|
|
|
var row = Event.findElement(e,"tr");
|
2011-03-02 05:14:47 +01:00
|
|
|
var self = this;
|
2007-07-19 12:40:28 +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',
|
2011-04-19 11:18:35 +02:00
|
|
|
'data': {forceajax: 1, SecurityID: jQuery('input[name=SecurityID]').val()},
|
2011-03-02 05:14:47 +01:00
|
|
|
'success': function(){
|
2012-03-12 17:03:14 +01:00
|
|
|
jQuery(row).remove();
|
|
|
|
// 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': this.ajaxErrorHandler
|
|
|
|
});
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
Event.stop(e);
|
|
|
|
},
|
|
|
|
|
|
|
|
removeById: function(id) {
|
2012-02-16 12:27:47 +01:00
|
|
|
var el = jQuery('#record-' + this.id + '-' + id)[0];
|
2007-07-19 12:40:28 +02:00
|
|
|
if(el) el.parentNode.removeChild(el);
|
|
|
|
this._summarise();
|
|
|
|
},
|
|
|
|
|
2010-10-15 04:27:59 +02:00
|
|
|
/**
|
|
|
|
* according to the clicked element in "Select bar", mark records that have same class as the element.
|
|
|
|
*/
|
|
|
|
markRecords: function(e){
|
|
|
|
var el = Event.element(e);
|
|
|
|
if(el.nodeName != "a") el = Event.findElement(e,"a");
|
|
|
|
|
|
|
|
if(el.rel == "all"){
|
|
|
|
this.markAll();
|
|
|
|
}else if(el.rel == 'none') {
|
|
|
|
this.unmarkAll();
|
|
|
|
}else{
|
|
|
|
this.unmarkAll();
|
2012-02-16 12:27:47 +01:00
|
|
|
var records = jQuery('#' + this.id + ' td.' + el.rel + ' input.checkbox');
|
2010-10-15 04:27:59 +02:00
|
|
|
var i=0;
|
|
|
|
for(i; i<records.length; i++){
|
|
|
|
records[i].checked = 'checked';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* mark all record in current view of the table
|
|
|
|
*/
|
|
|
|
markAll: function(e){
|
|
|
|
var records = $$('#'+this.id+' td.markingcheckbox input.checkbox');
|
|
|
|
var i=0;
|
|
|
|
for(i; i<records.length; i++){
|
|
|
|
records[i].checked = 'checked';
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* unmark all records in current view of the table
|
|
|
|
*/
|
|
|
|
unmarkAll: function(e){
|
|
|
|
var records = $$('#'+this.id+' td.markingcheckbox input.checkbox');
|
|
|
|
var i=0;
|
|
|
|
for(i; i<records.length; i++){
|
|
|
|
records[i].checked = '';
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
refresh: function(e) {
|
2011-03-02 05:14:47 +01:00
|
|
|
var self = this;
|
|
|
|
|
2008-08-11 04:25:44 +02:00
|
|
|
if(e) {
|
|
|
|
var el = Event.element(e);
|
|
|
|
if(el.nodeName != "a") el = Event.findElement(e,"a");
|
|
|
|
} else {
|
2012-02-16 12:27:47 +01:00
|
|
|
var el = jQuery('#' + this.id)[0];
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2008-11-18 02:48:37 +01:00
|
|
|
|
|
|
|
if(el.getAttribute('href')) {
|
2011-03-02 05:14:47 +01:00
|
|
|
jQuery.ajax({
|
|
|
|
'url': el.getAttribute('href'),
|
2011-04-19 11:18:35 +02:00
|
|
|
'data': {'update': 1},
|
2011-03-02 05:14:47 +01:00
|
|
|
'success': function(response) {
|
2011-04-19 11:18:35 +02:00
|
|
|
jQuery('#' + self.id).replaceWith(response)
|
2011-03-02 05:14:47 +01:00
|
|
|
// reapply behaviour and reattach methods to TF container node
|
|
|
|
// e.g. <div class="TableListField">
|
2012-02-16 12:27:47 +01:00
|
|
|
Behaviour.apply(jQuery('#' + self.id)[0], true);
|
2011-03-02 05:14:47 +01:00
|
|
|
}
|
|
|
|
});
|
2008-11-18 02:48:37 +01:00
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2008-08-11 04:25:44 +02:00
|
|
|
if(e) Event.stop(e);
|
2007-07-19 12:40:28 +02:00
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
2007-10-17 04:29:42 +02:00
|
|
|
ajaxErrorHandler: function(response) {
|
|
|
|
if(typeof(window.ajaxErrorHandler) == 'function') {
|
|
|
|
window.ajaxErrorHandler();
|
|
|
|
} else {
|
|
|
|
alert(this.errorMessage);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
_getSummaryDefs: function(summaryCols) {
|
|
|
|
summaryCols.each(function(col, pos) {
|
|
|
|
if( col ) {
|
|
|
|
var func = this._getSummaryFunction(col.className);
|
|
|
|
this._summaryDefs[pos] = {col: col, pos: pos, func: func};
|
|
|
|
}
|
|
|
|
}.bind(this));
|
|
|
|
|
|
|
|
this._summarise();
|
|
|
|
},
|
|
|
|
|
|
|
|
_summarise: function() {
|
|
|
|
var rows = $$('tbody tr', this);
|
2007-10-17 04:29:42 +02:00
|
|
|
if(!rows) return false;
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
var columnData = [];
|
|
|
|
// prepare the array (gets js-errors otherwise)
|
|
|
|
var cols = $$('td', rows[0]);
|
|
|
|
for(colPos=0; colPos<cols.length; colPos++) {
|
|
|
|
columnData[colPos] = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
for(rowPos=0; rowPos<rows.length; rowPos++) {
|
|
|
|
// avoid wrong calculations for nested lists
|
|
|
|
if(Element.hasClassName(rows[rowPos], "subitem")) continue;
|
|
|
|
|
|
|
|
var cols = $$('td', rows[rowPos]);
|
|
|
|
var colPos = 0;
|
|
|
|
for(colPos; colPos<cols.length; colPos++) {
|
|
|
|
//if(!columnData[colPos]) columnData[colPos] = [];
|
|
|
|
if(this._summaryDefs[colPos] && this._summaryDefs[colPos].func) {
|
|
|
|
columnData[colPos][rowPos] = this._getValue(cols[colPos]);
|
|
|
|
} else {
|
|
|
|
columnData[colPos][rowPos] = "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for(colPos=0; colPos<columnData.length; colPos++) {
|
|
|
|
if(this._summaryDefs[colPos] && this._summaryDefs[colPos].func) {
|
|
|
|
var summaryVal = this._summaryDefs[colPos].func.apply(this,[columnData[colPos]]);
|
|
|
|
this._summaryDefs[colPos].col.innerHTML = summaryVal;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_getSummaryFunction: function(func) {
|
|
|
|
if(this[func] && typeof this[func] == "function") {
|
|
|
|
// local
|
|
|
|
return this[func];
|
|
|
|
} else if(window[func] && typeof window[func] == "function") {
|
|
|
|
// global
|
|
|
|
return window[func];
|
|
|
|
} else {
|
|
|
|
// not existing
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_getValue: function(col) {
|
|
|
|
var inputNode = $$('input', col);
|
|
|
|
if(inputNode[0]) {
|
|
|
|
return $F(inputNode[0]);
|
|
|
|
}
|
|
|
|
var selectNode = $$('select', col);
|
|
|
|
if(selectNode[0]) {
|
|
|
|
return $F(selectNode[0]);
|
|
|
|
}
|
|
|
|
return col.innerHTML.stripTags();
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ############# Summary Functions ##############
|
|
|
|
*/
|
|
|
|
|
|
|
|
sum: function(arr) {
|
|
|
|
var sum = 0;
|
|
|
|
arr.each(function(val) {
|
|
|
|
sum += val*1; // convert to float
|
|
|
|
});
|
|
|
|
return sum;
|
|
|
|
},
|
|
|
|
|
|
|
|
sumCurrency: function(arr) {
|
|
|
|
var sum = 0;
|
|
|
|
arr.each(function(val) {
|
|
|
|
if(!val) return;
|
|
|
|
val = val.replace(/\$/,'');
|
|
|
|
val = val.replace(/\,/,'');
|
|
|
|
sum+= val*1; // convert to float
|
|
|
|
});
|
|
|
|
return sum.toCurrency();
|
|
|
|
},
|
|
|
|
|
|
|
|
max: function(arr) {
|
|
|
|
return arr.max();
|
|
|
|
},
|
|
|
|
|
|
|
|
min: function(arr) {
|
|
|
|
return arr.min();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TableListRecord = Class.create();
|
|
|
|
TableListRecord.prototype = {
|
|
|
|
|
|
|
|
onmouseover : function() {
|
|
|
|
Element.addClassName(this,'over');
|
|
|
|
},
|
|
|
|
|
|
|
|
onmouseout : function() {
|
|
|
|
Element.removeClassName(this,'over');
|
|
|
|
},
|
|
|
|
|
|
|
|
ajaxRequest : function(url, subform) {
|
2011-03-02 05:14:47 +01:00
|
|
|
var self = this;
|
2007-07-19 12:40:28 +02:00
|
|
|
// Highlight the new row
|
|
|
|
if(this.parentNode.selectedRow) {
|
|
|
|
Element.removeClassName(this.parentNode.selectedRow,'current');
|
|
|
|
}
|
|
|
|
this.parentNode.selectedRow = this;
|
|
|
|
Element.addClassName(this,'current');
|
|
|
|
|
2012-02-16 12:27:47 +01:00
|
|
|
this.subform = document.getElementById(subform);
|
2007-07-19 12:40:28 +02:00
|
|
|
Element.addClassName(this, 'loading');
|
|
|
|
statusMessage('loading');
|
2011-03-02 05:14:47 +01:00
|
|
|
jQuery.ajax({
|
|
|
|
'url': url + this.id.replace('record-',''),
|
|
|
|
'method' : 'post',
|
|
|
|
'data' : {'ajax': 1},
|
|
|
|
success : function() {
|
|
|
|
self.select_success();
|
2011-03-15 22:15:01 +01:00
|
|
|
},
|
2011-03-02 05:14:47 +01:00
|
|
|
failure : ajaxErrorHandler
|
2007-07-19 12:40:28 +02:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2009-07-17 01:58:01 +02:00
|
|
|
getRecordId: function(){
|
|
|
|
parts = this.id.match( /.*[\-]{1}(\d+)$/ );
|
|
|
|
if(parts) return parts[1];
|
|
|
|
else return false;
|
|
|
|
},
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
select_success : function(response) {
|
|
|
|
Element.removeClassName(this, 'loading');
|
|
|
|
this.subform.loadNewPage(response.responseText);
|
|
|
|
|
|
|
|
statusMessage('loaded','good');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-11 02:03:57 +02:00
|
|
|
TableListRecord.applyTo('div.TableListField tr');
|
2008-02-25 03:10:37 +01:00
|
|
|
TableListField.applyTo('div.TableListField');
|
2012-02-16 12:27:47 +01:00
|
|
|
|
|
|
|
Number.prototype.CURRENCIES = {
|
|
|
|
en_GB: '$ ###,###.##'
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Caution: Not finished!
|
|
|
|
* @param iso string (Not used) Please use in combination with Number.CURRENCIES to achieve i18n
|
|
|
|
* @return string
|
|
|
|
*
|
|
|
|
* @see http://www.jibbering.com/faq/faq_notes/type_convert.html
|
|
|
|
* @see http://www.rgagnon.com/jsdetails/js-0063.html
|
|
|
|
* @see http://www.mredkj.com/javascript/nfdocs.html
|
|
|
|
*/
|
|
|
|
Number.prototype.toCurrency = function(iso) {
|
|
|
|
if(!iso) iso = SS_DEFAULT_ISO;
|
|
|
|
// TODO stub, please implement properly
|
|
|
|
return "$" + this.toFixed(2);
|
|
|
|
}
|