mirror of
https://github.com/frappe/books.git
synced 2024-11-10 15:50:56 +00:00
103 lines
2.7 KiB
JavaScript
103 lines
2.7 KiB
JavaScript
const BaseDocument = require('./document');
|
|
const frappe = require('frappejs');
|
|
|
|
module.exports = class BaseMeta extends BaseDocument {
|
|
constructor(data) {
|
|
super(data);
|
|
this.event_handlers = {};
|
|
this.list_options = {
|
|
fields: ['name', 'modified']
|
|
};
|
|
if (this.setup_meta) {
|
|
this.setup_meta();
|
|
}
|
|
}
|
|
|
|
get_field(fieldname) {
|
|
if (!this.field_map) {
|
|
this.field_map = {};
|
|
for (let df of this.fields) {
|
|
this.field_map[df.fieldname] = df;
|
|
}
|
|
}
|
|
return this.field_map[fieldname];
|
|
}
|
|
|
|
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];
|
|
}
|
|
|
|
get_valid_fields() {
|
|
if (!this._valid_fields) {
|
|
this._valid_fields = [];
|
|
|
|
const doctype_fields = this.fields.map((df) => df.fieldname);
|
|
|
|
// standard fields
|
|
for (let df of frappe.model.standard_fields) {
|
|
if (frappe.db.type_map[df.fieldtype] && !doctype_fields.includes(df.fieldname)) {
|
|
this._valid_fields.push(df);
|
|
}
|
|
}
|
|
|
|
// parent fields
|
|
if (this.istable) {
|
|
for (let df of frappe.model.child_fields) {
|
|
if (frappe.db.type_map[df.fieldtype] && !doctype_fields.includes(df.fieldname)) {
|
|
this._valid_fields.push(df);
|
|
}
|
|
}
|
|
}
|
|
|
|
// doctype fields
|
|
for (let df of this.fields) {
|
|
if (frappe.db.type_map[df.fieldtype]) {
|
|
this._valid_fields.push(df);
|
|
}
|
|
}
|
|
}
|
|
|
|
return this._valid_fields;
|
|
}
|
|
|
|
get_keyword_fields() {
|
|
return this.keyword_fields || this.meta.fields.filter(df => df.reqd).map(df => df.fieldname);
|
|
}
|
|
|
|
validate_select(df, value) {
|
|
let options = df.options;
|
|
if (typeof options === 'string') {
|
|
// values given as string
|
|
options = df.options.split('\n');
|
|
}
|
|
if (!options.includes(value)) {
|
|
throw new frappe.errors.ValueError(`${value} must be one of ${options.join(", ")}`);
|
|
}
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|
|
}
|
|
} |