generated from surplex/repository-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathext2mui.js
executable file
·172 lines (162 loc) · 4.87 KB
/
ext2mui.js
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
export default async function (namespaces, customColumnMapperStr) {
// we cannot pass 'customColumnMapper' as a function, so we re-create the function from a string
const customColumnMapper = Function("col", "result", "'use strict';return (" + customColumnMapperStr + ")")()
let visitedObjects = []
let visitedClasses = []
let results = {}
const isObject = obj => obj === Object(obj)
const mapColumnDef = (col) => {
const result = {}
result.field = col.dataIndex
if (!col.filter) {
result.filterable = false
}
if (col.hasOwnProperty("flex")) {
result.flex = col.flex
}
if (col.hasOwnProperty("width")) {
result.width = col.width
}
if (col.hasOwnProperty("text")) {
result.headerName = col.text
}
if (col.hasOwnProperty("menuDisabled")) {
result.disableColumnMenu = col.menuDisabled
}
if (col.hasOwnProperty("sortable")) {
result.sortable = col.sortable
}
if (col.hasOwnProperty("hidden")) {
result.hide = col.hidden
}
if (col.hasOwnProperty("tooltip")) {
result.description = col.tooltip
}
if (col.hasOwnProperty("resizable")) {
result.resizable = col.resizable
}
if (col.hasOwnProperty("align")) {
if (col.align === "start") {
result.align = "left"
}
if (col.align === "center") {
result.align = "center"
}
if (col.align === "end") {
result.align = "right"
}
}
if (col.hasOwnProperty("renderer")) {
result.renderCell = col.renderer.toString()
}
if (col.hasOwnProperty("tdCls")) {
result.cellClassName = col.tdCls
}
customColumnMapper(col, result)
return result
}
const mapGridDef = (def) => {
if (!def.columns || !Array.isArray(def.columns)) {
return false
}
let copy = []
// flatten grouped columns because there is no simple mapping possible
def.columns.forEach((col, i) => {
if(!col.hasOwnProperty("columns")){
copy.push(mapColumnDef(col))
}else{
copy = copy.concat(mapGridDef(col))
}
})
return copy
}
const extractGridItem = (item, currentClass) => {
if (item.xtype && item.xtype.includes("grid")) {
const def = mapGridDef(item)
if (def) {
const gridName = item.reference || item.itemId || "unknown"
results[currentClass + ":" + gridName] = def
}
} else if (isObject(item)) {
extractGrids(item, currentClass)
}
}
const extractGrids = (root, currentClass) => {
if (visitedObjects.includes(root) || !root || !isObject(root)) {
return
}
visitedObjects.push(root)
for (const key of Object.keys(root)) {
if (!root[key] || key === "self" || key === "superclass") {
continue
}
if (key === "$className") {
if (visitedClasses.includes(root.$className)) {
return
}
visitedClasses.push(root.$className)
currentClass = root.$className
if (root.prototype) {
extractGrids(root.prototype, currentClass)
}
} else if (key === "initConfig" && typeof (root.initConfig === "function")) {
root.$calledInitConfig = true
root.bindings = {}
try {
root.initConfig({})
} catch (e) {
console.log("Error initializing " + currentClass)
}
if (root.initialConfig) {
extractGrids(root.initialConfig, currentClass)
}
} else if (key === "items") {
if (Array.isArray(root.items)) {
root.items.forEach(i => extractGridItem(i, currentClass))
} else {
extractGridItem(root.items, currentClass)
}
} else if (isObject(root[key])) {
extractGrids(root[key], currentClass)
}
}
}
const timeout = 10000
const promisesNS = []
promisesNS.push(new Promise((resolve, reject) => {
let start = Date.now()
let handle = window.setInterval(() => {
if (Date.now() >= (start + timeout)) {
reject(`Timeout waiting for ExtJs`)
}
if (window.Ext && window.Ext.isReady) {
window.clearInterval(handle)
// prevent any automatic request when initializing components
window.Ext.Ajax.on("beforeRequest", () => false)
resolve()
}
}, 100)
}))
namespaces.forEach(ns => promisesNS.push(new Promise((resolve, reject) => {
let start = Date.now()
let handle = window.setInterval(() => {
if (Date.now() >= (start + timeout)) {
reject(`Timeout waiting for Namespace: ${ns}`)
}
if (window[ns]) {
window.clearInterval(handle)
resolve()
}
}, 100)
})))
return new Promise((resolve, reject) => {
try {
Promise.all(promisesNS).then(() => {
namespaces.forEach(ns => extractGrids(window[ns], "root"))
resolve(JSON.stringify(results))
}).catch(reject)
} catch (e) {
console.error(e)
}
})
}