-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBase.js
129 lines (118 loc) · 3.15 KB
/
Base.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
/*
* Copyright (c) 2021 Lenze SE
* SPDX-License-Identifier: Apache-2.0
*/
class Base {
constructor(Base) {
this.isNull = this.isNull.bind(this);
this.isPrimitive = this.isPrimitive.bind(this);
this.isObject = this.isObject.bind(this);
this.isArray = this.isArray.bind(this);
this.elementExists = this.elementExists.bind(this);
this.convertToJSON = this.convertToJSON.bind(this);
this.hasParentType = this.hasParentType.bind(this);
this.hasInParentTreeType = this.hasInParentTreeType.bind(this);
this.getParentByType = this.getParentByType.bind(this);
}
getQueryVariable(variable) {
//var query = ;
var vars = window.location.search.substring(1).split("&");
for (var i = 0; i < vars.length; i++) {
var pair = vars[i].split("=");
if(pair[0] == variable)
return pair[1];
}
return null;
}
isNull(SubElement) {
if (SubElement == null)
return true;
return false;
}
isPrimitive(SubElement) {
return (SubElement !== Object(SubElement));
}
isObject(SubElement) {
return (SubElement === Object(SubElement));
}
isArray(SubElement) {
if (!this.isObject(SubElement))
return false;
return (Array.isArray(SubElement));
}
elementExists(JSON, elementName) {
if (JSON.hasOwnProperty(elementName) && (!this.isNull(JSON[elementName])))
return true;
return false;
}
convertToJSON(data, datatype) {
switch(datatype) {
default:
case "String":
case "string":
case "str":
return String(data);
break;
case "Integer":
case "integer":
case "int":
var parsed = parseInt(data, 10);
if (isNaN(parsed))
return 0;
return parsed;
break;
case "Double":
case "double":
case "Float":
case "float":
var parsed = parseFloat(data).toFixed(1);
if (isNaN(parsed))
return 0;
return parsed;
break;
case "Boolean":
case "boolean":
case "bool":
switch (data) {
default:
return false;
break;
case "1":
case "true":
case true:
case 1:
return true;
break;
case "0":
case "false":
case false:
case 0:
return false;
break;
}
return parseBoolean(data);
break;
}
}
hasParentType(obj, type) {
if (!this.elementExists(obj, "parentObj"))
return false;
if (obj.parentObj.tType === type)
return true;
return false;
}
hasInParentTreeType(obj, type) {
if (!this.elementExists(obj, "parentObj"))
return false;
if (this.hasParentType(obj, type))
return true;
return this.hasInParentTreeType(obj.parentObj, type);
}
getParentByType(obj, type) {
if (!this.elementExists(obj, "parentObj"))
return null;
if (this.hasParentType(obj, type))
return obj.parentObj;
return this.getParentByType(obj.parentObj, type);
}
}