forked from BasicPrimitives/javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDropDownBox.js
More file actions
122 lines (115 loc) · 3.2 KB
/
DropDownBox.js
File metadata and controls
122 lines (115 loc) · 3.2 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
import { ControlType } from './enums';
export function DropDownBoxConfig(id, defaultItem, caption, items, valueType, onUpdate) {
this.controlType = ControlType.DropDownBox;
this.id = id;
this.defaultItem = defaultItem;
this.caption = caption;
this.items = items;
this.valueType = valueType;
this.onUpdate = onUpdate;
};
export function DropDownBoxRender() {
this.render = function (config, namespace, defaultItem) {
var controlBody = ["p",
{
"title": config.id,
"$": function (element) {
element.addEventListener('change', function () {
config.onUpdate(element, config);
});
}
},
config.caption,
":",
'\xa0'
];
var controlList = ["select",
{
"class": "form-select",
"aria-label": config.caption,
"id": namespace + config.id
}
];
var key, value;
controlBody.push(controlList);
if (Array.isArray(config.items)) {
var hasItem = false;
if (defaultItem == null) {
controlList.push(["option",
{
"value": '-1',
"selected": "selected"
},
"NULL"
]);
hasItem = true;
}
for (var index = 0, len = config.items.length; index < len; index += 1) {
value = config.items[index];
var properties = {
"value": (value == "NULL" ? -1 : value)
};
if (value == defaultItem) {
properties["selected"] = "selected";
}
controlList.push(["option",
properties,
value.toString()
]);
if (value == defaultItem) {
hasItem = true;
}
}
if (!hasItem) {
controlList.push(["option",
{
"value": defaultItem,
"selected": "selected"
},
defaultItem.toString()
]);
}
} else {
if (defaultItem == null) {
controlList.push(["option",
{
"value": '-1',
"selected": "selected"
},
"NULL"
]);
}
for (key in config.items) {
if (config.items.hasOwnProperty(key)) {
value = config.items[key];
var properties = {
"value": (value == "NULL" ? -1 : value)
};
if (value == defaultItem) {
properties["selected"] = "selected";
}
controlList.push(["option",
properties,
primitives.splitCamelCaseName(key).join(" ")
]);
}
}
}
return controlBody;
};
this.getValue = function (item, namespace, formatters) {
var result;
var formatter = formatters[item.valueType],
element = document.getElementById(namespace + item.id);
if (element.selectedIndex == -1) {
result = null;
} else {
var value = element.options[element.selectedIndex].value;
result = formatter(value);
};
if (result == -1) {
result = null;
}
return result;
};
};