DynamicInput.js
3.31 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
function updateSelect($targetElement, options, emptyOptionHtml) {
$targetElement.children().not('[value=""]').remove();
var groups = new Array();
var optgroup = null;
$j.each(options, function(index, value){
if ($j(value).data('group')) {
if (groups.indexOf($j(value).data('group')) == -1) {
if (optgroup != null) {
optgroup.appendTo($targetElement);
}
optgroup = $j('<optgroup />').attr('label', $j(value).data('group'));
groups.push($j(value).data('group'));
}
$j(value).appendTo(optgroup);
} else {
$j(value).appendTo($targetElement);
}
});
if (optgroup != null) {
optgroup.appendTo($targetElement);
}
if (options.length > 0) {
$targetElement.removeAttr('disabled');
$targetElement.children('[value=""]').first().html(emptyOptionHtml || "Selecione uma opção");
}
else
$targetElement.children(':first').html('Sem opções');
}
function resetSelect($targetElement) {
$targetElement.children().not('[value=""]').remove();
$targetElement.children().first().attr('checked', 'checked');
//$targetElement.attr('disabled', 'disabled');
}
function xmlResourcesToSelectOptions(resources, parentNodeName, nodeIdAttrName, nodeValueAttrName) {
var options = [];
$j.each($j(resources).find(parentNodeName).children(), function(index, value){
var $value = $j(value);
var text;
var $option = $j('<option />');
$option.attr('value', $value.attr(nodeIdAttrName));
if (typeof nodeValueAttrName != 'undefined')
text = safeCapitalize($value.attr(nodeValueAttrName));
else
text = safeCapitalize($value.text());
$option.html(text);
options.push($option);
});
return options;
}
function jsonResourcesToSelectOptions(resources) {
var options = [];
$j.each(resources, function(id, value) {
// como arrays com chave numerica são ordenados pela chave pode-se enviar
// arrays como { __123 : 'value a', __111 : 'value b'} com a chave iniciando com '__'
// para que seja respeitado a posição dos elementos da lista e não pela chave
// assim o '__' do inicio do id será removido antes de usa-lo.
if (id.indexOf && id.substr && id.indexOf('__') == 0)
id = id.substr(2);
var opt = $j('<option />').attr('value', id);
var newValue = value;
if (typeof(value) == 'object') {
$j.each(value, function(optId, optValue) {
if (optId != 'value') {
opt.data(optId, optValue);
} else {
newValue = optValue;
}
});
}
opt.html(safeCapitalize(newValue));
options.push(opt);
});
return options;
}
function getFirstDefined(attrIdNames) {
if (! $j.isArray(attrIdNames))
attrIdNames = [attrIdNames];
var $element = undefined;
$j.each(attrIdNames, function(index, attIdName){
$element = $j(buildId(attIdName));
if ($element.length > 0)
return false;
});
return $element;
}
function getElementFor(entityName) {
var $element = getFirstDefined([entityName + "_id" ,
"ref_cod_" + entityName,
"ref_ref_cod_" + entityName,
entityName]);
if ($element == undefined)
safeLog("Elemento não definido para '" + entityName + "'");
return $element;
}