Utils.js
6.42 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
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
// jquery utils
function buildId(id) {
return typeof(id) == 'string' && id.length > 0 && id.charAt(0) != '#' ? '#' + id : id;
}
// console utils
function safeLog(value) {
if(typeof(console) != 'undefined' && typeof(console.log) == 'function')
console.log(value);
}
// form utils
var formUtils = {
submit : function(form) {
if (form == undefined)
form = $j('form:first');
form.removeAttr('onsubmit');
if (validationUtils.validatesFields()) {
$j('form:first').find('#btn_enviar').attr('disabled', 'disabled').val('Aguarde...');
form.submit();
}
}
};
function fixupFieldsWidth(additionalFields, force){
if (! $j(document).data('fixed-fields-width') || force) {
var maxWidth = 0;
var $fields = $j('form select');
if (additionalFields)
$j.merge($fields, additionalFields);
//get maxWidh
$j.each($fields, function(index, field){
$field = $j(field);
if ($field.outerWidth() > maxWidth)
maxWidth = $field.outerWidth();
});
//set maxWidth
$j.each($fields, function(index, field){
$j(field).width(maxWidth);
});
$j(document).data('fixed-fields-width', true);
}
else
safeLog('fixupFieldsWidth already called, skipping!');
};
// options (hash) utils
var optionsUtils = {
get : function(options, optionName) {
var value = options[optionName];
if (typeof value == 'undefined')
throw new Error("Option '" + optionName + "' not defined in simpleSearchOptions.");
return value;
},
merge : function(defaultOptions, options) {
if (typeof options == 'undefined')
options = {};
return $j.extend({}, defaultOptions, options);
}
};
// key-value object (hash) utils
var objectUtils = {
length : function(hash) {
return Object.getOwnPropertyNames(hash).length;
},
join : function(object, glue, separator) {
if (glue == undefined)
glue = '=';
if (separator == undefined)
separator = ' ';
return $j.map(Object.getOwnPropertyNames(object), function(k) { return [k, object[k]].join(glue) }).join(separator)
}
};
// window utils
var windowUtils = {
// open a new window, for more information see developer.mozilla.org/en-US/docs/DOM/window.open
open : function(url, name, options) {
var defaultOptions = {
name : 'new_window',
options : {}
};
var defaultWindowOptions = {
top : 0,
left : 0,
height : $j(window).height(),
width : $j(window).width(),
resizable : 'yes',
scrollbars : 'yes',
menubar : 'no',
toolbar : 'no',
location : 'no',
personalbar : 'no',
status : 'no'
};
options = optionsUtils.merge(defaultOptions, options);
options.options = optionsUtils.merge(defaultWindowOptions, options.options);
window.open(url, options.name, objectUtils.join(options.options)).focus();
}
};
// string utils
var stringUtils = {
toUtf8 : function(s) {
try {
s = decodeURIComponent(escape(s));
}
catch(e) {
safeLog('Erro ao decodificar string utf8: ' + s);
}
return s;
}
};
// #TODO migrar restante funcoes string utils para hash stringUtils / migrar referencias a estas.
function safeToUpperCase(value) {
if (typeof(value) == 'string')
value = value.toUpperCase();
return value;
}
function safeToLowerCase(value) {
if (typeof(value) == 'string')
value = value.toLowerCase();
return value;
}
function safeCapitalize(value) {
if (typeof(value) == 'string') {
value = value.toLowerCase();
var words = value.split(' ');
$j.each(words, function(index, word){
words[index] = word.substr(0, 1).toUpperCase() + word.substr(1);
});
value = words.join(' ');
}
return value;
}
function safeSort(values) {
try{
var sortedValues = values.sort(function(a, b) {
if (typeof(a) == 'string' && typeof(b) == 'string')
var isGreaterThan = a.toLowerCase() > b.toLowerCase();
else
var isGreaterThan = a > b;
return isGreaterThan ? 1 : -1;
});
return sortedValues;
}
catch(e) {
safeLog('Erro ao ordenar valores: ' + e);
safeLog(values);
return values;
}
}
function safeUtf8Decode(s) {
return stringUtils.toUtf8(s);
}
// feedback messages
// #TODO migrar todas referencias de "handleMessages([{type*" para "messageUtils.<type>"
var messageUtils = {
error : function(msg, targetId) {
handleMessages([{type : 'error', msg : safeUtf8Decode(msg)}], targetId);
},
success : function(msg, targetId) {
handleMessages([{type : 'success', msg : safeUtf8Decode(msg)}], targetId);
},
notice : function(msg, targetId) {
handleMessages([{type : 'notice', msg : safeUtf8Decode(msg)}], targetId);
},
removeStyle : function(targetElementOrId, delay) {
// 30 seconds, by default
if (delay == undefined)
delay = 30000;
var $targetElement = $j(buildId(targetElementOrId));
window.setTimeout(function() {
$targetElement.removeClass('success').removeClass('error').removeClass('notice');
}, delay);
},
handleMessages : function(messages, targetElementOrId) {
var $feedbackMessages = $j('#feedback-messages');
var $targetElement = $j(buildId(targetElementOrId));
var messagesType = [];
for (var i = 0; i < messages.length; i++) {
var delay = messages[i].type == 'success' ? 5000 : 10000;
$j('<p />').hide()
.data('target_id', $targetElement.attr('id'))
.addClass(messages[i].type)
.html(stringUtils.toUtf8(messages[i].msg))
.appendTo($feedbackMessages)
.fadeIn().delay(delay).fadeOut(function() {
$j(this).remove()
});
if (messagesType.indexOf(messages[i].type < 0))
messagesType.push(messages[i].type);
}
if ($targetElement) {
$targetElement.removeClass('error').removeClass('success').removeClass('notice');
$targetElement.addClass(messagesType.join(' '));
messageUtils.removeStyle($targetElement);
if (messagesType.indexOf('error') > -1)
$targetElement.first().focus();
}
}
};
// backward compatibility
var handleMessages = messageUtils.handleMessages;
// when page is ready
(function($) {
$(document).ready(function() {
// add div for feedback messages
$j('<div />').attr('id', 'feedback-messages').appendTo($j('#corpo'));
}); // ready
})(jQuery);