2
0
mirror of https://github.com/frappe/books.git synced 2025-02-04 21:18:32 +00:00

Dynamic Labels

This commit is contained in:
thefalconx33 2019-09-17 13:44:09 +05:30
parent 187b852538
commit b0e572b16c
6 changed files with 259 additions and 272 deletions

View File

@ -358,7 +358,7 @@ module.exports = class BaseDocument extends Observable {
// helper functions // helper functions
getSum(tablefield, childfield) { getSum(tablefield, childfield) {
return this[tablefield] return this[tablefield]
.map(d => frappe.parseNumber(d[childfield]) || 0) .map(d => d[childfield] || 0)
.reduce((a, b) => a + b, 0); .reduce((a, b) => a + b, 0);
} }

View File

@ -1,6 +1,6 @@
const BaseDocument = require('./document'); const BaseDocument = require('./document');
const frappe = require('frappejs'); const frappe = require('frappejs');
const model = require('./index') const model = require('./index');
const indicatorColor = require('frappejs/ui/constants/indicators'); const indicatorColor = require('frappejs/ui/constants/indicators');
module.exports = class BaseMeta extends BaseDocument { module.exports = class BaseMeta extends BaseDocument {
@ -49,25 +49,28 @@ module.exports = class BaseMeta extends BaseDocument {
} }
getLabel(fieldname) { getLabel(fieldname) {
return this.getField(fieldname).label; let df = this.getField(fieldname);
return df.getLabel || df.label;
} }
getTableFields() { getTableFields() {
if (this._tableFields===undefined) { if (this._tableFields === undefined) {
this._tableFields = this.fields.filter(field => field.fieldtype === 'Table'); this._tableFields = this.fields.filter(
field => field.fieldtype === 'Table'
);
} }
return this._tableFields; return this._tableFields;
} }
getFormulaFields() { getFormulaFields() {
if (this._formulaFields===undefined) { if (this._formulaFields === undefined) {
this._formulaFields = this.fields.filter(field => field.formula); this._formulaFields = this.fields.filter(field => field.formula);
} }
return this._formulaFields; return this._formulaFields;
} }
hasFormula() { hasFormula() {
if (this._hasFormula===undefined) { if (this._hasFormula === undefined) {
this._hasFormula = false; this._hasFormula = false;
if (this.getFormulaFields().length) { if (this.getFormulaFields().length) {
this._hasFormula = true; this._hasFormula = true;
@ -94,39 +97,51 @@ module.exports = class BaseMeta extends BaseDocument {
getValidFields({ withChildren = true } = {}) { getValidFields({ withChildren = true } = {}) {
if (!this._validFields) { if (!this._validFields) {
this._validFields = []; this._validFields = [];
this._validFieldsWithChildren = []; this._validFieldsWithChildren = [];
const _add = (field) => { const _add = field => {
this._validFields.push(field); this._validFields.push(field);
this._validFieldsWithChildren.push(field); this._validFieldsWithChildren.push(field);
} };
const doctype_fields = this.fields.map((field) => field.fieldname); const doctype_fields = this.fields.map(field => field.fieldname);
// standard fields // standard fields
for (let field of model.commonFields) { for (let field of model.commonFields) {
if (frappe.db.typeMap[field.fieldtype] && !doctype_fields.includes(field.fieldname)) { if (
frappe.db.typeMap[field.fieldtype] &&
!doctype_fields.includes(field.fieldname)
) {
_add(field); _add(field);
} }
} }
if (this.isSubmittable) { if (this.isSubmittable) {
_add({fieldtype:'Check', fieldname: 'submitted', label: frappe._('Submitted')}) _add({
fieldtype: 'Check',
fieldname: 'submitted',
label: frappe._('Submitted')
});
} }
if (this.isChild) { if (this.isChild) {
// child fields // child fields
for (let field of model.childFields) { for (let field of model.childFields) {
if (frappe.db.typeMap[field.fieldtype] && !doctype_fields.includes(field.fieldname)) { if (
frappe.db.typeMap[field.fieldtype] &&
!doctype_fields.includes(field.fieldname)
) {
_add(field); _add(field);
} }
} }
} else { } else {
// parent fields // parent fields
for (let field of model.parentFields) { for (let field of model.parentFields) {
if (frappe.db.typeMap[field.fieldtype] && !doctype_fields.includes(field.fieldname)) { if (
frappe.db.typeMap[field.fieldtype] &&
!doctype_fields.includes(field.fieldname)
) {
_add(field); _add(field);
} }
} }
@ -135,7 +150,10 @@ module.exports = class BaseMeta extends BaseDocument {
if (this.isTree) { if (this.isTree) {
// tree fields // tree fields
for (let field of model.treeFields) { for (let field of model.treeFields) {
if (frappe.db.typeMap[field.fieldtype] && !doctype_fields.includes(field.fieldname)) { if (
frappe.db.typeMap[field.fieldtype] &&
!doctype_fields.includes(field.fieldname)
) {
_add(field); _add(field);
} }
} }
@ -167,10 +185,12 @@ module.exports = class BaseMeta extends BaseDocument {
if (!this._keywordFields) { if (!this._keywordFields) {
this._keywordFields = this.keywordFields; this._keywordFields = this.keywordFields;
if (!(this._keywordFields && this._keywordFields.length && this.fields)) { if (!(this._keywordFields && this._keywordFields.length && this.fields)) {
this._keywordFields = this.fields.filter(field => field.fieldtype !== 'Table' && field.required).map(field => field.fieldname); this._keywordFields = this.fields
.filter(field => field.fieldtype !== 'Table' && field.required)
.map(field => field.fieldname);
} }
if (!(this._keywordFields && this._keywordFields.length)) { if (!(this._keywordFields && this._keywordFields.length)) {
this._keywordFields = ['name'] this._keywordFields = ['name'];
} }
} }
return this._keywordFields; return this._keywordFields;
@ -185,7 +205,9 @@ module.exports = class BaseMeta extends BaseDocument {
options = field.options.split('\n'); options = field.options.split('\n');
} }
if (!options.includes(value)) { if (!options.includes(value)) {
throw new frappe.errors.ValueError(`${value} must be one of ${options.join(", ")}`); throw new frappe.errors.ValueError(
`${value} must be one of ${options.join(', ')}`
);
} }
return value; return value;
} }
@ -208,7 +230,7 @@ module.exports = class BaseMeta extends BaseDocument {
0: indicatorColor.GRAY, 0: indicatorColor.GRAY,
1: indicatorColor.BLUE 1: indicatorColor.BLUE
} }
} };
} }
} }
} }
@ -229,4 +251,4 @@ module.exports = class BaseMeta extends BaseDocument {
} }
} }
} }
} };

View File

@ -8,9 +8,10 @@
> >
<div class="col" v-for="(column, j) in section.columns" :key="j"> <div class="col" v-for="(column, j) in section.columns" :key="j">
<frappe-control <frappe-control
ref="frappe-control"
v-for="(fieldname, k) in column.fields" v-for="(fieldname, k) in column.fields"
v-if="shouldRenderField(fieldname)" v-if="shouldRenderField(fieldname)"
:key="k" :key="getDocField(fieldname).label"
:docfield="getDocField(fieldname)" :docfield="getDocField(fieldname)"
:value="$data[fieldname]" :value="$data[fieldname]"
:doc="doc" :doc="doc"
@ -48,10 +49,17 @@ export default {
this[df.fieldname] = doc[df.fieldname]; this[df.fieldname] = doc[df.fieldname];
}); });
} }
this.updateLabels();
}); });
this.setLabelOptions();
}, },
methods: { methods: {
updateLabels() {
this.$refs['frappe-control'].forEach(control => {
control.docfield.label = control.docfield.getLabel
? control.docfield.getLabel(this.doc)
: control.docfield.label;
});
},
getDocField(fieldname) { getDocField(fieldname) {
return this.fields.find(df => df.fieldname === fieldname); return this.fields.find(df => df.fieldname === fieldname);
}, },
@ -73,13 +81,6 @@ export default {
return true; return true;
}, },
setLabelOptions() {
this.fields.forEach(field => {
if (field.labelOption) {
field.labelOption = field.labelOption(this.doc);
}
});
},
updateDoc(fieldname, value) { updateDoc(fieldname, value) {
this.doc.set(fieldname, value); this.doc.set(fieldname, value);
this.$emit('updateDoc', { this.$emit('updateDoc', {

View File

@ -6,6 +6,11 @@ export default {
} }
return this.getWrapperElement(h); return this.getWrapperElement(h);
}, },
data() {
return {
label: this.docfield.label
};
},
props: { props: {
docfield: Object, docfield: Object,
value: [String, Number, Array, FileList], value: [String, Number, Array, FileList],
@ -68,23 +73,13 @@ export default {
]; ];
}, },
getLabelElement(h) { getLabelElement(h) {
const hasLabelOptions = Boolean(this.docfield.labelOption);
let label = this.docfield.label;
if (hasLabelOptions) {
const replaceableKeys = Object.keys(this.docfield.labelOption);
for (let key of replaceableKeys) {
label = label.replace(key.toString(), this.docfield.labelOption[key]);
}
}
return h('label', { return h('label', {
class: [this.labelClass, 'text-muted'], class: [this.labelClass, 'text-muted'],
attrs: { attrs: {
for: this.id for: this.id
}, },
domProps: { domProps: {
textContent: label textContent: this.label
} }
}); });
}, },

View File

@ -9,15 +9,7 @@ module.exports = {
field = { fieldtype: field }; field = { fieldtype: field };
} }
if (field.fieldtype === 'Currency') { if (field.fieldtype === 'Currency') {
if (field.currencyInfo) {
value = numberFormat.formatNumber(
value,
field.currencyInfo.numberFormat,
field.currencyInfo.symbol
);
} else {
value = numberFormat.formatNumber(value); value = numberFormat.formatNumber(value);
}
} else if (field.fieldtype === 'Text') { } else if (field.fieldtype === 'Text') {
// value = markdown.makeHtml(value || ''); // value = markdown.makeHtml(value || '');
} else if (field.fieldtype === 'Date') { } else if (field.fieldtype === 'Date') {

View File

@ -14,39 +14,21 @@ const numberFormats = {
module.exports = { module.exports = {
// parse a formatted number string // parse a formatted number string
// from "4,555,000.34" -> 4555000.34 // from "4,555,000.34" -> 4555000.34
parseNumber(number, format, symbol) { parseNumber(number, format = '#,###.##') {
if (!number) { if (!number) {
return 0; return 0;
} }
if (!format) {
format = frappe.AccountingSettings.numberFormat || '#,###.##';
}
if (!symbol) {
symbol = frappe.AccountingSettings.symbol || '';
}
if (typeof number === 'number') { if (typeof number === 'number') {
return number; return number;
} }
if (isNaN(parseFloat(number))) {
// remove symbol
number = number.substr(2);
}
const info = this.getFormatInfo(format); const info = this.getFormatInfo(format);
return parseFloat(this.removeSeparator(number, info.groupSep)); return parseFloat(this.removeSeparator(number, info.groupSep));
}, },
formatNumber(number, format, symbol, precision = null) { formatNumber(number, format = '#,###.##', precision = null) {
if (!number) { if (!number) {
number = 0; number = 0;
} }
if (!format) {
format = frappe.AccountingSettings.numberFormat || '#,###.##';
}
if (!symbol) {
symbol = frappe.AccountingSettings.symbol || '';
}
let info = this.getFormatInfo(format); let info = this.getFormatInfo(format);
if (precision) { if (precision) {
info.precision = precision; info.precision = precision;
@ -96,12 +78,7 @@ module.exports = {
parts[1] = parts[1] && info.fractionSep ? info.fractionSep + parts[1] : ''; parts[1] = parts[1] && info.fractionSep ? info.fractionSep + parts[1] : '';
// join // join
return ( return (is_negative ? '-' : '') + parts[0] + parts[1];
(symbol.length ? `${symbol} ` : '') +
(is_negative ? '-' : '') +
parts[0] +
parts[1]
);
}, },
getFormatInfo(format) { getFormatInfo(format) {