Commit d199b90b authored by Jerome Mariette's avatar Jerome Mariette
Browse files

add a template for new lines in handsontable

parent e778455b
......@@ -748,7 +748,7 @@ Handsontable.cellTypes["bootdate"] = Handsontable.BootstrapDateCell;
}
$("#workflow_form").validate({ rules: rules });
// for multiple and append values, use the handsontable
$("[id^=handsontable_]").each(function(){
var id_parts = $(this).attr("id").split("handsontable_"),
......@@ -756,7 +756,7 @@ Handsontable.cellTypes["bootdate"] = Handsontable.BootstrapDateCell;
dataSchema = {},
colHeaders = new Array(),
columns = new Array(),
data = {},
rowTemplate = new Array(),
allTypes = {},
allActions = {},
allRequired = {},
......@@ -764,7 +764,7 @@ Handsontable.cellTypes["bootdate"] = Handsontable.BootstrapDateCell;
allHelps[param_name] = params_per_name[param_name].help;
for (var i in params_per_name[param_name].sub_parameters) {
dataSchema[params_per_name[param_name].sub_parameters[i].name] = null;
data[params_per_name[param_name].sub_parameters[i].name] = params_per_name[param_name].sub_parameters[i]["default"];
rowTemplate.push(params_per_name[param_name].sub_parameters[i]["default"]);
colHeaders.push(params_per_name[param_name].sub_parameters[i].display_name);
allHelps[params_per_name[param_name].sub_parameters[i].name] = params_per_name[param_name].sub_parameters[i].help;
if (params_per_name[param_name].sub_parameters[i].type == "bool") {
......@@ -775,7 +775,7 @@ Handsontable.cellTypes["bootdate"] = Handsontable.BootstrapDateCell;
var all_choices = $.merge( [""], params_per_name[param_name].sub_parameters[i].choices );
columns.push({data: params_per_name[param_name].sub_parameters[i].name, type:"dropdown", source:all_choices});
} else {
// TODO handle files and multiple values
// TODO handle files and append values
allTypes[params_per_name[param_name].sub_parameters[i].name] = params_per_name[param_name].sub_parameters[i].type;
allActions[params_per_name[param_name].sub_parameters[i].name] = params_per_name[param_name].sub_parameters[i].action;
allRequired[params_per_name[param_name].sub_parameters[i].name] = params_per_name[param_name].sub_parameters[i].required;
......@@ -819,8 +819,30 @@ Handsontable.cellTypes["bootdate"] = Handsontable.BootstrapDateCell;
});
}
}
// functions to handle templating from a new handsontable row
function isEmptyRow(instance, row) {
var rowData = instance.getData()[row];
for (var i = 0, ilen = rowData.length; i < ilen; i++) {
if (rowData[i] !== null) {
return false;
}
}
return true;
}
function defaultValueRenderer (instance, td, row, col, prop, value, cellProperties) {
var args = arguments;
if (args[5] === null && isEmptyRow(instance, row)) {
args[5] = rowTemplate[col];
td.style.color = '#999';
} else {
td.style.color = '';
}
Handsontable.renderers.TextRenderer.apply(this, args);
}
$(this).handsontable({
data: data,
data: [],
dataSchema: dataSchema,
colHeaders: colHeaders,
minSpareRows: 1,
......@@ -845,7 +867,7 @@ Handsontable.cellTypes["bootdate"] = Handsontable.BootstrapDateCell;
afterValidate: function(isValid, value, row, prop, source) {
if (!isValid) {
// do not add this error if it's on the last row
if (this.countRows()-1 != row) {
if (this.countRows() == 1 || this.countRows()-1 != row) {
$this.handsontable_errors[prop+"."+row] = $("#error_handsontable_"+prop.split("___")[0]).html();
}
} else {
......@@ -868,7 +890,40 @@ Handsontable.cellTypes["bootdate"] = Handsontable.BootstrapDateCell;
afterRender: function(isForced) {
// remove all errors from the last line (it's not a real value line)
for (var i = 0; i < this.countCols(); i++) {
$(this.getCell(this.countRows()-1, i)).removeClass("htInvalid")
if (this.countRows() != 1) {
$(this.getCell(this.countRows()-1, i)).removeClass("htInvalid")
}
}
},
cells: function (row, col, prop) {
var cellProperties = {};
cellProperties.renderer = defaultValueRenderer;
return cellProperties;
},
beforeChange: function (changes) {
var instance = this
, i
, ilen = changes.length
, c
, clen = instance.colCount
, rowColumnSeen = {}
, rowsToFill = {};
for (i = 0; i < ilen; i++) {
if (changes[i][2] === null && changes[i][3] !== null) { //if oldVal is empty
if (isEmptyRow(instance, changes[i][0])) {
rowColumnSeen[changes[i][0] + '/' + changes[i][1]] = true; //add this row/col combination to cache so it will not be overwritten by template
rowsToFill[changes[i][0]] = true;
}
}
}
for (var r in rowsToFill) {
if (rowsToFill.hasOwnProperty(r)) {
for (c = 0; c < clen; c++) {
if (!rowColumnSeen[r + '/' + c]) { //if it is not provided by user in this change set, take value from template
changes.push([r, c, null, rowTemplate[c]]);
}
}
}
}
}
});
......
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