Commit 33b12588 authored by Xiaowu Zhang's avatar Xiaowu Zhang Committed by Klaus Wölfel

erp5_production_planning: display total line for production matrix

parent 6aeb55f8
...@@ -235,7 +235,7 @@ ...@@ -235,7 +235,7 @@
</item> </item>
<item> <item>
<key> <string>css_class</string> </key> <key> <string>css_class</string> </key>
<value> <string></string> </value> <value> <string>matrixbox_production_planning</string> </value>
</item> </item>
<item> <item>
<key> <string>default</string> </key> <key> <string>default</string> </key>
......
var matrixbox;
var sRegExp = new RegExp('(-?[0-9]+)([0-9]{3})');
function format(num) {
// format a number to string, only 1 234 format is supported.
num = num.toString();
while(sRegExp.test(num)) {
num = num.replace(sRegExp, '$1 $2');
}
return num;
}
function parse(str) {
// parse a string to a number
// \s is for opera, others are for chrome / firefox
return parseFloat(
str.replace(/\s/g, "")
.replace(/ /g, "")
.replace(/&nbsp;/g, "")
);
}
/* matrixbox sums */
// XXX must also add a class to the matrixbox, not to apply on all mb
function addTotalLineToMatrixBox() {
var nb_columns = matrixbox.find("tr:first td").length;
matrixbox.find("td.footer").attr('colspan', nb_columns);
// add tr for summary line (only if having more than one line)
if (matrixbox.find("tr.DataA, tr.DataB").length > 1) {
var last_line = matrixbox.find('tr:last');
var sumline = $('<tr class="MatrixBoxSumLine" style="border-top: 1px solid #3D6474"/>').insertBefore(last_line);
// add as many td in the summary line
$('<td class="Data footer"/>').appendTo(sumline); // ( + 1 for the title column )
// once again, if we only have one column, we do not add a summary
// column
for (i=1; i< (nb_columns == 2 ? 1 : nb_columns); i++) {
$('<td class="Data footer" style="text-align: right"><span class="input figure"></span></td>').appendTo(sumline);
}
}
}
function recalculateSumLine(idx, element) {
// recalculates the sum line. `element` must be the tr with class
// .MatrixBoxSumLine
jQuery(element).find('span.input').each(function(idx, element){
var sum=0,
inputs = matrixbox.find('tr').find('td:eq(' + (idx+1) + ')');
for (i=1; i<inputs.length-1; i++) {
var input = inputs[i];
var tmp = input.querySelector('input');
if (tmp) {
v = parse(tmp.value);
} else {
v = parse(input.innerText);
}
if (v) { sum += v; }
}
jQuery(element).text(format(sum));
});
}
function recalculateSumColumn(idx, element) {
// recalculates the value of a sum column. `element` must be the td with class
// .MatrixBoxSumColumn
var me = jQuery(element);
var sum = 0;
inputs = me.parent().siblings();
for (i=1; i<inputs.length; i++) {
var input = inputs[i];
var tmp = input.querySelector('input');
if (tmp) {
v = parse(tmp.value);
} else {
v = parse(input.innerText);
}
if (v) { sum += v; }
}
me.text(format(sum));
}
function addTotalColumnToMatrixBox() {
// initialise:
nb_columns = matrixbox.find("tr:first td").length;
// add td for summary column (only if having more than one column)
if (nb_columns > 2) {
$('<td style="text-align: right"><span class="MatrixBoxSumColumn input figure" disabled="disabled"/></td>').appendTo(
matrixbox.find('tr.DataA, tr.DataB')
);
$('<td/>').appendTo(matrixbox.find('tr.matrixbox_label_line'));
}
}
function addTotalLineColumnToMatrixBox(matrixbox_container) {
matrixbox = $(matrixbox_container.querySelector('.MatrixContent'));
addTotalLineToMatrixBox();
//addTotalColumnToMatrixBox();
//matrixbox.find('.MatrixBoxSumColumn').each(recalculateSumColumn);
matrixbox.find('.MatrixBoxSumLine').each(recalculateSumLine);
}
var config = {
childList: true,
subtree: true,
attributes: false,
characterData: false
};
function observe(mutationsList) {
mutationsList.forEach(function observerMutation(mutation) {
if (mutation.type === 'childList') {
len = mutation.addedNodes.length;
for (i = 0; i < len; i += 1) {
node = mutation.addedNodes[i];
if (node.nodeType === Node.ELEMENT_NODE) {
// Make sure matrixbox is fully displayed
if (node.getAttribute('class') == 'Data footer') {
var matrixbox = document.querySelector('.matrixbox_production_planning');
addTotalLineColumnToMatrixBox(matrixbox);
observer.disconnect();
}
}
}
}
});
}
// Create an observer instance linked to the callback function
var observer = new MutationObserver(observe);
// Start observing the target node for configured mutations
observer.observe(document, config);
<?xml version="1.0"?>
<ZopeData>
<record id="1" aka="AAAAAAAAAAE=">
<pickle>
<global name="File" module="OFS.Image"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>__name__</string> </key>
<value> <string>matrix_dynamic_total_line_column.js</string> </value>
</item>
<item>
<key> <string>content_type</string> </key>
<value> <string>application/javascript</string> </value>
</item>
<item>
<key> <string>precondition</string> </key>
<value> <string></string> </value>
</item>
<item>
<key> <string>title</string> </key>
<value> <string></string> </value>
</item>
</dictionary>
</pickle>
</record>
</ZopeData>
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment