-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathUIBDefaultComponent.java
More file actions
153 lines (125 loc) · 6.06 KB
/
UIBDefaultComponent.java
File metadata and controls
153 lines (125 loc) · 6.06 KB
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
package solutions.bellatrix.servicenow.components.uiBuilder;
import solutions.bellatrix.servicenow.components.enums.UibComponentType;
import solutions.bellatrix.servicenow.components.serviceNow.SnComponent;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.function.BooleanSupplier;
import java.util.function.Supplier;
import lombok.SneakyThrows;
import solutions.bellatrix.core.plugins.EventListener;
import solutions.bellatrix.core.utilities.SingletonFactory;
import solutions.bellatrix.web.components.ComponentActionEventArgs;
import solutions.bellatrix.web.components.Span;
import solutions.bellatrix.web.components.TextInput;
import solutions.bellatrix.web.components.WebComponent;
import solutions.bellatrix.web.components.contracts.ComponentText;
import solutions.bellatrix.web.components.shadowdom.ShadowRoot;
import solutions.bellatrix.web.services.ComponentCreateService;
import solutions.bellatrix.web.validations.ComponentValidator;
@SuppressWarnings("unused")
public abstract class UIBDefaultComponent extends SnComponent implements ComponentText {
public final static EventListener<ComponentActionEventArgs> SETTING_VALUE = new EventListener<>();
public final static EventListener<ComponentActionEventArgs> VALUE_SET = new EventListener<>();
public String getValue() {
return this.defaultGetValue();
}
public UIBDefaultComponent textInput() {
var shadow = create().by(ShadowRoot.class, this.getFindStrategy()).getShadowRoot();
return shadow.createByXPath(UIBDefaultComponent.class, ".//input");
}
protected String formControlXpathLocator() {
return ".//*[contains(concat(' ',normalize-space(@class),' '),' form-control ')]";
}
protected ShadowRoot getDropDownWrapper() {
return create().byCss(ShadowRoot.class, "seismic-hoist");
}
protected boolean isDropDownOpen() {
return create().allByCss(ShadowRoot.class, "seismic-hoist").stream().findFirst().isPresent();
}
protected void invokeValueSetEvent(String text) {
VALUE_SET.broadcast(new ComponentActionEventArgs(this, text));
}
protected Class<?> getSetTextParamClass() {
return String.class;
}
public abstract void setText(String text);
public boolean isRequired() {
return getAttribute("required") != null;
}
@SneakyThrows
public void validateIsRequired(boolean isRequired) {
try {
Method method;
if (isRequired) {
method = ComponentValidator.class.getDeclaredMethod("defaultValidateAttributeTrue", WebComponent.class, BooleanSupplier.class, String.class);
} else {
method = ComponentValidator.class.getDeclaredMethod("defaultValidateAttributeFalse", WebComponent.class, BooleanSupplier.class, String.class);
}
method.invoke(SingletonFactory.getInstance(ComponentValidator.class), this, (BooleanSupplier) this::isRequired, "required");
} catch (InvocationTargetException e) {
throw e.getCause();
}
}
public String getLabel() {
// TODO: adjust locator
var xpathLocator = ".//label//span[contains(concat(' ',normalize-space(@class),' '),' label-text ')]";
return this.createByXPath(Span.class, xpathLocator).getText();
}
@SneakyThrows
public void validateLabel(String label) {
try {
Method method = ComponentValidator.class.getDeclaredMethod("defaultValidateAttributeIs", WebComponent.class, Supplier.class, String.class, String.class);
method.invoke(SingletonFactory.getInstance(ComponentValidator.class), this, (Supplier<String>) this::getLabel, label, String.format("label \u001B[35m%s\u001B[0m", label));
} catch (InvocationTargetException e) {
throw e.getCause();
}
}
public boolean isDisabled() {
boolean isDisabled = checkFormControlContainsClassDisabled();
if (checkFormControlContainsAttributeReadonly()) {
isDisabled = true;
}
return isDisabled;
}
public Boolean checkFormControlContainsClassDisabled() {
return formControl().getHtmlClass().contains("disabled");
}
public Boolean checkFormControlContainsAttributeReadonly() {
var readonlyAttribute = formControl().getAttribute("readonly");
return readonlyAttribute != null;
}
@SneakyThrows
public void validateIsDisabled(boolean isDisabled) {
try {
Method method;
if (isDisabled) {
method = ComponentValidator.class.getDeclaredMethod("defaultValidateAttributeTrue", WebComponent.class, BooleanSupplier.class, String.class);
} else {
method = ComponentValidator.class.getDeclaredMethod("defaultValidateAttributeFalse", WebComponent.class, BooleanSupplier.class, String.class);
}
method.invoke(SingletonFactory.getInstance(ComponentValidator.class), this, (BooleanSupplier) this::isDisabled, "disabled");
} catch (InvocationTargetException e) {
throw e.getCause();
}
}
public String getType() {
var xpathLocator = ".//div[@data-type='label']";
return this.createByXPath(Span.class, xpathLocator).getWrappedElement().getAttribute("type");
}
public abstract UibComponentType componentType();
@SneakyThrows
public void validateType() {
try {
Method method = ComponentValidator.class.getDeclaredMethod("defaultValidateAttributeIs", WebComponent.class, Supplier.class, String.class, String.class);
method.invoke(SingletonFactory.getInstance(ComponentValidator.class), this, (Supplier<String>) this::getType, componentType().toString(), String.format("type \u001B[35m%s\u001B[0m", componentType()));
} catch (InvocationTargetException e) {
throw e.getCause();
}
}
public TextInput formControl() {
return this.createByXPath(TextInput.class, formControlXpathLocator());
}
protected ComponentCreateService create() {
return new ComponentCreateService();
}
}