2018-01-24 11:52:05 +00:00
|
|
|
const BaseDocument = require('./document');
|
2018-01-16 06:09:17 +00:00
|
|
|
const frappe = require('frappejs');
|
2018-02-16 13:13:46 +00:00
|
|
|
const model = require('./index')
|
2018-07-09 14:55:24 +00:00
|
|
|
const indicatorColor = require('frappejs/ui/constants/indicators');
|
2018-01-12 12:25:07 +00:00
|
|
|
|
2018-01-24 11:52:05 +00:00
|
|
|
module.exports = class BaseMeta extends BaseDocument {
|
2018-01-12 12:25:07 +00:00
|
|
|
constructor(data) {
|
|
|
|
super(data);
|
2018-02-22 11:21:42 +00:00
|
|
|
this.setDefaultIndicators();
|
2018-02-08 12:28:51 +00:00
|
|
|
if (this.setupMeta) {
|
|
|
|
this.setupMeta();
|
2018-01-12 12:25:07 +00:00
|
|
|
}
|
2018-02-22 11:21:42 +00:00
|
|
|
if (!this.titleField) {
|
|
|
|
this.titleField = 'name';
|
|
|
|
}
|
2018-01-12 12:25:07 +00:00
|
|
|
}
|
|
|
|
|
2018-02-19 06:31:07 +00:00
|
|
|
hasField(fieldname) {
|
|
|
|
return this.getField(fieldname) ? true : false;
|
|
|
|
}
|
|
|
|
|
2018-02-08 11:45:32 +00:00
|
|
|
getField(fieldname) {
|
2018-01-31 12:56:21 +00:00
|
|
|
if (!this._field_map) {
|
|
|
|
this._field_map = {};
|
|
|
|
for (let field of this.fields) {
|
|
|
|
this._field_map[field.fieldname] = field;
|
2018-01-12 12:25:07 +00:00
|
|
|
}
|
|
|
|
}
|
2018-01-31 12:56:21 +00:00
|
|
|
return this._field_map[fieldname];
|
2018-01-31 13:04:46 +00:00
|
|
|
}
|
2018-01-31 12:56:21 +00:00
|
|
|
|
2018-08-18 15:54:17 +00:00
|
|
|
/**
|
|
|
|
* Get fields filtered by filters
|
|
|
|
* @param {Object} filters
|
|
|
|
*
|
|
|
|
* Usage:
|
|
|
|
* meta = frappe.getMeta('ToDo')
|
|
|
|
* dataFields = meta.getFieldsWith({ fieldtype: 'Data' })
|
|
|
|
*/
|
|
|
|
getFieldsWith(filters) {
|
|
|
|
return this.fields.filter(df => {
|
|
|
|
let match = true;
|
|
|
|
for (const key in filters) {
|
|
|
|
const value = filters[key];
|
|
|
|
match = df[key] === value;
|
|
|
|
}
|
|
|
|
return match;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-03-09 13:00:51 +00:00
|
|
|
getLabel(fieldname) {
|
|
|
|
return this.getField(fieldname).label;
|
|
|
|
}
|
|
|
|
|
2018-02-08 06:46:38 +00:00
|
|
|
getTableFields() {
|
2018-02-08 11:45:32 +00:00
|
|
|
if (this._tableFields===undefined) {
|
2018-02-08 06:46:38 +00:00
|
|
|
this._tableFields = this.fields.filter(field => field.fieldtype === 'Table');
|
2018-01-31 13:04:46 +00:00
|
|
|
}
|
2018-02-08 06:46:38 +00:00
|
|
|
return this._tableFields;
|
2018-01-31 13:04:46 +00:00
|
|
|
}
|
2018-01-12 12:25:07 +00:00
|
|
|
|
2018-02-08 11:45:32 +00:00
|
|
|
getFormulaFields() {
|
|
|
|
if (this._formulaFields===undefined) {
|
|
|
|
this._formulaFields = this.fields.filter(field => field.formula);
|
|
|
|
}
|
|
|
|
return this._formulaFields;
|
|
|
|
}
|
|
|
|
|
2018-02-16 13:13:46 +00:00
|
|
|
hasFormula() {
|
|
|
|
if (this._hasFormula===undefined) {
|
|
|
|
this._hasFormula = false;
|
2018-02-09 12:55:55 +00:00
|
|
|
if (this.getFormulaFields().length) {
|
2018-02-16 13:13:46 +00:00
|
|
|
this._hasFormula = true;
|
2018-02-09 12:55:55 +00:00
|
|
|
} else {
|
|
|
|
for (let tablefield of this.getTableFields()) {
|
|
|
|
if (frappe.getMeta(tablefield.childtype).getFormulaFields().length) {
|
2018-02-16 13:13:46 +00:00
|
|
|
this._hasFormula = true;
|
2018-02-09 12:55:55 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-02-16 13:13:46 +00:00
|
|
|
return this._hasFormula;
|
2018-02-09 12:55:55 +00:00
|
|
|
}
|
|
|
|
|
2018-01-12 12:25:07 +00:00
|
|
|
async set(fieldname, value) {
|
|
|
|
this[fieldname] = value;
|
|
|
|
await this.trigger(fieldname);
|
|
|
|
}
|
|
|
|
|
|
|
|
get(fieldname) {
|
|
|
|
return this[fieldname];
|
|
|
|
}
|
|
|
|
|
2018-02-12 12:01:31 +00:00
|
|
|
getValidFields({ withChildren = true } = {}) {
|
2018-02-27 16:29:14 +00:00
|
|
|
if (!this._validFields) {
|
2018-02-01 11:07:36 +00:00
|
|
|
|
2018-02-27 16:29:14 +00:00
|
|
|
this._validFields = [];
|
|
|
|
this._validFieldsWithChildren = [];
|
2018-02-01 11:07:36 +00:00
|
|
|
|
|
|
|
const _add = (field) => {
|
2018-02-27 16:29:14 +00:00
|
|
|
this._validFields.push(field);
|
|
|
|
this._validFieldsWithChildren.push(field);
|
2018-02-01 11:07:36 +00:00
|
|
|
}
|
2018-01-12 12:25:07 +00:00
|
|
|
|
2018-01-31 12:56:21 +00:00
|
|
|
const doctype_fields = this.fields.map((field) => field.fieldname);
|
2018-01-23 08:00:29 +00:00
|
|
|
|
2018-01-12 12:25:07 +00:00
|
|
|
// standard fields
|
2018-02-27 16:29:14 +00:00
|
|
|
for (let field of model.commonFields) {
|
|
|
|
if (frappe.db.typeMap[field.fieldtype] && !doctype_fields.includes(field.fieldname)) {
|
2018-02-01 11:07:36 +00:00
|
|
|
_add(field);
|
2018-01-23 08:00:29 +00:00
|
|
|
}
|
2018-01-12 12:25:07 +00:00
|
|
|
}
|
|
|
|
|
2018-02-27 16:29:14 +00:00
|
|
|
if (this.isSubmittable) {
|
|
|
|
_add({fieldtype:'Check', fieldname: 'submitted', label: frappe._('Submitted')})
|
|
|
|
}
|
|
|
|
|
2018-02-12 12:01:31 +00:00
|
|
|
if (this.isChild) {
|
2018-01-31 13:04:46 +00:00
|
|
|
// child fields
|
2018-02-27 16:29:14 +00:00
|
|
|
for (let field of model.childFields) {
|
|
|
|
if (frappe.db.typeMap[field.fieldtype] && !doctype_fields.includes(field.fieldname)) {
|
2018-02-01 11:07:36 +00:00
|
|
|
_add(field);
|
2018-01-23 08:00:29 +00:00
|
|
|
}
|
2018-01-12 12:25:07 +00:00
|
|
|
}
|
2018-01-31 12:56:21 +00:00
|
|
|
} else {
|
2018-01-31 13:04:46 +00:00
|
|
|
// parent fields
|
2018-02-27 16:29:14 +00:00
|
|
|
for (let field of model.parentFields) {
|
|
|
|
if (frappe.db.typeMap[field.fieldtype] && !doctype_fields.includes(field.fieldname)) {
|
2018-02-01 11:07:36 +00:00
|
|
|
_add(field);
|
2018-01-31 12:56:21 +00:00
|
|
|
}
|
|
|
|
}
|
2018-01-31 13:04:46 +00:00
|
|
|
}
|
2018-01-12 12:25:07 +00:00
|
|
|
|
2018-04-18 06:48:24 +00:00
|
|
|
if (this.isTree) {
|
|
|
|
// tree fields
|
|
|
|
for (let field of model.treeFields) {
|
|
|
|
if (frappe.db.typeMap[field.fieldtype] && !doctype_fields.includes(field.fieldname)) {
|
|
|
|
_add(field);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-12 12:25:07 +00:00
|
|
|
// doctype fields
|
2018-01-31 12:56:21 +00:00
|
|
|
for (let field of this.fields) {
|
2018-02-27 16:29:14 +00:00
|
|
|
let include = frappe.db.typeMap[field.fieldtype];
|
2018-01-31 12:56:21 +00:00
|
|
|
|
|
|
|
if (include) {
|
2018-02-01 11:07:36 +00:00
|
|
|
_add(field);
|
|
|
|
}
|
|
|
|
|
2018-02-12 12:01:31 +00:00
|
|
|
// include tables if (withChildren = True)
|
2018-02-01 11:07:36 +00:00
|
|
|
if (!include && field.fieldtype === 'Table') {
|
2018-02-27 16:29:14 +00:00
|
|
|
this._validFieldsWithChildren.push(field);
|
2018-01-12 12:25:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-12 12:01:31 +00:00
|
|
|
if (withChildren) {
|
2018-02-27 16:29:14 +00:00
|
|
|
return this._validFieldsWithChildren;
|
2018-02-01 11:07:36 +00:00
|
|
|
} else {
|
2018-02-27 16:29:14 +00:00
|
|
|
return this._validFields;
|
2018-02-01 11:07:36 +00:00
|
|
|
}
|
2018-01-12 12:25:07 +00:00
|
|
|
}
|
|
|
|
|
2018-02-08 06:46:38 +00:00
|
|
|
getKeywordFields() {
|
2018-02-12 12:01:31 +00:00
|
|
|
if (!this._keywordFields) {
|
|
|
|
this._keywordFields = this.keywordFields;
|
|
|
|
if (!(this._keywordFields && this._keywordFields.length && this.fields)) {
|
2018-03-08 13:31:22 +00:00
|
|
|
this._keywordFields = this.fields.filter(field => field.fieldtype !== 'Table' && field.required).map(field => field.fieldname);
|
2018-02-12 12:01:31 +00:00
|
|
|
}
|
|
|
|
if (!(this._keywordFields && this._keywordFields.length)) {
|
|
|
|
this._keywordFields = ['name']
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return this._keywordFields;
|
2018-01-12 12:25:07 +00:00
|
|
|
}
|
|
|
|
|
2018-02-13 11:54:57 +00:00
|
|
|
validateSelect(field, value) {
|
2018-01-31 12:56:21 +00:00
|
|
|
let options = field.options;
|
2018-03-08 13:31:22 +00:00
|
|
|
if (!options) return;
|
|
|
|
|
2018-01-12 12:25:07 +00:00
|
|
|
if (typeof options === 'string') {
|
|
|
|
// values given as string
|
2018-01-31 12:56:21 +00:00
|
|
|
options = field.options.split('\n');
|
2018-01-12 12:25:07 +00:00
|
|
|
}
|
|
|
|
if (!options.includes(value)) {
|
2018-01-15 11:55:31 +00:00
|
|
|
throw new frappe.errors.ValueError(`${value} must be one of ${options.join(", ")}`);
|
2018-01-12 12:25:07 +00:00
|
|
|
}
|
2018-01-25 10:04:48 +00:00
|
|
|
return value;
|
2018-01-12 12:25:07 +00:00
|
|
|
}
|
|
|
|
|
2018-02-13 11:54:57 +00:00
|
|
|
async trigger(event, params = {}) {
|
|
|
|
Object.assign(params, {
|
2018-01-12 12:25:07 +00:00
|
|
|
doc: this,
|
2018-02-13 11:54:57 +00:00
|
|
|
name: event
|
2018-01-12 12:25:07 +00:00
|
|
|
});
|
|
|
|
|
2018-02-13 11:54:57 +00:00
|
|
|
await super.trigger(event, params);
|
2018-01-12 12:25:07 +00:00
|
|
|
}
|
2018-02-22 11:21:42 +00:00
|
|
|
|
|
|
|
setDefaultIndicators() {
|
|
|
|
if (!this.indicators) {
|
2018-02-27 16:29:14 +00:00
|
|
|
if (this.isSubmittable) {
|
|
|
|
this.indicators = {
|
|
|
|
key: 'submitted',
|
|
|
|
colors: {
|
2018-07-09 14:55:24 +00:00
|
|
|
0: indicatorColor.GRAY,
|
|
|
|
1: indicatorColor.BLUE
|
2018-02-27 16:29:14 +00:00
|
|
|
}
|
2018-02-22 11:21:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
getIndicatorColor(doc) {
|
|
|
|
if (frappe.isDirty(this.name, doc.name)) {
|
2018-07-09 14:55:24 +00:00
|
|
|
return indicatorColor.ORANGE;
|
2018-02-22 11:21:42 +00:00
|
|
|
} else {
|
2018-02-27 16:29:14 +00:00
|
|
|
if (this.indicators) {
|
|
|
|
let value = doc[this.indicators.key];
|
|
|
|
if (value) {
|
2018-07-09 14:55:24 +00:00
|
|
|
return this.indicators.colors[value] || indicatorColor.GRAY;
|
2018-02-27 16:29:14 +00:00
|
|
|
} else {
|
2018-07-09 14:55:24 +00:00
|
|
|
return indicatorColor.GRAY;
|
2018-02-27 16:29:14 +00:00
|
|
|
}
|
2018-02-22 11:21:42 +00:00
|
|
|
} else {
|
2018-07-09 14:55:24 +00:00
|
|
|
return indicatorColor.GRAY;
|
2018-02-22 11:21:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-01-24 11:52:05 +00:00
|
|
|
}
|