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 | "attachEvent" : function(obj, eventName, eventHandler) |
---|
37 | { |
---|
38 | if ( obj ) |
---|
39 | { |
---|
40 | if ( eventName.substring(0, 2) == 'on' ) |
---|
41 | { |
---|
42 | eventName = eventName.substring(2,eventName.length); |
---|
43 | } |
---|
44 | if ( obj.addEventListener ) |
---|
45 | { |
---|
46 | obj.addEventListener(eventName, eventHandler, false); |
---|
47 | } |
---|
48 | else if ( obj.attachEvent ) |
---|
49 | { |
---|
50 | obj.attachEvent('on'+eventName, eventHandler); |
---|
51 | } |
---|
52 | } |
---|
53 | }, |
---|
54 | |
---|
55 | "byId" : function() |
---|
56 | { |
---|
57 | var elements = new Array(); |
---|
58 | for ( var i = 0; i < arguments.length; i++ ) |
---|
59 | { |
---|
60 | element = arguments[i]; |
---|
61 | if ( this.isString(element) ) |
---|
62 | element = document.getElementById(element); |
---|
63 | |
---|
64 | if (arguments.length == 1) |
---|
65 | return element; |
---|
66 | |
---|
67 | elements.push(element); |
---|
68 | } |
---|
69 | return elements; |
---|
70 | }, |
---|
71 | |
---|
72 | "byTag" : function() |
---|
73 | { |
---|
74 | var elements = new Array(); |
---|
75 | for ( var i = 0; i < arguments.length; i++ ) |
---|
76 | { |
---|
77 | element = arguments[i]; |
---|
78 | if ( this.isString(element) ) |
---|
79 | element = document.getElementsByTagName(element); |
---|
80 | |
---|
81 | if ( arguments.length == 1 ) |
---|
82 | return element; |
---|
83 | |
---|
84 | elements.push(element); |
---|
85 | } |
---|
86 | return elements; |
---|
87 | }, |
---|
88 | |
---|
89 | "client_height" : function() |
---|
90 | { |
---|
91 | if( typeof( window.innerWidth ) == 'number' ) |
---|
92 | { |
---|
93 | //Non-IE |
---|
94 | return window.innerHeight; |
---|
95 | } |
---|
96 | else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) |
---|
97 | { |
---|
98 | //IE 6+ in 'standards compliant mode' |
---|
99 | return document.documentElement.clientHeight; |
---|
100 | } |
---|
101 | else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) |
---|
102 | { |
---|
103 | //IE 4 compatible |
---|
104 | return document.body.clientHeight; |
---|
105 | } |
---|
106 | }, |
---|
107 | |
---|
108 | "client_width" : function() |
---|
109 | { |
---|
110 | if( typeof( window.innerWidth ) == 'number' ) |
---|
111 | { |
---|
112 | //Non-IE |
---|
113 | return window.innerWidth; |
---|
114 | } |
---|
115 | else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) |
---|
116 | { |
---|
117 | //IE 6+ in 'standards compliant mode' |
---|
118 | return document.documentElement.clientWidth; |
---|
119 | } |
---|
120 | else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) |
---|
121 | { |
---|
122 | //IE 4 compatible |
---|
123 | return document.body.clientWidth; |
---|
124 | } |
---|
125 | }, |
---|
126 | |
---|
127 | "_confEl" : function(pElement, pAttribute, pValue) |
---|
128 | { |
---|
129 | if ( !func.isString(pValue) ) |
---|
130 | return false; |
---|
131 | |
---|
132 | if ( this.isString(pElement) ) |
---|
133 | { |
---|
134 | pElement = document.getElementById(pElement); |
---|
135 | } |
---|
136 | |
---|
137 | switch ( pAttribute ) |
---|
138 | { |
---|
139 | /* |
---|
140 | * No IE a propriedade style nÃo funciona se adicionada via setAttribute() |
---|
141 | */ |
---|
142 | case 'style' : |
---|
143 | pElement.style.cssText = pValue; |
---|
144 | break; |
---|
145 | case 'class' : |
---|
146 | pElement.setAttribute(pAttribute, pValue); |
---|
147 | pElement.setAttribute(pAttribute+'Name', pValue); // IE |
---|
148 | break; |
---|
149 | default : |
---|
150 | pElement.setAttribute(pAttribute, pValue); |
---|
151 | } /* switch */ |
---|
152 | }, |
---|
153 | |
---|
154 | "confEl" : function() |
---|
155 | { |
---|
156 | switch ( arguments.length ) |
---|
157 | { |
---|
158 | case 1 : |
---|
159 | matriz = arguments[0]; |
---|
160 | if ( !this.isArray(matriz) ) |
---|
161 | return false; |
---|
162 | |
---|
163 | for ( var i in matriz ) |
---|
164 | { |
---|
165 | pElement = matriz[i]['element']; |
---|
166 | delete matriz[i]['element']; |
---|
167 | |
---|
168 | for ( var j in matriz[i] ) |
---|
169 | { |
---|
170 | this._confEl(pElement, j, matriz[i][j]); |
---|
171 | } |
---|
172 | } |
---|
173 | break; |
---|
174 | case 2 : |
---|
175 | var pElement = arguments[0]; |
---|
176 | |
---|
177 | if ( !this.isArray(arguments[1]) ) |
---|
178 | return false; |
---|
179 | |
---|
180 | for ( var i in arguments[1] ) |
---|
181 | { |
---|
182 | this._confEl(pElement, i, arguments[1][i]); |
---|
183 | } |
---|
184 | break; |
---|
185 | case 3 : |
---|
186 | var pElement = arguments[0]; |
---|
187 | |
---|
188 | this._confEl(pElement, arguments[1], arguments[2]); |
---|
189 | break; |
---|
190 | } |
---|
191 | }, |
---|
192 | |
---|
193 | "elH" : function(pEl) |
---|
194 | { |
---|
195 | // no IE a funcao scrollHeight não retorna o valor correto |
---|
196 | return document.getElementById(pEl).offsetHeight; |
---|
197 | }, |
---|
198 | |
---|
199 | "elHW" : function(pEl) |
---|
200 | { |
---|
201 | aux = { |
---|
202 | h:this.elH(pEl), |
---|
203 | w:this.elW(pEl) |
---|
204 | }; |
---|
205 | return aux; |
---|
206 | }, |
---|
207 | |
---|
208 | "elW" : function(pEl) |
---|
209 | { |
---|
210 | return document.getElementById(pEl).offsetWidth; |
---|
211 | }, |
---|
212 | |
---|
213 | "insEl" : function() |
---|
214 | { |
---|
215 | if ( arguments.length == 1 ) |
---|
216 | { |
---|
217 | var body = document.getElementsByTagName('body'); |
---|
218 | body[0].appendChild(arguments[0]); |
---|
219 | } |
---|
220 | else |
---|
221 | { |
---|
222 | var parentEl; |
---|
223 | switch ( typeof arguments[arguments.length-1] ) |
---|
224 | { |
---|
225 | case 'object' : |
---|
226 | parentEl = arguments[arguments.length-1]; |
---|
227 | break; |
---|
228 | case 'string' : |
---|
229 | parentEl = document.getElementById(arguments[arguments.length-1]); |
---|
230 | break; |
---|
231 | case 'boolean' : parentEl = document.getElementsByTagName('body').item(0); |
---|
232 | } |
---|
233 | |
---|
234 | for ( var i = 0; i < arguments.length-1; i++ ) |
---|
235 | { |
---|
236 | if ( this.isString(arguments[i]) ) |
---|
237 | arguments[i] = document.createTextNode(arguments[i]); |
---|
238 | |
---|
239 | parentEl.appendChild(arguments[i]); |
---|
240 | } |
---|
241 | } /* else */ |
---|
242 | return true; |
---|
243 | }, |
---|
244 | |
---|
245 | "insElB" : function() |
---|
246 | { |
---|
247 | if ( arguments.length < 2 ) |
---|
248 | { |
---|
249 | return false; |
---|
250 | } |
---|
251 | else |
---|
252 | { |
---|
253 | var referenceEl; |
---|
254 | var parentEl; |
---|
255 | switch ( typeof arguments[arguments.length-1] ) |
---|
256 | { |
---|
257 | case 'object' : |
---|
258 | parentEl = arguments[arguments.length-1].parentNode; |
---|
259 | referenceEl = arguments[arguments.length-1]; |
---|
260 | break; |
---|
261 | case 'string' : |
---|
262 | referenceEl = document.getElementById(arguments[arguments.length-1]); |
---|
263 | parentEl = referenceEl.parentNode; |
---|
264 | } |
---|
265 | |
---|
266 | for ( var i = 0; i < arguments.length-1; i++ ) |
---|
267 | { |
---|
268 | parentEl.insertBefore(arguments[i], referenceEl); |
---|
269 | } |
---|
270 | } /* else */ |
---|
271 | return true; |
---|
272 | }, |
---|
273 | |
---|
274 | "isArray" : function(a) |
---|
275 | { |
---|
276 | return this.isObject(a) && a.constructor == Array; |
---|
277 | }, |
---|
278 | |
---|
279 | "isFunction" : function(a) |
---|
280 | { |
---|
281 | return typeof a == 'function'; |
---|
282 | }, |
---|
283 | |
---|
284 | "isNumber" : function(a) |
---|
285 | { |
---|
286 | return typeof a == 'number' && isFinite(a); |
---|
287 | }, |
---|
288 | |
---|
289 | "isNull" : function(a) |
---|
290 | { |
---|
291 | return a === null; |
---|
292 | }, |
---|
293 | |
---|
294 | "isObject" : function(a) |
---|
295 | { |
---|
296 | return (a && typeof a == 'object') || this.isFunction(a); |
---|
297 | }, |
---|
298 | |
---|
299 | "isString" : function(a) |
---|
300 | { |
---|
301 | return typeof a == 'string'; |
---|
302 | }, |
---|
303 | |
---|
304 | "isUndefined" : function(a) |
---|
305 | { |
---|
306 | return typeof a == 'undefined'; |
---|
307 | }, |
---|
308 | |
---|
309 | "newEl" : function(pElement) |
---|
310 | { |
---|
311 | if ( arguments.length == 1 ) |
---|
312 | { |
---|
313 | switch ( typeof pElement) |
---|
314 | { |
---|
315 | case 'string' : |
---|
316 | return document.createElement(pElement); |
---|
317 | break; |
---|
318 | case 'object' : |
---|
319 | var retorno = new Array(); |
---|
320 | if (this.isArray(pElement)) |
---|
321 | { |
---|
322 | for (var i in pElement) |
---|
323 | { |
---|
324 | retorno[i] = document.createElement(pElement[i]); |
---|
325 | } |
---|
326 | return retorno; |
---|
327 | } |
---|
328 | break; |
---|
329 | } /* switch */ |
---|
330 | } /* if */ |
---|
331 | else |
---|
332 | { |
---|
333 | var retorno = new Array(); |
---|
334 | for ( var i = 0; i < arguments.length; i++ ) |
---|
335 | { |
---|
336 | retorno[i] = document.createElement(arguments[i]); |
---|
337 | } |
---|
338 | return retorno; |
---|
339 | } |
---|
340 | }, |
---|
341 | |
---|
342 | "trim" : function(pString) |
---|
343 | { |
---|
344 | return pString.replace(/^( )*|( )*$/g, ''); |
---|
345 | } |
---|
346 | } |
---|