3 Contains useful Element prototypes, to be used with the dollar function <$>.
6 <Moo.js>, <Function.js>, <Array.js>, <String.js>
9 Valerio Proietti, <http://mad4milk.net>
15 - Some functions are inspired by those found in prototype.js <http://prototype.conio.net/> (c) 2005 Sam Stephenson sam [at] conio [dot] net, MIT-style license
20 Custom class to allow all of its methods to be used with any DOM element via the dollar function <$>.
23 var Element = new Class({
27 Creates a new element of the type passed in.
30 el - the tag name for the element you wish to create.
33 >var div = new Element('div');
36 initialize: function(el){
37 if ($type(el) == 'string') el = document.createElement(el);
41 inject: function(el, where){
42 el = $(el) || new Element(el);
44 case "before": $(el.parentNode).insertBefore(this, el); break;
46 if (!el.getNext()) $(el.parentNode).appendChild(this);
47 else $(el.parentNode).insertBefore(this, el.getNext());
49 case "inside": el.appendChild(this); break;
55 Property: injectBefore
56 Inserts the Element before the passed element.
59 el - a string representing the element to be injected in (myElementId, or div), or an element reference.
60 If you pass div or another tag, the element will be created.
64 ><div id="myElement"></div>
65 ><div id="mySecondElement"></div>
67 >$('mySecondElement').injectBefore('myElement');
69 ><div id="myElement"></div>
70 ><div id="mySecondElement"></div>
74 injectBefore: function(el){
75 return this.inject(el, 'before');
80 Same as <Element.injectBefore>, but inserts the element after.
83 injectAfter: function(el){
84 return this.inject(el, 'after');
88 Property: injectInside
89 Same as <Element.injectBefore>, but inserts the element inside.
92 injectInside: function(el){
93 return this.inject(el, 'inside');
98 Inserts the passed element inside the Element. Works as <Element.injectInside> but in reverse.
101 el - a string representing the element to be injected in (myElementId, or div), or an element reference.
102 If you pass div or another tag, the element will be created.
106 this.appendChild($(el) || new Element(el));
112 Removes the Element from the DOM.
115 >$('myElement').remove() //bye bye
119 this.parentNode.removeChild(this);
124 Clones the Element and returns the cloned one.
130 >var clone = $('myElement').clone().injectAfter('myElement');
131 >//clones the Element and append the clone after the Element.
134 clone: function(contents){
135 return $(this.cloneNode(contents || true));
139 Property: replaceWith
140 Replaces the Element with an element passed.
143 el - a string representing the element to be injected in (myElementId, or div), or an element reference.
144 If you pass div or another tag, the element will be created.
147 the passed in element
150 >$('myOldElement').replaceWith($('myNewElement')); //$('myOldElement') is gone, and $('myNewElement') is in its place.
153 replaceWith: function(el){
154 var el = $(el) || new Element(el);
155 this.parentNode.replaceChild(el, this);
161 Appends text node to a DOM element.
164 text - the text to append.
167 ><div id="myElement">hey</div>
168 >$('myElement').appendText(' howdy'); //myElement innerHTML is now "hey howdy"
171 appendText: function(text){
172 if (this.getTag() == 'style' && window.ActiveXObject) this.styleSheet.cssText = text;
173 else this.appendChild(document.createTextNode(text));
179 Tests the Element to see if it has the passed in className.
182 true - the Element has the class
186 className - the class name to test.
189 ><div id="myElement" class="testClass"></div>
190 >$('myElement').hasClass('testClass'); //returns true
193 hasClass: function(className){
194 return !!this.className.test("\\b"+className+"\\b");
199 Adds the passed in class to the Element, if the element doesnt already have it.
202 className - the class name to add
205 ><div id="myElement" class="testClass"></div>
206 >$('myElement').addClass('newClass'); //<div id="myElement" class="testClass newClass"></div>
209 addClass: function(className){
210 if (!this.hasClass(className)) this.className = (this.className+' '+className.trim()).clean();
215 Property: removeClass
216 works like <Element.addClass>, but removes the class from the element.
219 removeClass: function(className){
220 if (this.hasClass(className)) this.className = this.className.replace(className.trim(), '').clean();
225 Property: toggleClass
226 Adds or removes the passed in class name to the element, depending on if it's present or not.
229 className - the class to add or remove
232 ><div id="myElement" class="myClass"></div>
233 >$('myElement').toggleClass('myClass');
234 ><div id="myElement" class=""></div>
235 >$('myElement').toggleClass('myClass');
236 ><div id="myElement" class="myClass"></div>
239 toggleClass: function(className){
240 if (this.hasClass(className)) return this.removeClass(className);
241 else return this.addClass(className);
246 Sets a css property to the Element.
249 property - the property to set
250 value - the value to which to set it
253 >$('myElement').setStyle('width', '300px'); //the width is now 300px
256 setStyle: function(property, value){
257 if (property == 'opacity') this.setOpacity(parseFloat(value));
258 else this.style[property.camelCase()] = value;
264 Applies a collection of styles to the Element.
267 source - an object or string containing all the styles to apply
270 >$('myElement').setStyles({
271 > border: '1px solid #000',
278 >$('myElement').setStyle('border: 1px solid #000; width: 300px; height: 400px;');
281 setStyles: function(source){
282 if ($type(source) == 'object') {
283 for (var property in source) this.setStyle(property, source[property]);
284 } else if ($type(source) == 'string') {
285 if (window.ActiveXObject) this.cssText = source;
286 else this.setAttribute('style', source);
293 Sets the opacity of the Element, and sets also visibility == "hidden" if opacity == 0, and visibility = "visible" if opacity == 1.
296 opacity - Accepts numbers from 0 to 1.
299 >$('myElement').setOpacity(0.5) //make it 50% transparent
302 setOpacity: function(opacity){
304 if(this.style.visibility != "hidden") this.style.visibility = "hidden";
306 if(this.style.visibility != "visible") this.style.visibility = "visible";
308 if (window.ActiveXObject) this.style.filter = "alpha(opacity=" + opacity*100 + ")";
309 this.style.opacity = opacity;
315 Returns the style of the Element given the property passed in.
318 property - the css style property you want to retrieve
321 >$('myElement').getStyle('width'); //returns "400px"
322 >//but you can also use
323 >$('myElement').getStyle('width').toInt(); //returns "400"
326 the style as a string
329 getStyle: function(property){
330 var proPerty = property.camelCase();
331 var style = this.style[proPerty] || false;
333 if (document.defaultView) style = document.defaultView.getComputedStyle(this,null).getPropertyValue(property);
334 else if (this.currentStyle) style = this.currentStyle[proPerty];
336 if (style && ['color', 'backgroundColor', 'borderColor'].test(proPerty) && style.test('rgb')) style = style.rgbToHex();
342 Attaches an event listener to a DOM element.
345 action - the event to monitor ('click', 'load', etc)
346 fn - the function to execute
349 >$('myElement').addEvent('click', function(){alert('clicked!')});
352 addEvent: function(action, fn){
353 this[action+fn] = fn.bind(this);
354 if (this.addEventListener) this.addEventListener(action, fn, false);
355 else this.attachEvent('on'+action, this[action+fn]);
357 if (this != window) Unload.functions.push(function(){
358 el.removeEvent(action, fn);
359 el[action+fn] = null;
365 Property: removeEvent
366 Works as Element.addEvent, but instead removes the previously added event listener.
369 removeEvent: function(action, fn){
370 if (this.removeEventListener) this.removeEventListener(action, fn, false);
371 else this.detachEvent('on'+action, this[action+fn]);
375 getBrother: function(what){
376 var el = this[what+'Sibling'];
377 while ($type(el) == 'textnode') el = el[what+'Sibling'];
382 Property: getPrevious
383 Returns the previousSibling of the Element, excluding text nodes.
386 >$('myElement').getPrevious(); //get the previous DOM element from myElement
389 the sibling element or undefined if none found.
392 getPrevious: function(){
393 return this.getBrother('previous');
398 Works as Element.getPrevious, but tries to find the nextSibling.
402 return this.getBrother('next');
407 Works as <Element.getPrevious>, but tries to find the firstChild.
410 getFirst: function(){
411 var el = this.firstChild;
412 while ($type(el) == 'textnode') el = el.nextSibling;
418 Works as <Element.getPrevious>, but tries to find the lastChild.
422 var el = this.lastChild;
423 while ($type(el) == 'textnode')
424 el = el.previousSibling;
429 Property: setProperty
430 Sets an attribute for the Element.
433 property - the property to assign the value passed in
434 value - the value to assign to the property passed in
437 >$('myImage').setProperty('src', 'whatever.gif'); //myImage now points to whatever.gif for its source
440 setProperty: function(property, value){
443 case 'class': this.className = value; break;
444 case 'style': this.setStyles(value); break;
445 case 'name': if (window.ActiveXObject && this.getTag() == 'input'){
446 el = $(document.createElement('<input name="'+value+'" />'));
447 $A(this.attributes).each(function(attribute){
448 if (attribute.name != 'name') el.setProperty(attribute.name, attribute.value);
451 if (this.parentNode) this.replaceWith(el);
453 default: this.setAttribute(property, value);
459 Property: setProperties
460 Sets numerous attributes for the Element.
463 source - an object with key/value pairs.
466 >$('myElement').setProperties({
467 > src: 'whatever.gif',
468 > alt: 'whatever dude'
470 ><img src="whatever.gif" alt="whatever dude">
473 setProperties: function(source){
474 for (var property in source) this.setProperty(property, source[property]);
480 Sets the innerHTML of the Element.
483 html - the new innerHTML for the element.
486 >$('myElement').setHTML(newHTML) //the innerHTML of myElement is now = newHTML
489 setHTML: function(html){
490 this.innerHTML = html;
495 Property: getProperty
496 Gets the an attribute of the Element.
499 property - the attribute to retrieve
502 >$('myImage').getProperty('src') // returns whatever.gif
505 the value, or an empty string
508 getProperty: function(property){
509 return this.getAttribute(property);
514 Returns the tagName of the element in lower case.
517 >$('myImage').getTag() // returns 'img'
520 The tag name in lower case
524 return this.tagName.toLowerCase();
527 getOffset: function(what){
528 what = what.capitalize();
532 offset += el['offset'+what] || 0;
533 el = el.offsetParent;
540 Returns the distance from the top of the window to the Element.
544 return this.getOffset('top');
549 Returns the distance from the left of the window to the Element.
553 return this.getOffset('left');
558 Returns the value of the Element, if its tag is textarea, select or input.
561 getValue: function(){
563 switch(this.getTag()){
564 case 'select': value = this.getElementsByTagName('option')[this.selectedIndex].value; break;
565 case 'input': if ( (this.checked && ['checkbox', 'radio'].test(this.type)) || (['hidden', 'text', 'password'].test(this.type)) )
566 value = this.value; break;
567 case 'textarea': value = this.value;
574 new Object.Native(Element);
577 hasClassName: Element.prototype.hasClass,
578 addClassName: Element.prototype.addClass,
579 removeClassName: Element.prototype.removeClass,
580 toggleClassName: Element.prototype.toggleClass
583 /* Section: Utility Functions */
587 Applies a method with the passed in args to the passed in element. Useful if you dont want to extend the element
591 method - a string representing the Element Class method to execute on that element
592 args - an array representing the arguments to pass to that method
595 >$Element(el, 'hasClass', className) //true or false
598 function $Element(el, method, args){
599 if ($type(args) != 'array') args = [args];
600 return Element.prototype[method].apply(el, args);
605 returns the element passed in with all the Element prototypes applied.
608 el - a reference to an actual element or a string representing the id of an element
611 >$('myElement') // gets a DOM element by id with all the Element prototypes applied.
612 >var div = document.getElementById('myElement');
613 >$(div) //returns an Element also with all the mootools extentions applied.
615 You'll use this when you aren't sure if a variable is an actual element or an id, as
616 well as just shorthand for document.getElementById().
619 a DOM element or false (if no id was found)
622 you need to call $ on an element only once to get all the prototypes.
623 But its no harm to call it multiple times, as it will detect if it has been already extended.
627 if ($type(el) == 'string') el = document.getElementById(el);
628 if ($type(el) == 'element'){
630 Unload.elements.push(el);
631 el.extend = Object.extend;
632 el.extend(Element.prototype);
638 window.addEvent = document.addEvent = Element.prototype.addEvent;
639 window.removeEvent = document.removeEvent = Element.prototype.removeEvent;
643 elements: [], functions: [], vars: [],
646 Unload.functions.each(function(fn){
650 window.removeEvent('unload', window.removeFunction);
652 Unload.elements.each(function(el){
653 for(var p in Element.prototype){
664 window.removeFunction = Unload.unload;
665 window.addEvent('unload', window.removeFunction);