123 lines
3.8 KiB
JavaScript
123 lines
3.8 KiB
JavaScript
const VersionedModel = require('../VersionedModel')
|
|
const uuid = require('uuid/v4')
|
|
|
|
/*
|
|
* Node Model
|
|
* -------------------------------------------------------------
|
|
* Put some description here!
|
|
*/
|
|
class Node extends VersionedModel {
|
|
static get services() {
|
|
return [...super.services, 'models']
|
|
}
|
|
|
|
static get schema() {
|
|
// Return a flitter-orm schema here.
|
|
return {
|
|
...super.schema,
|
|
UUID: { type: String, default: () => uuid() },
|
|
Type: String,
|
|
Value: {
|
|
Mode: {type: String, default: 'norm'},
|
|
Value: String,
|
|
},
|
|
AdditionalData: Object,
|
|
PageId: String,
|
|
CreatedAt: { type: Date, default: () => new Date },
|
|
UpdatedAt: { type: Date, default: () => new Date },
|
|
CreatedUserId: String,
|
|
UpdateUserId: String
|
|
};
|
|
}
|
|
|
|
// Static and instance methods can go here
|
|
get page() {
|
|
const Page = this.models.get('api:Page')
|
|
return this.belongs_to_one(Page, "PageId", "UUID")
|
|
}
|
|
|
|
to_html() {
|
|
switch(this.Type){
|
|
case 'paragraph':
|
|
return '<p>' + this.Value.Value + '</p>'
|
|
case 'header1':
|
|
return '<h1>' + this.Value.Value + '</h1>'
|
|
case 'header2':
|
|
return '<h2>' + this.Value.Value + '</h2>'
|
|
case 'header3':
|
|
return '<h3>' + this.Value.Value + '</h3>'
|
|
case 'header4':
|
|
return '<h4>' + this.Value.Value + '</h4>'
|
|
case 'block_code':
|
|
return '<pre><code>' + this.Value.Value + '</code></pre>'
|
|
case 'click_link':
|
|
return '<a href="' + this.Value.Value + '">' + this.Value.Value + '</a>'
|
|
}
|
|
}
|
|
|
|
update_from_raw(data) {
|
|
if ( data.Type ) this.Type = data.Type
|
|
if ( data.Value ) this.Value = data.Value
|
|
if ( data.AdditionalData ) this.AdditionalData = data.AdditionalData;
|
|
}
|
|
|
|
async cast_to_version_data() {
|
|
const data = await super.cast_to_version_data()
|
|
|
|
// If supported, fetch the current version of the node item
|
|
if ( this.Type === 'code_ref' && this.Value?.Value ) {
|
|
const Codium = this.models.get('api:Codium')
|
|
const code = await Codium.findOne({ UUID: this.Value?.Value, Active: true })
|
|
if ( code ) {
|
|
data.associated_type_version_num = code.version_num
|
|
}
|
|
} else if ( this.Type === 'file_ref' && this.Value?.Value ) {
|
|
const FileGroup = this.models.get('api:FileGroup')
|
|
const group = await FileGroup.findOne({ UUID: this.Value?.Value })
|
|
if ( group ) {
|
|
data.associated_type_version_num = group.version_num
|
|
}
|
|
} else if ( this.Type === 'database_ref' && this.Value?.Value ) {
|
|
const Database = this.models.get('api:db:Database')
|
|
const db = await Database.findOne({ Active: true, UUID: this.Value?.Value })
|
|
if ( db ) {
|
|
data.associated_type_version_num = db.version_num
|
|
}
|
|
}
|
|
|
|
return data
|
|
}
|
|
|
|
async revert_to_version(version_num, user_id = undefined) {
|
|
const reverted = await super.revert_to_version(version_num, user_id)
|
|
const data = await this.get_version_data(version_num)
|
|
|
|
if ( !data.associated_type_version_num ) return reverted
|
|
|
|
// If supported, revert the version of the node item
|
|
if ( this.Type === 'code_ref' && this.Value?.Value ) {
|
|
const Codium = this.models.get('api:Codium')
|
|
const code = await Codium.findOne({ UUID: this.Value?.Value, Active: true })
|
|
if ( code ) {
|
|
await code.revert_to_version(data.associated_type_version_num)
|
|
}
|
|
} else if ( this.Type === 'file_ref' && this.Value?.Value ) {
|
|
const FileGroup = this.models.get('api:FileGroup')
|
|
const group = await FileGroup.findOne({ UUID: this.Value?.Value })
|
|
if ( group ) {
|
|
await group.revert_to_version(data.associated_type_version_num)
|
|
}
|
|
} else if ( this.Type === 'database_ref' && this.Value?.Value ) {
|
|
const Database = this.models.get('api:db:Database')
|
|
const db = await Database.findOne({ Active: true, UUID: this.Value?.Value })
|
|
if ( db ) {
|
|
await db.revert_to_version(data.associated_type_version_num)
|
|
}
|
|
}
|
|
|
|
return reverted
|
|
}
|
|
}
|
|
|
|
module.exports = exports = Node;
|