-
Notifications
You must be signed in to change notification settings - Fork 0
/
lowercase.js
84 lines (84 loc) · 2.51 KB
/
lowercase.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
JFCustomWidgetUtils.domReady(function () {
class LowerCase {
params;
input = document.querySelector("#formatted");
messageEl = document.querySelector("#message");
fieldObj;
tarVal;
constructor() {
this.params = JFCustomWidget.getWidgetSettings();
this.params.srcId
? (this.params.srcId = this.params.srcId.substring(1))
: this.showError("Field ID must be specified.");
this.updateValue();
JFCustomWidget.listenFromField(this.params.srcId, "change", (res) => {
this.updateValue();
});
}
updateValue = async () => {
this.fieldObj = await this.getFields();
if (this.fieldObj.data[0].type) {
switch (this.params.format) {
case "Lowercase":
this.tarVal = this.fieldObj.data[0].value.toLowerCase();
break;
case "Uppercase":
this.tarVal = this.fieldObj.data[0].value.toUpperCase();
break;
case "Proper Case":
this.tarVal = this.toTitleCase(this.fieldObj.data[0].value);
break;
}
this.params.trim == "Yes" && (this.tarVal = this.tarVal.trim());
this.input.value = this.tarVal;
JFCustomWidget.sendData({
value: this.tarVal,
});
return this.tarVal;
} else {
this.showError("Invalid source field ID.");
}
};
getFields = () => {
return new Promise((resolve, reject) => {
JFCustomWidget.getFieldsValueById(
[this.params.srcId.split("_")[1] || "error"],
(n) => resolve(n)
);
});
};
toTitleCase(str) {
return str.replace(/\w\S*/g, function (txt) {
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
});
}
resizeWidget() {
var height = document.getElementsByTagName("body")[0].clientHeight || 94;
if (JFCustomWidget.isFromCardform() && height < 94) {
height = 94;
}
JFCustomWidget.requestFrameResize({
height: height,
});
}
showError(error) {
this.input.style.display = "none";
this.messageEl.innerHTML = error;
}
getData() {
return {
valid: !!this.tarVal,
value: this.tarVal,
};
}
}
JFCustomWidget.subscribe("ready", function (data) {
var widget = new LowerCase(data);
JFCustomWidget.subscribe("submit", function () {
JFCustomWidget.sendSubmit(widget.getData());
});
JFCustomWidget.subscribe("show", function () {
widget.resizeWidget();
});
});
});