/*
2* freeDOM (0.2.9)
3*
4* Mantenedores:
5* Andre Metzen (andre[at]metzen.com.br)
6* Leandro Vieira (leandro[at]w3invent.com.br)
7* 28/12/2006 - 10:37
8*
9* Quem contribuiu:
10* Elcio Ferreira (elcio[at]visie.com.br)
11* Diego Nunes (dnunes[at]gmail.com)
12* Lucas Ferreira (contato@lucasferreira.com)
13*
14* Funcoes disponiveis:
15* @name: $(strId[, strId2, strId3, ...])
16* @name: $tags(strTagName,objParentNode)
17* @name: $before(objNew,objRefer)
18* @name: $after(objNew,objRefer)
19* @name: $replace(objNew,objOld)
20* @name: $newTN(strConteudo)
21* @name: $new(strTagName, strParams, strConteudo)
22* @name: $append(objNode, objParentNode)
23* @name: $remove(objNode)
24* @name: getSrc(evt)
25* @name: cEvt(evt)
26* @name: JSON(t)
27* @name: addClass(objTarget, strClass)
28* @name: remEvent(o, evType, fn)
29* @name: addEvent(objTarget, strEvtType, fnFunction, useCapture, arrParams)
30* @name: getPosition(e)
31* @name: removeClass(objTarget, strClass)
32* @name: hasClass(objTarget, strClass)
33*
34*/
35
36/*
37* @name: $(strId[, strId2, strId3, ...])
38* @version: 1.0
39* @author: Andre Metzen
40* @param: strId => String, Array of String
41* @return: Node Object, Array of Node Objects
42* @description: Retorna o(s) elemento(s) cujo id e igual a "strId"
43*/
44function $(strId)
45{
46 var i, arrReturn,arrStrId;
47 if(arguments.length > 1)
48 {
49 arrStrId = new Array();
50 for(i=0; i<arguments.length; i++)
51 arrStrId.push(arguments[i]);
52 }
53
54 if(strId instanceof Array)
55 {
56 arrStrId = strId;
57 }
58
59 if(arrStrId instanceof Array)
60 {
61 arrReturn = new Array();
62 for(i=0; i<arrStrId.length; i++)
63 arrReturn[i] = document.getElementById(arrStrId[i]);
64 }
65 else
66 {
67 arrReturn = document.getElementById(strId);
68 }
69
70 return arrReturn;
71}
72
73/*
74* @name: $tags(strTagName,objParentNode)
75* @version: 1.0
76* @author: Andre Metzen
77* @param: strTagName => String
78* @param: objParentNode => Node Object(optional)
79* @return: Array of Node Objects
80* @description: Retorna todas as tags que possuam nodeName igual a "strTagName" dentro do objeto "objParentNode".
81* Caso "objParentNode" nao seja definido, e usado como padrao "document"
82*/
83function $tags(strTagName,objParentNode)
84{
85 if(typeof objParentNode == "undefined")
86 {
87 objParentNode = document;
88 }
89
90 return objParentNode.getElementsByTagName(strTagName);
91}
92
93/*
94* @name: $name(strTagName,objParentNode)
95* @version: 1.0
96* @author: Willian Fernandes
97* @param: strName => String
98* @param: objParentNode => Node Object(optional)
99* @return: Array of Node Objects
100* @description: Retorna todas as tags que possuam atributo name igual a "strTagName" dentro do objeto "objParentNode".
101* Caso "objParentNode" nao seja definido, e usado como padrao "document"
102*/
103function $name(strName, objParentNode)
104{
105 if (typeof objParentNode == "undefined")
106 {
107 objParentNode = document;
108 }
109
110 return objParentNode.getElementsByName(strName);
111}
112
113/*
114* @name: $before(objNew,objRefer)
115* @version: 1.0
116* @author: Andre Metzen
117* @param: objNew => Node Object
118* @param: objRefer => Node Object
119* @return: Node Object
120* @description: Insere o objeto "objNew" logo acima na arvore de nó do objeto "objRefer"
121*/
122function $before(objNew,objRefer)
123{
124 return objRefer.parentNode.insertBefore(objNew,objRefer);
125}
126
127/*
128* @name: $after(objNew,objRefer)
129* @version: 1.0
130* @author: Leandro Vieira
131* @param: objNew => Node Object
132* @param: objRefer => Node Object
133* @return: Node Object
134* @description: Insere o objeto "objNew" logo abaixo na arvore de nó do objeto "objRefer"
135*/
136function $after(objNew,objRefer)
137{
138 return objRefer.parentNode.insertBefore(objNew,objRefer.nextSibling);
139}
140
141/*
142* @name: $replace(objNew,objOld)
143* @version: 1.0
144* @author: Andre Metzen
145* @param: objNew => Node Object
146* @param: objOld => Node Object
147* @return: Node Object, false
148* @description: Substitue o objeto "objOld" pelo objeto "objNew"
149*/
150function $replace(objNew,objOld)
151{
152 if(objOld.parentNode)
153 {
154 return objOld.parentNode.replaceChild(objNew,objOld);
155 }
156 else
157 {
158 return false;
159 }
160}
161
162/*
163* @name: $newTN(strConteudo)
164* @version: 1.0
165* @author: Andre Metzen
166* @param: strConteudo => String
167* @return: Text Node Object, false
168* @description: Cria e retorna um text node com o conteudo passado em "strConteudo"
169*/
170function $newTN(strConteudo)
171{
172 if(typeof strConteudo == "string")
173 {
174 return document.createTextNode(strConteudo);
175 }
176 else
177 {
178 return false;
179 }
180}
181
182/*
183* @name: $new(strTagName, strParams, strConteudo)
184* @version: 1.0
185* @author: Diego Nunes, adaptado por Andre Metzen
186* @param: strTagName => String
187* @param: strParams => String, Array of strings; ex: "href=#" or ["href=#","target=_blank"]
188* @param: strConteudo => String, Array of objects or strings
189* @return: Node Object
190* @description: Cria um novo elemento do tipo "strTagName". O parametro "strParams" sao as propriedades que serao aplicadas ao objeto.
191* "strParams" pode ser uma string, caso seja apenas uma propriedade, ou um vetor, para n propriedades.
192* O parametro "strConteudo" e o conteudo. Pode ser passado como um vetor ou diretamente. Aceita-se como
193* valor objetos ou string.
194*/
195function $new(strTagName, strParams, strContent)
196{
197 var i, j, newElement, arrParameters;
198 var fixIEParams = ["name","enctype"];
199
200 if(typeof strContent == "undefined")
201 {
202 strContent = strParams;
203 strParams = null;
204 }
205
206 if(document.all && !window.opera)
207 {
208 strTagName = '<'+strTagName+' ';
209
210 if(strParams instanceof Array)
211 for(i=0; i<strParams.length && (arrParameters = strParams[i].split("=")); i++)
212 for(j=0; j<fixIEParams.length; j++)
213 {
214 if(arrParameters[0] == fixIEParams[j]) strTagName += arrParameters[0]+'="'+arrParameters[1]+'" ';
215 }
216 else
217 if(typeof strParams == "string" && ( arrParameters = strParams.split("=")) )
218 for(j=0; j<fixIEParams.length; j++) if(arrParameters[0] == fixIEParams[j]) strTagName += arrParameters[0]+'="'+arrParameters[1]+'" ';
219
220 strTagName += '>';
221 }
222
223 newElement=document.createElement(strTagName);
224
225 if(strParams instanceof Array)
226 {
227 for(i=0; i<strParams.length && (arrParameters = strParams[i].split("=")); i++)
228 {
229 newElement[arrParameters[0]] = arrParameters[1];
230 }
231 }
232 else
233 {
234 if(typeof strParams == "string" && ( arrParameters = strParams.split("=")) )
235 {
236 newElement[arrParameters[0]] = (arrParameters.length==2) ? arrParameters[1] : "";
237 }
238 }
239
240 if(strContent instanceof Array)
241 {
242 for(i=0; i<strContent.length; i++)
243 {
244 (typeof(strContent[i]) =="string") ? $append($newTN(strContent[i]), newElement) : $append(strContent[i], newElement);
245 }
246 }
247 else
248 {
249 $append(strContent, newElement);
250 }
251
252 return newElement;
253}
254
255/*
256* @name: $append(objNode, objParentNode)
257* @version: 1.0
258* @author: Andre Metzen
259* @param: objNode => Node Object, String, Array of Node Objects or Strings
260* @param: objParentNode => Node Object
261* @return: Integer
262* @description: Adiciona o "objNode" como ultimo no filho de "objParentNode". Se "objNode" for uma string
263* e criada um Text Node e adicionado como ultimo no. Caso "objParentNode" nao seja definido e tomado
264* como padrao "document.body"
265*/
266function $append(objNode, objParentNode)
267{
268 var i;
269 if(typeof(objParentNode) == "undefined")
270 {
271 objParentNode = document.body;
272 }
273
274 if(objNode=="" || objNode == null)
275 {
276 return true;
277 }
278 if(objNode instanceof Array)
279 {
280 for(i=0; i<objNode.length; i++)
281 {
282 $append(objNode[i], objParentNode);
283 }
284 }
285 else
286 {
287 if(typeof(objNode) == "string")
288 {
289 objParentNode.appendChild($newTN(objNode))
290 }
291 else
292 {
293 objParentNode.appendChild(objNode);
294 }
295 }
296
297 return objParentNode.childNodes.length;
298}
299
300/*
301* @name: $remove(objNode)
302* @version: 1.0
303* @author: Andre Metzen
304* @param: objNode => Node Object
305* @return: void
306* @description: Remove o elemento "objNode"
307*/
308function $remove(objNode)
309{
310 if(objNode && objNode.parentNode)
311 {
312 objNode.parentNode.removeChild(objNode);
313 }
314}
315
316/*
317* @name: getStc(evt)
318* @version: 1.0
319* @author: Elcio Ferreira
320* @param: evt => Event Object
321* @return: void
322* @description: Retorna o objeto no qual o evento foi realizado
323*/
324function getSrc(evt)
325{
326 if(typeof(evt) == "undefined")
327 {
328 evt=window.event;
329 }
330
331 var objSrc=evt.target?evt.target:evt.srcElement;
332
333 if(objSrc.nodeType==3)
334 {
335 objSrc=objSrc.parentNode;
336 }
337 return objSrc;
338}
339
340/*
341* @name: cEvt(evt)
342* @version: 1.0
343* @author: Diego Nunes
344* @param: evt => Event Object
345* @return: false
346* @description: Cancela o efeito do evento "evt"
347*/
348function cEvt(evt)
349{
350 try
351 {
352 evt.preventDefault();
353 }
354 catch(e)
355 {
356 event.returnValue=false;
357 }
358
359 return false;
360}
361
362/*
363* @name: JSON(t)
364* @version: 1.0
365* @author: Elcio Ferreira
366* @param: t => String
367* @return: mixed
368* @description: Interpreta a string no formato JSON
369*/
370function JSON(t)
371{
372 try
373 {
374 return eval("["+t+"]")[0];
375 }
376 catch(e)
377 {
378 throw("Eval Error on code:\n"+t);
379 }
380}
381
382
383/*
384* @name: addClass(objTarget, strClass)
385* @version: 1.0
386* @author: Diego Nunes
387* @param: objTarget => Node Object
388* @param: strClass => String
389* @return: String
390* @description: Adiciona a class strClass ao objeto objTarget caso ele não possua essa classe
391*/
392function addClass(objTarget, strClass)
393{
394 if(hasClass(objTarget, strClass))
395 {
396 return false;
397 }
398
399 return objTarget.className = ((objTarget.className) ? objTarget.className : '') +' '+ strClass;
400}
401
402/*
403* @name: hasClass(objTarget, strClass)
404* @version: 1.0
405* @author: Diego Nunes
406* @param: objTarget => Node Object
407* @param: strClass => String
408* @return: Boolean
409* @description: Verifica se o objTarget já possui a classe strClass
410*/
411function hasClass(objTarget, strClass)
412{
413 var n, i, tToks=objTarget.className.split(' ');
414 for (n=tToks.length; n--; )
415 {
416 if(tToks[n]==strClass)
417 {
418 return true;
419 }
420 }
421 return false;
422}
423
424/*
425* @name: removeClass(objTarget, strClass)
426* @version: 1.0
427* @author: Diego Nunes
428* @param: objTarget => Node Object
429* @param: strClass => String
430* @return: String
431* @description: Remove a classe strClass de objTarget, se ele possuir
432*/
433function removeClass(objTarget, strClass)
434{
435 if(!objTarget.className)
436 {
437 return '';
438 }
439
440 var n, i, tRs=[], tToks=objTarget.className.split(' ');
441 for(n=tToks.length, i=0; i<n; i++)
442 {
443 if(tToks[i]!=strClass)
444 {
445 tRs[tRs.length]=tToks[i];
446 }
447 }
448
449 return objTarget.className = (tRs.length ? tRs.join(' ') : '');
450}
451
452/*
453* @name: getPosition(e)
454* @version: 1.0
455* @author: Unknown
456* @param: e => Event Object
457* @return: Object
458* @description: Retorna um objeto possuido as propriedades x e y que representa as coordenadas do mouse no evento e
459*/
460function getPosition(e)
461{
462 e = e || window.event;
463 var cursor = {x:0, y:0};
464 if (e.pageX || e.pageY)
465 {
466 cursor.x = e.pageX;
467 cursor.y = e.pageY;
468 }
469 else
470 {
471 cursor.x = e.clientX + (document.documentElement.scrollLeft || document.body.scrollLeft) - document.documentElement.clientLeft;
472 cursor.y = e.clientY + (document.documentElement.scrollTop || document.body.scrollTop) - document.documentElement.clientTop;
473 }
474 return cursor;
475}
476
477/*
478* @name: addEvent(objTarget, strEvtType, fnFunction, useCapture, arrParams)
479* @version: 1.0
480* @author: Stuart Langridge (adaptado por Andre Metzen e Leandro Vieira)
481* @param: objTarget => Node Object
482* @param: strEvtType => String
483* @param: fnFunction => Function Object
484* @param: useCapture => Boolean
485* @param: arrParams => Array
486* @return: Boolean
487* @description:
488*/
489function addEvent(objTarget, strEvtType, fnFunction, useCapture, arrParams)
490{
491 if(!objTarget) return false;
492
493 var fnNewFunction;
494
495 if(typeof arrParams != "undefined")
496 {
497 /** Se for necessario passar varios argumentos em arrParams */
498 if ( typeof(arrParams) != 'string' )
499 {
500 var arrParamsToFunction = []; // Iniciamos um array que contera as mensagens de erro
501 for ( var i = 0; i < arrParams.length; i++ )
502 {
503 arrParamsToFunction.push(arrParams[i]);
504 }
505
506 fnNewFunction = function() { fnFunction.apply(objTarget,[arrParamsToFunction]); };
507 }
508 else
509 {
510 fnNewFunction = function() { fnFunction.apply(objTarget, [arrParams]); };
511 }
512 }
513 else
514 {
515 fnNewFunction = fnFunction;
516 }
517
518 if (objTarget.addEventListener)
519 {
520 objTarget.addEventListener(strEvtType, fnNewFunction, useCapture);
521 return true;
522 }
523 else
524 {
525 if (objTarget.attachEvent)
526 {
527 return objTarget.attachEvent('on' + strEvtType, fnNewFunction);
528 }
529 else
530 {
531 objTarget['on' + strEvtType] = fnNewFunction;
532 }
533 }
534};
535
536/*
537* @name: remEvent(o, evType, fn)
538* @version: 1.1
539* @author: Andre Metzen
540* @param: objTarget => Node Object
541* @param: strEvtType => String
542* @param: fnFunction => Function Object
543* @return: Boolean
544* @description:
545*/
546function remEvent(objTarget, strEvtType, fnFunction)
547{
548 if (document.detachEvent)
549 {
550 objTarget.detachEvent('on'+ strEvtType, fnFunction, true);
551 return true;
552 }
553
554 if (objTarget.removeEventListener)
555 {
556 objTarget.removeEventListener(strEvtType, fnFunction, true);
557 return true;
558 }
559}