2
0
mirror of https://github.com/frappe/books.git synced 2024-11-10 15:50:56 +00:00
books/model/meta.js

123 lines
3.4 KiB
JavaScript
Raw Normal View History

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-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);
this.event_handlers = {};
this.list_options = {
fields: ['name', 'modified']
};
if (this.setup_meta) {
this.setup_meta();
}
}
get_field(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];
}
get_table_fields() {
if (!this._table_fields) {
this._table_fields = this.fields.filter(field => field.fieldtype==='Table');
}
return this._table_fields;
}
2018-01-12 12:25:07 +00:00
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-01-31 12:56:21 +00:00
get_valid_fields({with_children = true} = {}) {
2018-01-12 12:25:07 +00:00
if (!this._valid_fields) {
this._valid_fields = [];
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-01-31 12:56:21 +00:00
for (let field of frappe.model.common_fields) {
if (frappe.db.type_map[field.fieldtype] && !doctype_fields.includes(field.fieldname)) {
this._valid_fields.push(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
if (this.is_child) {
// child fields
for (let field of frappe.model.child_fields) {
if (frappe.db.type_map[field.fieldtype] && !doctype_fields.includes(field.fieldname)) {
this._valid_fields.push(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 {
// parent fields
for (let field of frappe.model.parent_fields) {
if (frappe.db.type_map[field.fieldtype] && !doctype_fields.includes(field.fieldname)) {
this._valid_fields.push(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) {
let include = frappe.db.type_map[field.fieldtype];
// include tables if (with_children = True)
if (!include && with_children) {
include = field.fieldtype === 'Table';
}
if (include) {
this._valid_fields.push(field);
2018-01-12 12:25:07 +00:00
}
}
}
return this._valid_fields;
}
get_keyword_fields() {
2018-01-31 12:56:21 +00:00
return this.keyword_fields || this.meta.fields.filter(field => field.reqd).map(field => field.fieldname);
2018-01-12 12:25:07 +00:00
}
2018-01-31 12:56:21 +00:00
validate_select(field, value) {
let options = field.options;
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)) {
throw new frappe.errors.ValueError(`${value} must be one of ${options.join(", ")}`);
2018-01-12 12:25:07 +00:00
}
return value;
2018-01-12 12:25:07 +00:00
}
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 11:52:05 +00:00
}