1 | /* |
---|
2 | +------------------------------------------------+ |
---|
3 | | @FILE: functions.js | |
---|
4 | | @AUTHOR: [NUTS] Rodrigo Souza | |
---|
5 | | @DATE: Sex 27 Out 2006 12:38:22 BRST | |
---|
6 | | @LAST CHANGE: Seg 27 Nov 2006 12:35:22 BRST:: | |
---|
7 | +------------------------------------------------+ |
---|
8 | */ |
---|
9 | |
---|
10 | /* |
---|
11 | * @DESC The method has one optional boolean argument |
---|
12 | * deep which when set recursively creates a |
---|
13 | * deep copy, otherwise (when deep is false |
---|
14 | * or not defined) a shallow copy is created. |
---|
15 | */ |
---|
16 | /*Object.prototype.clone = function(deep) |
---|
17 | { |
---|
18 | objectClone = new this.constructor(); |
---|
19 | for (var property in this) |
---|
20 | { |
---|
21 | if (!deep) |
---|
22 | objectClone[property] = this[property]; |
---|
23 | else if (typeof this[property] == 'object') |
---|
24 | objectClone[property] = this[property].clone(deep); |
---|
25 | else |
---|
26 | objectClone[property] = this[property]; |
---|
27 | } |
---|
28 | |
---|
29 | return objectClone; |
---|
30 | }*/ |
---|
31 | |
---|
32 | /* |
---|
33 | * |
---|
34 | */ |
---|
35 | var func = { |
---|
36 | "json" : false, |
---|
37 | "attachEvent" : function(obj, eventName, eventHandler) |
---|
38 | { |
---|
39 | if ( obj ) |
---|
40 | { |
---|
41 | if ( eventName.substring(0, 2) == 'on' ) |
---|
42 | { |
---|
43 | eventName = eventName.substring(2,eventName.length); |
---|
44 | } |
---|
45 | if ( obj.addEventListener ) |
---|
46 | { |
---|
47 | obj.addEventListener(eventName, eventHandler, false); |
---|
48 | } |
---|
49 | else if ( obj.attachEvent ) |
---|
50 | { |
---|
51 | obj.attachEvent('on'+eventName, eventHandler); |
---|
52 | } |
---|
53 | } |
---|
54 | }, |
---|
55 | |
---|
56 | "byId" : function() |
---|
57 | { |
---|
58 | var elements = new Array(); |
---|
59 | for ( var i = 0; i < arguments.length; i++ ) |
---|
60 | { |
---|
61 | element = arguments[i]; |
---|
62 | if ( this.isString(element) ) |
---|
63 | element = document.getElementById(element); |
---|
64 | |
---|
65 | if (arguments.length == 1) |
---|
66 | return element; |
---|
67 | |
---|
68 | elements.push(element); |
---|
69 | } |
---|
70 | return elements; |
---|
71 | }, |
---|
72 | |
---|
73 | "byTag" : function() |
---|
74 | { |
---|
75 | var elements = new Array(); |
---|
76 | for ( var i = 0; i < arguments.length; i++ ) |
---|
77 | { |
---|
78 | element = arguments[i]; |
---|
79 | if ( this.isString(element) ) |
---|
80 | element = document.getElementsByTagName(element); |
---|
81 | |
---|
82 | if ( arguments.length == 1 ) |
---|
83 | return element; |
---|
84 | |
---|
85 | elements.push(element); |
---|
86 | } |
---|
87 | return elements; |
---|
88 | }, |
---|
89 | |
---|
90 | "client_height" : function() |
---|
91 | { |
---|
92 | if( typeof( window.innerWidth ) == 'number' ) |
---|
93 | { |
---|
94 | //Non-IE |
---|
95 | return window.innerHeight; |
---|
96 | } |
---|
97 | else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) |
---|
98 | { |
---|
99 | //IE 6+ in 'standards compliant mode' |
---|
100 | return document.documentElement.clientHeight; |
---|
101 | } |
---|
102 | else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) |
---|
103 | { |
---|
104 | //IE 4 compatible |
---|
105 | return document.body.clientHeight; |
---|
106 | } |
---|
107 | }, |
---|
108 | |
---|
109 | "client_width" : function() |
---|
110 | { |
---|
111 | if( typeof( window.innerWidth ) == 'number' ) |
---|
112 | { |
---|
113 | //Non-IE |
---|
114 | return window.innerWidth; |
---|
115 | } |
---|
116 | else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) |
---|
117 | { |
---|
118 | //IE 6+ in 'standards compliant mode' |
---|
119 | return document.documentElement.clientWidth; |
---|
120 | } |
---|
121 | else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) |
---|
122 | { |
---|
123 | //IE 4 compatible |
---|
124 | return document.body.clientWidth; |
---|
125 | } |
---|
126 | }, |
---|
127 | |
---|
128 | "_confEl" : function(pElement, pAttribute, pValue) |
---|
129 | { |
---|
130 | if ( !func.isString(pValue) ) |
---|
131 | return false; |
---|
132 | |
---|
133 | if ( this.isString(pElement) ) |
---|
134 | { |
---|
135 | pElement = document.getElementById(pElement); |
---|
136 | } |
---|
137 | |
---|
138 | switch ( pAttribute ) |
---|
139 | { |
---|
140 | /* |
---|
141 | * No IE a propriedade style nÃo funciona se adicionada via setAttribute() |
---|
142 | */ |
---|
143 | case 'style' : |
---|
144 | pElement.style.cssText = pValue; |
---|
145 | break; |
---|
146 | case 'class' : |
---|
147 | pElement.setAttribute(pAttribute, pValue); |
---|
148 | pElement.setAttribute(pAttribute+'Name', pValue); // IE |
---|
149 | break; |
---|
150 | default : |
---|
151 | pElement.setAttribute(pAttribute, pValue); |
---|
152 | } /* switch */ |
---|
153 | }, |
---|
154 | |
---|
155 | "confEl" : function() |
---|
156 | { |
---|
157 | switch ( arguments.length ) |
---|
158 | { |
---|
159 | case 1 : |
---|
160 | matriz = arguments[0]; |
---|
161 | if ( !this.isArray(matriz) ) |
---|
162 | return false; |
---|
163 | |
---|
164 | for ( var i in matriz ) |
---|
165 | { |
---|
166 | pElement = matriz[i]['element']; |
---|
167 | delete matriz[i]['element']; |
---|
168 | |
---|
169 | for ( var j in matriz[i] ) |
---|
170 | { |
---|
171 | this._confEl(pElement, j, matriz[i][j]); |
---|
172 | } |
---|
173 | } |
---|
174 | break; |
---|
175 | case 2 : |
---|
176 | var pElement = arguments[0]; |
---|
177 | |
---|
178 | if ( !this.isArray(arguments[1]) ) |
---|
179 | return false; |
---|
180 | |
---|
181 | for ( var i in arguments[1] ) |
---|
182 | { |
---|
183 | this._confEl(pElement, i, arguments[1][i]); |
---|
184 | } |
---|
185 | break; |
---|
186 | case 3 : |
---|
187 | var pElement = arguments[0]; |
---|
188 | |
---|
189 | this._confEl(pElement, arguments[1], arguments[2]); |
---|
190 | break; |
---|
191 | } |
---|
192 | }, |
---|
193 | |
---|
194 | "elH" : function(pEl) |
---|
195 | { |
---|
196 | // no IE a funcao scrollHeight não retorna o valor correto |
---|
197 | return document.getElementById(pEl).offsetHeight; |
---|
198 | }, |
---|
199 | |
---|
200 | "elHW" : function(pEl) |
---|
201 | { |
---|
202 | aux = { |
---|
203 | h:this.elH(pEl), |
---|
204 | w:this.elW(pEl) |
---|
205 | }; |
---|
206 | return aux; |
---|
207 | }, |
---|
208 | |
---|
209 | "elW" : function(pEl) |
---|
210 | { |
---|
211 | return document.getElementById(pEl).offsetWidth; |
---|
212 | }, |
---|
213 | |
---|
214 | "interface" : function(_XmlDoc) |
---|
215 | { |
---|
216 | var data = _XmlDoc.getElementsByTagName('retorno').item(0); |
---|
217 | if ( data.childNodes.length == 1 ) |
---|
218 | data = data.firstChild.nodeValue; |
---|
219 | |
---|
220 | if ( /^("(\\.|[^"\\\n\r])*?"|[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t])+?$/.test(data) ) |
---|
221 | { |
---|
222 | eval('data = ' + data); |
---|
223 | if ( this.json ){ |
---|
224 | this.json = false; |
---|
225 | }else{ |
---|
226 | data = this.json2array(data); |
---|
227 | } |
---|
228 | } |
---|
229 | |
---|
230 | return data; |
---|
231 | |
---|
232 | }, |
---|
233 | |
---|
234 | "insEl" : function() |
---|
235 | { |
---|
236 | try |
---|
237 | { |
---|
238 | if ( arguments.length == 1 ) |
---|
239 | { |
---|
240 | var body = document.getElementsByTagName('body'); |
---|
241 | body[0].appendChild(arguments[0]); |
---|
242 | } |
---|
243 | else |
---|
244 | { |
---|
245 | var parentEl; |
---|
246 | switch ( typeof arguments[arguments.length-1] ) |
---|
247 | { |
---|
248 | case 'object' : |
---|
249 | parentEl = arguments[arguments.length-1]; |
---|
250 | break; |
---|
251 | case 'string' : |
---|
252 | parentEl = document.getElementById(arguments[arguments.length-1]); |
---|
253 | break; |
---|
254 | case 'boolean' : parentEl = document.getElementsByTagName('body').item(0); |
---|
255 | } |
---|
256 | |
---|
257 | for ( var i = 0; i < arguments.length-1; i++ ) |
---|
258 | { |
---|
259 | if ( this.isString(arguments[i]) ) |
---|
260 | arguments[i] = document.createTextNode(arguments[i]); |
---|
261 | |
---|
262 | parentEl.appendChild(arguments[i]); |
---|
263 | } |
---|
264 | } /* else */ |
---|
265 | return true; |
---|
266 | } |
---|
267 | catch(e) |
---|
268 | {} |
---|
269 | }, |
---|
270 | |
---|
271 | "insElB" : function() |
---|
272 | { |
---|
273 | try |
---|
274 | { |
---|
275 | if ( arguments.length < 2 ) |
---|
276 | { |
---|
277 | return false; |
---|
278 | } |
---|
279 | else |
---|
280 | { |
---|
281 | var referenceEl; |
---|
282 | var parentEl; |
---|
283 | switch ( typeof arguments[arguments.length-1] ) |
---|
284 | { |
---|
285 | case 'object' : |
---|
286 | parentEl = arguments[arguments.length-1].parentNode; |
---|
287 | referenceEl = arguments[arguments.length-1]; |
---|
288 | break; |
---|
289 | case 'string' : |
---|
290 | referenceEl = document.getElementById(arguments[arguments.length-1]); |
---|
291 | parentEl = referenceEl.parentNode; |
---|
292 | } |
---|
293 | |
---|
294 | for ( var i = 0; i < arguments.length-1; i++ ) |
---|
295 | { |
---|
296 | parentEl.insertBefore(arguments[i], referenceEl); |
---|
297 | } |
---|
298 | } /* else */ |
---|
299 | return true; |
---|
300 | } |
---|
301 | catch(e) |
---|
302 | {} |
---|
303 | }, |
---|
304 | |
---|
305 | "isArray" : function(a) |
---|
306 | { |
---|
307 | return this.isObject(a) && a.constructor == Array; |
---|
308 | }, |
---|
309 | |
---|
310 | "isFunction" : function(a) |
---|
311 | { |
---|
312 | return typeof a == 'function'; |
---|
313 | }, |
---|
314 | |
---|
315 | "isNumber" : function(a) |
---|
316 | { |
---|
317 | return typeof a == 'number' && isFinite(a); |
---|
318 | }, |
---|
319 | |
---|
320 | "isNull" : function(a) |
---|
321 | { |
---|
322 | return a === null; |
---|
323 | }, |
---|
324 | |
---|
325 | "isObject" : function(a) |
---|
326 | { |
---|
327 | return (a && typeof a == 'object') || this.isFunction(a); |
---|
328 | }, |
---|
329 | |
---|
330 | "isString" : function(a) |
---|
331 | { |
---|
332 | return typeof a == 'string'; |
---|
333 | }, |
---|
334 | |
---|
335 | "isUndefined" : function(a) |
---|
336 | { |
---|
337 | return typeof a == 'undefined'; |
---|
338 | }, |
---|
339 | |
---|
340 | "json2array" : function(pObject) |
---|
341 | { |
---|
342 | var retorno = []; |
---|
343 | for( var i in pObject ) |
---|
344 | if( pObject[i].constructor == Object ) |
---|
345 | { |
---|
346 | retorno[i] = []; |
---|
347 | retorno[i] = this.json2array(pObject[i]); |
---|
348 | } |
---|
349 | else |
---|
350 | retorno[i] = pObject[i]; |
---|
351 | return retorno; |
---|
352 | }, |
---|
353 | |
---|
354 | "newEl" : function(pElement) |
---|
355 | { |
---|
356 | if ( arguments.length == 1 ) |
---|
357 | { |
---|
358 | switch ( typeof pElement) |
---|
359 | { |
---|
360 | case 'string' : |
---|
361 | return document.createElement(pElement); |
---|
362 | break; |
---|
363 | case 'object' : |
---|
364 | var retorno = new Array(); |
---|
365 | if (this.isArray(pElement)) |
---|
366 | { |
---|
367 | for (var i in pElement) |
---|
368 | { |
---|
369 | retorno[i] = document.createElement(pElement[i]); |
---|
370 | } |
---|
371 | return retorno; |
---|
372 | } |
---|
373 | break; |
---|
374 | } /* switch */ |
---|
375 | } /* if */ |
---|
376 | else |
---|
377 | { |
---|
378 | var retorno = new Array(); |
---|
379 | for ( var i = 0; i < arguments.length; i++ ) |
---|
380 | { |
---|
381 | retorno[i] = document.createElement(arguments[i]); |
---|
382 | } |
---|
383 | return retorno; |
---|
384 | } |
---|
385 | }, |
---|
386 | |
---|
387 | "trim" : function(pString) |
---|
388 | { |
---|
389 | return pString.replace(/^( )*|( )*$/g, ''); |
---|
390 | } |
---|
391 | } |
---|