right.js 143 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014
  1. /**
  2. * RightJS v2.2.3 - http://rightjs.org
  3. * Released under the terms of MIT license
  4. *
  5. * Copyright (C) 2008-2011 Nikolay Nemshilov
  6. */
  7. /**
  8. * The basic layout for RightJS builds
  9. *
  10. * Copyright (C) 2008-2011 Nikolay Nemshilov
  11. */
  12. var RightJS = (function(window, document, Object, Array, String, Function, Number, Math, undefined) {
  13. /**
  14. * The framework description object
  15. *
  16. * Copyright (C) 2008-2011 Nikolay Nemshilov
  17. */
  18. var RightJS = function(value) {
  19. return value; // <- a dummy method to emulate the safe-mode
  20. };
  21. RightJS.version = "2.2.3";
  22. RightJS.modules =["core", "dom", "form", "events", "xhr", "fx", "cookie"];
  23. /**
  24. * There are some util methods
  25. *
  26. * Credits:
  27. * Some of the functionality and names are inspired or copied from
  28. * - Prototype (http://prototypejs.org) Copyright (C) Sam Stephenson
  29. * - MooTools (http://mootools.net) Copyright (C) Valerio Proietti
  30. *
  31. * Copyright (C) 2008-2011 Nikolay V. Nemshilov
  32. */
  33. /**
  34. * Some top-level variables to shortify the things
  35. */
  36. var A_proto = Array.prototype,
  37. to_s = Object.prototype.toString, slice = A_proto.slice,
  38. HTML = document.documentElement, UID = 1, // !#server
  39. Wrappers_Cache = [], UID_KEY = 'uniqueNumber', // DON'T change the UID_KEY!
  40. /**
  41. * extends the first object with the keys and values of the second one
  42. *
  43. * NOTE: the third optional argument tells if the existing values
  44. * of the first object should _NOT_ get updated by the values of the second object
  45. *
  46. * @param oritinal Object destintation object
  47. * @param source Object source object
  48. * @param Boolean flag if the function should not overwrite intersecting values
  49. * @return Object extended destination object
  50. */
  51. $ext = RightJS.$ext = function(dest, source, dont_overwrite) {
  52. var src = source || {}, key;
  53. for (key in src) {
  54. if (!dont_overwrite || !(key in dest)) {
  55. dest[key] = src[key];
  56. }
  57. }
  58. return dest;
  59. },
  60. /** !#server
  61. * evals the given javascript text in the context of the current window
  62. *
  63. * @param String javascript
  64. * @return void
  65. */
  66. $eval = RightJS.$eval = function(text) {
  67. if (text) {
  68. if ('execScript' in window) {
  69. current_Document.win()._.execScript(text);
  70. } else {
  71. $E('script', {text: text}).insertTo(HTML);
  72. }
  73. }
  74. },
  75. /**
  76. * throws an exception to break iterations throw a callback
  77. *
  78. * @return void
  79. * @throws Break
  80. */
  81. $break = RightJS.$break = function() {
  82. throw new Break();
  83. },
  84. /**
  85. * generates aliases for the object properties
  86. *
  87. * @param object Object object
  88. * @param names Object aliases hash
  89. * @return Object the extended objects
  90. */
  91. $alias = RightJS.$alias = function(object, names) {
  92. for (var new_name in names) {
  93. object[new_name] = object[names[new_name]];
  94. }
  95. return object;
  96. },
  97. /**
  98. * checks if the given value or a reference points
  99. * to a really defined value
  100. *
  101. * NOTE: will return true for variables equal to null, false, 0, and so one.
  102. *
  103. * EXAMPLE:
  104. *
  105. * var smth = null;
  106. * defined(smth); <- will return true
  107. *
  108. * var obj = {};
  109. * defined(obj['smth']); <- will return false
  110. *
  111. * @param mixed value
  112. * @return boolean check result
  113. */
  114. defined = RightJS.defined = function(value) {
  115. return typeof(value) !== 'undefined';
  116. },
  117. /**
  118. * checks if the given value is a function
  119. *
  120. * @param mixed value
  121. * @return boolean check result
  122. */
  123. isFunction = RightJS.isFunction = function(value) {
  124. return typeof(value) === 'function';
  125. },
  126. /**
  127. * checks if the given value is a string
  128. *
  129. * @param mixed value
  130. * @return boolean check result
  131. */
  132. isString = RightJS.isString = function(value) {
  133. return typeof(value) === 'string';
  134. },
  135. /**
  136. * checks if the given value is a number
  137. *
  138. * @param mixed value to check
  139. * @return boolean check result
  140. */
  141. isNumber = RightJS.isNumber = function(value) {
  142. return typeof(value) === 'number';
  143. },
  144. /**
  145. * checks if the given value is a hash-like object
  146. *
  147. * @param mixed value
  148. * @return boolean check result
  149. */
  150. isHash = RightJS.isHash = function(value) {
  151. return to_s.call(value) === '[object Object]';
  152. },
  153. /**
  154. * checks if the given value is an array
  155. *
  156. * @param mixed value to check
  157. * @return boolean check result
  158. */
  159. isArray = RightJS.isArray = function(value) {
  160. return to_s.call(value) === '[object Array]';
  161. },
  162. /** !#server
  163. * checks if the given value is an element
  164. *
  165. * @param mixed value to check
  166. * @return boolean check result
  167. */
  168. isElement = RightJS.isElement = function(value) {
  169. return value != null && value.nodeType === 1;
  170. },
  171. /** !#server
  172. * checks if the given value is a DOM-node
  173. *
  174. * @param mixed value to check
  175. * @return boolean check result
  176. */
  177. isNode = RightJS.isNode = function(value) {
  178. return value != null && value.nodeType != null;
  179. },
  180. /** !#server
  181. * searches an element by id and/or extends it with the framework extentions
  182. *
  183. * @param String element id or Element to extend
  184. * @return Element or null
  185. */
  186. $ = RightJS.$ = function(object) {
  187. if (object instanceof Wrapper) {
  188. return object;
  189. } else if (typeof object === 'string') {
  190. object = document.getElementById(object);
  191. }
  192. return wrap(object);
  193. },
  194. /** !#server
  195. * Finds all the elements in the document by the given css_rule
  196. *
  197. * @param String element
  198. * @param Boolean raw search marker
  199. * @return Array search result
  200. */
  201. $$ = RightJS.$$ = function(css_rule, raw) {
  202. return current_Document.find(css_rule, raw);
  203. },
  204. /** !#server
  205. * shortcut to instance new elements
  206. *
  207. * @param String tag name
  208. * @param object options
  209. * @return Element instance
  210. */
  211. $E = RightJS.$E = function(tag_name, options) {
  212. return new Element(tag_name, options);
  213. },
  214. /**
  215. * shortcut, generates an array of words from a given string
  216. *
  217. * @param String string
  218. * @return Array of words
  219. */
  220. $w = RightJS.$w = function(string) {
  221. return string.trim().split(/\s+/);
  222. },
  223. /**
  224. * generates an unique id for an object
  225. *
  226. * @param Object object
  227. * @return Integer uniq id
  228. */
  229. $uid = RightJS.$uid = function(item) {
  230. return UID_KEY in item ? item[UID_KEY] : (item[UID_KEY] = UID++);
  231. },
  232. /**
  233. * converts any iterables into an array
  234. *
  235. * @param Object iterable
  236. * @return Array list
  237. */
  238. $A = RightJS.$A = function(it) {
  239. return slice.call(it, 0);
  240. };
  241. /** !#server
  242. * IE needs a patch for the $A function
  243. * because it doesn't handle all the cases
  244. */
  245. if (!A_proto.map) {
  246. $A = RightJS.$A = function(it) {
  247. try {
  248. return slice.call(it, 0);
  249. } catch(e) {
  250. for (var a=[], i=0, length = it.length; i < length; i++) {
  251. a[i] = it[i];
  252. }
  253. return a;
  254. }
  255. };
  256. }
  257. /** !#server
  258. * Internet Explorer needs some additional mumbo-jumbo in here
  259. */
  260. if (isHash(HTML)) {
  261. isHash = RightJS.isHash = function(value) {
  262. return to_s.call(value) === '[object Object]' &&
  263. value != null && value.hasOwnProperty != null;
  264. };
  265. }
  266. /**
  267. * Generating methods for native units extending
  268. */
  269. // adds a standard '.include' method to the native unit
  270. function extend_native(klass) {
  271. return $ext(klass, {
  272. Methods: {},
  273. include: function() {
  274. for (var i=0, l = arguments.length; i < l; i++) {
  275. if (isHash(arguments[i])) {
  276. $ext(klass.prototype, arguments[i]);
  277. $ext(klass.Methods, arguments[i]);
  278. }
  279. }
  280. }
  281. });
  282. }
  283. for (var i=0, natives = 'Array Function Number String Date RegExp'.split(' '); i < natives.length; i++) {
  284. RightJS[natives[i]] = extend_native(new Function('return '+ natives[i])());
  285. }
  286. // referring those two as well
  287. RightJS.Object = Object;
  288. RightJS.Math = Math;
  289. /**
  290. * Checks if the data is an array and if not,
  291. * then makes an array out of it
  292. *
  293. * @param mixed in data
  294. * @return Array data
  295. */
  296. function ensure_array(data) {
  297. return isArray(data) ? data : [data];
  298. }
  299. /**
  300. * The Object class extentions
  301. *
  302. * Credits:
  303. * Some functionality is inspired by
  304. * - Prototype (http://prototypejs.org) Copyright (C) Sam Stephenson
  305. *
  306. * Copyright (C) 2008-2011 Nikolay V. Nemshilov
  307. */
  308. $ext(Object, {
  309. /**
  310. * extracts the list of the attribute names of the given object
  311. *
  312. * @param Object object
  313. * @return Array keys list
  314. */
  315. keys: function(object) {
  316. var keys = [], key;
  317. for (key in object) {
  318. keys.push(key);
  319. }
  320. return keys;
  321. },
  322. /**
  323. * extracts the list of the attribute values of the given object
  324. *
  325. * @param Object object
  326. * @return Array values list
  327. */
  328. values: function(object) {
  329. var values = [], key;
  330. for (key in object) {
  331. values.push(object[key]);
  332. }
  333. return values;
  334. },
  335. /**
  336. * Calls the function with every key/value pair on the hash
  337. *
  338. * @param in Object the data hash
  339. * @param Function the callback
  340. * @param scope Object an optional scope
  341. * @return Object the original hash
  342. */
  343. each: function(object, callback, scope) {
  344. for (var key in object) {
  345. callback.call(scope, key, object[key]);
  346. }
  347. return object;
  348. },
  349. /**
  350. * checks if the object-hash has no keys
  351. *
  352. * @param Object object
  353. * @return check result
  354. */
  355. empty: function(object) {
  356. for (var key in object) { return false; }
  357. return true;
  358. },
  359. /**
  360. * A simple cloning method
  361. * NOTE: does not clone the things recoursively!
  362. *
  363. * @param Object object
  364. * @return Object clone
  365. */
  366. clone: function(object) {
  367. return Object.merge(object);
  368. },
  369. /**
  370. * returns a copy of the object which contains
  371. * all the same keys/values except the key-names
  372. * passed the the method arguments
  373. *
  374. * @param Object object
  375. * @param String key-name to exclude
  376. * .....
  377. * @return Object filtered copy
  378. */
  379. without: function() {
  380. var filter = $A(arguments), object = filter.shift(), copy = {}, key;
  381. for (key in object) {
  382. if (!filter.include(key)) {
  383. copy[key] = object[key];
  384. }
  385. }
  386. return copy;
  387. },
  388. /**
  389. * returns a copy of the object which contains all the
  390. * key/value pairs from the specified key-names list
  391. *
  392. * NOTE: if some key does not exists in the original object, it will be just skipped
  393. *
  394. * @param Object object
  395. * @param String key name to exclude
  396. * .....
  397. * @return Object filtered copy
  398. */
  399. only: function() {
  400. var filter = $A(arguments), object = filter.shift(), copy = {},
  401. i=0, length = filter.length;
  402. for (; i < length; i++) {
  403. if (filter[i] in object) {
  404. copy[filter[i]] = object[filter[i]];
  405. }
  406. }
  407. return copy;
  408. },
  409. /**
  410. * merges the given objects and returns the result
  411. *
  412. * NOTE this method _DO_NOT_ change the objects, it creates a new object
  413. * which conatins all the given ones.
  414. * if there is some keys introspections, the last object wins.
  415. * all non-object arguments will be omitted
  416. *
  417. * @param first Object object
  418. * @param second Object mixing
  419. * ......
  420. * @return Object merged object
  421. */
  422. merge: function() {
  423. var object = {}, i=0, args=arguments, key;
  424. for (l = args.length; i < l; i++) {
  425. if (isHash(args[i])) {
  426. for (key in args[i]) {
  427. object[key] = isHash(args[i][key]) && !(args[i][key] instanceof Class) ?
  428. Object.merge(key in object ? object[key] : {}, args[i][key]) : args[i][key];
  429. }
  430. }
  431. }
  432. return object;
  433. },
  434. /**
  435. * converts a hash-object into an equivalent url query string
  436. *
  437. * @param Object object
  438. * @return String query
  439. */
  440. toQueryString: function(object) {
  441. var tokens = [], key, value, encode = encodeURIComponent;
  442. for (key in object) {
  443. value = ensure_array(object[key]);
  444. for (var i=0, l = value.length; i < l; i++) {
  445. tokens.push(encode(key) +'='+ encode(value[i]));
  446. }
  447. }
  448. return tokens.join('&');
  449. }
  450. }, true);
  451. /**
  452. * here are the starndard Math object extends
  453. *
  454. * Credits:
  455. * The idea of random mehtod is taken from
  456. * - Ruby (http://www.ruby-lang.org) Copyright (C) Yukihiro Matsumoto
  457. *
  458. * Copyright (C) 2008-2010 Nikolay Nemshilov
  459. */
  460. var Math_old_random = Math.random;
  461. /**
  462. * the standard random method replacement, to make it more useful
  463. *
  464. * USE:
  465. * Math.random(); // original functionality, returns a float between 0 and 1
  466. * Math.random(10); // returns an integer between 0 and 10
  467. * Math.random(1,4); // returns an integer between 1 and 4
  468. *
  469. * @param min Integer minimum value if there's two arguments and maximum value if there's only one
  470. * @param max Integer maximum value
  471. * @return Float random between 0 and 1 if there's no arguments or an integer in the given range
  472. */
  473. Math.random = function(min, max) {
  474. if (arguments.length === 0) {
  475. return Math_old_random();
  476. } else if (arguments.length === 1) {
  477. max = min;
  478. min = 0;
  479. }
  480. return ~~(Math_old_random() * (max-min+1) + ~~min);
  481. };
  482. /**
  483. * The Array class extentions
  484. *
  485. * Credits:
  486. * Some of the functionality is inspired by
  487. * - Prototype (http://prototypejs.org) Copyright (C) Sam Stephenson
  488. * - Ruby (http://www.ruby-lang.org) Copyright (C) Yukihiro Matsumoto
  489. *
  490. * Copyright (C) 2008-2010 Nikolay Nemshilov
  491. */
  492. var original_sort = A_proto.sort,
  493. // JavaScript 1.6 methods recatching up or faking
  494. for_each = A_proto.forEach || function(callback, scope) {
  495. for (var i=0, l=this.length; i < l; i++) {
  496. callback.call(scope, this[i], i, this);
  497. }
  498. },
  499. filter = A_proto.filter || function(callback, scope) {
  500. for (var result=[], j=0, i=0, l=this.length; i < l; i++) {
  501. if (callback.call(scope, this[i], i, this)) {
  502. result[j++] = this[i];
  503. }
  504. }
  505. return result;
  506. },
  507. reject = function(callback, scope) {
  508. for (var result=[], j=0, i=0, l=this.length; i < l; i++) {
  509. if (!callback.call(scope, this[i], i, this)) {
  510. result[j++] = this[i];
  511. }
  512. }
  513. return result;
  514. },
  515. map = A_proto.map || function(callback, scope) {
  516. for (var result=[], i=0, l=this.length; i < l; i++) {
  517. result[i] = callback.call(scope, this[i], i, this);
  518. }
  519. return result;
  520. },
  521. some = A_proto.some || function(callback, scope) {
  522. for (var i=0, l=this.length; i < l; i++) {
  523. if (callback.call(scope, this[i], i, this)) {
  524. return true;
  525. }
  526. }
  527. return false;
  528. },
  529. every = A_proto.every || function(callback, scope) {
  530. for (var i=0, l=this.length; i < l; i++) {
  531. if (!callback.call(scope, this[i], i, this)) {
  532. return false;
  533. }
  534. }
  535. return true;
  536. },
  537. first = function(callback, scope) {
  538. for (var i=0, l=this.length; i < l; i++) {
  539. if (callback.call(scope, this[i], i, this)) {
  540. return this[i];
  541. }
  542. }
  543. return undefined;
  544. },
  545. last = function(callback, scope) {
  546. for (var i=this.length-1; i > -1; i--) {
  547. if (callback.call(scope, this[i], i, this)) {
  548. return this[i];
  549. }
  550. }
  551. return undefined;
  552. };
  553. //
  554. // RightJS callbacks magick preprocessing
  555. //
  556. // prepares a correct callback function
  557. function guess_callback(argsi, array) {
  558. var callback = argsi[0], args = slice.call(argsi, 1), scope = array, attr;
  559. if (typeof(callback) === 'string') {
  560. attr = callback;
  561. if (array.length !== 0 && typeof(array[0][attr]) === 'function') {
  562. callback = function(object) { return object[attr].apply(object, args); };
  563. } else {
  564. callback = function(object) { return object[attr]; };
  565. }
  566. } else {
  567. scope = args[0];
  568. }
  569. return [callback, scope];
  570. }
  571. // defining the manual break errors class
  572. function Break() {}
  573. // calls the given method with preprocessing the arguments
  574. function call_method(func, scope, args) {
  575. try {
  576. return func.apply(scope, guess_callback(args, scope));
  577. } catch(e) { if (!(e instanceof Break)) { throw(e); } }
  578. return undefined;
  579. }
  580. // checks the value as a boolean
  581. function boolean_check(i) {
  582. return !!i;
  583. }
  584. // default sorting callback
  585. function default_sort(a, b) {
  586. return a > b ? 1 : a < b ? -1 : 0;
  587. }
  588. Array.include({
  589. /**
  590. * IE fix
  591. * returns the index of the value in the array
  592. *
  593. * @param mixed value
  594. * @param Integer optional offset
  595. * @return Integer index or -1 if not found
  596. */
  597. indexOf: A_proto.indexOf || function(value, from) {
  598. for (var i=(from<0) ? Math.max(0, this.length+from) : from || 0, l=this.length; i < l; i++) {
  599. if (this[i] === value) {
  600. return i;
  601. }
  602. }
  603. return -1;
  604. },
  605. /**
  606. * IE fix
  607. * returns the last index of the value in the array
  608. *
  609. * @param mixed value
  610. * @return Integer index or -1 if not found
  611. */
  612. lastIndexOf: A_proto.lastIndexOf || function(value) {
  613. for (var i=this.length-1; i > -1; i--) {
  614. if (this[i] === value) {
  615. return i;
  616. }
  617. }
  618. return -1;
  619. },
  620. /**
  621. * returns the first element of the array
  622. *
  623. * @return mixed first element of the array
  624. */
  625. first: function() {
  626. return arguments.length ? call_method(first, this, arguments) : this[0];
  627. },
  628. /**
  629. * returns the last element of the array
  630. *
  631. * @return mixed last element of the array
  632. */
  633. last: function() {
  634. return arguments.length ? call_method(last, this, arguments) : this[this.length-1];
  635. },
  636. /**
  637. * returns a random item of the array
  638. *
  639. * @return mixed a random item
  640. */
  641. random: function() {
  642. return this.length === 0 ? undefined : this[Math.random(this.length-1)];
  643. },
  644. /**
  645. * returns the array size
  646. *
  647. * @return Integer the array size
  648. */
  649. size: function() {
  650. return this.length;
  651. },
  652. /**
  653. * cleans the array
  654. * @return Array this
  655. */
  656. clean: function() {
  657. this.length = 0;
  658. return this;
  659. },
  660. /**
  661. * checks if the array has no elements in it
  662. *
  663. * @return boolean check result
  664. */
  665. empty: function() {
  666. return this.length === 0;
  667. },
  668. /**
  669. * creates a copy of the given array
  670. *
  671. * @return Array copy of the array
  672. */
  673. clone: function() {
  674. return this.slice(0);
  675. },
  676. /**
  677. * calls the given callback function in the given scope for each element of the array
  678. *
  679. * @param Function callback
  680. * @param Object scope
  681. * @return Array this
  682. */
  683. each: function() {
  684. call_method(for_each, this, arguments);
  685. return this;
  686. },
  687. forEach: for_each,
  688. /**
  689. * creates a list of the array items converted in the given callback function
  690. *
  691. * @param Function callback
  692. * @param Object optional scope
  693. * @return Array collected
  694. */
  695. map: function() {
  696. return call_method(map, this, arguments);
  697. },
  698. /**
  699. * creates a list of the array items which are matched in the given callback function
  700. *
  701. * @param Function callback
  702. * @param Object optional scope
  703. * @return Array filtered copy
  704. */
  705. filter: function() {
  706. return call_method(filter, this, arguments);
  707. },
  708. /**
  709. * creates a list of the array items that are not matching the give callback function
  710. *
  711. * @param Function callback
  712. * @param Object optionl scope
  713. * @return Array filtered copy
  714. */
  715. reject: function() {
  716. return call_method(reject, this, arguments);
  717. },
  718. /**
  719. * checks if any of the array elements is logically true
  720. *
  721. * @param Function optional callback for checks
  722. * @param Object optional scope for the callback
  723. * @return boolean check result
  724. */
  725. some: function(value) {
  726. return call_method(some, this, value ? arguments : [boolean_check]);
  727. },
  728. /**
  729. * checks if all the array elements are logically true
  730. *
  731. * @param Function optional callback for checks
  732. * @param Object optional scope for the callback
  733. * @return Boolean check result
  734. */
  735. every: function(value) {
  736. return call_method(every, this, value ? arguments : [boolean_check]);
  737. },
  738. /**
  739. * applies the given lambda to each element in the array
  740. *
  741. * NOTE: changes the array by itself
  742. *
  743. * @param Function callback
  744. * @param Object optional scope
  745. * @return Array this
  746. */
  747. walk: function() {
  748. this.map.apply(this, arguments).forEach(function(value, i) { this[i] = value; }, this);
  749. return this;
  750. },
  751. /**
  752. * similar to the concat function but it adds only the values which are not on the list yet
  753. *
  754. * @param Array to merge
  755. * ....................
  756. * @return Array new merged
  757. */
  758. merge: function() {
  759. for (var copy = this.clone(), arg, i=0; i < arguments.length; i++) {
  760. arg = ensure_array(arguments[i]);
  761. for (var j=0; j < arg.length; j++) {
  762. if (copy.indexOf(arg[j]) == -1) {
  763. copy.push(arg[j]);
  764. }
  765. }
  766. }
  767. return copy;
  768. },
  769. /**
  770. * flats out complex array into a single dimension array
  771. *
  772. * @return Array flatten copy
  773. */
  774. flatten: function() {
  775. var copy = [];
  776. this.forEach(function(value) {
  777. if (isArray(value)) {
  778. copy = copy.concat(value.flatten());
  779. } else {
  780. copy.push(value);
  781. }
  782. });
  783. return copy;
  784. },
  785. /**
  786. * returns a copy of the array whithout any null or undefined values
  787. *
  788. * @return Array filtered version
  789. */
  790. compact: function() {
  791. return this.without(null, undefined);
  792. },
  793. /**
  794. * returns a copy of the array which contains only the unique values
  795. *
  796. * @return Array filtered copy
  797. */
  798. uniq: function() {
  799. return [].merge(this);
  800. },
  801. /**
  802. * checks if all of the given values
  803. * exists in the given array
  804. *
  805. * @param mixed value
  806. * ....
  807. * @return boolean check result
  808. */
  809. includes: function() {
  810. for (var i=0; i < arguments.length; i++) {
  811. if (this.indexOf(arguments[i]) === -1) {
  812. return false;
  813. }
  814. }
  815. return true;
  816. },
  817. /**
  818. * returns a copy of the array without the items passed as the arguments
  819. *
  820. * @param mixed value
  821. * ......
  822. * @return Array filtered copy
  823. */
  824. without: function() {
  825. var filter = slice.call(arguments);
  826. return this.filter(function(value) {
  827. return filter.indexOf(value) === -1;
  828. });
  829. },
  830. /**
  831. * Shuffles the array items in a random order
  832. *
  833. * @return Array shuffled version
  834. */
  835. shuffle: function() {
  836. var shuff = this.clone(), j, x, i = shuff.length;
  837. for (; i > 0; j = Math.random(i-1), x = shuff[--i], shuff[i] = shuff[j], shuff[j] = x) {}
  838. return shuff;
  839. },
  840. /**
  841. * Default sort fix for numeric values
  842. *
  843. * @param Function callback
  844. * @return Array self
  845. */
  846. sort: function(callback) {
  847. return original_sort.apply(this, (callback || !isNumber(this[0])) ? arguments : [default_sort]);
  848. },
  849. /**
  850. * sorts the array by running its items though a lambda or calling their attributes
  851. *
  852. * @param Function callback or attribute name
  853. * @param Object scope or attribute argument
  854. * @return Array sorted copy
  855. */
  856. sortBy: function() {
  857. var pair = guess_callback(arguments, this);
  858. return this.sort(function(a, b) {
  859. return default_sort(
  860. pair[0].call(pair[1], a),
  861. pair[0].call(pair[1], b)
  862. );
  863. });
  864. },
  865. /**
  866. * Returns the minimal value on the list
  867. *
  868. * @return Number minimal value
  869. */
  870. min: function() {
  871. return Math.min.apply(Math, this);
  872. },
  873. /**
  874. * Returns the maximal value
  875. *
  876. * @return Number maximal value
  877. */
  878. max: function() {
  879. return Math.max.apply(Math, this);
  880. },
  881. /**
  882. * Returns a summ of all the items on the list
  883. *
  884. * @return Number a summ of values on the list
  885. */
  886. sum: function() {
  887. for(var sum=0, i=0, l=this.length; i < l; sum += this[i++]) {}
  888. return sum;
  889. }
  890. });
  891. A_proto.include = A_proto.includes;
  892. /**
  893. * The String class extentions
  894. *
  895. * Credits:
  896. * Some of the functionality inspired by
  897. * - Prototype (http://prototypejs.org) Copyright (C) Sam Stephenson
  898. * The trim function taken from work of Steven Levithan
  899. * - http://blog.stevenlevithan.com/archives/faster-trim-javascript
  900. *
  901. * Copyright (C) 2008-2011 Nikolay V. Nemshilov
  902. */
  903. String.include({
  904. /**
  905. * checks if the string is an empty string
  906. *
  907. * @return boolean check result
  908. */
  909. empty: function() {
  910. return this == '';
  911. },
  912. /**
  913. * checks if the string contains only white-spaces
  914. *
  915. * @return boolean check result
  916. */
  917. blank: function() {
  918. return this == false;
  919. },
  920. /**
  921. * removes trailing whitespaces
  922. *
  923. * @return String trimmed version
  924. */
  925. trim: String.prototype.trim || function() {
  926. var str = this.replace(/^\s\s*/, ''), i = str.length;
  927. while ((/\s/).test(str.charAt(--i))) {}
  928. return str.slice(0, i + 1);
  929. },
  930. /**
  931. * returns a copy of the string with all the tags removed
  932. * @return String without tags
  933. */
  934. stripTags: function() {
  935. return this.replace(/<\/?[^>]+>/ig, '');
  936. },
  937. /**
  938. * removes all the scripts declarations out of the string
  939. * @param mixed option. If it equals true the scrips will be executed,
  940. * if a function the scripts will be passed in it
  941. * @return String without scripts
  942. */
  943. stripScripts: function(option) {
  944. var scripts = '', text = this.replace(
  945. /<script[^>]*>([\s\S]*?)<\/script>/img,
  946. function(match, source) {
  947. scripts += source + "\n";
  948. return '';
  949. }
  950. );
  951. if (option === true) {
  952. $eval(scripts);
  953. } else if (isFunction(option)) {
  954. option(scripts, text);
  955. }
  956. return text;
  957. },
  958. /**
  959. * extracts all the scripts out of the string
  960. *
  961. * @return String the extracted stcripts
  962. */
  963. extractScripts: function() {
  964. var scripts = '';
  965. this.stripScripts(function(s) { scripts = s; });
  966. return scripts;
  967. },
  968. /**
  969. * evals all the scripts in the string
  970. *
  971. * @return String self (unchanged version with scripts still in their place)
  972. */
  973. evalScripts: function() {
  974. this.stripScripts(true);
  975. return this;
  976. },
  977. /**
  978. * converts underscored or dasherized string to a camelized one
  979. * @returns String camelized version
  980. */
  981. camelize: function() {
  982. return this.replace(/(\-|_)+(.)?/g, function(match, dash, chr) {
  983. return chr ? chr.toUpperCase() : '';
  984. });
  985. },
  986. /**
  987. * converts a camelized or dasherized string into an underscored one
  988. * @return String underscored version
  989. */
  990. underscored: function() {
  991. return this.replace(/([a-z\d])([A-Z]+)/g, '$1_$2').replace(/\-/g, '_').toLowerCase();
  992. },
  993. /**
  994. * returns a capitalised version of the string
  995. *
  996. * @return String captialised version
  997. */
  998. capitalize: function() {
  999. return this.charAt(0).toUpperCase() + this.substring(1).toLowerCase();
  1000. },
  1001. /**
  1002. * checks if the string contains the given substring
  1003. *
  1004. * @param String string
  1005. * @return boolean check result
  1006. */
  1007. includes: function(string) {
  1008. return this.indexOf(string) != -1;
  1009. },
  1010. /**
  1011. * checks if the string starts with the given substring
  1012. *
  1013. * @param String string
  1014. * @param boolean ignore the letters case
  1015. * @return boolean check result
  1016. */
  1017. startsWith: function(string, ignorecase) {
  1018. return (ignorecase !== true ? this.indexOf(string) :
  1019. this.toLowerCase().indexOf(string.toLowerCase())
  1020. ) === 0;
  1021. },
  1022. /**
  1023. * checks if the string ends with the given substring
  1024. *
  1025. * @param String substring
  1026. * @param boolean ignore the letters case
  1027. * @return boolean check result
  1028. */
  1029. endsWith: function(string, ignorecase) {
  1030. return this.length - (
  1031. ignorecase !== true ? this.lastIndexOf(string) :
  1032. this.toLowerCase().lastIndexOf(string.toLowerCase())
  1033. ) === string.length;
  1034. },
  1035. /**
  1036. * converts the string to an integer value
  1037. * @param Integer base
  1038. * @return Integer or NaN
  1039. */
  1040. toInt: function(base) {
  1041. return parseInt(this, base === undefined ? 10 : base);
  1042. },
  1043. /**
  1044. * converts the string to a float value
  1045. * @param boolean flat if the method should not use a flexible matching
  1046. * @return Float or NaN
  1047. */
  1048. toFloat: function(strict) {
  1049. return parseFloat(strict === true ? this :
  1050. this.replace(',', '.').replace(/(\d)-(\d)/, '$1.$2'));
  1051. }
  1052. });
  1053. String.prototype.include = String.prototype.includes;
  1054. /**
  1055. * The Function class extentions
  1056. *
  1057. * Credits:
  1058. * Some of the functionality inspired by
  1059. * - Prototype (http://prototypejs.org) Copyright (C) Sam Stephenson
  1060. *
  1061. * Copyright (C) 2008-2011 Nikolay V. Nemshilov
  1062. */
  1063. Function.include({
  1064. /**
  1065. * binds the function to be executed in the given scope
  1066. *
  1067. * @param Object scope
  1068. * @param mixed optional curry (left) argument
  1069. * ....
  1070. * @return Function binded function
  1071. */
  1072. bind: function() {
  1073. var args = $A(arguments), scope = args.shift(), func = this;
  1074. return function() {
  1075. return func.apply(scope,
  1076. (args.length !== 0 || arguments.length !== 0) ?
  1077. args.concat($A(arguments)) : args
  1078. );
  1079. };
  1080. },
  1081. /**
  1082. * binds the function as an event listener to the given scope object
  1083. *
  1084. * @param Object scope
  1085. * @param mixed optional curry (left) argument
  1086. * .......
  1087. * @return Function binded function
  1088. */
  1089. bindAsEventListener: function() {
  1090. var args = $A(arguments), scope = args.shift(), func = this;
  1091. return function(event) {
  1092. return func.apply(scope, [event].concat(args).concat($A(arguments)));
  1093. };
  1094. },
  1095. /**
  1096. * allows you to put some curry in your cookery
  1097. *
  1098. * @param mixed value to curry
  1099. * ....
  1100. * @return Function curried function
  1101. */
  1102. curry: function() {
  1103. return this.bind.apply(this, [this].concat($A(arguments)));
  1104. },
  1105. /**
  1106. * The right side curry feature
  1107. *
  1108. * @param mixed value to curry
  1109. * ....
  1110. * @return Function curried function
  1111. */
  1112. rcurry: function() {
  1113. var curry = $A(arguments), func = this;
  1114. return function() {
  1115. return func.apply(func, $A(arguments).concat(curry));
  1116. };
  1117. },
  1118. /**
  1119. * delays the function execution
  1120. *
  1121. * @param Integer delay ms
  1122. * @param mixed value to curry
  1123. * .....
  1124. * @return Integer timeout marker
  1125. */
  1126. delay: function() {
  1127. var args = $A(arguments), timeout = args.shift(),
  1128. timer = new Number(setTimeout(this.bind.apply(this, [this].concat(args)), timeout));
  1129. timer.cancel = function() { clearTimeout(this); };
  1130. return timer;
  1131. },
  1132. /**
  1133. * creates a periodical execution of the function with the given timeout
  1134. *
  1135. * @param Integer delay ms
  1136. * @param mixed value to curry
  1137. * ...
  1138. * @return Ineger interval marker
  1139. */
  1140. periodical: function() {
  1141. var args = $A(arguments), timeout = args.shift(),
  1142. timer = new Number(setInterval(this.bind.apply(this, [this].concat(args)), timeout));
  1143. timer.stop = function() { clearInterval(this); };
  1144. return timer;
  1145. },
  1146. /**
  1147. * Chains the given function after the current one
  1148. *
  1149. * @param Function the next function
  1150. * @param mixed optional value to curry
  1151. * ......
  1152. * @return Function chained function
  1153. */
  1154. chain: function() {
  1155. var args = $A(arguments), func = args.shift(), current = this;
  1156. return function() {
  1157. var result = current.apply(current, arguments);
  1158. func.apply(func, args);
  1159. return result;
  1160. };
  1161. }
  1162. });
  1163. /**
  1164. * The Number class extentions
  1165. *
  1166. * Credits:
  1167. * Some methods inspired by
  1168. * - Ruby (http://www.ruby-lang.org) Copyright (C) Yukihiro Matsumoto
  1169. *
  1170. * Copyright (C) 2008-2010 Nikolay V. Nemshilov
  1171. */
  1172. Number.include({
  1173. /**
  1174. * executes the given callback the given number of times
  1175. *
  1176. * @param Function callback
  1177. * @param Object optional callback execution scope
  1178. * @return void
  1179. */
  1180. times: function(callback, scope) {
  1181. for (var i=0; i < this; i++) {
  1182. callback.call(scope, i);
  1183. }
  1184. return this;
  1185. },
  1186. upto: function(number, callback, scope) {
  1187. for (var i=this+0; i <= number; i++) {
  1188. callback.call(scope, i);
  1189. }
  1190. return this;
  1191. },
  1192. downto: function(number, callback, scope) {
  1193. for (var i=this+0; i >= number; i--) {
  1194. callback.call(scope, i);
  1195. }
  1196. return this;
  1197. },
  1198. abs: function() {
  1199. return Math.abs(this);
  1200. },
  1201. round: function(size) {
  1202. return size ? parseFloat(this.toFixed(size)) : Math.round(this);
  1203. },
  1204. ceil: function() {
  1205. return Math.ceil(this);
  1206. },
  1207. floor: function() {
  1208. return Math.floor(this);
  1209. },
  1210. min: function(value) {
  1211. return this < value ? value : this + 0;
  1212. },
  1213. max: function(value) {
  1214. return this > value ? value : this + 0;
  1215. }
  1216. });
  1217. /**
  1218. * The Regexp class extentions
  1219. *
  1220. * Credits:
  1221. * Inspired by
  1222. * - Prototype (http://prototypejs.org) Copyright (C) Sam Stephenson
  1223. *
  1224. * Copyright (C) 2008-2010 Nikolay V. Nemshilov
  1225. */
  1226. /**
  1227. * Escapes the string for safely use as a regular expression
  1228. *
  1229. * @param String raw string
  1230. * @return String escaped string
  1231. */
  1232. RegExp.escape = function(string) {
  1233. return (''+string).replace(/([.*+?\^=!:${}()|\[\]\/\\])/g, '\\$1');
  1234. };
  1235. /**
  1236. * The basic Class unit
  1237. *
  1238. * Credits:
  1239. * The Class unit is inspired by its implementation in
  1240. * - Prototype (http://prototypejs.org) Copyright (C) Sam Stephenson
  1241. * - MooTools (http://mootools.net) Copyright (C) Valerio Proietti
  1242. * - Ruby (http://www.ruby-lang.org) Copyright (C) Yukihiro Matsumoto
  1243. *
  1244. * Copyright (C) 2008-2011 Nikolay Nemshilov
  1245. */
  1246. var Class = RightJS.Class = function() {
  1247. var args = $A(arguments).slice(0,2),
  1248. props = args.pop() || {},
  1249. parent = args.pop(),
  1250. klass = arguments[2], // you can send your own klass as the third argument
  1251. SKlass = function() {};
  1252. // if the parent class only was specified
  1253. if (!args.length && !isHash(props)) {
  1254. parent = props; props = {};
  1255. }
  1256. // !#server:begin
  1257. if (!klass && parent && (parent === Wrapper || parent.ancestors.include(Wrapper))) {
  1258. klass = Wrapper_makeKlass();
  1259. }
  1260. // !#server:end
  1261. // defining the basic klass function
  1262. klass = $ext(klass || function() {
  1263. Class_checkPrebind(this);
  1264. return 'initialize' in this ?
  1265. this.initialize.apply(this, arguments) :
  1266. this;
  1267. }, Class_Methods);
  1268. // handling the inheritance
  1269. parent = parent || Class;
  1270. SKlass.prototype = parent.prototype;
  1271. klass.prototype = new SKlass();
  1272. klass.parent = parent;
  1273. klass.prototype.constructor = klass;
  1274. // collecting the list of ancestors
  1275. klass.ancestors = [];
  1276. while (parent) {
  1277. klass.ancestors.push(parent);
  1278. parent = parent.parent;
  1279. }
  1280. // handling the module injections
  1281. ['extend', 'include'].each(function(name) {
  1282. if (name in props) {
  1283. klass[name].apply(klass, ensure_array(props[name]));
  1284. }
  1285. });
  1286. return klass.include(props);
  1287. },
  1288. /**
  1289. * Class utility methods
  1290. *
  1291. * Copyright (C) 2008-2011 Nikolay Nemshilov
  1292. */
  1293. Class_Methods = {
  1294. /**
  1295. * this method will extend the class-level with the given objects
  1296. *
  1297. * NOTE: this method _WILL_OVERWRITE_ the existing itercecting entries
  1298. *
  1299. * NOTE: this method _WILL_NOT_OVERWRITE_ the class prototype and
  1300. * the class 'name' and 'parent' attributes. If one of those
  1301. * exists in one of the received modeuls, the attribute will be
  1302. * skipped
  1303. *
  1304. * @param Object module to extend
  1305. * ....
  1306. * @return Class the klass
  1307. */
  1308. extend: function() {
  1309. $A(arguments).filter(isHash).each(function(module) {
  1310. $ext(this, Class_clean_module(module, true));
  1311. Class_handle_module_callbacks(this, module, true);
  1312. }, this);
  1313. return this;
  1314. },
  1315. /**
  1316. * extends the class prototype with the given objects
  1317. * NOTE: this method _WILL_OVERWRITE_ the existing itercecting entries
  1318. * NOTE: this method _WILL_NOT_OVERWRITE_ the 'klass' attribute of the klass.prototype
  1319. *
  1320. * @param Object module to include
  1321. * ....
  1322. * @return Class the klass
  1323. */
  1324. include: function() {
  1325. var klasses = [this].concat(this.ancestors);
  1326. $A(arguments).filter(isHash).each(function(module) {
  1327. Object.each(Class_clean_module(module, false), function(name, method) {
  1328. // searching for the super-method
  1329. for (var super_method, i=0, l = klasses.length; i < l; i++) {
  1330. if (name in klasses[i].prototype) {
  1331. super_method = klasses[i].prototype[name];
  1332. break;
  1333. }
  1334. }
  1335. this.prototype[name] = isFunction(method) && isFunction(super_method) ?
  1336. function() {
  1337. this.$super = super_method;
  1338. return method.apply(this, arguments);
  1339. } : method;
  1340. }, this);
  1341. Class_handle_module_callbacks(this, module, false);
  1342. }, this);
  1343. return this;
  1344. }
  1345. },
  1346. Class_module_callback_names = $w(
  1347. 'selfExtended self_extended selfIncluded self_included extend include'
  1348. );
  1349. // hooking up the class-methods to the root class
  1350. $ext(Class, Class_Methods);
  1351. Class.prototype.$super = undefined;
  1352. function Class_clean_module(module, extend) {
  1353. return Object.without.apply(Object, [module].concat(
  1354. Class_module_callback_names.concat( extend ?
  1355. $w('prototype parent ancestors') : ['constructor']
  1356. )
  1357. ));
  1358. }
  1359. function Class_handle_module_callbacks(klass, module, extend) {
  1360. (module[Class_module_callback_names[extend ? 0 : 2]] ||
  1361. module[Class_module_callback_names[extend ? 1 : 3]] ||
  1362. function() {}
  1363. ).call(module, klass);
  1364. }
  1365. /**
  1366. * This method gets through a list of the object its class and all the ancestors
  1367. * and finds a hash named after property, used for configuration purposes with
  1368. * the Observer and Options modules
  1369. *
  1370. * NOTE: this method will look for capitalized and uppercased versions of the
  1371. * property name
  1372. *
  1373. * @param Object a class instance
  1374. * @param String property name
  1375. * @return Object hash or null if nothing found
  1376. */
  1377. function Class_findSet(object, property) {
  1378. var upcased = property.toUpperCase(),
  1379. constructor = object.constructor,
  1380. candidates = [object, constructor].concat(constructor.ancestors || []),
  1381. i = 0;
  1382. for (l = candidates.length; i < l; i++) {
  1383. if (upcased in candidates[i]) {
  1384. return candidates[i][upcased];
  1385. } else if (property in candidates[i]) {
  1386. return candidates[i][property];
  1387. }
  1388. }
  1389. return null;
  1390. }
  1391. /**
  1392. * Handles the 'prebind' feature for Class instances
  1393. *
  1394. * @param Class instance
  1395. * @return void
  1396. */
  1397. function Class_checkPrebind(object) {
  1398. if ('prebind' in object && isArray(object.prebind)) {
  1399. object.prebind.each(function(method) {
  1400. object[method] = object[method].bind(object);
  1401. });
  1402. }
  1403. }
  1404. /**
  1405. * This is a simple mix-in module to be included in other classes
  1406. *
  1407. * Basically it privdes the <tt>setOptions</tt> method which processes
  1408. * an instance options assigment and merging with the default options
  1409. *
  1410. * Credits:
  1411. * The idea of the module is inspired by
  1412. * - MooTools (http://mootools.net) Copyright (C) Valerio Proietti
  1413. *
  1414. * Copyright (C) 2008-2011 Nikolay V. Nemshilov
  1415. */
  1416. var Options = RightJS.Options = {
  1417. /**
  1418. * assigns the options by merging them with the default ones
  1419. *
  1420. * @param Object options
  1421. * @return Object current instance
  1422. */
  1423. setOptions: function(opts) {
  1424. var options = this.options = $ext($ext({},
  1425. Object.clone(Class_findSet(this, 'Options'))), opts
  1426. ), match, key;
  1427. // hooking up the observer options
  1428. if (isFunction(this.on)) {
  1429. for (key in options) {
  1430. if ((match = key.match(/on([A-Z][A-Za-z]+)/))) {
  1431. this.on(match[1].toLowerCase(), options[key]);
  1432. delete(options[key]);
  1433. }
  1434. }
  1435. }
  1436. return this;
  1437. },
  1438. /**
  1439. * Cuts of an options hash from the end of the arguments list
  1440. * assigns them using the #setOptions method and then
  1441. * returns the list of other arguments as an Array instance
  1442. *
  1443. * @param mixed iterable
  1444. * @return Array of the arguments
  1445. */
  1446. cutOptions: function(in_args) {
  1447. var args = $A(in_args);
  1448. this.setOptions(isHash(args.last()) ? args.pop() : {});
  1449. return args;
  1450. }
  1451. };
  1452. /**
  1453. * standard Observer class.
  1454. *
  1455. * Might be used as a usual class or as a builder over another objects
  1456. *
  1457. * Credits:
  1458. * The naming principle is inspired by
  1459. * - Prototype (http://prototypejs.org) Copyright (C) Sam Stephenson
  1460. *
  1461. * Copyright (C) 2008-2011 Nikolay Nemshilov
  1462. */
  1463. var Observer = RightJS.Observer = new Class({
  1464. include: Options,
  1465. /**
  1466. * general constructor
  1467. *
  1468. * @param Object options
  1469. */
  1470. initialize: function(options) {
  1471. this.setOptions(options);
  1472. Observer_createShortcuts(this, Class_findSet(this, 'Events'));
  1473. return this;
  1474. },
  1475. /**
  1476. * binds an event listener
  1477. *
  1478. * USAGE:
  1479. * on(String event, Function callback[, arguments, ...]);
  1480. * on(String event, String method_name[, arguments, ...]);
  1481. * on(Object events_hash);
  1482. *
  1483. * @return Observer self
  1484. */
  1485. on: function() {
  1486. Observer_on(this, arguments, function(h) { return h; });
  1487. return this;
  1488. },
  1489. /**
  1490. * checks if the observer observes given event and/or callback
  1491. *
  1492. * USAGE:
  1493. * observes(String event)
  1494. * observes(Function callback)
  1495. * observes(String event, Function callback)
  1496. *
  1497. * @retun boolean check result
  1498. */
  1499. observes: function(event, callback) {
  1500. if (!isString(event)) { callback = event; event = null; }
  1501. if (isString(callback)) { callback = callback in this ? this[callback] : null; }
  1502. return (this.$listeners || []).some(function(i) {
  1503. return (event && callback) ? i.e === event && i.f === callback :
  1504. event ? i.e === event : i.f === callback;
  1505. });
  1506. },
  1507. /**
  1508. * stops observing an event or/and function
  1509. *
  1510. * USAGE:
  1511. * stopObserving(String event)
  1512. * stopObserving(Function callback)
  1513. * stopObserving(String event, Function callback)
  1514. *
  1515. * @return Observer self
  1516. */
  1517. stopObserving: function(event, callback) {
  1518. Observer_stopObserving(this, event, callback, function() {});
  1519. return this;
  1520. },
  1521. /**
  1522. * returns the listeners list for the event
  1523. *
  1524. * NOTE: if no event was specified the method will return _all_
  1525. * event listeners for _all_ the events
  1526. *
  1527. * @param String event name
  1528. * @return Array of listeners
  1529. */
  1530. listeners: function(event) {
  1531. return (this.$listeners || []).filter(function(i) {
  1532. return !event || i.e === event;
  1533. }).map(function(i) { return i.f; }).uniq();
  1534. },
  1535. /**
  1536. * initiates the event handling
  1537. *
  1538. * @param String event name
  1539. * @param mixed optional argument
  1540. * ........
  1541. * @return Observer self
  1542. */
  1543. fire: function() {
  1544. var args = $A(arguments), event = args.shift();
  1545. (this.$listeners || []).each(function(i) {
  1546. if (i.e === event) {
  1547. i.f.apply(this, i.a.concat(args));
  1548. }
  1549. }, this);
  1550. return this;
  1551. }
  1552. }),
  1553. /**
  1554. * adds an observer functionality to any object
  1555. *
  1556. * @param Object object
  1557. * @param Array optional events list to build shortcuts
  1558. * @return Object extended object
  1559. */
  1560. Observer_create = Observer.create = function(object, events) {
  1561. $ext(object, Object.without(Observer.prototype, 'initialize', 'setOptions'), true);
  1562. return Observer_createShortcuts(object, events || Class_findSet(object, 'Events'));
  1563. },
  1564. /**
  1565. * builds shortcut methods to wire/fire events on the object
  1566. *
  1567. * @param Object object to extend
  1568. * @param Array list of event names
  1569. * @return Object extended object
  1570. */
  1571. Observer_createShortcuts = Observer.createShortcuts = function(object, names) {
  1572. (names || []).each(function(name) {
  1573. var method_name = 'on'+name.replace(/(^|_|:)([a-z])/g,
  1574. function(match, pre, chr) { return chr.toUpperCase(); }
  1575. );
  1576. if (!(method_name in object)) {
  1577. object[method_name] = function() {
  1578. return this.on.apply(this, [name].concat($A(arguments)));
  1579. };
  1580. }
  1581. });
  1582. return object;
  1583. };
  1584. function Observer_on(object, o_args, preprocess) {
  1585. var args = slice.call(o_args, 2),
  1586. event = o_args[0],
  1587. callback = o_args[1],
  1588. name = false;
  1589. if (isString(event)) {
  1590. switch (typeof callback) {
  1591. case "string":
  1592. name = callback;
  1593. callback = callback in object ? object[callback] : function() {};
  1594. case "function":
  1595. ('$listeners' in object ? object.$listeners : (
  1596. object.$listeners = []
  1597. )).push(preprocess({
  1598. e: event, f: callback, a: args, r: name || false, t: object
  1599. }));
  1600. break;
  1601. default:
  1602. if (isArray(callback)) {
  1603. for (var i=0; i < callback.length; i++) {
  1604. object.on.apply(object, [event].concat(
  1605. ensure_array(callback[i])
  1606. ).concat(args));
  1607. }
  1608. }
  1609. }
  1610. } else {
  1611. // assuming it's a hash of key-value pairs
  1612. args = slice.call(o_args, 1);
  1613. for (name in event) {
  1614. object.on.apply(object, [name].concat(
  1615. ensure_array(event[name])
  1616. ).concat(args));
  1617. }
  1618. }
  1619. }
  1620. function Observer_stopObserving(object, event, callback, preprocess) {
  1621. if (isHash(event)) {
  1622. for (var key in event) {
  1623. object.stopObserving(key, event[key]);
  1624. }
  1625. } else {
  1626. if (!isString(event)) { callback = event; event = null; }
  1627. if (isString(callback)){ callback = object[callback]; }
  1628. object.$listeners = (object.$listeners || []).filter(function(i) {
  1629. var result = (event && callback) ?
  1630. (i.e !== event || i.f !== callback) :
  1631. (event ? i.e !== event : i.f !== callback);
  1632. if (!result) { preprocess(i); }
  1633. return result;
  1634. });
  1635. }
  1636. }
  1637. /**
  1638. * this object will contain info about the current browser
  1639. *
  1640. * Copyright (C) 2008-2011 Nikolay V. Nemshilov
  1641. */
  1642. var agent = navigator.userAgent,
  1643. Browser_Opera = 'opera' in window,
  1644. Browser_IE = 'attachEvent' in window && !Browser_Opera,
  1645. Browser = RightJS.Browser = {
  1646. IE: Browser_IE,
  1647. Opera: Browser_Opera,
  1648. WebKit: agent.include('AppleWebKit/'),
  1649. Gecko: agent.include('Gecko') && !agent.include('KHTML'),
  1650. MobileSafari: /Apple.*Mobile.*Safari/.test(agent),
  1651. Konqueror: agent.include('Konqueror'),
  1652. // internal marker for the browsers which require the olds module
  1653. OLD: !document.querySelector,
  1654. // internal marker for IE browsers version <= 8
  1655. IE8L: false
  1656. },
  1657. IE8_OR_LESS = false,
  1658. IE_OPACITY = !('opacity' in HTML.style) && ('filter' in HTML.style);
  1659. try {
  1660. // checking if that an IE version <= 8
  1661. document.createElement('<input/>');
  1662. Browser.OLD = Browser.IE8L = IE8_OR_LESS = true;
  1663. } catch(e) {}
  1664. /**
  1665. * The dom-wrapper main unit
  1666. *
  1667. * This unit is basically for the internal use
  1668. * so that we could control the common functionality
  1669. * among all the wrappers
  1670. *
  1671. * Copyright (C) 2010-2011 Nikolay Nemshilov
  1672. */
  1673. var Wrapper = RightJS.Wrapper = new Class({
  1674. // predefining the property in the prototype
  1675. _: undefined,
  1676. /**
  1677. * Default constructor
  1678. *
  1679. * @param mixed raw dom unit
  1680. * @return void
  1681. */
  1682. initialize: function(raw_object) {
  1683. this._ = raw_object;
  1684. }
  1685. });
  1686. // exposing the cache so it could be manupulated externally
  1687. Wrapper.Cache = Wrappers_Cache;
  1688. // instantiating the actual class object for a wrapper
  1689. function Wrapper_makeKlass() {
  1690. /**
  1691. * Default wrappers Klass function
  1692. *
  1693. * @param mixed the raw object
  1694. * @param Object options
  1695. * @return void
  1696. */
  1697. return function(object, options) {
  1698. Class_checkPrebind(this);
  1699. this.initialize.apply(this, arguments); // <- there might be a different number of args in a subclass
  1700. var item = this._, uid = UID_KEY in item ? item[UID_KEY] :
  1701. // NOTE we use positive indexes for dom-elements and negative for everything else
  1702. (item[UID_KEY] = (item.nodeType === 1 ? 1 : -1) * UID++);
  1703. Wrappers_Cache[uid] = this;
  1704. };
  1705. }
  1706. /**
  1707. * Element's own Klass function
  1708. * we need that because it does some dynamic typecasting mumbo jumbo
  1709. * plus we would like to optimize some stuff here and there
  1710. *
  1711. * @param raw dom element or the tag name
  1712. * @param Object options
  1713. * @return Element instance
  1714. */
  1715. function Element_Klass(element, options) {
  1716. Element_initialize(this, element, options);
  1717. var inst = this, raw = inst._, cast = Wrapper.Cast(raw),
  1718. uid = UID_KEY in raw ? raw[UID_KEY] : (raw[UID_KEY] = UID++);
  1719. if (cast !== undefined) {
  1720. inst = new cast(raw, options);
  1721. if ('$listeners' in this) {
  1722. inst.$listeners = this.$listeners;
  1723. }
  1724. }
  1725. Wrappers_Cache[uid] = inst;
  1726. return inst;
  1727. }
  1728. // searches for a suitable class for dynamic typecasting
  1729. Wrapper.Cast = function(unit) {
  1730. return unit.tagName in Element_wrappers ? Element_wrappers[unit.tagName] : undefined;
  1731. };
  1732. /**
  1733. * Event's own Klass function, we don't need to check
  1734. * nothing in here, don't need to hit the wrappers cache and so one
  1735. *
  1736. * @param raw dom-event or a string event-name
  1737. * @param bounding element or an object with options
  1738. * @return void
  1739. */
  1740. function Event_Klass(event, bound_element) {
  1741. if (typeof(event) === 'string') {
  1742. event = $ext({type: event}, bound_element);
  1743. this.stopped = event.bubbles === false;
  1744. if (isHash(bound_element)) {
  1745. $ext(this, bound_element);
  1746. }
  1747. }
  1748. this._ = event;
  1749. this.type = event.type;
  1750. this.which = event.which;
  1751. this.keyCode = event.keyCode;
  1752. this.target = wrap(
  1753. // Webkit throws events on textual nodes as well, gotta fix that
  1754. event.target != null && 'nodeType' in event.target && event.target.nodeType === 3 ?
  1755. event.target.parentNode : event.target
  1756. );
  1757. this.currentTarget = wrap(event.currentTarget);
  1758. this.relatedTarget = wrap(event.relatedTarget);
  1759. this.pageX = event.pageX;
  1760. this.pageY = event.pageY;
  1761. // making old IE attrs looks like w3c standards
  1762. if (IE8_OR_LESS && 'srcElement' in event) {
  1763. this.which = event.button === 2 ? 3 : event.button === 4 ? 2 : 1;
  1764. this.target = wrap(event.srcElement) || bound_element;
  1765. this.relatedTarget = this.target._ === event.fromElement ? wrap(event.toElement) : this.target;
  1766. this.currentTarget = bound_element;
  1767. var scrolls = this.target.win().scrolls();
  1768. this.pageX = event.clientX + scrolls.x;
  1769. this.pageY = event.clientY + scrolls.y;
  1770. }
  1771. }
  1772. /**
  1773. * Private quick wrapping function, unlike `$`
  1774. * it doesn't search by ID and handle double-wrapps
  1775. * just pure dom-wrapping functionality
  1776. *
  1777. * @param raw dom unit
  1778. * @return Wrapper dom-wrapper
  1779. */
  1780. function wrap(object) {
  1781. if (object != null) {
  1782. var wrapper = UID_KEY in object ? Wrappers_Cache[object[UID_KEY]] : undefined;
  1783. if (wrapper !== undefined) {
  1784. return wrapper;
  1785. } else if (object.nodeType === 1) {
  1786. return new Element(object);
  1787. } else if (object.nodeType === 9) {
  1788. return new Document(object);
  1789. } else if (object.window == object) {
  1790. return new Window(object);
  1791. } else if (isElement(object.target) || isElement(object.srcElement)) {
  1792. return new Event(object);
  1793. }
  1794. }
  1795. return object;
  1796. }
  1797. /**
  1798. * A simple document wrapper
  1799. *
  1800. * Copyright (C) 2010-2011 Nikolay Nemshilov
  1801. */
  1802. var Document = RightJS.Document = new Class(Wrapper, {
  1803. // returns the window reference
  1804. win: function() {
  1805. return wrap(this._.defaultView || this._.parentWindow);
  1806. }
  1807. }),
  1808. // a common local wrapped document reference
  1809. current_Document = wrap(document);
  1810. /**
  1811. * the window object extensions
  1812. *
  1813. * Copyright (C) 2008-2011 Nikolay Nemshilov
  1814. */
  1815. var Window = RightJS.Window = new Class(Wrapper, {
  1816. /**
  1817. * Selfreference to have a common interface with the rest of the wrappers
  1818. * in case of events handling
  1819. *
  1820. * @return Window
  1821. */
  1822. win: function() {
  1823. return this;
  1824. },
  1825. /**
  1826. * returns the inner-size of the window
  1827. *
  1828. * @return Object x: d+, y: d+
  1829. */
  1830. size: function() {
  1831. var win = this._, html = win.document.documentElement;
  1832. return win.innerWidth ? {x: win.innerWidth, y: win.innerHeight} :
  1833. {x: html.clientWidth, y: html.clientHeight};
  1834. },
  1835. /**
  1836. * returns the scrolls for the window
  1837. *
  1838. * @return Object x: d+, y: d+
  1839. */
  1840. scrolls: function() {
  1841. var win = this._, doc = win.document, body = doc.body, html = doc.documentElement;
  1842. return (win.pageXOffset || win.pageYOffset) ? {x: win.pageXOffset, y: win.pageYOffset} :
  1843. (body && (body.scrollLeft || body.scrollTop)) ? {x: body.scrollLeft, y: body.scrollTop} :
  1844. {x: html.scrollLeft, y: html.scrollTop};
  1845. },
  1846. /**
  1847. * overloading the native scrollTo method to support hashes and element references
  1848. *
  1849. * @param mixed number left position, a hash position, element or a string element id
  1850. * @param number top position
  1851. * @param Object fx options
  1852. * @return window self
  1853. */
  1854. scrollTo: function(left, top, fx_options) {
  1855. var left_pos = left, top_pos = top,
  1856. element = isNumber(left) ? null : $(left);
  1857. if(element instanceof Element) {
  1858. left = element.position();
  1859. }
  1860. if (isHash(left)) {
  1861. top_pos = left.y;
  1862. left_pos = left.x;
  1863. }
  1864. // checking if a smooth scroll was requested
  1865. if (isHash(fx_options = fx_options || top) && RightJS.Fx) {
  1866. new Fx.Scroll(this, fx_options).start({x: left_pos, y: top_pos});
  1867. } else {
  1868. this._.scrollTo(left_pos, top_pos);
  1869. }
  1870. return this;
  1871. }
  1872. });
  1873. /**
  1874. * represents some additional functionality for the Event class
  1875. *
  1876. * NOTE: there more additional functionality for the Event class in the rightjs-goods project
  1877. *
  1878. * Credits:
  1879. * The additional method names are inspired by
  1880. * - Prototype (http://prototypejs.org) Copyright (C) Sam Stephenson
  1881. *
  1882. * Copyright (C) 2008-2011 Nikolay Nemshilov
  1883. */
  1884. var Event = RightJS.Event = new Class(Wrapper, {
  1885. // predefining the keys to spped up the assignments
  1886. type: null,
  1887. which: null,
  1888. keyCode: null,
  1889. target: null,
  1890. currentTarget: null,
  1891. relatedTarget: null,
  1892. pageX: null,
  1893. pageY: null,
  1894. /**
  1895. * the class constructor
  1896. *
  1897. * @param raw dom-event
  1898. * @param HTMLElement the bound element
  1899. * @return void
  1900. */
  1901. initialize: Event_Klass, // the actual initialization happens in the Klass function
  1902. /**
  1903. * Stops the event bubbling process
  1904. *
  1905. * @return RightJS.Event this
  1906. */
  1907. stopPropagation: function() {
  1908. if (this._.stopPropagation) {
  1909. this._.stopPropagation();
  1910. } else {
  1911. this._.cancelBubble = true;
  1912. }
  1913. this.stopped = true;
  1914. return this;
  1915. },
  1916. /**
  1917. * Prevents the default browser action on the event
  1918. *
  1919. * @return RightJS.Event this
  1920. */
  1921. preventDefault: function() {
  1922. if (this._.preventDefault) {
  1923. this._.preventDefault();
  1924. } else {
  1925. this._.returnValue = false;
  1926. }
  1927. return this;
  1928. },
  1929. /**
  1930. * Fully stops the event
  1931. *
  1932. * @return RightJS.Event this
  1933. */
  1934. stop: function() {
  1935. return this.stopPropagation().preventDefault();
  1936. },
  1937. /**
  1938. * Returns the event position
  1939. *
  1940. * @return Object {x: ..., y: ...}
  1941. */
  1942. position: function() {
  1943. return {x: this.pageX, y: this.pageY};
  1944. },
  1945. /**
  1946. * Returns the event's offset relative to the target element
  1947. *
  1948. * @return Object {x: ..., y: ...} or null
  1949. */
  1950. offset: function() {
  1951. if(this.target instanceof Element) {
  1952. var element_position = this.target.position();
  1953. return {
  1954. x: this.pageX - element_position.x,
  1955. y: this.pageY - element_position.y
  1956. };
  1957. }
  1958. // triggered outside browser window (at toolbar etc.)
  1959. return null;
  1960. },
  1961. /**
  1962. * Finds the element between the event target
  1963. * and the boundary element that matches the
  1964. * css-rule
  1965. *
  1966. * @param String css-rule
  1967. * @return Element element or null
  1968. */
  1969. find: function(css_rule) {
  1970. if (this.target instanceof Wrapper && this.currentTarget instanceof Wrapper) {
  1971. var target = this.target._,
  1972. search = this.currentTarget.find(css_rule, true);
  1973. while (target) {
  1974. if (search.indexOf(target) !== -1) {
  1975. return wrap(target);
  1976. }
  1977. target = target.parentNode;
  1978. }
  1979. }
  1980. return undefined;
  1981. }
  1982. }, Event_Klass),
  1983. Event_delegation_shortcuts = [];
  1984. /**
  1985. * The DOM Element unit handling
  1986. *
  1987. * Copyright (C) 2008-2011 Nikolay Nemshilov
  1988. */
  1989. var Element = RightJS.Element = new Class(Wrapper, {
  1990. /**
  1991. * constructor
  1992. *
  1993. * NOTE: this constructor will dynamically typecast
  1994. * the wrappers depending on the element tag-name
  1995. *
  1996. * @param String element tag name or an HTMLElement instance
  1997. * @param Object options
  1998. * @return Element element
  1999. */
  2000. initialize: function(element, options) {
  2001. Element_initialize(this, element, options);
  2002. }
  2003. }, Element_Klass),
  2004. Element_wrappers = Element.Wrappers = {},
  2005. elements_cache = {},
  2006. /**
  2007. * bulds dom-elements
  2008. *
  2009. * @param String element tag name
  2010. * @param Object options
  2011. * @return HTMLElement
  2012. */
  2013. make_element = function (tag, options) {
  2014. return (tag in elements_cache ? elements_cache[tag] : (
  2015. elements_cache[tag] = document.createElement(tag)
  2016. )).cloneNode(false);
  2017. };
  2018. //
  2019. // IE 6,7,8 (not 9!) browsers have a bug with checkbox and radio input elements
  2020. // it doesn't place the 'checked' property correctly, plus there are some issues
  2021. // with clonned SELECT objects, so we are replaceing the elements maker in here
  2022. //
  2023. if (IE8_OR_LESS) {
  2024. make_element = function(tag, options) {
  2025. if (tag === 'input' && options !== undefined) {
  2026. tag = '<input name="'+ options.name +
  2027. '" type='+ options.type +
  2028. (options.checked ? ' checked' : '') +
  2029. '/>';
  2030. }
  2031. return document.createElement(tag);
  2032. };
  2033. }
  2034. /**
  2035. * Basic element's constructor
  2036. *
  2037. * @param Element wrapper instance
  2038. * @param mixed raw dom element of a string tag name
  2039. * @param Object options
  2040. * @return void
  2041. */
  2042. function Element_initialize(inst, element, options) {
  2043. if (typeof element === 'string') {
  2044. inst._ = make_element(element, options);
  2045. if (options !== undefined) {
  2046. for (var key in options) {
  2047. switch (key) {
  2048. case 'id': inst._.id = options[key]; break;
  2049. case 'html': inst._.innerHTML = options[key]; break;
  2050. case 'class': inst._.className = options[key]; break;
  2051. case 'on': inst.on(options[key]); break;
  2052. default: inst.set(key, options[key]);
  2053. }
  2054. }
  2055. }
  2056. } else {
  2057. inst._ = element;
  2058. }
  2059. }
  2060. /**
  2061. * The DOM Element unit structures handling module
  2062. *
  2063. * NOTE: all the methods will process and return only the Element nodes
  2064. * all the textual nodes will be skipped
  2065. *
  2066. * NOTE: if a css-rule was specified then the result of the method
  2067. * will be filtered/adjusted depends on the rule
  2068. *
  2069. * Credits:
  2070. * The naming principle and most of the names are taken from
  2071. * - Prototype (http://prototypejs.org) Copyright (C) Sam Stephenson
  2072. * The insertions system implementation is inspired by
  2073. * - MooTools (http://mootools.net) Copyright (C) Valerio Proietti
  2074. *
  2075. * Copyright (C) 2008-2011 Nikolay Nemshilov
  2076. */
  2077. Element.include({
  2078. parent: function(css_rule) {
  2079. var parent = this._.parentNode, parent_type = parent && parent.nodeType;
  2080. return css_rule ? this.parents(css_rule)[0] :
  2081. (parent_type === 1 || parent_type === 9) ? // <- IE6 sometimes has a fragment node in there
  2082. wrap(parent) : null;
  2083. },
  2084. parents: function(css_rule) {
  2085. return recursively_collect(this, 'parentNode', css_rule);
  2086. },
  2087. children: function(css_rule) {
  2088. return this.find(css_rule).filter(function(element) {
  2089. return element._.parentNode === this._;
  2090. }, this);
  2091. },
  2092. siblings: function(css_rule) {
  2093. return this.prevSiblings(css_rule).reverse().concat(this.nextSiblings(css_rule));
  2094. },
  2095. nextSiblings: function(css_rule) {
  2096. return recursively_collect(this, 'nextSibling', css_rule);
  2097. },
  2098. prevSiblings: function(css_rule) {
  2099. return recursively_collect(this, 'previousSibling', css_rule);
  2100. },
  2101. next: function(css_rule) {
  2102. return !css_rule && this._.nextElementSibling !== undefined ?
  2103. wrap(this._.nextElementSibling) : this.nextSiblings(css_rule)[0];
  2104. },
  2105. prev: function(css_rule) {
  2106. return !css_rule && this._.previousElementSibling !== undefined ?
  2107. wrap(this._.previousElementSibling) : this.prevSiblings(css_rule)[0];
  2108. },
  2109. /**
  2110. * removes the elemnt out of this parent node
  2111. *
  2112. * @return Element self
  2113. */
  2114. remove: function() {
  2115. var element = this._, parent = element.parentNode;
  2116. if (parent) {
  2117. parent.removeChild(element);
  2118. }
  2119. return this;
  2120. },
  2121. /**
  2122. * handles the elements insertion functionality
  2123. *
  2124. * The content might be one of the following data
  2125. *
  2126. * o) an element instance
  2127. * o) a String (all the scripts will be parsed out and executed)
  2128. * o) a list of Elements
  2129. * o) a hash like {position: content}
  2130. *
  2131. * @param mixed data to insert
  2132. * @param String position to insert top/bottom/before/after/instead
  2133. * @return Element self
  2134. */
  2135. insert: function(content, position) {
  2136. var scripts = null, element = this._;
  2137. position = position === undefined ? 'bottom' : position;
  2138. if (typeof(content) !== 'object') {
  2139. scripts = content = (''+content);
  2140. } else if (content instanceof Element) {
  2141. content = content._;
  2142. }
  2143. Element_insertions[position](element,
  2144. content.nodeType === undefined ?
  2145. Element_createFragment(
  2146. (position === 'bottom' || position === 'top') ?
  2147. element : element.parentNode, content
  2148. ) : content
  2149. );
  2150. if (scripts !== null) { scripts.evalScripts(); }
  2151. return this;
  2152. },
  2153. /**
  2154. * Inserts the element inside the given one at the given position
  2155. *
  2156. * @param mixed destination element reference
  2157. * @param String optional position
  2158. * @return Element this
  2159. */
  2160. insertTo: function(element, position) {
  2161. $(element).insert(this, position);
  2162. return this;
  2163. },
  2164. /**
  2165. * A shortcut to uppend several units into the element
  2166. *
  2167. * @param mixed data
  2168. * ..................
  2169. * @return Element this
  2170. */
  2171. append: function(first) {
  2172. return this.insert(isString(first) ? $A(arguments).join('') : arguments);
  2173. },
  2174. /**
  2175. * updates the content of the element by the given content
  2176. *
  2177. * @param mixed content (a String, an Element or a list of elements)
  2178. * @return Element self
  2179. */
  2180. update: function(content) {
  2181. if (typeof(content) !== 'object') {
  2182. content = '' + content;
  2183. try {
  2184. this._.innerHTML = content;
  2185. } catch(e) {
  2186. return this.clean().insert(content);
  2187. }
  2188. content.evalScripts();
  2189. return this;
  2190. } else {
  2191. return this.clean().insert(content);
  2192. }
  2193. },
  2194. /**
  2195. * Works with the Element's innerHTML property
  2196. * This method works both ways! if a content is provided
  2197. * then it will be assigned, otherwise will return
  2198. * the innerHTML property
  2199. *
  2200. * @param String html content
  2201. * @return String html content or Element this
  2202. */
  2203. html: function(content) {
  2204. return content === undefined ? this._.innerHTML : this.update(content);
  2205. },
  2206. /**
  2207. * Works with the Element's innerHTML property as a text
  2208. * when set something, it will appear as is with everything quoted
  2209. * when get, will return a string without any tags in it
  2210. *
  2211. * @param String text content
  2212. * @return String text content or Element this
  2213. */
  2214. text: function(text) {
  2215. return text === undefined ? (this._.textContent || this._.innerText) :
  2216. this.update(this.doc()._.createTextNode(text));
  2217. },
  2218. /**
  2219. * replaces the current element by the given content
  2220. *
  2221. * @param mixed content (a String, an Element or a list of elements)
  2222. * @return Element self
  2223. */
  2224. replace: function(content) {
  2225. return this.insert(content, 'instead');
  2226. },
  2227. /**
  2228. * wraps the element with the given element
  2229. *
  2230. * @param Element wrapper
  2231. * @return Element self
  2232. */
  2233. wrap: function(wrapper) {
  2234. var element = this._, parent = element.parentNode;
  2235. if (parent) {
  2236. wrapper = $(wrapper)._;
  2237. parent.replaceChild(wrapper, element);
  2238. wrapper.appendChild(element);
  2239. }
  2240. return this;
  2241. },
  2242. /**
  2243. * removes all the child nodes out of the element
  2244. *
  2245. * @return Element self
  2246. */
  2247. clean: function() {
  2248. while (this._.firstChild) {
  2249. this._.removeChild(this._.firstChild);
  2250. }
  2251. return this;
  2252. },
  2253. /**
  2254. * checks if the element has no child nodes
  2255. *
  2256. * @return boolean check result
  2257. */
  2258. empty: function() {
  2259. return this.html().blank();
  2260. },
  2261. /**
  2262. * Creates a clean clone of the element without any events attached to it
  2263. *
  2264. * @return Element new clone
  2265. */
  2266. clone: function() {
  2267. return new Element(this._.cloneNode(true));
  2268. },
  2269. /**
  2270. * Returns an index of the element among the other child elements
  2271. *
  2272. * NOTE: doesn't count the textual nodes!
  2273. *
  2274. * @return Integer index
  2275. */
  2276. index: function() {
  2277. var node = this._,
  2278. sibling = node.parentNode.firstChild,
  2279. index = 0;
  2280. while (sibling !== node) {
  2281. if (sibling.nodeType === 1) { // counting elements only
  2282. index ++;
  2283. }
  2284. sibling = sibling.nextSibling;
  2285. }
  2286. return index;
  2287. }
  2288. });
  2289. /**
  2290. * Recursively collects the target element's related nodes
  2291. *
  2292. * @param Element context
  2293. * @param name String pointer attribute name
  2294. * @param rule String optional css-atom rule
  2295. * @return Array found elements
  2296. */
  2297. function recursively_collect(where, attr, css_rule) {
  2298. var node = where._, result = [], i=0, no_rule = !css_rule;
  2299. while ((node = node[attr])) {
  2300. if (node.nodeType === 1 && (no_rule || wrap(node).match(css_rule))) {
  2301. result[i++] = wrap(node);
  2302. }
  2303. }
  2304. return result;
  2305. }
  2306. // list of insertions handling functions
  2307. // NOTE: each of the methods will be called in the contects of the current element
  2308. var Element_insertions = {
  2309. bottom: function(target, content) {
  2310. target.appendChild(content);
  2311. },
  2312. top: function(target, content) {
  2313. if (target.firstChild !== null) {
  2314. target.insertBefore(content, target.firstChild);
  2315. } else {
  2316. target.appendChild(content);
  2317. }
  2318. },
  2319. after: function(target, content) {
  2320. var parent = target.parentNode, sibling = target.nextSibling;
  2321. if (sibling !== null) {
  2322. parent.insertBefore(content, sibling);
  2323. } else {
  2324. parent.appendChild(content);
  2325. }
  2326. },
  2327. before: function(target, content) {
  2328. target.parentNode.insertBefore(content, target);
  2329. },
  2330. instead: function(target, content) {
  2331. target.parentNode.replaceChild(content, target);
  2332. }
  2333. },
  2334. // the element insertion wrappers list
  2335. Element_wraps = {
  2336. TBODY: ['<TABLE>', '</TABLE>', 2],
  2337. TR: ['<TABLE><TBODY>', '</TBODY></TABLE>', 3],
  2338. TD: ['<TABLE><TBODY><TR>', '</TR></TBODY></TABLE>', 4],
  2339. COL: ['<TABLE><COLGROUP>', '</COLGROUP><TBODY></TBODY></TABLE>', 2],
  2340. LEGEND: ['<FIELDSET>', '</FIELDSET>', 2],
  2341. AREA: ['<map>', '</map>', 2],
  2342. OPTION: ['<SELECT>', '</SELECT>', 2]
  2343. };
  2344. $alias(Element_wraps, {
  2345. OPTGROUP: 'OPTION',
  2346. THEAD: 'TBODY',
  2347. TFOOT: 'TBODY',
  2348. TH: 'TD'
  2349. });
  2350. // converts any data into a html fragment unit
  2351. var fragment = document.createDocumentFragment(),
  2352. tmp_cont = document.createElement('DIV');
  2353. function Element_createFragment(context, content) {
  2354. if (typeof(content) === 'string') {
  2355. var tag = context.tagName,
  2356. tmp = tmp_cont,
  2357. wrap = tag in Element_wraps ? Element_wraps[tag] : ['', '', 1],
  2358. depth = wrap[2];
  2359. tmp.innerHTML = wrap[0] + '<'+ tag + '>' + content + '</'+ tag + '>' + wrap[1];
  2360. while (depth-- !== 0) {
  2361. tmp = tmp.firstChild;
  2362. }
  2363. content = tmp.childNodes;
  2364. while (content.length !== 0) {
  2365. fragment.appendChild(content[0]);
  2366. }
  2367. } else {
  2368. for (var i=0, length = content.length, node; i < length; i++) {
  2369. node = content[content.length === length ? i : 0];
  2370. fragment.appendChild(node instanceof Element ? node._ : node);
  2371. }
  2372. }
  2373. return fragment;
  2374. }
  2375. /**
  2376. * this module contains the element unit styles related methods
  2377. *
  2378. * Credits:
  2379. * Some of the functionality is inspired by
  2380. * - Prototype (http://prototypejs.org) Copyright (C) Sam Stephenson
  2381. * - MooTools (http://mootools.net) Copyright (C) Valerio Proietti
  2382. * - Dojo (www.dojotoolkit.org) Copyright (C) The Dojo Foundation
  2383. *
  2384. * Copyright (C) 2008-2011 Nikolay V. Nemshilov
  2385. */
  2386. Element.include({
  2387. /**
  2388. * assigns styles out of the hash to the element
  2389. *
  2390. * NOTE: the style keys might be camelized or dasherized, both cases should work
  2391. *
  2392. * @param Object styles list or String style name
  2393. * @param String style value in case of the first param a string style name
  2394. * @return Element self
  2395. */
  2396. setStyle: function(hash, value) {
  2397. var key, c_key, style = {}, element_style = this._.style;
  2398. if (value !== undefined) { style[hash] = value; hash = style; }
  2399. else if(isString(hash)) {
  2400. hash.split(';').each(function(option) {
  2401. var els = option.split(':').map('trim');
  2402. if (els[0] && els[1]) {
  2403. style[els[0]] = els[1];
  2404. }
  2405. });
  2406. hash = style;
  2407. }
  2408. for (key in hash) {
  2409. c_key = key.indexOf('-') < 0 ? key : key.camelize();
  2410. if (IE_OPACITY && key === 'opacity') {
  2411. element_style.filter = 'alpha(opacity='+ hash[key] * 100 +')';
  2412. } else if (key === 'float') {
  2413. c_key = Browser_IE ? 'styleFloat' : 'cssFloat';
  2414. }
  2415. element_style[c_key] = hash[key];
  2416. }
  2417. return this;
  2418. },
  2419. /**
  2420. * returns style of the element
  2421. *
  2422. * NOTE: will include the CSS level definitions
  2423. *
  2424. * @param String style key
  2425. * @return String style value or null if not set
  2426. */
  2427. getStyle: function(key) {
  2428. return clean_style(this._.style, key) || clean_style(this.computedStyles(), key);
  2429. },
  2430. /**
  2431. * returns the hash of computed styles for the element
  2432. *
  2433. * @return Object/CSSDefinition computed styles
  2434. */
  2435. computedStyles: HTML.currentStyle ? function() {
  2436. return this._.currentStyle || {};
  2437. } : HTML.runtimeStyle ? function() {
  2438. return this._.runtimeStyle || {};
  2439. } : function() {
  2440. return this._.ownerDocument.defaultView.getComputedStyle(this._, null);
  2441. },
  2442. /**
  2443. * checks if the element has the given class name
  2444. *
  2445. * @param String class name
  2446. * @return boolean check result
  2447. */
  2448. hasClass: function(name) {
  2449. return (' '+this._.className+' ').indexOf(' '+name+' ') != -1;
  2450. },
  2451. /**
  2452. * sets the whole class-name string for the element
  2453. *
  2454. * @param String class-name
  2455. * @return Element self
  2456. */
  2457. setClass: function(class_name) {
  2458. this._.className = class_name;
  2459. return this;
  2460. },
  2461. /**
  2462. * Returns the current class-name
  2463. *
  2464. * @return String class-name
  2465. */
  2466. getClass: function() {
  2467. return this._.className;
  2468. },
  2469. /**
  2470. * adds the given class name to the element
  2471. *
  2472. * @param String class name
  2473. * @return Element self
  2474. */
  2475. addClass: function(name) {
  2476. var testee = ' '+this._.className+' ';
  2477. if (testee.indexOf(' '+name+' ') == -1) {
  2478. this._.className += (testee === ' ' ? '' : ' ') + name;
  2479. }
  2480. return this;
  2481. },
  2482. /**
  2483. * removes the given class name
  2484. *
  2485. * @param String class name
  2486. * @return Element self
  2487. */
  2488. removeClass: function(name) {
  2489. this._.className = (' '+this._.className+' ').replace(' '+name+' ', ' ').trim();
  2490. return this;
  2491. },
  2492. /**
  2493. * toggles the given class name on the element
  2494. *
  2495. * @param String class name
  2496. * @return Element self
  2497. */
  2498. toggleClass: function(name) {
  2499. return this[this.hasClass(name) ? 'removeClass' : 'addClass'](name);
  2500. },
  2501. /**
  2502. * adds the given class-name to the element
  2503. * and removes it from all the element siblings
  2504. *
  2505. * @param String class name
  2506. * @return Element self
  2507. */
  2508. radioClass: function(name) {
  2509. this.siblings().each('removeClass', name);
  2510. return this.addClass(name);
  2511. }
  2512. });
  2513. /**
  2514. * cleans up a style value
  2515. *
  2516. * @param Object styles hash
  2517. * @param String style-key
  2518. * @return String clean style
  2519. */
  2520. function clean_style(style, key) {
  2521. key = key.camelize();
  2522. if (key === 'opacity') {
  2523. return IE_OPACITY ? (
  2524. (/opacity=(\d+)/i.exec(style.filter || '') ||
  2525. ['', '100'])[1].toInt() / 100
  2526. )+'' :style[key].replace(',', '.');
  2527. }
  2528. if (key === 'float') {
  2529. key = Browser_IE ? 'styleFloat' : 'cssFloat';
  2530. }
  2531. var value = style[key];
  2532. // Opera returns named colors with quotes
  2533. if (Browser_Opera && /color/i.test(key) && value) {
  2534. value = value.replace(/"/g, '');
  2535. }
  2536. return value;
  2537. }
  2538. /**
  2539. * Common DOM Element unit methods
  2540. *
  2541. * Credits:
  2542. * Most of the naming system in the module inspired by
  2543. * - Prototype (http://prototypejs.org) Copyright (C) Sam Stephenson
  2544. *
  2545. * Copyright (C) 2008-2011 Nikolay V. Nemshilov
  2546. */
  2547. Element.include({
  2548. /**
  2549. * sets the element attributes
  2550. *
  2551. * @param String attr name or Object attributes hash
  2552. * @param mixed attribute value
  2553. * @return Element self
  2554. */
  2555. set: function(hash, value) {
  2556. if (typeof(hash) === 'string') { var val = {}; val[hash] = value; hash = val; }
  2557. var key, element = this._;
  2558. for (key in hash) {
  2559. if (key === 'style') {
  2560. this.setStyle(hash[key]);
  2561. } else {
  2562. // some attributes are not available as properties
  2563. if (!(key in element)) {
  2564. element.setAttribute(key, ''+hash[key]);
  2565. }
  2566. element[key] = hash[key];
  2567. }
  2568. }
  2569. return this;
  2570. },
  2571. /**
  2572. * returns the attribute value for the name
  2573. *
  2574. * @param String attr name
  2575. * @return mixed value
  2576. */
  2577. get: function(name) {
  2578. var element = this._, value = element[name] || element.getAttribute(name);
  2579. return value === '' ? null : value;
  2580. },
  2581. /**
  2582. * checks if the element has that attribute
  2583. *
  2584. * @param String attr name
  2585. * @return Boolean check result
  2586. */
  2587. has: function(name) {
  2588. return this.get(name) !== null;
  2589. },
  2590. /**
  2591. * erases the given attribute of the element
  2592. *
  2593. * @param String attr name
  2594. * @return Element self
  2595. */
  2596. erase: function(name) {
  2597. this._.removeAttribute(name);
  2598. return this;
  2599. },
  2600. /**
  2601. * checks if the elemnt is hidden
  2602. *
  2603. * NOTE: will check css level computed styles too
  2604. *
  2605. * @return boolean check result
  2606. */
  2607. hidden: function() {
  2608. return this.getStyle('display') === 'none';
  2609. },
  2610. /**
  2611. * checks if the element is visible
  2612. *
  2613. * @return boolean check result
  2614. */
  2615. visible: function() {
  2616. return !this.hidden();
  2617. },
  2618. /**
  2619. * hides the element
  2620. *
  2621. * @param String optional effect name
  2622. * @param Object the optional effect options
  2623. * @return Element self
  2624. */
  2625. hide: function(effect, options) {
  2626. if (this.visible()) {
  2627. this._d = this.getStyle('display');
  2628. this._.style.display = 'none';
  2629. }
  2630. return this;
  2631. },
  2632. /**
  2633. * shows the element
  2634. *
  2635. * @return Element self
  2636. */
  2637. show: function() {
  2638. if (this.hidden()) {
  2639. var element = this._, value = this._d, dummy;
  2640. // trying to guess the default 'style.display' for this kind of elements
  2641. if (!value || value === 'none') {
  2642. dummy = $E(element.tagName).insertTo(HTML);
  2643. value = dummy.getStyle('display');
  2644. dummy.remove();
  2645. }
  2646. // failsafe in case the user been naughty
  2647. if (value === 'none') {
  2648. value = 'block';
  2649. }
  2650. element.style.display = value;
  2651. }
  2652. return this;
  2653. },
  2654. /**
  2655. * toggles the visibility state of the element
  2656. *
  2657. * @return Element self
  2658. */
  2659. toggle: function() {
  2660. return this[this.visible() ? 'hide' : 'show']();
  2661. },
  2662. /**
  2663. * shows the element and hides all the sibligns
  2664. *
  2665. * @param String optional effect name
  2666. * @param Object the optional effect options
  2667. * @return Element self
  2668. */
  2669. radio: function(effect, options) {
  2670. this.siblings().each('hide', effect, options);
  2671. return this.show();
  2672. }
  2673. });
  2674. /**
  2675. * this module contains the Element's part of functionality
  2676. * responsible for the dimensions and positions getting/setting
  2677. *
  2678. * Copyright (C) 2008-2011 Nikolay Nemshilov
  2679. */
  2680. Element.include({
  2681. /**
  2682. * Returns the reference to this element document
  2683. *
  2684. * @return RightJS.Document
  2685. */
  2686. doc: function() {
  2687. return wrap(this._.ownerDocument);
  2688. },
  2689. /**
  2690. * Returns the reference to this elements window
  2691. *
  2692. * @return RightJS.Window
  2693. */
  2694. win: function() {
  2695. return this.doc().win();
  2696. },
  2697. /**
  2698. * Returns the element size as a hash
  2699. *
  2700. * @return Object {x: NNN, y: NNN}
  2701. */
  2702. size: function() {
  2703. return { x: this._.offsetWidth, y: this._.offsetHeight };
  2704. },
  2705. /**
  2706. * Returns the element absolute position
  2707. *
  2708. * NOTE: see the konq.js file for the manual version of the method
  2709. *
  2710. * @return Object {x: NNN, y: NNN}
  2711. */
  2712. position: function() {
  2713. var rect = this._.getBoundingClientRect(),
  2714. html = this.doc()._.documentElement,
  2715. scrolls = this.win().scrolls();
  2716. return {
  2717. x: rect.left + scrolls.x - html.clientLeft,
  2718. y: rect.top + scrolls.y - html.clientTop
  2719. };
  2720. },
  2721. /**
  2722. * Returns the element scrolls
  2723. *
  2724. * @return Object {x: NNN, y: NNN}
  2725. */
  2726. scrolls: function() {
  2727. return { x: this._.scrollLeft, y: this._.scrollTop };
  2728. },
  2729. /**
  2730. * returns the element dimensions hash
  2731. *
  2732. * @return Object dimensions (top, left, width, height, scrollLeft, scrollTop)
  2733. */
  2734. dimensions: function() {
  2735. var size = this.size(),
  2736. scrolls = this.scrolls(),
  2737. position = this.position();
  2738. return {
  2739. top: position.y,
  2740. left: position.x,
  2741. width: size.x,
  2742. height: size.y,
  2743. scrollLeft: scrolls.x,
  2744. scrollTop: scrolls.y
  2745. };
  2746. },
  2747. /**
  2748. * Checks if the element overlaps the given position
  2749. *
  2750. * @param Object position {x: NNN, y: NNN}
  2751. * @return boolean check result
  2752. */
  2753. overlaps: function(target) {
  2754. var pos = this.position(), size = this.size();
  2755. return target.x > pos.x && target.x < (pos.x + size.x) &&
  2756. target.y > pos.y && target.y < (pos.y + size.y);
  2757. },
  2758. /**
  2759. * sets the width of the element in pixels
  2760. *
  2761. * NOTE: will double assign the size of the element, so it match the exact
  2762. * size including any possible borders and paddings
  2763. *
  2764. * @param Integer width in pixels
  2765. * @return Element self
  2766. */
  2767. setWidth: function(width_px) {
  2768. var style = this._.style;
  2769. style.width = width_px + 'px';
  2770. style.width = (2 * width_px - this._.offsetWidth) + 'px';
  2771. return this;
  2772. },
  2773. /**
  2774. * sets the width of the element in pixels
  2775. *
  2776. * NOTE: will double assign the size of the element, so it match the exact
  2777. * size including any possible borders and paddings
  2778. *
  2779. * @param Integer height in pixels
  2780. * @return Element self
  2781. */
  2782. setHeight: function(height_px) {
  2783. var style = this._.style;
  2784. style.height = height_px + 'px';
  2785. style.height = (2 * height_px - this._.offsetHeight) + 'px';
  2786. return this;
  2787. },
  2788. /**
  2789. * sets the size of the element in pixels
  2790. *
  2791. * NOTE: will double assign the size of the element, so it match the exact
  2792. * size including any possible borders and paddings
  2793. *
  2794. * @param width Integer width in pixels or {x: 10, y: 20} like object
  2795. * @param height Integer height
  2796. * @return Element self
  2797. */
  2798. resize: function(width, height) {
  2799. if (isHash(width)) {
  2800. height = width.y;
  2801. width = width.x;
  2802. }
  2803. return this.setWidth(width).setHeight(height);
  2804. },
  2805. /**
  2806. * sets the element position (against the window corner)
  2807. *
  2808. * @param left Number left position in pixels or an object like {x: 10, y: 20}
  2809. * @param top Number top position in pixels
  2810. * @return Element self
  2811. */
  2812. moveTo: function(left, top) {
  2813. if (isHash(left)) {
  2814. top = left.y;
  2815. left = left.x;
  2816. }
  2817. return this.setStyle({
  2818. left: left + 'px',
  2819. top: top + 'px'
  2820. });
  2821. },
  2822. /**
  2823. * sets the scroll position
  2824. *
  2825. * @param left Integer left scroll px or an object like {x: 22, y: 33}
  2826. * @param top Integer top scroll px
  2827. * @return Element self
  2828. */
  2829. scrollTo: function(left, top) {
  2830. if (isHash(left)) {
  2831. top = left.y;
  2832. left = left.x;
  2833. }
  2834. this._.scrollLeft = left;
  2835. this._.scrollTop = top;
  2836. return this;
  2837. },
  2838. /**
  2839. * makes the window be scrolled to the element
  2840. *
  2841. * @param Object fx options
  2842. * @return Element self
  2843. */
  2844. scrollThere: function(options) {
  2845. this.win().scrollTo(this, options);
  2846. return this;
  2847. }
  2848. });
  2849. /**
  2850. * DOM Element events handling methods
  2851. *
  2852. * Copyright (C) 2008-2011 Nikolay Nemshilov
  2853. */
  2854. [Element, Document, Window].each('include', $ext(Observer_create({}), {
  2855. /**
  2856. * The basic events handling attachment method
  2857. * SEE Observer#on for more details about supported arguments
  2858. *
  2859. * @returnt this
  2860. */
  2861. on: function() {
  2862. Observer_on(this, arguments, function(hash) {
  2863. if (hash.e === 'mouseenter' || hash.e === 'mouseleave') {
  2864. mouse_io_activate();
  2865. hash.n = hash.e;
  2866. hash.w = function() {};
  2867. // NOTE: we don't attach this listener to the actual element!
  2868. // so it didn't screw with IE's native enter/leave handlers
  2869. } else {
  2870. if (hash.e === 'contextmenu' && Browser.Konqueror) {
  2871. hash.n = 'rightclick';
  2872. } else if (hash.e === 'mousewheel' && Browser.Gecko) {
  2873. hash.n = 'DOMMouseScroll';
  2874. } else {
  2875. hash.n = hash.e;
  2876. }
  2877. hash.w = function(event) {
  2878. event = new Event(event, hash.t);
  2879. if (hash.f.apply(hash.t, (hash.r?[]:[event]).concat(hash.a)) === false) {
  2880. event.stop();
  2881. }
  2882. };
  2883. if (IE8_OR_LESS) {
  2884. hash.t._.attachEvent('on'+hash.n, hash.w);
  2885. } else {
  2886. hash.t._.addEventListener(hash.n, hash.w, false);
  2887. }
  2888. }
  2889. return hash;
  2890. });
  2891. return this;
  2892. },
  2893. /**
  2894. * Stops an event handling
  2895. *
  2896. * @param String event name or a function callback
  2897. * @param function callback or nothing
  2898. * @return this
  2899. */
  2900. stopObserving: function(event, callback) {
  2901. Observer_stopObserving(this, event, callback, function(hash) {
  2902. if (IE8_OR_LESS) {
  2903. hash.t._.detachEvent('on'+ hash.n, hash.w);
  2904. } else {
  2905. hash.t._.removeEventListener(hash.n, hash.w, false);
  2906. }
  2907. });
  2908. return this;
  2909. },
  2910. /**
  2911. * Artificially trigers the event on the element
  2912. *
  2913. * @param string event name or an Event instance
  2914. * @param Object options
  2915. * @return this
  2916. */
  2917. fire: function(event, options) {
  2918. var parent = this.parent && this.parent();
  2919. if (!(event instanceof Event)) {
  2920. event = new Event(event, $ext({target: this._}, options));
  2921. }
  2922. // setting up the currentTarget reference
  2923. event.currentTarget = this;
  2924. (this.$listeners || []).each(function(hash) {
  2925. if (hash.e === event.type &&
  2926. hash.f.apply(this, (hash.r?[]:[event]).concat(hash.a)) === false
  2927. ) {
  2928. event.stop();
  2929. }
  2930. }, this);
  2931. // manually bypassing the event to the parent one if it should bubble
  2932. if (parent && parent.fire && !event.stopped) {
  2933. parent.fire(event);
  2934. }
  2935. return this;
  2936. },
  2937. /**
  2938. * a simple events terminator method to be hooked like this.onClick('stopEvent');
  2939. *
  2940. * @return false
  2941. */
  2942. stopEvent: function() { return false; }
  2943. }));
  2944. // couple more shortcuts for the window
  2945. Observer_createShortcuts(Window.prototype, $w('blur focus scroll resize load'));
  2946. /**
  2947. * Registers a list of event-binding shortcuts like
  2948. * $(element).onClick
  2949. * $(element).onMouseover
  2950. *
  2951. * @param String space separated event names
  2952. * @return void
  2953. */
  2954. function Element_add_event_shortcuts(tokens) {
  2955. tokens = $w(tokens);
  2956. Event_delegation_shortcuts = Event_delegation_shortcuts.concat(tokens);
  2957. Observer_createShortcuts(Element.prototype, tokens);
  2958. Observer_createShortcuts(Document.prototype, tokens);
  2959. }
  2960. Element_add_event_shortcuts(
  2961. 'click rightclick contextmenu mousedown mouseup '+
  2962. 'mouseover mouseout mousemove keypress keydown keyup'
  2963. );
  2964. /**
  2965. * The DOM elements selection handling
  2966. *
  2967. * NOTE: this module is just a wrap over the native CSS-selectors feature
  2968. * see the olds/css.js file for the manual selector code
  2969. *
  2970. * Copyright (C) 2008-2011 Nikolay Nemshilov
  2971. */
  2972. [Element, Document].each('include', {
  2973. /**
  2974. * Extracts the first element matching the css-rule,
  2975. * or just any first element if no css-rule was specified
  2976. *
  2977. * @param String css-rule
  2978. * @return Element matching node or null
  2979. */
  2980. first: function(css_rule) {
  2981. return wrap(
  2982. css_rule === undefined && this._.firstElementChild !== undefined ?
  2983. this._.firstElementChild : this._.querySelector(css_rule || '*')
  2984. );
  2985. },
  2986. /**
  2987. * Finds a list of matching nodes, or all the descendant nodes if no css-rule provided
  2988. *
  2989. * @param String css-rule
  2990. * @param boolean raw-search
  2991. * @return Array of elements
  2992. */
  2993. find: function(css_rule, raw) {
  2994. var query = this._.querySelectorAll(css_rule || '*'), result, i=0, l = query.length;
  2995. if (raw === true) {
  2996. result = $A(query);
  2997. } else {
  2998. for (result = []; i < l; i++) {
  2999. result[i] = wrap(query[i]);
  3000. }
  3001. }
  3002. return result;
  3003. },
  3004. /**
  3005. * checks if the element matches this css-rule
  3006. *
  3007. * NOTE: the element should be attached to the page
  3008. *
  3009. * @param String css-rule
  3010. * @return Boolean check result
  3011. */
  3012. match: function(css_rule) {
  3013. // finding the top parent element (the element might not be on the document)
  3014. var element = this._, parent = element, result, faking = false;
  3015. while (parent.parentNode !== null && parent.parentNode.nodeType !== 11) {
  3016. parent = parent.parentNode;
  3017. }
  3018. // creating a fake context when needed
  3019. if (element === parent) {
  3020. parent = document.createElement('div');
  3021. parent.appendChild(element);
  3022. faking = true;
  3023. }
  3024. result = wrap(parent).find(css_rule, true).indexOf(element) !== -1;
  3025. if (faking) {
  3026. parent.removeChild(element);
  3027. }
  3028. return result;
  3029. }
  3030. });
  3031. /**
  3032. * The dom-ready event handling code
  3033. *
  3034. * Credits:
  3035. * The basic principles of the module are originated from
  3036. * - MooTools (http://mootools.net) Copyright (C) Valerio Proietti
  3037. *
  3038. * Copyright (C) 2009-2011 Nikolay Nemshilov
  3039. */
  3040. Document.include({
  3041. on: function(name) {
  3042. if (name === 'ready' && !this._iR) {
  3043. var document = this._, ready = this.fire.bind(this, 'ready');
  3044. // IE and Konqueror browsers
  3045. if ('readyState' in document) {
  3046. (function() {
  3047. if (['loaded','complete'].include(document.readyState)) {
  3048. ready();
  3049. } else {
  3050. arguments.callee.delay(50);
  3051. }
  3052. })();
  3053. } else {
  3054. document.addEventListener('DOMContentLoaded', ready, false);
  3055. }
  3056. this._iR = true;
  3057. }
  3058. return this.$super.apply(this, arguments);
  3059. }
  3060. });
  3061. Observer_createShortcuts(Document.prototype, ['ready']);
  3062. /**
  3063. * The form unit class and extensions
  3064. *
  3065. * Credits:
  3066. * The basic principles of the module are inspired by
  3067. * - Prototype (http://prototypejs.org) Copyright (C) Sam Stephenson
  3068. *
  3069. * Copyright (C) 2009-2011 Nikolay Nemshilov
  3070. */
  3071. var Form = RightJS.Form = Element_wrappers.FORM = new Class(Element, {
  3072. /**
  3073. * constructor
  3074. *
  3075. * NOTE: this constructor can be called as a normal Element constructor
  3076. * or with the options only, which will make a FORM element
  3077. *
  3078. * var form = new Form(raw_form_object_element);
  3079. * var form = new Form({method: 'post', action: '/boo/hoo'});
  3080. *
  3081. * @param Object options or HTMLFormElement object
  3082. * @return void
  3083. */
  3084. initialize: function(in_options) {
  3085. var options = in_options || {}, remote = 'remote' in options, element = options;
  3086. if (isHash(options) && !isElement(options)) {
  3087. element = 'form';
  3088. options = Object.without(options, 'remote');
  3089. }
  3090. this.$super(element, options);
  3091. if (remote) {
  3092. this.remotize();
  3093. }
  3094. },
  3095. /**
  3096. * returns the form elements as an array of extended units
  3097. *
  3098. * @return Array of elements
  3099. */
  3100. elements: function() {
  3101. return this.find('input,button,select,textarea');
  3102. },
  3103. /**
  3104. * returns the list of all the input elements on the form
  3105. *
  3106. * @return Array of elements
  3107. */
  3108. inputs: function() {
  3109. return this.elements().filter(function(input) {
  3110. return !['submit', 'button', 'reset', 'image', null].include(input._.type);
  3111. });
  3112. },
  3113. /**
  3114. * Accessing an input by name
  3115. *
  3116. * @param String name
  3117. * @return Input field
  3118. */
  3119. input: function(name) {
  3120. return wrap(this._[name]);
  3121. },
  3122. /**
  3123. * focuses on the first input element on the form
  3124. *
  3125. * @return Form this
  3126. */
  3127. focus: function() {
  3128. var element = this.inputs().first(function(input) {
  3129. return input._.type !== 'hidden';
  3130. });
  3131. if (element) { element.focus(); }
  3132. return this;
  3133. },
  3134. /**
  3135. * removes focus out of all the form elements
  3136. *
  3137. * @return Form this
  3138. */
  3139. blur: function() {
  3140. this.elements().each('blur');
  3141. return this;
  3142. },
  3143. /**
  3144. * disables all the elements on the form
  3145. *
  3146. * @return Form this
  3147. */
  3148. disable: function() {
  3149. this.elements().each('disable');
  3150. return this;
  3151. },
  3152. /**
  3153. * enables all the elements on the form
  3154. *
  3155. * @return Form this
  3156. */
  3157. enable: function() {
  3158. this.elements().each('enable');
  3159. return this;
  3160. },
  3161. /**
  3162. * returns the list of the form values
  3163. *
  3164. * @return Object values
  3165. */
  3166. values: function() {
  3167. var values = {}, value, name, element, input;
  3168. this.inputs().each(function(element) {
  3169. input = element._;
  3170. name = input.name;
  3171. if (!input.disabled && name && (
  3172. !['checkbox', 'radio'].include(input.type) || input.checked
  3173. )) {
  3174. value = element.getValue();
  3175. if (name.endsWith('[]')) {
  3176. value = (values[name] || []).concat([value]);
  3177. }
  3178. values[name] = value;
  3179. }
  3180. });
  3181. return values;
  3182. },
  3183. /**
  3184. * returns the key/values organized ready to be sent via a get request
  3185. *
  3186. * @return String serialized values
  3187. */
  3188. serialize: function() {
  3189. return Object.toQueryString(this.values());
  3190. },
  3191. /**
  3192. * Delegating the submit method
  3193. *
  3194. * @return Form this
  3195. */
  3196. submit: function() {
  3197. this._.submit();
  3198. return this;
  3199. },
  3200. /**
  3201. * Delegating the 'reset' method
  3202. *
  3203. * @return Form this
  3204. */
  3205. reset: function() {
  3206. this._.reset();
  3207. return this;
  3208. }
  3209. });
  3210. // creating the event shortcuts
  3211. Element_add_event_shortcuts('submit reset focus blur disable enable change');
  3212. /**
  3213. * The form input element class
  3214. *
  3215. * Copyright (C) 2010-2011 Nikolay Nemshilov
  3216. */
  3217. var Input = RightJS.Input =
  3218. // retgistering the typecasted wrappers
  3219. Element_wrappers.INPUT =
  3220. Element_wrappers.BUTTON =
  3221. Element_wrappers.SELECT =
  3222. Element_wrappers.TEXTAREA =
  3223. Element_wrappers.OPTGROUP =
  3224. new Class(Element, {
  3225. /**
  3226. * Constructor
  3227. *
  3228. * NOTE: this constructor can be called in several ways
  3229. *
  3230. * Like normal Element
  3231. * var input = new Input('texarea', {...});
  3232. * var input = new Input(document.createElement('select'));
  3233. *
  3234. * Or with options only which will make an INPUT element by default
  3235. * var input = new Input({type: 'password', name: 'password'});
  3236. *
  3237. * @param HTMLElement or a String tag name or Options for default 'input' tag
  3238. * @param Object options
  3239. * @return void
  3240. */
  3241. initialize: function(element, options) {
  3242. // type to tag name conversion
  3243. if (!element || (isHash(element) && !isElement(element))) {
  3244. options = element || {};
  3245. if (/textarea|select/.test(options.type || '')) {
  3246. element = options.type;
  3247. delete(options.type);
  3248. } else {
  3249. element = 'input';
  3250. }
  3251. }
  3252. this.$super(element, options);
  3253. },
  3254. /**
  3255. * Returns a reference to the input's form
  3256. *
  3257. * @return Form wrapped form
  3258. */
  3259. form: function() {
  3260. return wrap(this._.form);
  3261. },
  3262. /**
  3263. * Overloading the method to fix some issues with IE and FF
  3264. *
  3265. * @param mixed content
  3266. * @param string optional position
  3267. * @return Input this
  3268. */
  3269. insert: function(content, position) {
  3270. this.$super(content, position);
  3271. // manually resetting the selected option in here
  3272. this.find('option').each(function(option) {
  3273. option._.selected = !!option.get('selected');
  3274. });
  3275. return this;
  3276. },
  3277. /**
  3278. * Overloading the method so it always called the '#insert' method
  3279. *
  3280. * @param mixed content
  3281. * @return Input this
  3282. */
  3283. update: function(content) {
  3284. return this.clean().insert(content);
  3285. },
  3286. /**
  3287. * uniform access to the element values
  3288. *
  3289. * @return String element value
  3290. */
  3291. getValue: function() {
  3292. if (this._.type == 'select-multiple') {
  3293. return this.find('option').map(function(option) {
  3294. return option._.selected ? option._.value : null;
  3295. }).compact();
  3296. } else {
  3297. return this._.value;
  3298. }
  3299. },
  3300. /**
  3301. * uniform accesss to set the element value
  3302. *
  3303. * @param String value
  3304. * @return Element this
  3305. */
  3306. setValue: function(value) {
  3307. if (this._.type == 'select-multiple') {
  3308. value = ensure_array(value).map(String);
  3309. this.find('option').each(function(option) {
  3310. option._.selected = value.include(option._.value);
  3311. });
  3312. } else {
  3313. this._.value = value;
  3314. }
  3315. return this;
  3316. },
  3317. /**
  3318. * Both ways getter/setter for the value parameter
  3319. *
  3320. * @param mixed value
  3321. * @return mixed this or the value
  3322. */
  3323. value: function(value) {
  3324. return this[value === undefined ? 'getValue' : 'setValue'](value);
  3325. },
  3326. /**
  3327. * focuses on the first input element on the form
  3328. *
  3329. * @return Form this
  3330. */
  3331. focus: function() {
  3332. this._.focus();
  3333. this.focused = true;
  3334. if (Browser_IE) { this.fire('focus', {bubbles: false}); }
  3335. return this;
  3336. },
  3337. /**
  3338. * removes focus out of all the form elements
  3339. *
  3340. * @return Form this
  3341. */
  3342. blur: function() {
  3343. this._.blur();
  3344. this.focused = false;
  3345. if (Browser_IE) { this.fire('blur', {bubbles: false}); }
  3346. return this;
  3347. },
  3348. /**
  3349. * focuses on the element and selects its content
  3350. *
  3351. * @return Element this
  3352. */
  3353. select: function() {
  3354. this._.select();
  3355. return this.focus();
  3356. },
  3357. /**
  3358. * disables all the elements on the form
  3359. *
  3360. * @return Form this
  3361. */
  3362. disable: function() {
  3363. this._.disabled = true;
  3364. return this.fire('disable');
  3365. },
  3366. /**
  3367. * enables all the elements on the form
  3368. *
  3369. * @return Form this
  3370. */
  3371. enable: function() {
  3372. this._.disabled = false;
  3373. return this.fire('enable');
  3374. },
  3375. /**
  3376. * A bidirectional method to set/get the disabled status of the input field
  3377. *
  3378. * @param boolean optional value
  3379. * @return Input in setter mode boolean in getter
  3380. */
  3381. disabled: function(value) {
  3382. return value === undefined ? this._.disabled : this[value ? 'disable' : 'enable']();
  3383. },
  3384. /**
  3385. * A bidirectional method to set/get the checked status of the input field
  3386. *
  3387. * @param boolean optional value
  3388. * @return Input in setter mode boolean in getter
  3389. */
  3390. checked: function(value) {
  3391. if (value === undefined) {
  3392. value = this._.checked;
  3393. } else {
  3394. this._.checked = value;
  3395. value = this;
  3396. }
  3397. return value;
  3398. }
  3399. });
  3400. /**
  3401. * This module provides correct focus/blur events bubbling
  3402. *
  3403. * Copyright (C) 2010-2011 Nikolay Nemshilov
  3404. */
  3405. /**
  3406. * Triggers a manual focus/blur events bubbling
  3407. *
  3408. * @param raw dom-event
  3409. * @return void
  3410. */
  3411. function focus_boobler(raw_event) {
  3412. var event = new Event(raw_event),
  3413. target = event.target,
  3414. parent = target.parent && target.parent();
  3415. event.type = raw_event.type === 'focusin' || raw_event.type === 'focus' ? 'focus' : 'blur';
  3416. if (parent) { parent.fire(event); }
  3417. }
  3418. /**
  3419. * Hooking up the 'focus' and 'blur' events
  3420. * at the document level and then rebooble them
  3421. * manually like they were normal events
  3422. *
  3423. */
  3424. if (IE8_OR_LESS) {
  3425. document.attachEvent('onfocusin', focus_boobler);
  3426. document.attachEvent('onfocusout', focus_boobler);
  3427. } else {
  3428. document.addEventListener('focus', focus_boobler, true);
  3429. document.addEventListener('blur', focus_boobler, true);
  3430. }
  3431. /**
  3432. * Provides the mouse enter/leave events handling emulation
  3433. *
  3434. * Copyright (C) 2010-2011 Nikolay Nemshilov
  3435. */
  3436. var mouse_io_index = [], mouse_io_inactive = true;
  3437. /**
  3438. * Fires the actual mouseenter/mouseleave event
  3439. *
  3440. * @param original event
  3441. * @param raw dom element
  3442. * @param integer uid
  3443. * @param boolean mouseenter or mouseleave
  3444. * @return void
  3445. */
  3446. function mouse_io_fire(raw, element, uid, enter) {
  3447. var event = new Event(raw);
  3448. event.type = enter === true ? 'mouseenter' : 'mouseleave';
  3449. event.bubbles = false;
  3450. event.stopped = true;
  3451. event.target = wrap(element);
  3452. // replacing the #find method so that UJS didn't
  3453. // get broke with trying to find nested elements
  3454. event.find = function(css_rule) {
  3455. return $$(css_rule, true)
  3456. .indexOf(this.target._) === -1 ?
  3457. undefined : this.target;
  3458. };
  3459. event.target.fire(event);
  3460. current_Document.fire(event);
  3461. }
  3462. /**
  3463. * Figures out the enter/leave events by listening the
  3464. * mouseovers in the document
  3465. *
  3466. * @param raw dom event
  3467. * @return void
  3468. */
  3469. function mouse_io_handler(e) {
  3470. var target = e.target || e.srcElement,
  3471. from = e.relatedTarget || e.fromElement,
  3472. element = target,
  3473. passed = false,
  3474. parents = [],
  3475. uid, event;
  3476. while (element.nodeType === 1) {
  3477. uid = $uid(element);
  3478. if (mouse_io_index[uid] === undefined) {
  3479. mouse_io_fire(e, element, uid,
  3480. mouse_io_index[uid] = true
  3481. );
  3482. }
  3483. if (element === from) {
  3484. passed = true;
  3485. }
  3486. parents.push(element);
  3487. element = element.parentNode;
  3488. }
  3489. if (from && !passed) {
  3490. while (from !== null && from.nodeType === 1 && parents.indexOf(from) === -1) {
  3491. uid = $uid(from);
  3492. if (mouse_io_index[uid] !== undefined) {
  3493. mouse_io_fire(e, from, uid,
  3494. mouse_io_index[uid] = undefined
  3495. );
  3496. }
  3497. from = from.parentNode;
  3498. }
  3499. }
  3500. }
  3501. /**
  3502. * Calling 'mouseleave' for all currently active elements on the page
  3503. *
  3504. * @return void
  3505. */
  3506. function mouse_io_reset(e) {
  3507. mouse_io_index.each(function(value, uid) {
  3508. if (value && Wrappers_Cache[uid]) {
  3509. mouse_io_fire(e, Wrappers_Cache[uid]._, uid, false);
  3510. }
  3511. });
  3512. }
  3513. /**
  3514. * Activating the mouse-io events emulation
  3515. *
  3516. * @return void
  3517. */
  3518. function mouse_io_activate() {
  3519. if (mouse_io_inactive) {
  3520. mouse_io_inactive = false;
  3521. if (Browser_IE) {
  3522. document.attachEvent('onmouseover', mouse_io_handler);
  3523. window.attachEvent('blur', mouse_io_reset);
  3524. } else {
  3525. document.addEventListener('mouseover', mouse_io_handler, false);
  3526. window.addEventListener('blur', mouse_io_reset, false);
  3527. }
  3528. }
  3529. }
  3530. Element_add_event_shortcuts('mouseenter mouseleave');
  3531. /**
  3532. * This module the standard events delegation interface
  3533. *
  3534. * Copyright (C) 2010-2011 Nikolay Nemshilov
  3535. */
  3536. [Element, Document].each('include', {
  3537. /**
  3538. * Attaches a delegative event listener to the element/document
  3539. *
  3540. * USAGE:
  3541. * $(element).delegate('click', '#css.rule', function() {...});
  3542. * $(element).delegate('click', '#css.rule', [func1, func2, ...]);
  3543. * $(element).delegate('click', '#css.rule', 'addClass', 'boo');
  3544. * $(element).delegate('click', '#css.rule', 'hide');
  3545. *
  3546. * $(element).delegate('click', {
  3547. * '#css.rule1': function() {},
  3548. * '#css.rule2': [func1, func2, ...],
  3549. * '#css.rule3': ['addClass', 'boo'],
  3550. * '#css.rule4': 'hide'
  3551. * });
  3552. *
  3553. * @param event name
  3554. * @param css-rule a hash or rules
  3555. * @param callback
  3556. * @return this
  3557. */
  3558. delegate: function(event) {
  3559. var rules = delegation_rules(arguments), css_rule, i, j, list;
  3560. for (css_rule in rules) {
  3561. for (i=0, list = rules[css_rule]; i < list.length; i++) {
  3562. // registering the delegative listener
  3563. this.on(event, build_delegative_listener(css_rule, list[i], this));
  3564. // adding the css-rule and callback references to the store
  3565. $ext(this.$listeners.last(), { dr: css_rule, dc: list[i][0] });
  3566. }
  3567. }
  3568. return this;
  3569. },
  3570. /**
  3571. * Removes a delegative event listener from the element
  3572. *
  3573. * USAGE:
  3574. * $(element).undelegate('click');
  3575. * $(element).undelegate('click', '#css.rule');
  3576. * $(element).undelegate('click', '#css.rule', function() {});
  3577. * $(element).undelegate('click', '#css.rule', [func1, func2, ...]);
  3578. * $(element).undelegate('click', '#css.rule', 'addClass', 'boo');
  3579. * $(element).undelegate('click', '#css.rule', 'hide');
  3580. *
  3581. * $(element).undelegate('click', {
  3582. * '#css.rule1': function() {},
  3583. * '#css.rule2': [func1, func2, ...],
  3584. * '#css.rule3': ['addClass', 'boo'],
  3585. * '#css.rule4': 'hide'
  3586. * });
  3587. *
  3588. * @param event name
  3589. * @param css-rule or a hash or rules
  3590. * @param callback
  3591. * @return this
  3592. */
  3593. undelegate: function(event) {
  3594. delegation_listeners(arguments, this).each(function(h) {
  3595. this.stopObserving(h.n, h.f);
  3596. }, this);
  3597. return this;
  3598. },
  3599. /**
  3600. * Checks if there is sucha delegative event listener
  3601. *
  3602. * USAGE:
  3603. * $(element).delegates('click');
  3604. * $(element).delegates('click', '#css.rule');
  3605. * $(element).delegates('click', '#css.rule', function() {});
  3606. * $(element).delegates('click', '#css.rule', [func1, func2, ...]);
  3607. * $(element).delegates('click', '#css.rule', 'addClass', 'boo');
  3608. * $(element).delegates('click', '#css.rule', 'hide');
  3609. *
  3610. * $(element).delegates('click', {
  3611. * '#css.rule1': function() {},
  3612. * '#css.rule2': [func1, func2, ...],
  3613. * '#css.rule3': ['addClass', 'boo'],
  3614. * '#css.rule4': 'hide'
  3615. * });
  3616. *
  3617. * NOTE:
  3618. * if several rules are specified then it will check if
  3619. * _any_ of them are delegateed
  3620. *
  3621. * @param event name
  3622. * @param css-rule or a hash of rules
  3623. * @param callback
  3624. * @return boolean check result
  3625. */
  3626. delegates: function() {
  3627. return !!delegation_listeners(arguments, this).length;
  3628. }
  3629. });
  3630. /**
  3631. * Builds the actual event listener that will delegate stuff
  3632. * to other elements as they reach the element where the listener
  3633. * attached
  3634. *
  3635. * @param String css rule
  3636. * @param Arguments the original arguments list
  3637. * @param Object scope
  3638. * @return Function the actual event listener
  3639. */
  3640. function build_delegative_listener(css_rule, entry, scope) {
  3641. var args = $A(entry), callback = args.shift();
  3642. return function(event) {
  3643. var target = event.find(css_rule);
  3644. return target === undefined ? target :
  3645. typeof(callback) === 'string' ?
  3646. target[callback].apply(target, args) :
  3647. callback.apply(target, [event].concat(args));
  3648. };
  3649. }
  3650. /**
  3651. * Converts the events-delegation api arguments
  3652. * into a systematic hash of rules
  3653. *
  3654. * @param Arguments arguments
  3655. * @return Object hash of rules
  3656. */
  3657. function delegation_rules(raw_args) {
  3658. var args = $A(raw_args), rules = args[1] || {}, hash = {}, css_rule;
  3659. if (isString(rules)) {
  3660. hash[rules] = args.slice(2);
  3661. if (isArray(hash[rules][0])) {
  3662. hash[rules] = hash[rules][0].map(ensure_array);
  3663. }
  3664. } else {
  3665. hash = rules;
  3666. }
  3667. // converting everything into a hash of lists of callbacks
  3668. for (css_rule in hash) {
  3669. hash[css_rule] = ensure_array(hash[css_rule]);
  3670. hash[css_rule] = isArray(hash[css_rule][0]) ? hash[css_rule] : [hash[css_rule]];
  3671. }
  3672. return hash;
  3673. }
  3674. /**
  3675. * Returns the list of delegative listeners that match the conditions
  3676. *
  3677. * @param Arguments raw-arguments
  3678. * @param Element the element
  3679. * @return Array list of matching listeners
  3680. */
  3681. function delegation_listeners(args, object) {
  3682. var event = args[0], i, list,
  3683. rules = delegation_rules(args),
  3684. rules_are_empty = !Object.keys(rules).length;
  3685. return (object.$listeners || []).filter(function(hash) {
  3686. return hash.dr && hash.n === event && (
  3687. rules_are_empty || (function() {
  3688. for (var css_rule in rules) {
  3689. if (hash.dr === css_rule) {
  3690. for (i=0, list = rules[css_rule]; i < list.length; i++) {
  3691. if (!list[i].length || list[i][0] === hash.dc) {
  3692. return true;
  3693. }
  3694. }
  3695. }
  3696. }
  3697. return false;
  3698. })()
  3699. );
  3700. });
  3701. }
  3702. /**
  3703. * Some String level shortcuts to handle collections of elements
  3704. *
  3705. * Copyright (C) 2011 Nikolay Nemshilov
  3706. */
  3707. /**
  3708. * Some nice shortcuts for the document-level events delegation handling
  3709. *
  3710. * USAGE:
  3711. *
  3712. * "ul#main-menu li".on("click", function() { alert('clicked'); });
  3713. * "ul#main-menu li".on("mouseover", "addClass", "hovered");
  3714. * "ul#main-menu li".on("mouseout", "removeClass", "hovered");
  3715. *
  3716. * // or like that in a shash
  3717. * "ul#main-menu li".on({
  3718. * click: function() { alert('clicked'); },
  3719. * mouseover: ['addClass', 'hovered'],
  3720. * mouseout: ['removeClass', 'hovered'],
  3721. * dblclick: 'hide'
  3722. * });
  3723. *
  3724. *
  3725. * "#css.rule".observes('click');
  3726. * "#css.rule".observes('click', function() {});
  3727. * "#css.rule".observes('click', 'method_name');
  3728. * ....
  3729. *
  3730. * "#css.rule".stopObserving('click');
  3731. * "#css.rule".stopObserving('click', function() {});
  3732. * "#css.rule".stopObserving('click', 'method_name');
  3733. * ....
  3734. */
  3735. Object.each({
  3736. on: 'delegate',
  3737. stopObserving: 'undelegate',
  3738. observes: 'delegates'
  3739. }, function(name, method) {
  3740. String.prototype[name] = function() {
  3741. var args = $A(arguments), result;
  3742. args.splice(1,0,''+this);
  3743. result = current_Document[method].apply(current_Document, args);
  3744. return result === current_Document ? this : result;
  3745. };
  3746. });
  3747. var old_on = String.prototype.on;
  3748. String.prototype.on = function(hash) {
  3749. if (isHash(hash)) {
  3750. for (var key in hash) {
  3751. old_on.apply(this, [key].concat([hash[key]]));
  3752. }
  3753. } else {
  3754. old_on.apply(this, arguments);
  3755. }
  3756. return this;
  3757. };
  3758. /**
  3759. * building the list of String#onEvent shortucts
  3760. *
  3761. * USAGE:
  3762. *
  3763. * "#css.rule".onClick(function() {...});
  3764. * "#css.rule".onMouseover('method_name');
  3765. */
  3766. Event_delegation_shortcuts.each(function(name) {
  3767. String.prototype['on'+name.capitalize()] = function() {
  3768. return this.on.apply(this, [name].concat($A(arguments)));
  3769. };
  3770. });
  3771. /**
  3772. * The rest of the DOM methods access
  3773. *
  3774. * USAGE:
  3775. * "#css.rule".addClass('boo-hoo');
  3776. * "#css.rule".setStyle({color: 'red'});
  3777. *
  3778. */
  3779. $w('Element Input Form').each(function(klass) {
  3780. Object.each(klass in RightJS ? RightJS[klass].prototype : {}, function(name, method) {
  3781. if (isFunction(method) && !(name in String.prototype)) {
  3782. String.prototype[name] = function() {
  3783. var nodes = $$(this, true), i=0, l = nodes.length, first=true, element, result;
  3784. for (; i < l; i++) {
  3785. element = wrap(nodes[i]);
  3786. result = element[name].apply(element, arguments);
  3787. // checking if that's a data-retrieving call
  3788. if (first) {
  3789. if (result !== element) {
  3790. return result;
  3791. }
  3792. first = false;
  3793. }
  3794. }
  3795. // don't return the string itself in here,
  3796. // it will screw with data-retrieving calls on empty collections
  3797. return null;
  3798. };
  3799. }
  3800. });
  3801. });
  3802. /**
  3803. * XMLHttpRequest wrapper
  3804. *
  3805. * Credits:
  3806. * Some of the functionality inspired by
  3807. * - Prototype (http://prototypejs.org) Copyright (C) Sam Stephenson
  3808. * - MooTools (http://mootools.net) Copyright (C) Valerio Proietti
  3809. * - jQuery (http://jquery.com) Copyright (C) John Resig
  3810. *
  3811. * Copyright (C) 2008-2011 Nikolay V. Nemshilov
  3812. */
  3813. var Xhr = RightJS.Xhr = new Class(Observer, {
  3814. extend: {
  3815. // supported events list
  3816. EVENTS: $w('success failure complete request cancel create'),
  3817. // default options
  3818. Options: {
  3819. headers: {
  3820. 'X-Requested-With': 'XMLHttpRequest',
  3821. 'Accept': 'text/javascript,text/html,application/xml,text/xml,*/*'
  3822. },
  3823. method: 'post',
  3824. encoding: 'utf-8',
  3825. async: true,
  3826. evalScripts: false,
  3827. evalResponse: false,
  3828. evalJS: true,
  3829. evalJSON: true,
  3830. secureJSON: true,
  3831. urlEncoded: true,
  3832. spinner: null,
  3833. spinnerFx: 'fade',
  3834. params: null,
  3835. iframed: false,
  3836. jsonp: false
  3837. },
  3838. /**
  3839. * Shortcut to initiate and send an XHR in a single call
  3840. *
  3841. * @param String url
  3842. * @param Object options
  3843. * @return Xhr request
  3844. */
  3845. load: function(url, options) {
  3846. return new this(url, $ext({method: 'get'}, options)).send();
  3847. }
  3848. },
  3849. /**
  3850. * basic constructor
  3851. *
  3852. * @param String url
  3853. * @param Object options
  3854. */
  3855. initialize: function(url, options) {
  3856. this.initCallbacks(); // system level callbacks should be initialized before the user callbacks
  3857. this.url = url;
  3858. // copying some options to the instance level attributes
  3859. $ext(this.$super(options), this.options);
  3860. // merging in the global params
  3861. if (this.params != Xhr.Options.params) {
  3862. this.params = this.prepareData(Xhr.Options.params, this.params);
  3863. }
  3864. // removing the local spinner if it's the same as the global one
  3865. if (Xhr.Options.spinner && $(this.spinner) === $(Xhr.Options.spinner)) {
  3866. this.spinner = null;
  3867. }
  3868. },
  3869. /**
  3870. * sets a header
  3871. *
  3872. * @param name String header name
  3873. * @param value String header value
  3874. * @return Xhr self
  3875. */
  3876. setHeader: function(name, value) {
  3877. this.headers[name] = value;
  3878. return this;
  3879. },
  3880. /**
  3881. * tries to get a response header
  3882. *
  3883. * @return mixed String header value or undefined
  3884. */
  3885. getHeader: function(name) {
  3886. var value;
  3887. try {
  3888. value = this.xhr.getResponseHeader(name);
  3889. } catch(e) {}
  3890. return value;
  3891. },
  3892. /**
  3893. * checks if the request was successful
  3894. *
  3895. * @return boolean check result
  3896. */
  3897. successful: function() {
  3898. return (this.status >= 200) && (this.status < 300);
  3899. },
  3900. /**
  3901. * performs the actual request sending
  3902. *
  3903. * @param Object options
  3904. * @return Xhr self
  3905. */
  3906. send: function(params) {
  3907. var add_params = {},
  3908. url = this.url,
  3909. method = this.method.toLowerCase(),
  3910. headers = this.headers,
  3911. key, xhr;
  3912. if (method == 'put' || method == 'delete') {
  3913. add_params._method = method;
  3914. method = 'post';
  3915. }
  3916. var data = this.prepareData(this.params, this.prepareParams(params), add_params);
  3917. if (this.urlEncoded && method == 'post' && !headers['Content-type']) {
  3918. this.setHeader('Content-type', 'application/x-www-form-urlencoded;charset='+this.encoding);
  3919. }
  3920. if (method == 'get') {
  3921. if (data) { url += (url.include('?') ? '&' : '?') + data; }
  3922. data = null;
  3923. }
  3924. xhr = this.xhr = this.createXhr();
  3925. this.fire('create');
  3926. xhr.open(method, url, this.async);
  3927. xhr.onreadystatechange = this.stateChanged.bind(this);
  3928. for (key in headers) {
  3929. xhr.setRequestHeader(key, headers[key]);
  3930. }
  3931. xhr.send(data);
  3932. this.fire('request');
  3933. if (!this.async) { this.stateChanged(); }
  3934. return this;
  3935. },
  3936. /**
  3937. * elements automaticall update method, creates an Xhr request
  3938. * and updates the element innerHTML value onSuccess.
  3939. *
  3940. * @param Element element
  3941. * @param Object optional request params
  3942. * @return Xhr self
  3943. */
  3944. update: function(element, params) {
  3945. return this.onSuccess(function(r) { element.update(r.text); }).send(params);
  3946. },
  3947. /**
  3948. * stops the request processing
  3949. *
  3950. * @return Xhr self
  3951. */
  3952. cancel: function() {
  3953. var xhr = this.xhr;
  3954. if (!xhr || xhr.canceled) { return this; }
  3955. xhr.abort();
  3956. xhr.onreadystatechange = function() {};
  3957. xhr.canceled = true;
  3958. return this.fire('cancel');
  3959. },
  3960. // protected
  3961. // wrapping the original method to send references to the xhr objects
  3962. fire: function(name) {
  3963. return this.$super(name, this, this.xhr);
  3964. },
  3965. // creates new request instance
  3966. createXhr: function() {
  3967. if (this.jsonp) {
  3968. return new Xhr.JSONP(this);
  3969. } else if (this.form && this.form.first('input[type=file]')) {
  3970. return new Xhr.IFramed(this.form);
  3971. } else if ('ActiveXObject' in window){
  3972. return new ActiveXObject('MSXML2.XMLHTTP');
  3973. } else {
  3974. return new XMLHttpRequest();
  3975. }
  3976. },
  3977. // prepares user sending params
  3978. prepareParams: function(params) {
  3979. if (params && params instanceof Form) {
  3980. this.form = params;
  3981. params = params.values();
  3982. }
  3983. return params;
  3984. },
  3985. // converts all the params into a url params string
  3986. prepareData: function() {
  3987. return $A(arguments).map(function(param) {
  3988. if (!isString(param)) {
  3989. param = Object.toQueryString(param);
  3990. }
  3991. return param.blank() ? null : param;
  3992. }).compact().join('&');
  3993. },
  3994. // handles the state change
  3995. stateChanged: function() {
  3996. var xhr = this.xhr;
  3997. if (xhr.readyState != 4 || xhr.canceled) { return; }
  3998. try { this.status = xhr.status;
  3999. } catch(e) { this.status = 0; }
  4000. this.text = this.responseText = xhr.responseText;
  4001. this.xml = this.responseXML = xhr.responseXML;
  4002. this.fire('complete').fire(this.successful() ? 'success' : 'failure');
  4003. },
  4004. // called on success
  4005. tryScripts: function(response) {
  4006. var content_type = this.getHeader('Content-type');
  4007. if (this.evalResponse || (this.evalJS && /(ecma|java)script/i.test(content_type))) {
  4008. $eval(this.text);
  4009. } else if (/json/.test(content_type) && this.evalJSON) {
  4010. this.json = this.responseJSON = this.sanitizedJSON();
  4011. } else if (this.evalScripts) {
  4012. this.text.evalScripts();
  4013. }
  4014. },
  4015. // sanitizes the json-response texts
  4016. sanitizedJSON: function() {
  4017. if (!(/^[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]*$/).test(
  4018. this.text.replace(/\\./g, '@').replace(/"[^"\\\n\r]*"/g, '')
  4019. )) {
  4020. if (this.secureJSON) {
  4021. throw "JSON error: "+this.text;
  4022. }
  4023. return null;
  4024. }
  4025. return 'JSON' in window ? JSON.parse(this.text) :
  4026. (new Function("return "+this.text))();
  4027. },
  4028. // initializes the request callbacks
  4029. initCallbacks: function() {
  4030. // connecting basic callbacks
  4031. this.on({
  4032. success: 'tryScripts',
  4033. create: 'showSpinner',
  4034. complete: 'hideSpinner',
  4035. cancel: 'hideSpinner'
  4036. });
  4037. // wiring the global xhr callbacks
  4038. Xhr.EVENTS.each(function(name) {
  4039. this.on(name, function() { Xhr.fire(name, this, this.xhr); });
  4040. }, this);
  4041. },
  4042. showSpinner: function() { Xhr.showSpinner.call(this, this); },
  4043. hideSpinner: function() { Xhr.hideSpinner.call(this, this); }
  4044. });
  4045. // attaching the common spinner handling
  4046. $ext(Observer_create(Xhr), {
  4047. counter: 0,
  4048. // shows the spinner
  4049. showSpinner: function(context) {
  4050. Xhr.trySpinner(context, 'show');
  4051. },
  4052. // hides the spinner
  4053. hideSpinner: function(context) {
  4054. Xhr.trySpinner(context, 'hide');
  4055. },
  4056. trySpinner: function(context, method) {
  4057. var object = context || Xhr.Options, spinner = $(object.spinner);
  4058. if (spinner) { spinner[method](object.spinnerFx, {duration: 100}); }
  4059. },
  4060. // counts a request in
  4061. countIn: function() {
  4062. Xhr.counter ++;
  4063. Xhr.showSpinner();
  4064. },
  4065. // counts a request out
  4066. countOut: function() {
  4067. Xhr.counter --;
  4068. if (Xhr.counter < 1) {
  4069. Xhr.hideSpinner();
  4070. }
  4071. }
  4072. }).on({
  4073. create: 'countIn',
  4074. complete: 'countOut',
  4075. cancel: 'countOut'
  4076. });
  4077. /**
  4078. * Here are the Form unit Xhr extensions
  4079. *
  4080. * Credits:
  4081. * Some of the functionality inspired by
  4082. * - Prototype (http://prototypejs.org) Copyright (C) Sam Stephenson
  4083. * - jQuery (http://jquery.com) Copyright (C) John Resig
  4084. *
  4085. * Copyright (C) 2009-2011 Nikolay V. Nemshilov
  4086. */
  4087. Form.include({
  4088. /**
  4089. * sends the form via xhr request
  4090. *
  4091. * @param Options xhr request options
  4092. * @return Form this
  4093. */
  4094. send: function(options) {
  4095. options = options || {};
  4096. options.method = options.method || this._.method || 'post';
  4097. this.xhr = new Xhr(
  4098. this._.action || document.location.href,
  4099. $ext({spinner: this.first('.spinner')}, options)
  4100. )
  4101. .onComplete(this.enable.bind(this))
  4102. .onCancel(this.enable.bind(this))
  4103. .send(this);
  4104. this.disable.bind(this).delay(1); // webkit needs this async call with iframed calls
  4105. return this;
  4106. },
  4107. /**
  4108. * Cancels current Xhr request (if there are any)
  4109. *
  4110. * @return Form this
  4111. */
  4112. cancelXhr: function() {
  4113. if (this.xhr instanceof Xhr) {
  4114. this.xhr.cancel();
  4115. }
  4116. return this;
  4117. },
  4118. /**
  4119. * makes the form be remote by default
  4120. *
  4121. * @param Object default options
  4122. * @return Form this
  4123. */
  4124. remotize: function(options) {
  4125. if (!this.remote) {
  4126. this.on('submit', Form_remote_send, options);
  4127. this.remote = true;
  4128. }
  4129. return this;
  4130. },
  4131. /**
  4132. * removes the remote call hook
  4133. *
  4134. * @return Form this
  4135. */
  4136. unremotize: function() {
  4137. this.stopObserving('submit', Form_remote_send);
  4138. this.remote = false;
  4139. return this;
  4140. }
  4141. });
  4142. /**
  4143. * Catches the form submit events and sends the form remotely
  4144. *
  4145. * @param Event submit
  4146. * @param Object xhr options
  4147. * @return void
  4148. */
  4149. function Form_remote_send(event, options) {
  4150. event.stop();
  4151. this.send(options);
  4152. }
  4153. /**
  4154. * this module contains the Element unit XHR related extensions
  4155. *
  4156. * Credits:
  4157. * - jQuery (http://jquery.com) Copyright (C) John Resig
  4158. *
  4159. * Copyright (C) 2008-2011 Nikolay V. Nemshilov
  4160. */
  4161. Element.include({
  4162. /**
  4163. * performs an Xhr request to the given url
  4164. * and updates the element internals with the responseText
  4165. *
  4166. * @param String url address
  4167. * @param Object xhr options
  4168. * @return Element this
  4169. */
  4170. load: function(url, options) {
  4171. new Xhr(url, $ext({method: 'get'}, options)).update(this);
  4172. return this;
  4173. }
  4174. });
  4175. /**
  4176. * A dummy XmlHTTPRequest interface to be used in other
  4177. * fake requests
  4178. *
  4179. * Copyright (C) 2010-2011 Nikolay Nemshilov
  4180. */
  4181. Xhr.Dummy = {
  4182. open: function() {},
  4183. setRequestHeader: function() {},
  4184. onreadystatechange: function() {}
  4185. };
  4186. /**
  4187. * This unit presents a fake drop in replacement for the XmlHTTPRequest unit
  4188. * but works with an iframe targeting in the background
  4189. *
  4190. * Copyright (C) 2008-2011 Nikolay Nemshilov
  4191. */
  4192. Xhr.IFramed = new Class({
  4193. include: Xhr.Dummy,
  4194. /**
  4195. * constructor
  4196. *
  4197. * @param Form form which will be submitted via the frame
  4198. * @return void
  4199. */
  4200. initialize: function(form) {
  4201. this.form = form;
  4202. this.id = 'xhr_'+ new Date().getTime();
  4203. this.form.doc().first('body').append('<i><iframe name="'+this.id+'" id="'+this.id+
  4204. '" width="0" height="0" frameborder="0" src="about:blank"></iframe></i>',
  4205. 'after');
  4206. $(this.id).on('load', this.onLoad.bind(this));
  4207. },
  4208. send: function() {
  4209. this.form.set('target', this.id).submit();
  4210. },
  4211. onLoad: function() {
  4212. this.status = 200;
  4213. this.readyState = 4;
  4214. this.form.set('target', '');
  4215. try {
  4216. this.responseText = window[this.id].document.documentElement.innerHTML;
  4217. } catch(e) { }
  4218. this.onreadystatechange();
  4219. },
  4220. abort: function() {
  4221. $(this.id).set('src', 'about:blank');
  4222. }
  4223. });
  4224. /**
  4225. * The JSONP Xhr request tonnel
  4226. *
  4227. * Copyright (C) 2010-2011 Nikolay Nemshilov
  4228. */
  4229. Xhr.JSONP = new Class({
  4230. include: Xhr.Dummy,
  4231. prefix: 'jsonp',
  4232. /**
  4233. * Constructor
  4234. *
  4235. * @param Xhr the actual xhr request object
  4236. * @return void
  4237. */
  4238. initialize: function(xhr) {
  4239. this.xhr = xhr;
  4240. this.name = this.prefix + new Date().getTime();
  4241. this.param = (isString(xhr.jsonp) ?
  4242. xhr.jsonp : 'callback') + "=" + this.name;
  4243. this.script = $E('script', {
  4244. charset: xhr.encoding,
  4245. async: xhr.async
  4246. });
  4247. },
  4248. /**
  4249. * saving the url and method for the further use
  4250. *
  4251. * @param method String request method
  4252. * @param address String request url address
  4253. * @param Boolean async request marker
  4254. * @return void
  4255. */
  4256. open: function(method, url, async) {
  4257. this.url = url;
  4258. this.method = method;
  4259. },
  4260. /**
  4261. * Sends the actual request by inserting the script into the document body
  4262. *
  4263. * @param String data
  4264. * @return void
  4265. */
  4266. send: function(data) {
  4267. window[this.name] = this.finish.bind(this);
  4268. this.script.set('src', this.url + (this.url.include('?') ? '&' : '?') + this.param + "&" + data)
  4269. .insertTo($$('script').last(), 'after');
  4270. },
  4271. /**
  4272. * Receives the actual JSON data from the server
  4273. *
  4274. * @param Object JSON data
  4275. * @return void
  4276. */
  4277. finish: function(data) {
  4278. this.status = 200;
  4279. this.readyState = 4;
  4280. this.xhr.json = this.xhr.responseJSON = data;
  4281. this.onreadystatechange();
  4282. },
  4283. /**
  4284. * We can't really cancel a JSONP request
  4285. * but we can prevent the default handler to ckick in
  4286. *
  4287. * @return void
  4288. */
  4289. abort: function() {
  4290. window[this.name] = function() {};
  4291. }
  4292. });
  4293. /**
  4294. * Basic visual effects class
  4295. *
  4296. * Credits:
  4297. * The basic principles, structures and naming system are inspired by
  4298. * - MooTools (http://mootools.net) Copyright (C) Valerio Proietti
  4299. *
  4300. * Copyright (C) 2008-2011 Nikolay V. Nemshilov
  4301. */
  4302. var Fx = RightJS.Fx = new Class(Observer, {
  4303. extend: {
  4304. EVENTS: $w('start finish cancel'),
  4305. // named durations
  4306. Durations: {
  4307. 'short': 200,
  4308. 'normal': 400,
  4309. 'long': 800
  4310. },
  4311. // default options
  4312. Options: {
  4313. fps: IE8_OR_LESS ? 40 : 60,
  4314. duration: 'normal',
  4315. transition: 'Sin',
  4316. queue: true
  4317. },
  4318. // list of basic transitions
  4319. Transitions: {
  4320. Sin: function(i) {
  4321. return -(Math.cos(Math.PI * i) - 1) / 2;
  4322. },
  4323. Cos: function(i) {
  4324. return Math.asin((i-0.5) * 2)/Math.PI + 0.5;
  4325. },
  4326. Exp: function(i) {
  4327. return Math.pow(2, 8 * (i - 1));
  4328. },
  4329. Log: function(i) {
  4330. return 1 - Math.pow(2, - 8 * i);
  4331. },
  4332. Lin: function(i) {
  4333. return i;
  4334. }
  4335. }
  4336. },
  4337. /**
  4338. * Basic constructor
  4339. *
  4340. * @param Object options
  4341. */
  4342. initialize: function(element, options) {
  4343. this.$super(options);
  4344. this.element = $(element);
  4345. fx_register(this);
  4346. },
  4347. /**
  4348. * starts the transition
  4349. *
  4350. * @return Fx this
  4351. */
  4352. start: function() {
  4353. if (fx_add_to_queue(this, arguments)) { return this; }
  4354. var options = this.options,
  4355. duration = Fx.Durations[options.duration] || options.duration,
  4356. transition = Fx.Transitions[options.transition] || options.transition,
  4357. steps = (duration / 1000 * this.options.fps).ceil(),
  4358. interval = (1000 / this.options.fps).round();
  4359. fx_mark_current(this);
  4360. this.prepare.apply(this, arguments);
  4361. fx_start_timer(this, transition, interval, steps);
  4362. return this.fire('start', this);
  4363. },
  4364. /**
  4365. * finishes the transition
  4366. *
  4367. * @return Fx this
  4368. */
  4369. finish: function() {
  4370. fx_stop_timer(this);
  4371. fx_remove_from_queue(this);
  4372. this.fire('finish');
  4373. fx_run_next(this);
  4374. return this;
  4375. },
  4376. /**
  4377. * interrupts the transition
  4378. *
  4379. * NOTE:
  4380. * this method cancels all the scheduled effects
  4381. * in the element chain
  4382. *
  4383. * @return Fx this
  4384. */
  4385. cancel: function() {
  4386. fx_stop_timer(this);
  4387. fx_remove_from_queue(this);
  4388. return this.fire('cancel');
  4389. },
  4390. // protected
  4391. // dummy method, should be implemented in a subclass
  4392. prepare: function() {},
  4393. // dummy method, processes the element properties
  4394. render: function() {}
  4395. }),
  4396. // global effects registry
  4397. scheduled_fx = [], running_fx = [];
  4398. /**
  4399. * Registers the element in the effects queue
  4400. *
  4401. * @param Fx effect
  4402. * @return void
  4403. */
  4404. function fx_register(fx) {
  4405. var uid = $uid((fx.element || {})._ || {});
  4406. fx.ch = (scheduled_fx[uid] = scheduled_fx[uid] || []);
  4407. fx.cr = (running_fx[uid] = running_fx[uid] || []);
  4408. }
  4409. /**
  4410. * Registers the effect in the effects queue
  4411. *
  4412. * @param Fx fx
  4413. * @param Arguments original arguments list
  4414. * @return boolean true if it queued and false if it's ready to go
  4415. */
  4416. function fx_add_to_queue(fx, args) {
  4417. var chain = fx.ch, queue = fx.options.queue;
  4418. if (!chain || fx.$ch) {
  4419. return (fx.$ch = false);
  4420. }
  4421. if (queue) {
  4422. chain.push([args, fx]);
  4423. }
  4424. return queue && chain[0][1] !== fx;
  4425. }
  4426. /**
  4427. * Puts the fx into the list of currently running effects
  4428. *
  4429. * @param Fx fx
  4430. * @return void
  4431. */
  4432. function fx_mark_current(fx) {
  4433. if (fx.cr) {
  4434. fx.cr.push(fx);
  4435. }
  4436. }
  4437. /**
  4438. * Removes the fx from the queue
  4439. *
  4440. * @param Fx fx
  4441. * @return void
  4442. */
  4443. function fx_remove_from_queue(fx) {
  4444. var currents = fx.cr;
  4445. if (currents) {
  4446. currents.splice(currents.indexOf(fx), 1);
  4447. }
  4448. }
  4449. /**
  4450. * Tries to invoke the next effect in the queue
  4451. *
  4452. * @param Fx fx
  4453. * @return void
  4454. */
  4455. function fx_run_next(fx) {
  4456. var chain = fx.ch, next = chain.shift();
  4457. if ((next = chain[0])) {
  4458. next[1].$ch = true;
  4459. next[1].start.apply(next[1], next[0]);
  4460. }
  4461. }
  4462. /**
  4463. * Cancels all currently running and scheduled effects
  4464. * on the element
  4465. *
  4466. * @param Element element
  4467. * @return void
  4468. */
  4469. function fx_cancel_all(element) {
  4470. var uid = $uid(element._);
  4471. (running_fx[uid] || []).each('cancel');
  4472. (scheduled_fx[uid] || []).splice(0);
  4473. }
  4474. /**
  4475. * Initializes the fx rendering timer
  4476. *
  4477. * @param Fx fx
  4478. * @param Function transition stops calculator
  4479. * @param Float interval
  4480. * @param Integer number of steps
  4481. * @return void
  4482. */
  4483. function fx_start_timer(fx, transition, interval, steps) {
  4484. var number = 1;
  4485. fx._timer = setInterval(function() {
  4486. if (number > steps) {
  4487. fx.finish();
  4488. } else {
  4489. fx.render(transition(number/steps));
  4490. number ++;
  4491. }
  4492. }, interval);
  4493. }
  4494. /**
  4495. * Cancels the Fx rendering timer (if any)
  4496. *
  4497. * @param Fx fx
  4498. * @return void
  4499. */
  4500. function fx_stop_timer(fx) {
  4501. if (fx._timer) {
  4502. clearInterval(fx._timer);
  4503. }
  4504. }
  4505. /**
  4506. * There are the String unit extensions for the effects library
  4507. *
  4508. * Copyright (C) 2008-2009 Nikolay V. Nemshilov
  4509. */
  4510. String.COLORS = {
  4511. maroon: '#800000',
  4512. red: '#ff0000',
  4513. orange: '#ffA500',
  4514. yellow: '#ffff00',
  4515. olive: '#808000',
  4516. purple: '#800080',
  4517. fuchsia: '#ff00ff',
  4518. white: '#ffffff',
  4519. lime: '#00ff00',
  4520. green: '#008000',
  4521. navy: '#000080',
  4522. blue: '#0000ff',
  4523. aqua: '#00ffff',
  4524. teal: '#008080',
  4525. black: '#000000',
  4526. silver: '#c0c0c0',
  4527. gray: '#808080',
  4528. brown: '#a52a2a'
  4529. };
  4530. String.include({
  4531. /**
  4532. * converts a #XXX or rgb(X, X, X) sring into standard #XXXXXX color string
  4533. *
  4534. * @return String hex color
  4535. */
  4536. toHex: function() {
  4537. var match = /^#(\w)(\w)(\w)$/.exec(this);
  4538. if (match) {
  4539. match = "#"+ match[1]+match[1]+match[2]+match[2]+match[3]+match[3];
  4540. } else if ((match = /^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/.exec(this))) {
  4541. match = "#"+ match.slice(1).map(function(bit) {
  4542. bit = (bit-0).toString(16);
  4543. return bit.length == 1 ? '0'+bit : bit;
  4544. }).join('');
  4545. } else {
  4546. match = String.COLORS[this] || this;
  4547. }
  4548. return match;
  4549. },
  4550. /**
  4551. * converts a hex string into an rgb array
  4552. *
  4553. * @param boolean flag if need an array
  4554. * @return String rgb(R,G,B) or Array [R,G,B]
  4555. */
  4556. toRgb: function(array) {
  4557. var match = /#([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})/i.exec(this.toHex()||'');
  4558. if (match) {
  4559. match = match.slice(1).map('toInt', 16);
  4560. match = array ? match : 'rgb('+match+')';
  4561. }
  4562. return match;
  4563. }
  4564. });
  4565. /**
  4566. * This block contains additional Element shortcuts for effects easy handling
  4567. *
  4568. * Credits:
  4569. * Some ideas are inspired by
  4570. * - MooTools (http://mootools.net) Copyright (C) Valerio Proietti
  4571. *
  4572. * Copyright (C) 2008-2011 Nikolay V. Nemshilov
  4573. */
  4574. Element.include({
  4575. /**
  4576. * Stops all the visual effects on the element
  4577. *
  4578. * @return Element this
  4579. */
  4580. stop: function() {
  4581. fx_cancel_all(this);
  4582. return this;
  4583. },
  4584. /**
  4585. * hides the element with given visual effect
  4586. *
  4587. * @param String fx name
  4588. * @param Object fx options
  4589. * @return Element this
  4590. */
  4591. hide: function(fx, options) {
  4592. return (fx && this.visible()) ? call_fx(this, fx, ['out', options]) : this.$super();
  4593. },
  4594. /**
  4595. * shows the element with the given visual effect
  4596. *
  4597. * @param String fx name
  4598. * @param Object fx options
  4599. * @return Element this
  4600. */
  4601. show: function(fx, options) {
  4602. return (fx && !this.visible()) ? call_fx(this, fx, ['in', options]) : this.$super();
  4603. },
  4604. /**
  4605. * Toggles the element state with visual effect
  4606. *
  4607. * @param String fx name
  4608. * @param Object fx options
  4609. * @return Element this
  4610. */
  4611. toggle: function(fx, options) {
  4612. return fx ? call_fx(this, fx, ['toggle', options]) : this.$super();
  4613. },
  4614. /**
  4615. * Removes the element out of the DOM structure
  4616. *
  4617. * @param String fx name
  4618. * @param Object fx options
  4619. * @return Element this
  4620. */
  4621. remove: function(fx, options) {
  4622. return (fx && this.visible()) ? call_fx(this, fx, ['out', $ext(options || {}, {
  4623. onFinish: this.$super.bind(this)
  4624. })]) : this.$super();
  4625. },
  4626. /**
  4627. * runs the Fx.Morth effect to the given style
  4628. *
  4629. * @param style Object style
  4630. * @param options Object optional effect options
  4631. * @return Element self
  4632. */
  4633. morph: function(style, options) {
  4634. return call_fx(this, 'morph', [style, options || {}]); // <- don't replace with arguments
  4635. },
  4636. /**
  4637. * highlights the element
  4638. *
  4639. * @param start String start color
  4640. * @param end String optional end color
  4641. * @param Object effect options
  4642. * @return Element self
  4643. */
  4644. highlight: function() {
  4645. return call_fx(this, 'highlight', arguments);
  4646. },
  4647. /**
  4648. * runs the Fx.Fade effect on the element
  4649. *
  4650. * @param mixed fade direction 'in' 'out' or a float number
  4651. * @return Element self
  4652. */
  4653. fade: function() {
  4654. return call_fx(this, 'fade', arguments);
  4655. },
  4656. /**
  4657. * runs the Fx.Slide effect on the element
  4658. *
  4659. * @param String 'in' or 'out'
  4660. * @param Object effect options
  4661. * @return Element self
  4662. */
  4663. slide: function() {
  4664. return call_fx(this, 'slide', arguments);
  4665. },
  4666. /**
  4667. * Starts the smooth scrolling effect
  4668. *
  4669. * @param position Object {x: NNN, y: NNN} where to scroll
  4670. * @param options Object fx-options
  4671. * @return Element this
  4672. */
  4673. scroll: function(value, options) {
  4674. return call_fx(this, 'scroll', [value, options||{}]);
  4675. },
  4676. /**
  4677. * wraps the old scroll to be able to run it with fxes
  4678. *
  4679. * If you send two hashes then will start a smooth scrolling
  4680. * otherwise will just jump over with the usual method
  4681. *
  4682. * @return Element this
  4683. */
  4684. scrollTo: function(value, options) {
  4685. return isHash(options) ? this.scroll(value, options) : this.$super.apply(this, arguments);
  4686. }
  4687. });
  4688. /**
  4689. * Calls the visual effect on the element
  4690. *
  4691. * @param Element context
  4692. * @param String fx-name
  4693. * @param Object fx-options
  4694. * @return Element context
  4695. */
  4696. function call_fx(element, name, params) {
  4697. var args = $A(params).compact(),
  4698. options = isHash(args.last()) ? args.pop() : {},
  4699. fx = new Fx[name.capitalize()](element, options);
  4700. fx.start.apply(fx, args);
  4701. return element;
  4702. }
  4703. /**
  4704. * This class provides the basic effect for styles manipulation
  4705. *
  4706. * Copyright (C) 2008-2011 Nikolay Nemshilov
  4707. */
  4708. /////////////////////////////////////////////////////////////////////////////
  4709. // Native css-transitions based implementation
  4710. /////////////////////////////////////////////////////////////////////////////
  4711. var native_fx_prefix = ['WebkitT', 'OT', 'MozT', 'MsT', 't'].first(function(name) {
  4712. return name + 'ransition' in HTML.style;
  4713. }),
  4714. native_fx_transition = native_fx_prefix + 'ransition',
  4715. native_fx_property = native_fx_transition + 'Property',
  4716. native_fx_duration = native_fx_transition + 'Duration',
  4717. native_fx_function = native_fx_transition + 'TimingFunction',
  4718. // basic transition algorithm replacements
  4719. native_fx_functions = {
  4720. Sin: 'cubic-bezier(.3,0,.6,1)',
  4721. Cos: 'cubic-bezier(0,.3,.6,0)',
  4722. Log: 'cubic-bezier(0.6,.3,.8)',
  4723. Exp: 'cubic-bezier(.6,0,.8,.3)',
  4724. Lin: 'cubic-bezier(0,0,1,1)'
  4725. };
  4726. function native_fx_prepare(style) {
  4727. var options = this.options,
  4728. element = this.element,
  4729. element_style = element._.style,
  4730. old_style = Object.only(
  4731. element.computedStyles(),
  4732. native_fx_property,
  4733. native_fx_duration,
  4734. native_fx_function
  4735. );
  4736. function reset_transitions_style() {
  4737. for (var key in old_style) {
  4738. element_style[key] = old_style[key];
  4739. }
  4740. }
  4741. this
  4742. .onFinish(reset_transitions_style)
  4743. .onCancel(function() {
  4744. element_style[native_fx_property] = 'none';
  4745. setTimeout(reset_transitions_style, 1);
  4746. });
  4747. // setting up the transition
  4748. element_style[native_fx_property] = 'all';
  4749. element_style[native_fx_duration] = (Fx.Durations[options.duration] || options.duration) +'ms';
  4750. element_style[native_fx_function] = native_fx_functions[options.transition] || options.transition;
  4751. setTimeout(function() { element.setStyle(style); }, 0);
  4752. }
  4753. // NOTE: OPERA's css-transitions are a bit jerky so we disable them by default
  4754. Fx.Options.engine = native_fx_prefix === undefined || Browser_Opera ? 'javascript' : 'native';
  4755. ////////////////////////////////////////////////////////////////////////////
  4756. // Manual version
  4757. ////////////////////////////////////////////////////////////////////////////
  4758. Fx.Morph = new Class(Fx, {
  4759. // protected
  4760. // parepares the effect
  4761. prepare: function(style) {
  4762. if (this.options.engine === 'native' && native_fx_prefix !== undefined) {
  4763. this.render = this.transition = function() {};
  4764. native_fx_prepare.call(this, style);
  4765. } else {
  4766. var keys = style_keys(style),
  4767. before = clone_style(this.element, keys),
  4768. after = end_style(this.element, style, keys);
  4769. clean_styles(this.element, before, after);
  4770. this.before = parse_style(before);
  4771. this.after = parse_style(after);
  4772. }
  4773. },
  4774. render: function(delta) {
  4775. var before, after, value, style = this.element._.style, key, i, l;
  4776. for (key in this.after) {
  4777. before = this.before[key];
  4778. after = this.after[key];
  4779. for (i=0, l = after.length; i < l; i++) {
  4780. value = before[i] + (after[i] - before[i]) * delta;
  4781. if (after.r) {
  4782. value = Math.round(value);
  4783. }
  4784. after.t[i*2 + 1] = value;
  4785. }
  4786. style[key] = after.t.join('');
  4787. }
  4788. }
  4789. });
  4790. // a list of common style names to compact the code a bit
  4791. var directions = $w('Top Left Right Bottom');
  4792. // adds variants to the style names list
  4793. function add_variants(keys, key, variants) {
  4794. for (var i=0; i < variants.length; i++) {
  4795. keys.push(key + variants[i]);
  4796. }
  4797. }
  4798. // creates an appropriate style-keys list out of the user styles
  4799. function style_keys(style) {
  4800. var keys = [], border_types = ['Style', 'Color', 'Width'], key, i, j;
  4801. for (key in style) {
  4802. if (key.startsWith('border')) {
  4803. for (i=0; i < 3; i++) {
  4804. for (j=0; j < 4; j++) {
  4805. keys.push('border' + directions[j] + border_types[i]);
  4806. }
  4807. }
  4808. } else if (key === 'margin' || key === 'padding') {
  4809. add_variants(keys, key, directions);
  4810. } else if (key.startsWith('background')) {
  4811. add_variants(keys, 'background', ['Color', 'Position', 'PositionX', 'PositionY']);
  4812. } else if (key === 'opacity' && IE_OPACITY) {
  4813. keys.push('filter');
  4814. } else {
  4815. keys.push(key);
  4816. }
  4817. }
  4818. return keys;
  4819. }
  4820. // checks if the color is transparent
  4821. function is_transparent(color) {
  4822. return color === 'transparent' || color === 'rgba(0, 0, 0, 0)';
  4823. }
  4824. // adjusts the border-styles
  4825. function check_border_styles(element, before, after) {
  4826. for (var i=0; i < 4; i++) {
  4827. var
  4828. bd_style = 'border' + directions[i] + 'Style',
  4829. bd_width = 'border' + directions[i] + 'Width',
  4830. bd_color = 'border' + directions[i] + 'Color';
  4831. if (bd_style in before && before[bd_style] != after[bd_style]) {
  4832. var style = element._.style;
  4833. if (before[bd_style] == 'none') {
  4834. style[bd_width] = '0px';
  4835. }
  4836. style[bd_style] = after[bd_style];
  4837. if (is_transparent(before[bd_color])) {
  4838. style[bd_color] = element.getStyle('Color');
  4839. }
  4840. }
  4841. }
  4842. }
  4843. // parses the style hash into a processable format
  4844. function parse_style(values) {
  4845. var result = {}, re = /[\d\.\-]+/g, m, key, value, i;
  4846. for (key in values) {
  4847. m = values[key].match(re);
  4848. value = m.map('toFloat');
  4849. value.t = values[key].split(re);
  4850. value.r = value.t[0] === 'rgb(';
  4851. if (value.t.length == 1) { value.t.unshift(''); }
  4852. for (i=0; i < value.length; i++) {
  4853. value.t.splice(i*2 + 1, 0, value[i]);
  4854. }
  4855. result[key] = value;
  4856. }
  4857. return result;
  4858. }
  4859. // cleans up and optimizies the styles
  4860. function clean_styles(element, before, after) {
  4861. var key;
  4862. for (key in after) {
  4863. // checking the height/width options
  4864. if ((key == 'width' || key == 'height') && before[key] == 'auto') {
  4865. before[key] = element._['offset'+key.capitalize()] + 'px';
  4866. }
  4867. }
  4868. // IE opacity filter fix
  4869. if (IE_OPACITY && after.filter && !before.filter) {
  4870. before.filter = 'alpha(opacity=100)';
  4871. }
  4872. // adjusting the border style
  4873. check_border_styles(element, before, after);
  4874. // cleaing up the list
  4875. for (key in after) {
  4876. // proprocessing colors
  4877. if (after[key] !== before[key] && /color/i.test(key)) {
  4878. if (Browser_Opera) {
  4879. after[key] = after[key].replace(/"/g, '');
  4880. before[key] = before[key].replace(/"/g, '');
  4881. }
  4882. if (!is_transparent(after[key])) { after[key] = after[key].toRgb(); }
  4883. if (!is_transparent(before[key])) { before[key] = before[key].toRgb(); }
  4884. if (!after[key] || !before[key]) { after[key] = before[key] = ''; }
  4885. }
  4886. // filling up the missing size
  4887. if (/\d/.test(after[key]) && !/\d/.test(before[key])) {
  4888. before[key] = after[key].replace(/[\d\.\-]+/g, '0');
  4889. }
  4890. // removing unprocessable keys
  4891. if (after[key] === before[key] || !/\d/.test(before[key]) || !/\d/.test(after[key])) {
  4892. delete(after[key]);
  4893. delete(before[key]);
  4894. }
  4895. }
  4896. }
  4897. // cloning the element current styles hash
  4898. function clone_style(element, keys) {
  4899. var i=0, len = keys.length, style = element.computedStyles(), clean = {}, key;
  4900. for (; i < len; i++) {
  4901. key = keys[i];
  4902. if (key in style) {
  4903. clean[key] = ''+ style[key];
  4904. // libwebkit bug fix for in case of languages pack applied
  4905. if (key === 'opacity') {
  4906. clean[key] = clean[key].replace(',', '.');
  4907. }
  4908. }
  4909. }
  4910. return clean;
  4911. }
  4912. // calculating the end styles hash
  4913. function end_style(element, style, keys) {
  4914. var dummy = element.clone()
  4915. .setStyle('position:absolute;z-index:-1;visibility:hidden')
  4916. .setWidth(element.size().x)
  4917. .setStyle(style), after;
  4918. if (element.parent()) {
  4919. element.insert(dummy, 'before');
  4920. }
  4921. after = clone_style(dummy, keys);
  4922. dummy.remove();
  4923. return after;
  4924. }
  4925. /**
  4926. * the elements hightlighting effect
  4927. *
  4928. * Copyright (C) 2008-2011 Nikolay V. Nemshilov
  4929. */
  4930. Fx.Highlight = new Class(Fx.Morph, {
  4931. extend: {
  4932. Options: Object.merge(Fx.Options, {
  4933. color: '#FF8',
  4934. transition: 'Exp'
  4935. })
  4936. },
  4937. // protected
  4938. /**
  4939. * starts the transition
  4940. *
  4941. * @param high String the hightlight color
  4942. * @param back String optional fallback color
  4943. * @return self
  4944. */
  4945. prepare: function(start, end) {
  4946. var element = this.element,
  4947. element_style = element._.style,
  4948. style_name = 'backgroundColor',
  4949. end_color = end || element.getStyle(style_name);
  4950. if (is_transparent(end_color)) {
  4951. this.onFinish(function() { element_style[style_name] = 'transparent'; });
  4952. // trying to find the end color
  4953. end_color = [element].concat(element.parents())
  4954. .map('getStyle', style_name)
  4955. .reject(is_transparent)
  4956. .compact().first() || '#FFF';
  4957. }
  4958. element_style[style_name] = (start || this.options.color);
  4959. return this.$super({backgroundColor: end_color});
  4960. }
  4961. });
  4962. /**
  4963. * this is a superclass for the bidirectional effects
  4964. *
  4965. * Copyright (C) 2008-2011 Nikolay V. Nemshilov
  4966. */
  4967. Fx.Twin = new Class(Fx.Morph, {
  4968. /**
  4969. * hides the element if it meant to be switched off
  4970. *
  4971. * @return Fx self
  4972. */
  4973. finish: function() {
  4974. if (this.how === 'out') {
  4975. // calling 'prototype' to prevent circular calls from subclasses
  4976. Element.prototype.hide.call(this.element);
  4977. }
  4978. return this.$super();
  4979. },
  4980. // protected
  4981. /**
  4982. * assigns the direction of the effect in or out
  4983. *
  4984. * @param String 'in', 'out' or 'toggle', 'toggle' by default
  4985. */
  4986. setHow: function(how) {
  4987. this.how = how || 'toggle';
  4988. if (this.how === 'toggle') {
  4989. this.how = this.element.visible() ? 'out' : 'in';
  4990. }
  4991. }
  4992. });
  4993. /**
  4994. * the slide effects wrapper
  4995. *
  4996. * Copyright (C) 2008-2011 Nikolay V. Nemshilov
  4997. */
  4998. Fx.Slide = new Class(Fx.Twin, {
  4999. extend: {
  5000. Options: Object.merge(Fx.Options, {
  5001. direction: 'top'
  5002. })
  5003. },
  5004. // protected
  5005. prepare: function(how) {
  5006. this.setHow(how);
  5007. // calling 'prototype' to prevent circular calls from subclasses
  5008. var element = Element.prototype.show.call(this.element),
  5009. element_style = element._.style,
  5010. old_styles = Object.only(
  5011. element_style,
  5012. 'overflow', 'width', 'height',
  5013. 'marginTop', 'marginLeft'
  5014. );
  5015. function restore_styles() {
  5016. for (var key in old_styles) {
  5017. element_style[key] = old_styles[key];
  5018. }
  5019. }
  5020. this.onFinish(restore_styles).onCancel(restore_styles);
  5021. element_style.overflow = 'hidden';
  5022. return this.$super(fx_slide_prepare_styles(
  5023. element_style,
  5024. element.size(),
  5025. this.options.direction,
  5026. this.how
  5027. ));
  5028. }
  5029. });
  5030. function fx_slide_prepare_styles(element_style, size, direction, how) {
  5031. var style = {},
  5032. margin_left = element_style.marginLeft.toFloat() || 0,
  5033. margin_top = element_style.marginTop.toFloat() || 0,
  5034. to_right = direction === 'right',
  5035. to_bottom = direction === 'bottom',
  5036. vertical = direction === 'top' || to_bottom;
  5037. if (how === 'out') {
  5038. style[vertical ? 'height' : 'width'] = '0px';
  5039. if (to_right) {
  5040. style.marginLeft = margin_left + size.x+'px';
  5041. } else if (to_bottom) {
  5042. style.marginTop = margin_top + size.y +'px';
  5043. }
  5044. } else {
  5045. if (vertical) {
  5046. style.height = size.y + 'px';
  5047. element_style.height = '0px';
  5048. } else {
  5049. style.width = size.x + 'px';
  5050. element_style.width = '0px';
  5051. }
  5052. if (to_right) {
  5053. style.marginLeft = margin_left + 'px';
  5054. element_style.marginLeft = margin_left + size.x + 'px';
  5055. } else if (to_bottom) {
  5056. style.marginTop = margin_top + 'px';
  5057. element_style.marginTop = margin_top + size.y + 'px';
  5058. }
  5059. }
  5060. return style;
  5061. }
  5062. /**
  5063. * The opacity effects wrapper
  5064. *
  5065. * Copyright (C) 2008-2011 Nikolay V. Nemshilov
  5066. */
  5067. Fx.Fade = new Class(Fx.Twin, {
  5068. prepare: function(how) {
  5069. this.setHow(how);
  5070. if (this.how === 'in') {
  5071. // calling 'prototype' to prevent circular calls from subclasses
  5072. Element.prototype.show.call(this.element.setStyle({opacity: 0}));
  5073. }
  5074. return this.$super({opacity: this.how === 'in' ? 1 : 0});
  5075. }
  5076. });
  5077. /**
  5078. * An abstract attributes based Fx
  5079. *
  5080. * Copyright (C) 2010 Nikolay Nemshilov
  5081. */
  5082. Fx.Attr = new Class(Fx, {
  5083. prepare: function(attrs) {
  5084. this.before = {};
  5085. this.after = attrs;
  5086. var key, element = this.element._;
  5087. for (key in attrs) {
  5088. this.before[key] = element[key];
  5089. }
  5090. },
  5091. render: function(delta) {
  5092. var key, element = this.element._, before = this.before;
  5093. for (key in before) {
  5094. element[key] = before[key] + (this.after[key] - before[key]) * delta;
  5095. }
  5096. }
  5097. });
  5098. /**
  5099. * A smooth scrolling visual effect
  5100. *
  5101. * Copyright (C) 2009-2011 Nikolay Nemshilov
  5102. */
  5103. Fx.Scroll = new Class(Fx.Attr, {
  5104. initialize: function(element, options) {
  5105. element = $(element);
  5106. // swapping the actual scrollable when it's the window
  5107. this.$super(
  5108. element instanceof Window ?
  5109. element._.document[
  5110. 'body' in element._.document ? 'body' : 'documentElement'
  5111. ] : element,
  5112. options
  5113. );
  5114. },
  5115. prepare: function(value) {
  5116. var attrs = {};
  5117. if ('x' in value) { attrs.scrollLeft = value.x; }
  5118. if ('y' in value) { attrs.scrollTop = value.y; }
  5119. this.$super(attrs);
  5120. }
  5121. });
  5122. /**
  5123. * this module handles the work with cookies
  5124. *
  5125. * Credits:
  5126. * Most things in the unit are take from
  5127. * - MooTools (http://mootools.net) Copyright (C) Valerio Proietti
  5128. *
  5129. * Copyright (C) 2008-2010 Nikolay V. Nemshilov
  5130. */
  5131. var Cookie = RightJS.Cookie = new Class({
  5132. include: Options,
  5133. extend: {
  5134. // sets the cookie
  5135. set: function(name, value, options) {
  5136. return new this(name, options).set(value);
  5137. },
  5138. // gets the cookie
  5139. get: function(name) {
  5140. return new this(name).get();
  5141. },
  5142. // deletes the cookie
  5143. remove: function(name) {
  5144. return new this(name).remove();
  5145. },
  5146. // checks if the cookies are enabled
  5147. enabled: function() {
  5148. document.cookie = "__t=1";
  5149. return document.cookie.indexOf("__t=1")!=-1;
  5150. },
  5151. // some basic options
  5152. Options: {
  5153. secure: false,
  5154. document: document
  5155. }
  5156. },
  5157. /**
  5158. * constructor
  5159. * @param String cookie name
  5160. * @param Object options
  5161. * @return void
  5162. */
  5163. initialize: function(name, options) {
  5164. this.name = name;
  5165. this.setOptions(options);
  5166. },
  5167. /**
  5168. * sets the cookie with the name
  5169. *
  5170. * @param mixed value
  5171. * @return Cookie this
  5172. */
  5173. set: function(data) {
  5174. var value = encodeURIComponent(data), options = this.options;
  5175. if (options.domain) { value += '; domain=' + options.domain; }
  5176. if (options.path) { value += '; path=' + options.path; }
  5177. if (options.duration) {
  5178. var date = new Date();
  5179. date.setTime(date.getTime() + options.duration * 24 * 60 * 60 * 1000);
  5180. value += '; expires=' + date.toGMTString();
  5181. }
  5182. if (options.secure) { value += '; secure'; }
  5183. options.document.cookie = this.name + '=' + value;
  5184. return this;
  5185. },
  5186. /**
  5187. * searches for a cookie with the name
  5188. *
  5189. * @return mixed saved value or null if nothing found
  5190. */
  5191. get: function() {
  5192. var value = this.options.document.cookie.match(
  5193. '(?:^|;)\\s*' + RegExp.escape(this.name) + '=([^;]*)'
  5194. );
  5195. return value ? decodeURIComponent(value[1]) : null;
  5196. },
  5197. /**
  5198. * removes the cookie
  5199. *
  5200. * @return Cookie this
  5201. */
  5202. remove: function() {
  5203. this.options.duration = -1;
  5204. return this.set('');
  5205. }
  5206. });
  5207. // globalizing the top-level variables
  5208. $ext(window, Object.without(RightJS, 'version', 'modules'));
  5209. return RightJS;
  5210. })(window, document, Object, Array, String, Function, Number, Math);
  5211. /**
  5212. * The old browsers support patch loading script
  5213. * will be included in the core file when it's built
  5214. * with the no-olds option
  5215. *
  5216. * Basically it just checks all the script tags on the page
  5217. * finds the core inclusion tag and uses it's src attribute
  5218. * to dynamically load the olds patch
  5219. *
  5220. * Copyright (C) 2009-2011 Nikolay V. Nemshilov
  5221. */
  5222. if (RightJS.Browser.OLD) {
  5223. (function(d) {
  5224. var script = d.createElement('script'),
  5225. scripts = d.getElementsByTagName('script'),
  5226. rjs_spt = scripts[scripts.length - 1];
  5227. script.src = rjs_spt.src.replace(/(^|\/)(right)([^\/]+)$/, '$1$2-olds$3');
  5228. rjs_spt.parentNode.appendChild(script);
  5229. })(document);
  5230. }