2018-01-24 17:22:05 +05:30
|
|
|
const BaseDocument = require('./document');
|
2018-01-16 11:39:17 +05:30
|
|
|
const frappe = require('frappejs');
|
2018-01-12 17:55:07 +05:30
|
|
|
|
2018-01-24 17:22:05 +05:30
|
|
|
module.exports = class BaseMeta extends BaseDocument {
|
2018-01-12 17:55:07 +05:30
|
|
|
constructor(data) {
|
|
|
|
super(data);
|
|
|
|
this.event_handlers = {};
|
|
|
|
this.list_options = {
|
|
|
|
fields: ['name', 'modified']
|
|
|
|
};
|
2018-01-31 18:34:46 +05:30
|
|
|
if (this.setup_meta) {
|
2018-01-12 17:55:07 +05:30
|
|
|
this.setup_meta();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
get_field(fieldname) {
|
2018-01-31 18:26:21 +05:30
|
|
|
if (!this._field_map) {
|
|
|
|
this._field_map = {};
|
|
|
|
for (let field of this.fields) {
|
|
|
|
this._field_map[field.fieldname] = field;
|
2018-01-12 17:55:07 +05:30
|
|
|
}
|
|
|
|
}
|
2018-01-31 18:26:21 +05:30
|
|
|
return this._field_map[fieldname];
|
2018-01-31 18:34:46 +05:30
|
|
|
}
|
2018-01-31 18:26:21 +05:30
|
|
|
|
2018-02-08 12:16:38 +05:30
|
|
|
getTableFields() {
|
|
|
|
if (!this._tableFields) {
|
|
|
|
this._tableFields = this.fields.filter(field => field.fieldtype === 'Table');
|
2018-01-31 18:34:46 +05:30
|
|
|
}
|
2018-02-08 12:16:38 +05:30
|
|
|
return this._tableFields;
|
2018-01-31 18:34:46 +05:30
|
|
|
}
|
2018-01-12 17:55:07 +05:30
|
|
|
|
|
|
|
on(key, fn) {
|
|
|
|
if (!this.event_handlers[key]) {
|
|
|
|
this.event_handlers[key] = [];
|
|
|
|
}
|
|
|
|
this.event_handlers[key].push(fn);
|
|
|
|
}
|
|
|
|
|
|
|
|
async set(fieldname, value) {
|
|
|
|
this[fieldname] = value;
|
|
|
|
await this.trigger(fieldname);
|
|
|
|
}
|
|
|
|
|
|
|
|
get(fieldname) {
|
|
|
|
return this[fieldname];
|
|
|
|
}
|
|
|
|
|
2018-02-08 12:16:38 +05:30
|
|
|
getValidFields({ with_children = true } = {}) {
|
2018-01-12 17:55:07 +05:30
|
|
|
if (!this._valid_fields) {
|
2018-02-01 16:37:36 +05:30
|
|
|
|
2018-01-12 17:55:07 +05:30
|
|
|
this._valid_fields = [];
|
2018-02-01 16:37:36 +05:30
|
|
|
this._valid_fields_with_children = [];
|
|
|
|
|
|
|
|
const _add = (field) => {
|
|
|
|
this._valid_fields.push(field);
|
|
|
|
this._valid_fields_with_children.push(field);
|
|
|
|
}
|
2018-01-12 17:55:07 +05:30
|
|
|
|
2018-01-31 18:26:21 +05:30
|
|
|
const doctype_fields = this.fields.map((field) => field.fieldname);
|
2018-01-23 13:30:29 +05:30
|
|
|
|
2018-01-12 17:55:07 +05:30
|
|
|
// standard fields
|
2018-01-31 18:26:21 +05:30
|
|
|
for (let field of frappe.model.common_fields) {
|
|
|
|
if (frappe.db.type_map[field.fieldtype] && !doctype_fields.includes(field.fieldname)) {
|
2018-02-01 16:37:36 +05:30
|
|
|
_add(field);
|
2018-01-23 13:30:29 +05:30
|
|
|
}
|
2018-01-12 17:55:07 +05:30
|
|
|
}
|
|
|
|
|
2018-01-31 18:26:21 +05:30
|
|
|
if (this.is_child) {
|
2018-01-31 18:34:46 +05:30
|
|
|
// child fields
|
|
|
|
for (let field of frappe.model.child_fields) {
|
2018-01-31 18:26:21 +05:30
|
|
|
if (frappe.db.type_map[field.fieldtype] && !doctype_fields.includes(field.fieldname)) {
|
2018-02-01 16:37:36 +05:30
|
|
|
_add(field);
|
2018-01-23 13:30:29 +05:30
|
|
|
}
|
2018-01-12 17:55:07 +05:30
|
|
|
}
|
2018-01-31 18:26:21 +05:30
|
|
|
} else {
|
2018-01-31 18:34:46 +05:30
|
|
|
// parent fields
|
|
|
|
for (let field of frappe.model.parent_fields) {
|
2018-01-31 18:26:21 +05:30
|
|
|
if (frappe.db.type_map[field.fieldtype] && !doctype_fields.includes(field.fieldname)) {
|
2018-02-01 16:37:36 +05:30
|
|
|
_add(field);
|
2018-01-31 18:26:21 +05:30
|
|
|
}
|
|
|
|
}
|
2018-01-31 18:34:46 +05:30
|
|
|
}
|
2018-01-12 17:55:07 +05:30
|
|
|
|
|
|
|
// doctype fields
|
2018-01-31 18:26:21 +05:30
|
|
|
for (let field of this.fields) {
|
2018-01-31 18:34:46 +05:30
|
|
|
let include = frappe.db.type_map[field.fieldtype];
|
2018-01-31 18:26:21 +05:30
|
|
|
|
|
|
|
if (include) {
|
2018-02-01 16:37:36 +05:30
|
|
|
_add(field);
|
|
|
|
}
|
|
|
|
|
|
|
|
// include tables if (with_children = True)
|
|
|
|
if (!include && field.fieldtype === 'Table') {
|
|
|
|
this._valid_fields_with_children.push(field);
|
2018-01-12 17:55:07 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-01 16:37:36 +05:30
|
|
|
if (with_children) {
|
|
|
|
return this._valid_fields_with_children;
|
|
|
|
} else {
|
|
|
|
return this._valid_fields;
|
|
|
|
}
|
2018-01-12 17:55:07 +05:30
|
|
|
}
|
|
|
|
|
2018-02-08 12:16:38 +05:30
|
|
|
getKeywordFields() {
|
2018-01-31 18:26:21 +05:30
|
|
|
return this.keyword_fields || this.meta.fields.filter(field => field.reqd).map(field => field.fieldname);
|
2018-01-12 17:55:07 +05:30
|
|
|
}
|
|
|
|
|
2018-01-31 18:26:21 +05:30
|
|
|
validate_select(field, value) {
|
|
|
|
let options = field.options;
|
2018-01-12 17:55:07 +05:30
|
|
|
if (typeof options === 'string') {
|
|
|
|
// values given as string
|
2018-01-31 18:26:21 +05:30
|
|
|
options = field.options.split('\n');
|
2018-01-12 17:55:07 +05:30
|
|
|
}
|
|
|
|
if (!options.includes(value)) {
|
2018-01-15 17:25:31 +05:30
|
|
|
throw new frappe.errors.ValueError(`${value} must be one of ${options.join(", ")}`);
|
2018-01-12 17:55:07 +05:30
|
|
|
}
|
2018-01-25 15:34:48 +05:30
|
|
|
return value;
|
2018-01-12 17:55:07 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
async trigger(key, event = {}) {
|
|
|
|
Object.assign(event, {
|
|
|
|
doc: this,
|
|
|
|
name: key
|
|
|
|
});
|
|
|
|
|
|
|
|
if (this.event_handlers[key]) {
|
|
|
|
for (var handler of this.event_handlers[key]) {
|
|
|
|
await handler(event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-01-24 17:22:05 +05:30
|
|
|
}
|