-
Notifications
You must be signed in to change notification settings - Fork 4
/
domUtil.js
203 lines (156 loc) · 6.09 KB
/
domUtil.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// Copyright 2023 to WebRocketX under the
// GNU LESSER GENERAL PUBLIC LICENSE Version 2.1
// See license file for more details
// version 1.10.1 07/16/2023
function DomUtil(){
var me = this;
me.hideElement = hideElement;
me.showElement = showElement;
me.elementVisible = elementVisible;
me.getElement = getElement;
me.getChildElement = getChildElement;
me.getChildElementsByType = getChildElementsByType;
me.getFirstChildElement = getFirstChildElement;
me.setText = setText;
me.getText = getText;
me.verifyElementExists = verifyElementExists;
me.removeAllChildElements = removeAllChildElements;
me.removeFirstChildElement = removeFirstChildElement;
me.appendChildElement = appendChildElement;
// native call is more powerful than jquery in this case
function getElement( element ){
var elementObject = null;
if ( typeof element == 'string' ) {
var elementObject = document.getElementById(element);
if(elementObject == null){
elementObject = document.getElementsByName(element);
if(elementObject.length == 0){
return null;
}
else {
elementObject = elementObject[0];
}
}
}
else {
elementObject = element;
}
if (elementObject == undefined) {
return null;
}
return elementObject;
}
// searches deeper than immediate children
function getChildElement( myparent, idName){
// if object is already found
if ( typeof idName != 'string' ) {
return idName;
}
var childElement = null;
var parentObj = me.verifyElementExists(myparent);
var childElements = $(parentObj).find("*");
for (i=0;i<childElements.length;i++){
if((childElements[i].id==idName) || (childElements[i].name==idName)){
childElement =childElements[i];
break;
}
}
if (childElement == undefined) {
return null;
}
return childElement;
}
function getFirstChildElement(myparent){
var parentObject = me.verifyElementExists(myparent);
return $(parentObject).children()[0];
}
function setText( element, contents )
{
var elementObject = me.verifyElementExists(element);
if( contents == null ) {
$(elementObject).text("");
}
else {
if ((elementObject.tagName=="INPUT")||(elementObject.tagName=="TEXTAREA")) {
elementObject.value=contents;
}
else {
$(elementObject).text(contents);
}
}
}
function getText( element ){
var elementObject = me.verifyElementExists(element);
return $(elementObject).text();
}
// searches deeper than immediate children
function getChildElementsByType(myparent, elementType) {
var parentObject = me.verifyElementExists(myparent);
return $(parentObject).find(elementType);
}
function elementVisible( element ){
var elementObject = me.verifyElementExists(element);
return $(elementObject).is(':visible');
}
function hideElement( element ){
var elementObject = me.verifyElementExists(element);
$(elementObject).hide();
}
function showElement( element, displayType ){
//displayType is an optional string
//valid values are: default, element, inline, and list-item
var showStyle = "element";
if ( displayType != undefined ) {
showStyle = displayType;
}
var elementObject = me.verifyElementExists(element);
if (showStyle == "element") {
$(elementObject).show();
}
else {
$(elementObject).css("display",showStyle);
}
}
function removeAllChildElements(elementName) {
var elementObject = me.getElement(elementName);
if (elementObject != null) {
$(elementObject).empty();
}
}
function removeFirstChildElement(parentElement) {
var parentElementObject = me.getElement(parentElement);
//removes specified child and also returns handle to child element
var childObj = me.getFirstChildElement(parentElementObject);
if (childObj != null) {
// use native call here instead of jquery
// because jquery destroys data attribues in its remove operation
try {
parentElementObject.removeChild(childObj);
}
catch (e) {
alert(e);
}
}
return childObj;
}
function appendChildElement(parentElement, childElement) {
var parentObj = me.getElement(parentElement);
var childObj = me.getElement(childElement);
if ((parentObj != null) && (childObj != null)) {
$(parentObj).append(childObj);
}
}
function verifyElementExists(element) {
if (element==null) {
alert("Null value passed to jElementController.verifyElementExists()");
return element;
}
var elementObject = me.getElement(element);
if (elementObject==null) {
alert(element+" not found.");
return elementObject;
}
return elementObject;
}
}
var domUtil = new DomUtil();