Commit a439b2ae authored by Sven Franck's avatar Sven Franck

app: made text format/crop/merge generic over table/list

parent a9679be0
......@@ -1739,11 +1739,13 @@
factory.element({
"type": block.type,
"direct": {
"className": (block.aside ? "ui-li-aside" : "") +
(block.text_i18n ? "translate" : "")
"className": (block.aside ? "ui-li-aside " : " ") +
(block.text_i18n ? "translate " : " ") + block.class_list
},
"attributes": {"data-i18n": block.text_i18n || ""},
"logic": {"text": block.text}
"logic": {
"text": block.text,
"data-i18n": block.text_i18n || null
}
})
);
}
......@@ -3365,21 +3367,10 @@
cell = util.mergeObject(core, util.cloneObject(segment.field_list[k]));
// custom header (!) cells have text/i18n defined
// TODO: convert to input field here?
if (!cell.custom) {
crop = cell.crop;
// HACK: only used for price_currency crop contents
if (crop) {
re = new RegExp(crop, 'g');
cell.text = record[cell.field].replace(re, '');
} else {
// TODO: not all numbers want to be rounded to 2 digits....
if (!isNaN(parseFloat(record[cell.field])) && isFinite(record[cell.field])) {
cell.text = Math.round(record[cell.field]).toFixed(2);
} else {
cell.text = record[cell.field];
}
}
cell.text_i18n = null;
cell.text = util.generateText(record[cell.field], cell, record)
cell.text_i18n = record.text_i18n || null;
}
row.push(cell);
}
......@@ -3460,14 +3451,16 @@
for (k = 0; k < section.field_list.length; k += 1) {
field = section.field_list[k];
// add object to text-array
if (new_item[pos].text) {
// add object to text-array
// TODO: so much nulllll, also how to convert to input field?
new_item[pos].text.push({
"type": field.type || "span",
"text": record[field.field],
"text": util.generateText(record[field.field], field, record),
"aside": field.aside || null,
"count": field.count || null,
"text_i18n": record[field.field + "_18n"] || null
"text_i18n": record.text_i18n || null,
"class_list": field.class_list || ""
});
} else {
// set type to record field or field value (custom fields!)
......@@ -5439,6 +5432,58 @@
*/
util = {};
/**
* Crop a string (data field)
* @method crop
* @param {string} str String to crop
* @param {string} crop String to crop
* @return {string} cropped string
**/
util.crop = function (str, crop) {
var re = new RegExp(crop, 'g');
return str.replace(re, '');
};
/**
* format a string (data field)
* @method format
* @param {string} str String to format
* @param {object} spec Formating options (type, etc)
* @return {string} formatted string
**/
util.format = function (str, spec) {
switch (spec.type) {
case "integer":
if (!isNaN(parseFloat(str)) && isFinite(str)) {
return Math.round(str).toFixed(spec.digits);
}
}
return str;
};
/**
* generates a text based on mapping criteria
* @method generateText
* @param {string} str String to modify
* @param {object} spec Formatting options
* @param {object} record Full record (only used for merge)
* @return {string} modified string
**/
util.generateText = function (str, spec, record) {
if (spec.crop) {
str = util.crop(str, spec.crop);
}
if (spec.format) {
str = util.format(str, spec.format);
}
// TODO: ? vs merge -> make generic
if (spec.mergeText) {
str += " " + record[spec.mergeText];
}
return str;
};
/**
* Fetch a value from an object based on an array path
* @method fetchByArray
......
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