2020-11-02 18:31:05 +00:00
|
|
|
const VersionedModel = require('../VersionedModel')
|
|
|
|
const uuid = require('uuid/v4')
|
2020-02-08 09:54:48 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Node Model
|
|
|
|
* -------------------------------------------------------------
|
|
|
|
* Put some description here!
|
|
|
|
*/
|
2020-11-02 18:31:05 +00:00
|
|
|
class Node extends VersionedModel {
|
2020-03-01 21:37:52 +00:00
|
|
|
static get services() {
|
|
|
|
return [...super.services, 'models']
|
|
|
|
}
|
|
|
|
|
2020-02-08 09:54:48 +00:00
|
|
|
static get schema() {
|
|
|
|
// Return a flitter-orm schema here.
|
|
|
|
return {
|
2020-11-02 18:31:05 +00:00
|
|
|
...super.schema,
|
2020-02-08 12:57:38 +00:00
|
|
|
UUID: { type: String, default: () => uuid() },
|
2020-02-08 09:54:48 +00:00
|
|
|
Type: String,
|
2020-02-08 12:57:38 +00:00
|
|
|
Value: {
|
|
|
|
Mode: {type: String, default: 'norm'},
|
|
|
|
Value: String,
|
|
|
|
},
|
2020-11-17 04:49:19 +00:00
|
|
|
AdditionalData: Object,
|
2020-02-08 12:57:38 +00:00
|
|
|
PageId: String,
|
|
|
|
CreatedAt: { type: Date, default: () => new Date },
|
|
|
|
UpdatedAt: { type: Date, default: () => new Date },
|
|
|
|
CreatedUserId: String,
|
|
|
|
UpdateUserId: String
|
2020-02-08 09:54:48 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-03-16 15:35:57 +00:00
|
|
|
__initialize_version(base, data = {}) {
|
|
|
|
super.__initialize_version(base, data)
|
|
|
|
this.associated_type_version_num = data.associated_type_version_num
|
|
|
|
}
|
|
|
|
|
2020-02-08 09:54:48 +00:00
|
|
|
// Static and instance methods can go here
|
|
|
|
get page() {
|
2020-03-01 21:37:52 +00:00
|
|
|
const Page = this.models.get('api:Page')
|
2020-10-13 02:46:50 +00:00
|
|
|
return this.belongs_to_one(Page, "PageId", "UUID")
|
2020-02-08 09:54:48 +00:00
|
|
|
}
|
2020-02-08 17:16:04 +00:00
|
|
|
|
2020-02-08 18:47:03 +00:00
|
|
|
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>'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-08 17:16:04 +00:00
|
|
|
update_from_raw(data) {
|
|
|
|
if ( data.Type ) this.Type = data.Type
|
|
|
|
if ( data.Value ) this.Value = data.Value
|
2020-11-17 04:49:19 +00:00
|
|
|
if ( data.AdditionalData ) this.AdditionalData = data.AdditionalData;
|
2020-02-08 17:16:04 +00:00
|
|
|
}
|
2020-11-02 18:47:34 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2020-11-02 18:51:56 +00:00
|
|
|
} 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
|
|
|
|
}
|
2020-11-02 21:53:39 +00:00
|
|
|
} 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
|
|
|
|
}
|
2020-11-02 18:47:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return data
|
|
|
|
}
|
2020-11-06 18:42:52 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2020-02-08 09:54:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = exports = Node;
|