mootools1.1.js 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409
  1. /*
  2. Script: Core.js
  3. Mootools - My Object Oriented javascript.
  4. License:
  5. MIT-style license.
  6. MooTools Copyright:
  7. copyright (c) 2007 Valerio Proietti, <http://mad4milk.net>
  8. MooTools Credits:
  9. - Class is slightly based on Base.js <http://dean.edwards.name/weblog/2006/03/base/> (c) 2006 Dean Edwards, License <http://creativecommons.org/licenses/LGPL/2.1/>
  10. - 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
  11. - Documentation by Aaron Newton (aaron.newton [at] cnet [dot] com) and Valerio Proietti.
  12. */
  13. var MooTools = {
  14. version: '1.11'
  15. };
  16. /* Section: Core Functions */
  17. /*
  18. Function: $defined
  19. Returns true if the passed in value/object is defined, that means is not null or undefined.
  20. Arguments:
  21. obj - object to inspect
  22. */
  23. function $defined(obj){
  24. return (obj != undefined);
  25. };
  26. /*
  27. Function: $type
  28. Returns the type of object that matches the element passed in.
  29. Arguments:
  30. obj - the object to inspect.
  31. Example:
  32. >var myString = 'hello';
  33. >$type(myString); //returns "string"
  34. Returns:
  35. 'element' - if obj is a DOM element node
  36. 'textnode' - if obj is a DOM text node
  37. 'whitespace' - if obj is a DOM whitespace node
  38. 'arguments' - if obj is an arguments object
  39. 'object' - if obj is an object
  40. 'string' - if obj is a string
  41. 'number' - if obj is a number
  42. 'boolean' - if obj is a boolean
  43. 'function' - if obj is a function
  44. 'regexp' - if obj is a regular expression
  45. 'class' - if obj is a Class. (created with new Class, or the extend of another class).
  46. 'collection' - if obj is a native htmlelements collection, such as childNodes, getElementsByTagName .. etc.
  47. false - (boolean) if the object is not defined or none of the above.
  48. */
  49. function $type(obj){
  50. if (!$defined(obj)) return false;
  51. if (obj.htmlElement) return 'element';
  52. var type = typeof obj;
  53. if (type == 'object' && obj.nodeName){
  54. switch(obj.nodeType){
  55. case 1: return 'element';
  56. case 3: return (/\S/).test(obj.nodeValue) ? 'textnode' : 'whitespace';
  57. }
  58. }
  59. if (type == 'object' || type == 'function'){
  60. switch(obj.constructor){
  61. case Array: return 'array';
  62. case RegExp: return 'regexp';
  63. case Class: return 'class';
  64. }
  65. if (typeof obj.length == 'number'){
  66. if (obj.item) return 'collection';
  67. if (obj.callee) return 'arguments';
  68. }
  69. }
  70. return type;
  71. };
  72. /*
  73. Function: $merge
  74. merges a number of objects recursively without referencing them or their sub-objects.
  75. Arguments:
  76. any number of objects.
  77. Example:
  78. >var mergedObj = $merge(obj1, obj2, obj3);
  79. >//obj1, obj2, and obj3 are unaltered
  80. */
  81. function $merge(){
  82. var mix = {};
  83. for (var i = 0; i < arguments.length; i++){
  84. for (var property in arguments[i]){
  85. var ap = arguments[i][property];
  86. var mp = mix[property];
  87. if (mp && $type(ap) == 'object' && $type(mp) == 'object') mix[property] = $merge(mp, ap);
  88. else mix[property] = ap;
  89. }
  90. }
  91. return mix;
  92. };
  93. /*
  94. Function: $extend
  95. Copies all the properties from the second passed object to the first passed Object.
  96. If you do myWhatever.extend = $extend the first parameter will become myWhatever, and your extend function will only need one parameter.
  97. Example:
  98. (start code)
  99. var firstOb = {
  100. 'name': 'John',
  101. 'lastName': 'Doe'
  102. };
  103. var secondOb = {
  104. 'age': '20',
  105. 'sex': 'male',
  106. 'lastName': 'Dorian'
  107. };
  108. $extend(firstOb, secondOb);
  109. //firstOb will become:
  110. {
  111. 'name': 'John',
  112. 'lastName': 'Dorian',
  113. 'age': '20',
  114. 'sex': 'male'
  115. };
  116. (end)
  117. Returns:
  118. The first object, extended.
  119. */
  120. var $extend = function(){
  121. var args = arguments;
  122. if (!args[1]) args = [this, args[0]];
  123. for (var property in args[1]) args[0][property] = args[1][property];
  124. return args[0];
  125. };
  126. /*
  127. Function: $native
  128. Will add a .extend method to the objects passed as a parameter, but the property passed in will be copied to the object's prototype only if non previously existent.
  129. Its handy if you dont want the .extend method of an object to overwrite existing methods.
  130. Used automatically in MooTools to implement Array/String/Function/Number methods to browser that dont support them whitout manual checking.
  131. Arguments:
  132. a number of classes/native javascript objects
  133. */
  134. var $native = function(){
  135. for (var i = 0, l = arguments.length; i < l; i++){
  136. arguments[i].extend = function(props){
  137. for (var prop in props){
  138. if (!this.prototype[prop]) this.prototype[prop] = props[prop];
  139. if (!this[prop]) this[prop] = $native.generic(prop);
  140. }
  141. };
  142. }
  143. };
  144. $native.generic = function(prop){
  145. return function(bind){
  146. return this.prototype[prop].apply(bind, Array.prototype.slice.call(arguments, 1));
  147. };
  148. };
  149. $native(Function, Array, String, Number);
  150. /*
  151. Function: $chk
  152. Returns true if the passed in value/object exists or is 0, otherwise returns false.
  153. Useful to accept zeroes.
  154. Arguments:
  155. obj - object to inspect
  156. */
  157. function $chk(obj){
  158. return !!(obj || obj === 0);
  159. };
  160. /*
  161. Function: $pick
  162. Returns the first object if defined, otherwise returns the second.
  163. Arguments:
  164. obj - object to test
  165. picked - the default to return
  166. Example:
  167. (start code)
  168. function say(msg){
  169. alert($pick(msg, 'no meessage supplied'));
  170. }
  171. (end)
  172. */
  173. function $pick(obj, picked){
  174. return $defined(obj) ? obj : picked;
  175. };
  176. /*
  177. Function: $random
  178. Returns a random integer number between the two passed in values.
  179. Arguments:
  180. min - integer, the minimum value (inclusive).
  181. max - integer, the maximum value (inclusive).
  182. Returns:
  183. a random integer between min and max.
  184. */
  185. function $random(min, max){
  186. return Math.floor(Math.random() * (max - min + 1) + min);
  187. };
  188. /*
  189. Function: $time
  190. Returns the current timestamp
  191. Returns:
  192. a timestamp integer.
  193. */
  194. function $time(){
  195. return new Date().getTime();
  196. };
  197. /*
  198. Function: $clear
  199. clears a timeout or an Interval.
  200. Returns:
  201. null
  202. Arguments:
  203. timer - the setInterval or setTimeout to clear.
  204. Example:
  205. >var myTimer = myFunction.delay(5000); //wait 5 seconds and execute my function.
  206. >myTimer = $clear(myTimer); //nevermind
  207. See also:
  208. <Function.delay>, <Function.periodical>
  209. */
  210. function $clear(timer){
  211. clearTimeout(timer);
  212. clearInterval(timer);
  213. return null;
  214. };
  215. /*
  216. Class: Abstract
  217. Abstract class, to be used as singleton. Will add .extend to any object
  218. Arguments:
  219. an object
  220. Returns:
  221. the object with an .extend property, equivalent to <$extend>.
  222. */
  223. var Abstract = function(obj){
  224. obj = obj || {};
  225. obj.extend = $extend;
  226. return obj;
  227. };
  228. //window, document
  229. var Window = new Abstract(window);
  230. var Document = new Abstract(document);
  231. document.head = document.getElementsByTagName('head')[0];
  232. /*
  233. Class: window
  234. Some properties are attached to the window object by the browser detection.
  235. Note:
  236. browser detection is entirely object-based. We dont sniff.
  237. Properties:
  238. window.ie - will be set to true if the current browser is internet explorer (any).
  239. window.ie6 - will be set to true if the current browser is internet explorer 6.
  240. window.ie7 - will be set to true if the current browser is internet explorer 7.
  241. window.gecko - will be set to true if the current browser is Mozilla/Gecko.
  242. window.webkit - will be set to true if the current browser is Safari/Konqueror.
  243. window.webkit419 - will be set to true if the current browser is Safari2 / webkit till version 419.
  244. window.webkit420 - will be set to true if the current browser is Safari3 (Webkit SVN Build) / webkit over version 419.
  245. window.opera - is set to true by opera itself.
  246. */
  247. window.xpath = !!(document.evaluate);
  248. if (window.ActiveXObject) window.ie = window[window.XMLHttpRequest ? 'ie7' : 'ie6'] = true;
  249. else if (document.childNodes && !document.all && !navigator.taintEnabled) window.webkit = window[window.xpath ? 'webkit420' : 'webkit419'] = true;
  250. else if (document.getBoxObjectFor !== null) window.gecko = true;
  251. /*compatibility*/
  252. window.khtml = window.webkit;
  253. Object.extend = $extend;
  254. /*end compatibility*/
  255. //htmlelement
  256. if (typeof HTMLElement == 'undefined'){
  257. var HTMLElement = function(){};
  258. if (window.webkit) document.createElement("iframe"); //fixes safari
  259. HTMLElement.prototype = (window.webkit) ? window["[[DOMElement.prototype]]"] : {};
  260. };
  261. HTMLElement.prototype.htmlElement = function(){};
  262. //enables background image cache for internet explorer 6
  263. if (window.ie6) try {document.execCommand("BackgroundImageCache", false, true);} catch(e){};
  264. /*
  265. Script: Class.js
  266. Contains the Class Function, aims to ease the creation of reusable Classes.
  267. License:
  268. MIT-style license.
  269. */
  270. /*
  271. Class: Class
  272. The base class object of the <http://mootools.net> framework.
  273. Creates a new class, its initialize method will fire upon class instantiation.
  274. Initialize wont fire on instantiation when you pass *null*.
  275. Arguments:
  276. properties - the collection of properties that apply to the class.
  277. Example:
  278. (start code)
  279. var Cat = new Class({
  280. initialize: function(name){
  281. this.name = name;
  282. }
  283. });
  284. var myCat = new Cat('Micia');
  285. alert(myCat.name); //alerts 'Micia'
  286. (end)
  287. */
  288. var Class = function(properties){
  289. var klass = function(){
  290. return (arguments[0] !== null && this.initialize && $type(this.initialize) == 'function') ? this.initialize.apply(this, arguments) : this;
  291. };
  292. $extend(klass, this);
  293. klass.prototype = properties;
  294. klass.constructor = Class;
  295. return klass;
  296. };
  297. /*
  298. Property: empty
  299. Returns an empty function
  300. */
  301. Class.empty = function(){};
  302. Class.prototype = {
  303. /*
  304. Property: extend
  305. Returns the copy of the Class extended with the passed in properties.
  306. Arguments:
  307. properties - the properties to add to the base class in this new Class.
  308. Example:
  309. (start code)
  310. var Animal = new Class({
  311. initialize: function(age){
  312. this.age = age;
  313. }
  314. });
  315. var Cat = Animal.extend({
  316. initialize: function(name, age){
  317. this.parent(age); //will call the previous initialize;
  318. this.name = name;
  319. }
  320. });
  321. var myCat = new Cat('Micia', 20);
  322. alert(myCat.name); //alerts 'Micia'
  323. alert(myCat.age); //alerts 20
  324. (end)
  325. */
  326. extend: function(properties){
  327. var proto = new this(null);
  328. for (var property in properties){
  329. var pp = proto[property];
  330. proto[property] = Class.Merge(pp, properties[property]);
  331. }
  332. return new Class(proto);
  333. },
  334. /*
  335. Property: implement
  336. Implements the passed in properties to the base Class prototypes, altering the base class, unlike <Class.extend>.
  337. Arguments:
  338. properties - the properties to add to the base class.
  339. Example:
  340. (start code)
  341. var Animal = new Class({
  342. initialize: function(age){
  343. this.age = age;
  344. }
  345. });
  346. Animal.implement({
  347. setName: function(name){
  348. this.name = name
  349. }
  350. });
  351. var myAnimal = new Animal(20);
  352. myAnimal.setName('Micia');
  353. alert(myAnimal.name); //alerts 'Micia'
  354. (end)
  355. */
  356. implement: function(){
  357. for (var i = 0, l = arguments.length; i < l; i++) $extend(this.prototype, arguments[i]);
  358. }
  359. };
  360. //internal
  361. Class.Merge = function(previous, current){
  362. if (previous && previous != current){
  363. var type = $type(current);
  364. if (type != $type(previous)) return current;
  365. switch(type){
  366. case 'function':
  367. var merged = function(){
  368. this.parent = arguments.callee.parent;
  369. return current.apply(this, arguments);
  370. };
  371. merged.parent = previous;
  372. return merged;
  373. case 'object': return $merge(previous, current);
  374. }
  375. }
  376. return current;
  377. };
  378. /*
  379. Script: Array.js
  380. Contains Array prototypes, <$A>, <$each>
  381. License:
  382. MIT-style license.
  383. */
  384. /*
  385. Class: Array
  386. A collection of The Array Object prototype methods.
  387. */
  388. //custom methods
  389. Array.extend({
  390. /*
  391. Property: forEach
  392. Iterates through an array; This method is only available for browsers without native *forEach* support.
  393. For more info see <http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Array:forEach>
  394. *forEach* executes the provided function (callback) once for each element present in the array. callback is invoked only for indexes of the array which have assigned values; it is not invoked for indexes which have been deleted or which have never been assigned values.
  395. Arguments:
  396. fn - function to execute with each item in the array; passed the item and the index of that item in the array
  397. bind - the object to bind "this" to (see <Function.bind>)
  398. Example:
  399. >['apple','banana','lemon'].each(function(item, index){
  400. > alert(index + " = " + item); //alerts "0 = apple" etc.
  401. >}, bindObj); //optional second arg for binding, not used here
  402. */
  403. forEach: function(fn, bind){
  404. for (var i = 0, j = this.length; i < j; i++) fn.call(bind, this[i], i, this);
  405. },
  406. /*
  407. Property: filter
  408. This method is provided only for browsers without native *filter* support.
  409. For more info see <http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Objects:Array:filter>
  410. *filter* calls a provided callback function once for each element in an array, and constructs a new array of all the values for which callback returns a true value. callback is invoked only for indexes of the array which have assigned values; it is not invoked for indexes which have been deleted or which have never been assigned values. Array elements which do not pass the callback test are simply skipped, and are not included in the new array.
  411. Arguments:
  412. fn - function to execute with each item in the array; passed the item and the index of that item in the array
  413. bind - the object to bind "this" to (see <Function.bind>)
  414. Example:
  415. >var biggerThanTwenty = [10,3,25,100].filter(function(item, index){
  416. > return item > 20;
  417. >});
  418. >//biggerThanTwenty = [25,100]
  419. */
  420. filter: function(fn, bind){
  421. var results = [];
  422. for (var i = 0, j = this.length; i < j; i++){
  423. if (fn.call(bind, this[i], i, this)) results.push(this[i]);
  424. }
  425. return results;
  426. },
  427. /*
  428. Property: map
  429. This method is provided only for browsers without native *map* support.
  430. For more info see <http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Array:map>
  431. *map* calls a provided callback function once for each element in an array, in order, and constructs a new array from the results. callback is invoked only for indexes of the array which have assigned values; it is not invoked for indexes which have been deleted or which have never been assigned values.
  432. Arguments:
  433. fn - function to execute with each item in the array; passed the item and the index of that item in the array
  434. bind - the object to bind "this" to (see <Function.bind>)
  435. Example:
  436. >var timesTwo = [1,2,3].map(function(item, index){
  437. > return item*2;
  438. >});
  439. >//timesTwo = [2,4,6];
  440. */
  441. map: function(fn, bind){
  442. var results = [];
  443. for (var i = 0, j = this.length; i < j; i++) results[i] = fn.call(bind, this[i], i, this);
  444. return results;
  445. },
  446. /*
  447. Property: every
  448. This method is provided only for browsers without native *every* support.
  449. For more info see <http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Array:every>
  450. *every* executes the provided callback function once for each element present in the array until it finds one where callback returns a false value. If such an element is found, the every method immediately returns false. Otherwise, if callback returned a true value for all elements, every will return true. callback is invoked only for indexes of the array which have assigned values; it is not invoked for indexes which have been deleted or which have never been assigned values.
  451. Arguments:
  452. fn - function to execute with each item in the array; passed the item and the index of that item in the array
  453. bind - the object to bind "this" to (see <Function.bind>)
  454. Example:
  455. >var areAllBigEnough = [10,4,25,100].every(function(item, index){
  456. > return item > 20;
  457. >});
  458. >//areAllBigEnough = false
  459. */
  460. every: function(fn, bind){
  461. for (var i = 0, j = this.length; i < j; i++){
  462. if (!fn.call(bind, this[i], i, this)) return false;
  463. }
  464. return true;
  465. },
  466. /*
  467. Property: some
  468. This method is provided only for browsers without native *some* support.
  469. For more info see <http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Array:some>
  470. *some* executes the callback function once for each element present in the array until it finds one where callback returns a true value. If such an element is found, some immediately returns true. Otherwise, some returns false. callback is invoked only for indexes of the array which have assigned values; it is not invoked for indexes which have been deleted or which have never been assigned values.
  471. Arguments:
  472. fn - function to execute with each item in the array; passed the item and the index of that item in the array
  473. bind - the object to bind "this" to (see <Function.bind>)
  474. Example:
  475. >var isAnyBigEnough = [10,4,25,100].some(function(item, index){
  476. > return item > 20;
  477. >});
  478. >//isAnyBigEnough = true
  479. */
  480. some: function(fn, bind){
  481. for (var i = 0, j = this.length; i < j; i++){
  482. if (fn.call(bind, this[i], i, this)) return true;
  483. }
  484. return false;
  485. },
  486. /*
  487. Property: indexOf
  488. This method is provided only for browsers without native *indexOf* support.
  489. For more info see <http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Array:indexOf>
  490. *indexOf* compares a search element to elements of the Array using strict equality (the same method used by the ===, or triple-equals, operator).
  491. Arguments:
  492. item - any type of object; element to locate in the array
  493. from - integer; optional; the index of the array at which to begin the search (defaults to 0)
  494. Example:
  495. >['apple','lemon','banana'].indexOf('lemon'); //returns 1
  496. >['apple','lemon'].indexOf('banana'); //returns -1
  497. */
  498. indexOf: function(item, from){
  499. var len = this.length;
  500. for (var i = (from < 0) ? Math.max(0, len + from) : from || 0; i < len; i++){
  501. if (this[i] === item) return i;
  502. }
  503. return -1;
  504. },
  505. /*
  506. Property: each
  507. Same as <Array.forEach>.
  508. Arguments:
  509. fn - function to execute with each item in the array; passed the item and the index of that item in the array
  510. bind - optional, the object that the "this" of the function will refer to.
  511. Example:
  512. >var Animals = ['Cat', 'Dog', 'Coala'];
  513. >Animals.each(function(animal){
  514. > document.write(animal)
  515. >});
  516. */
  517. /*
  518. Property: copy
  519. returns a copy of the array.
  520. Returns:
  521. a new array which is a copy of the current one.
  522. Arguments:
  523. start - integer; optional; the index where to start the copy, default is 0. If negative, it is taken as the offset from the end of the array.
  524. length - integer; optional; the number of elements to copy. By default, copies all elements from start to the end of the array.
  525. Example:
  526. >var letters = ["a","b","c"];
  527. >var copy = letters.copy(); // ["a","b","c"] (new instance)
  528. */
  529. copy: function(start, length){
  530. start = start || 0;
  531. if (start < 0) start = this.length + start;
  532. length = length || (this.length - start);
  533. var newArray = [];
  534. for (var i = 0; i < length; i++) newArray[i] = this[start++];
  535. return newArray;
  536. },
  537. /*
  538. Property: remove
  539. Removes all occurrences of an item from the array.
  540. Arguments:
  541. item - the item to remove
  542. Returns:
  543. the Array with all occurrences of the item removed.
  544. Example:
  545. >["1","2","3","2"].remove("2") // ["1","3"];
  546. */
  547. remove: function(item){
  548. var i = 0;
  549. var len = this.length;
  550. while (i < len){
  551. if (this[i] === item){
  552. this.splice(i, 1);
  553. len--;
  554. } else {
  555. i++;
  556. }
  557. }
  558. return this;
  559. },
  560. /*
  561. Property: contains
  562. Tests an array for the presence of an item.
  563. Arguments:
  564. item - the item to search for in the array.
  565. from - integer; optional; the index at which to begin the search, default is 0. If negative, it is taken as the offset from the end of the array.
  566. Returns:
  567. true - the item was found
  568. false - it wasn't
  569. Example:
  570. >["a","b","c"].contains("a"); // true
  571. >["a","b","c"].contains("d"); // false
  572. */
  573. contains: function(item, from){
  574. return this.indexOf(item, from) != -1;
  575. },
  576. /*
  577. Property: associate
  578. Creates an object with key-value pairs based on the array of keywords passed in
  579. and the current content of the array.
  580. Arguments:
  581. keys - the array of keywords.
  582. Example:
  583. (start code)
  584. var Animals = ['Cat', 'Dog', 'Coala', 'Lizard'];
  585. var Speech = ['Miao', 'Bau', 'Fruuu', 'Mute'];
  586. var Speeches = Animals.associate(Speech);
  587. //Speeches['Miao'] is now Cat.
  588. //Speeches['Bau'] is now Dog.
  589. //...
  590. (end)
  591. */
  592. associate: function(keys){
  593. var obj = {}, length = Math.min(this.length, keys.length);
  594. for (var i = 0; i < length; i++) obj[keys[i]] = this[i];
  595. return obj;
  596. },
  597. /*
  598. Property: extend
  599. Extends an array with another one.
  600. Arguments:
  601. array - the array to extend ours with
  602. Example:
  603. >var Animals = ['Cat', 'Dog', 'Coala'];
  604. >Animals.extend(['Lizard']);
  605. >//Animals is now: ['Cat', 'Dog', 'Coala', 'Lizard'];
  606. */
  607. extend: function(array){
  608. for (var i = 0, j = array.length; i < j; i++) this.push(array[i]);
  609. return this;
  610. },
  611. /*
  612. Property: merge
  613. merges an array in another array, without duplicates. (case- and type-sensitive)
  614. Arguments:
  615. array - the array to merge from.
  616. Example:
  617. >['Cat','Dog'].merge(['Dog','Coala']); //returns ['Cat','Dog','Coala']
  618. */
  619. merge: function(array){
  620. for (var i = 0, l = array.length; i < l; i++) this.include(array[i]);
  621. return this;
  622. },
  623. /*
  624. Property: include
  625. includes the passed in element in the array, only if its not already present. (case- and type-sensitive)
  626. Arguments:
  627. item - item to add to the array (if not present)
  628. Example:
  629. >['Cat','Dog'].include('Dog'); //returns ['Cat','Dog']
  630. >['Cat','Dog'].include('Coala'); //returns ['Cat','Dog','Coala']
  631. */
  632. include: function(item){
  633. if (!this.contains(item)) this.push(item);
  634. return this;
  635. },
  636. /*
  637. Property: getRandom
  638. returns a random item in the Array
  639. */
  640. getRandom: function(){
  641. return this[$random(0, this.length - 1)] || null;
  642. },
  643. /*
  644. Property: getLast
  645. returns the last item in the Array
  646. */
  647. getLast: function(){
  648. return this[this.length - 1] || null;
  649. }
  650. });
  651. //copies
  652. Array.prototype.each = Array.prototype.forEach;
  653. Array.each = Array.forEach;
  654. /* Section: Utility Functions */
  655. /*
  656. Function: $A()
  657. Same as <Array.copy>, but as function.
  658. Useful to apply Array prototypes to iterable objects, as a collection of DOM elements or the arguments object.
  659. Example:
  660. (start code)
  661. function myFunction(){
  662. $A(arguments).each(argument, function(){
  663. alert(argument);
  664. });
  665. };
  666. //the above will alert all the arguments passed to the function myFunction.
  667. (end)
  668. */
  669. function $A(array){
  670. return Array.copy(array);
  671. };
  672. /*
  673. Function: $each
  674. Use to iterate through iterables that are not regular arrays, such as builtin getElementsByTagName calls, arguments of a function, or an object.
  675. Arguments:
  676. iterable - an iterable element or an objct.
  677. function - function to apply to the iterable.
  678. bind - optional, the 'this' of the function will refer to this object.
  679. Function argument:
  680. The function argument will be passed the following arguments.
  681. item - the current item in the iterator being procesed
  682. index - integer; the index of the item, or key in case of an object.
  683. Examples:
  684. (start code)
  685. $each(['Sun','Mon','Tue'], function(day, index){
  686. alert('name:' + day + ', index: ' + index);
  687. });
  688. //alerts "name: Sun, index: 0", "name: Mon, index: 1", etc.
  689. //over an object
  690. $each({first: "Sunday", second: "Monday", third: "Tuesday"}, function(value, key){
  691. alert("the " + key + " day of the week is " + value);
  692. });
  693. //alerts "the first day of the week is Sunday",
  694. //"the second day of the week is Monday", etc.
  695. (end)
  696. */
  697. function $each(iterable, fn, bind){
  698. if (iterable && typeof iterable.length == 'number' && $type(iterable) != 'object'){
  699. Array.forEach(iterable, fn, bind);
  700. } else {
  701. for (var name in iterable) fn.call(bind || iterable, iterable[name], name);
  702. }
  703. };
  704. /*compatibility*/
  705. Array.prototype.test = Array.prototype.contains;
  706. /*end compatibility*/
  707. /*
  708. Script: String.js
  709. Contains String prototypes.
  710. License:
  711. MIT-style license.
  712. */
  713. /*
  714. Class: String
  715. A collection of The String Object prototype methods.
  716. */
  717. String.extend({
  718. /*
  719. Property: test
  720. Tests a string with a regular expression.
  721. Arguments:
  722. regex - a string or regular expression object, the regular expression you want to match the string with
  723. params - optional, if first parameter is a string, any parameters you want to pass to the regex ('g' has no effect)
  724. Returns:
  725. true if a match for the regular expression is found in the string, false if not.
  726. See <http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Objects:RegExp:test>
  727. Example:
  728. >"I like cookies".test("cookie"); // returns true
  729. >"I like cookies".test("COOKIE", "i") // ignore case, returns true
  730. >"I like cookies".test("cake"); // returns false
  731. */
  732. test: function(regex, params){
  733. return (($type(regex) == 'string') ? new RegExp(regex, params) : regex).test(this);
  734. },
  735. /*
  736. Property: toInt
  737. parses a string to an integer.
  738. Returns:
  739. either an int or "NaN" if the string is not a number.
  740. Example:
  741. >var value = "10px".toInt(); // value is 10
  742. */
  743. toInt: function(){
  744. return parseInt(this, 10);
  745. },
  746. /*
  747. Property: toFloat
  748. parses a string to an float.
  749. Returns:
  750. either a float or "NaN" if the string is not a number.
  751. Example:
  752. >var value = "10.848".toFloat(); // value is 10.848
  753. */
  754. toFloat: function(){
  755. return parseFloat(this);
  756. },
  757. /*
  758. Property: camelCase
  759. Converts a hiphenated string to a camelcase string.
  760. Example:
  761. >"I-like-cookies".camelCase(); //"ILikeCookies"
  762. Returns:
  763. the camel cased string
  764. */
  765. camelCase: function(){
  766. return this.replace(/-\D/g, function(match){
  767. return match.charAt(1).toUpperCase();
  768. });
  769. },
  770. /*
  771. Property: hyphenate
  772. Converts a camelCased string to a hyphen-ated string.
  773. Example:
  774. >"ILikeCookies".hyphenate(); //"I-like-cookies"
  775. */
  776. hyphenate: function(){
  777. return this.replace(/\w[A-Z]/g, function(match){
  778. return (match.charAt(0) + '-' + match.charAt(1).toLowerCase());
  779. });
  780. },
  781. /*
  782. Property: capitalize
  783. Converts the first letter in each word of a string to Uppercase.
  784. Example:
  785. >"i like cookies".capitalize(); //"I Like Cookies"
  786. Returns:
  787. the capitalized string
  788. */
  789. capitalize: function(){
  790. return this.replace(/\b[a-z]/g, function(match){
  791. return match.toUpperCase();
  792. });
  793. },
  794. /*
  795. Property: trim
  796. Trims the leading and trailing spaces off a string.
  797. Example:
  798. >" i like cookies ".trim() //"i like cookies"
  799. Returns:
  800. the trimmed string
  801. */
  802. trim: function(){
  803. return this.replace(/^\s+|\s+$/g, '');
  804. },
  805. /*
  806. Property: clean
  807. trims (<String.trim>) a string AND removes all the double spaces in a string.
  808. Returns:
  809. the cleaned string
  810. Example:
  811. >" i like cookies \n\n".clean() //"i like cookies"
  812. */
  813. clean: function(){
  814. return this.replace(/\s{2,}/g, ' ').trim();
  815. },
  816. /*
  817. Property: rgbToHex
  818. Converts an RGB value to hexidecimal. The string must be in the format of "rgb(255,255,255)" or "rgba(255,255,255,1)";
  819. Arguments:
  820. array - boolean value, defaults to false. Use true if you want the array ['FF','33','00'] as output instead of "#FF3300"
  821. Returns:
  822. hex string or array. returns "transparent" if the output is set as string and the fourth value of rgba in input string is 0.
  823. Example:
  824. >"rgb(17,34,51)".rgbToHex(); //"#112233"
  825. >"rgba(17,34,51,0)".rgbToHex(); //"transparent"
  826. >"rgb(17,34,51)".rgbToHex(true); //['11','22','33']
  827. */
  828. rgbToHex: function(array){
  829. var rgb = this.match(/\d{1,3}/g);
  830. return (rgb) ? rgb.rgbToHex(array) : false;
  831. },
  832. /*
  833. Property: hexToRgb
  834. Converts a hexidecimal color value to RGB. Input string must be the hex color value (with or without the hash). Also accepts triplets ('333');
  835. Arguments:
  836. array - boolean value, defaults to false. Use true if you want the array [255,255,255] as output instead of "rgb(255,255,255)";
  837. Returns:
  838. rgb string or array.
  839. Example:
  840. >"#112233".hexToRgb(); //"rgb(17,34,51)"
  841. >"#112233".hexToRgb(true); //[17,34,51]
  842. */
  843. hexToRgb: function(array){
  844. var hex = this.match(/^#?(\w{1,2})(\w{1,2})(\w{1,2})$/);
  845. return (hex) ? hex.slice(1).hexToRgb(array) : false;
  846. },
  847. /*
  848. Property: contains
  849. checks if the passed in string is contained in the String. also accepts an optional second parameter, to check if the string is contained in a list of separated values.
  850. Example:
  851. >'a b c'.contains('c', ' '); //true
  852. >'a bc'.contains('bc'); //true
  853. >'a bc'.contains('b', ' '); //false
  854. */
  855. contains: function(string, s){
  856. return (s) ? (s + this + s).indexOf(s + string + s) > -1 : this.indexOf(string) > -1;
  857. },
  858. /*
  859. Property: escapeRegExp
  860. Returns string with escaped regular expression characters
  861. Example:
  862. >var search = 'animals.sheeps[1]'.escapeRegExp(); // search is now 'animals\.sheeps\[1\]'
  863. Returns:
  864. Escaped string
  865. */
  866. escapeRegExp: function(){
  867. return this.replace(/([.*+?^${}()|[\]\/\\])/g, '\\$1');
  868. }
  869. });
  870. Array.extend({
  871. /*
  872. Property: rgbToHex
  873. see <String.rgbToHex>, but as an array method.
  874. */
  875. rgbToHex: function(array){
  876. if (this.length < 3) return false;
  877. if (this.length == 4 && this[3] === 0 && !array) return 'transparent';
  878. var hex = [];
  879. for (var i = 0; i < 3; i++){
  880. var bit = (this[i] - 0).toString(16);
  881. hex.push((bit.length == 1) ? '0' + bit : bit);
  882. }
  883. return array ? hex : '#' + hex.join('');
  884. },
  885. /*
  886. Property: hexToRgb
  887. same as <String.hexToRgb>, but as an array method.
  888. */
  889. hexToRgb: function(array){
  890. if (this.length != 3) return false;
  891. var rgb = [];
  892. for (var i = 0; i < 3; i++){
  893. rgb.push(parseInt((this[i].length == 1) ? this[i] + this[i] : this[i], 16));
  894. }
  895. return array ? rgb : 'rgb(' + rgb.join(',') + ')';
  896. }
  897. });
  898. /*
  899. Script: Function.js
  900. Contains Function prototypes and utility functions .
  901. License:
  902. MIT-style license.
  903. Credits:
  904. - 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
  905. */
  906. /*
  907. Class: Function
  908. A collection of The Function Object prototype methods.
  909. */
  910. Function.extend({
  911. /*
  912. Property: create
  913. Main function to create closures.
  914. Returns:
  915. a function.
  916. Arguments:
  917. options - An Options object.
  918. Options:
  919. bind - The object that the "this" of the function will refer to. Default is the current function.
  920. event - If set to true, the function will act as an event listener and receive an event as first argument.
  921. If set to a class name, the function will receive a new instance of this class (with the event passed as argument's constructor) as first argument.
  922. Default is false.
  923. arguments - A single argument or array of arguments that will be passed to the function when called.
  924. If both the event and arguments options are set, the event is passed as first argument and the arguments array will follow.
  925. Default is no custom arguments, the function will receive the standard arguments when called.
  926. delay - Numeric value: if set, the returned function will delay the actual execution by this amount of milliseconds and return a timer handle when called.
  927. Default is no delay.
  928. periodical - Numeric value: if set, the returned function will periodically perform the actual execution with this specified interval and return a timer handle when called.
  929. Default is no periodical execution.
  930. attempt - If set to true, the returned function will try to execute and return either the results or false on error. Default is false.
  931. */
  932. create: function(options){
  933. var fn = this;
  934. options = $merge({
  935. 'bind': fn,
  936. 'event': false,
  937. 'arguments': null,
  938. 'delay': false,
  939. 'periodical': false,
  940. 'attempt': false
  941. }, options);
  942. if ($chk(options.arguments) && $type(options.arguments) != 'array') options.arguments = [options.arguments];
  943. return function(event){
  944. var args;
  945. if (options.event){
  946. event = event || window.event;
  947. args = [(options.event === true) ? event : new options.event(event)];
  948. if (options.arguments) args.extend(options.arguments);
  949. }
  950. else args = options.arguments || arguments;
  951. var returns = function(){
  952. return fn.apply($pick(options.bind, fn), args);
  953. };
  954. if (options.delay) return setTimeout(returns, options.delay);
  955. if (options.periodical) return setInterval(returns, options.periodical);
  956. if (options.attempt) try {return returns();} catch(err){return false;};
  957. return returns();
  958. };
  959. },
  960. /*
  961. Property: pass
  962. Shortcut to create closures with arguments and bind.
  963. Returns:
  964. a function.
  965. Arguments:
  966. args - the arguments passed. must be an array if arguments > 1
  967. bind - optional, the object that the "this" of the function will refer to.
  968. Example:
  969. >myFunction.pass([arg1, arg2], myElement);
  970. */
  971. pass: function(args, bind){
  972. return this.create({'arguments': args, 'bind': bind});
  973. },
  974. /*
  975. Property: attempt
  976. Tries to execute the function, returns either the result of the function or false on error.
  977. Arguments:
  978. args - the arguments passed. must be an array if arguments > 1
  979. bind - optional, the object that the "this" of the function will refer to.
  980. Example:
  981. >myFunction.attempt([arg1, arg2], myElement);
  982. */
  983. attempt: function(args, bind){
  984. return this.create({'arguments': args, 'bind': bind, 'attempt': true})();
  985. },
  986. /*
  987. Property: bind
  988. method to easily create closures with "this" altered.
  989. Arguments:
  990. bind - optional, the object that the "this" of the function will refer to.
  991. args - optional, the arguments passed. must be an array if arguments > 1
  992. Returns:
  993. a function.
  994. Example:
  995. >function myFunction(){
  996. > this.setStyle('color', 'red');
  997. > // note that 'this' here refers to myFunction, not an element
  998. > // we'll need to bind this function to the element we want to alter
  999. >};
  1000. >var myBoundFunction = myFunction.bind(myElement);
  1001. >myBoundFunction(); // this will make the element myElement red.
  1002. */
  1003. bind: function(bind, args){
  1004. return this.create({'bind': bind, 'arguments': args});
  1005. },
  1006. /*
  1007. Property: bindAsEventListener
  1008. cross browser method to pass event firer
  1009. Arguments:
  1010. bind - optional, the object that the "this" of the function will refer to.
  1011. args - optional, the arguments passed. must be an array if arguments > 1
  1012. Returns:
  1013. a function with the parameter bind as its "this" and as a pre-passed argument event or window.event, depending on the browser.
  1014. Example:
  1015. >function myFunction(event){
  1016. > alert(event.clientx) //returns the coordinates of the mouse..
  1017. >};
  1018. >myElement.onclick = myFunction.bindAsEventListener(myElement);
  1019. */
  1020. bindAsEventListener: function(bind, args){
  1021. return this.create({'bind': bind, 'event': true, 'arguments': args});
  1022. },
  1023. /*
  1024. Property: delay
  1025. Delays the execution of a function by a specified duration.
  1026. Arguments:
  1027. delay - the duration to wait in milliseconds.
  1028. bind - optional, the object that the "this" of the function will refer to.
  1029. args - optional, the arguments passed. must be an array if arguments > 1
  1030. Example:
  1031. >myFunction.delay(50, myElement) //wait 50 milliseconds, then call myFunction and bind myElement to it
  1032. >(function(){alert('one second later...')}).delay(1000); //wait a second and alert
  1033. */
  1034. delay: function(delay, bind, args){
  1035. return this.create({'delay': delay, 'bind': bind, 'arguments': args})();
  1036. },
  1037. /*
  1038. Property: periodical
  1039. Executes a function in the specified intervals of time
  1040. Arguments:
  1041. interval - the duration of the intervals between executions.
  1042. bind - optional, the object that the "this" of the function will refer to.
  1043. args - optional, the arguments passed. must be an array if arguments > 1
  1044. */
  1045. periodical: function(interval, bind, args){
  1046. return this.create({'periodical': interval, 'bind': bind, 'arguments': args})();
  1047. }
  1048. });
  1049. /*
  1050. Script: Number.js
  1051. Contains the Number prototypes.
  1052. License:
  1053. MIT-style license.
  1054. */
  1055. /*
  1056. Class: Number
  1057. A collection of The Number Object prototype methods.
  1058. */
  1059. Number.extend({
  1060. /*
  1061. Property: toInt
  1062. Returns this number; useful because toInt must work on both Strings and Numbers.
  1063. */
  1064. toInt: function(){
  1065. return parseInt(this);
  1066. },
  1067. /*
  1068. Property: toFloat
  1069. Returns this number as a float; useful because toFloat must work on both Strings and Numbers.
  1070. */
  1071. toFloat: function(){
  1072. return parseFloat(this);
  1073. },
  1074. /*
  1075. Property: limit
  1076. Limits the number.
  1077. Arguments:
  1078. min - number, minimum value
  1079. max - number, maximum value
  1080. Returns:
  1081. the number in the given limits.
  1082. Example:
  1083. >(12).limit(2, 6.5) // returns 6.5
  1084. >(-4).limit(2, 6.5) // returns 2
  1085. >(4.3).limit(2, 6.5) // returns 4.3
  1086. */
  1087. limit: function(min, max){
  1088. return Math.min(max, Math.max(min, this));
  1089. },
  1090. /*
  1091. Property: round
  1092. Returns the number rounded to specified precision.
  1093. Arguments:
  1094. precision - integer, number of digits after the decimal point. Can also be negative or zero (default).
  1095. Example:
  1096. >12.45.round() // returns 12
  1097. >12.45.round(1) // returns 12.5
  1098. >12.45.round(-1) // returns 10
  1099. Returns:
  1100. The rounded number.
  1101. */
  1102. round: function(precision){
  1103. precision = Math.pow(10, precision || 0);
  1104. return Math.round(this * precision) / precision;
  1105. },
  1106. /*
  1107. Property: times
  1108. Executes a passed in function the specified number of times
  1109. Arguments:
  1110. function - the function to be executed on each iteration of the loop
  1111. Example:
  1112. >(4).times(alert);
  1113. */
  1114. times: function(fn){
  1115. for (var i = 0; i < this; i++) fn(i);
  1116. }
  1117. });
  1118. /*
  1119. Script: Element.js
  1120. Contains useful Element prototypes, to be used with the dollar function <$>.
  1121. License:
  1122. MIT-style license.
  1123. Credits:
  1124. - 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
  1125. */
  1126. /*
  1127. Class: Element
  1128. Custom class to allow all of its methods to be used with any DOM element via the dollar function <$>.
  1129. */
  1130. var Element = new Class({
  1131. /*
  1132. Property: initialize
  1133. Creates a new element of the type passed in.
  1134. Arguments:
  1135. el - string; the tag name for the element you wish to create. you can also pass in an element reference, in which case it will be extended.
  1136. props - object; the properties you want to add to your element.
  1137. Accepts the same keys as <Element.setProperties>, but also allows events and styles
  1138. Props:
  1139. the key styles will be used as setStyles, the key events will be used as addEvents. any other key is used as setProperty.
  1140. Example:
  1141. (start code)
  1142. new Element('a', {
  1143. 'styles': {
  1144. 'display': 'block',
  1145. 'border': '1px solid black'
  1146. },
  1147. 'events': {
  1148. 'click': function(){
  1149. //aaa
  1150. },
  1151. 'mousedown': function(){
  1152. //aaa
  1153. }
  1154. },
  1155. 'class': 'myClassSuperClass',
  1156. 'href': 'http://mad4milk.net'
  1157. });
  1158. (end)
  1159. */
  1160. initialize: function(el, props){
  1161. if ($type(el) == 'string'){
  1162. if (window.ie && props && (props.name || props.type)){
  1163. var name = (props.name) ? ' name="' + props.name + '"' : '';
  1164. var type = (props.type) ? ' type="' + props.type + '"' : '';
  1165. delete props.name;
  1166. delete props.type;
  1167. el = '<' + el + name + type + '>';
  1168. }
  1169. el = document.createElement(el);
  1170. }
  1171. el = $(el);
  1172. return (!props || !el) ? el : el.set(props);
  1173. }
  1174. });
  1175. /*
  1176. Class: Elements
  1177. - Every dom function such as <$$>, or in general every function that returns a collection of nodes in mootools, returns them as an Elements class.
  1178. - The purpose of the Elements class is to allow <Element> methods to work also on <Elements> array.
  1179. - Elements is also an Array, so it accepts all the <Array> methods.
  1180. - Every node of the Elements instance is already extended with <$>.
  1181. Example:
  1182. >$$('myselector').each(function(el){
  1183. > //...
  1184. >});
  1185. some iterations here, $$('myselector') is also an array.
  1186. >$$('myselector').setStyle('color', 'red');
  1187. every element returned by $$('myselector') also accepts <Element> methods, in this example every element will be made red.
  1188. */
  1189. var Elements = new Class({
  1190. initialize: function(elements){
  1191. return (elements) ? $extend(elements, this) : this;
  1192. }
  1193. });
  1194. Elements.extend = function(props){
  1195. for (var prop in props){
  1196. this.prototype[prop] = props[prop];
  1197. this[prop] = $native.generic(prop);
  1198. }
  1199. };
  1200. /*
  1201. Section: Utility Functions
  1202. Function: $
  1203. returns the element passed in with all the Element prototypes applied.
  1204. Arguments:
  1205. el - a reference to an actual element or a string representing the id of an element
  1206. Example:
  1207. >$('myElement') // gets a DOM element by id with all the Element prototypes applied.
  1208. >var div = document.getElementById('myElement');
  1209. >$(div) //returns an Element also with all the mootools extentions applied.
  1210. You'll use this when you aren't sure if a variable is an actual element or an id, as
  1211. well as just shorthand for document.getElementById().
  1212. Returns:
  1213. a DOM element or false (if no id was found).
  1214. Note:
  1215. you need to call $ on an element only once to get all the prototypes.
  1216. But its no harm to call it multiple times, as it will detect if it has been already extended.
  1217. */
  1218. function $(el){
  1219. if (!el) return null;
  1220. if (el.htmlElement) return Garbage.collect(el);
  1221. if ([window, document].contains(el)) return el;
  1222. var type = $type(el);
  1223. if (type == 'string'){
  1224. el = document.getElementById(el);
  1225. type = (el) ? 'element' : false;
  1226. }
  1227. if (type != 'element') return null;
  1228. if (el.htmlElement) return Garbage.collect(el);
  1229. if (['object', 'embed'].contains(el.tagName.toLowerCase())) return el;
  1230. $extend(el, Element.prototype);
  1231. el.htmlElement = function(){};
  1232. return Garbage.collect(el);
  1233. };
  1234. /*
  1235. Function: $$
  1236. Selects, and extends DOM elements. Elements arrays returned with $$ will also accept all the <Element> methods.
  1237. The return type of element methods run through $$ is always an array. If the return array is only made by elements,
  1238. $$ will be applied automatically.
  1239. Arguments:
  1240. HTML Collections, arrays of elements, arrays of strings as element ids, elements, strings as selectors.
  1241. Any number of the above as arguments are accepted.
  1242. Note:
  1243. if you load <Element.Selectors.js>, $$ will also accept CSS Selectors, otherwise the only selectors supported are tag names.
  1244. Example:
  1245. >$$('a') //an array of all anchor tags on the page
  1246. >$$('a', 'b') //an array of all anchor and bold tags on the page
  1247. >$$('#myElement') //array containing only the element with id = myElement. (only with <Element.Selectors.js>)
  1248. >$$('#myElement a.myClass') //an array of all anchor tags with the class "myClass"
  1249. >//within the DOM element with id "myElement" (only with <Element.Selectors.js>)
  1250. >$$(myelement, myelement2, 'a', ['myid', myid2, 'myid3'], document.getElementsByTagName('div')) //an array containing:
  1251. >// the element referenced as myelement if existing,
  1252. >// the element referenced as myelement2 if existing,
  1253. >// all the elements with a as tag in the page,
  1254. >// the element with id = myid if existing
  1255. >// the element with id = myid2 if existing
  1256. >// the element with id = myid3 if existing
  1257. >// all the elements with div as tag in the page
  1258. Returns:
  1259. array - array of all the dom elements matched, extended with <$>. Returns as <Elements>.
  1260. */
  1261. document.getElementsBySelector = document.getElementsByTagName;
  1262. function $$(){
  1263. var elements = [];
  1264. for (var i = 0, j = arguments.length; i < j; i++){
  1265. var selector = arguments[i];
  1266. switch($type(selector)){
  1267. case 'element': elements.push(selector);
  1268. case 'boolean': break;
  1269. case false: break;
  1270. case 'string': selector = document.getElementsBySelector(selector, true);
  1271. default: elements.extend(selector);
  1272. }
  1273. }
  1274. return $$.unique(elements);
  1275. };
  1276. $$.unique = function(array){
  1277. var elements = [];
  1278. for (var i = 0, l = array.length; i < l; i++){
  1279. if (array[i].$included) continue;
  1280. var element = $(array[i]);
  1281. if (element && !element.$included){
  1282. element.$included = true;
  1283. elements.push(element);
  1284. }
  1285. }
  1286. for (var n = 0, d = elements.length; n < d; n++) elements[n].$included = null;
  1287. return new Elements(elements);
  1288. };
  1289. Elements.Multi = function(property){
  1290. return function(){
  1291. var args = arguments;
  1292. var items = [];
  1293. var elements = true;
  1294. for (var i = 0, j = this.length, returns; i < j; i++){
  1295. returns = this[i][property].apply(this[i], args);
  1296. if ($type(returns) != 'element') elements = false;
  1297. items.push(returns);
  1298. };
  1299. return (elements) ? $$.unique(items) : items;
  1300. };
  1301. };
  1302. Element.extend = function(properties){
  1303. for (var property in properties){
  1304. HTMLElement.prototype[property] = properties[property];
  1305. Element.prototype[property] = properties[property];
  1306. Element[property] = $native.generic(property);
  1307. var elementsProperty = (Array.prototype[property]) ? property + 'Elements' : property;
  1308. Elements.prototype[elementsProperty] = Elements.Multi(property);
  1309. }
  1310. };
  1311. /*
  1312. Class: Element
  1313. Custom class to allow all of its methods to be used with any DOM element via the dollar function <$>.
  1314. */
  1315. Element.extend({
  1316. /*
  1317. Property: set
  1318. you can set events, styles and properties with this shortcut. same as calling new Element.
  1319. */
  1320. set: function(props){
  1321. for (var prop in props){
  1322. var val = props[prop];
  1323. switch(prop){
  1324. case 'styles': this.setStyles(val); break;
  1325. case 'events': if (this.addEvents) this.addEvents(val); break;
  1326. case 'properties': this.setProperties(val); break;
  1327. default: this.setProperty(prop, val);
  1328. }
  1329. }
  1330. return this;
  1331. },
  1332. inject: function(el, where){
  1333. el = $(el);
  1334. switch(where){
  1335. case 'before': el.parentNode.insertBefore(this, el); break;
  1336. case 'after':
  1337. var next = el.getNext();
  1338. if (!next) el.parentNode.appendChild(this);
  1339. else el.parentNode.insertBefore(this, next);
  1340. break;
  1341. case 'top':
  1342. var first = el.firstChild;
  1343. if (first){
  1344. el.insertBefore(this, first);
  1345. break;
  1346. }
  1347. default: el.appendChild(this);
  1348. }
  1349. return this;
  1350. },
  1351. /*
  1352. Property: injectBefore
  1353. Inserts the Element before the passed element.
  1354. Arguments:
  1355. el - an element reference or the id of the element to be injected in.
  1356. Example:
  1357. >html:
  1358. ><div id="myElement"></div>
  1359. ><div id="mySecondElement"></div>
  1360. >js:
  1361. >$('mySecondElement').injectBefore('myElement');
  1362. >resulting html:
  1363. ><div id="mySecondElement"></div>
  1364. ><div id="myElement"></div>
  1365. */
  1366. injectBefore: function(el){
  1367. return this.inject(el, 'before');
  1368. },
  1369. /*
  1370. Property: injectAfter
  1371. Same as <Element.injectBefore>, but inserts the element after.
  1372. */
  1373. injectAfter: function(el){
  1374. return this.inject(el, 'after');
  1375. },
  1376. /*
  1377. Property: injectInside
  1378. Same as <Element.injectBefore>, but inserts the element inside.
  1379. */
  1380. injectInside: function(el){
  1381. return this.inject(el, 'bottom');
  1382. },
  1383. /*
  1384. Property: injectTop
  1385. Same as <Element.injectInside>, but inserts the element inside, at the top.
  1386. */
  1387. injectTop: function(el){
  1388. return this.inject(el, 'top');
  1389. },
  1390. /*
  1391. Property: adopt
  1392. Inserts the passed elements inside the Element.
  1393. Arguments:
  1394. accepts elements references, element ids as string, selectors ($$('stuff')) / array of elements, array of ids as strings and collections.
  1395. */
  1396. adopt: function(){
  1397. var elements = [];
  1398. $each(arguments, function(argument){
  1399. elements = elements.concat(argument);
  1400. });
  1401. $$(elements).inject(this);
  1402. return this;
  1403. },
  1404. /*
  1405. Property: remove
  1406. Removes the Element from the DOM.
  1407. Example:
  1408. >$('myElement').remove() //bye bye
  1409. */
  1410. remove: function(){
  1411. return this.parentNode.removeChild(this);
  1412. },
  1413. /*
  1414. Property: clone
  1415. Clones the Element and returns the cloned one.
  1416. Arguments:
  1417. contents - boolean, when true the Element is cloned with childNodes, default true
  1418. Returns:
  1419. the cloned element
  1420. Example:
  1421. >var clone = $('myElement').clone().injectAfter('myElement');
  1422. >//clones the Element and append the clone after the Element.
  1423. */
  1424. clone: function(contents){
  1425. var el = $(this.cloneNode(contents !== false));
  1426. if (!el.$events) return el;
  1427. el.$events = {};
  1428. for (var type in this.$events) el.$events[type] = {
  1429. 'keys': $A(this.$events[type].keys),
  1430. 'values': $A(this.$events[type].values)
  1431. };
  1432. return el.removeEvents();
  1433. },
  1434. /*
  1435. Property: replaceWith
  1436. Replaces the Element with an element passed.
  1437. Arguments:
  1438. el - a string representing the element to be injected in (myElementId, or div), or an element reference.
  1439. If you pass div or another tag, the element will be created.
  1440. Returns:
  1441. the passed in element
  1442. Example:
  1443. >$('myOldElement').replaceWith($('myNewElement')); //$('myOldElement') is gone, and $('myNewElement') is in its place.
  1444. */
  1445. replaceWith: function(el){
  1446. el = $(el);
  1447. this.parentNode.replaceChild(el, this);
  1448. return el;
  1449. },
  1450. /*
  1451. Property: appendText
  1452. Appends text node to a DOM element.
  1453. Arguments:
  1454. text - the text to append.
  1455. Example:
  1456. ><div id="myElement">hey</div>
  1457. >$('myElement').appendText(' howdy'); //myElement innerHTML is now "hey howdy"
  1458. */
  1459. appendText: function(text){
  1460. this.appendChild(document.createTextNode(text));
  1461. return this;
  1462. },
  1463. /*
  1464. Property: hasClass
  1465. Tests the Element to see if it has the passed in className.
  1466. Returns:
  1467. true - the Element has the class
  1468. false - it doesn't
  1469. Arguments:
  1470. className - string; the class name to test.
  1471. Example:
  1472. ><div id="myElement" class="testClass"></div>
  1473. >$('myElement').hasClass('testClass'); //returns true
  1474. */
  1475. hasClass: function(className){
  1476. return this.className.contains(className, ' ');
  1477. },
  1478. /*
  1479. Property: addClass
  1480. Adds the passed in class to the Element, if the element doesnt already have it.
  1481. Arguments:
  1482. className - string; the class name to add
  1483. Example:
  1484. ><div id="myElement" class="testClass"></div>
  1485. >$('myElement').addClass('newClass'); //<div id="myElement" class="testClass newClass"></div>
  1486. */
  1487. addClass: function(className){
  1488. if (!this.hasClass(className)) this.className = (this.className + ' ' + className).clean();
  1489. return this;
  1490. },
  1491. /*
  1492. Property: removeClass
  1493. Works like <Element.addClass>, but removes the class from the element.
  1494. */
  1495. removeClass: function(className){
  1496. this.className = this.className.replace(new RegExp('(^|\\s)' + className + '(?:\\s|$)'), '$1').clean();
  1497. return this;
  1498. },
  1499. /*
  1500. Property: toggleClass
  1501. Adds or removes the passed in class name to the element, depending on if it's present or not.
  1502. Arguments:
  1503. className - the class to add or remove
  1504. Example:
  1505. ><div id="myElement" class="myClass"></div>
  1506. >$('myElement').toggleClass('myClass');
  1507. ><div id="myElement" class=""></div>
  1508. >$('myElement').toggleClass('myClass');
  1509. ><div id="myElement" class="myClass"></div>
  1510. */
  1511. toggleClass: function(className){
  1512. return this.hasClass(className) ? this.removeClass(className) : this.addClass(className);
  1513. },
  1514. /*
  1515. Property: setStyle
  1516. Sets a css property to the Element.
  1517. Arguments:
  1518. property - the property to set
  1519. value - the value to which to set it; for numeric values that require "px" you can pass an integer
  1520. Example:
  1521. >$('myElement').setStyle('width', '300px'); //the width is now 300px
  1522. >$('myElement').setStyle('width', 300); //the width is now 300px
  1523. */
  1524. setStyle: function(property, value){
  1525. switch(property){
  1526. case 'opacity': return this.setOpacity(parseFloat(value));
  1527. case 'float': property = (window.ie) ? 'styleFloat' : 'cssFloat';
  1528. }
  1529. property = property.camelCase();
  1530. switch($type(value)){
  1531. case 'number': if (!['zIndex', 'zoom'].contains(property)) value += 'px'; break;
  1532. case 'array': value = 'rgb(' + value.join(',') + ')';
  1533. }
  1534. this.style[property] = value;
  1535. return this;
  1536. },
  1537. /*
  1538. Property: setStyles
  1539. Applies a collection of styles to the Element.
  1540. Arguments:
  1541. source - an object or string containing all the styles to apply. When its a string it overrides old style.
  1542. Examples:
  1543. >$('myElement').setStyles({
  1544. > border: '1px solid #000',
  1545. > width: 300,
  1546. > height: 400
  1547. >});
  1548. OR
  1549. >$('myElement').setStyles('border: 1px solid #000; width: 300px; height: 400px;');
  1550. */
  1551. setStyles: function(source){
  1552. switch($type(source)){
  1553. case 'object': Element.setMany(this, 'setStyle', source); break;
  1554. case 'string': this.style.cssText = source;
  1555. }
  1556. return this;
  1557. },
  1558. /*
  1559. Property: setOpacity
  1560. Sets the opacity of the Element, and sets also visibility == "hidden" if opacity === 0, and visibility = "visible" if opacity > 0.
  1561. Arguments:
  1562. opacity - float; Accepts values from 0 to 1.
  1563. Example:
  1564. >$('myElement').setOpacity(0.5) //make it 50% transparent
  1565. */
  1566. setOpacity: function(opacity){
  1567. if (opacity === 0){
  1568. if (this.style.visibility != "hidden") this.style.visibility = "hidden";
  1569. } else {
  1570. if (this.style.visibility != "visible") this.style.visibility = "visible";
  1571. }
  1572. if (!this.currentStyle || !this.currentStyle.hasLayout) this.style.zoom = 1;
  1573. if (window.ie) this.style.filter = (opacity == 1) ? '' : "alpha(opacity=" + opacity * 100 + ")";
  1574. this.style.opacity = this.$tmp.opacity = opacity;
  1575. return this;
  1576. },
  1577. /*
  1578. Property: getStyle
  1579. Returns the style of the Element given the property passed in.
  1580. Arguments:
  1581. property - the css style property you want to retrieve
  1582. Example:
  1583. >$('myElement').getStyle('width'); //returns "400px"
  1584. >//but you can also use
  1585. >$('myElement').getStyle('width').toInt(); //returns 400
  1586. Returns:
  1587. the style as a string
  1588. */
  1589. getStyle: function(property){
  1590. property = property.camelCase();
  1591. var result = this.style[property];
  1592. if (!$chk(result)){
  1593. if (property == 'opacity') return this.$tmp.opacity;
  1594. result = [];
  1595. for (var style in Element.Styles){
  1596. if (property == style){
  1597. Element.Styles[style].each(function(s){
  1598. var style = this.getStyle(s);
  1599. result.push(parseInt(style) ? style : '0px');
  1600. }, this);
  1601. if (property == 'border'){
  1602. var every = result.every(function(bit){
  1603. return (bit == result[0]);
  1604. });
  1605. return (every) ? result[0] : false;
  1606. }
  1607. return result.join(' ');
  1608. }
  1609. }
  1610. if (property.contains('border')){
  1611. if (Element.Styles.border.contains(property)){
  1612. return ['Width', 'Style', 'Color'].map(function(p){
  1613. return this.getStyle(property + p);
  1614. }, this).join(' ');
  1615. } else if (Element.borderShort.contains(property)){
  1616. return ['Top', 'Right', 'Bottom', 'Left'].map(function(p){
  1617. return this.getStyle('border' + p + property.replace('border', ''));
  1618. }, this).join(' ');
  1619. }
  1620. }
  1621. if (document.defaultView) result = document.defaultView.getComputedStyle(this, null).getPropertyValue(property.hyphenate());
  1622. else if (this.currentStyle) result = this.currentStyle[property];
  1623. }
  1624. if (window.ie) result = Element.fixStyle(property, result, this);
  1625. if (result && property.test(/color/i) && result.contains('rgb')){
  1626. return result.split('rgb').splice(1,4).map(function(color){
  1627. return color.rgbToHex();
  1628. }).join(' ');
  1629. }
  1630. return result;
  1631. },
  1632. /*
  1633. Property: getStyles
  1634. Returns an object of styles of the Element for each argument passed in.
  1635. Arguments:
  1636. properties - strings; any number of style properties
  1637. Example:
  1638. >$('myElement').getStyles('width','height','padding');
  1639. >//returns an object like:
  1640. >{width: "10px", height: "10px", padding: "10px 0px 10px 0px"}
  1641. */
  1642. getStyles: function(){
  1643. return Element.getMany(this, 'getStyle', arguments);
  1644. },
  1645. walk: function(brother, start){
  1646. brother += 'Sibling';
  1647. var el = (start) ? this[start] : this[brother];
  1648. while (el && $type(el) != 'element') el = el[brother];
  1649. return $(el);
  1650. },
  1651. /*
  1652. Property: getPrevious
  1653. Returns the previousSibling of the Element, excluding text nodes.
  1654. Example:
  1655. >$('myElement').getPrevious(); //get the previous DOM element from myElement
  1656. Returns:
  1657. the sibling element or undefined if none found.
  1658. */
  1659. getPrevious: function(){
  1660. return this.walk('previous');
  1661. },
  1662. /*
  1663. Property: getNext
  1664. Works as Element.getPrevious, but tries to find the nextSibling.
  1665. */
  1666. getNext: function(){
  1667. return this.walk('next');
  1668. },
  1669. /*
  1670. Property: getFirst
  1671. Works as <Element.getPrevious>, but tries to find the firstChild.
  1672. */
  1673. getFirst: function(){
  1674. return this.walk('next', 'firstChild');
  1675. },
  1676. /*
  1677. Property: getLast
  1678. Works as <Element.getPrevious>, but tries to find the lastChild.
  1679. */
  1680. getLast: function(){
  1681. return this.walk('previous', 'lastChild');
  1682. },
  1683. /*
  1684. Property: getParent
  1685. returns the $(element.parentNode)
  1686. */
  1687. getParent: function(){
  1688. return $(this.parentNode);
  1689. },
  1690. /*
  1691. Property: getChildren
  1692. returns all the $(element.childNodes), excluding text nodes. Returns as <Elements>.
  1693. */
  1694. getChildren: function(){
  1695. return $$(this.childNodes);
  1696. },
  1697. /*
  1698. Property: hasChild
  1699. returns true if the passed in element is a child of the $(element).
  1700. */
  1701. hasChild: function(el){
  1702. return !!$A(this.getElementsByTagName('*')).contains(el);
  1703. },
  1704. /*
  1705. Property: getProperty
  1706. Gets the an attribute of the Element.
  1707. Arguments:
  1708. property - string; the attribute to retrieve
  1709. Example:
  1710. >$('myImage').getProperty('src') // returns whatever.gif
  1711. Returns:
  1712. the value, or an empty string
  1713. */
  1714. getProperty: function(property){
  1715. var index = Element.Properties[property];
  1716. if (index) return this[index];
  1717. var flag = Element.PropertiesIFlag[property] || 0;
  1718. if (!window.ie || flag) return this.getAttribute(property, flag);
  1719. var node = this.attributes[property];
  1720. return (node) ? node.nodeValue : null;
  1721. },
  1722. /*
  1723. Property: removeProperty
  1724. Removes an attribute from the Element
  1725. Arguments:
  1726. property - string; the attribute to remove
  1727. */
  1728. removeProperty: function(property){
  1729. var index = Element.Properties[property];
  1730. if (index) this[index] = '';
  1731. else this.removeAttribute(property);
  1732. return this;
  1733. },
  1734. /*
  1735. Property: getProperties
  1736. same as <Element.getStyles>, but for properties
  1737. */
  1738. getProperties: function(){
  1739. return Element.getMany(this, 'getProperty', arguments);
  1740. },
  1741. /*
  1742. Property: setProperty
  1743. Sets an attribute for the Element.
  1744. Arguments:
  1745. property - string; the property to assign the value passed in
  1746. value - the value to assign to the property passed in
  1747. Example:
  1748. >$('myImage').setProperty('src', 'whatever.gif'); //myImage now points to whatever.gif for its source
  1749. */
  1750. setProperty: function(property, value){
  1751. var index = Element.Properties[property];
  1752. if (index) this[index] = value;
  1753. else this.setAttribute(property, value);
  1754. return this;
  1755. },
  1756. /*
  1757. Property: setProperties
  1758. Sets numerous attributes for the Element.
  1759. Arguments:
  1760. source - an object with key/value pairs.
  1761. Example:
  1762. (start code)
  1763. $('myElement').setProperties({
  1764. src: 'whatever.gif',
  1765. alt: 'whatever dude'
  1766. });
  1767. <img src="whatever.gif" alt="whatever dude">
  1768. (end)
  1769. */
  1770. setProperties: function(source){
  1771. return Element.setMany(this, 'setProperty', source);
  1772. },
  1773. /*
  1774. Property: setHTML
  1775. Sets the innerHTML of the Element.
  1776. Arguments:
  1777. html - string; the new innerHTML for the element.
  1778. Example:
  1779. >$('myElement').setHTML(newHTML) //the innerHTML of myElement is now = newHTML
  1780. */
  1781. setHTML: function(){
  1782. this.innerHTML = $A(arguments).join('');
  1783. return this;
  1784. },
  1785. /*
  1786. Property: setText
  1787. Sets the inner text of the Element.
  1788. Arguments:
  1789. text - string; the new text content for the element.
  1790. Example:
  1791. >$('myElement').setText('some text') //the text of myElement is now = 'some text'
  1792. */
  1793. setText: function(text){
  1794. var tag = this.getTag();
  1795. if (['style', 'script'].contains(tag)){
  1796. if (window.ie){
  1797. if (tag == 'style') this.styleSheet.cssText = text;
  1798. else if (tag == 'script') this.setProperty('text', text);
  1799. return this;
  1800. } else {
  1801. this.removeChild(this.firstChild);
  1802. return this.appendText(text);
  1803. }
  1804. }
  1805. this[$defined(this.innerText) ? 'innerText' : 'textContent'] = text;
  1806. return this;
  1807. },
  1808. /*
  1809. Property: getText
  1810. Gets the inner text of the Element.
  1811. */
  1812. getText: function(){
  1813. var tag = this.getTag();
  1814. if (['style', 'script'].contains(tag)){
  1815. if (window.ie){
  1816. if (tag == 'style') return this.styleSheet.cssText;
  1817. else if (tag == 'script') return this.getProperty('text');
  1818. } else {
  1819. return this.innerHTML;
  1820. }
  1821. }
  1822. return ($pick(this.innerText, this.textContent));
  1823. },
  1824. /*
  1825. Property: getTag
  1826. Returns the tagName of the element in lower case.
  1827. Example:
  1828. >$('myImage').getTag() // returns 'img'
  1829. Returns:
  1830. The tag name in lower case
  1831. */
  1832. getTag: function(){
  1833. return this.tagName.toLowerCase();
  1834. },
  1835. /*
  1836. Property: empty
  1837. Empties an element of all its children.
  1838. Example:
  1839. >$('myDiv').empty() // empties the Div and returns it
  1840. Returns:
  1841. The element.
  1842. */
  1843. empty: function(){
  1844. Garbage.trash(this.getElementsByTagName('*'));
  1845. return this.setHTML('');
  1846. }
  1847. });
  1848. Element.fixStyle = function(property, result, element){
  1849. if ($chk(parseInt(result))) return result;
  1850. if (['height', 'width'].contains(property)){
  1851. var values = (property == 'width') ? ['left', 'right'] : ['top', 'bottom'];
  1852. var size = 0;
  1853. values.each(function(value){
  1854. size += element.getStyle('border-' + value + '-width').toInt() + element.getStyle('padding-' + value).toInt();
  1855. });
  1856. return element['offset' + property.capitalize()] - size + 'px';
  1857. } else if (property.test(/border(.+)Width|margin|padding/)){
  1858. return '0px';
  1859. }
  1860. return result;
  1861. };
  1862. Element.Styles = {'border': [], 'padding': [], 'margin': []};
  1863. ['Top', 'Right', 'Bottom', 'Left'].each(function(direction){
  1864. for (var style in Element.Styles) Element.Styles[style].push(style + direction);
  1865. });
  1866. Element.borderShort = ['borderWidth', 'borderStyle', 'borderColor'];
  1867. Element.getMany = function(el, method, keys){
  1868. var result = {};
  1869. $each(keys, function(key){
  1870. result[key] = el[method](key);
  1871. });
  1872. return result;
  1873. };
  1874. Element.setMany = function(el, method, pairs){
  1875. for (var key in pairs) el[method](key, pairs[key]);
  1876. return el;
  1877. };
  1878. Element.Properties = new Abstract({
  1879. 'class': 'className', 'for': 'htmlFor', 'colspan': 'colSpan', 'rowspan': 'rowSpan',
  1880. 'accesskey': 'accessKey', 'tabindex': 'tabIndex', 'maxlength': 'maxLength',
  1881. 'readonly': 'readOnly', 'frameborder': 'frameBorder', 'value': 'value',
  1882. 'disabled': 'disabled', 'checked': 'checked', 'multiple': 'multiple', 'selected': 'selected'
  1883. });
  1884. Element.PropertiesIFlag = {
  1885. 'href': 2, 'src': 2
  1886. };
  1887. Element.Methods = {
  1888. Listeners: {
  1889. addListener: function(type, fn){
  1890. if (this.addEventListener) this.addEventListener(type, fn, false);
  1891. else this.attachEvent('on' + type, fn);
  1892. return this;
  1893. },
  1894. removeListener: function(type, fn){
  1895. if (this.removeEventListener) this.removeEventListener(type, fn, false);
  1896. else this.detachEvent('on' + type, fn);
  1897. return this;
  1898. }
  1899. }
  1900. };
  1901. window.extend(Element.Methods.Listeners);
  1902. document.extend(Element.Methods.Listeners);
  1903. Element.extend(Element.Methods.Listeners);
  1904. var Garbage = {
  1905. elements: [],
  1906. collect: function(el){
  1907. if (!el.$tmp){
  1908. Garbage.elements.push(el);
  1909. el.$tmp = {'opacity': 1};
  1910. }
  1911. return el;
  1912. },
  1913. trash: function(elements){
  1914. for (var i = 0, j = elements.length, el; i < j; i++){
  1915. if (!(el = elements[i]) || !el.$tmp) continue;
  1916. if (el.$events) el.fireEvent('trash').removeEvents();
  1917. for (var p in el.$tmp) el.$tmp[p] = null;
  1918. for (var d in Element.prototype) el[d] = null;
  1919. Garbage.elements[Garbage.elements.indexOf(el)] = null;
  1920. el.htmlElement = el.$tmp = el = null;
  1921. }
  1922. Garbage.elements.remove(null);
  1923. },
  1924. empty: function(){
  1925. Garbage.collect(window);
  1926. Garbage.collect(document);
  1927. Garbage.trash(Garbage.elements);
  1928. }
  1929. };
  1930. window.addListener('beforeunload', function(){
  1931. window.addListener('unload', Garbage.empty);
  1932. if (window.ie) window.addListener('unload', CollectGarbage);
  1933. });