-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
inputs-legacy.ts
144 lines (120 loc) · 3.99 KB
/
inputs-legacy.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import { GenericInput } from '../types/inputs'
/**
* XML API Inputs - Legacy!
*/
export default class InputsLegacy {
/**
* Map single input (generic)
* Legacy method
*
* @param {Element} input
* @param {string | string[]} wantedAttributes
* @returns {BaseInputType}
*/
static mapSingle(input: Element, wantedAttributes: string | string[] = '*'): GenericInput {
const output: { [key: string]: any } = {}
// Map all base attributes of input
for (const attrName in input.attributes) {
const attr: Attr = input.attributes[attrName]
// Guard attribute not having name, being a function or nodeValue being a function
if (!attr.name
|| typeof attr.name !== 'string'
|| typeof attr === 'function'
|| attr.value === 'function'
) {
continue
}
// Only add the attribute to the output object if it is mandatory or desired by user
if (
['key', 'type'].includes(attr.name)
|| wantedAttributes === '*'
|| wantedAttributes.includes(attr.name)
) {
output[attr.name] = attr.value
}
}
switch (output.type) {
// If input is a title
case 'GT':
case 'Xaml':
// Guard - Attach fields only if "field" attributes is desired
if (wantedAttributes !== '*' && !wantedAttributes.includes('fields')) {
break
}
output.fields = []
// A regular .forEach doesn't work on this somehow...
for (const i in input.childNodes) {
const titleFieldEl: Element = input.childNodes[i] as Element
// Guard child node does not have node name and the node is not of correct type
if (!titleFieldEl.nodeName || !['image', 'text'].includes(titleFieldEl.nodeName)) {
continue
}
const nameAttr = titleFieldEl.attributes.getNamedItem('name')
// Guard child node does not have a name attribute
if (!nameAttr) {
continue
}
// Build fields of object from its attributes
const titleField: { [key: string]: any } = {
type: titleFieldEl.nodeName,
name: nameAttr.value,
value: (titleFieldEl as Node).textContent?.trim()
}
output.fields.push(titleField)
}
break
case 'Replay':
// Replay input - extract child node info
const replayChildNode = input.childNodes[1]! as Element
output.replay = {}
for (const attrName in replayChildNode.attributes) {
const attr: Attr = replayChildNode.attributes[attrName]
// Guard attribute not having name, being a function or nodeValue being a function
if (!attr.name
|| typeof attr.name !== 'string'
|| typeof attr === 'function'
|| attr.value === 'function'
) {
continue
}
// console.log('ADD REPLAY ATTR', attr.name, attr.value)
output.replay[attr.name] = attr.value
}
// A regular .forEach doesn't work on this somehow...
for (const i in replayChildNode.childNodes) {
const replayEl: Node = replayChildNode.childNodes[i]
// Guard entry does not have node name and the node is not of correct type
if (
!replayEl.nodeName
|| !['timecode', 'timecodeA', 'timecodeB'].includes(replayEl.nodeName)
) {
continue
}
// console.log('REPLAY CHILD NODE', replayEl.nodeName, replayEl.textContent)
output.replay[replayEl.nodeName] = replayEl.textContent?.trim()
}
break
default:
break
}
return output
}
/**
* Map inputs (generic)
* Legacy method
*
* @param {Element[]} xmlInputs
* @param wantedAttributes
* @returns {GenericInput[]}
*/
static mapGeneric(xmlInputs: Element[], wantedAttributes: string | string[] = '*'): GenericInput[] {
// Map all data from raw input
const xmlInputsMapped = xmlInputs.map(input => InputsLegacy.mapSingle(input, wantedAttributes))
// Make a dictionary and populate it
const inputsDictionary: any = {}
xmlInputsMapped.forEach((input: any) => {
inputsDictionary[input.key] = input
})
return inputsDictionary
}
}