1
|
|
|
import {ModelInterface, TableInterface} from "../../JeloquentInterfaces"; |
2
|
|
|
|
3
|
|
|
export default class Field { |
4
|
|
|
|
5
|
|
|
protected $fieldValue: unknown; |
6
|
|
|
|
7
|
|
|
protected $name: string; |
8
|
|
|
|
9
|
|
|
protected $originalValue: unknown; |
10
|
|
|
|
11
|
|
|
protected $parent: ModelInterface; |
12
|
|
|
|
13
|
|
|
protected $previousValue: unknown; |
14
|
|
|
|
15
|
|
|
private _isPrimary: boolean; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* |
19
|
|
|
* @param name |
20
|
|
|
* @param isPrimary |
21
|
|
|
*/ |
22
|
|
|
constructor(name: string, isPrimary = false) { |
23
|
|
|
this._isPrimary = isPrimary; |
24
|
|
|
this.$name = name; |
25
|
|
|
this.$fieldValue = null; |
26
|
|
|
this.$previousValue = undefined; |
27
|
|
|
this.$originalValue = undefined; |
28
|
|
|
this.$parent = null; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
get isDirty(): boolean { |
32
|
|
|
return this.$fieldValue != this.$previousValue; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
get isPrimary(): boolean { |
36
|
|
|
return this._isPrimary; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
get name(): string { |
40
|
|
|
return this.$name; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
get originalValue(): unknown { |
44
|
|
|
return this.$originalValue; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
get previousValue(): unknown { |
48
|
|
|
return this.$previousValue; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
get value(): unknown { |
52
|
|
|
return this.$fieldValue; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
set _value(value) { |
56
|
|
|
if (this.$originalValue === undefined) { |
57
|
|
|
this.$originalValue = JSON.parse(JSON.stringify(this.value ?? value)); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
this.$previousValue = JSON.parse(JSON.stringify(this.value)); |
61
|
|
|
this.$fieldValue = value; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
set value(value: unknown) { |
65
|
|
|
if (this.$previousValue === undefined) { |
66
|
|
|
this.$previousValue = JSON.parse(JSON.stringify(this.value ?? value)); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
if (this.$originalValue === undefined) { |
70
|
|
|
this.$originalValue = JSON.parse(JSON.stringify(this.value ?? value)); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
this.$previousValue = JSON.parse(JSON.stringify(this.value)); |
74
|
|
|
|
75
|
|
|
this.$fieldValue = value; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
resetDirty(): void { |
79
|
|
|
this.$originalValue = JSON.parse(JSON.stringify(this.$fieldValue)); |
80
|
|
|
this.$previousValue = JSON.parse(JSON.stringify(this.$fieldValue)); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
setName(): Field { |
84
|
|
|
return this; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
setup(parent: ModelInterface): Field { |
88
|
|
|
this.$parent = parent; |
89
|
|
|
return this.setName().setParentProperties(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
tableSetup(table: TableInterface): void { |
93
|
|
|
console.info(table.name); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
toJson(): object { |
97
|
|
|
const object = {}; |
98
|
|
|
object[this.$name] = this.value; |
99
|
|
|
return JSON.parse(JSON.stringify(object)); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
protected setParentProperties(): Field { |
103
|
|
|
return this; |
104
|
|
|
} |
105
|
|
|
} |