12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648 |
- /**
- * Handsontable 0.10.0-beta4
- * Handsontable is a simple jQuery plugin for editable tables with basic copy-paste compatibility with Excel and Google Docs
- *
- * Copyright 2012, Marcin Warpechowski
- * Licensed under the MIT license.
- * http://handsontable.com/
- *
- * Date: Wed Nov 27 2013 14:18:10 GMT+0100 (CET)
- */
- /*jslint white: true, browser: true, plusplus: true, indent: 4, maxerr: 50 */
- var Handsontable = { //class namespace
- extension: {}, //extenstion namespace
- helper: {} //helper namespace
- };
- (function ($, window, Handsontable) {
- "use strict";
- Handsontable.activeGuid = null;
- /**
- * Handsontable constructor
- * @param rootElement The jQuery element in which Handsontable DOM will be inserted
- * @param userSettings
- * @constructor
- */
- Handsontable.Core = function (rootElement, userSettings) {
- var priv
- , datamap
- , grid
- , selection
- , editorManager
- , autofill
- , instance = this
- , GridSettings = function () {};
- Handsontable.helper.extend(GridSettings.prototype, DefaultSettings.prototype); //create grid settings as a copy of default settings
- Handsontable.helper.extend(GridSettings.prototype, userSettings); //overwrite defaults with user settings
- Handsontable.helper.extend(GridSettings.prototype, expandType(userSettings));
- this.rootElement = rootElement;
- var $document = $(document.documentElement);
- var $body = $(document.body);
- this.guid = 'ht_' + Handsontable.helper.randomString(); //this is the namespace for global events
- if (!this.rootElement[0].id) {
- this.rootElement[0].id = this.guid; //if root element does not have an id, assign a random id
- }
- priv = {
- cellSettings: [],
- columnSettings: [],
- columnsSettingConflicts: ['data', 'width'],
- settings: new GridSettings(), // current settings instance
- settingsFromDOM: {},
- selStart: new Handsontable.SelectionPoint(),
- selEnd: new Handsontable.SelectionPoint(),
- isPopulated: null,
- scrollable: null,
- extensions: {},
- colToProp: null,
- propToCol: null,
- dataSchema: null,
- dataType: 'array',
- firstRun: true
- };
- datamap = {
- recursiveDuckSchema: function (obj) {
- var schema;
- if ($.isPlainObject(obj)) {
- schema = {};
- for (var i in obj) {
- if (obj.hasOwnProperty(i)) {
- if ($.isPlainObject(obj[i])) {
- schema[i] = datamap.recursiveDuckSchema(obj[i]);
- }
- else {
- schema[i] = null;
- }
- }
- }
- }
- else {
- schema = [];
- }
- return schema;
- },
- recursiveDuckColumns: function (schema, lastCol, parent) {
- var prop, i;
- if (typeof lastCol === 'undefined') {
- lastCol = 0;
- parent = '';
- }
- if ($.isPlainObject(schema)) {
- for (i in schema) {
- if (schema.hasOwnProperty(i)) {
- if (schema[i] === null) {
- prop = parent + i;
- priv.colToProp.push(prop);
- priv.propToCol[prop] = lastCol;
- lastCol++;
- }
- else {
- lastCol = datamap.recursiveDuckColumns(schema[i], lastCol, i + '.');
- }
- }
- }
- }
- return lastCol;
- },
- createMap: function () {
- if (typeof datamap.getSchema() === "undefined") {
- throw new Error("trying to create `columns` definition but you didnt' provide `schema` nor `data`");
- }
- var i, ilen, schema = datamap.getSchema();
- priv.colToProp = [];
- priv.propToCol = {};
- if (priv.settings.columns) {
- for (i = 0, ilen = priv.settings.columns.length; i < ilen; i++) {
- priv.colToProp[i] = priv.settings.columns[i].data;
- priv.propToCol[priv.settings.columns[i].data] = i;
- }
- }
- else {
- datamap.recursiveDuckColumns(schema);
- }
- },
- colToProp: function (col) {
- col = Handsontable.PluginHooks.execute(instance, 'modifyCol', col);
- if (priv.colToProp && typeof priv.colToProp[col] !== 'undefined') {
- return priv.colToProp[col];
- }
- else {
- return col;
- }
- },
- propToCol: function (prop) {
- var col;
- if (typeof priv.propToCol[prop] !== 'undefined') {
- col = priv.propToCol[prop];
- }
- else {
- col = prop;
- }
- col = Handsontable.PluginHooks.execute(instance, 'modifyCol', col);
- return col;
- },
- getSchema: function () {
- if (priv.settings.dataSchema) {
- if (typeof priv.settings.dataSchema === 'function') {
- return priv.settings.dataSchema();
- }
- return priv.settings.dataSchema;
- }
- return priv.duckDataSchema;
- },
- /**
- * Creates row at the bottom of the data array
- * @param {Number} [index] Optional. Index of the row before which the new row will be inserted
- */
- createRow: function (index, amount) {
- var row
- , colCount = instance.countCols()
- , numberOfCreatedRows = 0
- , currentIndex;
- if (!amount) {
- amount = 1;
- }
- if (typeof index !== 'number' || index >= instance.countRows()) {
- index = instance.countRows();
- }
- currentIndex = index;
- while (numberOfCreatedRows < amount && instance.countRows() < priv.settings.maxRows) {
- if (priv.dataType === 'array') {
- row = [];
- for (var c = 0; c < colCount; c++) {
- row.push(null);
- }
- }
- else if (priv.dataType === 'function') {
- row = priv.settings.dataSchema(index);
- }
- else {
- row = $.extend(true, {}, datamap.getSchema());
- }
- if (index === instance.countRows()) {
- GridSettings.prototype.data.push(row);
- }
- else {
- GridSettings.prototype.data.splice(index, 0, row);
- }
- numberOfCreatedRows++;
- currentIndex++;
- }
- instance.PluginHooks.run('afterCreateRow', index, numberOfCreatedRows);
- instance.forceFullRender = true; //used when data was changed
- return numberOfCreatedRows;
- },
- /**
- * Creates col at the right of the data array
- * @param {Number} [index] Optional. Index of the column before which the new column will be inserted
- * * @param {Number} [amount] Optional.
- */
- createCol: function (index, amount) {
- if (priv.dataType === 'object' || priv.settings.columns) {
- throw new Error("Cannot create new column. When data source in an object, you can only have as much columns as defined in first data row, data schema or in the 'columns' setting");
- }
- var rlen = instance.countRows()
- , data = GridSettings.prototype.data
- , constructor
- , numberOfCreatedCols = 0
- , currentIndex;
- if (!amount) {
- amount = 1;
- }
- currentIndex = index;
- while (numberOfCreatedCols < amount && instance.countCols() < priv.settings.maxCols){
- constructor = Handsontable.helper.columnFactory(GridSettings, priv.columnsSettingConflicts);
- if (typeof index !== 'number' || index >= instance.countCols()) {
- for (var r = 0; r < rlen; r++) {
- if (typeof data[r] === 'undefined') {
- data[r] = [];
- }
- data[r].push(null);
- }
- // Add new column constructor
- priv.columnSettings.push(constructor);
- }
- else {
- for (var r = 0 ; r < rlen; r++) {
- data[r].splice(currentIndex, 0, null);
- }
- // Add new column constructor at given index
- priv.columnSettings.splice(currentIndex, 0, constructor);
- }
- numberOfCreatedCols++;
- currentIndex++;
- }
- instance.PluginHooks.run('afterCreateCol', index, numberOfCreatedCols);
- instance.forceFullRender = true; //used when data was changed
- return numberOfCreatedCols;
- },
- /**
- * Removes row from the data array
- * @param {Number} [index] Optional. Index of the row to be removed. If not provided, the last row will be removed
- * @param {Number} [amount] Optional. Amount of the rows to be removed. If not provided, one row will be removed
- */
- removeRow: function (index, amount) {
- if (!amount) {
- amount = 1;
- }
- if (typeof index !== 'number') {
- index = -amount;
- }
- index = (instance.countRows() + index) % instance.countRows();
- // We have to map the physical row ids to logical and than perform removing with (possibly) new row id
- var logicRows = this.physicalRowsToLogical(index, amount);
- var actionWasNotCancelled = instance.PluginHooks.execute('beforeRemoveRow', index, amount);
- if(actionWasNotCancelled === false){
- return;
- }
- var newData = GridSettings.prototype.data.filter(function (row, index) {
- return logicRows.indexOf(index) == -1;
- });
- GridSettings.prototype.data.length = 0;
- Array.prototype.push.apply(GridSettings.prototype.data, newData);
- instance.PluginHooks.run('afterRemoveRow', index, amount);
- instance.forceFullRender = true; //used when data was changed
- },
- /**
- * Removes column from the data array
- * @param {Number} [index] Optional. Index of the column to be removed. If not provided, the last column will be removed
- * @param {Number} [amount] Optional. Amount of the columns to be removed. If not provided, one column will be removed
- */
- removeCol: function (index, amount) {
- if (priv.dataType === 'object' || priv.settings.columns) {
- throw new Error("cannot remove column with object data source or columns option specified");
- }
- if (!amount) {
- amount = 1;
- }
- if (typeof index !== 'number') {
- index = -amount;
- }
- index = (instance.countCols() + index) % instance.countCols();
- var actionWasNotCancelled = instance.PluginHooks.execute('beforeRemoveCol', index, amount);
- if(actionWasNotCancelled === false){
- return;
- }
- var data = GridSettings.prototype.data;
- for (var r = 0, rlen = instance.countRows(); r < rlen; r++) {
- data[r].splice(index, amount);
- }
- priv.columnSettings.splice(index, amount);
- instance.PluginHooks.run('afterRemoveCol', index, amount);
- instance.forceFullRender = true; //used when data was changed
- },
- /**
- * Add / removes data from the column
- * @param {Number} col Index of column in which do you want to do splice.
- * @param {Number} index Index at which to start changing the array. If negative, will begin that many elements from the end
- * @param {Number} amount An integer indicating the number of old array elements to remove. If amount is 0, no elements are removed
- * param {...*} elements Optional. The elements to add to the array. If you don't specify any elements, spliceCol simply removes elements from the array
- */
- spliceCol: function (col, index, amount/*, elements...*/) {
- var elements = 4 <= arguments.length ? [].slice.call(arguments, 3) : [];
- var colData = instance.getDataAtCol(col);
- var removed = colData.slice(index, index + amount);
- var after = colData.slice(index + amount);
- Handsontable.helper.extendArray(elements, after);
- var i = 0;
- while (i < amount) {
- elements.push(null); //add null in place of removed elements
- i++;
- }
- Handsontable.helper.to2dArray(elements);
- instance.populateFromArray(index, col, elements, null, null, 'spliceCol');
- return removed;
- },
- /**
- * Add / removes data from the row
- * @param {Number} row Index of row in which do you want to do splice.
- * @param {Number} index Index at which to start changing the array. If negative, will begin that many elements from the end
- * @param {Number} amount An integer indicating the number of old array elements to remove. If amount is 0, no elements are removed
- * param {...*} elements Optional. The elements to add to the array. If you don't specify any elements, spliceCol simply removes elements from the array
- */
- spliceRow: function (row, index, amount/*, elements...*/) {
- var elements = 4 <= arguments.length ? [].slice.call(arguments, 3) : [];
- var rowData = instance.getDataAtRow(row);
- var removed = rowData.slice(index, index + amount);
- var after = rowData.slice(index + amount);
- Handsontable.helper.extendArray(elements, after);
- var i = 0;
- while (i < amount) {
- elements.push(null); //add null in place of removed elements
- i++;
- }
- instance.populateFromArray(row, index, [elements], null, null, 'spliceRow');
- return removed;
- },
- /**
- * Returns single value from the data array
- * @param {Number} row
- * @param {Number} prop
- */
- getVars: {},
- get: function (row, prop) {
- datamap.getVars.row = row;
- datamap.getVars.prop = prop;
- instance.PluginHooks.run('beforeGet', datamap.getVars);
- if (typeof datamap.getVars.prop === 'string' && datamap.getVars.prop.indexOf('.') > -1) {
- var sliced = datamap.getVars.prop.split(".");
- var out = priv.settings.data[datamap.getVars.row];
- if (!out) {
- return null;
- }
- for (var i = 0, ilen = sliced.length; i < ilen; i++) {
- out = out[sliced[i]];
- if (typeof out === 'undefined') {
- return null;
- }
- }
- return out;
- }
- else if (typeof datamap.getVars.prop === 'function') {
- /**
- * allows for interacting with complex structures, for example
- * d3/jQuery getter/setter properties:
- *
- * {columns: [{
- * data: function(row, value){
- * if(arguments.length === 1){
- * return row.property();
- * }
- * row.property(value);
- * }
- * }]}
- */
- return datamap.getVars.prop(priv.settings.data.slice(
- datamap.getVars.row,
- datamap.getVars.row + 1
- )[0]);
- }
- else {
- return priv.settings.data[datamap.getVars.row] ? priv.settings.data[datamap.getVars.row][datamap.getVars.prop] : null;
- }
- },
- /**
- * Saves single value to the data array
- * @param {Number} row
- * @param {Number} prop
- * @param {String} value
- * @param {String} [source] Optional. Source of hook runner.
- */
- setVars: {},
- set: function (row, prop, value, source) {
- datamap.setVars.row = row;
- datamap.setVars.prop = prop;
- datamap.setVars.value = value;
- instance.PluginHooks.run('beforeSet', datamap.setVars, source || "datamapGet");
- if (typeof datamap.setVars.prop === 'string' && datamap.setVars.prop.indexOf('.') > -1) {
- var sliced = datamap.setVars.prop.split(".");
- var out = priv.settings.data[datamap.setVars.row];
- for (var i = 0, ilen = sliced.length - 1; i < ilen; i++) {
- out = out[sliced[i]];
- }
- out[sliced[i]] = datamap.setVars.value;
- }
- else if (typeof datamap.setVars.prop === 'function') {
- /* see the `function` handler in `get` */
- datamap.setVars.prop(priv.settings.data.slice(
- datamap.setVars.row,
- datamap.setVars.row + 1
- )[0], datamap.setVars.value);
- }
- else {
- priv.settings.data[datamap.setVars.row][datamap.setVars.prop] = datamap.setVars.value;
- }
- },
- /**
- * This ridiculous piece of code maps rows Id that are present in table data to those displayed for user.
- * The trick is, the physical row id (stored in settings.data) is not necessary the same
- * as the logical (displayed) row id (e.g. when sorting is applied).
- */
- physicalRowsToLogical: function (index, amount) {
- var physicRow = (GridSettings.prototype.data.length + index) % GridSettings.prototype.data.length;
- var logicRows = [];
- var rowsToRemove = amount;
- while (physicRow < GridSettings.prototype.data.length && rowsToRemove) {
- this.get(physicRow, 0); //this performs an actual mapping and saves the result to getVars
- logicRows.push(this.getVars.row);
- rowsToRemove--;
- physicRow++;
- }
- return logicRows;
- },
- /**
- * Clears the data array
- */
- clear: function () {
- for (var r = 0; r < instance.countRows(); r++) {
- for (var c = 0; c < instance.countCols(); c++) {
- datamap.set(r, datamap.colToProp(c), '');
- }
- }
- },
- /**
- * Returns the data array
- * @return {Array}
- */
- getAll: function () {
- return priv.settings.data;
- },
- /**
- * Returns data range as array
- * @param {Object} start Start selection position
- * @param {Object} end End selection position
- * @return {Array}
- */
- getRange: function (start, end) {
- var r, rlen, c, clen, output = [], row;
- rlen = Math.max(start.row, end.row);
- clen = Math.max(start.col, end.col);
- for (r = Math.min(start.row, end.row); r <= rlen; r++) {
- row = [];
- for (c = Math.min(start.col, end.col); c <= clen; c++) {
- row.push(datamap.get(r, datamap.colToProp(c)));
- }
- output.push(row);
- }
- return output;
- },
- /**
- * Return data as text (tab separated columns)
- * @param {Object} start (Optional) Start selection position
- * @param {Object} end (Optional) End selection position
- * @return {String}
- */
- getText: function (start, end) {
- return SheetClip.stringify(datamap.getRange(start, end));
- }
- };
- grid = {
- /**
- * Inserts or removes rows and columns
- * @param {String} action Possible values: "insert_row", "insert_col", "remove_row", "remove_col"
- * @param {Number} index
- * @param {Number} amount
- * @param {String} [source] Optional. Source of hook runner.
- * @param {Boolean} [keepEmptyRows] Optional. Flag for preventing deletion of empty rows.
- */
- alter: function (action, index, amount, source, keepEmptyRows) {
- var delta;
- amount = amount || 1;
- switch (action) {
- case "insert_row":
- delta = datamap.createRow(index, amount);
- if (delta) {
- if (priv.selStart.exists() && priv.selStart.row() >= index) {
- priv.selStart.row(priv.selStart.row() + delta);
- selection.transformEnd(delta, 0); //will call render() internally
- }
- else {
- selection.refreshBorders(); //it will call render and prepare methods
- }
- }
- break;
- case "insert_col":
- delta = datamap.createCol(index, amount);
- if (delta) {
- if(Handsontable.helper.isArray(instance.getSettings().colHeaders)){
- var spliceArray = [index, 0];
- spliceArray.length += delta; //inserts empty (undefined) elements at the end of an array
- Array.prototype.splice.apply(instance.getSettings().colHeaders, spliceArray); //inserts empty (undefined) elements into the colHeader array
- }
- if (priv.selStart.exists() && priv.selStart.col() >= index) {
- priv.selStart.col(priv.selStart.col() + delta);
- selection.transformEnd(0, delta); //will call render() internally
- }
- else {
- selection.refreshBorders(); //it will call render and prepare methods
- }
- }
- break;
- case "remove_row":
- datamap.removeRow(index, amount);
- priv.cellSettings.splice(index, amount);
- grid.adjustRowsAndCols();
- selection.refreshBorders(); //it will call render and prepare methods
- break;
- case "remove_col":
- datamap.removeCol(index, amount);
- for(var row = 0, len = datamap.getAll().length; row < len; row++){
- if(row in priv.cellSettings){ //if row hasn't been rendered it wouldn't have cellSettings
- priv.cellSettings[row].splice(index, amount);
- }
- }
- if(Handsontable.helper.isArray(instance.getSettings().colHeaders)){
- if(typeof index == 'undefined'){
- index = -1;
- }
- instance.getSettings().colHeaders.splice(index, amount);
- }
- priv.columnSettings.splice(index, amount);
- grid.adjustRowsAndCols();
- selection.refreshBorders(); //it will call render and prepare methods
- break;
- default:
- throw new Error('There is no such action "' + action + '"');
- break;
- }
- if (!keepEmptyRows) {
- grid.adjustRowsAndCols(); //makes sure that we did not add rows that will be removed in next refresh
- }
- },
- /**
- * Makes sure there are empty rows at the bottom of the table
- */
- adjustRowsAndCols: function () {
- var r, rlen, emptyRows = instance.countEmptyRows(true), emptyCols;
- //should I add empty rows to data source to meet minRows?
- rlen = instance.countRows();
- if (rlen < priv.settings.minRows) {
- for (r = 0; r < priv.settings.minRows - rlen; r++) {
- datamap.createRow();
- }
- }
- //should I add empty rows to meet minSpareRows?
- if (emptyRows < priv.settings.minSpareRows) {
- for (; emptyRows < priv.settings.minSpareRows && instance.countRows() < priv.settings.maxRows; emptyRows++) {
- datamap.createRow();
- }
- }
- //count currently empty cols
- emptyCols = instance.countEmptyCols(true);
- //should I add empty cols to meet minCols?
- if (!priv.settings.columns && instance.countCols() < priv.settings.minCols) {
- for (; instance.countCols() < priv.settings.minCols; emptyCols++) {
- datamap.createCol();
- }
- }
- //should I add empty cols to meet minSpareCols?
- if (!priv.settings.columns && priv.dataType === 'array' && emptyCols < priv.settings.minSpareCols) {
- for (; emptyCols < priv.settings.minSpareCols && instance.countCols() < priv.settings.maxCols; emptyCols++) {
- datamap.createCol();
- }
- }
- if (priv.settings.enterBeginsEditing) {
- for (; (((priv.settings.minRows || priv.settings.minSpareRows) && instance.countRows() > priv.settings.minRows) && (priv.settings.minSpareRows && emptyRows > priv.settings.minSpareRows)); emptyRows--) {
- datamap.removeRow();
- }
- }
- if (priv.settings.enterBeginsEditing && !priv.settings.columns) {
- for (; (((priv.settings.minCols || priv.settings.minSpareCols) && instance.countCols() > priv.settings.minCols) && (priv.settings.minSpareCols && emptyCols > priv.settings.minSpareCols)); emptyCols--) {
- datamap.removeCol();
- }
- }
- var rowCount = instance.countRows();
- var colCount = instance.countCols();
- if (rowCount === 0 || colCount === 0) {
- selection.deselect();
- }
- if (priv.selStart.exists()) {
- var selectionChanged;
- var fromRow = priv.selStart.row();
- var fromCol = priv.selStart.col();
- var toRow = priv.selEnd.row();
- var toCol = priv.selEnd.col();
- //if selection is outside, move selection to last row
- if (fromRow > rowCount - 1) {
- fromRow = rowCount - 1;
- selectionChanged = true;
- if (toRow > fromRow) {
- toRow = fromRow;
- }
- } else if (toRow > rowCount - 1) {
- toRow = rowCount - 1;
- selectionChanged = true;
- if (fromRow > toRow) {
- fromRow = toRow;
- }
- }
- //if selection is outside, move selection to last row
- if (fromCol > colCount - 1) {
- fromCol = colCount - 1;
- selectionChanged = true;
- if (toCol > fromCol) {
- toCol = fromCol;
- }
- } else if (toCol > colCount - 1) {
- toCol = colCount - 1;
- selectionChanged = true;
- if (fromCol > toCol) {
- fromCol = toCol;
- }
- }
- if (selectionChanged) {
- instance.selectCell(fromRow, fromCol, toRow, toCol);
- }
- }
- },
- /**
- * Populate cells at position with 2d array
- * @param {Object} start Start selection position
- * @param {Array} input 2d array
- * @param {Object} [end] End selection position (only for drag-down mode)
- * @param {String} [source="populateFromArray"]
- * @param {String} [method="overwrite"]
- * @return {Object|undefined} ending td in pasted area (only if any cell was changed)
- */
- populateFromArray: function (start, input, end, source, method) {
- var r, rlen, c, clen, setData = [], current = {};
- rlen = input.length;
- if (rlen === 0) {
- return false;
- }
- var repeatCol
- , repeatRow
- , cmax
- , rmax;
- // insert data with specified pasteMode method
- switch (method) {
- case 'shift_down' :
- repeatCol = end ? end.col - start.col + 1 : 0;
- repeatRow = end ? end.row - start.row + 1 : 0;
- input = Handsontable.helper.translateRowsToColumns(input);
- for (c = 0, clen = input.length, cmax = Math.max(clen, repeatCol); c < cmax; c++) {
- if (c < clen) {
- for (r = 0, rlen = input[c].length; r < repeatRow - rlen; r++) {
- input[c].push(input[c][r % rlen]);
- }
- input[c].unshift(start.col + c, start.row, 0);
- instance.spliceCol.apply(instance, input[c]);
- }
- else {
- input[c % clen][0] = start.col + c;
- instance.spliceCol.apply(instance, input[c % clen]);
- }
- }
- break;
- case 'shift_right' :
- repeatCol = end ? end.col - start.col + 1 : 0;
- repeatRow = end ? end.row - start.row + 1 : 0;
- for (r = 0, rlen = input.length, rmax = Math.max(rlen, repeatRow); r < rmax; r++) {
- if (r < rlen) {
- for (c = 0, clen = input[r].length; c < repeatCol - clen; c++) {
- input[r].push(input[r][c % clen]);
- }
- input[r].unshift(start.row + r, start.col, 0);
- instance.spliceRow.apply(instance, input[r]);
- }
- else {
- input[r % rlen][0] = start.row + r;
- instance.spliceRow.apply(instance, input[r % rlen]);
- }
- }
- break;
- case 'overwrite' :
- default:
- // overwrite and other not specified options
- current.row = start.row;
- current.col = start.col;
- for (r = 0; r < rlen; r++) {
- if ((end && current.row > end.row) || (!priv.settings.minSpareRows && current.row > instance.countRows() - 1) || (current.row >= priv.settings.maxRows)) {
- break;
- }
- current.col = start.col;
- clen = input[r] ? input[r].length : 0;
- for (c = 0; c < clen; c++) {
- if ((end && current.col > end.col) || (!priv.settings.minSpareCols && current.col > instance.countCols() - 1) || (current.col >= priv.settings.maxCols)) {
- break;
- }
- if (!instance.getCellMeta(current.row, current.col).readOnly) {
- setData.push([current.row, current.col, input[r][c]]);
- }
- current.col++;
- if (end && c === clen - 1) {
- c = -1;
- }
- }
- current.row++;
- if (end && r === rlen - 1) {
- r = -1;
- }
- }
- instance.setDataAtCell(setData, null, null, source || 'populateFromArray');
- break;
- }
- },
- /**
- * Returns the top left (TL) and bottom right (BR) selection coordinates
- * @param {Object[]} coordsArr
- * @returns {Object}
- */
- getCornerCoords: function (coordsArr) {
- function mapProp(func, array, prop) {
- function getProp(el) {
- return el[prop];
- }
- if (Array.prototype.map) {
- return func.apply(Math, array.map(getProp));
- }
- return func.apply(Math, $.map(array, getProp));
- }
- return {
- TL: {
- row: mapProp(Math.min, coordsArr, "row"),
- col: mapProp(Math.min, coordsArr, "col")
- },
- BR: {
- row: mapProp(Math.max, coordsArr, "row"),
- col: mapProp(Math.max, coordsArr, "col")
- }
- };
- },
- /**
- * Returns array of td objects given start and end coordinates
- */
- getCellsAtCoords: function (start, end) {
- var corners = grid.getCornerCoords([start, end]);
- var r, c, output = [];
- for (r = corners.TL.row; r <= corners.BR.row; r++) {
- for (c = corners.TL.col; c <= corners.BR.col; c++) {
- output.push(instance.view.getCellAtCoords({
- row: r,
- col: c
- }));
- }
- }
- return output;
- }
- };
- this.selection = selection = { //this public assignment is only temporary
- inProgress: false,
- /**
- * Sets inProgress to true. This enables onSelectionEnd and onSelectionEndByProp to function as desired
- */
- begin: function () {
- instance.selection.inProgress = true;
- },
- /**
- * Sets inProgress to false. Triggers onSelectionEnd and onSelectionEndByProp
- */
- finish: function () {
- var sel = instance.getSelected();
- instance.PluginHooks.run("afterSelectionEnd", sel[0], sel[1], sel[2], sel[3]);
- instance.PluginHooks.run("afterSelectionEndByProp", sel[0], instance.colToProp(sel[1]), sel[2], instance.colToProp(sel[3]));
- instance.selection.inProgress = false;
- },
- isInProgress: function () {
- return instance.selection.inProgress;
- },
- /**
- * Starts selection range on given td object
- * @param {Object} coords
- */
- setRangeStart: function (coords) {
- priv.selStart.coords(coords);
- selection.setRangeEnd(coords);
- },
- /**
- * Ends selection range on given td object
- * @param {Object} coords
- * @param {Boolean} [scrollToCell=true] If true, viewport will be scrolled to range end
- */
- setRangeEnd: function (coords, scrollToCell) {
- instance.selection.begin();
- priv.selEnd.coords(coords);
- if (!priv.settings.multiSelect) {
- priv.selStart.coords(coords);
- }
- //set up current selection
- instance.view.wt.selections.current.clear();
- instance.view.wt.selections.current.add(priv.selStart.arr());
- //set up area selection
- instance.view.wt.selections.area.clear();
- if (selection.isMultiple()) {
- instance.view.wt.selections.area.add(priv.selStart.arr());
- instance.view.wt.selections.area.add(priv.selEnd.arr());
- }
- //set up highlight
- if (priv.settings.currentRowClassName || priv.settings.currentColClassName) {
- instance.view.wt.selections.highlight.clear();
- instance.view.wt.selections.highlight.add(priv.selStart.arr());
- instance.view.wt.selections.highlight.add(priv.selEnd.arr());
- }
- //trigger handlers
- instance.PluginHooks.run("afterSelection", priv.selStart.row(), priv.selStart.col(), priv.selEnd.row(), priv.selEnd.col());
- instance.PluginHooks.run("afterSelectionByProp", priv.selStart.row(), datamap.colToProp(priv.selStart.col()), priv.selEnd.row(), datamap.colToProp(priv.selEnd.col()));
- if (scrollToCell !== false) {
- instance.view.scrollViewport(coords);
- }
- selection.refreshBorders();
- },
- /**
- * Destroys editor, redraws borders around cells, prepares editor
- * @param {Boolean} revertOriginal
- * @param {Boolean} keepEditor
- */
- refreshBorders: function (revertOriginal, keepEditor) {
- if (!keepEditor) {
- editorManager.destroyEditor(revertOriginal);
- }
- instance.view.render();
- if (selection.isSelected() && !keepEditor) {
- editorManager.prepareEditor();
- }
- },
- /**
- * Returns information if we have a multiselection
- * @return {Boolean}
- */
- isMultiple: function () {
- return !(priv.selEnd.col() === priv.selStart.col() && priv.selEnd.row() === priv.selStart.row());
- },
- /**
- * Selects cell relative to current cell (if possible)
- */
- transformStart: function (rowDelta, colDelta, force) {
- if (priv.selStart.row() + rowDelta > instance.countRows() - 1) {
- if (force && priv.settings.minSpareRows > 0) {
- instance.alter("insert_row", instance.countRows());
- }
- else if (priv.settings.autoWrapCol) {
- rowDelta = 1 - instance.countRows();
- colDelta = priv.selStart.col() + colDelta == instance.countCols() - 1 ? 1 - instance.countCols() : 1;
- }
- }
- else if (priv.settings.autoWrapCol && priv.selStart.row() + rowDelta < 0 && priv.selStart.col() + colDelta >= 0) {
- rowDelta = instance.countRows() - 1;
- colDelta = priv.selStart.col() + colDelta == 0 ? instance.countCols() - 1 : -1;
- }
- if (priv.selStart.col() + colDelta > instance.countCols() - 1) {
- if (force && priv.settings.minSpareCols > 0) {
- instance.alter("insert_col", instance.countCols());
- }
- else if (priv.settings.autoWrapRow) {
- rowDelta = priv.selStart.row() + rowDelta == instance.countRows() - 1 ? 1 - instance.countRows() : 1;
- colDelta = 1 - instance.countCols();
- }
- }
- else if (priv.settings.autoWrapRow && priv.selStart.col() + colDelta < 0 && priv.selStart.row() + rowDelta >= 0) {
- rowDelta = priv.selStart.row() + rowDelta == 0 ? instance.countRows() - 1 : -1;
- colDelta = instance.countCols() - 1;
- }
- var totalRows = instance.countRows();
- var totalCols = instance.countCols();
- var coords = {
- row: priv.selStart.row() + rowDelta,
- col: priv.selStart.col() + colDelta
- };
- if (coords.row < 0) {
- coords.row = 0;
- }
- else if (coords.row > 0 && coords.row >= totalRows) {
- coords.row = totalRows - 1;
- }
- if (coords.col < 0) {
- coords.col = 0;
- }
- else if (coords.col > 0 && coords.col >= totalCols) {
- coords.col = totalCols - 1;
- }
- selection.setRangeStart(coords);
- },
- /**
- * Sets selection end cell relative to current selection end cell (if possible)
- */
- transformEnd: function (rowDelta, colDelta) {
- if (priv.selEnd.exists()) {
- var totalRows = instance.countRows();
- var totalCols = instance.countCols();
- var coords = {
- row: priv.selEnd.row() + rowDelta,
- col: priv.selEnd.col() + colDelta
- };
- if (coords.row < 0) {
- coords.row = 0;
- }
- else if (coords.row > 0 && coords.row >= totalRows) {
- coords.row = totalRows - 1;
- }
- if (coords.col < 0) {
- coords.col = 0;
- }
- else if (coords.col > 0 && coords.col >= totalCols) {
- coords.col = totalCols - 1;
- }
- selection.setRangeEnd(coords);
- }
- },
- /**
- * Returns true if currently there is a selection on screen, false otherwise
- * @return {Boolean}
- */
- isSelected: function () {
- return priv.selEnd.exists();
- },
- /**
- * Returns true if coords is within current selection coords
- * @return {Boolean}
- */
- inInSelection: function (coords) {
- if (!selection.isSelected()) {
- return false;
- }
- var sel = grid.getCornerCoords([priv.selStart.coords(), priv.selEnd.coords()]);
- return (sel.TL.row <= coords.row && sel.BR.row >= coords.row && sel.TL.col <= coords.col && sel.BR.col >= coords.col);
- },
- /**
- * Deselects all selected cells
- */
- deselect: function () {
- if (!selection.isSelected()) {
- return;
- }
- instance.selection.inProgress = false; //needed by HT inception
- priv.selEnd = new Handsontable.SelectionPoint(); //create new empty point to remove the existing one
- instance.view.wt.selections.current.clear();
- instance.view.wt.selections.area.clear();
- editorManager.destroyEditor();
- selection.refreshBorders();
- instance.PluginHooks.run('afterDeselect');
- },
- /**
- * Select all cells
- */
- selectAll: function () {
- if (!priv.settings.multiSelect) {
- return;
- }
- selection.setRangeStart({
- row: 0,
- col: 0
- });
- selection.setRangeEnd({
- row: instance.countRows() - 1,
- col: instance.countCols() - 1
- }, false);
- },
- /**
- * Deletes data from selected cells
- */
- empty: function () {
- if (!selection.isSelected()) {
- return;
- }
- var corners = grid.getCornerCoords([priv.selStart.coords(), priv.selEnd.coords()]);
- var r, c, changes = [];
- for (r = corners.TL.row; r <= corners.BR.row; r++) {
- for (c = corners.TL.col; c <= corners.BR.col; c++) {
- if (!instance.getCellMeta(r, c).readOnly) {
- changes.push([r, c, '']);
- }
- }
- }
- instance.setDataAtCell(changes);
- }
- };
- this.autofill = autofill = { //this public assignment is only temporary
- handle: null,
- /**
- * Create fill handle and fill border objects
- */
- init: function () {
- if (!autofill.handle) {
- autofill.handle = {};
- }
- else {
- autofill.handle.disabled = false;
- }
- },
- /**
- * Hide fill handle and fill border permanently
- */
- disable: function () {
- autofill.handle.disabled = true;
- },
- /**
- * Selects cells down to the last row in the left column, then fills down to that cell
- */
- selectAdjacent: function () {
- var select, data, r, maxR, c;
- if (selection.isMultiple()) {
- select = instance.view.wt.selections.area.getCorners();
- }
- else {
- select = instance.view.wt.selections.current.getCorners();
- }
- data = datamap.getAll();
- rows : for (r = select[2] + 1; r < instance.countRows(); r++) {
- for (c = select[1]; c <= select[3]; c++) {
- if (data[r][c]) {
- break rows;
- }
- }
- if (!!data[r][select[1] - 1] || !!data[r][select[3] + 1]) {
- maxR = r;
- }
- }
- if (maxR) {
- instance.view.wt.selections.fill.clear();
- instance.view.wt.selections.fill.add([select[0], select[1]]);
- instance.view.wt.selections.fill.add([maxR, select[3]]);
- autofill.apply();
- }
- },
- /**
- * Apply fill values to the area in fill border, omitting the selection border
- */
- apply: function () {
- var drag, select, start, end, _data;
- autofill.handle.isDragged = 0;
- drag = instance.view.wt.selections.fill.getCorners();
- if (!drag) {
- return;
- }
- instance.view.wt.selections.fill.clear();
- if (selection.isMultiple()) {
- select = instance.view.wt.selections.area.getCorners();
- }
- else {
- select = instance.view.wt.selections.current.getCorners();
- }
- if (drag[0] === select[0] && drag[1] < select[1]) {
- start = {
- row: drag[0],
- col: drag[1]
- };
- end = {
- row: drag[2],
- col: select[1] - 1
- };
- }
- else if (drag[0] === select[0] && drag[3] > select[3]) {
- start = {
- row: drag[0],
- col: select[3] + 1
- };
- end = {
- row: drag[2],
- col: drag[3]
- };
- }
- else if (drag[0] < select[0] && drag[1] === select[1]) {
- start = {
- row: drag[0],
- col: drag[1]
- };
- end = {
- row: select[0] - 1,
- col: drag[3]
- };
- }
- else if (drag[2] > select[2] && drag[1] === select[1]) {
- start = {
- row: select[2] + 1,
- col: drag[1]
- };
- end = {
- row: drag[2],
- col: drag[3]
- };
- }
- if (start) {
- _data = SheetClip.parse(datamap.getText(priv.selStart.coords(), priv.selEnd.coords()));
- instance.PluginHooks.run('beforeAutofill', start, end, _data);
- grid.populateFromArray(start, _data, end, 'autofill');
- selection.setRangeStart({row: drag[0], col: drag[1]});
- selection.setRangeEnd({row: drag[2], col: drag[3]});
- }
- /*else {
- //reset to avoid some range bug
- selection.refreshBorders();
- }*/
- },
- /**
- * Show fill border
- */
- showBorder: function (coords) {
- coords.row = coords[0];
- coords.col = coords[1];
- var corners = grid.getCornerCoords([priv.selStart.coords(), priv.selEnd.coords()]);
- if (priv.settings.fillHandle !== 'horizontal' && (corners.BR.row < coords.row || corners.TL.row > coords.row)) {
- coords = [coords.row, corners.BR.col];
- }
- else if (priv.settings.fillHandle !== 'vertical') {
- coords = [corners.BR.row, coords.col];
- }
- else {
- return; //wrong direction
- }
- instance.view.wt.selections.fill.clear();
- instance.view.wt.selections.fill.add([priv.selStart.coords().row, priv.selStart.coords().col]);
- instance.view.wt.selections.fill.add([priv.selEnd.coords().row, priv.selEnd.coords().col]);
- instance.view.wt.selections.fill.add(coords);
- instance.view.render();
- }
- };
- this.init = function () {
- instance.PluginHooks.run('beforeInit');
- this.view = new Handsontable.TableView(this);
- editorManager = new Handsontable.EditorManager(instance, priv, selection, datamap);
- this.updateSettings(priv.settings, true);
- this.parseSettingsFromDOM();
- this.forceFullRender = true; //used when data was changed
- this.view.render();
- if (typeof priv.firstRun === 'object') {
- instance.PluginHooks.run('afterChange', priv.firstRun[0], priv.firstRun[1]);
- priv.firstRun = false;
- }
- instance.PluginHooks.run('afterInit');
- };
- function ValidatorsQueue() { //moved this one level up so it can be used in any function here. Probably this should be moved to a separate file
- var resolved = false;
- return {
- validatorsInQueue: 0,
- addValidatorToQueue: function () {
- this.validatorsInQueue++;
- resolved = false;
- },
- removeValidatorFormQueue: function () {
- this.validatorsInQueue = this.validatorsInQueue - 1 < 0 ? 0 : this.validatorsInQueue - 1;
- this.checkIfQueueIsEmpty();
- },
- onQueueEmpty: function () {
- },
- checkIfQueueIsEmpty: function () {
- if (this.validatorsInQueue == 0 && resolved == false) {
- resolved = true;
- this.onQueueEmpty();
- }
- }
- };
- }
- function validateChanges(changes, source, callback) {
- var waitingForValidator = new ValidatorsQueue();
- waitingForValidator.onQueueEmpty = resolve;
- for (var i = changes.length - 1; i >= 0; i--) {
- if (changes[i] === null) {
- changes.splice(i, 1);
- }
- else {
- var row = changes[i][0];
- var col = datamap.propToCol(changes[i][1]);
- var logicalCol = instance.runHooksAndReturn('modifyCol', col); //column order may have changes, so we need to translate physical col index (stored in datasource) to logical (displayed to user)
- var cellProperties = instance.getCellMeta(row, logicalCol);
- if (cellProperties.type === 'numeric' && typeof changes[i][3] === 'string') {
- if (changes[i][3].length > 0 && /^-?[\d\s]*\.?\d*$/.test(changes[i][3])) {
- changes[i][3] = numeral().unformat(changes[i][3] || '0'); //numeral cannot unformat empty string
- }
- }
- if (instance.getCellValidator(cellProperties)) {
- waitingForValidator.addValidatorToQueue();
- instance.validateCell(changes[i][3], cellProperties, (function (i, cellProperties) {
- return function (result) {
- if (typeof result !== 'boolean') {
- throw new Error("Validation error: result is not boolean");
- }
- if (result === false && cellProperties.allowInvalid === false) {
- changes.splice(i, 1); // cancel the change
- cellProperties.valid = true; // we cancelled the change, so cell value is still valid
- --i;
- }
- waitingForValidator.removeValidatorFormQueue();
- }
- })(i, cellProperties)
- , source);
- }
- }
- }
- waitingForValidator.checkIfQueueIsEmpty();
- function resolve() {
- var beforeChangeResult;
- if (changes.length) {
- beforeChangeResult = instance.PluginHooks.execute("beforeChange", changes, source);
- if (typeof beforeChangeResult === 'function') {
- $.when(result).then(function () {
- callback(); //called when async validators and async beforeChange are resolved
- });
- }
- else if (beforeChangeResult === false) {
- changes.splice(0, changes.length); //invalidate all changes (remove everything from array)
- }
- }
- if (typeof beforeChangeResult !== 'function') {
- callback(); //called when async validators are resolved and beforeChange was not async
- }
- }
- }
- /**
- * Internal function to apply changes. Called after validateChanges
- * @param {Array} changes Array in form of [row, prop, oldValue, newValue]
- * @param {String} source String that identifies how this change will be described in changes array (useful in onChange callback)
- */
- function applyChanges(changes, source) {
- var i = changes.length - 1;
- if (i < 0) {
- return;
- }
- for (; 0 <= i; i--) {
- if (changes[i] === null) {
- changes.splice(i, 1);
- continue;
- }
- if (priv.settings.minSpareRows) {
- while (changes[i][0] > instance.countRows() - 1) {
- datamap.createRow();
- }
- }
- if (priv.dataType === 'array' && priv.settings.minSpareCols) {
- while (datamap.propToCol(changes[i][1]) > instance.countCols() - 1) {
- datamap.createCol();
- }
- }
- datamap.set(changes[i][0], changes[i][1], changes[i][3]);
- }
- instance.forceFullRender = true; //used when data was changed
- grid.adjustRowsAndCols();
- selection.refreshBorders(null, true);
- instance.PluginHooks.run('afterChange', changes, source || 'edit');
- }
- this.validateCell = function (value, cellProperties, callback, source) {
- var validator = instance.getCellValidator(cellProperties);
- if (Object.prototype.toString.call(validator) === '[object RegExp]') {
- validator = (function (validator) {
- return function (value, callback) {
- callback(validator.test(value));
- }
- })(validator);
- }
- if (typeof validator == 'function') {
- value = instance.PluginHooks.execute("beforeValidate", value, cellProperties.row, cellProperties.prop, source);
- // To provide consistent behaviour, validation should be always asynchronous
- setTimeout(function () {
- validator.call(cellProperties, value, function (valid) {
- cellProperties.valid = valid;
- valid = instance.PluginHooks.execute("afterValidate", valid, value, cellProperties.row, cellProperties.prop, source);
- callback(valid);
- });
- });
- } else { //resolve callback even if validator function was not found
- cellProperties.valid = true;
- callback(true);
- }
- };
- function setDataInputToArray(row, prop_or_col, value) {
- if (typeof row === "object") { //is it an array of changes
- return row;
- }
- else if ($.isPlainObject(value)) { //backwards compatibility
- return value;
- }
- else {
- return [
- [row, prop_or_col, value]
- ];
- }
- }
- /**
- * Set data at given cell
- * @public
- * @param {Number|Array} row or array of changes in format [[row, col, value], ...]
- * @param {Number|String} col or source String
- * @param {String} value
- * @param {String} source String that identifies how this change will be described in changes array (useful in onChange callback)
- */
- this.setDataAtCell = function (row, col, value, source) {
- var input = setDataInputToArray(row, col, value)
- , i
- , ilen
- , changes = []
- , prop;
- for (i = 0, ilen = input.length; i < ilen; i++) {
- if (typeof input[i] !== 'object') {
- throw new Error('Method `setDataAtCell` accepts row number or changes array of arrays as its first parameter');
- }
- if (typeof input[i][1] !== 'number') {
- throw new Error('Method `setDataAtCell` accepts row and column number as its parameters. If you want to use object property name, use method `setDataAtRowProp`');
- }
- prop = datamap.colToProp(input[i][1]);
- changes.push([
- input[i][0],
- prop,
- datamap.get(input[i][0], prop),
- input[i][2]
- ]);
- }
- if (!source && typeof row === "object") {
- source = col;
- }
- validateChanges(changes, source, function () {
- applyChanges(changes, source);
- });
- };
- /**
- * Set data at given row property
- * @public
- * @param {Number|Array} row or array of changes in format [[row, prop, value], ...]
- * @param {String} prop or source String
- * @param {String} value
- * @param {String} source String that identifies how this change will be described in changes array (useful in onChange callback)
- */
- this.setDataAtRowProp = function (row, prop, value, source) {
- var input = setDataInputToArray(row, prop, value)
- , i
- , ilen
- , changes = [];
- for (i = 0, ilen = input.length; i < ilen; i++) {
- changes.push([
- input[i][0],
- input[i][1],
- datamap.get(input[i][0], input[i][1]),
- input[i][2]
- ]);
- }
- if (!source && typeof row === "object") {
- source = prop;
- }
- validateChanges(changes, source, function () {
- applyChanges(changes, source);
- });
- };
- /**
- * Listen to document body keyboard input
- */
- this.listen = function () {
- Handsontable.activeGuid = instance.guid;
- if (document.activeElement && document.activeElement !== document.body) {
- document.activeElement.blur();
- }
- else if (!document.activeElement) { //IE
- document.body.focus();
- }
- };
- /**
- * Stop listening to document body keyboard input
- */
- this.unlisten = function () {
- Handsontable.activeGuid = null;
- };
- /**
- * Returns true if current Handsontable instance is listening on document body keyboard input
- */
- this.isListening = function () {
- return Handsontable.activeGuid === instance.guid;
- };
- /**
- * Destroys current editor, renders and selects current cell. If revertOriginal != true, edited data is saved
- * @param {Boolean} revertOriginal
- */
- this.destroyEditor = function (revertOriginal) {
- selection.refreshBorders(revertOriginal);
- };
- /**
- * Populate cells at position with 2d array
- * @param {Number} row Start row
- * @param {Number} col Start column
- * @param {Array} input 2d array
- * @param {Number=} endRow End row (use when you want to cut input when certain row is reached)
- * @param {Number=} endCol End column (use when you want to cut input when certain column is reached)
- * @param {String=} [source="populateFromArray"]
- * @param {String=} [method="overwrite"]
- * @return {Object|undefined} ending td in pasted area (only if any cell was changed)
- */
- this.populateFromArray = function (row, col, input, endRow, endCol, source, method) {
- if (!(typeof input === 'object' && typeof input[0] === 'object')) {
- throw new Error("populateFromArray parameter `input` must be an array of arrays"); //API changed in 0.9-beta2, let's check if you use it correctly
- }
- return grid.populateFromArray({row: row, col: col}, input, typeof endRow === 'number' ? {row: endRow, col: endCol} : null, source, method);
- };
- /**
- * Adds/removes data from the column
- * @param {Number} col Index of column in which do you want to do splice.
- * @param {Number} index Index at which to start changing the array. If negative, will begin that many elements from the end
- * @param {Number} amount An integer indicating the number of old array elements to remove. If amount is 0, no elements are removed
- * param {...*} elements Optional. The elements to add to the array. If you don't specify any elements, spliceCol simply removes elements from the array
- */
- this.spliceCol = function (col, index, amount/*, elements... */) {
- return datamap.spliceCol.apply(null, arguments);
- };
- /**
- * Adds/removes data from the row
- * @param {Number} row Index of column in which do you want to do splice.
- * @param {Number} index Index at which to start changing the array. If negative, will begin that many elements from the end
- * @param {Number} amount An integer indicating the number of old array elements to remove. If amount is 0, no elements are removed
- * param {...*} elements Optional. The elements to add to the array. If you don't specify any elements, spliceCol simply removes elements from the array
- */
- this.spliceRow = function (row, index, amount/*, elements... */) {
- return datamap.spliceRow.apply(null, arguments);
- };
- /**
- * Returns the top left (TL) and bottom right (BR) selection coordinates
- * @param {Object[]} coordsArr
- * @returns {Object}
- */
- this.getCornerCoords = function (coordsArr) {
- return grid.getCornerCoords(coordsArr);
- };
- /**
- * Returns current selection. Returns undefined if there is no selection.
- * @public
- * @return {Array} [`startRow`, `startCol`, `endRow`, `endCol`]
- */
- this.getSelected = function () { //https://github.com/warpech/jquery-handsontable/issues/44 //cjl
- if (selection.isSelected()) {
- return [priv.selStart.row(), priv.selStart.col(), priv.selEnd.row(), priv.selEnd.col()];
- }
- };
- /**
- * Parse settings from DOM and CSS
- * @public
- */
- this.parseSettingsFromDOM = function () {
- var overflow = this.rootElement.css('overflow');
- if (overflow === 'scroll' || overflow === 'auto') {
- this.rootElement[0].style.overflow = 'visible';
- priv.settingsFromDOM.overflow = overflow;
- }
- else if (priv.settings.width === void 0 || priv.settings.height === void 0) {
- priv.settingsFromDOM.overflow = 'auto';
- }
- if (priv.settings.width === void 0) {
- priv.settingsFromDOM.width = this.rootElement.width();
- }
- else {
- priv.settingsFromDOM.width = void 0;
- }
- priv.settingsFromDOM.height = void 0;
- if (priv.settings.height === void 0) {
- if (priv.settingsFromDOM.overflow === 'scroll' || priv.settingsFromDOM.overflow === 'auto') {
- //this needs to read only CSS/inline style and not actual height
- //so we need to call getComputedStyle on cloned container
- var clone = this.rootElement[0].cloneNode(false);
- var parent = this.rootElement[0].parentNode;
- if (parent) {
- clone.removeAttribute('id');
- parent.appendChild(clone);
- var computedHeight = parseInt(window.getComputedStyle(clone, null).getPropertyValue('height'), 10);
- if(isNaN(computedHeight) && clone.currentStyle){
- computedHeight = parseInt(clone.currentStyle.height, 10)
- }
- if (computedHeight > 0) {
- priv.settingsFromDOM.height = computedHeight;
- }
- parent.removeChild(clone);
- }
- }
- }
- };
- /**
- * Render visible data
- * @public
- */
- this.render = function () {
- if (instance.view) {
- instance.forceFullRender = true; //used when data was changed
- instance.parseSettingsFromDOM();
- selection.refreshBorders(null, true);
- }
- };
- /**
- * Load data from array
- * @public
- * @param {Array} data
- */
- this.loadData = function (data) {
- if (typeof data === 'object' && data !== null) {
- if (!(data.push && data.splice)) { //check if data is array. Must use duck-type check so Backbone Collections also pass it
- //when data is not an array, attempt to make a single-row array of it
- data = [data];
- }
- }
- else if(data === null) {
- data = [];
- var row;
- for (var r = 0, rlen = priv.settings.startRows; r < rlen; r++) {
- row = [];
- for (var c = 0, clen = priv.settings.startCols; c < clen; c++) {
- row.push(null);
- }
- data.push(row);
- }
- }
- else {
- throw new Error("loadData only accepts array of objects or array of arrays (" + typeof data + " given)");
- }
- priv.isPopulated = false;
- GridSettings.prototype.data = data;
- if (priv.settings.dataSchema instanceof Array || data[0] instanceof Array) {
- priv.dataType = 'array';
- }
- else if (typeof priv.settings.dataSchema === 'function') {
- priv.dataType = 'function';
- }
- else {
- priv.dataType = 'object';
- }
- if (data[0]) {
- priv.duckDataSchema = datamap.recursiveDuckSchema(data[0]);
- }
- else {
- priv.duckDataSchema = {};
- }
- datamap.createMap();
- grid.adjustRowsAndCols();
- instance.PluginHooks.run('afterLoadData');
- if (priv.firstRun) {
- priv.firstRun = [null, 'loadData'];
- }
- else {
- instance.PluginHooks.run('afterChange', null, 'loadData');
- instance.render();
- }
- priv.isPopulated = true;
- };
- /**
- * Return the current data object (the same that was passed by `data` configuration option or `loadData` method). Optionally you can provide cell range `r`, `c`, `r2`, `c2` to get only a fragment of grid data
- * @public
- * @param {Number} r (Optional) From row
- * @param {Number} c (Optional) From col
- * @param {Number} r2 (Optional) To row
- * @param {Number} c2 (Optional) To col
- * @return {Array|Object}
- */
- this.getData = function (r, c, r2, c2) {
- if (typeof r === 'undefined') {
- return datamap.getAll();
- }
- else {
- return datamap.getRange({row: r, col: c}, {row: r2, col: c2});
- }
- };
- /**
- * Update settings
- * @public
- */
- this.updateSettings = function (settings, init) {
- var i, clen;
- if (typeof settings.rows !== "undefined") {
- throw new Error("'rows' setting is no longer supported. do you mean startRows, minRows or maxRows?");
- }
- if (typeof settings.cols !== "undefined") {
- throw new Error("'cols' setting is no longer supported. do you mean startCols, minCols or maxCols?");
- }
- for (i in settings) {
- if (i === 'data') {
- continue; //loadData will be triggered later
- }
- else {
- if (instance.PluginHooks.hooks[i] !== void 0 || instance.PluginHooks.legacy[i] !== void 0) {
- instance.PluginHooks.add(i, settings[i]);
- }
- else {
- // Update settings
- if (!init && settings.hasOwnProperty(i)) {
- GridSettings.prototype[i] = settings[i];
- }
- //launch extensions
- if (Handsontable.extension[i]) {
- priv.extensions[i] = new Handsontable.extension[i](instance, settings[i]);
- }
- }
- }
- }
- // Load data or create data map
- if (settings.data === void 0 && priv.settings.data === void 0) {
- instance.loadData(null); //data source created just now
- }
- else if (settings.data !== void 0) {
- instance.loadData(settings.data); //data source given as option
- }
- else if (settings.columns !== void 0) {
- datamap.createMap();
- }
- // Init columns constructors configuration
- clen = instance.countCols();
- //Clear cellSettings cache
- priv.cellSettings.length = 0;
- if (clen > 0) {
- var proto, column;
- for (i = 0; i < clen; i++) {
- priv.columnSettings[i] = Handsontable.helper.columnFactory(GridSettings, priv.columnsSettingConflicts);
- // shortcut for prototype
- proto = priv.columnSettings[i].prototype;
- // Use settings provided by user
- if (GridSettings.prototype.columns) {
- column = GridSettings.prototype.columns[i];
- Handsontable.helper.extend(proto, column);
- Handsontable.helper.extend(proto, expandType(column));
- }
- }
- }
- if (typeof settings.fillHandle !== "undefined") {
- if (autofill.handle && settings.fillHandle === false) {
- autofill.disable();
- }
- else if (!autofill.handle && settings.fillHandle !== false) {
- autofill.init();
- }
- }
- if (typeof settings.className !== "undefined") {
- if (GridSettings.prototype.className) {
- instance.rootElement.removeClass(GridSettings.prototype.className);
- }
- if (settings.className) {
- instance.rootElement.addClass(settings.className);
- }
- }
- if (!init) {
- instance.PluginHooks.run('afterUpdateSettings');
- }
- grid.adjustRowsAndCols();
- if (instance.view && !priv.firstRun) {
- instance.forceFullRender = true; //used when data was changed
- selection.refreshBorders(null, true);
- }
- };
- this.getValue = function () {
- var sel = instance.getSelected();
- if (GridSettings.prototype.getValue) {
- if (typeof GridSettings.prototype.getValue === 'function') {
- return GridSettings.prototype.getValue.call(instance);
- }
- else if (sel) {
- return instance.getData()[sel[0]][GridSettings.prototype.getValue];
- }
- }
- else if (sel) {
- return instance.getDataAtCell(sel[0], sel[1]);
- }
- };
- function expandType(obj) {
- if (!obj.hasOwnProperty('type')) return; //ignore obj.prototype.type
- var type, expandedType = {};
- if (typeof obj.type === 'object') {
- type = obj.type;
- }
- else if (typeof obj.type === 'string') {
- type = Handsontable.cellTypes[obj.type];
- if (type === void 0) {
- throw new Error('You declared cell type "' + obj.type + '" as a string that is not mapped to a known object. Cell type must be an object or a string mapped to an object in Handsontable.cellTypes');
- }
- }
- for (var i in type) {
- if (type.hasOwnProperty(i) && !obj.hasOwnProperty(i)) {
- expandedType[i] = type[i];
- }
- }
- return expandedType;
- }
- /**
- * Returns current settings object
- * @return {Object}
- */
- this.getSettings = function () {
- return priv.settings;
- };
- /**
- * Returns current settingsFromDOM object
- * @return {Object}
- */
- this.getSettingsFromDOM = function () {
- return priv.settingsFromDOM;
- };
- /**
- * Clears grid
- * @public
- */
- this.clear = function () {
- selection.selectAll();
- selection.empty();
- };
- /**
- * Inserts or removes rows and columns
- * @param {String} action See grid.alter for possible values
- * @param {Number} index
- * @param {Number} amount
- * @param {String} [source] Optional. Source of hook runner.
- * @param {Boolean} [keepEmptyRows] Optional. Flag for preventing deletion of empty rows.
- * @public
- */
- this.alter = function (action, index, amount, source, keepEmptyRows) {
- grid.alter(action, index, amount, source, keepEmptyRows);
- };
- /**
- * Returns <td> element corresponding to params row, col
- * @param {Number} row
- * @param {Number} col
- * @public
- * @return {Element}
- */
- this.getCell = function (row, col) {
- return instance.view.getCellAtCoords({row: row, col: col});
- };
- /**
- * Returns property name associated with column number
- * @param {Number} col
- * @public
- * @return {String}
- */
- this.colToProp = function (col) {
- return datamap.colToProp(col);
- };
- /**
- * Returns column number associated with property name
- * @param {String} prop
- * @public
- * @return {Number}
- */
- this.propToCol = function (prop) {
- return datamap.propToCol(prop);
- };
- /**
- * Return value at `row`, `col`
- * @param {Number} row
- * @param {Number} col
- * @public
- * @return value (mixed data type)
- */
- this.getDataAtCell = function (row, col) {
- return datamap.get(row, datamap.colToProp(col));
- };
- /**
- * Return value at `row`, `prop`
- * @param {Number} row
- * @param {String} prop
- * @public
- * @return value (mixed data type)
- */
- this.getDataAtRowProp = function (row, prop) {
- return datamap.get(row, prop);
- };
- /**
- * Return value at `col`
- * @param {Number} col
- * @public
- * @return value (mixed data type)
- */
- this.getDataAtCol = function (col) {
- return [].concat.apply([], datamap.getRange({row: 0, col: col}, {row: priv.settings.data.length - 1, col: col}));
- };
- /**
- * Return value at `prop`
- * @param {String} prop
- * @public
- * @return value (mixed data type)
- */
- this.getDataAtProp = function (prop) {
- return [].concat.apply([], datamap.getRange({row: 0, col: datamap.propToCol(prop)}, {row: priv.settings.data.length - 1, col: datamap.propToCol(prop)}));
- };
- /**
- * Return value at `row`
- * @param {Number} row
- * @public
- * @return value (mixed data type)
- */
- this.getDataAtRow = function (row) {
- return priv.settings.data[row];
- };
- /**
- * Returns cell meta data object corresponding to params row, col
- * @param {Number} row
- * @param {Number} col
- * @public
- * @return {Object}
- */
- this.getCellMeta = function (row, col) {
- var prop = datamap.colToProp(col)
- , cellProperties;
- row = translateRowIndex(row);
- col = translateColIndex(col);
- if ("undefined" === typeof priv.columnSettings[col]) {
- priv.columnSettings[col] = Handsontable.helper.columnFactory(GridSettings, priv.columnsSettingConflicts);
- }
- if (!priv.cellSettings[row]) {
- priv.cellSettings[row] = [];
- }
- if (!priv.cellSettings[row][col]) {
- priv.cellSettings[row][col] = new priv.columnSettings[col]();
- }
- cellProperties = priv.cellSettings[row][col]; //retrieve cellProperties from cache
- cellProperties.row = row;
- cellProperties.col = col;
- cellProperties.prop = prop;
- cellProperties.instance = instance;
- instance.PluginHooks.run('beforeGetCellMeta', row, col, cellProperties);
- Handsontable.helper.extend(cellProperties, expandType(cellProperties)); //for `type` added in beforeGetCellMeta
- if (cellProperties.cells) {
- var settings = cellProperties.cells.call(cellProperties, row, col, prop);
- if (settings) {
- Handsontable.helper.extend(cellProperties, settings);
- Handsontable.helper.extend(cellProperties, expandType(settings)); //for `type` added in cells
- }
- }
- instance.PluginHooks.run('afterGetCellMeta', row, col, cellProperties);
- return cellProperties;
- /**
- * If displayed rows order is different than the order of rows stored in memory (i.e. sorting is applied)
- * we need to translate logical (stored) row index to physical (displayed) index.
- * @param row - original row index
- * @returns {int} translated row index
- */
- function translateRowIndex(row){
- var getVars = {row: row};
- instance.PluginHooks.execute('beforeGet', getVars);
- return getVars.row;
- }
- /**
- * If displayed columns order is different than the order of columns stored in memory (i.e. column were moved using manualColumnMove plugin)
- * we need to translate logical (stored) column index to physical (displayed) index.
- * @param col - original column index
- * @returns {int} - translated column index
- */
- function translateColIndex(col){
- return Handsontable.PluginHooks.execute(instance, 'modifyCol', col); // warning: this must be done after datamap.colToProp
- }
- };
- this.getCellRenderer = function (row, col) {
- var renderer = Handsontable.helper.cellMethodLookupFactory('renderer').call(this, row, col);
- if(typeof renderer == 'string'){
- renderer = Handsontable.cellLookup.renderer[renderer];
- }
- return renderer
- };
- this.getCellEditor = Handsontable.helper.cellMethodLookupFactory('editor');
- this.getCellValidator = Handsontable.helper.cellMethodLookupFactory('validator');
- /**
- * Validates all cells using their validator functions and calls callback when finished. Does not render the view
- * @param callback
- */
- this.validateCells = function (callback) {
- var waitingForValidator = new ValidatorsQueue();
- waitingForValidator.onQueueEmpty = callback;
- var i = instance.countRows() - 1;
- while (i >= 0) {
- var j = instance.countCols() - 1;
- while (j >= 0) {
- waitingForValidator.addValidatorToQueue();
- instance.validateCell(instance.getDataAtCell(i, j), instance.getCellMeta(i, j), function () {
- waitingForValidator.removeValidatorFormQueue();
- }, 'validateCells');
- j--;
- }
- i--;
- }
- waitingForValidator.checkIfQueueIsEmpty();
- };
- /**
- * Return array of row headers (if they are enabled). If param `row` given, return header at given row as string
- * @param {Number} row (Optional)
- * @return {Array|String}
- */
- this.getRowHeader = function (row) {
- if (row === void 0) {
- var out = [];
- for (var i = 0, ilen = instance.countRows(); i < ilen; i++) {
- out.push(instance.getRowHeader(i));
- }
- return out;
- }
- else if (Object.prototype.toString.call(priv.settings.rowHeaders) === '[object Array]' && priv.settings.rowHeaders[row] !== void 0) {
- return priv.settings.rowHeaders[row];
- }
- else if (typeof priv.settings.rowHeaders === 'function') {
- return priv.settings.rowHeaders(row);
- }
- else if (priv.settings.rowHeaders && typeof priv.settings.rowHeaders !== 'string' && typeof priv.settings.rowHeaders !== 'number') {
- return row + 1;
- }
- else {
- return priv.settings.rowHeaders;
- }
- };
- /**
- * Returns information of this table is configured to display row headers
- * @returns {boolean}
- */
- this.hasRowHeaders = function () {
- return !!priv.settings.rowHeaders;
- };
- /**
- * Returns information of this table is configured to display column headers
- * @returns {boolean}
- */
- this.hasColHeaders = function () {
- if (priv.settings.colHeaders !== void 0) {
- return !!priv.settings.colHeaders;
- }
- for (var i = 0, ilen = instance.countCols(); i < ilen; i++) {
- if (instance.getColHeader(i)) {
- return true;
- }
- }
- return false;
- };
- /**
- * Return array of column headers (if they are enabled). If param `col` given, return header at given column as string
- * @param {Number} col (Optional)
- * @return {Array|String}
- */
- this.getColHeader = function (col) {
- if (col === void 0) {
- var out = [];
- for (var i = 0, ilen = instance.countCols(); i < ilen; i++) {
- out.push(instance.getColHeader(i));
- }
- return out;
- }
- else {
- col = Handsontable.PluginHooks.execute(instance, 'modifyCol', col);
- if (priv.settings.columns && priv.settings.columns[col] && priv.settings.columns[col].title) {
- return priv.settings.columns[col].title;
- }
- else if (Object.prototype.toString.call(priv.settings.colHeaders) === '[object Array]' && priv.settings.colHeaders[col] !== void 0) {
- return priv.settings.colHeaders[col];
- }
- else if (typeof priv.settings.colHeaders === 'function') {
- return priv.settings.colHeaders(col);
- }
- else if (priv.settings.colHeaders && typeof priv.settings.colHeaders !== 'string' && typeof priv.settings.colHeaders !== 'number') {
- return Handsontable.helper.spreadsheetColumnLabel(col);
- }
- else {
- return priv.settings.colHeaders;
- }
- }
- };
- /**
- * Return column width from settings (no guessing). Private use intended
- * @param {Number} col
- * @return {Number}
- */
- this._getColWidthFromSettings = function (col) {
- var cellProperties = instance.getCellMeta(0, col);
- var width = cellProperties.width;
- if (width === void 0 || width === priv.settings.width) {
- width = cellProperties.colWidths;
- }
- if (width !== void 0) {
- switch (typeof width) {
- case 'object': //array
- width = width[col];
- break;
- case 'function':
- width = width(col);
- break;
- }
- if (typeof width === 'string') {
- width = parseInt(width, 10);
- }
- }
- return width;
- };
- /**
- * Return column width
- * @param {Number} col
- * @return {Number}
- */
- this.getColWidth = function (col) {
- col = Handsontable.PluginHooks.execute(instance, 'modifyCol', col);
- var response = {
- width: instance._getColWidthFromSettings(col)
- };
- if (!response.width) {
- response.width = 50;
- }
- instance.PluginHooks.run('afterGetColWidth', col, response);
- return response.width;
- };
- /**
- * Return total number of rows in grid
- * @return {Number}
- */
- this.countRows = function () {
- return priv.settings.data.length;
- };
- /**
- * Return total number of columns in grid
- * @return {Number}
- */
- this.countCols = function () {
- if (priv.dataType === 'object' || priv.dataType === 'function') {
- if (priv.settings.columns && priv.settings.columns.length) {
- return priv.settings.columns.length;
- }
- else {
- return priv.colToProp.length;
- }
- }
- else if (priv.dataType === 'array') {
- if (priv.settings.columns && priv.settings.columns.length) {
- return priv.settings.columns.length;
- }
- else if (priv.settings.data && priv.settings.data[0] && priv.settings.data[0].length) {
- return priv.settings.data[0].length;
- }
- else {
- return 0;
- }
- }
- };
- /**
- * Return index of first visible row
- * @return {Number}
- */
- this.rowOffset = function () {
- return instance.view.wt.getSetting('offsetRow');
- };
- /**
- * Return index of first visible column
- * @return {Number}
- */
- this.colOffset = function () {
- return instance.view.wt.getSetting('offsetColumn');
- };
- /**
- * Return number of visible rows. Returns -1 if table is not visible
- * @return {Number}
- */
- this.countVisibleRows = function () {
- return instance.view.wt.drawn ? instance.view.wt.wtTable.rowStrategy.countVisible() : -1;
- };
- /**
- * Return number of visible columns. Returns -1 if table is not visible
- * @return {Number}
- */
- this.countVisibleCols = function () {
- return instance.view.wt.drawn ? instance.view.wt.wtTable.columnStrategy.countVisible() : -1;
- };
- /**
- * Return number of empty rows
- * @return {Boolean} ending If true, will only count empty rows at the end of the data source
- */
- this.countEmptyRows = function (ending) {
- var i = instance.countRows() - 1
- , empty = 0;
- while (i >= 0) {
- datamap.get(i, 0);
- if (instance.isEmptyRow(datamap.getVars.row)) {
- empty++;
- }
- else if (ending) {
- break;
- }
- i--;
- }
- return empty;
- };
- /**
- * Return number of empty columns
- * @return {Boolean} ending If true, will only count empty columns at the end of the data source row
- */
- this.countEmptyCols = function (ending) {
- if (instance.countRows() < 1) {
- return 0;
- }
- var i = instance.countCols() - 1
- , empty = 0;
- while (i >= 0) {
- if (instance.isEmptyCol(i)) {
- empty++;
- }
- else if (ending) {
- break;
- }
- i--;
- }
- return empty;
- };
- /**
- * Return true if the row at the given index is empty, false otherwise
- * @param {Number} r Row index
- * @return {Boolean}
- */
- this.isEmptyRow = function (r) {
- if (priv.settings.isEmptyRow) {
- return priv.settings.isEmptyRow.call(instance, r);
- }
- var val;
- for (var c = 0, clen = instance.countCols(); c < clen; c++) {
- val = instance.getDataAtCell(r, c);
- if (val !== '' && val !== null && typeof val !== 'undefined') {
- return false;
- }
- }
- return true;
- };
- /**
- * Return true if the column at the given index is empty, false otherwise
- * @param {Number} c Column index
- * @return {Boolean}
- */
- this.isEmptyCol = function (c) {
- if (priv.settings.isEmptyCol) {
- return priv.settings.isEmptyCol.call(instance, c);
- }
- var val;
- for (var r = 0, rlen = instance.countRows(); r < rlen; r++) {
- val = instance.getDataAtCell(r, c);
- if (val !== '' && val !== null && typeof val !== 'undefined') {
- return false;
- }
- }
- return true;
- };
- /**
- * Selects cell on grid. Optionally selects range to another cell
- * @param {Number} row
- * @param {Number} col
- * @param {Number} [endRow]
- * @param {Number} [endCol]
- * @param {Boolean} [scrollToCell=true] If true, viewport will be scrolled to the selection
- * @public
- * @return {Boolean}
- */
- this.selectCell = function (row, col, endRow, endCol, scrollToCell) {
- if (typeof row !== 'number' || row < 0 || row >= instance.countRows()) {
- return false;
- }
- if (typeof col !== 'number' || col < 0 || col >= instance.countCols()) {
- return false;
- }
- if (typeof endRow !== "undefined") {
- if (typeof endRow !== 'number' || endRow < 0 || endRow >= instance.countRows()) {
- return false;
- }
- if (typeof endCol !== 'number' || endCol < 0 || endCol >= instance.countCols()) {
- return false;
- }
- }
- priv.selStart.coords({row: row, col: col});
- if (document.activeElement && document.activeElement !== document.documentElement && document.activeElement !== document.body) {
- document.activeElement.blur(); //needed or otherwise prepare won't focus the cell. selectionSpec tests this (should move focus to selected cell)
- }
- instance.listen();
- if (typeof endRow === "undefined") {
- selection.setRangeEnd({row: row, col: col}, scrollToCell);
- }
- else {
- selection.setRangeEnd({row: endRow, col: endCol}, scrollToCell);
- }
- instance.selection.finish();
- return true;
- };
- this.selectCellByProp = function (row, prop, endRow, endProp, scrollToCell) {
- arguments[1] = datamap.propToCol(arguments[1]);
- if (typeof arguments[3] !== "undefined") {
- arguments[3] = datamap.propToCol(arguments[3]);
- }
- return instance.selectCell.apply(instance, arguments);
- };
- /**
- * Deselects current sell selection on grid
- * @public
- */
- this.deselectCell = function () {
- selection.deselect();
- };
- /**
- * Remove grid from DOM
- * @public
- */
- this.destroy = function () {
- instance.clearTimeouts();
- if (instance.view) { //in case HT is destroyed before initialization has finished
- instance.view.wt.destroy();
- }
- instance.rootElement.empty();
- instance.rootElement.removeData('handsontable');
- instance.rootElement.off('.handsontable');
- $(window).off('.' + instance.guid);
- $document.off('.' + instance.guid);
- $body.off('.' + instance.guid);
- instance.copyPaste.removeCallback(priv.onCut);
- instance.copyPaste.removeCallback(priv.onPaste);
- instance.PluginHooks.run('afterDestroy');
- };
- /**
- * Returns active editor object
- * @returns {Object}
- */
- this.getActiveEditor = function(){
- return editorManager.getActiveEditor();
- };
- /**
- * Return Handsontable instance
- * @public
- * @return {Object}
- */
- this.getInstance = function () {
- return instance.rootElement.data("handsontable");
- };
- (function () {
- // Create new instance of plugin hooks
- instance.PluginHooks = new Handsontable.PluginHookClass();
- // Upgrade methods to call of global PluginHooks instance
- var _run = instance.PluginHooks.run
- , _exe = instance.PluginHooks.execute;
- instance.PluginHooks.run = function (key, p1, p2, p3, p4, p5) {
- _run.call(this, instance, key, p1, p2, p3, p4, p5);
- Handsontable.PluginHooks.run(instance, key, p1, p2, p3, p4, p5);
- };
- instance.PluginHooks.execute = function (key, p1, p2, p3, p4, p5) {
- var globalHandlerResult = Handsontable.PluginHooks.execute(instance, key, p1, p2, p3, p4, p5);
- var localHandlerResult = _exe.call(this, instance, key, globalHandlerResult, p2, p3, p4, p5);
- return typeof localHandlerResult == 'undefined' ? globalHandlerResult : localHandlerResult;
- };
- // Map old API with new methods
- instance.addHook = function () {
- instance.PluginHooks.add.apply(instance.PluginHooks, arguments);
- };
- instance.addHookOnce = function () {
- instance.PluginHooks.once.apply(instance.PluginHooks, arguments);
- };
- instance.removeHook = function () {
- instance.PluginHooks.remove.apply(instance.PluginHooks, arguments);
- };
- instance.runHooks = function () {
- instance.PluginHooks.run.apply(instance.PluginHooks, arguments);
- };
- instance.runHooksAndReturn = function () {
- return instance.PluginHooks.execute.apply(instance.PluginHooks, arguments);
- };
- })();
- this.timeouts = {};
- /**
- * Sets timeout. Purpose of this method is to clear all known timeouts when `destroy` method is called
- * @public
- */
- this.registerTimeout = function (key, handle, ms) {
- clearTimeout(this.timeouts[key]);
- this.timeouts[key] = setTimeout(handle, ms || 0);
- };
- /**
- * Clears all known timeouts
- * @public
- */
- this.clearTimeouts = function () {
- for (var key in this.timeouts) {
- if (this.timeouts.hasOwnProperty(key)) {
- clearTimeout(this.timeouts[key]);
- }
- }
- };
- /**
- * Handsontable version
- */
- this.version = '0.10.0-beta4'; //inserted by grunt from package.json
- };
- var DefaultSettings = function () {};
- DefaultSettings.prototype = {
- data: void 0,
- width: void 0,
- height: void 0,
- startRows: 5,
- startCols: 5,
- minRows: 0,
- minCols: 0,
- maxRows: Infinity,
- maxCols: Infinity,
- minSpareRows: 0,
- minSpareCols: 0,
- multiSelect: true,
- fillHandle: true,
- fixedRowsTop: 0,
- fixedColumnsLeft: 0,
- outsideClickDeselects: true,
- enterBeginsEditing: true,
- enterMoves: {row: 1, col: 0},
- tabMoves: {row: 0, col: 1},
- autoWrapRow: false,
- autoWrapCol: false,
- copyRowsLimit: 1000,
- copyColsLimit: 1000,
- pasteMode: 'overwrite',
- currentRowClassName: void 0,
- currentColClassName: void 0,
- stretchH: 'hybrid',
- isEmptyRow: void 0,
- isEmptyCol: void 0,
- observeDOMVisibility: true,
- allowInvalid: true,
- invalidCellClassName: 'htInvalid',
- fragmentSelection: false,
- readOnly: false,
- nativeScrollbars: false,
- type: 'text'
- };
- Handsontable.DefaultSettings = DefaultSettings;
- $.fn.handsontable = function (action) {
- var i
- , ilen
- , args
- , output
- , userSettings
- , $this = this.first() // Use only first element from list
- , instance = $this.data('handsontable');
- // Init case
- if (typeof action !== 'string') {
- userSettings = action || {};
- if (instance) {
- instance.updateSettings(userSettings);
- }
- else {
- instance = new Handsontable.Core($this, userSettings);
- $this.data('handsontable', instance);
- instance.init();
- }
- return $this;
- }
- // Action case
- else {
- args = [];
- if (arguments.length > 1) {
- for (i = 1, ilen = arguments.length; i < ilen; i++) {
- args.push(arguments[i]);
- }
- }
- if (instance) {
- if (typeof instance[action] !== 'undefined') {
- output = instance[action].apply(instance, args);
- }
- else {
- throw new Error('Handsontable do not provide action: ' + action);
- }
- }
- return output;
- }
- };
- /**
- * Handsontable TableView constructor
- * @param {Object} instance
- */
- Handsontable.TableView = function (instance) {
- var that = this
- , $window = $(window)
- , $documentElement = $(document.documentElement);
- this.instance = instance;
- this.settings = instance.getSettings();
- this.settingsFromDOM = instance.getSettingsFromDOM();
- instance.rootElement.data('originalStyle', instance.rootElement[0].getAttribute('style')); //needed to retrieve original style in jsFiddle link generator in HT examples. may be removed in future versions
- // in IE7 getAttribute('style') returns an object instead of a string, but we only support IE8+
- instance.rootElement.addClass('handsontable');
- var table = document.createElement('TABLE');
- table.className = 'htCore';
- this.THEAD = document.createElement('THEAD');
- table.appendChild(this.THEAD);
- this.TBODY = document.createElement('TBODY');
- table.appendChild(this.TBODY);
- instance.$table = $(table);
- instance.rootElement.prepend(instance.$table);
- instance.rootElement.on('mousedown.handsontable', function (event) {
- if (!that.isTextSelectionAllowed(event.target)) {
- clearTextSelection();
- event.preventDefault();
- window.focus(); //make sure that window that contains HOT is active. Important when HOT is in iframe.
- }
- });
- $documentElement.on('keyup.' + instance.guid, function (event) {
- if (instance.selection.isInProgress() && !event.shiftKey) {
- instance.selection.finish();
- }
- });
- var isMouseDown;
- $documentElement.on('mouseup.' + instance.guid, function (event) {
- if (instance.selection.isInProgress() && event.which === 1) { //is left mouse button
- instance.selection.finish();
- }
- isMouseDown = false;
- if (instance.autofill.handle && instance.autofill.handle.isDragged) {
- if (instance.autofill.handle.isDragged > 1) {
- instance.autofill.apply();
- }
- instance.autofill.handle.isDragged = 0;
- }
- if (Handsontable.helper.isOutsideInput(document.activeElement)) {
- instance.unlisten();
- }
- });
- $documentElement.on('mousedown.' + instance.guid, function (event) {
- var next = event.target;
- if (next !== that.wt.wtTable.spreader) { //immediate click on "spreader" means click on the right side of vertical scrollbar
- while (next !== document.documentElement) {
- if (next === null) {
- return; //click on something that was a row but now is detached (possibly because your click triggered a rerender)
- }
- if (next === instance.rootElement[0] || next.nodeName === 'HANDSONTABLE-TABLE') {
- return; //click inside container or Web Component (HANDSONTABLE-TABLE is the name of the custom element)
- }
- next = next.parentNode;
- }
- }
- if (that.settings.outsideClickDeselects) {
- instance.deselectCell();
- }
- else {
- instance.destroyEditor();
- }
- });
- instance.rootElement.on('mousedown.handsontable', '.dragdealer', function () {
- instance.destroyEditor();
- });
- instance.$table.on('selectstart', function (event) {
- if (that.settings.fragmentSelection) {
- return;
- }
- //https://github.com/warpech/jquery-handsontable/issues/160
- //selectstart is IE only event. Prevent text from being selected when performing drag down in IE8
- event.preventDefault();
- });
- var clearTextSelection = function () {
- //http://stackoverflow.com/questions/3169786/clear-text-selection-with-javascript
- if (window.getSelection) {
- if (window.getSelection().empty) { // Chrome
- window.getSelection().empty();
- } else if (window.getSelection().removeAllRanges) { // Firefox
- window.getSelection().removeAllRanges();
- }
- } else if (document.selection) { // IE?
- document.selection.empty();
- }
- };
- var walkontableConfig = {
- table: table,
- stretchH: this.settings.stretchH,
- data: instance.getDataAtCell,
- totalRows: instance.countRows,
- totalColumns: instance.countCols,
- nativeScrollbars: this.settings.nativeScrollbars,
- offsetRow: 0,
- offsetColumn: 0,
- width: this.getWidth(),
- height: this.getHeight(),
- fixedColumnsLeft: function () {
- return that.settings.fixedColumnsLeft;
- },
- fixedRowsTop: function () {
- return that.settings.fixedRowsTop;
- },
- rowHeaders: function () {
- return instance.hasRowHeaders() ? [function (index, TH) {
- that.appendRowHeader(index, TH);
- }] : []
- },
- columnHeaders: function () {
- return instance.hasColHeaders() ? [function (index, TH) {
- that.appendColHeader(index, TH);
- }] : []
- },
- columnWidth: instance.getColWidth,
- cellRenderer: function (row, col, TD) {
- var prop = that.instance.colToProp(col)
- , cellProperties = that.instance.getCellMeta(row, col)
- , renderer = that.instance.getCellRenderer(cellProperties)
- var value = that.instance.getDataAtRowProp(row, prop);
- renderer(that.instance, TD, row, col, prop, value, cellProperties);
- that.instance.PluginHooks.run('afterRenderer', TD, row, col, prop, value, cellProperties);
- },
- selections: {
- current: {
- className: 'current',
- border: {
- width: 2,
- color: '#5292F7',
- style: 'solid',
- cornerVisible: function () {
- return that.settings.fillHandle && !that.isCellEdited() && !instance.selection.isMultiple()
- }
- }
- },
- area: {
- className: 'area',
- border: {
- width: 1,
- color: '#89AFF9',
- style: 'solid',
- cornerVisible: function () {
- return that.settings.fillHandle && !that.isCellEdited() && instance.selection.isMultiple()
- }
- }
- },
- highlight: {
- highlightRowClassName: that.settings.currentRowClassName,
- highlightColumnClassName: that.settings.currentColClassName
- },
- fill: {
- className: 'fill',
- border: {
- width: 1,
- color: 'red',
- style: 'solid'
- }
- }
- },
- hideBorderOnMouseDownOver: function () {
- return that.settings.fragmentSelection;
- },
- onCellMouseDown: function (event, coords, TD) {
- instance.listen();
- isMouseDown = true;
- var coordsObj = {row: coords[0], col: coords[1]};
- if (event.button === 2 && instance.selection.inInSelection(coordsObj)) { //right mouse button
- //do nothing
- }
- else if (event.shiftKey) {
- instance.selection.setRangeEnd(coordsObj);
- }
- else {
- instance.selection.setRangeStart(coordsObj);
- }
- instance.PluginHooks.run('afterOnCellMouseDown', event, coords, TD);
- },
- /*onCellMouseOut: function (/*event, coords, TD* /) {
- if (isMouseDown && that.settings.fragmentSelection === 'single') {
- clearTextSelection(); //otherwise text selection blinks during multiple cells selection
- }
- },*/
- onCellMouseOver: function (event, coords/*, TD*/) {
- var coordsObj = {row: coords[0], col: coords[1]};
- if (isMouseDown) {
- /*if (that.settings.fragmentSelection === 'single') {
- clearTextSelection(); //otherwise text selection blinks during multiple cells selection
- }*/
- instance.selection.setRangeEnd(coordsObj);
- }
- else if (instance.autofill.handle && instance.autofill.handle.isDragged) {
- instance.autofill.handle.isDragged++;
- instance.autofill.showBorder(coords);
- }
- },
- onCellCornerMouseDown: function (event) {
- instance.autofill.handle.isDragged = 1;
- event.preventDefault();
- instance.PluginHooks.run('afterOnCellCornerMouseDown', event);
- },
- onCellCornerDblClick: function () {
- instance.autofill.selectAdjacent();
- },
- beforeDraw: function (force) {
- that.beforeRender(force);
- },
- onDraw: function(force){
- that.onDraw(force);
- }
- };
- instance.PluginHooks.run('beforeInitWalkontable', walkontableConfig);
- this.wt = new Walkontable(walkontableConfig);
- $window.on('resize.' + instance.guid, function () {
- instance.registerTimeout('resizeTimeout', function () {
- instance.parseSettingsFromDOM();
- var newWidth = that.getWidth();
- var newHeight = that.getHeight();
- if (walkontableConfig.width !== newWidth || walkontableConfig.height !== newHeight) {
- instance.forceFullRender = true;
- that.render();
- walkontableConfig.width = newWidth;
- walkontableConfig.height = newHeight;
- }
- }, 60);
- });
- $(that.wt.wtTable.spreader).on('mousedown.handsontable, contextmenu.handsontable', function (event) {
- if (event.target === that.wt.wtTable.spreader && event.which === 3) { //right mouse button exactly on spreader means right clickon the right hand side of vertical scrollbar
- event.stopPropagation();
- }
- });
- $documentElement.on('click.' + instance.guid, function () {
- if (that.settings.observeDOMVisibility) {
- if (that.wt.drawInterrupted) {
- that.instance.forceFullRender = true;
- that.render();
- }
- }
- });
- };
- Handsontable.TableView.prototype.isTextSelectionAllowed = function (el) {
- if (el.nodeName === 'TEXTAREA') {
- return (true);
- }
- if (this.settings.fragmentSelection && this.wt.wtDom.isChildOf(el, this.TBODY)) {
- return (true);
- }
- return false;
- };
- Handsontable.TableView.prototype.isCellEdited = function () {
- var activeEditor = this.instance.getActiveEditor();
- return activeEditor && activeEditor.isOpened();
- };
- Handsontable.TableView.prototype.getWidth = function () {
- var val = this.settings.width !== void 0 ? this.settings.width : this.settingsFromDOM.width;
- return typeof val === 'function' ? val() : val;
- };
- Handsontable.TableView.prototype.getHeight = function () {
- var val = this.settings.height !== void 0 ? this.settings.height : this.settingsFromDOM.height;
- return typeof val === 'function' ? val() : val;
- };
- Handsontable.TableView.prototype.beforeRender = function (force) {
- if (force) { //force = did Walkontable decide to do full render
- this.instance.PluginHooks.run('beforeRender', this.instance.forceFullRender); //this.instance.forceFullRender = did Handsontable request full render?
- this.wt.update('width', this.getWidth());
- this.wt.update('height', this.getHeight());
- }
- };
- Handsontable.TableView.prototype.onDraw = function(force){
- if (force) { //force = did Walkontable decide to do full render
- this.instance.PluginHooks.run('afterRender', this.instance.forceFullRender); //this.instance.forceFullRender = did Handsontable request full render?
- }
- };
- Handsontable.TableView.prototype.render = function () {
- this.wt.draw(!this.instance.forceFullRender);
- this.instance.forceFullRender = false;
- this.instance.rootElement.triggerHandler('render.handsontable');
- };
- /**
- * Returns td object given coordinates
- */
- Handsontable.TableView.prototype.getCellAtCoords = function (coords) {
- var td = this.wt.wtTable.getCell([coords.row, coords.col]);
- if (td < 0) { //there was an exit code (cell is out of bounds)
- return null;
- }
- else {
- return td;
- }
- };
- /**
- * Scroll viewport to selection
- * @param coords
- */
- Handsontable.TableView.prototype.scrollViewport = function (coords) {
- this.wt.scrollViewport([coords.row, coords.col]);
- };
- /**
- * Append row header to a TH element
- * @param row
- * @param TH
- */
- Handsontable.TableView.prototype.appendRowHeader = function (row, TH) {
- if (row > -1) {
- this.wt.wtDom.fastInnerHTML(TH, this.instance.getRowHeader(row));
- }
- else {
- var DIV = document.createElement('DIV');
- DIV.className = 'relative';
- this.wt.wtDom.fastInnerText(DIV, '\u00A0');
- this.wt.wtDom.empty(TH);
- TH.appendChild(DIV);
- }
- };
- /**
- * Append column header to a TH element
- * @param col
- * @param TH
- */
- Handsontable.TableView.prototype.appendColHeader = function (col, TH) {
- var DIV = document.createElement('DIV')
- , SPAN = document.createElement('SPAN');
- DIV.className = 'relative';
- SPAN.className = 'colHeader';
- this.wt.wtDom.fastInnerHTML(SPAN, this.instance.getColHeader(col));
- DIV.appendChild(SPAN);
- this.wt.wtDom.empty(TH);
- TH.appendChild(DIV);
- this.instance.PluginHooks.run('afterGetColHeader', col, TH);
- };
- /**
- * Given a element's left position relative to the viewport, returns maximum element width until the right edge of the viewport (before scrollbar)
- * @param {Number} left
- * @return {Number}
- */
- Handsontable.TableView.prototype.maximumVisibleElementWidth = function (left) {
- var rootWidth = this.wt.wtViewport.getWorkspaceWidth();
- if(this.settings.nativeScrollbars) {
- return rootWidth;
- }
- return rootWidth - left;
- };
- /**
- * Given a element's top position relative to the viewport, returns maximum element height until the bottom edge of the viewport (before scrollbar)
- * @param {Number} top
- * @return {Number}
- */
- Handsontable.TableView.prototype.maximumVisibleElementHeight = function (top) {
- var rootHeight = this.wt.wtViewport.getWorkspaceHeight();
- if(this.settings.nativeScrollbars) {
- return rootHeight;
- }
- return rootHeight - top;
- };
- /**
- * Utility to register editors and common namespace for keeping reference to all editor classes
- */
- (function (Handsontable) {
- 'use strict';
- function RegisteredEditor(editorClass) {
- var clazz, instances;
- instances = {};
- clazz = editorClass;
- this.getInstance = function (hotInstance) {
- if (!(hotInstance.guid in instances)) {
- instances[hotInstance.guid] = new clazz(hotInstance);
- }
- return instances[hotInstance.guid];
- }
- }
- var registeredEditors = {};
- Handsontable.editors = {
- /**
- * Registers editor under given name
- * @param {String} editorName
- * @param {Function} editorClass
- */
- registerEditor: function (editorName, editorClass) {
- registeredEditors[editorName] = new RegisteredEditor(editorClass);
- },
- /**
- * Returns instance (singleton) of editor class
- * @param {String|Function} editorName/editorClass
- * @returns {Function} editorClass
- */
- getEditor: function (editorName, hotInstance) {
- if (typeof editorName == 'function'){
- var editorClass = editorName;
- editorName = editorClass.toString();
- this.registerEditor(editorName, editorClass);
- }
- if (typeof editorName != 'string'){
- throw Error('Only strings and functions can be passed as "editor" parameter ');
- }
- if (!(editorName in registeredEditors)) {
- throw Error('No editor registered under name "' + editorName + '"');
- }
- return registeredEditors[editorName].getInstance(hotInstance);
- }
- };
- })(Handsontable);
- (function(Handsontable){
- 'use strict';
- Handsontable.EditorManager = function(instance, priv, selection, datamap){
- var that = this;
- var $document = $(document);
- var keyCodes = Handsontable.helper.keyCode;
- var activeEditor;
- var init = function () {
- priv.onCut = function onCut() {
- if (!instance.isListening()) {
- return;
- }
- selection.empty();
- };
- priv.onPaste = function onPaste(str) {
- if (!instance.isListening() || !selection.isSelected()) {
- return;
- }
- var input = str.replace(/^[\r\n]*/g, '').replace(/[\r\n]*$/g, '') //remove newline from the start and the end of the input
- , inputArray = SheetClip.parse(input)
- , coords = instance.getCornerCoords([priv.selStart.coords(), priv.selEnd.coords()])
- , areaStart = coords.TL
- , areaEnd = {
- row: Math.max(coords.BR.row, inputArray.length - 1 + coords.TL.row),
- col: Math.max(coords.BR.col, inputArray[0].length - 1 + coords.TL.col)
- };
- instance.PluginHooks.once('afterChange', function (changes, source) {
- if (changes && changes.length) {
- instance.selectCell(areaStart.row, areaStart.col, areaEnd.row, areaEnd.col);
- }
- });
- instance.populateFromArray(areaStart.row, areaStart.col, inputArray, areaEnd.row, areaEnd.col, 'paste', priv.settings.pasteMode);
- };
- function onKeyDown(event) {
- if (!instance.isListening()) {
- return;
- }
- if (priv.settings.beforeOnKeyDown) { // HOT in HOT Plugin
- priv.settings.beforeOnKeyDown.call(instance, event);
- }
- if (Handsontable.helper.isCtrlKey(event.keyCode)) {
- //when CTRL is pressed, prepare selectable text in textarea
- //http://stackoverflow.com/questions/3902635/how-does-one-capture-a-macs-command-key-via-javascript
- that.setCopyableText();
- return;
- }
- instance.PluginHooks.run('beforeKeyDown', event);
- if (!event.isImmediatePropagationStopped()) {
- priv.lastKeyCode = event.keyCode;
- if (selection.isSelected()) {
- var ctrlDown = (event.ctrlKey || event.metaKey) && !event.altKey; //catch CTRL but not right ALT (which in some systems triggers ALT+CTRL)
- if (Handsontable.helper.isPrintableChar(event.keyCode) && ctrlDown) {
- if (event.keyCode === 65) { //CTRL + A
- selection.selectAll(); //select all cells
- that.setCopyableText();
- event.preventDefault();
- event.stopImmediatePropagation();
- }
- }
- if (!activeEditor.isWaiting()) {
- if (!Handsontable.helper.isMetaKey(event.keyCode) && !ctrlDown) {
- that.openEditor('');
- event.stopPropagation(); //required by HandsontableEditor
- return;
- }
- }
- var rangeModifier = event.shiftKey ? selection.setRangeEnd : selection.setRangeStart;
- switch (event.keyCode) {
- case keyCodes.ARROW_UP:
- if (that.isEditorOpened() && !activeEditor.isWaiting()){
- that.closeEditorAndSaveChanges(ctrlDown);
- }
- moveSelectionUp(event.shiftKey);
- event.preventDefault();
- event.stopPropagation(); //required by HandsontableEditor
- break;
- case keyCodes.ARROW_DOWN:
- if (that.isEditorOpened() && !activeEditor.isWaiting()){
- that.closeEditorAndSaveChanges(ctrlDown);
- }
- moveSelectionDown(event.shiftKey);
- event.preventDefault();
- event.stopPropagation(); //required by HandsontableEditor
- break;
- case keyCodes.ARROW_RIGHT:
- if(that.isEditorOpened() && !activeEditor.isWaiting()){
- that.closeEditorAndSaveChanges(ctrlDown);
- }
- moveSelectionRight(event.shiftKey);
- event.preventDefault();
- event.stopPropagation(); //required by HandsontableEditor
- break;
- case keyCodes.ARROW_LEFT:
- if(that.isEditorOpened() && !activeEditor.isWaiting()){
- that.closeEditorAndSaveChanges(ctrlDown);
- }
- moveSelectionLeft(event.shiftKey);
- event.preventDefault();
- event.stopPropagation(); //required by HandsontableEditor
- break;
- case keyCodes.TAB:
- var tabMoves = typeof priv.settings.tabMoves === 'function' ? priv.settings.tabMoves(event) : priv.settings.tabMoves;
- if (event.shiftKey) {
- selection.transformStart(-tabMoves.row, -tabMoves.col); //move selection left
- }
- else {
- selection.transformStart(tabMoves.row, tabMoves.col, true); //move selection right (add a new column if needed)
- }
- event.preventDefault();
- event.stopPropagation(); //required by HandsontableEditor
- break;
- case keyCodes.BACKSPACE:
- case keyCodes.DELETE:
- selection.empty(event);
- that.prepareEditor();
- event.preventDefault();
- break;
- case keyCodes.F2: /* F2 */
- that.openEditor();
- event.preventDefault(); //prevent Opera from opening Go to Page dialog
- break;
- case keyCodes.ENTER: /* return/enter */
- if(that.isEditorOpened()){
- if (activeEditor.state !== Handsontable.EditorState.WAITING){
- that.closeEditorAndSaveChanges(ctrlDown);
- }
- moveSelectionAfterEnter(event.shiftKey);
- } else {
- if (instance.getSettings().enterBeginsEditing){
- that.openEditor();
- } else {
- moveSelectionAfterEnter(event.shiftKey);
- }
- }
- event.preventDefault(); //don't add newline to field
- event.stopImmediatePropagation(); //required by HandsontableEditor
- break;
- case keyCodes.ESCAPE:
- if(that.isEditorOpened()){
- that.closeEditorAndRestoreOriginalValue(ctrlDown);
- }
- event.preventDefault();
- break;
- case keyCodes.HOME:
- if (event.ctrlKey || event.metaKey) {
- rangeModifier({row: 0, col: priv.selStart.col()});
- }
- else {
- rangeModifier({row: priv.selStart.row(), col: 0});
- }
- event.preventDefault(); //don't scroll the window
- event.stopPropagation(); //required by HandsontableEditor
- break;
- case keyCodes.END:
- if (event.ctrlKey || event.metaKey) {
- rangeModifier({row: instance.countRows() - 1, col: priv.selStart.col()});
- }
- else {
- rangeModifier({row: priv.selStart.row(), col: instance.countCols() - 1});
- }
- event.preventDefault(); //don't scroll the window
- event.stopPropagation(); //required by HandsontableEditor
- break;
- case keyCodes.PAGE_UP:
- selection.transformStart(-instance.countVisibleRows(), 0);
- instance.view.wt.scrollVertical(-instance.countVisibleRows());
- instance.view.render();
- event.preventDefault(); //don't page up the window
- event.stopPropagation(); //required by HandsontableEditor
- break;
- case keyCodes.PAGE_DOWN:
- selection.transformStart(instance.countVisibleRows(), 0);
- instance.view.wt.scrollVertical(instance.countVisibleRows());
- instance.view.render();
- event.preventDefault(); //don't page down the window
- event.stopPropagation(); //required by HandsontableEditor
- break;
- default:
- break;
- }
- }
- }
- }
- $document.on('keydown.handsontable.' + instance.guid, onKeyDown);
- function onDblClick() {
- // that.instance.destroyEditor();
- that.openEditor();
- }
- instance.view.wt.update('onCellDblClick', onDblClick);
- instance.copyPaste = CopyPaste.getInstance();
- instance.copyPaste.onCut(priv.onCut);
- instance.copyPaste.onPaste(priv.onPaste);
- instance.addHook('afterDestroy', function(){
- $document.off('keydown.handsontable.' + instance.guid);
- });
- function moveSelectionAfterEnter(shiftKey){
- var enterMoves = typeof priv.settings.enterMoves === 'function' ? priv.settings.enterMoves(event) : priv.settings.enterMoves;
- if (shiftKey) {
- selection.transformStart(-enterMoves.row, -enterMoves.col); //move selection up
- }
- else {
- selection.transformStart(enterMoves.row, enterMoves.col, true); //move selection down (add a new row if needed)
- }
- }
- function moveSelectionUp(shiftKey){
- if (shiftKey) {
- selection.transformEnd(-1, 0);
- }
- else {
- selection.transformStart(-1, 0);
- }
- }
- function moveSelectionDown(shiftKey){
- if (shiftKey) {
- selection.transformEnd(1, 0); //expanding selection down with shift
- }
- else {
- selection.transformStart(1, 0); //move selection down
- }
- }
- function moveSelectionRight(shiftKey){
- if (shiftKey) {
- selection.transformEnd(0, 1);
- }
- else {
- selection.transformStart(0, 1);
- }
- }
- function moveSelectionLeft(shiftKey){
- if (shiftKey) {
- selection.transformEnd(0, -1);
- }
- else {
- selection.transformStart(0, -1);
- }
- }
- };
- /**
- * Destroy current editor, if exists
- * @param {Boolean} revertOriginal
- */
- this.destroyEditor = function (revertOriginal) {
- this.closeEditor(revertOriginal);
- };
- this.getActiveEditor = function () {
- return activeEditor;
- };
- /**
- * Prepares copyable text in the invisible textarea
- */
- this.setCopyableText = function () {
- var selection = instance.getSelected();
- var settings = instance.getSettings();
- var copyRowsLimit = settings.copyRowsLimit;
- var copyColsLimit = settings.copyColsLimit;
- var startRow = Math.min(selection[0], selection[2]);
- var startCol = Math.min(selection[1], selection[3]);
- var endRow = Math.max(selection[0], selection[2]);
- var endCol = Math.max(selection[1], selection[3]);
- var finalEndRow = Math.min(endRow, startRow + copyRowsLimit - 1);
- var finalEndCol = Math.min(endCol, startCol + copyColsLimit - 1);
- instance.copyPaste.copyable(datamap.getText({row: startRow, col: startCol}, {row: finalEndRow, col: finalEndCol}));
- if (endRow !== finalEndRow || endCol !== finalEndCol) {
- instance.PluginHooks.run("afterCopyLimit", endRow - startRow + 1, endCol - startCol + 1, copyRowsLimit, copyColsLimit);
- }
- };
- var pendingPrepare = false;
- /**
- * Prepare text input to be displayed at given grid cell
- */
- this.prepareEditor = function () {
- if (activeEditor && activeEditor.state === Handsontable.EditorState.WAITING){
- if(!pendingPrepare){
- pendingPrepare = true;
- this.closeEditor(false, false, function(){
- pendingPrepare = false;
- that.prepareEditor();
- });
- }
- return;
- }
- var row = priv.selStart.row();
- var col = priv.selStart.col();
- var prop = instance.colToProp(col);
- var td = instance.getCell(row, col);
- var originalValue = instance.getDataAtCell(row, col);
- var cellProperties = instance.getCellMeta(row, col);
- var editorClass = instance.getCellEditor(cellProperties);
- activeEditor = Handsontable.editors.getEditor(editorClass, instance);
- activeEditor.prepare(row, col, prop, td, originalValue, cellProperties);
- };
- this.isEditorOpened = function () {
- return activeEditor.isOpened();
- };
- this.openEditor = function (initialValue) {
- activeEditor.beginEditing(initialValue);
- };
- this.closeEditor = function (restoreOriginalValue, ctrlDown, callback) {
- if (!activeEditor){
- if(callback) {
- callback(false);
- }
- }
- else {
- activeEditor.finishEditing(restoreOriginalValue, ctrlDown, callback);
- }
- };
- this.closeEditorAndSaveChanges = function(ctrlDown){
- return this.closeEditor(false, ctrlDown);
- };
- this.closeEditorAndRestoreOriginalValue = function(ctrlDown){
- return this.closeEditor(true, ctrlDown);
- };
- init();
- };
- })(Handsontable);
- /**
- * DOM helper optimized for maximum performance
- * It is recommended for Handsontable plugins and renderers, because it is much faster than jQuery
- * @type {WalkonableDom}
- */
- Handsontable.Dom = new WalkontableDom();
- /**
- * Returns true if keyCode represents a printable character
- * @param {Number} keyCode
- * @return {Boolean}
- */
- Handsontable.helper.isPrintableChar = function (keyCode) {
- return ((keyCode == 32) || //space
- (keyCode >= 48 && keyCode <= 57) || //0-9
- (keyCode >= 96 && keyCode <= 111) || //numpad
- (keyCode >= 186 && keyCode <= 192) || //;=,-./`
- (keyCode >= 219 && keyCode <= 222) || //[]{}\|"'
- keyCode >= 226 || //special chars (229 for Asian chars)
- (keyCode >= 65 && keyCode <= 90)); //a-z
- };
- Handsontable.helper.isMetaKey = function (keyCode) {
- var keyCodes = Handsontable.helper.keyCode;
- var metaKeys = [
- keyCodes.ARROW_DOWN,
- keyCodes.ARROW_UP,
- keyCodes.ARROW_LEFT,
- keyCodes.ARROW_RIGHT,
- keyCodes.HOME,
- keyCodes.END,
- keyCodes.DELETE,
- keyCodes.BACKSPACE,
- keyCodes.F1,
- keyCodes.F2,
- keyCodes.F3,
- keyCodes.F4,
- keyCodes.F5,
- keyCodes.F6,
- keyCodes.F7,
- keyCodes.F8,
- keyCodes.F9,
- keyCodes.F10,
- keyCodes.F11,
- keyCodes.F12,
- keyCodes.TAB,
- keyCodes.PAGE_DOWN,
- keyCodes.PAGE_UP,
- keyCodes.ENTER,
- keyCodes.ESCAPE,
- keyCodes.SHIFT
- ];
- return metaKeys.indexOf(keyCode) != -1;
- };
- Handsontable.helper.isCtrlKey = function (keyCode) {
- return [17, 224, 91, 93].indexOf(keyCode) != -1;
- };
- /**
- * Converts a value to string
- * @param value
- * @return {String}
- */
- Handsontable.helper.stringify = function (value) {
- switch (typeof value) {
- case 'string':
- case 'number':
- return value + '';
- break;
- case 'object':
- if (value === null) {
- return '';
- }
- else {
- return value.toString();
- }
- break;
- case 'undefined':
- return '';
- break;
- default:
- return value.toString();
- }
- };
- /**
- * Generates spreadsheet-like column names: A, B, C, ..., Z, AA, AB, etc
- * @param index
- * @returns {String}
- */
- Handsontable.helper.spreadsheetColumnLabel = function (index) {
- var dividend = index + 1;
- var columnLabel = '';
- var modulo;
- while (dividend > 0) {
- modulo = (dividend - 1) % 26;
- columnLabel = String.fromCharCode(65 + modulo) + columnLabel;
- dividend = parseInt((dividend - modulo) / 26, 10);
- }
- return columnLabel;
- };
- /**
- * Checks if value of n is a numeric one
- * http://jsperf.com/isnan-vs-isnumeric/4
- * @param n
- * @returns {boolean}
- */
- Handsontable.helper.isNumeric = function (n) {
- var t = typeof n;
- return t == 'number' ? !isNaN(n) && isFinite(n) :
- t == 'string' ? !n.length ? false :
- n.length == 1 ? /\d/.test(n) :
- /^\s*[+-]?\s*(?:(?:\d+(?:\.\d+)?(?:e[+-]?\d+)?)|(?:0x[a-f\d]+))\s*$/i.test(n) :
- t == 'object' ? !!n && typeof n.valueOf() == "number" && !(n instanceof Date) : false;
- };
- Handsontable.helper.isArray = function (obj) {
- return Object.prototype.toString.call(obj).match(/array/i) !== null;
- };
- /**
- * Checks if child is a descendant of given parent node
- * http://stackoverflow.com/questions/2234979/how-to-check-in-javascript-if-one-element-is-a-child-of-another
- * @param parent
- * @param child
- * @returns {boolean}
- */
- Handsontable.helper.isDescendant = function (parent, child) {
- var node = child.parentNode;
- while (node != null) {
- if (node == parent) {
- return true;
- }
- node = node.parentNode;
- }
- return false;
- };
- /**
- * Generates a random hex string. Used as namespace for Handsontable instance events.
- * @return {String} - 16 character random string: "92b1bfc74ec4"
- */
- Handsontable.helper.randomString = function () {
- return walkontableRandomString();
- };
- /**
- * Inherit without without calling parent constructor, and setting `Child.prototype.constructor` to `Child` instead of `Parent`.
- * Creates temporary dummy function to call it as constructor.
- * Described in ticket: https://github.com/warpech/jquery-handsontable/pull/516
- * @param {Object} Child child class
- * @param {Object} Parent parent class
- * @return {Object} extended Child
- */
- Handsontable.helper.inherit = function (Child, Parent) {
- Parent.prototype.constructor = Parent;
- Child.prototype = new Parent();
- Child.prototype.constructor = Child;
- return Child;
- };
- /**
- * Perform shallow extend of a target object with extension's own properties
- * @param {Object} target An object that will receive the new properties
- * @param {Object} extension An object containing additional properties to merge into the target
- */
- Handsontable.helper.extend = function (target, extension) {
- for (var i in extension) {
- if (extension.hasOwnProperty(i)) {
- target[i] = extension[i];
- }
- }
- };
- Handsontable.helper.getPrototypeOf = function (obj) {
- var prototype;
- if(typeof obj.__proto__ == "object"){
- prototype = obj.__proto__;
- } else {
- var oldConstructor,
- constructor = obj.constructor;
- if (typeof obj.constructor == "function") {
- oldConstructor = constructor;
- if (delete obj.constructor){
- constructor = obj.constructor; // get real constructor
- obj.constructor = oldConstructor; // restore constructor
- }
- }
- prototype = constructor ? constructor.prototype : null; // needed for IE
- }
- return prototype;
- };
- /**
- * Factory for columns constructors.
- * @param {Object} GridSettings
- * @param {Array} conflictList
- * @return {Object} ColumnSettings
- */
- Handsontable.helper.columnFactory = function (GridSettings, conflictList) {
- function ColumnSettings () {}
- Handsontable.helper.inherit(ColumnSettings, GridSettings);
- // Clear conflict settings
- for (var i = 0, len = conflictList.length; i < len; i++) {
- ColumnSettings.prototype[conflictList[i]] = void 0;
- }
- return ColumnSettings;
- };
- Handsontable.helper.translateRowsToColumns = function (input) {
- var i
- , ilen
- , j
- , jlen
- , output = []
- , olen = 0;
- for (i = 0, ilen = input.length; i < ilen; i++) {
- for (j = 0, jlen = input[i].length; j < jlen; j++) {
- if (j == olen) {
- output.push([]);
- olen++;
- }
- output[j].push(input[i][j])
- }
- }
- return output;
- };
- Handsontable.helper.to2dArray = function (arr) {
- var i = 0
- , ilen = arr.length;
- while (i < ilen) {
- arr[i] = [arr[i]];
- i++;
- }
- };
- Handsontable.helper.extendArray = function (arr, extension) {
- var i = 0
- , ilen = extension.length;
- while (i < ilen) {
- arr.push(extension[i]);
- i++;
- }
- };
- /**
- * Determines if the given DOM element is an input field placed outside of HOT.
- * Notice: By 'input' we mean input, textarea and select nodes
- * @param element - DOM element
- * @returns {boolean}
- */
- Handsontable.helper.isOutsideInput = function (element) {
- var inputs = ['INPUT', 'SELECT', 'TEXTAREA'];
- return inputs.indexOf(element.nodeName) > -1 && element.className.indexOf('handsontableInput') == -1;
- };
- Handsontable.helper.keyCode = {
- MOUSE_LEFT: 1,
- MOUSE_RIGHT: 3,
- MOUSE_MIDDLE: 2,
- BACKSPACE: 8,
- COMMA: 188,
- DELETE: 46,
- END: 35,
- ENTER: 13,
- ESCAPE: 27,
- HOME: 36,
- PAGE_DOWN: 34,
- PAGE_UP: 33,
- PERIOD: 190,
- SPACE: 32,
- SHIFT: 16,
- TAB: 9,
- ARROW_RIGHT: 39,
- ARROW_LEFT: 37,
- ARROW_UP: 38,
- ARROW_DOWN: 40,
- F1: 112,
- F2: 113,
- F3: 114,
- F4: 115,
- F5: 116,
- F6: 117,
- F7: 118,
- F8: 119,
- F9: 120,
- F10: 121,
- F11: 122,
- F12: 123,
- A: 65,
- X: 88,
- C: 67,
- V: 86
- };
- /**
- * Determines whether given object is a plain Object.
- * Note: String and Array are not plain Objects
- * @param {*} obj
- * @returns {boolean}
- */
- Handsontable.helper.isObject = function (obj) {
- return Object.prototype.toString.call(obj) == '[object Object]';
- };
- /**
- * Determines whether given object is an Array.
- * Note: String is not an Array
- * @param {*} obj
- * @returns {boolean}
- */
- Handsontable.helper.isArray = function(obj){
- return Array.isArray ? Array.isArray(obj) : Object.prototype.toString.call(obj) == '[object Array]';
- };
- Handsontable.helper.pivot = function (arr) {
- var pivotedArr = [];
- if(!arr || arr.length == 0 || !arr[0] || arr[0].length == 0){
- return pivotedArr;
- }
- var rowCount = arr.length;
- var colCount = arr[0].length;
- for(var i = 0; i < rowCount; i++){
- for(var j = 0; j < colCount; j++){
- if(!pivotedArr[j]){
- pivotedArr[j] = [];
- }
- pivotedArr[j][i] = arr[i][j];
- }
- }
- return pivotedArr;
- };
- Handsontable.helper.proxy = function (fun, context) {
- return function () {
- return fun.apply(context, arguments);
- };
- };
- Handsontable.helper.cellMethodLookupFactory = function (methodName) {
- return function cellMethodLookup (row, col) {
- return (function getMethodFromProperties(properties) {
- if (!properties){
- return; //method not found
- }
- else if(properties.hasOwnProperty(methodName)){
- return properties[methodName]; //method defined directly
- } else if(properties.hasOwnProperty('type')){
- var type;
- if(typeof properties.type != 'string' ){
- throw new Error('Cell type must be a string ');
- }
- type = translateTypeNameToObject(properties.type);
- return type[methodName]; //method defined in type. if does not exist (eg. validator), returns undefined
- }
- return getMethodFromProperties(Handsontable.helper.getPrototypeOf(properties));
- })(typeof row == 'number' ? this.getCellMeta(row, col) : row);
- };
- function translateTypeNameToObject(typeName) {
- var type = Handsontable.cellTypes[typeName];
- if(typeof type == 'undefined'){
- throw new Error('You declared cell type "' + typeName + '" as a string that is not mapped to a known object. Cell type must be an object or a string mapped to an object in Handsontable.cellTypes');
- }
- return type;
- }
- };
- Handsontable.SelectionPoint = function () {
- this._row = null; //private use intended
- this._col = null;
- };
- Handsontable.SelectionPoint.prototype.exists = function () {
- return (this._row !== null);
- };
- Handsontable.SelectionPoint.prototype.row = function (val) {
- if (val !== void 0) {
- this._row = val;
- }
- return this._row;
- };
- Handsontable.SelectionPoint.prototype.col = function (val) {
- if (val !== void 0) {
- this._col = val;
- }
- return this._col;
- };
- Handsontable.SelectionPoint.prototype.coords = function (coords) {
- if (coords !== void 0) {
- this._row = coords.row;
- this._col = coords.col;
- }
- return {
- row: this._row,
- col: this._col
- }
- };
- Handsontable.SelectionPoint.prototype.arr = function (arr) {
- if (arr !== void 0) {
- this._row = arr[0];
- this._col = arr[1];
- }
- return [this._row, this._col]
- };
- /**
- * Default text renderer
- * @param {Object} instance Handsontable instance
- * @param {Element} TD Table cell where to render
- * @param {Number} row
- * @param {Number} col
- * @param {String|Number} prop Row object property name
- * @param value Value to render (remember to escape unsafe HTML before inserting to DOM!)
- * @param {Object} cellProperties Cell properites (shared by cell renderer and editor)
- */
- Handsontable.TextRenderer = function (instance, TD, row, col, prop, value, cellProperties) {
- if (!value && cellProperties.placeholder) {
- value = cellProperties.placeholder;
- instance.view.wt.wtDom.addClass(TD, 'htPlaceholder');
- }
- var escaped = Handsontable.helper.stringify(value);
- if (cellProperties.rendererTemplate) {
- instance.view.wt.wtDom.empty(TD);
- var TEMPLATE = document.createElement('TEMPLATE');
- TEMPLATE.setAttribute('bind', '{{}}');
- TEMPLATE.innerHTML = cellProperties.rendererTemplate;
- HTMLTemplateElement.decorate(TEMPLATE);
- TEMPLATE.model = instance.getDataAtRow(row);
- TD.appendChild(TEMPLATE);
- }
- else {
- instance.view.wt.wtDom.fastInnerText(TD, escaped); //this is faster than innerHTML. See: https://github.com/warpech/jquery-handsontable/wiki/JavaScript-&-DOM-performance-tips
- }
- if (cellProperties.readOnly) {
- instance.view.wt.wtDom.addClass(TD, 'htDimmed');
- }
- if (cellProperties.valid === false && cellProperties.invalidCellClassName) {
- instance.view.wt.wtDom.addClass(TD, cellProperties.invalidCellClassName);
- }
- };
- (function (Handsontable) {
- var clonableWRAPPER = document.createElement('DIV');
- clonableWRAPPER.className = 'htAutocompleteWrapper';
- var clonableARROW = document.createElement('DIV');
- clonableARROW.className = 'htAutocompleteArrow';
- clonableARROW.appendChild(document.createTextNode('\u25BC'));
- //this is faster than innerHTML. See: https://github.com/warpech/jquery-handsontable/wiki/JavaScript-&-DOM-performance-tips
- var wrapTdContentWithWrapper = function(TD, WRAPPER){
- WRAPPER.innerHTML = TD.innerHTML;
- Handsontable.Dom.empty(TD);
- TD.appendChild(WRAPPER);
- };
- /**
- * Autocomplete renderer
- * @param {Object} instance Handsontable instance
- * @param {Element} TD Table cell where to render
- * @param {Number} row
- * @param {Number} col
- * @param {String|Number} prop Row object property name
- * @param value Value to render (remember to escape unsafe HTML before inserting to DOM!)
- * @param {Object} cellProperties Cell properites (shared by cell renderer and editor)
- */
- Handsontable.AutocompleteRenderer = function (instance, TD, row, col, prop, value, cellProperties) {
- var WRAPPER = clonableWRAPPER.cloneNode(true); //this is faster than createElement
- var ARROW = clonableARROW.cloneNode(true); //this is faster than createElement
- Handsontable.TextRenderer(instance, TD, row, col, prop, value, cellProperties);
- // wrapTdContentWithWrapper(TD, WRAPPER);
- // WRAPPER.appendChild(ARROW);
- TD.appendChild(ARROW);
- Handsontable.Dom.addClass(TD, 'htAutocomplete');
- if (!TD.firstChild) { //http://jsperf.com/empty-node-if-needed
- //otherwise empty fields appear borderless in demo/renderers.html (IE)
- TD.appendChild(document.createTextNode('\u00A0')); //\u00A0 equals for a text node
- //this is faster than innerHTML. See: https://github.com/warpech/jquery-handsontable/wiki/JavaScript-&-DOM-performance-tips
- }
- if (!instance.acArrowListener) {
- //not very elegant but easy and fast
- instance.acArrowListener = function () {
- instance.view.wt.getSetting('onCellDblClick');
- };
- instance.rootElement.on('mousedown', '.htAutocompleteArrow', instance.acArrowListener); //this way we don't bind event listener to each arrow. We rely on propagation instead
- }
- };
- })(Handsontable);
- var clonableINPUT = document.createElement('INPUT');
- clonableINPUT.className = 'htCheckboxRendererInput';
- clonableINPUT.type = 'checkbox';
- clonableINPUT.setAttribute('autocomplete', 'off');
- /**
- * Checkbox renderer
- * @param {Object} instance Handsontable instance
- * @param {Element} TD Table cell where to render
- * @param {Number} row
- * @param {Number} col
- * @param {String|Number} prop Row object property name
- * @param value Value to render (remember to escape unsafe HTML before inserting to DOM!)
- * @param {Object} cellProperties Cell properites (shared by cell renderer and editor)
- */
- Handsontable.CheckboxRenderer = function (instance, TD, row, col, prop, value, cellProperties) {
- if (typeof cellProperties.checkedTemplate === "undefined") {
- cellProperties.checkedTemplate = true;
- }
- if (typeof cellProperties.uncheckedTemplate === "undefined") {
- cellProperties.uncheckedTemplate = false;
- }
- instance.view.wt.wtDom.empty(TD); //TODO identify under what circumstances this line can be removed
- var INPUT = clonableINPUT.cloneNode(false); //this is faster than createElement
- if (value === cellProperties.checkedTemplate || value === Handsontable.helper.stringify(cellProperties.checkedTemplate)) {
- INPUT.checked = true;
- TD.appendChild(INPUT);
- }
- else if (value === cellProperties.uncheckedTemplate || value === Handsontable.helper.stringify(cellProperties.uncheckedTemplate)) {
- TD.appendChild(INPUT);
- }
- else if (value === null) { //default value
- INPUT.className += ' noValue';
- TD.appendChild(INPUT);
- }
- else {
- instance.view.wt.wtDom.fastInnerText(TD, '#bad value#'); //this is faster than innerHTML. See: https://github.com/warpech/jquery-handsontable/wiki/JavaScript-&-DOM-performance-tips
- }
- var $input = $(INPUT);
- if (cellProperties.readOnly) {
- $input.on('click', function (event) {
- event.preventDefault();
- });
- }
- else {
- $input.on('mousedown', function (event) {
- event.stopPropagation(); //otherwise can confuse cell mousedown handler
- });
- $input.on('mouseup', function (event) {
- event.stopPropagation(); //otherwise can confuse cell dblclick handler
- });
- $input.on('change', function(){
- if (this.checked) {
- instance.setDataAtRowProp(row, prop, cellProperties.checkedTemplate);
- }
- else {
- instance.setDataAtRowProp(row, prop, cellProperties.uncheckedTemplate);
- }
- });
- }
- if(!instance.CheckboxRenderer || !instance.CheckboxRenderer.beforeKeyDownHookBound){
- instance.CheckboxRenderer = {
- beforeKeyDownHookBound : true
- };
- instance.addHook('beforeKeyDown', function(event){
- if(event.keyCode == 32){
- event.stopImmediatePropagation();
- event.preventDefault();
- var selection = instance.getSelected();
- var cell, checkbox, cellProperties;
- var selStart = {
- row: Math.min(selection[0], selection[2]),
- col: Math.min(selection[1], selection[3])
- };
- var selEnd = {
- row: Math.max(selection[0], selection[2]),
- col: Math.max(selection[1], selection[3])
- };
- for(var row = selStart.row; row <= selEnd.row; row++ ){
- for(var col = selEnd.col; col <= selEnd.col; col++){
- cell = instance.getCell(row, col);
- cellProperties = instance.getCellMeta(row, col);
- checkbox = cell.querySelectorAll('input[type=checkbox]');
- if(checkbox.length > 0 && !cellProperties.readOnly){
- for(var i = 0, len = checkbox.length; i < len; i++){
- checkbox[i].checked = !checkbox[i].checked;
- $(checkbox[i]).trigger('change');
- }
- }
- }
- }
- }
- });
- }
- return TD;
- };
- /**
- * Numeric cell renderer
- * @param {Object} instance Handsontable instance
- * @param {Element} TD Table cell where to render
- * @param {Number} row
- * @param {Number} col
- * @param {String|Number} prop Row object property name
- * @param value Value to render (remember to escape unsafe HTML before inserting to DOM!)
- * @param {Object} cellProperties Cell properites (shared by cell renderer and editor)
- */
- Handsontable.NumericRenderer = function (instance, TD, row, col, prop, value, cellProperties) {
- if (Handsontable.helper.isNumeric(value)) {
- if (typeof cellProperties.language !== 'undefined') {
- numeral.language(cellProperties.language)
- }
- value = numeral(value).format(cellProperties.format || '0'); //docs: http://numeraljs.com/
- instance.view.wt.wtDom.addClass(TD, 'htNumeric');
- }
- Handsontable.TextRenderer(instance, TD, row, col, prop, value, cellProperties);
- };
- (function(Handosntable){
- Handsontable.PasswordRenderer = function (instance, TD, row, col, prop, value, cellProperties) {
- Handsontable.TextRenderer.apply(this, arguments);
- value = TD.innerHTML;
- var hash;
- var hashLength = cellProperties.hashLength || value.length;
- var hashSymbol = cellProperties.hashSymbol || '*';
- for( hash = ''; hash.split(hashSymbol).length - 1 < hashLength; hash += hashSymbol);
- instance.view.wt.wtDom.fastInnerHTML(TD, hash);
- };
- })(Handsontable);
- (function (Handsontable) {
- 'use strict';
- Handsontable.EditorState = {
- VIRGIN: 'STATE_VIRGIN', //before editing
- EDITING: 'STATE_EDITING',
- WAITING: 'STATE_WAITING', //waiting for async validation
- FINISHED: 'STATE_FINISHED'
- };
- function BaseEditor(instance) {
- this.instance = instance;
- this.state = Handsontable.EditorState.VIRGIN;
- this._opened = false;
- this._closeCallback = function () {
- };
- this.init();
- }
- BaseEditor.prototype._fireCallbacks = function(result) {
- this._closeCallback(result);
- }
- BaseEditor.prototype.init = function(){};
- BaseEditor.prototype.getValue = function(){
- throw Error('Editor getValue() method unimplemented');
- };
- BaseEditor.prototype.setValue = function(newValue){
- throw Error('Editor setValue() method unimplemented');
- };
- BaseEditor.prototype.open = function(){
- throw Error('Editor open() method unimplemented');
- };
- BaseEditor.prototype.close = function(){
- throw Error('Editor close() method unimplemented');
- };
- BaseEditor.prototype.prepare = function(row, col, prop, td, originalValue, cellProperties){
- this.TD = td;
- this.row = row;
- this.col = col;
- this.prop = prop;
- this.originalValue = originalValue;
- this.cellProperties = cellProperties;
- this.state = Handsontable.EditorState.VIRGIN;
- };
- BaseEditor.prototype.extend = function(){
- var baseClass = this.constructor;
- function Editor(){
- baseClass.apply(this, arguments);
- }
- function inherit(Child, Parent){
- function Bridge() {
- }
- Bridge.prototype = Parent.prototype;
- Child.prototype = new Bridge();
- Child.prototype.constructor = Child;
- return Child;
- }
- return inherit(Editor, baseClass);
- };
- BaseEditor.prototype.saveValue = function (val, ctrlDown) {
- if (ctrlDown) { //if ctrl+enter and multiple cells selected, behave like Excel (finish editing and apply to all cells)
- var sel = this.instance.getSelected();
- this.instance.populateFromArray(sel[0], sel[1], val, sel[2], sel[3], 'edit');
- }
- else {
- this.instance.populateFromArray(this.row, this.col, val, null, null, 'edit');
- }
- };
- BaseEditor.prototype.beginEditing = function(initialValue){
- if (this.state != Handsontable.EditorState.VIRGIN) {
- return;
- }
- if (this.cellProperties.readOnly) {
- return;
- }
- this.instance.view.scrollViewport({row: this.row, col: this.col});
- this.instance.view.render();
- this.state = Handsontable.EditorState.EDITING;
- initialValue = typeof initialValue == 'string' ? initialValue : this.originalValue;
- this.setValue(Handsontable.helper.stringify(initialValue));
- this.open();
- this._opened = true;
- this.instance.view.render(); //only rerender the selections (FillHandle should disappear when beginediting is triggered)
- };
- BaseEditor.prototype.finishEditing = function (restoreOriginalValue, ctrlDown, callback) {
- if (callback) {
- var old = this._closeCallback;
- this._closeCallback = function (result) {
- old(result);
- callback(result);
- };
- }
- if (this.isWaiting()) {
- return;
- }
- this._closeCallback = function () {
- };
- if (this.state == Handsontable.EditorState.VIRGIN) {
- var that = this;
- setTimeout(function () {
- that._fireCallbacks(true);
- });
- return;
- }
- if (this.state == Handsontable.EditorState.EDITING) {
- var val;
- if (restoreOriginalValue) {
- val = [
- [this.originalValue]
- ];
- } else {
- val = [
- [String.prototype.trim.call(this.getValue())] //String.prototype.trim is defined in Walkontable polyfill.js
- ];
- }
- this.state = Handsontable.EditorState.WAITING;
- this.saveValue(val, ctrlDown);
- if(this.instance.getCellValidator(this.cellProperties)){
- var that = this;
- this.instance.addHookOnce('afterValidate', function (result) {
- that.state = Handsontable.EditorState.FINISHED;
- that.discardEditor(result);
- });
- } else {
- this.state = Handsontable.EditorState.FINISHED;
- this.discardEditor(true);
- }
- }
- };
- BaseEditor.prototype.discardEditor = function (result) {
- if (this.state !== Handsontable.EditorState.FINISHED) {
- return;
- }
- if (result === false && this.cellProperties.allowInvalid !== true) { //validator was defined and failed
- this.instance.selectCell(this.row, this.col);
- this.focus();
- this.state = Handsontable.EditorState.EDITING;
- this._fireCallbacks(false);
- }
- else {
- this.close();
- this._opened = false;
- this.state = Handsontable.EditorState.VIRGIN;
- this._fireCallbacks(true);
- }
- };
- BaseEditor.prototype.isOpened = function(){
- return this._opened;
- };
- BaseEditor.prototype.isWaiting = function () {
- return this.state === Handsontable.EditorState.WAITING;
- };
- Handsontable.editors.BaseEditor = BaseEditor;
- })(Handsontable);
- (function(Handsontable){
- var TextEditor = Handsontable.editors.BaseEditor.prototype.extend();
- TextEditor.prototype.init = function(){
- this.createElements();
- this.bindEvents();
- };
- TextEditor.prototype.getValue = function(){
- return this.TEXTAREA.value
- };
- TextEditor.prototype.setValue = function(newValue){
- this.TEXTAREA.value = newValue;
- };
- var onBeforeKeyDown = function onBeforeKeyDown(event){
- var instance = this;
- var that = instance.getActiveEditor();
- var keyCodes = Handsontable.helper.keyCode;
- var ctrlDown = (event.ctrlKey || event.metaKey) && !event.altKey; //catch CTRL but not right ALT (which in some systems triggers ALT+CTRL)
- //Process only events that have been fired in the editor
- if (event.target !== that.TEXTAREA || event.isImmediatePropagationStopped()){
- return;
- }
- if (event.keyCode === 17 || event.keyCode === 224 || event.keyCode === 91 || event.keyCode === 93) {
- //when CTRL or its equivalent is pressed and cell is edited, don't prepare selectable text in textarea
- event.stopImmediatePropagation();
- return;
- }
- switch (event.keyCode) {
- case keyCodes.ARROW_RIGHT:
- if (that.wtDom.getCaretPosition(that.TEXTAREA) !== that.TEXTAREA.value.length) {
- event.stopImmediatePropagation();
- }
- break;
- case keyCodes.ARROW_LEFT: /* arrow left */
- if (that.wtDom.getCaretPosition(that.TEXTAREA) !== 0) {
- event.stopImmediatePropagation();
- }
- break;
- case keyCodes.ENTER:
- var selected = that.instance.getSelected();
- var isMultipleSelection = !(selected[0] === selected[2] && selected[1] === selected[3]);
- if ((ctrlDown && !isMultipleSelection) || event.altKey) { //if ctrl+enter or alt+enter, add new line
- if(that.isOpened()){
- that.setValue(that.getValue() + '\n');
- that.focus();
- } else {
- that.beginEditing(that.originalValue + '\n')
- }
- event.stopImmediatePropagation();
- }
- event.preventDefault(); //don't add newline to field
- break;
- case keyCodes.A:
- case keyCodes.X:
- case keyCodes.C:
- case keyCodes.V:
- if(ctrlDown){
- event.stopImmediatePropagation(); //CTRL+A, CTRL+C, CTRL+V, CTRL+X should only work locally when cell is edited (not in table context)
- break;
- }
- case keyCodes.BACKSPACE:
- case keyCodes.DELETE:
- case keyCodes.HOME:
- case keyCodes.END:
- event.stopImmediatePropagation(); //backspace, delete, home, end should only work locally when cell is edited (not in table context)
- break;
- }
- };
- TextEditor.prototype.open = function(){
- this.refreshDimensions(); //need it instantly, to prevent https://github.com/warpech/jquery-handsontable/issues/348
- this.TEXTAREA.focus();
- this.wtDom.setCaretPosition(this.TEXTAREA, this.TEXTAREA.value.length);
- this.instance.addHook('beforeKeyDown', onBeforeKeyDown);
- };
- TextEditor.prototype.close = function(){
- this.textareaParentStyle.display = 'none';
- if (document.activeElement === this.TEXTAREA) {
- this.instance.listen(); //don't refocus the table if user focused some cell outside of HT on purpose
- }
- this.instance.removeHook('beforeKeyDown', onBeforeKeyDown);
- };
- TextEditor.prototype.focus = function(){
- this.TEXTAREA.focus();
- };
- TextEditor.prototype.createElements = function () {
- this.$body = $(document.body);
- this.wtDom = new WalkontableDom();
- this.TEXTAREA = document.createElement('TEXTAREA');
- this.$textarea = $(this.TEXTAREA);
- this.wtDom.addClass(this.TEXTAREA, 'handsontableInput');
- this.textareaStyle = this.TEXTAREA.style;
- this.textareaStyle.width = 0;
- this.textareaStyle.height = 0;
- this.TEXTAREA_PARENT = document.createElement('DIV');
- this.wtDom.addClass(this.TEXTAREA_PARENT, 'handsontableInputHolder');
- this.textareaParentStyle = this.TEXTAREA_PARENT.style;
- this.textareaParentStyle.top = 0;
- this.textareaParentStyle.left = 0;
- this.textareaParentStyle.display = 'none';
- this.TEXTAREA_PARENT.appendChild(this.TEXTAREA);
- this.instance.rootElement[0].appendChild(this.TEXTAREA_PARENT);
- var that = this;
- Handsontable.PluginHooks.add('afterRender', function () {
- that.instance.registerTimeout('refresh_editor_dimensions', function () {
- that.refreshDimensions();
- }, 0);
- });
- };
- TextEditor.prototype.refreshDimensions = function () {
- if (this.state !== Handsontable.EditorState.EDITING) {
- return;
- }
- ///start prepare textarea position
- this.TD = this.instance.getCell(this.row, this.col);
- if (!this.TD) {
- //TD is outside of the viewport. Otherwise throws exception when scrolling the table while a cell is edited
- return;
- }
- var $td = $(this.TD); //because old td may have been scrolled out with scrollViewport
- var currentOffset = this.wtDom.offset(this.TD);
- var containerOffset = this.wtDom.offset(this.instance.rootElement[0]);
- var scrollTop = this.instance.rootElement.scrollTop();
- var scrollLeft = this.instance.rootElement.scrollLeft();
- var editTop = currentOffset.top - containerOffset.top + scrollTop - 1;
- var editLeft = currentOffset.left - containerOffset.left + scrollLeft - 1;
- var settings = this.instance.getSettings();
- var rowHeadersCount = settings.rowHeaders === false ? 0 : 1;
- var colHeadersCount = settings.colHeaders === false ? 0 : 1;
- if (editTop < 0) {
- editTop = 0;
- }
- if (editLeft < 0) {
- editLeft = 0;
- }
- if (rowHeadersCount > 0 && parseInt($td.css('border-top-width'), 10) > 0) {
- editTop += 1;
- }
- if (colHeadersCount > 0 && parseInt($td.css('border-left-width'), 10) > 0) {
- editLeft += 1;
- }
- this.textareaParentStyle.top = editTop + 'px';
- this.textareaParentStyle.left = editLeft + 'px';
- ///end prepare textarea position
- var width = $td.width()
- , maxWidth = this.instance.view.maximumVisibleElementWidth(editLeft) - 10 //10 is TEXTAREAs border and padding
- , height = $td.outerHeight() - 4
- , maxHeight = this.instance.view.maximumVisibleElementHeight(editTop) - 5; //10 is TEXTAREAs border and padding
- if (parseInt($td.css('border-top-width'), 10) > 0) {
- height -= 1;
- }
- if (parseInt($td.css('border-left-width'), 10) > 0) {
- if (rowHeadersCount > 0) {
- width -= 1;
- }
- }
- //in future may change to pure JS http://stackoverflow.com/questions/454202/creating-a-textarea-with-auto-resize
- this.$textarea.autoResize({
- minHeight: Math.min(height, maxHeight),
- maxHeight: maxHeight, //TEXTAREA should never be wider than visible part of the viewport (should not cover the scrollbar)
- minWidth: Math.min(width, maxWidth),
- maxWidth: maxWidth, //TEXTAREA should never be wider than visible part of the viewport (should not cover the scrollbar)
- animate: false,
- extraSpace: 0
- });
- this.textareaParentStyle.display = 'block';
- };
- TextEditor.prototype.bindEvents = function () {
- this.$textarea.on('cut.editor', function (event) {
- event.stopPropagation();
- });
- this.$textarea.on('paste.editor', function (event) {
- event.stopPropagation();
- });
- };
- Handsontable.editors.TextEditor = TextEditor;
- Handsontable.editors.registerEditor('text', Handsontable.editors.TextEditor);
- })(Handsontable);
- (function(Handsontable){
- //Blank editor, because all the work is done by renderer
- var CheckboxEditor = Handsontable.editors.BaseEditor.prototype.extend();
- CheckboxEditor.prototype.beginEditing = function () {
- this.saveValue([
- [!this.originalValue]
- ]);
- };
- CheckboxEditor.prototype.finishEditing = function () {};
- CheckboxEditor.prototype.init = function () {};
- CheckboxEditor.prototype.open = function () {};
- CheckboxEditor.prototype.close = function () {};
- CheckboxEditor.prototype.getValue = function () {};
- CheckboxEditor.prototype.setValue = function () {};
- CheckboxEditor.prototype.focus = function () {};
- Handsontable.editors.CheckboxEditor = CheckboxEditor;
- Handsontable.editors.registerEditor('checkbox', CheckboxEditor);
- })(Handsontable);
- (function (Handsontable) {
- var DateEditor = Handsontable.editors.TextEditor.prototype.extend();
- DateEditor.prototype.init = function () {
- if (!$.datepicker) {
- throw new Error("jQuery UI Datepicker dependency not found. Did you forget to include jquery-ui.custom.js or its substitute?");
- }
- Handsontable.editors.TextEditor.prototype.init.apply(this, arguments);
- this.isCellEdited = false;
- var that = this;
- this.instance.addHook('afterDestroy', function () {
- that.destroyElements();
- })
- };
- DateEditor.prototype.createElements = function () {
- Handsontable.editors.TextEditor.prototype.createElements.apply(this, arguments);
- this.datePicker = document.createElement('DIV');
- this.instance.view.wt.wtDom.addClass(this.datePicker, 'htDatepickerHolder');
- this.datePickerStyle = this.datePicker.style;
- this.datePickerStyle.position = 'absolute';
- this.datePickerStyle.top = 0;
- this.datePickerStyle.left = 0;
- this.datePickerStyle.zIndex = 99;
- document.body.appendChild(this.datePicker);
- this.$datePicker = $(this.datePicker);
- var that = this;
- var defaultOptions = {
- dateFormat: "yy-mm-dd",
- showButtonPanel: true,
- changeMonth: true,
- changeYear: true,
- altField: this.$textarea,
- onSelect: function () {
- that.finishEditing(false);
- }
- };
- this.$datePicker.datepicker(defaultOptions);
- /**
- * Prevent recognizing clicking on jQuery Datepicker as clicking outside of table
- */
- this.$datePicker.on('mousedown', function (event) {
- event.stopPropagation();
- });
- this.hideDatepicker();
- };
- DateEditor.prototype.destroyElements = function () {
- this.$datePicker.datepicker('destroy');
- this.$datePicker.remove();
- };
- DateEditor.prototype.beginEditing = function (row, col, prop, useOriginalValue, suffix) {
- Handsontable.editors.TextEditor.prototype.beginEditing.apply(this, arguments);
- this.showDatepicker();
- };
- DateEditor.prototype.finishEditing = function (isCancelled, ctrlDown) {
- this.hideDatepicker();
- Handsontable.editors.TextEditor.prototype.finishEditing.apply(this, arguments);
- };
- DateEditor.prototype.showDatepicker = function () {
- var $td = $(this.TD);
- var offset = $td.offset();
- this.datePickerStyle.top = (offset.top + $td.height()) + 'px';
- this.datePickerStyle.left = offset.left + 'px';
- var dateOptions = {
- defaultDate: this.originalValue || void 0
- };
- $.extend(dateOptions, this.cellProperties);
- this.$datePicker.datepicker("option", dateOptions);
- if (this.originalValue) {
- this.$datePicker.datepicker("setDate", this.originalValue);
- }
- this.datePickerStyle.display = 'block';
- };
- DateEditor.prototype.hideDatepicker = function () {
- this.datePickerStyle.display = 'none';
- };
- Handsontable.editors.DateEditor = DateEditor;
- Handsontable.editors.registerEditor('date', DateEditor);
- })(Handsontable);
- /**
- * This is inception. Using Handsontable as Handsontable editor
- */
- (function (Handsontable) {
- "use strict";
- var HandsontableEditor = Handsontable.editors.TextEditor.prototype.extend();
- HandsontableEditor.prototype.createElements = function () {
- Handsontable.editors.TextEditor.prototype.createElements.apply(this, arguments);
- var DIV = document.createElement('DIV');
- DIV.className = 'handsontableEditor';
- this.TEXTAREA_PARENT.appendChild(DIV);
- this.$htContainer = $(DIV);
- this.$htContainer.handsontable();
- };
- HandsontableEditor.prototype.prepare = function (td, row, col, prop, value, cellProperties) {
- Handsontable.editors.TextEditor.prototype.prepare.apply(this, arguments);
- var parent = this;
- var options = {
- startRows: 0,
- startCols: 0,
- minRows: 0,
- minCols: 0,
- className: 'listbox',
- cells: function () {
- return {
- readOnly: true
- }
- },
- fillHandle: false,
- afterOnCellMouseDown: function () {
- parent.setValue(this.getValue());
- parent.instance.destroyEditor();
- },
- beforeOnKeyDown: function (event) {
- var instance = this;
- switch (event.keyCode) {
- case Handsontable.helper.keyCode.ESCAPE:
- parent.instance.destroyEditor(true);
- event.stopImmediatePropagation();
- event.preventDefault();
- break;
- case Handsontable.helper.keyCode.ENTER: //enter
- var sel = instance.getSelected();
- parent.setValue(this.getDataAtCell(sel[0], sel[1]));
- parent.instance.destroyEditor();
- break;
- case Handsontable.helper.keyCode.ARROW_UP:
- if (instance.getSelected() && instance.getSelected()[0] == 0 && !parent.cellProperties.strict){
- instance.deselectCell();
- parent.instance.listen();
- parent.focus();
- event.preventDefault();
- event.stopImmediatePropagation();
- }
- break;
- }
- }
- };
- if (this.cellProperties.handsontable) {
- options = $.extend(options, cellProperties.handsontable);
- }
- this.$htContainer.handsontable('destroy');
- this.$htContainer.handsontable(options);
- };
- var onBeforeKeyDown = function (event) {
- if (event.isImmediatePropagationStopped()) {
- return;
- }
- var editor = this.getActiveEditor();
- var innerHOT = editor.$htContainer.handsontable('getInstance');
- if (event.keyCode == Handsontable.helper.keyCode.ARROW_DOWN) {
- if (!innerHOT.getSelected()){
- innerHOT.selectCell(0, 0);
- } else {
- var selectedRow = innerHOT.getSelected()[0];
- var rowToSelect = selectedRow < innerHOT.countRows() - 1 ? selectedRow + 1 : selectedRow;
- innerHOT.selectCell(rowToSelect, 0);
- }
- event.preventDefault();
- event.stopImmediatePropagation();
- }
- };
- HandsontableEditor.prototype.open = function () {
- this.instance.addHook('beforeKeyDown', onBeforeKeyDown);
- Handsontable.editors.TextEditor.prototype.open.apply(this, arguments);
- this.$htContainer.handsontable('render');
- if (this.cellProperties.strict) {
- this.$htContainer.handsontable('selectCell', 0, 0);
- this.$textarea[0].style.visibility = 'hidden';
- } else {
- this.$htContainer.handsontable('deselectCell');
- this.$textarea[0].style.visibility = 'visible';
- }
- this.wtDom.setCaretPosition(this.$textarea[0], 0, this.$textarea[0].value.length);
- };
- HandsontableEditor.prototype.close = function () {
- this.instance.removeHook('beforeKeyDown', onBeforeKeyDown);
- this.instance.listen();
- Handsontable.editors.TextEditor.prototype.close.apply(this, arguments);
- };
- HandsontableEditor.prototype.focus = function () {
- this.instance.listen();
- Handsontable.editors.TextEditor.prototype.focus.apply(this, arguments);
- };
- HandsontableEditor.prototype.beginEditing = function (initialValue) {
- var onBeginEditing = this.instance.getSettings().onBeginEditing;
- if (onBeginEditing && onBeginEditing() === false) {
- return;
- }
- Handsontable.editors.TextEditor.prototype.beginEditing.apply(this, arguments);
- };
- HandsontableEditor.prototype.finishEditing = function (isCancelled, ctrlDown) {
- if (this.$htContainer.handsontable('isListening')) { //if focus is still in the HOT editor
- this.instance.listen(); //return the focus to the parent HOT instance
- }
- if (this.$htContainer.handsontable('getSelected')) {
- this.setValue(this.$htContainer.handsontable('getInstance').getValue());
- }
- return Handsontable.editors.TextEditor.prototype.finishEditing.apply(this, arguments);
- };
- Handsontable.editors.HandsontableEditor = HandsontableEditor;
- Handsontable.editors.registerEditor('handsontable', HandsontableEditor);
- })(Handsontable);
- (function (Handsontable) {
- var AutocompleteEditor = Handsontable.editors.HandsontableEditor.prototype.extend();
- AutocompleteEditor.prototype.init = function () {
- Handsontable.editors.HandsontableEditor.prototype.init.apply(this, arguments);
- this.query = null;
- }
- AutocompleteEditor.prototype.createElements = function(){
- Handsontable.editors.HandsontableEditor.prototype.createElements.apply(this, arguments);
- this.$htContainer.addClass('autocompleteEditor');
- };
- AutocompleteEditor.prototype.bindEvents = function(){
- var that = this;
- this.$textarea.on('keydown.autocompleteEditor', function(event){
- if(!Handsontable.helper.isMetaKey(event.keyCode) || [Handsontable.helper.keyCode.BACKSPACE, Handsontable.helper.keyCode.DELETE].indexOf(event.keyCode) != -1){
- setTimeout(function () {
- that.queryChoices(that.$textarea.val());
- });
- } else if (event.keyCode == Handsontable.helper.keyCode.ENTER && that.cellProperties.strict !== true){
- that.$htContainer.handsontable('deselectCell');
- }
- });
- this.$htContainer.on('mouseenter', function () {
- that.$htContainer.handsontable('deselectCell');
- });
- this.$htContainer.on('mouseleave', function () {
- that.queryChoices(that.query);
- });
- Handsontable.editors.HandsontableEditor.prototype.bindEvents.apply(this, arguments);
- };
- AutocompleteEditor.prototype.beginEditing = function () {
- Handsontable.editors.HandsontableEditor.prototype.beginEditing.apply(this, arguments);
- var that = this;
- setTimeout(function () {
- that.queryChoices(that.TEXTAREA.value);
- });
- var hot = this.$htContainer.handsontable('getInstance');
- hot.updateSettings({
- 'colWidths': [this.wtDom.outerWidth(this.TEXTAREA) - 2],
- renderer: function (instance, TD, row, col, prop, value, cellProperties) {
- Handsontable.TextRenderer.apply(this, arguments);
- var match = TD.innerHTML.match(new RegExp(that.query, 'i'));
- if(match){
- TD.innerHTML = value.replace(match[0], '<strong>' + match[0] + '</strong>');
- }
- }
- });
- };
- var onBeforeKeyDownInner;
- AutocompleteEditor.prototype.open = function () {
- var parent = this;
- onBeforeKeyDownInner = function (event) {
- var instance = this;
- if (event.keyCode == Handsontable.helper.keyCode.ARROW_UP){
- if (instance.getSelected() && instance.getSelected()[0] == 0){
- if(!parent.cellProperties.strict){
- instance.deselectCell();
- }
- parent.instance.listen();
- parent.focus();
- event.preventDefault();
- event.stopImmediatePropagation();
- }
- }
- };
- this.$htContainer.handsontable('getInstance').addHook('beforeKeyDown', onBeforeKeyDownInner);
- Handsontable.editors.HandsontableEditor.prototype.open.apply(this, arguments);
- this.$textarea[0].style.visibility = 'visible';
- parent.focus();
- };
- AutocompleteEditor.prototype.close = function () {
- this.$htContainer.handsontable('getInstance').removeHook('beforeKeyDown', onBeforeKeyDownInner);
- Handsontable.editors.HandsontableEditor.prototype.close.apply(this, arguments);
- };
- AutocompleteEditor.prototype.queryChoices = function(query){
- this.query = query;
- if (typeof this.cellProperties.source == 'function'){
- var that = this;
- this.cellProperties.source(query, function(choices){
- that.updateChoicesList(choices)
- });
- } else if (Handsontable.helper.isArray(this.cellProperties.source)) {
- var choices;
- if(!query || this.cellProperties.filter === false){
- choices = this.cellProperties.source;
- } else {
- choices = this.cellProperties.source.filter(function(choice){
- return choice.indexOf(query) != -1
- });
- }
- this.updateChoicesList(choices)
- } else {
- this.updateChoicesList([]);
- }
- };
- function findItemIndexToHighlight(items, value){
- var bestMatch = {};
- var valueLength = value.length;
- var currentItem;
- var indexOfValue;
- var charsLeft;
- for(var i = 0, len = items.length; i < len; i++){
- currentItem = items[i];
- if(valueLength > 0){
- indexOfValue = currentItem.indexOf(value)
- } else {
- indexOfValue = currentItem === value ? 0 : -1;
- }
- if(indexOfValue == -1) continue;
- charsLeft = currentItem.length - indexOfValue - valueLength;
- if( typeof bestMatch.indexOfValue == 'undefined'
- || bestMatch.indexOfValue > indexOfValue
- || ( bestMatch.indexOfValue == indexOfValue && bestMatch.charsLeft > charsLeft ) ){
- bestMatch.indexOfValue = indexOfValue;
- bestMatch.charsLeft = charsLeft;
- bestMatch.index = i;
- }
- }
- return bestMatch.index;
- }
- AutocompleteEditor.prototype.updateChoicesList = function (choices) {
- this.$htContainer.handsontable('loadData', Handsontable.helper.pivot([choices]));
- var value = this.getValue();
- var rowToHighlight;
- if(this.cellProperties.strict === true){
- rowToHighlight = findItemIndexToHighlight(choices, value);
- if ( typeof rowToHighlight == 'undefined'){
- rowToHighlight = 0;
- }
- }
- if(typeof rowToHighlight == 'undefined'){
- this.$htContainer.handsontable('deselectCell');
- } else {
- this.$htContainer.handsontable('selectCell', rowToHighlight, 0);
- }
- this.focus();
- };
- Handsontable.editors.AutocompleteEditor = AutocompleteEditor;
- Handsontable.editors.registerEditor('autocomplete', AutocompleteEditor);
- })(Handsontable);
- (function(Handsontable){
- var PasswordEditor = Handsontable.editors.TextEditor.prototype.extend();
- var wtDom = new WalkontableDom();
- PasswordEditor.prototype.createElements = function () {
- Handsontable.editors.TextEditor.prototype.createElements.apply(this, arguments);
- this.TEXTAREA = document.createElement('input');
- this.TEXTAREA.setAttribute('type', 'password');
- this.TEXTAREA.className = 'handsontableInput';
- this.textareaStyle = this.TEXTAREA.style;
- this.textareaStyle.width = 0;
- this.textareaStyle.height = 0;
- this.$textarea = $(this.TEXTAREA);
- wtDom.empty(this.TEXTAREA_PARENT);
- this.TEXTAREA_PARENT.appendChild(this.TEXTAREA);
- };
- Handsontable.editors.PasswordEditor = PasswordEditor;
- Handsontable.editors.registerEditor('password', PasswordEditor);
- })(Handsontable);
- (function (Handsontable) {
- var SelectEditor = Handsontable.editors.BaseEditor.prototype.extend();
- SelectEditor.prototype.init = function(){
- this.select = $('<select />')
- .addClass('htSelectEditor')
- .hide();
- this.instance.rootElement.append(this.select);
- };
- SelectEditor.prototype.prepare = function(){
- Handsontable.editors.BaseEditor.prototype.prepare.apply(this, arguments);
- var selectOptions = this.cellProperties.selectOptions;
- var options;
- if (typeof selectOptions == 'function'){
- options = this.prepareOptions(selectOptions(this.row, this.col, this.prop))
- } else {
- options = this.prepareOptions(selectOptions);
- }
- var optionElements = [];
- for (var option in options){
- if (options.hasOwnProperty(option)){
- var optionElement = $('<option />');
- optionElement.val(option);
- optionElement.html(options[option]);
- optionElements.push(optionElement);
- }
- }
- this.select.empty();
- this.select.append(optionElements);
- };
- SelectEditor.prototype.prepareOptions = function(optionsToPrepare){
- var preparedOptions = {};
- if (Handsontable.helper.isArray(optionsToPrepare)){
- for(var i = 0, len = optionsToPrepare.length; i < len; i++){
- preparedOptions[optionsToPrepare[i]] = optionsToPrepare[i];
- }
- }
- else if (typeof optionsToPrepare == 'object') {
- preparedOptions = optionsToPrepare;
- }
- return preparedOptions;
- };
- SelectEditor.prototype.getValue = function () {
- return this.select.val();
- };
- SelectEditor.prototype.setValue = function (value) {
- this.select.val(value);
- };
- var onBeforeKeyDown = function (event) {
- var instance = this;
- var editor = instance.getActiveEditor();
- switch (event.keyCode){
- case Handsontable.helper.keyCode.ARROW_UP:
- var previousOption = editor.select.find('option:selected').prev();
- if (previousOption.length == 1){
- previousOption.prop('selected', true);
- }
- event.stopImmediatePropagation();
- event.preventDefault();
- break;
- case Handsontable.helper.keyCode.ARROW_DOWN:
- var nextOption = editor.select.find('option:selected').next();
- if (nextOption.length == 1){
- nextOption.prop('selected', true);
- }
- event.stopImmediatePropagation();
- event.preventDefault();
- break;
- }
- };
- SelectEditor.prototype.open = function () {
- this.select.css({
- height: $(this.TD).height(),
- 'min-width' : $(this.TD).outerWidth()
- });
- this.select.show();
- this.select.offset($(this.TD).offset());
- this.instance.addHook('beforeKeyDown', onBeforeKeyDown);
- };
- SelectEditor.prototype.close = function () {
- this.select.hide();
- this.instance.removeHook('beforeKeyDown', onBeforeKeyDown);
- };
- Handsontable.editors.SelectEditor = SelectEditor;
- Handsontable.editors.registerEditor('select', SelectEditor);
- })(Handsontable);
- (function (Handsontable) {
- var DropdownEditor = Handsontable.editors.AutocompleteEditor.prototype.extend();
- DropdownEditor.prototype.prepare = function () {
- Handsontable.editors.AutocompleteEditor.prototype.prepare.apply(this, arguments);
- this.cellProperties.filter = false;
- this.cellProperties.strict = true;
- };
- Handsontable.editors.DropdownEditod = DropdownEditor;
- Handsontable.editors.registerEditor('dropdown', DropdownEditor);
- })(Handsontable);
- /**
- * Numeric cell validator
- * @param {*} value - Value of edited cell
- * @param {*} callback - Callback called with validation result
- */
- Handsontable.NumericValidator = function (value, callback) {
- if (value === null) {
- value = '';
- }
- callback(/^-?\d*\.?\d*$/.test(value));
- };
- /**
- * Function responsible for validation of autocomplete value
- * @param {*} value - Value of edited cell
- * @param {*} calback - Callback called with validation result
- */
- var process = function (value, callback) {
- var originalVal = value;
- var lowercaseVal = typeof originalVal === 'string' ? originalVal.toLowerCase() : null;
- return function (source) {
- var found = false;
- for (var s = 0, slen = source.length; s < slen; s++) {
- if (originalVal === source[s]) {
- found = true; //perfect match
- break;
- }
- else if (lowercaseVal === source[s].toLowerCase()) {
- // changes[i][3] = source[s]; //good match, fix the case << TODO?
- found = true;
- break;
- }
- }
- callback(found);
- }
- };
- /**
- * Autocomplete cell validator
- * @param {*} value - Value of edited cell
- * @param {*} calback - Callback called with validation result
- */
- Handsontable.AutocompleteValidator = function (value, callback) {
- if (this.strict && this.source) {
- typeof this.source === 'function' ? this.source(value, process(value, callback)) : process(value, callback)(this.source);
- } else {
- callback(true);
- }
- };
- /**
- * Cell type is just a shortcut for setting bunch of cellProperties (used in getCellMeta)
- */
- Handsontable.AutocompleteCell = {
- editor: 'autocomplete',
- renderer: Handsontable.AutocompleteRenderer,
- validator: Handsontable.AutocompleteValidator
- };
- Handsontable.CheckboxCell = {
- editor: 'checkbox',
- renderer: Handsontable.CheckboxRenderer
- };
- Handsontable.TextCell = {
- editor: 'text',
- renderer: Handsontable.TextRenderer
- };
- Handsontable.NumericCell = {
- editor: 'text',
- renderer: Handsontable.NumericRenderer,
- validator: Handsontable.NumericValidator,
- dataType: 'number'
- };
- Handsontable.DateCell = {
- editor: 'date',
- renderer: Handsontable.AutocompleteRenderer //displays small gray arrow on right side of the cell
- };
- Handsontable.HandsontableCell = {
- editor: 'handsontable',
- renderer: Handsontable.AutocompleteRenderer //displays small gray arrow on right side of the cell
- };
- Handsontable.PasswordCell = {
- editor: 'password',
- renderer: Handsontable.PasswordRenderer
- };
- Handsontable.DropdownCell = {
- editor: 'dropdown',
- renderer: Handsontable.AutocompleteRenderer,
- validator: Handsontable.AutocompleteValidator
- };
- //here setup the friendly aliases that are used by cellProperties.type
- Handsontable.cellTypes = {
- text: Handsontable.TextCell,
- date: Handsontable.DateCell,
- numeric: Handsontable.NumericCell,
- checkbox: Handsontable.CheckboxCell,
- autocomplete: Handsontable.AutocompleteCell,
- handsontable: Handsontable.HandsontableCell,
- password: Handsontable.PasswordCell,
- dropdown: Handsontable.DropdownCell
- };
- //here setup the friendly aliases that are used by cellProperties.renderer and cellProperties.editor
- Handsontable.cellLookup = {
- renderer: {
- text: Handsontable.TextRenderer,
- numeric: Handsontable.NumericRenderer,
- checkbox: Handsontable.CheckboxRenderer,
- autocomplete: Handsontable.AutocompleteRenderer,
- password: Handsontable.PasswordRenderer
- },
- validator: {
- numeric: Handsontable.NumericValidator,
- autocomplete: Handsontable.AutocompleteValidator
- }
- };
- Handsontable.PluginHookClass = (function () {
- var Hooks = function () {
- return {
- // Hooks
- beforeInitWalkontable: [],
- beforeInit: [],
- beforeRender: [],
- beforeChange: [],
- beforeRemoveCol: [],
- beforeRemoveRow: [],
- beforeValidate: [],
- beforeGet: [],
- beforeSet: [],
- beforeGetCellMeta: [],
- beforeAutofill: [],
- beforeKeyDown: [],
- beforeColumnSort: [],
- afterInit : [],
- afterLoadData : [],
- afterUpdateSettings: [],
- afterRender : [],
- afterRenderer : [],
- afterChange : [],
- afterValidate: [],
- afterGetCellMeta: [],
- afterGetColHeader: [],
- afterGetColWidth: [],
- afterDestroy: [],
- afterRemoveRow: [],
- afterCreateRow: [],
- afterRemoveCol: [],
- afterCreateCol: [],
- afterColumnResize: [],
- afterColumnMove: [],
- afterColumnSort: [],
- afterDeselect: [],
- afterSelection: [],
- afterSelectionByProp: [],
- afterSelectionEnd: [],
- afterSelectionEndByProp: [],
- afterCopyLimit: [],
- afterOnCellMouseDown: [],
- afterOnCellCornerMouseDown: [],
- // Modifiers
- modifyCol: []
- }
- };
- var legacy = {
- onBeforeChange: "beforeChange",
- onChange: "afterChange",
- onCreateRow: "afterCreateRow",
- onCreateCol: "afterCreateCol",
- onSelection: "afterSelection",
- onCopyLimit: "afterCopyLimit",
- onSelectionEnd: "afterSelectionEnd",
- onSelectionByProp: "afterSelectionByProp",
- onSelectionEndByProp: "afterSelectionEndByProp"
- };
- function PluginHookClass() {
- this.hooks = Hooks();
- this.legacy = legacy;
- }
- PluginHookClass.prototype.add = function (key, fn) {
- // provide support for old versions of HOT
- if (key in legacy) {
- key = legacy[key];
- }
- if (typeof this.hooks[key] === "undefined") {
- this.hooks[key] = [];
- }
- if (Handsontable.helper.isArray(fn)) {
- for (var i = 0, len = fn.length; i < len; i++) {
- this.hooks[key].push(fn[i]);
- }
- } else {
- this.hooks[key].push(fn);
- }
- return this;
- };
- PluginHookClass.prototype.once = function(key, fn){
- if(Handsontable.helper.isArray(fn)){
- for(var i = 0, len = fn.length; i < len; i++){
- fn[i].runOnce = true;
- this.add(key, fn[i]);
- }
- } else {
- fn.runOnce = true;
- this.add(key, fn);
- }
- };
- PluginHookClass.prototype.remove = function (key, fn) {
- var status = false;
- // provide support for old versions of HOT
- if (key in legacy) {
- key = legacy[key];
- }
- if (typeof this.hooks[key] !== 'undefined') {
- for (var i = 0, leni = this.hooks[key].length; i < leni; i++) {
- if (this.hooks[key][i] == fn) {
- delete this.hooks[key][i].runOnce;
- this.hooks[key].splice(i, 1);
- status = true;
- break;
- }
- }
- }
- return status;
- };
- PluginHookClass.prototype.run = function (instance, key, p1, p2, p3, p4, p5) {
- // provide support for old versions of HOT
- if (key in legacy) {
- key = legacy[key];
- }
- //performance considerations - http://jsperf.com/call-vs-apply-for-a-plugin-architecture
- if (typeof this.hooks[key] !== 'undefined') {
- //Make a copy of handler array
- var handlers = Array.prototype.slice.call(this.hooks[key]);
- for (var i = 0, leni = handlers.length; i < leni; i++) {
- handlers[i].call(instance, p1, p2, p3, p4, p5);
- if(handlers[i].runOnce){
- this.remove(key, handlers[i]);
- }
- }
- }
- };
- PluginHookClass.prototype.execute = function (instance, key, p1, p2, p3, p4, p5) {
- var res, handlers;
- // provide support for old versions of HOT
- if (key in legacy) {
- key = legacy[key];
- }
- //performance considerations - http://jsperf.com/call-vs-apply-for-a-plugin-architecture
- if (typeof this.hooks[key] !== 'undefined') {
- handlers = Array.prototype.slice.call(this.hooks[key]);
- for (var i = 0, leni = handlers.length; i < leni; i++) {
- res = handlers[i].call(instance, p1, p2, p3, p4, p5);
- if (res !== void 0) {
- p1 = res;
- }
- if(handlers[i].runOnce){
- this.remove(key, handlers[i]);
- }
- if(res === false){ //if any handler returned false
- return false; //event has been cancelled and further execution of handler queue is being aborted
- }
- }
- }
- return p1;
- };
- return PluginHookClass;
- })();
- Handsontable.PluginHooks = new Handsontable.PluginHookClass();
- (function (Handsontable) {
- function HandsontableAutoColumnSize() {
- var plugin = this
- , sampleCount = 5; //number of samples to take of each value length
- this.beforeInit = function () {
- var instance = this;
- instance.autoColumnWidths = [];
- if (instance.getSettings().autoColumnSize !== false) {
- if (!instance.autoColumnSizeTmp) {
- instance.autoColumnSizeTmp = {
- table: null,
- tableStyle: null,
- theadTh: null,
- tbody: null,
- container: null,
- containerStyle: null,
- determineBeforeNextRender: true
- };
- }
- instance.addHook('beforeRender', htAutoColumnSize.determineIfChanged);
- instance.addHook('afterGetColWidth', htAutoColumnSize.getColWidth);
- instance.addHook('afterDestroy', htAutoColumnSize.afterDestroy);
- instance.determineColumnWidth = plugin.determineColumnWidth;
- } else {
- instance.removeHook('beforeRender', htAutoColumnSize.determineIfChanged);
- instance.removeHook('afterGetColWidth', htAutoColumnSize.getColWidth);
- instance.removeHook('afterDestroy', htAutoColumnSize.afterDestroy);
- delete instance.determineColumnWidth;
- plugin.afterDestroy.call(instance);
- }
- };
- this.determineIfChanged = function (force) {
- if (force) {
- htAutoColumnSize.determineColumnsWidth.apply(this, arguments);
- }
- };
- this.determineColumnWidth = function (col) {
- var instance = this
- , tmp = instance.autoColumnSizeTmp;
- if (!tmp.container) {
- createTmpContainer.call(tmp, instance);
- }
- tmp.container.className = instance.rootElement[0].className + ' htAutoColumnSize';
- tmp.table.className = instance.$table[0].className;
- var rows = instance.countRows();
- var samples = {};
- var maxLen = 0;
- for (var r = 0; r < rows; r++) {
- var value = Handsontable.helper.stringify(instance.getDataAtCell(r, col));
- var len = value.length;
- if (len > maxLen) {
- maxLen = len;
- }
- if (!samples[len]) {
- samples[len] = {
- needed: sampleCount,
- strings: []
- };
- }
- if (samples[len].needed) {
- samples[len].strings.push({value: value, row: r});
- samples[len].needed--;
- }
- }
- var settings = instance.getSettings();
- if (settings.colHeaders) {
- instance.view.appendColHeader(col, tmp.theadTh); //TH innerHTML
- }
- instance.view.wt.wtDom.empty(tmp.tbody);
- var cellProperties = instance.getCellMeta(0, col);
- var renderer = instance.getCellRenderer(cellProperties);
- for (var i in samples) {
- if (samples.hasOwnProperty(i)) {
- for (var j = 0, jlen = samples[i].strings.length; j < jlen; j++) {
- var tr = document.createElement('tr');
- var td = document.createElement('td');
- renderer(instance, td, samples[i].strings[j].row, col, instance.colToProp(col), samples[i].strings[j].value, cellProperties);
- r++;
- tr.appendChild(td);
- tmp.tbody.appendChild(tr);
- }
- }
- }
- var parent = instance.rootElement[0].parentNode;
- parent.appendChild(tmp.container);
- var width = instance.view.wt.wtDom.outerWidth(tmp.table);
- parent.removeChild(tmp.container);
- var maxWidth = instance.view.wt.wtViewport.getViewportWidth() - 2; //2 is some overhead for cell border
- if (width > maxWidth) {
- width = maxWidth;
- }
- return width;
- };
- this.determineColumnsWidth = function () {
- var instance = this;
- var settings = this.getSettings();
- if (settings.autoColumnSize || !settings.colWidths) {
- var cols = this.countCols();
- for (var c = 0; c < cols; c++) {
- if (!instance._getColWidthFromSettings(c)) {
- this.autoColumnWidths[c] = plugin.determineColumnWidth.call(instance, c);
- }
- }
- }
- };
- this.getColWidth = function (col, response) {
- if (this.autoColumnWidths[col] && this.autoColumnWidths[col] > response.width) {
- response.width = this.autoColumnWidths[col];
- }
- };
- this.afterDestroy = function () {
- var instance = this;
- if (instance.autoColumnSizeTmp && instance.autoColumnSizeTmp.container && instance.autoColumnSizeTmp.container.parentNode) {
- instance.autoColumnSizeTmp.container.parentNode.removeChild(instance.autoColumnSizeTmp.container);
- }
- };
- function createTmpContainer(instance) {
- var d = document
- , tmp = this;
- tmp.table = d.createElement('table');
- tmp.theadTh = d.createElement('th');
- tmp.table.appendChild(d.createElement('thead')).appendChild(d.createElement('tr')).appendChild(tmp.theadTh);
- tmp.tableStyle = tmp.table.style;
- tmp.tableStyle.tableLayout = 'auto';
- tmp.tableStyle.width = 'auto';
- tmp.tbody = d.createElement('tbody');
- tmp.table.appendChild(tmp.tbody);
- tmp.container = d.createElement('div');
- tmp.container.className = instance.rootElement[0].className + ' hidden';
- tmp.containerStyle = tmp.container.style;
- tmp.container.appendChild(tmp.table);
- }
- }
- var htAutoColumnSize = new HandsontableAutoColumnSize();
- Handsontable.PluginHooks.add('beforeInit', htAutoColumnSize.beforeInit);
- Handsontable.PluginHooks.add('afterUpdateSettings', htAutoColumnSize.beforeInit);
- })(Handsontable);
- /**
- * This plugin sorts the view by a column (but does not sort the data source!)
- * @constructor
- */
- function HandsontableColumnSorting() {
- var plugin = this;
- this.init = function (source) {
- var instance = this;
- var sortingSettings = instance.getSettings().columnSorting;
- var sortingColumn, sortingOrder;
- instance.sortingEnabled = !!(sortingSettings);
- if (instance.sortingEnabled) {
- instance.sortIndex = [];
- var loadedSortingState = loadSortingState.call(instance);
- if (typeof loadedSortingState != 'undefined') {
- sortingColumn = loadedSortingState.sortColumn;
- sortingOrder = loadedSortingState.sortOrder;
- } else {
- sortingColumn = sortingSettings.column;
- sortingOrder = sortingSettings.sortOrder;
- }
- plugin.sortByColumn.call(instance, sortingColumn, sortingOrder);
- instance.sort = function(){
- var args = Array.prototype.slice.call(arguments);
- return plugin.sortByColumn.apply(instance, args)
- };
- if (typeof instance.getSettings().observeChanges == 'undefined'){
- enableObserveChangesPlugin.call(instance);
- }
- if (source == 'afterInit') {
- bindColumnSortingAfterClick.call(instance);
- instance.addHook('afterCreateRow', plugin.afterCreateRow);
- instance.addHook('afterRemoveRow', plugin.afterRemoveRow);
- instance.addHook('afterLoadData', plugin.init);
- }
- } else {
- delete instance.sort;
- instance.removeHook('afterCreateRow', plugin.afterCreateRow);
- instance.removeHook('afterRemoveRow', plugin.afterRemoveRow);
- instance.removeHook('afterLoadData', plugin.init);
- }
- };
- this.setSortingColumn = function (col, order) {
- var instance = this;
- if (typeof col == 'undefined') {
- delete instance.sortColumn;
- delete instance.sortOrder;
- return;
- } else if (instance.sortColumn === col && typeof order == 'undefined') {
- instance.sortOrder = !instance.sortOrder;
- } else {
- instance.sortOrder = typeof order != 'undefined' ? order : true;
- }
- instance.sortColumn = col;
- };
- this.sortByColumn = function (col, order) {
- var instance = this;
- plugin.setSortingColumn.call(instance, col, order);
- if(typeof instance.sortColumn == 'undefined'){
- return;
- }
- instance.PluginHooks.run('beforeColumnSort', instance.sortColumn, instance.sortOrder);
- plugin.sort.call(instance);
- instance.render();
- saveSortingState.call(instance);
- instance.PluginHooks.run('afterColumnSort', instance.sortColumn, instance.sortOrder);
- };
- var saveSortingState = function () {
- var instance = this;
- var sortingState = {};
- if (typeof instance.sortColumn != 'undefined') {
- sortingState.sortColumn = instance.sortColumn;
- }
- if (typeof instance.sortOrder != 'undefined') {
- sortingState.sortOrder = instance.sortOrder;
- }
- if (sortingState.hasOwnProperty('sortColumn') || sortingState.hasOwnProperty('sortOrder')) {
- instance.PluginHooks.run('persistentStateSave', 'columnSorting', sortingState);
- }
- };
- var loadSortingState = function () {
- var instance = this;
- var storedState = {};
- instance.PluginHooks.run('persistentStateLoad', 'columnSorting', storedState);
- return storedState.value;
- };
- var bindColumnSortingAfterClick = function () {
- var instance = this;
- instance.rootElement.on('click.handsontable', '.columnSorting', function (e) {
- if (instance.view.wt.wtDom.hasClass(e.target, 'columnSorting')) {
- var col = getColumn(e.target);
- plugin.sortByColumn.call(instance, col);
- }
- });
- function countRowHeaders() {
- var THs = instance.view.TBODY.querySelector('tr').querySelectorAll('th');
- return THs.length;
- }
- function getColumn(target) {
- var TH = instance.view.wt.wtDom.closest(target, 'TH');
- return instance.view.wt.wtDom.index(TH) - countRowHeaders();
- }
- };
- function enableObserveChangesPlugin () {
- var instance = this;
- instance.registerTimeout('enableObserveChanges', function(){
- instance.updateSettings({
- observeChanges: true
- });
- }, 0);
- }
- function defaultSort(sortOrder) {
- return function (a, b) {
- if (a[1] === b[1]) {
- return 0;
- }
- if (a[1] === null) {
- return 1;
- }
- if (b[1] === null) {
- return -1;
- }
- if (a[1] < b[1]) return sortOrder ? -1 : 1;
- if (a[1] > b[1]) return sortOrder ? 1 : -1;
- return 0;
- }
- }
- function dateSort(sortOrder) {
- return function (a, b) {
- if (a[1] === b[1]) {
- return 0;
- }
- if (a[1] === null) {
- return 1;
- }
- if (b[1] === null) {
- return -1;
- }
- var aDate = new Date(a[1]);
- var bDate = new Date(b[1]);
- if (aDate < bDate) return sortOrder ? -1 : 1;
- if (aDate > bDate) return sortOrder ? 1 : -1;
- return 0;
- }
- }
- this.sort = function () {
- var instance = this;
- if (typeof instance.sortOrder == 'undefined') {
- return;
- }
- instance.sortingEnabled = false; //this is required by translateRow plugin hook
- instance.sortIndex.length = 0;
- var colOffset = this.colOffset();
- for (var i = 0, ilen = this.countRows() - instance.getSettings()['minSpareRows']; i < ilen; i++) {
- this.sortIndex.push([i, instance.getDataAtCell(i, this.sortColumn + colOffset)]);
- }
- var colMeta = instance.getCellMeta(0, instance.sortColumn);
- var sortFunction;
- switch (colMeta.type) {
- case 'date':
- sortFunction = dateSort;
- break;
- default:
- sortFunction = defaultSort;
- }
- this.sortIndex.sort(sortFunction(instance.sortOrder));
- //Append spareRows
- for(var i = this.sortIndex.length; i < instance.countRows(); i++){
- this.sortIndex.push([i, instance.getDataAtCell(i, this.sortColumn + colOffset)]);
- }
- instance.sortingEnabled = true; //this is required by translateRow plugin hook
- };
- this.translateRow = function (row) {
- var instance = this;
- if (instance.sortingEnabled && instance.sortIndex && instance.sortIndex.length && instance.sortIndex[row]) {
- return instance.sortIndex[row][0];
- }
- return row;
- };
- this.onBeforeGetSet = function (getVars) {
- var instance = this;
- getVars.row = plugin.translateRow.call(instance, getVars.row);
- };
- this.untranslateRow = function (row) {
- var instance = this;
- if (instance.sortingEnabled && instance.sortIndex && instance.sortIndex.length) {
- for (var i = 0; i < instance.sortIndex.length; i++) {
- if (instance.sortIndex[i][0] == row) {
- return i;
- }
- }
- }
- };
- this.getColHeader = function (col, TH) {
- if (this.getSettings().columnSorting) {
- this.view.wt.wtDom.addClass(TH.querySelector('.colHeader'), 'columnSorting');
- }
- };
- function isSorted(instance){
- return typeof instance.sortColumn != 'undefined';
- }
- this.afterCreateRow = function(index, amount){
- var instance = this;
- if(!isSorted(instance)){
- return;
- }
- for(var i = 0; i < instance.sortIndex.length; i++){
- if (instance.sortIndex[i][0] >= index){
- instance.sortIndex[i][0] += amount;
- }
- }
- for(var i=0; i < amount; i++){
- instance.sortIndex.splice(index+i, 0, [index+i, instance.getData()[index+i][instance.sortColumn + instance.colOffset()]]);
- }
- saveSortingState.call(instance);
- };
- this.afterRemoveRow = function(index, amount){
- var instance = this;
- if(!isSorted(instance)){
- return;
- }
- var physicalRemovedIndex = plugin.untranslateRow.call(instance, index);
- instance.sortIndex.splice(index, amount);
- for(var i = 0; i < instance.sortIndex.length; i++){
- if (instance.sortIndex[i][0] > physicalRemovedIndex){
- instance.sortIndex[i][0] -= amount;
- }
- }
- saveSortingState.call(instance);
- };
- this.afterChangeSort = function (changes/*, source*/) {
- var instance = this;
- var sortColumnChanged = false;
- var selection = {};
- if (!changes) {
- return;
- }
- for (var i = 0; i < changes.length; i++) {
- if (changes[i][1] == instance.sortColumn) {
- sortColumnChanged = true;
- selection.row = plugin.translateRow.call(instance, changes[i][0]);
- selection.col = changes[i][1];
- break;
- }
- }
- if (sortColumnChanged) {
- setTimeout(function () {
- plugin.sort.call(instance);
- instance.render();
- instance.selectCell(plugin.untranslateRow.call(instance, selection.row), selection.col);
- }, 0);
- }
- };
- }
- var htSortColumn = new HandsontableColumnSorting();
- Handsontable.PluginHooks.add('afterInit', function () {
- htSortColumn.init.call(this, 'afterInit')
- });
- Handsontable.PluginHooks.add('afterUpdateSettings', function () {
- htSortColumn.init.call(this, 'afterUpdateSettings')
- });
- Handsontable.PluginHooks.add('beforeGet', htSortColumn.onBeforeGetSet);
- Handsontable.PluginHooks.add('beforeSet', htSortColumn.onBeforeGetSet);
- Handsontable.PluginHooks.add('afterGetColHeader', htSortColumn.getColHeader);
- (function (Handsontable) {
- 'use strict';
- function ContextMenu(instance, customOptions){
- this.instance = instance;
- var contextMenu = this;
- this.menu = createMenu();
- this.enabled = true;
- this.bindMouseEvents();
- this.instance.addHook('afterDestroy', function () {
- contextMenu.destroy();
- });
- this.defaultOptions = {
- items: {
- 'row_above': {
- name: 'Insert row above',
- callback: function(key, selection){
- this.alter("insert_row", selection.start.row());
- },
- disabled: function () {
- return this.countRows() >= this.getSettings().maxRows;
- }
- },
- 'row_below': {
- name: 'Insert row below',
- callback: function(key, selection){
- this.alter("insert_row", selection.end.row() + 1);
- },
- disabled: function () {
- return this.countRows() >= this.getSettings().maxRows;
- }
- },
- "hsep1": ContextMenu.SEPARATOR,
- 'col_left': {
- name: 'Insert column on the left',
- callback: function(key, selection){
- this.alter("insert_col", selection.start.col());
- },
- disabled: function () {
- return this.countCols() >= this.getSettings().maxCols;
- }
- },
- 'col_right': {
- name: 'Insert column on the right',
- callback: function(key, selection){
- this.alter("insert_col", selection.end.col() + 1);
- },
- disabled: function () {
- return this.countCols() >= this.getSettings().maxCols;
- }
- },
- "hsep2": ContextMenu.SEPARATOR,
- 'remove_row': {
- name: 'Remove row',
- callback: function(key, selection){
- var amount = selection.end.row() - selection.start.row() + 1;
- this.alter("remove_row", selection.start.row(), amount);
- }
- },
- 'remove_col': {
- name: 'Remove column',
- callback: function(key, selection){
- var amount = selection.end.col() - selection.start.col() + 1;
- this.alter("remove_col", selection.start.col(), amount);
- }
- },
- "hsep3": ContextMenu.SEPARATOR,
- 'undo': {
- name: 'Undo',
- callback: function(){
- this.undo();
- },
- disabled: function () {
- return this.undoRedo && !this.undoRedo.isUndoAvailable();
- }
- },
- 'redo': {
- name: 'Redo',
- callback: function(){
- this.redo();
- },
- disabled: function () {
- return this.undoRedo && !this.undoRedo.isRedoAvailable();
- }
- }
- }
- };
- this.options = {};
- Handsontable.helper.extend(this.options, this.defaultOptions);
- this.updateOptions(customOptions);
- function createMenu(){
- var menu = document.createElement('DIV');
- Handsontable.Dom.addClass(menu, 'htContextMenu');
- instance.rootElement[0].appendChild(menu);
- return menu;
- }
- }
- ContextMenu.prototype.bindMouseEvents = function (){
- function contextMenuOpenListener(event){
- event.preventDefault();
- if(event.target.nodeName != 'TD' && !(Handsontable.Dom.hasClass(event.target, 'current') && Handsontable.Dom.hasClass(event.target, 'wtBorder'))){
- return;
- }
- this.show(event.pageY, event.pageX);
- $(document).on('mousedown.htContextMenu', Handsontable.helper.proxy(ContextMenu.prototype.close, this));
- }
- this.instance.rootElement.on('contextmenu.htContextMenu', Handsontable.helper.proxy(contextMenuOpenListener, this));
- $(this.menu).on('mousedown', Handsontable.helper.proxy(this.performAction, this));
- };
- ContextMenu.prototype.performAction = function (){
- var hot = $(this.menu).handsontable('getInstance');
- var selectedItemIndex = hot.getSelected()[0];
- var selectedItem = hot.getData()[selectedItemIndex];
- if (selectedItem.disabled === true || (typeof selectedItem.disabled == 'function' && selectedItem.disabled.call(this.instance) === true)){
- return;
- }
- if(typeof selectedItem.callback != 'function'){
- return;
- }
- var corners = this.instance.getSelected();
- var normalizedSelection = ContextMenu.utils.normalizeSelection(corners);
- selectedItem.callback.call(this.instance, selectedItem.key, normalizedSelection);
- };
- ContextMenu.prototype.unbindMouseEvents = function () {
- this.instance.rootElement.off('contextmenu.htContextMenu');
- $(document).off('mousedown.htContextMenu');
- };
- ContextMenu.prototype.show = function(top, left){
- this.menu.style.display = 'block';
- $(this.menu).handsontable({
- data: ContextMenu.utils.convertItemsToArray(this.getItems()),
- colHeaders: false,
- colWidths: [160],
- readOnly: true,
- columns: [
- {
- data: 'name',
- renderer: Handsontable.helper.proxy(this.renderer, this)
- }
- ],
- beforeKeyDown: Handsontable.helper.proxy(this.onBeforeKeyDown, this)
- });
- this.setMenuPosition(top, left);
- $(this.menu).handsontable('listen');
- };
- ContextMenu.prototype.renderer = function(instance, TD, row, col, prop, value, cellProperties){
- var contextMenu = this;
- var item = instance.getData()[row];
- var wrapper = document.createElement('DIV');
- Handsontable.Dom.empty(TD);
- TD.appendChild(wrapper);
- if(itemIsSeparator(item)){
- Handsontable.Dom.addClass(TD, 'htSeparator');
- } else {
- Handsontable.Dom.fastInnerText(wrapper, value);
- }
- if (itemIsDisabled(item, contextMenu.instance)){
- Handsontable.Dom.addClass(TD, 'htDisabled');
- $(wrapper).on('mouseenter', function () {
- instance.deselectCell();
- });
- } else {
- Handsontable.Dom.removeClass(TD, 'htDisabled');
- $(wrapper).on('mouseenter', function () {
- instance.selectCell(row, col);
- });
- }
- function itemIsSeparator(item){
- return new RegExp(ContextMenu.SEPARATOR, 'i').test(item.name);
- }
- function itemIsDisabled(item, instance){
- return item.disabled === true || (typeof item.disabled == 'function' && item.disabled.call(contextMenu.instance) === true);
- }
- };
- ContextMenu.prototype.onBeforeKeyDown = function (event) {
- var contextMenu = this;
- var instance = $(contextMenu.menu).handsontable('getInstance');
- var selection = instance.getSelected();
- switch(event.keyCode){
- case Handsontable.helper.keyCode.ESCAPE:
- contextMenu.close();
- event.preventDefault();
- event.stopImmediatePropagation();
- break;
- case Handsontable.helper.keyCode.ENTER:
- if(instance.getSelected()){
- contextMenu.performAction();
- contextMenu.close();
- }
- break;
- case Handsontable.helper.keyCode.ARROW_DOWN:
- if(!selection){
- selectFirstCell(instance);
- } else {
- selectNextCell(selection[0], selection[1], instance);
- }
- event.preventDefault();
- event.stopImmediatePropagation();
- break;
- case Handsontable.helper.keyCode.ARROW_UP:
- if(!selection){
- selectLastCell(instance);
- } else {
- selectPrevCell(selection[0], selection[1], instance);
- }
- event.preventDefault();
- event.stopImmediatePropagation();
- break;
- }
- function selectFirstCell(instance) {
- var firstCell = instance.getCell(0, 0);
- if(ContextMenu.utils.isSeparator(firstCell) || ContextMenu.utils.isDisabled(firstCell)){
- selectNextCell(0, 0, instance);
- } else {
- instance.selectCell(0, 0);
- }
- }
- function selectLastCell(instance) {
- var lastRow = instance.countRows() - 1;
- var lastCell = instance.getCell(lastRow, 0);
- if(ContextMenu.utils.isSeparator(lastCell) || ContextMenu.utils.isDisabled(lastCell)){
- selectPrevCell(lastRow, 0, instance);
- } else {
- instance.selectCell(lastRow, 0);
- }
- }
- function selectNextCell(row, col, instance){
- var nextRow = row + 1;
- var nextCell = nextRow < instance.countRows() ? instance.getCell(nextRow, col) : null;
- if(!nextCell){
- return;
- }
- if(ContextMenu.utils.isSeparator(nextCell) || ContextMenu.utils.isDisabled(nextCell)){
- selectNextCell(nextRow, col, instance);
- } else {
- instance.selectCell(nextRow, col);
- }
- }
- function selectPrevCell(row, col, instance) {
- var prevRow = row - 1;
- var prevCell = prevRow >= 0 ? instance.getCell(prevRow, col) : null;
- if (!prevCell) {
- return;
- }
- if(ContextMenu.utils.isSeparator(prevCell) || ContextMenu.utils.isDisabled(prevCell)){
- selectPrevCell(prevRow, col, instance);
- } else {
- instance.selectCell(prevRow, col);
- }
- }
- };
- ContextMenu.prototype.getItems = function () {
- var items = {};
- function Item(rawItem){
- if(typeof rawItem == 'string'){
- this.name = rawItem;
- } else {
- Handsontable.helper.extend(this, rawItem);
- }
- }
- Item.prototype = this.options;
- for(var itemName in this.options.items){
- if(this.options.items.hasOwnProperty(itemName) && (!this.itemsFilter || this.itemsFilter.indexOf(itemName) != -1)){
- items[itemName] = new Item(this.options.items[itemName]);
- }
- }
- return items;
- };
- ContextMenu.prototype.updateOptions = function(newOptions){
- newOptions = newOptions || {};
- if(newOptions.items){
- for(var itemName in newOptions.items){
- var item = {};
- if(newOptions.items.hasOwnProperty(itemName)) {
- if(this.defaultOptions.items.hasOwnProperty(itemName)
- && Handsontable.helper.isObject(newOptions.items[itemName])){
- Handsontable.helper.extend(item, this.defaultOptions.items[itemName]);
- Handsontable.helper.extend(item, newOptions.items[itemName]);
- newOptions.items[itemName] = item;
- }
- }
- }
- }
- Handsontable.helper.extend(this.options, newOptions);
- };
- ContextMenu.prototype.setMenuPosition = function (cursorY, cursorX) {
- var cursor = {
- top: cursorY,
- topRelative: cursorY - window.pageYOffset,
- left: cursorX,
- leftRelative:cursorX - window.pageXOffset
- };
- if(this.menuFitsBelowCursor(cursor)){
- this.positionMenuBelowCursor(cursor);
- } else {
- this.positionMenuAboveCursor(cursor);
- }
- if(this.menuFitsOnRightOfCursor(cursor)){
- this.positionMenuOnRightOfCursor(cursor);
- } else {
- this.positionMenuOnLeftOfCursor(cursor);
- }
- };
- ContextMenu.prototype.menuFitsBelowCursor = function (cursor) {
- return cursor.topRelative + this.menu.offsetHeight <= document.documentElement.scrollTop + document.documentElement.clientHeight;
- };
- ContextMenu.prototype.menuFitsOnRightOfCursor = function (cursor) {
- return cursor.leftRelative + this.menu.offsetWidth <= document.documentElement.scrollLeft + document.documentElement.clientWidth;
- };
- ContextMenu.prototype.positionMenuBelowCursor = function (cursor) {
- this.menu.style.top = this.getCursorRelativeToContainer(cursor).top + 'px';
- };
- ContextMenu.prototype.positionMenuAboveCursor = function (cursor) {
- this.menu.style.top = (this.getCursorRelativeToContainer(cursor).top - this.menu.offsetHeight) + 'px';
- };
- ContextMenu.prototype.positionMenuOnRightOfCursor = function (cursor) {
- this.menu.style.left = this.getCursorRelativeToContainer(cursor).left + 'px';
- };
- ContextMenu.prototype.positionMenuOnLeftOfCursor = function (cursor) {
- this.menu.style.left = (this.getCursorRelativeToContainer(cursor).left - this.menu.offsetWidth) + 'px';
- };
- ContextMenu.prototype.getCursorRelativeToContainer = function (cursor) {
- var containerOffset = Handsontable.Dom.offset(this.instance.rootElement[0]);
- return {
- left: cursor.left - containerOffset.left,
- top: cursor.top - containerOffset.top
- }
- };
- ContextMenu.utils = {};
- ContextMenu.utils.convertItemsToArray = function (items) {
- var itemArray = [];
- var item;
- for(var itemName in items){
- if(items.hasOwnProperty(itemName)){
- if(typeof items[itemName] == 'string'){
- item = {name: items[itemName]};
- } else if (items[itemName].visible !== false) {
- item = items[itemName];
- } else {
- continue;
- }
- item.key = itemName;
- itemArray.push(item);
- }
- }
- return itemArray;
- };
- ContextMenu.utils.normalizeSelection = function(corners){
- var selection = {
- start: new Handsontable.SelectionPoint(),
- end: new Handsontable.SelectionPoint()
- };
- selection.start.row(Math.min(corners[0], corners[2]));
- selection.start.col(Math.min(corners[1], corners[3]));
- selection.end.row(Math.max(corners[0], corners[2]));
- selection.end.col(Math.max(corners[1], corners[3]));
- return selection;
- };
- ContextMenu.utils.isSeparator = function (cell) {
- return Handsontable.Dom.hasClass(cell, 'htSeparator');
- };
- ContextMenu.utils.isDisabled = function (cell) {
- return Handsontable.Dom.hasClass(cell, 'htDisabled');
- };
- ContextMenu.prototype.close = function () {
- this.hide();
- $(document).off('mousedown.htContextMenu');
- this.instance.listen();
- };
- ContextMenu.prototype.hide = function(){
- this.menu.style.display = 'none';
- $(this.menu).handsontable('destroy');
- };
- ContextMenu.prototype.enable = function () {
- if(!this.enabled){
- this.enabled = true;
- this.bindMouseEvents();
- }
- };
- ContextMenu.prototype.disable = function () {
- if(this.enabled){
- this.enabled = false;
- this.close();
- this.unbindMouseEvents();
- }
- };
- ContextMenu.prototype.destroy = function () {
- this.close();
- this.unbindMouseEvents();
- if(this.menu.parentNode){
- this.menu.parentNode.removeChild(this.menu);
- }
- };
- ContextMenu.prototype.filterItems = function(itemsToLeave){
- this.itemsFilter = itemsToLeave;
- };
- ContextMenu.SEPARATOR = "---------";
- function init(){
- var instance = this;
- var contextMenuSetting = instance.getSettings().contextMenu;
- var customOptions = Handsontable.helper.isObject(contextMenuSetting) ? contextMenuSetting : {};
- if(contextMenuSetting){
- if(!instance.contextMenu){
- instance.contextMenu = new ContextMenu(instance, customOptions);
- }
- instance.contextMenu.enable();
- if(Handsontable.helper.isArray(contextMenuSetting)){
- instance.contextMenu.filterItems(contextMenuSetting);
- }
- } else if(instance.contextMenu){
- instance.contextMenu.destroy();
- delete instance.contextMenu;
- }
- }
- Handsontable.PluginHooks.add('afterInit', init);
- Handsontable.PluginHooks.add('afterUpdateSettings', init);
- Handsontable.ContextMenu = ContextMenu;
- })(Handsontable);
- /**
- * This plugin adds support for legacy features, deprecated APIs, etc.
- */
- /**
- * Support for old autocomplete syntax
- * For old syntax, see: https://github.com/warpech/jquery-handsontable/blob/8c9e701d090ea4620fe08b6a1a048672fadf6c7e/README.md#defining-autocomplete
- */
- Handsontable.PluginHooks.add('beforeGetCellMeta', function (row, col, cellProperties) {
- var settings = this.getSettings(), data = this.getData(), i, ilen, a;
- //isWritable - deprecated since 0.8.0
- cellProperties.isWritable = !cellProperties.readOnly;
- //autocomplete - deprecated since 0.7.1 (see CHANGELOG.md)
- if (settings.autoComplete) {
- for (i = 0, ilen = settings.autoComplete.length; i < ilen; i++) {
- if (settings.autoComplete[i].match(row, col, data)) {
- if (typeof cellProperties.type === 'undefined') {
- cellProperties.type = Handsontable.AutocompleteCell;
- }
- else {
- if (typeof cellProperties.type.renderer === 'undefined') {
- cellProperties.type.renderer = Handsontable.AutocompleteCell.renderer;
- }
- if (typeof cellProperties.type.editor === 'undefined') {
- cellProperties.type.editor = Handsontable.AutocompleteCell.editor;
- }
- }
- for (a in settings.autoComplete[i]) {
- if (settings.autoComplete[i].hasOwnProperty(a) && a !== 'match' && typeof cellProperties[i] === 'undefined') {
- if (a === 'source') {
- cellProperties[a] = settings.autoComplete[i][a](row, col);
- }
- else {
- cellProperties[a] = settings.autoComplete[i][a];
- }
- }
- }
- break;
- }
- }
- }
- });
- function HandsontableManualColumnMove() {
- var pressed
- , startCol
- , endCol
- , startX
- , startOffset;
- var ghost = document.createElement('DIV')
- , ghostStyle = ghost.style;
- ghost.className = 'ghost';
- ghostStyle.position = 'absolute';
- ghostStyle.top = '25px';
- ghostStyle.left = 0;
- ghostStyle.width = '10px';
- ghostStyle.height = '10px';
- ghostStyle.backgroundColor = '#CCC';
- ghostStyle.opacity = 0.7;
- var saveManualColumnPositions = function () {
- var instance = this;
- instance.PluginHooks.run('persistentStateSave', 'manualColumnPositions', instance.manualColumnPositions);
- };
- var loadManualColumnPositions = function () {
- var instance = this;
- var storedState = {};
- instance.PluginHooks.run('persistentStateLoad', 'manualColumnPositions', storedState);
- return storedState.value;
- };
- var bindMoveColEvents = function () {
- var instance = this;
- instance.rootElement.on('mousemove.manualColumnMove', function (e) {
- if (pressed) {
- ghostStyle.left = startOffset + e.pageX - startX + 6 + 'px';
- if (ghostStyle.display === 'none') {
- ghostStyle.display = 'block';
- }
- }
- });
- instance.rootElement.on('mouseup.manualColumnMove', function () {
- if (pressed) {
- if (startCol < endCol) {
- endCol--;
- }
- if (instance.getSettings().rowHeaders) {
- startCol--;
- endCol--;
- }
- instance.manualColumnPositions.splice(endCol, 0, instance.manualColumnPositions.splice(startCol, 1)[0]);
- $('.manualColumnMover.active').removeClass('active');
- pressed = false;
- instance.forceFullRender = true;
- instance.view.render(); //updates all
- ghostStyle.display = 'none';
- saveManualColumnPositions.call(instance);
- instance.PluginHooks.run('afterColumnMove', startCol, endCol);
- }
- });
- instance.rootElement.on('mousedown.manualColumnMove', '.manualColumnMover', function (e) {
- var mover = e.currentTarget;
- var TH = instance.view.wt.wtDom.closest(mover, 'TH');
- startCol = instance.view.wt.wtDom.index(TH) + instance.colOffset();
- endCol = startCol;
- pressed = true;
- startX = e.pageX;
- var TABLE = instance.$table[0];
- TABLE.parentNode.appendChild(ghost);
- ghostStyle.width = instance.view.wt.wtDom.outerWidth(TH) + 'px';
- ghostStyle.height = instance.view.wt.wtDom.outerHeight(TABLE) + 'px';
- startOffset = parseInt(instance.view.wt.wtDom.offset(TH).left - instance.view.wt.wtDom.offset(TABLE).left, 10);
- ghostStyle.left = startOffset + 6 + 'px';
- });
- instance.rootElement.on('mouseenter.manualColumnMove', 'td, th', function () {
- if (pressed) {
- var active = instance.view.THEAD.querySelector('.manualColumnMover.active');
- if (active) {
- instance.view.wt.wtDom.removeClass(active, 'active');
- }
- endCol = instance.view.wt.wtDom.index(this) + instance.colOffset();
- var THs = instance.view.THEAD.querySelectorAll('th');
- var mover = THs[endCol].querySelector('.manualColumnMover');
- instance.view.wt.wtDom.addClass(mover, 'active');
- }
- });
- instance.addHook('afterDestroy', unbindMoveColEvents);
- };
- var unbindMoveColEvents = function(){
- var instance = this;
- instance.rootElement.off('mouseup.manualColumnMove');
- instance.rootElement.off('mousemove.manualColumnMove');
- instance.rootElement.off('mousedown.manualColumnMove');
- instance.rootElement.off('mouseenter.manualColumnMove');
- };
- this.beforeInit = function () {
- this.manualColumnPositions = [];
- };
- this.init = function (source) {
- var instance = this;
- var manualColMoveEnabled = !!(this.getSettings().manualColumnMove);
- if (manualColMoveEnabled) {
- var initialManualColumnPositions = this.getSettings().manualColumnMove;
- var loadedManualColumnPositions = loadManualColumnPositions.call(instance);
- if (typeof loadedManualColumnPositions != 'undefined') {
- this.manualColumnPositions = loadedManualColumnPositions;
- } else if (initialManualColumnPositions instanceof Array) {
- this.manualColumnPositions = initialManualColumnPositions;
- } else {
- this.manualColumnPositions = [];
- }
- instance.forceFullRender = true;
- if (source == 'afterInit') {
- bindMoveColEvents.call(this);
- if (this.manualColumnPositions.length > 0) {
- this.forceFullRender = true;
- this.render();
- }
- }
- } else {
- unbindMoveColEvents.call(this);
- this.manualColumnPositions = [];
- }
- };
- this.modifyCol = function (col) {
- //TODO test performance: http://jsperf.com/object-wrapper-vs-primitive/2
- if (this.getSettings().manualColumnMove) {
- if (typeof this.manualColumnPositions[col] === 'undefined') {
- this.manualColumnPositions[col] = col;
- }
- return this.manualColumnPositions[col];
- }
- return col;
- };
- this.getColHeader = function (col, TH) {
- if (this.getSettings().manualColumnMove) {
- var DIV = document.createElement('DIV');
- DIV.className = 'manualColumnMover';
- TH.firstChild.appendChild(DIV);
- }
- };
- }
- var htManualColumnMove = new HandsontableManualColumnMove();
- Handsontable.PluginHooks.add('beforeInit', htManualColumnMove.beforeInit);
- Handsontable.PluginHooks.add('afterInit', function () {
- htManualColumnMove.init.call(this, 'afterInit')
- });
- Handsontable.PluginHooks.add('afterUpdateSettings', function () {
- htManualColumnMove.init.call(this, 'afterUpdateSettings')
- });
- Handsontable.PluginHooks.add('afterGetColHeader', htManualColumnMove.getColHeader);
- Handsontable.PluginHooks.add('modifyCol', htManualColumnMove.modifyCol);
- function HandsontableManualColumnResize() {
- var pressed
- , currentTH
- , currentCol
- , currentWidth
- , instance
- , newSize
- , startX
- , startWidth
- , startOffset
- , resizer = document.createElement('DIV')
- , handle = document.createElement('DIV')
- , line = document.createElement('DIV')
- , lineStyle = line.style;
- resizer.className = 'manualColumnResizer';
- handle.className = 'manualColumnResizerHandle';
- resizer.appendChild(handle);
- line.className = 'manualColumnResizerLine';
- resizer.appendChild(line);
- var $document = $(document);
- $document.mousemove(function (e) {
- if (pressed) {
- currentWidth = startWidth + (e.pageX - startX);
- newSize = setManualSize(currentCol, currentWidth); //save col width
- resizer.style.left = startOffset + currentWidth + 'px';
- }
- });
- $document.mouseup(function () {
- if (pressed) {
- instance.view.wt.wtDom.removeClass(resizer, 'active');
- pressed = false;
- if(newSize != startWidth){
- instance.forceFullRender = true;
- instance.view.render(); //updates all
- saveManualColumnWidths.call(instance);
- instance.PluginHooks.run('afterColumnResize', currentCol, newSize);
- }
- refreshResizerPosition.call(instance, currentTH);
- }
- });
- var saveManualColumnWidths = function () {
- var instance = this;
- instance.PluginHooks.run('persistentStateSave', 'manualColumnWidths', instance.manualColumnWidths);
- };
- var loadManualColumnWidths = function () {
- var instance = this;
- var storedState = {};
- instance.PluginHooks.run('persistentStateLoad', 'manualColumnWidths', storedState);
- return storedState.value;
- };
- function refreshResizerPosition(TH) {
- instance = this;
- currentTH = TH;
- var col = this.view.wt.wtTable.getCoords(TH)[1]; //getCoords returns array [row, col]
- if (col >= 0) { //if not row header
- currentCol = col;
- var rootOffset = this.view.wt.wtDom.offset(this.rootElement[0]).left;
- var thOffset = this.view.wt.wtDom.offset(TH).left;
- startOffset = (thOffset - rootOffset) - 6;
- resizer.style.left = startOffset + parseInt(this.view.wt.wtDom.outerWidth(TH), 10) + 'px';
- this.rootElement[0].appendChild(resizer);
- }
- }
- function refreshLinePosition() {
- var instance = this;
- startWidth = parseInt(this.view.wt.wtDom.outerWidth(currentTH), 10);
- instance.view.wt.wtDom.addClass(resizer, 'active');
- lineStyle.height = instance.view.wt.wtDom.outerHeight(instance.$table[0]) + 'px';
- pressed = instance;
- }
- var bindManualColumnWidthEvents = function () {
- var instance = this;
- var dblclick = 0;
- var autoresizeTimeout = null;
- this.rootElement.on('mouseenter.handsontable', 'th', function (e) {
- if (!pressed) {
- refreshResizerPosition.call(instance, e.currentTarget);
- }
- });
- this.rootElement.on('mousedown.handsontable', '.manualColumnResizer', function () {
- if (autoresizeTimeout == null) {
- autoresizeTimeout = setTimeout(function () {
- if (dblclick >= 2) {
- newSize = instance.determineColumnWidth.call(instance, currentCol);
- setManualSize(currentCol, newSize);
- instance.forceFullRender = true;
- instance.view.render(); //updates all
- instance.PluginHooks.run('afterColumnResize', currentCol, newSize);
- }
- dblclick = 0;
- autoresizeTimeout = null;
- }, 500);
- }
- dblclick++;
- });
- this.rootElement.on('mousedown.handsontable', '.manualColumnResizer', function (e) {
- startX = e.pageX;
- refreshLinePosition.call(instance);
- newSize = startWidth;
- });
- };
- this.beforeInit = function () {
- this.manualColumnWidths = [];
- };
- this.init = function (source) {
- var instance = this;
- var manualColumnWidthEnabled = !!(this.getSettings().manualColumnResize);
- if (manualColumnWidthEnabled) {
- var initialColumnWidths = this.getSettings().manualColumnResize;
- var loadedManualColumnWidths = loadManualColumnWidths.call(instance);
- if (typeof loadedManualColumnWidths != 'undefined') {
- this.manualColumnWidths = loadedManualColumnWidths;
- } else if (initialColumnWidths instanceof Array) {
- this.manualColumnWidths = initialColumnWidths;
- } else {
- this.manualColumnWidths = [];
- }
- if (source == 'afterInit') {
- bindManualColumnWidthEvents.call(this);
- instance.forceFullRender = true;
- instance.render();
- }
- }
- };
- var setManualSize = function (col, width) {
- width = Math.max(width, 20);
- /**
- * We need to run col through modifyCol hook, in case the order of displayed columns is different than the order
- * in data source. For instance, this order can be modified by manualColumnMove plugin.
- */
- col = instance.PluginHooks.execute('modifyCol', col);
- instance.manualColumnWidths[col] = width;
- return width;
- };
- this.getColWidth = function (col, response) {
- if (this.getSettings().manualColumnResize && this.manualColumnWidths[col]) {
- response.width = this.manualColumnWidths[col];
- }
- };
- }
- var htManualColumnResize = new HandsontableManualColumnResize();
- Handsontable.PluginHooks.add('beforeInit', htManualColumnResize.beforeInit);
- Handsontable.PluginHooks.add('afterInit', function () {
- htManualColumnResize.init.call(this, 'afterInit')
- });
- Handsontable.PluginHooks.add('afterUpdateSettings', function () {
- htManualColumnResize.init.call(this, 'afterUpdateSettings')
- });
- Handsontable.PluginHooks.add('afterGetColWidth', htManualColumnResize.getColWidth);
- (function HandsontableObserveChanges() {
- Handsontable.PluginHooks.add('afterLoadData', init);
- Handsontable.PluginHooks.add('afterUpdateSettings', init);
- function init() {
- var instance = this;
- var pluginEnabled = instance.getSettings().observeChanges;
- if (!instance.observer && pluginEnabled) {
- createObserver.call(instance);
- bindEvents.call(instance);
- } else if (!pluginEnabled){
- destroy.call(instance);
- }
- }
- function createObserver(){
- var instance = this;
- instance.observeChangesActive = true;
- instance.pauseObservingChanges = function(){
- instance.observeChangesActive = false;
- };
- instance.resumeObservingChanges = function(){
- instance.observeChangesActive = true;
- };
- instance.observer = jsonpatch.observe(instance.getData(), function (patches) {
- if(instance.observeChangesActive){
- runHookForOperation.call(instance, patches);
- instance.render();
- }
- instance.runHooks('afterChangesObserved');
- });
- }
- function runHookForOperation(rawPatches){
- var instance = this;
- var patches = cleanPatches(rawPatches);
- for(var i = 0, len = patches.length; i < len; i++){
- var patch = patches[i];
- var parsedPath = parsePath(patch.path);
- switch(patch.op){
- case 'add':
- if(isNaN(parsedPath.col)){
- instance.runHooks('afterCreateRow', parsedPath.row);
- } else {
- instance.runHooks('afterCreateCol', parsedPath.col);
- }
- break;
- case 'remove':
- if(isNaN(parsedPath.col)){
- instance.runHooks('afterRemoveRow', parsedPath.row, 1);
- } else {
- instance.runHooks('afterRemoveCol', parsedPath.col, 1);
- }
- break;
- case 'replace':
- instance.runHooks('afterChange', [parsedPath.row, parsedPath.col, null, patch.value], 'external');
- break;
- }
- }
- function cleanPatches(rawPatches){
- var patches;
- patches = removeLengthRelatedPatches(rawPatches);
- patches = removeMultipleAddOrRemoveColPatches(patches);
- return patches;
- }
- /**
- * Removing or adding column will produce one patch for each table row.
- * This function leaves only one patch for each column add/remove operation
- */
- function removeMultipleAddOrRemoveColPatches(rawPatches){
- var newOrRemovedColumns = [];
- return rawPatches.filter(function(patch){
- var parsedPath = parsePath(patch.path);
- if(['add', 'remove'].indexOf(patch.op) != -1 && !isNaN(parsedPath.col)){
- if(newOrRemovedColumns.indexOf(parsedPath.col) != -1){
- return false;
- } else {
- newOrRemovedColumns.push(parsedPath.col);
- }
- }
- return true;
- });
- }
- /**
- * If observeChanges uses native Object.observe method, then it produces patches for length property.
- * This function removes them.
- */
- function removeLengthRelatedPatches(rawPatches){
- return rawPatches.filter(function(patch){
- return !/[/]length/ig.test(patch.path);
- })
- }
- function parsePath(path){
- var match = path.match(/^\/(\d+)\/?(.*)?$/);
- return {
- row: parseInt(match[1], 10),
- col: /^\d*$/.test(match[2]) ? parseInt(match[2], 10) : match[2]
- }
- }
- }
- function destroy(){
- var instance = this;
- if (instance.observer){
- destroyObserver.call(instance);
- unbindEvents.call(instance);
- }
- }
- function destroyObserver(){
- var instance = this;
- jsonpatch.unobserve(instance.getData(), instance.observer);
- delete instance.observeChangesActive;
- delete instance.pauseObservingChanges;
- delete instance.resumeObservingChanges;
- }
- function bindEvents(){
- var instance = this;
- instance.addHook('afterDestroy', destroy);
- instance.addHook('afterCreateRow', afterTableAlter);
- instance.addHook('afterRemoveRow', afterTableAlter);
- instance.addHook('afterCreateCol', afterTableAlter);
- instance.addHook('afterRemoveCol', afterTableAlter);
- instance.addHook('afterChange', function(changes, source){
- if(source != 'loadData'){
- afterTableAlter.call(this);
- }
- });
- }
- function unbindEvents(){
- var instance = this;
- instance.removeHook('afterDestroy', destroy);
- instance.removeHook('afterCreateRow', afterTableAlter);
- instance.removeHook('afterRemoveRow', afterTableAlter);
- instance.removeHook('afterCreateCol', afterTableAlter);
- instance.removeHook('afterRemoveCol', afterTableAlter);
- instance.removeHook('afterChange', afterTableAlter);
- }
- function afterTableAlter(){
- var instance = this;
- instance.pauseObservingChanges();
- instance.addHookOnce('afterChangesObserved', function(){
- instance.resumeObservingChanges();
- });
- }
- })();
- /*
- *
- * Plugin enables saving table state
- *
- * */
- function Storage(prefix) {
- var savedKeys;
- var saveSavedKeys = function () {
- window.localStorage[prefix + '__' + 'persistentStateKeys'] = JSON.stringify(savedKeys);
- };
- var loadSavedKeys = function () {
- var keysJSON = window.localStorage[prefix + '__' + 'persistentStateKeys'];
- var keys = typeof keysJSON == 'string' ? JSON.parse(keysJSON) : void 0;
- savedKeys = keys ? keys : [];
- };
- var clearSavedKeys = function () {
- savedKeys = [];
- saveSavedKeys();
- };
- loadSavedKeys();
- this.saveValue = function (key, value) {
- window.localStorage[prefix + '_' + key] = JSON.stringify(value);
- if (savedKeys.indexOf(key) == -1) {
- savedKeys.push(key);
- saveSavedKeys();
- }
- };
- this.loadValue = function (key, defaultValue) {
- key = typeof key != 'undefined' ? key : defaultValue;
- var value = window.localStorage[prefix + '_' + key];
- return typeof value == "undefined" ? void 0 : JSON.parse(value);
- };
- this.reset = function (key) {
- window.localStorage.removeItem(prefix + '_' + key);
- };
- this.resetAll = function () {
- for (var index = 0; index < savedKeys.length; index++) {
- window.localStorage.removeItem(prefix + '_' + savedKeys[index]);
- }
- clearSavedKeys();
- };
- }
- (function (StorageClass) {
- function HandsontablePersistentState() {
- var plugin = this;
- this.init = function () {
- var instance = this,
- pluginSettings = instance.getSettings()['persistentState'];
- plugin.enabled = !!(pluginSettings);
- if (!plugin.enabled) {
- removeHooks.call(instance);
- return;
- }
- if (!instance.storage) {
- instance.storage = new StorageClass(instance.rootElement[0].id);
- }
- instance.resetState = plugin.resetValue;
- addHooks.call(instance);
- };
- this.saveValue = function (key, value) {
- var instance = this;
- instance.storage.saveValue(key, value);
- };
- this.loadValue = function (key, saveTo) {
- var instance = this;
- saveTo.value = instance.storage.loadValue(key);
- };
- this.resetValue = function (key) {
- var instance = this;
- if (typeof key != 'undefined') {
- instance.storage.reset(key);
- } else {
- instance.storage.resetAll();
- }
- };
- var hooks = {
- 'persistentStateSave': plugin.saveValue,
- 'persistentStateLoad': plugin.loadValue,
- 'persistentStateReset': plugin.resetValue
- };
- function addHooks() {
- var instance = this;
- for (var hookName in hooks) {
- if (hooks.hasOwnProperty(hookName) && !hookExists.call(instance, hookName)) {
- instance.PluginHooks.add(hookName, hooks[hookName]);
- }
- }
- }
- function removeHooks() {
- var instance = this;
- for (var hookName in hooks) {
- if (hooks.hasOwnProperty(hookName) && hookExists.call(instance, hookName)) {
- instance.PluginHooks.remove(hookName, hooks[hookName]);
- }
- }
- }
- function hookExists(hookName) {
- var instance = this;
- return instance.PluginHooks.hooks.hasOwnProperty(hookName);
- }
- }
- var htPersistentState = new HandsontablePersistentState();
- Handsontable.PluginHooks.add('beforeInit', htPersistentState.init);
- Handsontable.PluginHooks.add('afterUpdateSettings', htPersistentState.init);
- })(Storage);
- /**
- * Handsontable UndoRedo class
- */
- (function(Handsontable){
- Handsontable.UndoRedo = function (instance) {
- var plugin = this;
- this.instance = instance;
- this.doneActions = [];
- this.undoneActions = [];
- this.ignoreNewActions = false;
- instance.addHook("afterChange", function (changes, origin) {
- if(changes){
- var action = new Handsontable.UndoRedo.ChangeAction(changes);
- plugin.done(action);
- }
- });
- instance.addHook("afterCreateRow", function (index, amount) {
- var action = new Handsontable.UndoRedo.CreateRowAction(index, amount);
- plugin.done(action);
- });
- instance.addHook("beforeRemoveRow", function (index, amount) {
- var originalData = plugin.instance.getData();
- index = ( originalData.length + index ) % originalData.length;
- var removedData = originalData.slice(index, index + amount);
- var action = new Handsontable.UndoRedo.RemoveRowAction(index, removedData);
- plugin.done(action);
- });
- instance.addHook("afterCreateCol", function (index, amount) {
- var action = new Handsontable.UndoRedo.CreateColumnAction(index, amount);
- plugin.done(action);
- });
- instance.addHook("beforeRemoveCol", function (index, amount) {
- var originalData = plugin.instance.getData();
- index = ( plugin.instance.countCols() + index ) % plugin.instance.countCols();
- var removedData = [];
- for (var i = 0, len = originalData.length; i < len; i++) {
- removedData[i] = originalData[i].slice(index, index + amount);
- }
- var headers;
- if(Handsontable.helper.isArray(instance.getSettings().colHeaders)){
- headers = instance.getSettings().colHeaders.slice(index, index + removedData.length);
- }
- var action = new Handsontable.UndoRedo.RemoveColumnAction(index, removedData, headers);
- plugin.done(action);
- });
- };
- Handsontable.UndoRedo.prototype.done = function (action) {
- if (!this.ignoreNewActions) {
- this.doneActions.push(action);
- this.undoneActions.length = 0;
- }
- };
- /**
- * Undo operation from current revision
- */
- Handsontable.UndoRedo.prototype.undo = function () {
- if (this.isUndoAvailable()) {
- var action = this.doneActions.pop();
- this.ignoreNewActions = true;
- action.undo(this.instance);
- this.ignoreNewActions = false;
- this.undoneActions.push(action);
- }
- };
- /**
- * Redo operation from current revision
- */
- Handsontable.UndoRedo.prototype.redo = function () {
- if (this.isRedoAvailable()) {
- var action = this.undoneActions.pop();
- this.ignoreNewActions = true;
- action.redo(this.instance);
- this.ignoreNewActions = true;
- this.doneActions.push(action);
- }
- };
- /**
- * Returns true if undo point is available
- * @return {Boolean}
- */
- Handsontable.UndoRedo.prototype.isUndoAvailable = function () {
- return this.doneActions.length > 0;
- };
- /**
- * Returns true if redo point is available
- * @return {Boolean}
- */
- Handsontable.UndoRedo.prototype.isRedoAvailable = function () {
- return this.undoneActions.length > 0;
- };
- /**
- * Clears undo history
- */
- Handsontable.UndoRedo.prototype.clear = function () {
- this.doneActions.length = 0;
- this.undoneActions.length = 0;
- };
- Handsontable.UndoRedo.Action = function () {
- };
- Handsontable.UndoRedo.Action.prototype.undo = function () {
- };
- Handsontable.UndoRedo.Action.prototype.redo = function () {
- };
- Handsontable.UndoRedo.ChangeAction = function (changes) {
- this.changes = changes;
- };
- Handsontable.helper.inherit(Handsontable.UndoRedo.ChangeAction, Handsontable.UndoRedo.Action);
- Handsontable.UndoRedo.ChangeAction.prototype.undo = function (instance) {
- var data = $.extend(true, [], this.changes);
- for (var i = 0, len = data.length; i < len; i++) {
- data[i].splice(3, 1);
- }
- instance.setDataAtRowProp(data, null, null, 'undo');
- };
- Handsontable.UndoRedo.ChangeAction.prototype.redo = function (instance) {
- var data = $.extend(true, [], this.changes);
- for (var i = 0, len = data.length; i < len; i++) {
- data[i].splice(2, 1);
- }
- instance.setDataAtRowProp(data, null, null, 'redo');
- };
- Handsontable.UndoRedo.CreateRowAction = function (index, amount) {
- this.index = index;
- this.amount = amount;
- };
- Handsontable.helper.inherit(Handsontable.UndoRedo.CreateRowAction, Handsontable.UndoRedo.Action);
- Handsontable.UndoRedo.CreateRowAction.prototype.undo = function (instance) {
- instance.alter('remove_row', this.index, this.amount);
- };
- Handsontable.UndoRedo.CreateRowAction.prototype.redo = function (instance) {
- instance.alter('insert_row', this.index + 1, this.amount);
- };
- Handsontable.UndoRedo.RemoveRowAction = function (index, data) {
- this.index = index;
- this.data = data;
- };
- Handsontable.helper.inherit(Handsontable.UndoRedo.RemoveRowAction, Handsontable.UndoRedo.Action);
- Handsontable.UndoRedo.RemoveRowAction.prototype.undo = function (instance) {
- var spliceArgs = [this.index, 0];
- Array.prototype.push.apply(spliceArgs, this.data);
- Array.prototype.splice.apply(instance.getData(), spliceArgs);
- instance.render();
- };
- Handsontable.UndoRedo.RemoveRowAction.prototype.redo = function (instance) {
- instance.alter('remove_row', this.index, this.data.length);
- };
- Handsontable.UndoRedo.CreateColumnAction = function (index, amount) {
- this.index = index;
- this.amount = amount;
- };
- Handsontable.helper.inherit(Handsontable.UndoRedo.CreateColumnAction, Handsontable.UndoRedo.Action);
- Handsontable.UndoRedo.CreateColumnAction.prototype.undo = function (instance) {
- instance.alter('remove_col', this.index, this.amount);
- };
- Handsontable.UndoRedo.CreateColumnAction.prototype.redo = function (instance) {
- instance.alter('insert_col', this.index + 1, this.amount);
- };
- Handsontable.UndoRedo.RemoveColumnAction = function (index, data, headers) {
- this.index = index;
- this.data = data;
- this.amount = this.data[0].length;
- this.headers = headers;
- };
- Handsontable.helper.inherit(Handsontable.UndoRedo.RemoveColumnAction, Handsontable.UndoRedo.Action);
- Handsontable.UndoRedo.RemoveColumnAction.prototype.undo = function (instance) {
- var row, spliceArgs;
- for (var i = 0, len = instance.getData().length; i < len; i++) {
- row = instance.getDataAtRow(i);
- spliceArgs = [this.index, 0];
- Array.prototype.push.apply(spliceArgs, this.data[i]);
- Array.prototype.splice.apply(row, spliceArgs);
- }
- if(typeof this.headers != 'undefined'){
- spliceArgs = [this.index, 0];
- Array.prototype.push.apply(spliceArgs, this.headers);
- Array.prototype.splice.apply(instance.getSettings().colHeaders, spliceArgs);
- }
- instance.render();
- };
- Handsontable.UndoRedo.RemoveColumnAction.prototype.redo = function (instance) {
- instance.alter('remove_col', this.index, this.amount);
- };
- })(Handsontable);
- (function(Handsontable){
- function init(){
- var instance = this;
- var pluginEnabled = typeof instance.getSettings().undo == 'undefined' || instance.getSettings().undo;
- if(pluginEnabled){
- if(!instance.undoRedo){
- instance.undoRedo = new Handsontable.UndoRedo(instance);
- exposeUndoRedoMethods(instance);
- instance.addHook('beforeKeyDown', onBeforeKeyDown);
- instance.addHook('afterChange', onAfterChange);
- }
- } else {
- if(instance.undoRedo){
- delete instance.undoRedo;
- removeExposedUndoRedoMethods(instance);
- instance.removeHook('beforeKeyDown', onBeforeKeyDown);
- instance.removeHook('afterChange', onAfterChange);
- }
- }
- }
- function onBeforeKeyDown(event){
- var instance = this;
- var ctrlDown = (event.ctrlKey || event.metaKey) && !event.altKey;
- if(ctrlDown){
- if (event.keyCode === 89 || (event.shiftKey && event.keyCode === 90)) { //CTRL + Y or CTRL + SHIFT + Z
- instance.undoRedo.redo();
- event.stopImmediatePropagation();
- }
- else if (event.keyCode === 90) { //CTRL + Z
- instance.undoRedo.undo();
- event.stopImmediatePropagation();
- }
- }
- }
- function onAfterChange(changes, source){
- var instance = this;
- if (source == 'loadData'){
- return instance.undoRedo.clear();
- }
- }
- function exposeUndoRedoMethods(instance){
- instance.undo = function(){
- return instance.undoRedo.undo();
- };
- instance.redo = function(){
- return instance.undoRedo.redo();
- };
- instance.isUndoAvailable = function(){
- return instance.undoRedo.isUndoAvailable();
- };
- instance.isRedoAvailable = function(){
- return instance.undoRedo.isRedoAvailable();
- };
- instance.clearUndo = function(){
- return instance.undoRedo.clear();
- };
- }
- function removeExposedUndoRedoMethods(instance){
- delete instance.undo;
- delete instance.redo;
- delete instance.isUndoAvailable;
- delete instance.isRedoAvailable;
- delete instance.clearUndo;
- }
- Handsontable.PluginHooks.add('afterInit', init);
- Handsontable.PluginHooks.add('afterUpdateSettings', init);
- })(Handsontable);
- /**
- * Plugin used to scroll Handsontable by selecting a cell and dragging outside of visible viewport
- * @constructor
- */
- function DragToScroll() {
- this.boundaries = null;
- this.callback = null;
- }
- /**
- * @param boundaries {Object} compatible with getBoundingClientRect
- */
- DragToScroll.prototype.setBoundaries = function (boundaries) {
- this.boundaries = boundaries;
- };
- /**
- * @param callback {Function}
- */
- DragToScroll.prototype.setCallback = function (callback) {
- this.callback = callback;
- };
- /**
- * Check if mouse position (x, y) is outside of the viewport
- * @param x
- * @param y
- */
- DragToScroll.prototype.check = function (x, y) {
- var diffX = 0;
- var diffY = 0;
- if (y < this.boundaries.top) {
- //y is less than top
- diffY = y - this.boundaries.top;
- }
- else if (y > this.boundaries.bottom) {
- //y is more than bottom
- diffY = y - this.boundaries.bottom;
- }
- if (x < this.boundaries.left) {
- //x is less than left
- diffX = x - this.boundaries.left;
- }
- else if (x > this.boundaries.right) {
- //x is more than right
- diffX = x - this.boundaries.right;
- }
- this.callback(diffX, diffY);
- };
- var listening = false;
- var dragToScroll;
- var instance;
- if (typeof Handsontable !== 'undefined') {
- var setupListening = function (instance) {
- var scrollHandler = instance.view.wt.wtScrollbars.vertical.scrollHandler; //native scroll
- dragToScroll = new DragToScroll();
- if (scrollHandler === window) {
- //not much we can do currently
- return;
- }
- else if (scrollHandler) {
- dragToScroll.setBoundaries(scrollHandler.getBoundingClientRect());
- }
- else {
- dragToScroll.setBoundaries(instance.$table[0].getBoundingClientRect());
- }
- dragToScroll.setCallback(function (scrollX, scrollY) {
- if (scrollX < 0) {
- if (scrollHandler) {
- scrollHandler.scrollLeft -= 50;
- }
- else {
- instance.view.wt.scrollHorizontal(-1).draw();
- }
- }
- else if (scrollX > 0) {
- if (scrollHandler) {
- scrollHandler.scrollLeft += 50;
- }
- else {
- instance.view.wt.scrollHorizontal(1).draw();
- }
- }
- if (scrollY < 0) {
- if (scrollHandler) {
- scrollHandler.scrollTop -= 20;
- }
- else {
- instance.view.wt.scrollVertical(-1).draw();
- }
- }
- else if (scrollY > 0) {
- if (scrollHandler) {
- scrollHandler.scrollTop += 20;
- }
- else {
- instance.view.wt.scrollVertical(1).draw();
- }
- }
- });
- };
- Handsontable.PluginHooks.add('afterInit', function () {
- $(document).on('mouseup.' + this.guid, function () {
- listening = false;
- });
- $(document).on('mousemove.' + this.guid, function (event) {
- if (listening) {
- dragToScroll.check(event.clientX, event.clientY);
- }
- });
- });
- Handsontable.PluginHooks.add('destroy', function () {
- $(document).off('.' + this.guid);
- });
- Handsontable.PluginHooks.add('afterOnCellMouseDown', function () {
- listening = true;
- setupListening(this);
- });
- Handsontable.PluginHooks.add('afterOnCellCornerMouseDown', function () {
- listening = true;
- setupListening(this);
- });
- }
- /*
- * jQuery.fn.autoResize 1.1+
- * --
- * https://github.com/warpech/jQuery.fn.autoResize
- *
- * This fork differs from others in a way that it autoresizes textarea in 2-dimensions (horizontally and vertically).
- * It was originally forked from alexbardas's repo but maybe should be merged with dpashkevich's repo in future.
- *
- * originally forked from:
- * https://github.com/jamespadolsey/jQuery.fn.autoResize
- * which is now located here:
- * https://github.com/alexbardas/jQuery.fn.autoResize
- * though the mostly maintained for is here:
- * https://github.com/dpashkevich/jQuery.fn.autoResize/network
- *
- * --
- * This program is free software. It comes without any warranty, to
- * the extent permitted by applicable law. You can redistribute it
- * and/or modify it under the terms of the Do What The Fuck You Want
- * To Public License, Version 2, as published by Sam Hocevar. See
- * http://sam.zoy.org/wtfpl/COPYING for more details. */
- (function($){
- autoResize.defaults = {
- onResize: function(){},
- animate: {
- duration: 200,
- complete: function(){}
- },
- extraSpace: 50,
- minHeight: 'original',
- maxHeight: 500,
- minWidth: 'original',
- maxWidth: 500
- };
- autoResize.cloneCSSProperties = [
- 'lineHeight', 'textDecoration', 'letterSpacing',
- 'fontSize', 'fontFamily', 'fontStyle', 'fontWeight',
- 'textTransform', 'textAlign', 'direction', 'wordSpacing', 'fontSizeAdjust',
- 'padding'
- ];
- autoResize.cloneCSSValues = {
- position: 'absolute',
- top: -9999,
- left: -9999,
- opacity: 0,
- overflow: 'hidden',
- overflowX: 'hidden',
- overflowY: 'hidden',
- border: '1px solid black',
- padding: '0.49em' //this must be about the width of caps W character
- };
- autoResize.resizableFilterSelector = 'textarea,input:not(input[type]),input[type=text],input[type=password]';
- autoResize.AutoResizer = AutoResizer;
- $.fn.autoResize = autoResize;
- function autoResize(config) {
- this.filter(autoResize.resizableFilterSelector).each(function(){
- new AutoResizer( $(this), config );
- });
- return this;
- }
- function AutoResizer(el, config) {
- if(this.clones) return;
- this.config = $.extend({}, autoResize.defaults, config);
- this.el = el;
- this.nodeName = el[0].nodeName.toLowerCase();
- this.previousScrollTop = null;
- if (config.maxWidth === 'original') config.maxWidth = el.width();
- if (config.minWidth === 'original') config.minWidth = el.width();
- if (config.maxHeight === 'original') config.maxHeight = el.height();
- if (config.minHeight === 'original') config.minHeight = el.height();
- if (this.nodeName === 'textarea') {
- el.css({
- resize: 'none',
- overflowY: 'none'
- });
- }
- el.data('AutoResizer', this);
- this.createClone();
- this.injectClone();
- this.bind();
- }
- AutoResizer.prototype = {
- bind: function() {
- var check = $.proxy(function(){
- this.check();
- return true;
- }, this);
- this.unbind();
- this.el
- .bind('keyup.autoResize', check)
- //.bind('keydown.autoResize', check)
- .bind('change.autoResize', check);
- this.check(null, true);
- },
- unbind: function() {
- this.el.unbind('.autoResize');
- },
- createClone: function() {
- var el = this.el,
- self = this,
- config = this.config;
- this.clones = $();
- if (config.minHeight !== 'original' || config.maxHeight !== 'original') {
- this.hClone = el.clone().height('auto');
- this.clones = this.clones.add(this.hClone);
- }
- if (config.minWidth !== 'original' || config.maxWidth !== 'original') {
- this.wClone = $('<div/>').width('auto').css({
- whiteSpace: 'nowrap',
- 'float': 'left'
- });
- this.clones = this.clones.add(this.wClone);
- }
- $.each(autoResize.cloneCSSProperties, function(i, p){
- self.clones.css(p, el.css(p));
- });
- this.clones
- .removeAttr('name')
- .removeAttr('id')
- .attr('tabIndex', -1)
- .css(autoResize.cloneCSSValues)
- .css('overflowY', 'scroll');
- },
- check: function(e, immediate) {
- var config = this.config,
- wClone = this.wClone,
- hClone = this.hClone,
- el = this.el,
- value = el.val();
- if (wClone) {
- wClone.text(value);
- // Calculate new width + whether to change
- var cloneWidth = wClone.outerWidth(),
- newWidth = (cloneWidth + config.extraSpace) >= config.minWidth ?
- cloneWidth + config.extraSpace : config.minWidth,
- currentWidth = el.width();
- newWidth = Math.min(newWidth, config.maxWidth);
- if (
- (newWidth < currentWidth && newWidth >= config.minWidth) ||
- (newWidth >= config.minWidth && newWidth <= config.maxWidth)
- ) {
- config.onResize.call(el);
- el.scrollLeft(0);
- config.animate && !immediate ?
- el.stop(1,1).animate({
- width: newWidth
- }, config.animate)
- : el.width(newWidth);
- }
- }
- if (hClone) {
- if (newWidth) {
- hClone.width(newWidth);
- }
- hClone.height(0).val(value).scrollTop(10000);
- var scrollTop = hClone[0].scrollTop + config.extraSpace;
- // Don't do anything if scrollTop hasen't changed:
- if (this.previousScrollTop === scrollTop) {
- return;
- }
- this.previousScrollTop = scrollTop;
- if (scrollTop >= config.maxHeight) {
- scrollTop = config.maxHeight;
- }
- if (scrollTop < config.minHeight) {
- scrollTop = config.minHeight;
- }
- if(scrollTop == config.maxHeight && newWidth == config.maxWidth) {
- el.css('overflowY', 'scroll');
- }
- else {
- el.css('overflowY', 'hidden');
- }
- config.onResize.call(el);
- // Either animate or directly apply height:
- config.animate && !immediate ?
- el.stop(1,1).animate({
- height: scrollTop
- }, config.animate)
- : el.height(scrollTop);
- }
- },
- destroy: function() {
- this.unbind();
- this.el.removeData('AutoResizer');
- this.clones.remove();
- delete this.el;
- delete this.hClone;
- delete this.wClone;
- delete this.clones;
- },
- injectClone: function() {
- (
- autoResize.cloneContainer ||
- (autoResize.cloneContainer = $('<arclones/>').appendTo('body'))
- ).empty().append(this.clones); //this should be refactored so that a node is never cloned more than once
- }
- };
- })(jQuery);
- /**
- * SheetClip - Spreadsheet Clipboard Parser
- * version 0.2
- *
- * This tiny library transforms JavaScript arrays to strings that are pasteable by LibreOffice, OpenOffice,
- * Google Docs and Microsoft Excel.
- *
- * Copyright 2012, Marcin Warpechowski
- * Licensed under the MIT license.
- * http://github.com/warpech/sheetclip/
- */
- /*jslint white: true*/
- (function (global) {
- "use strict";
- function countQuotes(str) {
- return str.split('"').length - 1;
- }
- global.SheetClip = {
- parse: function (str) {
- var r, rlen, rows, arr = [], a = 0, c, clen, multiline, last;
- rows = str.split('\n');
- if (rows.length > 1 && rows[rows.length - 1] === '') {
- rows.pop();
- }
- for (r = 0, rlen = rows.length; r < rlen; r += 1) {
- rows[r] = rows[r].split('\t');
- for (c = 0, clen = rows[r].length; c < clen; c += 1) {
- if (!arr[a]) {
- arr[a] = [];
- }
- if (multiline && c === 0) {
- last = arr[a].length - 1;
- arr[a][last] = arr[a][last] + '\n' + rows[r][0];
- if (multiline && (countQuotes(rows[r][0]) & 1)) { //& 1 is a bitwise way of performing mod 2
- multiline = false;
- arr[a][last] = arr[a][last].substring(0, arr[a][last].length - 1).replace(/""/g, '"');
- }
- }
- else {
- if (c === clen - 1 && rows[r][c].indexOf('"') === 0) {
- arr[a].push(rows[r][c].substring(1).replace(/""/g, '"'));
- multiline = true;
- }
- else {
- arr[a].push(rows[r][c].replace(/""/g, '"'));
- multiline = false;
- }
- }
- }
- if (!multiline) {
- a += 1;
- }
- }
- return arr;
- },
- stringify: function (arr) {
- var r, rlen, c, clen, str = '', val;
- for (r = 0, rlen = arr.length; r < rlen; r += 1) {
- for (c = 0, clen = arr[r].length; c < clen; c += 1) {
- if (c > 0) {
- str += '\t';
- }
- val = arr[r][c];
- if (typeof val === 'string') {
- if (val.indexOf('\n') > -1) {
- str += '"' + val.replace(/"/g, '""') + '"';
- }
- else {
- str += val;
- }
- }
- else if (val === null || val === void 0) { //void 0 resolves to undefined
- str += '';
- }
- else {
- str += val;
- }
- }
- str += '\n';
- }
- return str;
- }
- };
- }(window));
- /**
- * CopyPaste.js
- * Creates a textarea that stays hidden on the page and gets focused when user presses CTRL while not having a form input focused
- * In future we may implement a better driver when better APIs are available
- * @constructor
- */
- var CopyPaste = (function () {
- var instance;
- return {
- getInstance: function () {
- if (!instance) {
- instance = new CopyPasteClass();
- }
- return instance;
- }
- };
- })();
- function CopyPasteClass() {
- var that = this
- , style
- , parent;
- this.copyCallbacks = [];
- this.cutCallbacks = [];
- this.pasteCallbacks = [];
- var listenerElement = document.documentElement;
- parent = document.body;
- if (document.getElementById('CopyPasteDiv')) {
- this.elDiv = document.getElementById('CopyPasteDiv');
- this.elTextarea = this.elDiv.firstChild;
- }
- else {
- this.elDiv = document.createElement('DIV');
- this.elDiv.id = 'CopyPasteDiv';
- style = this.elDiv.style;
- style.position = 'fixed';
- style.top = 0;
- style.left = 0;
- parent.appendChild(this.elDiv);
- this.elTextarea = document.createElement('TEXTAREA');
- this.elTextarea.className = 'copyPaste';
- style = this.elTextarea.style;
- style.width = '1px';
- style.height = '1px';
- this.elDiv.appendChild(this.elTextarea);
- if (typeof style.opacity !== 'undefined') {
- style.opacity = 0;
- }
- else {
- /*@cc_on @if (@_jscript)
- if(typeof style.filter === 'string') {
- style.filter = 'alpha(opacity=0)';
- }
- @end @*/
- }
- }
- this._bindEvent(listenerElement, 'keydown', function (event) {
- var isCtrlDown = false;
- if (event.metaKey) { //mac
- isCtrlDown = true;
- }
- else if (event.ctrlKey && navigator.userAgent.indexOf('Mac') === -1) { //pc
- isCtrlDown = true;
- }
- if (isCtrlDown) {
- if (document.activeElement !== that.elTextarea && (that.getSelectionText() != '' || ['INPUT', 'SELECT', 'TEXTAREA'].indexOf(document.activeElement.nodeName) != -1)) {
- return; //this is needed by fragmentSelection in Handsontable. Ignore copypaste.js behavior if fragment of cell text is selected
- }
- that.selectNodeText(that.elTextarea);
- setTimeout(function () {
- that.selectNodeText(that.elTextarea);
- }, 0);
- }
- /* 67 = c
- * 86 = v
- * 88 = x
- */
- if (isCtrlDown && (event.keyCode === 67 || event.keyCode === 86 || event.keyCode === 88)) {
- // that.selectNodeText(that.elTextarea);
- if (event.keyCode === 88) { //works in all browsers, incl. Opera < 12.12
- setTimeout(function () {
- that.triggerCut(event);
- }, 0);
- }
- else if (event.keyCode === 86) {
- setTimeout(function () {
- that.triggerPaste(event);
- }, 0);
- }
- }
- });
- }
- //http://jsperf.com/textara-selection
- //http://stackoverflow.com/questions/1502385/how-can-i-make-this-code-work-in-ie
- CopyPasteClass.prototype.selectNodeText = function (el) {
- el.select();
- };
- //http://stackoverflow.com/questions/5379120/get-the-highlighted-selected-text
- CopyPasteClass.prototype.getSelectionText = function () {
- var text = "";
- if (window.getSelection) {
- text = window.getSelection().toString();
- } else if (document.selection && document.selection.type != "Control") {
- text = document.selection.createRange().text;
- }
- return text;
- };
- CopyPasteClass.prototype.copyable = function (str) {
- if (typeof str !== 'string' && str.toString === void 0) {
- throw new Error('copyable requires string parameter');
- }
- this.elTextarea.value = str;
- };
- /*CopyPasteClass.prototype.onCopy = function (fn) {
- this.copyCallbacks.push(fn);
- };*/
- CopyPasteClass.prototype.onCut = function (fn) {
- this.cutCallbacks.push(fn);
- };
- CopyPasteClass.prototype.onPaste = function (fn) {
- this.pasteCallbacks.push(fn);
- };
- CopyPasteClass.prototype.removeCallback = function (fn) {
- var i, ilen;
- for (i = 0, ilen = this.copyCallbacks.length; i < ilen; i++) {
- if (this.copyCallbacks[i] === fn) {
- this.copyCallbacks.splice(i, 1);
- return true;
- }
- }
- for (i = 0, ilen = this.cutCallbacks.length; i < ilen; i++) {
- if (this.cutCallbacks[i] === fn) {
- this.cutCallbacks.splice(i, 1);
- return true;
- }
- }
- for (i = 0, ilen = this.pasteCallbacks.length; i < ilen; i++) {
- if (this.pasteCallbacks[i] === fn) {
- this.pasteCallbacks.splice(i, 1);
- return true;
- }
- }
- return false;
- };
- CopyPasteClass.prototype.triggerCut = function (event) {
- var that = this;
- if (that.cutCallbacks) {
- setTimeout(function () {
- for (var i = 0, ilen = that.cutCallbacks.length; i < ilen; i++) {
- that.cutCallbacks[i](event);
- }
- }, 50);
- }
- };
- CopyPasteClass.prototype.triggerPaste = function (event, str) {
- var that = this;
- if (that.pasteCallbacks) {
- setTimeout(function () {
- var val = (str || that.elTextarea.value).replace(/\n$/, ''); //remove trailing newline
- for (var i = 0, ilen = that.pasteCallbacks.length; i < ilen; i++) {
- that.pasteCallbacks[i](val, event);
- }
- }, 50);
- }
- };
- //old version used this:
- // - http://net.tutsplus.com/tutorials/javascript-ajax/javascript-from-null-cross-browser-event-binding/
- // - http://stackoverflow.com/questions/4643249/cross-browser-event-object-normalization
- //but that cannot work with jQuery.trigger
- CopyPasteClass.prototype._bindEvent = (function () {
- if (window.jQuery) { //if jQuery exists, use jQuery event (for compatibility with $.trigger and $.triggerHandler, which can only trigger jQuery events - and we use that in tests)
- return function (elem, type, cb) {
- $(elem).on(type + '.copypaste', cb);
- };
- }
- else {
- return function (elem, type, cb) {
- elem.addEventListener(type, cb, false); //sorry, IE8 will only work with jQuery
- };
- }
- })();
- // json-patch-duplex.js 0.3.5
- // (c) 2013 Joachim Wester
- // MIT license
- var jsonpatch;
- (function (jsonpatch) {
- var objOps = {
- add: function (obj, key) {
- obj[key] = this.value;
- return true;
- },
- remove: function (obj, key) {
- delete obj[key];
- return true;
- },
- replace: function (obj, key) {
- obj[key] = this.value;
- return true;
- },
- move: function (obj, key, tree) {
- var temp = { op: "_get", path: this.from };
- apply(tree, [temp]);
- apply(tree, [
- { op: "remove", path: this.from }
- ]);
- apply(tree, [
- { op: "add", path: this.path, value: temp.value }
- ]);
- return true;
- },
- copy: function (obj, key, tree) {
- var temp = { op: "_get", path: this.from };
- apply(tree, [temp]);
- apply(tree, [
- { op: "add", path: this.path, value: temp.value }
- ]);
- return true;
- },
- test: function (obj, key) {
- return (JSON.stringify(obj[key]) === JSON.stringify(this.value));
- },
- _get: function (obj, key) {
- this.value = obj[key];
- }
- };
- var arrOps = {
- add: function (arr, i) {
- arr.splice(i, 0, this.value);
- return true;
- },
- remove: function (arr, i) {
- arr.splice(i, 1);
- return true;
- },
- replace: function (arr, i) {
- arr[i] = this.value;
- return true;
- },
- move: objOps.move,
- copy: objOps.copy,
- test: objOps.test,
- _get: objOps._get
- };
- var observeOps = {
- 'new': function (patches, path) {
- var patch = {
- op: "add",
- path: path + escapePathComponent(this.name),
- value: this.object[this.name]
- };
- patches.push(patch);
- },
- deleted: function (patches, path) {
- var patch = {
- op: "remove",
- path: path + escapePathComponent(this.name)
- };
- patches.push(patch);
- },
- updated: function (patches, path) {
- var patch = {
- op: "replace",
- path: path + escapePathComponent(this.name),
- value: this.object[this.name]
- };
- patches.push(patch);
- }
- };
- function escapePathComponent(str) {
- if (str.indexOf('/') === -1 && str.indexOf('~') === -1)
- return str;
- return str.replace(/~/g, '~0').replace(/\//g, '~1');
- }
- function _getPathRecursive(root, obj) {
- var found;
- for (var key in root) {
- if (root.hasOwnProperty(key)) {
- if (root[key] === obj) {
- return escapePathComponent(key) + '/';
- } else if (typeof root[key] === 'object') {
- found = _getPathRecursive(root[key], obj);
- if (found != '') {
- return escapePathComponent(key) + '/' + found;
- }
- }
- }
- }
- return '';
- }
- function getPath(root, obj) {
- if (root === obj) {
- return '/';
- }
- var path = _getPathRecursive(root, obj);
- if (path === '') {
- throw new Error("Object not found in root");
- }
- return '/' + path;
- }
- var beforeDict = [];
- jsonpatch.intervals;
- var Mirror = (function () {
- function Mirror(obj) {
- this.observers = [];
- this.obj = obj;
- }
- return Mirror;
- })();
- var ObserverInfo = (function () {
- function ObserverInfo(callback, observer) {
- this.callback = callback;
- this.observer = observer;
- }
- return ObserverInfo;
- })();
- function getMirror(obj) {
- for (var i = 0, ilen = beforeDict.length; i < ilen; i++) {
- if (beforeDict[i].obj === obj) {
- return beforeDict[i];
- }
- }
- }
- function getObserverFromMirror(mirror, callback) {
- for (var j = 0, jlen = mirror.observers.length; j < jlen; j++) {
- if (mirror.observers[j].callback === callback) {
- return mirror.observers[j].observer;
- }
- }
- }
- function removeObserverFromMirror(mirror, observer) {
- for (var j = 0, jlen = mirror.observers.length; j < jlen; j++) {
- if (mirror.observers[j].observer === observer) {
- mirror.observers.splice(j, 1);
- return;
- }
- }
- }
- function unobserve(root, observer) {
- generate(observer);
- if (Object.observe) {
- _unobserve(observer, root);
- } else {
- clearTimeout(observer.next);
- }
- var mirror = getMirror(root);
- removeObserverFromMirror(mirror, observer);
- }
- jsonpatch.unobserve = unobserve;
- function observe(obj, callback) {
- var patches = [];
- var root = obj;
- var observer;
- var mirror = getMirror(obj);
- if (!mirror) {
- mirror = new Mirror(obj);
- beforeDict.push(mirror);
- } else {
- observer = getObserverFromMirror(mirror, callback);
- }
- if (observer) {
- return observer;
- }
- if (Object.observe) {
- observer = function (arr) {
- //This "refresh" is needed to begin observing new object properties
- _unobserve(observer, obj);
- _observe(observer, obj);
- var a = 0, alen = arr.length;
- while (a < alen) {
- if (!(arr[a].name === 'length' && _isArray(arr[a].object)) && !(arr[a].name === '__Jasmine_been_here_before__')) {
- observeOps[arr[a].type].call(arr[a], patches, getPath(root, arr[a].object));
- }
- a++;
- }
- if (patches) {
- if (callback) {
- callback(patches);
- }
- }
- observer.patches = patches;
- patches = [];
- };
- } else {
- observer = {};
- mirror.value = JSON.parse(JSON.stringify(obj));
- if (callback) {
- //callbacks.push(callback); this has no purpose
- observer.callback = callback;
- observer.next = null;
- var intervals = this.intervals || [100, 1000, 10000, 60000];
- var currentInterval = 0;
- var dirtyCheck = function () {
- generate(observer);
- };
- var fastCheck = function () {
- clearTimeout(observer.next);
- observer.next = setTimeout(function () {
- dirtyCheck();
- currentInterval = 0;
- observer.next = setTimeout(slowCheck, intervals[currentInterval++]);
- }, 0);
- };
- var slowCheck = function () {
- dirtyCheck();
- if (currentInterval == intervals.length)
- currentInterval = intervals.length - 1;
- observer.next = setTimeout(slowCheck, intervals[currentInterval++]);
- };
- if (typeof window !== 'undefined') {
- if (window.addEventListener) {
- window.addEventListener('mousedown', fastCheck);
- window.addEventListener('mouseup', fastCheck);
- window.addEventListener('keydown', fastCheck);
- } else {
- window.attachEvent('onmousedown', fastCheck);
- window.attachEvent('onmouseup', fastCheck);
- window.attachEvent('onkeydown', fastCheck);
- }
- }
- observer.next = setTimeout(slowCheck, intervals[currentInterval++]);
- }
- }
- observer.patches = patches;
- observer.object = obj;
- mirror.observers.push(new ObserverInfo(callback, observer));
- return _observe(observer, obj);
- }
- jsonpatch.observe = observe;
- /// Listen to changes on an object tree, accumulate patches
- function _observe(observer, obj) {
- if (Object.observe) {
- Object.observe(obj, observer);
- for (var key in obj) {
- if (obj.hasOwnProperty(key)) {
- var v = obj[key];
- if (v && typeof (v) === "object") {
- _observe(observer, v);
- }
- }
- }
- }
- return observer;
- }
- function _unobserve(observer, obj) {
- if (Object.observe) {
- Object.unobserve(obj, observer);
- for (var key in obj) {
- if (obj.hasOwnProperty(key)) {
- var v = obj[key];
- if (v && typeof (v) === "object") {
- _unobserve(observer, v);
- }
- }
- }
- }
- return observer;
- }
- function generate(observer) {
- if (Object.observe) {
- Object.deliverChangeRecords(observer);
- } else {
- var mirror;
- for (var i = 0, ilen = beforeDict.length; i < ilen; i++) {
- if (beforeDict[i].obj === observer.object) {
- mirror = beforeDict[i];
- break;
- }
- }
- _generate(mirror.value, observer.object, observer.patches, "");
- }
- var temp = observer.patches;
- if (temp.length > 0) {
- observer.patches = [];
- if (observer.callback) {
- observer.callback(temp);
- }
- }
- return temp;
- }
- jsonpatch.generate = generate;
- var _objectKeys;
- if (Object.keys) {
- _objectKeys = Object.keys;
- } else {
- _objectKeys = function (obj) {
- var keys = [];
- for (var o in obj) {
- if (obj.hasOwnProperty(o)) {
- keys.push(o);
- }
- }
- return keys;
- };
- }
- // Dirty check if obj is different from mirror, generate patches and update mirror
- function _generate(mirror, obj, patches, path) {
- var newKeys = _objectKeys(obj);
- var oldKeys = _objectKeys(mirror);
- var changed = false;
- var deleted = false;
- for (var t = oldKeys.length - 1; t >= 0; t--) {
- var key = oldKeys[t];
- var oldVal = mirror[key];
- if (obj.hasOwnProperty(key)) {
- var newVal = obj[key];
- if (oldVal instanceof Object) {
- _generate(oldVal, newVal, patches, path + "/" + escapePathComponent(key));
- } else {
- if (oldVal != newVal) {
- changed = true;
- patches.push({ op: "replace", path: path + "/" + escapePathComponent(key), value: newVal });
- mirror[key] = newVal;
- }
- }
- } else {
- patches.push({ op: "remove", path: path + "/" + escapePathComponent(key) });
- delete mirror[key];
- deleted = true;
- }
- }
- if (!deleted && newKeys.length == oldKeys.length) {
- return;
- }
- for (var t = 0; t < newKeys.length; t++) {
- var key = newKeys[t];
- if (!mirror.hasOwnProperty(key)) {
- patches.push({ op: "add", path: path + "/" + escapePathComponent(key), value: obj[key] });
- mirror[key] = JSON.parse(JSON.stringify(obj[key]));
- }
- }
- }
- var _isArray;
- if (Array.isArray) {
- _isArray = Array.isArray;
- } else {
- _isArray = function (obj) {
- return obj.push && typeof obj.length === 'number';
- };
- }
- /// Apply a json-patch operation on an object tree
- function apply(tree, patches) {
- var result = false, p = 0, plen = patches.length, patch;
- while (p < plen) {
- patch = patches[p];
- // Find the object
- var keys = patch.path.split('/');
- var obj = tree;
- var t = 1;
- var len = keys.length;
- while (true) {
- if (_isArray(obj)) {
- var index = parseInt(keys[t], 10);
- t++;
- if (t >= len) {
- result = arrOps[patch.op].call(patch, obj, index, tree);
- break;
- }
- obj = obj[index];
- } else {
- var key = keys[t];
- if (key.indexOf('~') != -1)
- key = key.replace(/~1/g, '/').replace(/~0/g, '~');
- t++;
- if (t >= len) {
- result = objOps[patch.op].call(patch, obj, key, tree);
- break;
- }
- obj = obj[key];
- }
- }
- p++;
- }
- return result;
- }
- jsonpatch.apply = apply;
- })(jsonpatch || (jsonpatch = {}));
- if (typeof exports !== "undefined") {
- exports.apply = jsonpatch.apply;
- exports.observe = jsonpatch.observe;
- exports.unobserve = jsonpatch.unobserve;
- exports.generate = jsonpatch.generate;
- }
- function WalkontableBorder(instance, settings) {
- var style;
- //reference to instance
- this.instance = instance;
- this.settings = settings;
- this.wtDom = this.instance.wtDom;
- this.main = document.createElement("div");
- style = this.main.style;
- style.position = 'absolute';
- style.top = 0;
- style.left = 0;
- // style.visibility = 'hidden';
- for (var i = 0; i < 5; i++) {
- var DIV = document.createElement('DIV');
- DIV.className = 'wtBorder ' + (settings.className || '');
- style = DIV.style;
- style.backgroundColor = settings.border.color;
- style.height = settings.border.width + 'px';
- style.width = settings.border.width + 'px';
- this.main.appendChild(DIV);
- }
- this.top = this.main.childNodes[0];
- this.left = this.main.childNodes[1];
- this.bottom = this.main.childNodes[2];
- this.right = this.main.childNodes[3];
- /*$(this.top).on(sss, function(event) {
- event.preventDefault();
- event.stopImmediatePropagation();
- $(this).hide();
- });
- $(this.left).on(sss, function(event) {
- event.preventDefault();
- event.stopImmediatePropagation();
- $(this).hide();
- });
- $(this.bottom).on(sss, function(event) {
- event.preventDefault();
- event.stopImmediatePropagation();
- $(this).hide();
- });
- $(this.right).on(sss, function(event) {
- event.preventDefault();
- event.stopImmediatePropagation();
- $(this).hide();
- });*/
- this.topStyle = this.top.style;
- this.leftStyle = this.left.style;
- this.bottomStyle = this.bottom.style;
- this.rightStyle = this.right.style;
- this.corner = this.main.childNodes[4];
- this.corner.className += ' corner';
- this.cornerStyle = this.corner.style;
- this.cornerStyle.width = '5px';
- this.cornerStyle.height = '5px';
- this.cornerStyle.border = '2px solid #FFF';
- this.disappear();
- if (!instance.wtTable.bordersHolder) {
- instance.wtTable.bordersHolder = document.createElement('div');
- instance.wtTable.bordersHolder.className = 'htBorders';
- instance.wtTable.hider.appendChild(instance.wtTable.bordersHolder);
- }
- instance.wtTable.bordersHolder.appendChild(this.main);
- var down = false;
- var $body = $(document.body);
- $body.on('mousedown.walkontable.' + instance.guid, function () {
- down = true;
- });
- $body.on('mouseup.walkontable.' + instance.guid, function () {
- down = false
- });
- $(this.main.childNodes).on('mouseenter', function (event) {
- if (!down || !instance.getSetting('hideBorderOnMouseDownOver')) {
- return;
- }
- event.preventDefault();
- event.stopImmediatePropagation();
- var bounds = this.getBoundingClientRect();
- var $this = $(this);
- $this.hide();
- var isOutside = function (event) {
- if (event.clientY < Math.floor(bounds.top)) {
- return true;
- }
- if (event.clientY > Math.ceil(bounds.top + bounds.height)) {
- return true;
- }
- if (event.clientX < Math.floor(bounds.left)) {
- return true;
- }
- if (event.clientX > Math.ceil(bounds.left + bounds.width)) {
- return true;
- }
- };
- $body.on('mousemove.border.' + instance.guid, function (event) {
- if (isOutside(event)) {
- $body.off('mousemove.border.' + instance.guid);
- $this.show();
- }
- });
- });
- }
- /**
- * Show border around one or many cells
- * @param {Array} corners
- */
- WalkontableBorder.prototype.appear = function (corners) {
- var isMultiple, fromTD, toTD, fromOffset, toOffset, containerOffset, top, minTop, left, minLeft, height, width;
- if (this.disabled) {
- return;
- }
- var instance = this.instance
- , fromRow
- , fromColumn
- , toRow
- , toColumn
- , hideTop = false
- , hideLeft = false
- , hideBottom = false
- , hideRight = false
- , i
- , ilen
- , s;
- if (!instance.wtTable.isRowInViewport(corners[0])) {
- hideTop = true;
- }
- if (!instance.wtTable.isRowInViewport(corners[2])) {
- hideBottom = true;
- }
- ilen = instance.wtTable.rowStrategy.countVisible();
- for (i = 0; i < ilen; i++) {
- s = instance.wtTable.rowFilter.visibleToSource(i);
- if (s >= corners[0] && s <= corners[2]) {
- fromRow = s;
- break;
- }
- }
- for (i = ilen - 1; i >= 0; i--) {
- s = instance.wtTable.rowFilter.visibleToSource(i);
- if (s >= corners[0] && s <= corners[2]) {
- toRow = s;
- break;
- }
- }
- if (hideTop && hideBottom) {
- hideLeft = true;
- hideRight = true;
- }
- else {
- if (!instance.wtTable.isColumnInViewport(corners[1])) {
- hideLeft = true;
- }
- if (!instance.wtTable.isColumnInViewport(corners[3])) {
- hideRight = true;
- }
- ilen = instance.wtTable.columnStrategy.countVisible();
- for (i = 0; i < ilen; i++) {
- s = instance.wtTable.columnFilter.visibleToSource(i);
- if (s >= corners[1] && s <= corners[3]) {
- fromColumn = s;
- break;
- }
- }
- for (i = ilen - 1; i >= 0; i--) {
- s = instance.wtTable.columnFilter.visibleToSource(i);
- if (s >= corners[1] && s <= corners[3]) {
- toColumn = s;
- break;
- }
- }
- }
- if (fromRow !== void 0 && fromColumn !== void 0) {
- isMultiple = (fromRow !== toRow || fromColumn !== toColumn);
- fromTD = instance.wtTable.getCell([fromRow, fromColumn]);
- toTD = isMultiple ? instance.wtTable.getCell([toRow, toColumn]) : fromTD;
- fromOffset = this.wtDom.offset(fromTD);
- toOffset = isMultiple ? this.wtDom.offset(toTD) : fromOffset;
- containerOffset = this.wtDom.offset(instance.wtTable.TABLE);
- minTop = fromOffset.top;
- height = toOffset.top + this.wtDom.outerHeight(toTD) - minTop;
- minLeft = fromOffset.left;
- width = toOffset.left + this.wtDom.outerWidth(toTD) - minLeft;
- top = minTop - containerOffset.top - 1;
- left = minLeft - containerOffset.left - 1;
- var style = this.wtDom.getComputedStyle(fromTD);
- if (parseInt(style['borderTopWidth'], 10) > 0) {
- top += 1;
- height = height > 0 ? height - 1 : 0;
- }
- if (parseInt(style['borderLeftWidth'], 10) > 0) {
- left += 1;
- width = width > 0 ? width - 1 : 0;
- }
- }
- else {
- this.disappear();
- return;
- }
- if (hideTop) {
- this.topStyle.display = 'none';
- }
- else {
- this.topStyle.top = top + 'px';
- this.topStyle.left = left + 'px';
- this.topStyle.width = width + 'px';
- this.topStyle.display = 'block';
- }
- if (hideLeft) {
- this.leftStyle.display = 'none';
- }
- else {
- this.leftStyle.top = top + 'px';
- this.leftStyle.left = left + 'px';
- this.leftStyle.height = height + 'px';
- this.leftStyle.display = 'block';
- }
- var delta = Math.floor(this.settings.border.width / 2);
- if (hideBottom) {
- this.bottomStyle.display = 'none';
- }
- else {
- this.bottomStyle.top = top + height - delta + 'px';
- this.bottomStyle.left = left + 'px';
- this.bottomStyle.width = width + 'px';
- this.bottomStyle.display = 'block';
- }
- if (hideRight) {
- this.rightStyle.display = 'none';
- }
- else {
- this.rightStyle.top = top + 'px';
- this.rightStyle.left = left + width - delta + 'px';
- this.rightStyle.height = height + 1 + 'px';
- this.rightStyle.display = 'block';
- }
- if (hideBottom || hideRight || !this.hasSetting(this.settings.border.cornerVisible)) {
- this.cornerStyle.display = 'none';
- }
- else {
- this.cornerStyle.top = top + height - 4 + 'px';
- this.cornerStyle.left = left + width - 4 + 'px';
- this.cornerStyle.display = 'block';
- }
- };
- /**
- * Hide border
- */
- WalkontableBorder.prototype.disappear = function () {
- this.topStyle.display = 'none';
- this.leftStyle.display = 'none';
- this.bottomStyle.display = 'none';
- this.rightStyle.display = 'none';
- this.cornerStyle.display = 'none';
- };
- WalkontableBorder.prototype.hasSetting = function (setting) {
- if (typeof setting === 'function') {
- return setting();
- }
- return !!setting;
- };
- /**
- * WalkontableCellFilter
- * @constructor
- */
- function WalkontableCellFilter() {
- this.offset = 0;
- this.total = 0;
- this.fixedCount = 0;
- }
- WalkontableCellFilter.prototype.source = function (n) {
- return n;
- };
- WalkontableCellFilter.prototype.offsetted = function (n) {
- return n + this.offset;
- };
- WalkontableCellFilter.prototype.unOffsetted = function (n) {
- return n - this.offset;
- };
- WalkontableCellFilter.prototype.fixed = function (n) {
- if (n < this.fixedCount) {
- return n - this.offset;
- }
- else {
- return n;
- }
- };
- WalkontableCellFilter.prototype.unFixed = function (n) {
- if (n < this.fixedCount) {
- return n + this.offset;
- }
- else {
- return n;
- }
- };
- WalkontableCellFilter.prototype.visibleToSource = function (n) {
- return this.source(this.offsetted(this.fixed(n)));
- };
- WalkontableCellFilter.prototype.sourceToVisible = function (n) {
- return this.source(this.unOffsetted(this.unFixed(n)));
- };
- /**
- * WalkontableCellStrategy
- * @constructor
- */
- function WalkontableCellStrategy() {
- }
- WalkontableCellStrategy.prototype.getSize = function (index) {
- return this.cellSizes[index];
- };
- WalkontableCellStrategy.prototype.getContainerSize = function (proposedSize) {
- return typeof this.containerSizeFn === 'function' ? this.containerSizeFn(proposedSize) : this.containerSizeFn;
- };
- WalkontableCellStrategy.prototype.countVisible = function () {
- return this.cellCount;
- };
- WalkontableCellStrategy.prototype.isLastIncomplete = function () {
- return this.remainingSize >= 0;
- };
- /**
- * WalkontableClassNameList
- * @constructor
- */
- function WalkontableClassNameCache() {
- this.cache = [];
- }
- WalkontableClassNameCache.prototype.add = function (r, c, cls) {
- if (!this.cache[r]) {
- this.cache[r] = [];
- }
- if (!this.cache[r][c]) {
- this.cache[r][c] = [];
- }
- this.cache[r][c][cls] = true;
- };
- WalkontableClassNameCache.prototype.test = function (r, c, cls) {
- return (this.cache[r] && this.cache[r][c] && this.cache[r][c][cls]);
- };
- /**
- * WalkontableColumnFilter
- * @constructor
- */
- function WalkontableColumnFilter() {
- this.countTH = 0;
- }
- WalkontableColumnFilter.prototype = new WalkontableCellFilter();
- WalkontableColumnFilter.prototype.readSettings = function (instance) {
- this.offset = instance.wtSettings.settings.offsetColumn;
- this.total = instance.getSetting('totalColumns');
- this.fixedCount = instance.getSetting('fixedColumnsLeft');
- this.countTH = instance.getSetting('rowHeaders').length;
- };
- WalkontableColumnFilter.prototype.offsettedTH = function (n) {
- return n - this.countTH;
- };
- WalkontableColumnFilter.prototype.unOffsettedTH = function (n) {
- return n + this.countTH;
- };
- WalkontableColumnFilter.prototype.visibleRowHeadedColumnToSourceColumn = function (n) {
- return this.visibleToSource(this.offsettedTH(n));
- };
- WalkontableColumnFilter.prototype.sourceColumnToVisibleRowHeadedColumn = function (n) {
- return this.unOffsettedTH(this.sourceToVisible(n));
- };
- /**
- * WalkontableColumnStrategy
- * @param containerSizeFn
- * @param sizeAtIndex
- * @param strategy - all, last, none
- * @constructor
- */
- function WalkontableColumnStrategy(containerSizeFn, sizeAtIndex, strategy) {
- var size
- , i = 0;
- this.containerSizeFn = containerSizeFn;
- this.cellSizesSum = 0;
- this.cellSizes = [];
- this.cellStretch = [];
- this.cellCount = 0;
- this.remainingSize = 0;
- this.strategy = strategy;
- //step 1 - determine cells that fit containerSize and cache their widths
- while (true) {
- size = sizeAtIndex(i);
- if (size === void 0) {
- break; //total columns exceeded
- }
- if (this.cellSizesSum >= this.getContainerSize(this.cellSizesSum + size)) {
- break; //total width exceeded
- }
- this.cellSizes.push(size);
- this.cellSizesSum += size;
- this.cellCount++;
- i++;
- }
- var containerSize = this.getContainerSize(this.cellSizesSum);
- this.remainingSize = this.cellSizesSum - containerSize;
- //negative value means the last cell is fully visible and there is some space left for stretching
- //positive value means the last cell is not fully visible
- }
- WalkontableColumnStrategy.prototype = new WalkontableCellStrategy();
- WalkontableColumnStrategy.prototype.getSize = function (index) {
- return this.cellSizes[index] + (this.cellStretch[index] || 0);
- };
- WalkontableColumnStrategy.prototype.stretch = function () {
- //step 2 - apply stretching strategy
- var containerSize = this.getContainerSize(this.cellSizesSum)
- , i = 0;
- this.remainingSize = this.cellSizesSum - containerSize;
- this.cellStretch.length = 0; //clear previous stretch
- if (this.strategy === 'all') {
- if (this.remainingSize < 0) {
- var ratio = containerSize / this.cellSizesSum;
- var newSize;
- while (i < this.cellCount - 1) { //"i < this.cellCount - 1" is needed because last cellSize is adjusted after the loop
- newSize = Math.floor(ratio * this.cellSizes[i]);
- this.remainingSize += newSize - this.cellSizes[i];
- this.cellStretch[i] = newSize - this.cellSizes[i];
- i++;
- }
- this.cellStretch[this.cellCount - 1] = -this.remainingSize;
- this.remainingSize = 0;
- }
- }
- else if (this.strategy === 'last') {
- if (this.remainingSize < 0) {
- this.cellStretch[this.cellCount - 1] = -this.remainingSize;
- this.remainingSize = 0;
- }
- }
- };
- function Walkontable(settings) {
- var that = this,
- originalHeaders = [];
- this.guid = 'wt_' + walkontableRandomString(); //this is the namespace for global events
- //bootstrap from settings
- this.wtDom = new WalkontableDom();
- if (settings.cloneFrom) {
- this.cloneFrom = settings.cloneFrom;
- this.cloneDirection = settings.cloneDirection;
- this.wtSettings = settings.cloneFrom.wtSettings;
- this.wtTable = new WalkontableTable(this, settings.table);
- this.wtScroll = new WalkontableScroll(this);
- this.wtViewport = settings.cloneFrom.wtViewport;
- }
- else {
- this.wtSettings = new WalkontableSettings(this, settings);
- this.wtTable = new WalkontableTable(this, settings.table);
- this.wtScroll = new WalkontableScroll(this);
- this.wtViewport = new WalkontableViewport(this);
- this.wtScrollbars = new WalkontableScrollbars(this);
- this.wtWheel = new WalkontableWheel(this);
- this.wtEvent = new WalkontableEvent(this);
- }
- //find original headers
- if (this.wtTable.THEAD.childNodes.length && this.wtTable.THEAD.childNodes[0].childNodes.length) {
- for (var c = 0, clen = this.wtTable.THEAD.childNodes[0].childNodes.length; c < clen; c++) {
- originalHeaders.push(this.wtTable.THEAD.childNodes[0].childNodes[c].innerHTML);
- }
- if (!this.getSetting('columnHeaders').length) {
- this.update('columnHeaders', [function (column, TH) {
- that.wtDom.fastInnerText(TH, originalHeaders[column]);
- }]);
- }
- }
- //initialize selections
- this.selections = {};
- var selectionsSettings = this.getSetting('selections');
- if (selectionsSettings) {
- for (var i in selectionsSettings) {
- if (selectionsSettings.hasOwnProperty(i)) {
- this.selections[i] = new WalkontableSelection(this, selectionsSettings[i]);
- }
- }
- }
- this.drawn = false;
- this.drawInterrupted = false;
- if (window.Handsontable) {
- Handsontable.PluginHooks.add('beforeChange', function () {
- if (that.rowHeightCache) {
- that.rowHeightCache.length = 0;
- }
- });
- }
- }
- Walkontable.prototype.draw = function (selectionsOnly) {
- this.drawInterrupted = false;
- if (!selectionsOnly && !this.wtDom.isVisible(this.wtTable.TABLE)) {
- this.drawInterrupted = true; //draw interrupted because TABLE is not visible
- return;
- }
- this.getSetting('beforeDraw', !selectionsOnly);
- selectionsOnly = selectionsOnly && this.getSetting('offsetRow') === this.lastOffsetRow && this.getSetting('offsetColumn') === this.lastOffsetColumn;
- if (this.drawn) { //fix offsets that might have changed
- this.scrollVertical(0);
- this.scrollHorizontal(0);
- }
- this.lastOffsetRow = this.getSetting('offsetRow');
- this.lastOffsetColumn = this.getSetting('offsetColumn');
- this.wtTable.draw(selectionsOnly);
- this.getSetting('onDraw', !selectionsOnly);
- return this;
- };
- Walkontable.prototype.update = function (settings, value) {
- return this.wtSettings.update(settings, value);
- };
- Walkontable.prototype.scrollVertical = function (delta) {
- return this.wtScroll.scrollVertical(delta);
- };
- Walkontable.prototype.scrollHorizontal = function (delta) {
- return this.wtScroll.scrollHorizontal(delta);
- };
- Walkontable.prototype.scrollViewport = function (coords) {
- this.wtScroll.scrollViewport(coords);
- return this;
- };
- Walkontable.prototype.getViewport = function () {
- return [
- this.wtTable.rowFilter.visibleToSource(0),
- this.wtTable.columnFilter.visibleToSource(0),
- this.wtTable.getLastVisibleRow(),
- this.wtTable.getLastVisibleColumn()
- ];
- };
- Walkontable.prototype.getSetting = function (key, param1, param2, param3) {
- return this.wtSettings.getSetting(key, param1, param2, param3);
- };
- Walkontable.prototype.hasSetting = function (key) {
- return this.wtSettings.has(key);
- };
- Walkontable.prototype.destroy = function () {
- $(document.body).off('.' + this.guid);
- this.wtScrollbars.destroy();
- clearTimeout(this.wheelTimeout);
- this.wtEvent && this.wtEvent.destroy();
- };
- function WalkontableDom() {
- }
- //goes up the DOM tree (including given element) until it finds an element that matches the nodeName
- WalkontableDom.prototype.closest = function (elem, nodeNames, until) {
- while (elem != null && elem !== until) {
- if (elem.nodeType === 1 && nodeNames.indexOf(elem.nodeName) > -1) {
- return elem;
- }
- elem = elem.parentNode;
- }
- return null;
- };
- //goes up the DOM tree and checks if element is child of another element
- WalkontableDom.prototype.isChildOf = function (child, parent) {
- var node = child.parentNode;
- while (node != null) {
- if (node == parent) {
- return true;
- }
- node = node.parentNode;
- }
- return false;
- };
- /**
- * Counts index of element within its parent
- * WARNING: for performance reasons, assumes there are only element nodes (no text nodes). This is true for Walkotnable
- * Otherwise would need to check for nodeType or use previousElementSibling
- * @see http://jsperf.com/sibling-index/10
- * @param {Element} elem
- * @return {Number}
- */
- WalkontableDom.prototype.index = function (elem) {
- var i = 0;
- while (elem = elem.previousSibling) {
- ++i
- }
- return i;
- };
- if (document.documentElement.classList) {
- // HTML5 classList API
- WalkontableDom.prototype.hasClass = function (ele, cls) {
- return ele.classList.contains(cls);
- };
- WalkontableDom.prototype.addClass = function (ele, cls) {
- ele.classList.add(cls);
- };
- WalkontableDom.prototype.removeClass = function (ele, cls) {
- ele.classList.remove(cls);
- };
- }
- else {
- //http://snipplr.com/view/3561/addclass-removeclass-hasclass/
- WalkontableDom.prototype.hasClass = function (ele, cls) {
- return ele.className.match(new RegExp('(\\s|^)' + cls + '(\\s|$)'));
- };
- WalkontableDom.prototype.addClass = function (ele, cls) {
- if (!this.hasClass(ele, cls)) ele.className += " " + cls;
- };
- WalkontableDom.prototype.removeClass = function (ele, cls) {
- if (this.hasClass(ele, cls)) { //is this really needed?
- var reg = new RegExp('(\\s|^)' + cls + '(\\s|$)');
- ele.className = ele.className.replace(reg, ' ').trim(); //String.prototype.trim is defined in polyfill.js
- }
- };
- }
- /*//http://net.tutsplus.com/tutorials/javascript-ajax/javascript-from-null-cross-browser-event-binding/
- WalkontableDom.prototype.addEvent = (function () {
- var that = this;
- if (document.addEventListener) {
- return function (elem, type, cb) {
- if ((elem && !elem.length) || elem === window) {
- elem.addEventListener(type, cb, false);
- }
- else if (elem && elem.length) {
- var len = elem.length;
- for (var i = 0; i < len; i++) {
- that.addEvent(elem[i], type, cb);
- }
- }
- };
- }
- else {
- return function (elem, type, cb) {
- if ((elem && !elem.length) || elem === window) {
- elem.attachEvent('on' + type, function () {
- //normalize
- //http://stackoverflow.com/questions/4643249/cross-browser-event-object-normalization
- var e = window['event'];
- e.target = e.srcElement;
- //e.offsetX = e.layerX;
- //e.offsetY = e.layerY;
- e.relatedTarget = e.relatedTarget || e.type == 'mouseover' ? e.fromElement : e.toElement;
- if (e.target.nodeType === 3) e.target = e.target.parentNode; //Safari bug
- return cb.call(elem, e)
- });
- }
- else if (elem.length) {
- var len = elem.length;
- for (var i = 0; i < len; i++) {
- that.addEvent(elem[i], type, cb);
- }
- }
- };
- }
- })();
- WalkontableDom.prototype.triggerEvent = function (element, eventName, target) {
- var event;
- if (document.createEvent) {
- event = document.createEvent("MouseEvents");
- event.initEvent(eventName, true, true);
- } else {
- event = document.createEventObject();
- event.eventType = eventName;
- }
- event.eventName = eventName;
- event.target = target;
- if (document.createEvent) {
- target.dispatchEvent(event);
- } else {
- target.fireEvent("on" + event.eventType, event);
- }
- };*/
- WalkontableDom.prototype.removeTextNodes = function (elem, parent) {
- if (elem.nodeType === 3) {
- parent.removeChild(elem); //bye text nodes!
- }
- else if (['TABLE', 'THEAD', 'TBODY', 'TFOOT', 'TR'].indexOf(elem.nodeName) > -1) {
- var childs = elem.childNodes;
- for (var i = childs.length - 1; i >= 0; i--) {
- this.removeTextNodes(childs[i], elem);
- }
- }
- };
- /**
- * Remove childs function
- * WARNING - this doesn't unload events and data attached by jQuery
- * http://jsperf.com/jquery-html-vs-empty-vs-innerhtml/9
- * http://jsperf.com/jquery-html-vs-empty-vs-innerhtml/11 - no siginificant improvement with Chrome remove() method
- * @param element
- * @returns {void}
- */
- //
- WalkontableDom.prototype.empty = function (element) {
- var child;
- while (child = element.lastChild) {
- element.removeChild(child);
- }
- };
- WalkontableDom.prototype.HTML_CHARACTERS = /(<(.*)>|&(.*);)/;
- /**
- * Insert content into element trying avoid innerHTML method.
- * @return {void}
- */
- WalkontableDom.prototype.fastInnerHTML = function (element, content) {
- if (this.HTML_CHARACTERS.test(content)) {
- element.innerHTML = content;
- }
- else {
- this.fastInnerText(element, content);
- }
- };
- /**
- * Insert text content into element
- * @return {void}
- */
- if (document.createTextNode('test').textContent) { //STANDARDS
- WalkontableDom.prototype.fastInnerText = function (element, content) {
- var child = element.firstChild;
- if (child && child.nodeType === 3 && child.nextSibling === null) {
- //fast lane - replace existing text node
- //http://jsperf.com/replace-text-vs-reuse
- child.textContent = content;
- }
- else {
- //slow lane - empty element and insert a text node
- this.empty(element);
- element.appendChild(document.createTextNode(content));
- }
- };
- }
- else { //IE8
- WalkontableDom.prototype.fastInnerText = function (element, content) {
- var child = element.firstChild;
- if (child && child.nodeType === 3 && child.nextSibling === null) {
- //fast lane - replace existing text node
- //http://jsperf.com/replace-text-vs-reuse
- child.data = content;
- }
- else {
- //slow lane - empty element and insert a text node
- this.empty(element);
- element.appendChild(document.createTextNode(content));
- }
- };
- }
- /**
- * Returns true/false depending if element has offset parent
- * @param elem
- * @returns {boolean}
- */
- /*if (document.createTextNode('test').textContent) { //STANDARDS
- WalkontableDom.prototype.hasOffsetParent = function (elem) {
- return !!elem.offsetParent;
- }
- }
- else {
- WalkontableDom.prototype.hasOffsetParent = function (elem) {
- try {
- if (!elem.offsetParent) {
- return false;
- }
- }
- catch (e) {
- return false; //IE8 throws "Unspecified error" when offsetParent is not found - we catch it here
- }
- return true;
- }
- }*/
- /**
- * Returns true if element is attached to the DOM and visible, false otherwise
- * @param elem
- * @returns {boolean}
- */
- WalkontableDom.prototype.isVisible = function (elem) {
- //fast method according to benchmarks, but requires layout so slow in our case
- /*
- if (!WalkontableDom.prototype.hasOffsetParent(elem)) {
- return false; //fixes problem with UI Bootstrap <tabs> directive
- }
- // if (elem.offsetWidth > 0 || (elem.parentNode && elem.parentNode.offsetWidth > 0)) { //IE10 was mistaken here
- if (elem.offsetWidth > 0) {
- return true;
- }
- */
- //slow method
- var next = elem;
- while (next !== document.documentElement) { //until <html> reached
- if (next === null) { //parent detached from DOM
- return false;
- }
- else if (next.nodeType === 11) { //nodeType == 1 -> DOCUMENT_FRAGMENT_NODE
- return false;
- }
- else if (next.style.display === 'none') {
- return false;
- }
- next = next.parentNode;
- }
- return true;
- };
- /**
- * Returns elements top and left offset relative to the document. In our usage case compatible with jQuery but 2x faster
- * @param {HTMLElement} elem
- * @return {Object}
- */
- WalkontableDom.prototype.offset = function (elem) {
- if (this.hasCaptionProblem() && elem.firstChild && elem.firstChild.nodeName === 'CAPTION') {
- //fixes problem with Firefox ignoring <caption> in TABLE offset (see also WalkontableDom.prototype.outerHeight)
- //http://jsperf.com/offset-vs-getboundingclientrect/8
- var box = elem.getBoundingClientRect();
- return {
- top: box.top + (window.pageYOffset || document.documentElement.scrollTop) - (document.documentElement.clientTop || 0),
- left: box.left + (window.pageXOffset || document.documentElement.scrollLeft) - (document.documentElement.clientLeft || 0)
- };
- }
- var offsetLeft = elem.offsetLeft
- , offsetTop = elem.offsetTop
- , lastElem = elem;
- while (elem = elem.offsetParent) {
- if (elem === document.body) { //from my observation, document.body always has scrollLeft/scrollTop == 0
- break;
- }
- offsetLeft += elem.offsetLeft;
- offsetTop += elem.offsetTop;
- lastElem = elem;
- }
- if (lastElem && lastElem.style.position === 'fixed') { //slow - http://jsperf.com/offset-vs-getboundingclientrect/6
- //if(lastElem !== document.body) { //faster but does gives false positive in Firefox
- offsetLeft += window.pageXOffset || document.documentElement.scrollLeft;
- offsetTop += window.pageYOffset || document.documentElement.scrollTop;
- }
- return {
- left: offsetLeft,
- top: offsetTop
- };
- };
- WalkontableDom.prototype.getComputedStyle = function (elem) {
- return elem.currentStyle || document.defaultView.getComputedStyle(elem);
- };
- WalkontableDom.prototype.outerWidth = function (elem) {
- return elem.offsetWidth;
- };
- WalkontableDom.prototype.outerHeight = function (elem) {
- if (this.hasCaptionProblem() && elem.firstChild && elem.firstChild.nodeName === 'CAPTION') {
- //fixes problem with Firefox ignoring <caption> in TABLE.offsetHeight
- //jQuery (1.10.1) still has this unsolved
- //may be better to just switch to getBoundingClientRect
- //http://bililite.com/blog/2009/03/27/finding-the-size-of-a-table/
- //http://lists.w3.org/Archives/Public/www-style/2009Oct/0089.html
- //http://bugs.jquery.com/ticket/2196
- //http://lists.w3.org/Archives/Public/www-style/2009Oct/0140.html#start140
- return elem.offsetHeight + elem.firstChild.offsetHeight;
- }
- else {
- return elem.offsetHeight;
- }
- };
- (function () {
- var hasCaptionProblem;
- function detectCaptionProblem() {
- var TABLE = document.createElement('TABLE');
- TABLE.style.borderSpacing = 0;
- TABLE.style.borderWidth = 0;
- TABLE.style.padding = 0;
- var TBODY = document.createElement('TBODY');
- TABLE.appendChild(TBODY);
- TBODY.appendChild(document.createElement('TR'));
- TBODY.firstChild.appendChild(document.createElement('TD'));
- TBODY.firstChild.firstChild.innerHTML = '<tr><td>t<br>t</td></tr>';
- var CAPTION = document.createElement('CAPTION');
- CAPTION.innerHTML = 'c<br>c<br>c<br>c';
- CAPTION.style.padding = 0;
- CAPTION.style.margin = 0;
- TABLE.insertBefore(CAPTION, TBODY);
- document.body.appendChild(TABLE);
- hasCaptionProblem = (TABLE.offsetHeight < 2 * TABLE.lastChild.offsetHeight); //boolean
- document.body.removeChild(TABLE);
- }
- WalkontableDom.prototype.hasCaptionProblem = function () {
- if (hasCaptionProblem === void 0) {
- detectCaptionProblem();
- }
- return hasCaptionProblem;
- };
- /**
- * Returns caret position in text input
- * @author http://stackoverflow.com/questions/263743/how-to-get-caret-position-in-textarea
- * @return {Number}
- */
- WalkontableDom.prototype.getCaretPosition = function (el) {
- if (el.selectionStart) {
- return el.selectionStart;
- }
- else if (document.selection) { //IE8
- el.focus();
- var r = document.selection.createRange();
- if (r == null) {
- return 0;
- }
- var re = el.createTextRange(),
- rc = re.duplicate();
- re.moveToBookmark(r.getBookmark());
- rc.setEndPoint('EndToStart', re);
- return rc.text.length;
- }
- return 0;
- };
- /**
- * Sets caret position in text input
- * @author http://blog.vishalon.net/index.php/javascript-getting-and-setting-caret-position-in-textarea/
- * @param {Element} el
- * @param {Number} pos
- * @param {Number} endPos
- */
- WalkontableDom.prototype.setCaretPosition = function (el, pos, endPos) {
- if (endPos === void 0) {
- endPos = pos;
- }
- if (el.setSelectionRange) {
- el.focus();
- el.setSelectionRange(pos, endPos);
- }
- else if (el.createTextRange) { //IE8
- var range = el.createTextRange();
- range.collapse(true);
- range.moveEnd('character', endPos);
- range.moveStart('character', pos);
- range.select();
- }
- };
- })();
- function WalkontableEvent(instance) {
- var that = this;
- //reference to instance
- this.instance = instance;
- this.wtDom = this.instance.wtDom;
- var dblClickOrigin = [null, null];
- var dblClickTimeout = [null, null];
- var onMouseDown = function (event) {
- var cell = that.parentCell(event.target);
- if (that.wtDom.hasClass(event.target, 'corner')) {
- that.instance.getSetting('onCellCornerMouseDown', event, event.target);
- }
- else if (cell.TD && cell.TD.nodeName === 'TD') {
- if (that.instance.hasSetting('onCellMouseDown')) {
- that.instance.getSetting('onCellMouseDown', event, cell.coords, cell.TD);
- }
- }
- if (event.button !== 2) { //if not right mouse button
- if (cell.TD && cell.TD.nodeName === 'TD') {
- dblClickOrigin[0] = cell.TD;
- clearTimeout(dblClickTimeout[0]);
- dblClickTimeout[0] = setTimeout(function () {
- dblClickOrigin[0] = null;
- }, 1000);
- }
- }
- };
- var lastMouseOver;
- var onMouseOver = function (event) {
- if (that.instance.hasSetting('onCellMouseOver')) {
- var TABLE = that.instance.wtTable.TABLE;
- var TD = that.wtDom.closest(event.target, ['TD', 'TH'], TABLE);
- if (TD && TD !== lastMouseOver && that.wtDom.isChildOf(TD, TABLE)) {
- lastMouseOver = TD;
- if (TD.nodeName === 'TD') {
- that.instance.getSetting('onCellMouseOver', event, that.instance.wtTable.getCoords(TD), TD);
- }
- }
- }
- };
- /* var lastMouseOut;
- var onMouseOut = function (event) {
- if (that.instance.hasSetting('onCellMouseOut')) {
- var TABLE = that.instance.wtTable.TABLE;
- var TD = that.wtDom.closest(event.target, ['TD', 'TH'], TABLE);
- if (TD && TD !== lastMouseOut && that.wtDom.isChildOf(TD, TABLE)) {
- lastMouseOut = TD;
- if (TD.nodeName === 'TD') {
- that.instance.getSetting('onCellMouseOut', event, that.instance.wtTable.getCoords(TD), TD);
- }
- }
- }
- };*/
- var onMouseUp = function (event) {
- if (event.button !== 2) { //if not right mouse button
- var cell = that.parentCell(event.target);
- if (cell.TD === dblClickOrigin[0] && cell.TD === dblClickOrigin[1]) {
- if (that.wtDom.hasClass(event.target, 'corner')) {
- that.instance.getSetting('onCellCornerDblClick', event, cell.coords, cell.TD);
- }
- else if (cell.TD) {
- that.instance.getSetting('onCellDblClick', event, cell.coords, cell.TD);
- }
- dblClickOrigin[0] = null;
- dblClickOrigin[1] = null;
- }
- else if (cell.TD === dblClickOrigin[0]) {
- dblClickOrigin[1] = cell.TD;
- clearTimeout(dblClickTimeout[1]);
- dblClickTimeout[1] = setTimeout(function () {
- dblClickOrigin[1] = null;
- }, 1000);
- }
- }
- };
- $(this.instance.wtTable.holder).on('mousedown', onMouseDown);
- $(this.instance.wtTable.TABLE).on('mouseover', onMouseOver);
- // $(this.instance.wtTable.TABLE).on('mouseout', onMouseOut);
- $(this.instance.wtTable.holder).on('mouseup', onMouseUp);
- }
- WalkontableEvent.prototype.parentCell = function (elem) {
- var cell = {};
- var TABLE = this.instance.wtTable.TABLE;
- var TD = this.wtDom.closest(elem, ['TD', 'TH'], TABLE);
- if (TD && this.wtDom.isChildOf(TD, TABLE)) {
- cell.coords = this.instance.wtTable.getCoords(TD);
- cell.TD = TD;
- }
- else if (this.wtDom.hasClass(elem, 'wtBorder') && this.wtDom.hasClass(elem, 'current')) {
- cell.coords = this.instance.selections.current.selected[0];
- cell.TD = this.instance.wtTable.getCell(cell.coords);
- }
- return cell;
- };
- WalkontableEvent.prototype.destroy = function () {
- clearTimeout(this.dblClickTimeout0);
- clearTimeout(this.dblClickTimeout1);
- };
- function walkontableRangesIntersect() {
- var from = arguments[0];
- var to = arguments[1];
- for (var i = 1, ilen = arguments.length / 2; i < ilen; i++) {
- if (from <= arguments[2 * i + 1] && to >= arguments[2 * i]) {
- return true;
- }
- }
- return false;
- }
- /**
- * Generates a random hex string. Used as namespace for Walkontable instance events.
- * @return {String} - 16 character random string: "92b1bfc74ec4"
- */
- function walkontableRandomString() {
- function s4() {
- return Math.floor((1 + Math.random()) * 0x10000)
- .toString(16)
- .substring(1);
- }
- return s4() + s4() + s4() + s4();
- }
- var cachedScrollbarWidth;
- //http://stackoverflow.com/questions/986937/how-can-i-get-the-browsers-scrollbar-sizes
- function walkontableCalculateScrollbarWidth() {
- var inner = document.createElement('p');
- inner.style.width = "100%";
- inner.style.height = "200px";
- var outer = document.createElement('div');
- outer.style.position = "absolute";
- outer.style.top = "0px";
- outer.style.left = "0px";
- outer.style.visibility = "hidden";
- outer.style.width = "200px";
- outer.style.height = "150px";
- outer.style.overflow = "hidden";
- outer.appendChild(inner);
- document.body.appendChild(outer);
- var w1 = inner.offsetWidth;
- outer.style.overflow = 'scroll';
- var w2 = inner.offsetWidth;
- if (w1 == w2) w2 = outer.clientWidth;
- document.body.removeChild(outer);
- return (w1 - w2);
- }
- function walkontableGetScrollbarWidth() {
- if (cachedScrollbarWidth === void 0) {
- cachedScrollbarWidth = walkontableCalculateScrollbarWidth();
- }
- return cachedScrollbarWidth;
- }
- //http://stackoverflow.com/questions/3629183/why-doesnt-indexof-work-on-an-array-ie8
- if (!Array.prototype.indexOf) {
- Array.prototype.indexOf = function (elt /*, from*/) {
- var len = this.length >>> 0;
- var from = Number(arguments[1]) || 0;
- from = (from < 0)
- ? Math.ceil(from)
- : Math.floor(from);
- if (from < 0)
- from += len;
- for (; from < len; from++) {
- if (from in this &&
- this[from] === elt)
- return from;
- }
- return -1;
- };
- }
- /**
- * http://notes.jetienne.com/2011/05/18/cancelRequestAnimFrame-for-paul-irish-requestAnimFrame.html
- */
- window.requestAnimFrame = (function () {
- return window.requestAnimationFrame ||
- window.webkitRequestAnimationFrame ||
- window.mozRequestAnimationFrame ||
- window.oRequestAnimationFrame ||
- window.msRequestAnimationFrame ||
- function (/* function */ callback, /* DOMElement */ element) {
- return window.setTimeout(callback, 1000 / 60);
- };
- })();
- window.cancelRequestAnimFrame = (function () {
- return window.cancelAnimationFrame ||
- window.webkitCancelRequestAnimationFrame ||
- window.mozCancelRequestAnimationFrame ||
- window.oCancelRequestAnimationFrame ||
- window.msCancelRequestAnimationFrame ||
- clearTimeout
- })();
- //http://snipplr.com/view/13523/
- //modified for speed
- //http://jsperf.com/getcomputedstyle-vs-style-vs-css/8
- if (!window.getComputedStyle) {
- (function () {
- var elem;
- var styleObj = {
- getPropertyValue: function getPropertyValue(prop) {
- if (prop == 'float') prop = 'styleFloat';
- return elem.currentStyle[prop.toUpperCase()] || null;
- }
- };
- window.getComputedStyle = function (el) {
- elem = el;
- return styleObj;
- }
- })();
- }
- /**
- * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/Trim
- */
- if (!String.prototype.trim) {
- var trimRegex = /^\s+|\s+$/g;
- String.prototype.trim = function () {
- return this.replace(trimRegex, '');
- };
- }
- /**
- * WalkontableRowFilter
- * @constructor
- */
- function WalkontableRowFilter() {
- }
- WalkontableRowFilter.prototype = new WalkontableCellFilter();
- WalkontableRowFilter.prototype.readSettings = function (instance) {
- this.offset = instance.wtSettings.settings.offsetRow;
- this.total = instance.getSetting('totalRows');
- this.fixedCount = instance.getSetting('fixedRowsTop');
- };
- /**
- * WalkontableRowStrategy
- * @param containerSizeFn
- * @param sizeAtIndex
- * @constructor
- */
- function WalkontableRowStrategy(containerSizeFn, sizeAtIndex) {
- this.containerSizeFn = containerSizeFn;
- this.sizeAtIndex = sizeAtIndex;
- this.cellSizesSum = 0;
- this.cellSizes = [];
- this.cellCount = 0;
- this.remainingSize = -Infinity;
- }
- WalkontableRowStrategy.prototype = new WalkontableCellStrategy();
- WalkontableRowStrategy.prototype.add = function (i, TD, reverse) {
- if (!this.isLastIncomplete()) {
- var size = this.sizeAtIndex(i, TD);
- if (size === void 0) {
- return false; //total rows exceeded
- }
- var containerSize = this.getContainerSize(this.cellSizesSum + size);
- if (reverse) {
- this.cellSizes.unshift(size);
- }
- else {
- this.cellSizes.push(size);
- }
- this.cellSizesSum += size;
- this.cellCount++;
- this.remainingSize = this.cellSizesSum - containerSize;
- if (reverse && this.isLastIncomplete()) { //something is outside of the screen, maybe even some full rows?
- return false;
- }
- return true;
- }
- return false;
- };
- WalkontableRowStrategy.prototype.remove = function () {
- var size = this.cellSizes.pop();
- this.cellSizesSum -= size;
- this.cellCount--;
- this.remainingSize -= size;
- };
- WalkontableRowStrategy.prototype.removeOutstanding = function () {
- while (this.cellCount > 0 && this.cellSizes[this.cellCount - 1] < this.remainingSize) { //this row is completely off screen!
- this.remove();
- }
- };
- function WalkontableScroll(instance) {
- this.instance = instance;
- }
- WalkontableScroll.prototype.scrollVertical = function (delta) {
- if (!this.instance.drawn) {
- throw new Error('scrollVertical can only be called after table was drawn to DOM');
- }
- var instance = this.instance
- , newOffset
- , offset = instance.getSetting('offsetRow')
- , fixedCount = instance.getSetting('fixedRowsTop')
- , total = instance.getSetting('totalRows')
- , maxSize = instance.wtViewport.getViewportHeight();
- if (total > 0) {
- newOffset = this.scrollLogicVertical(delta, offset, total, fixedCount, maxSize, function (row) {
- if (row - offset < fixedCount && row - offset >= 0) {
- return instance.getSetting('rowHeight', row - offset);
- }
- else {
- return instance.getSetting('rowHeight', row);
- }
- }, function (isReverse) {
- instance.wtTable.verticalRenderReverse = isReverse;
- });
- }
- else {
- newOffset = 0;
- }
- if (newOffset !== offset) {
- this.instance.wtScrollbars.vertical.scrollTo(newOffset);
- }
- return instance;
- };
- WalkontableScroll.prototype.scrollHorizontal = function (delta) {
- if (!this.instance.drawn) {
- throw new Error('scrollHorizontal can only be called after table was drawn to DOM');
- }
- var instance = this.instance
- , newOffset
- , offset = instance.getSetting('offsetColumn')
- , fixedCount = instance.getSetting('fixedColumnsLeft')
- , total = instance.getSetting('totalColumns')
- , maxSize = instance.wtViewport.getViewportWidth();
- if (total > 0) {
- newOffset = this.scrollLogicHorizontal(delta, offset, total, fixedCount, maxSize, function (col) {
- if (col - offset < fixedCount && col - offset >= 0) {
- return instance.getSetting('columnWidth', col - offset);
- }
- else {
- return instance.getSetting('columnWidth', col);
- }
- });
- }
- else {
- newOffset = 0;
- }
- if (newOffset !== offset) {
- this.instance.wtScrollbars.horizontal.scrollTo(newOffset);
- }
- return instance;
- };
- WalkontableScroll.prototype.scrollLogicVertical = function (delta, offset, total, fixedCount, maxSize, cellSizeFn, setReverseRenderFn) {
- var newOffset = offset + delta;
- if (newOffset >= total - fixedCount) {
- newOffset = total - fixedCount - 1;
- setReverseRenderFn(true);
- }
- if (newOffset < 0) {
- newOffset = 0;
- }
- return newOffset;
- };
- WalkontableScroll.prototype.scrollLogicHorizontal = function (delta, offset, total, fixedCount, maxSize, cellSizeFn) {
- var newOffset = offset + delta
- , sum = 0
- , col;
- if (newOffset > fixedCount) {
- if (newOffset >= total - fixedCount) {
- newOffset = total - fixedCount - 1;
- }
- col = newOffset;
- while (sum < maxSize && col < total) {
- sum += cellSizeFn(col);
- col++;
- }
- if (sum < maxSize) {
- while (newOffset > 0) {
- //if sum still less than available width, we cannot scroll that far (must move offset to the left)
- sum += cellSizeFn(newOffset - 1);
- if (sum < maxSize) {
- newOffset--;
- }
- else {
- break;
- }
- }
- }
- }
- else if (newOffset < 0) {
- newOffset = 0;
- }
- return newOffset;
- };
- /**
- * Scrolls viewport to a cell by minimum number of cells
- */
- WalkontableScroll.prototype.scrollViewport = function (coords) {
- if (!this.instance.drawn) {
- return;
- }
- var offsetRow = this.instance.getSetting('offsetRow')
- , offsetColumn = this.instance.getSetting('offsetColumn')
- , lastVisibleRow = this.instance.wtTable.getLastVisibleRow()
- , totalRows = this.instance.getSetting('totalRows')
- , totalColumns = this.instance.getSetting('totalColumns')
- , fixedRowsTop = this.instance.getSetting('fixedRowsTop')
- , fixedColumnsLeft = this.instance.getSetting('fixedColumnsLeft');
- if (this.instance.getSetting('nativeScrollbars')) {
- var TD = this.instance.wtTable.getCell(coords);
- if (typeof TD === 'object') {
- var offset = WalkontableDom.prototype.offset(TD);
- var outerHeight = WalkontableDom.prototype.outerHeight(TD);
- var scrollY = this.instance.wtScrollbars.vertical.getScrollPosition();
- var clientHeight = WalkontableDom.prototype.outerHeight(this.instance.wtScrollbars.vertical.scrollHandler);
- if (this.instance.wtScrollbars.vertical.scrollHandler !== window) {
- offset.top = offset.top - WalkontableDom.prototype.offset(this.instance.wtScrollbars.vertical.scrollHandler).top;
- }
- clientHeight -= 20;
- if (outerHeight < clientHeight) {
- if (offset.top < scrollY) {
- this.instance.wtScrollbars.vertical.setScrollPosition(offset.top);
- }
- else if (offset.top + outerHeight > scrollY + clientHeight) {
- this.instance.wtScrollbars.vertical.setScrollPosition(offset.top - clientHeight + outerHeight);
- }
- }
- return;
- }
- }
- if (coords[0] < 0 || coords[0] > totalRows - 1) {
- throw new Error('row ' + coords[0] + ' does not exist');
- }
- else if (coords[1] < 0 || coords[1] > totalColumns - 1) {
- throw new Error('column ' + coords[1] + ' does not exist');
- }
- if (coords[0] > lastVisibleRow) {
- // this.scrollVertical(coords[0] - lastVisibleRow + 1);
- this.scrollVertical(coords[0] - fixedRowsTop - offsetRow);
- this.instance.wtTable.verticalRenderReverse = true;
- }
- else if (coords[0] === lastVisibleRow && this.instance.wtTable.rowStrategy.isLastIncomplete()) {
- // this.scrollVertical(coords[0] - lastVisibleRow + 1);
- this.scrollVertical(coords[0] - fixedRowsTop - offsetRow);
- this.instance.wtTable.verticalRenderReverse = true;
- }
- else if (coords[0] - fixedRowsTop < offsetRow) {
- this.scrollVertical(coords[0] - fixedRowsTop - offsetRow);
- }
- else {
- this.scrollVertical(0); //Craig's issue: remove row from the last scroll page should scroll viewport a row up if needed
- }
- if (this.instance.wtTable.isColumnBeforeViewport(coords[1])) {
- //scroll left
- this.instance.wtScrollbars.horizontal.scrollTo(coords[1] - fixedColumnsLeft);
- }
- else if (this.instance.wtTable.isColumnAfterViewport(coords[1]) || (this.instance.wtTable.getLastVisibleColumn() === coords[1] && !this.instance.wtTable.isLastColumnFullyVisible())) {
- //scroll right
- var sum = 0;
- for (var i = 0; i < fixedColumnsLeft; i++) {
- sum += this.instance.getSetting('columnWidth', i);
- }
- var scrollTo = coords[1];
- sum += this.instance.getSetting('columnWidth', scrollTo);
- var available = this.instance.wtViewport.getViewportWidth();
- if (sum < available) {
- var next = this.instance.getSetting('columnWidth', scrollTo - 1);
- while (sum + next < available && scrollTo >= fixedColumnsLeft) {
- scrollTo--;
- sum += next;
- }
- }
- this.instance.wtScrollbars.horizontal.scrollTo(scrollTo - fixedColumnsLeft);
- }
- /*else {
- //no scroll
- }*/
- return this.instance;
- };
- function WalkontableScrollbar() {
- }
- WalkontableScrollbar.prototype.init = function () {
- var that = this;
- //reference to instance
- this.$table = $(this.instance.wtTable.TABLE);
- //create elements
- this.slider = document.createElement('DIV');
- this.sliderStyle = this.slider.style;
- this.sliderStyle.position = 'absolute';
- this.sliderStyle.top = '0';
- this.sliderStyle.left = '0';
- this.sliderStyle.display = 'none';
- this.slider.className = 'dragdealer ' + this.type;
- this.handle = document.createElement('DIV');
- this.handleStyle = this.handle.style;
- this.handle.className = 'handle';
- this.slider.appendChild(this.handle);
- this.container = this.instance.wtTable.holder;
- this.container.appendChild(this.slider);
- var firstRun = true;
- this.dragTimeout = null;
- var dragDelta;
- var dragRender = function () {
- that.onScroll(dragDelta);
- };
- this.dragdealer = new Dragdealer(this.slider, {
- vertical: (this.type === 'vertical'),
- horizontal: (this.type === 'horizontal'),
- slide: false,
- speed: 100,
- animationCallback: function (x, y) {
- if (firstRun) {
- firstRun = false;
- return;
- }
- that.skipRefresh = true;
- dragDelta = that.type === 'vertical' ? y : x;
- if (that.dragTimeout === null) {
- that.dragTimeout = setInterval(dragRender, 100);
- dragRender();
- }
- },
- callback: function (x, y) {
- that.skipRefresh = false;
- clearInterval(that.dragTimeout);
- that.dragTimeout = null;
- dragDelta = that.type === 'vertical' ? y : x;
- that.onScroll(dragDelta);
- }
- });
- this.skipRefresh = false;
- };
- WalkontableScrollbar.prototype.onScroll = function (delta) {
- if (this.instance.drawn) {
- this.readSettings();
- if (this.total > this.visibleCount) {
- var newOffset = Math.round(this.handlePosition * this.total / this.sliderSize);
- if (delta === 1) {
- if (this.type === 'vertical') {
- this.instance.scrollVertical(Infinity).draw();
- }
- else {
- this.instance.scrollHorizontal(Infinity).draw();
- }
- }
- else if (newOffset !== this.offset) { //is new offset different than old offset
- if (this.type === 'vertical') {
- this.instance.scrollVertical(newOffset - this.offset).draw();
- }
- else {
- this.instance.scrollHorizontal(newOffset - this.offset).draw();
- }
- }
- else {
- this.refresh();
- }
- }
- }
- };
- /**
- * Returns what part of the scroller should the handle take
- * @param viewportCount {Number} number of visible rows or columns
- * @param totalCount {Number} total number of rows or columns
- * @return {Number} 0..1
- */
- WalkontableScrollbar.prototype.getHandleSizeRatio = function (viewportCount, totalCount) {
- if (!totalCount || viewportCount > totalCount || viewportCount == totalCount) {
- return 1;
- }
- return 1 / totalCount;
- };
- WalkontableScrollbar.prototype.prepare = function () {
- if (this.skipRefresh) {
- return;
- }
- var ratio = this.getHandleSizeRatio(this.visibleCount, this.total);
- if (((ratio === 1 || isNaN(ratio)) && this.scrollMode === 'auto') || this.scrollMode === 'none') {
- //isNaN is needed because ratio equals NaN when totalRows/totalColumns equals 0
- this.visible = false;
- }
- else {
- this.visible = true;
- }
- };
- WalkontableScrollbar.prototype.refresh = function () {
- if (this.skipRefresh) {
- return;
- }
- else if (!this.visible) {
- this.sliderStyle.display = 'none';
- return;
- }
- var ratio
- , sliderSize
- , handleSize
- , handlePosition
- , visibleCount = this.visibleCount
- , tableWidth = this.instance.wtViewport.getWorkspaceWidth()
- , tableHeight = this.instance.wtViewport.getWorkspaceHeight();
- if (tableWidth === Infinity) {
- tableWidth = this.instance.wtViewport.getWorkspaceActualWidth();
- }
- if (tableHeight === Infinity) {
- tableHeight = this.instance.wtViewport.getWorkspaceActualHeight();
- }
- if (this.type === 'vertical') {
- if (this.instance.wtTable.rowStrategy.isLastIncomplete()) {
- visibleCount--;
- }
- sliderSize = tableHeight - 2; //2 is sliders border-width
- this.sliderStyle.top = this.instance.wtDom.offset(this.$table[0]).top - this.instance.wtDom.offset(this.container).top + 'px';
- this.sliderStyle.left = tableWidth - 1 + 'px'; //1 is sliders border-width
- this.sliderStyle.height = Math.max(sliderSize, 0) + 'px';
- }
- else { //horizontal
- sliderSize = tableWidth - 2; //2 is sliders border-width
- this.sliderStyle.left = this.instance.wtDom.offset(this.$table[0]).left - this.instance.wtDom.offset(this.container).left + 'px';
- this.sliderStyle.top = tableHeight - 1 + 'px'; //1 is sliders border-width
- this.sliderStyle.width = Math.max(sliderSize, 0) + 'px';
- }
- ratio = this.getHandleSizeRatio(visibleCount, this.total);
- handleSize = Math.round(sliderSize * ratio);
- if (handleSize < 10) {
- handleSize = 15;
- }
- handlePosition = Math.floor(sliderSize * (this.offset / this.total));
- if (handleSize + handlePosition > sliderSize) {
- handlePosition = sliderSize - handleSize;
- }
- if (this.type === 'vertical') {
- this.handleStyle.height = handleSize + 'px';
- this.handleStyle.top = handlePosition + 'px';
- }
- else { //horizontal
- this.handleStyle.width = handleSize + 'px';
- this.handleStyle.left = handlePosition + 'px';
- }
- this.sliderStyle.display = 'block';
- };
- WalkontableScrollbar.prototype.destroy = function () {
- clearInterval(this.dragdealer.interval);
- };
- ///
- var WalkontableVerticalScrollbar = function (instance) {
- this.instance = instance;
- this.type = 'vertical';
- this.init();
- };
- WalkontableVerticalScrollbar.prototype = new WalkontableScrollbar();
- WalkontableVerticalScrollbar.prototype.scrollTo = function (cell) {
- this.instance.update('offsetRow', cell);
- };
- WalkontableVerticalScrollbar.prototype.readSettings = function () {
- this.scrollMode = this.instance.getSetting('scrollV');
- this.offset = this.instance.getSetting('offsetRow');
- this.total = this.instance.getSetting('totalRows');
- this.visibleCount = this.instance.wtTable.rowStrategy.countVisible();
- if(this.visibleCount > 1 && this.instance.wtTable.rowStrategy.isLastIncomplete()) {
- this.visibleCount--;
- }
- this.handlePosition = parseInt(this.handleStyle.top, 10);
- this.sliderSize = parseInt(this.sliderStyle.height, 10);
- this.fixedCount = this.instance.getSetting('fixedRowsTop');
- };
- ///
- var WalkontableHorizontalScrollbar = function (instance) {
- this.instance = instance;
- this.type = 'horizontal';
- this.init();
- };
- WalkontableHorizontalScrollbar.prototype = new WalkontableScrollbar();
- WalkontableHorizontalScrollbar.prototype.scrollTo = function (cell) {
- this.instance.update('offsetColumn', cell);
- };
- WalkontableHorizontalScrollbar.prototype.readSettings = function () {
- this.scrollMode = this.instance.getSetting('scrollH');
- this.offset = this.instance.getSetting('offsetColumn');
- this.total = this.instance.getSetting('totalColumns');
- this.visibleCount = this.instance.wtTable.columnStrategy.countVisible();
- if(this.visibleCount > 1 && this.instance.wtTable.columnStrategy.isLastIncomplete()) {
- this.visibleCount--;
- }
- this.handlePosition = parseInt(this.handleStyle.left, 10);
- this.sliderSize = parseInt(this.sliderStyle.width, 10);
- this.fixedCount = this.instance.getSetting('fixedColumnsLeft');
- };
- WalkontableHorizontalScrollbar.prototype.getHandleSizeRatio = function (viewportCount, totalCount) {
- if (!totalCount || viewportCount > totalCount || viewportCount == totalCount) {
- return 1;
- }
- return viewportCount / totalCount;
- };
- function WalkontableScrollbarNative() {
- this.lastWindowScrollPosition = NaN;
- this.maxOuts = 10; //max outs in one direction (before and after table)
- this.lastBegin = 0;
- this.lastEnd = 0;
- }
- /*
- Possible optimizations:
- [x] don't rerender if scroll delta is smaller than the fragment outside of the viewport
- [ ] move .style.top change before .draw()
- [ ] put .draw() in requestAnimationFrame
- [ ] don't rerender rows that remain visible after the scroll
- */
- WalkontableScrollbarNative.prototype.init = function () {
- this.TABLE = this.instance.wtTable.TABLE;
- this.fixed = this.instance.wtTable.hider;
- this.fixedContainer = this.instance.wtTable.holder;
- this.fixed.style.position = 'absolute';
- this.fixed.style.left = '0';
- this.scrollHandler = this.getScrollableElement(this.TABLE);
- this.$scrollHandler = $(this.scrollHandler); //in future remove jQuery from here
- };
- WalkontableScrollbarNative.prototype.getScrollableElement = function (TABLE) {
- var el = TABLE.parentNode;
- while (el && el.style) {
- if (el.style.overflow === 'scroll') {
- return el;
- }
- el = el.parentNode;
- }
- return window;
- };
- WalkontableScrollbarNative.prototype.prepare = function () {
- };
- WalkontableScrollbarNative.prototype.availableSize = function () {
- var availableSize;
- if (this.windowScrollPosition > this.tableParentOffset /*&& last > -1*/) { //last -1 means that viewport is scrolled behind the table
- if (this.instance.wtTable.getLastVisibleRow() === this.total - 1) {
- availableSize = this.instance.wtDom.outerHeight(this.TABLE);
- }
- else {
- availableSize = this.windowSize;
- }
- }
- else {
- availableSize = this.windowSize - (this.tableParentOffset);
- }
- return availableSize;
- };
- WalkontableScrollbarNative.prototype.refresh = function (selectionsOnly) {
- var last = this.getLastCell();
- this.measureBefore = this.sumCellSizes(0, this.offset);
- if (last === -1) { //last -1 means that viewport is scrolled behind the table
- this.measureAfter = 0;
- }
- else {
- this.measureAfter = this.sumCellSizes(last, this.total - last);
- }
- this.applyToDOM();
- this.clone && this.clone.draw(selectionsOnly);
- };
- WalkontableScrollbarNative.prototype.destroy = function () {
- this.$scrollHandler.off('.' + this.instance.guid);
- $(window).off('.' + this.instance.guid);
- $(document).off('.' + this.instance.guid);
- };
- function WalkontableCornerScrollbarNative(instance) {
- this.instance = instance;
- this.init();
- this.clone = this.makeClone('corner');
- }
- WalkontableCornerScrollbarNative.prototype = new WalkontableScrollbarNative();
- WalkontableCornerScrollbarNative.prototype.makeClone = function (direction) {
- if (this.instance.cloneFrom) {
- return;
- }
- var clone = $('<div id="cln_' + direction + '" class="handsontable"></div>');
- this.instance.wtTable.holder.parentNode.appendChild(clone[0]);
- clone.css({
- position: 'fixed',
- overflow: 'hidden'
- });
- var table2 = $('<table class="htCore"></table>');
- table2.className = this.instance.wtTable.TABLE.className;
- clone.append(table2);
- var walkontableConfig = {};
- walkontableConfig.cloneFrom = this.instance;
- walkontableConfig.cloneDirection = direction;
- walkontableConfig.table = table2[0];
- var wt = new Walkontable(walkontableConfig);
- return wt;
- };
- WalkontableCornerScrollbarNative.prototype.resetFixedPosition = function () {
- if (!this.instance.wtTable.holder.parentNode) {
- return; //removed from DOM
- }
- var elem = this.clone.wtTable.holder.parentNode;
- var box;
- if (this.scrollHandler === window) {
- box = this.instance.wtTable.holder.getBoundingClientRect();
- var top = Math.ceil(box.top, 10);
- var bottom = Math.ceil(box.bottom, 10);
- if (top < 0 && bottom > 0) {
- elem.style.top = '0';
- }
- else {
- elem.style.top = top + 'px';
- }
- var left = Math.ceil(box.left, 10);
- var right = Math.ceil(box.right, 10);
- if (left < 0 && right > 0) {
- elem.style.left = '0';
- }
- else {
- elem.style.left = left + 'px';
- }
- }
- else {
- box = this.scrollHandler.getBoundingClientRect();
- elem.style.top = Math.ceil(box.top, 10) + 'px';
- elem.style.left = Math.ceil(box.left, 10) + 'px';
- }
- elem.style.width = WalkontableDom.prototype.outerWidth(this.clone.wtTable.TABLE) + 4 + 'px';
- elem.style.height = WalkontableDom.prototype.outerHeight(this.clone.wtTable.TABLE) + 4 + 'px';
- };
- WalkontableCornerScrollbarNative.prototype.prepare = function () {
- };
- WalkontableCornerScrollbarNative.prototype.refresh = function (selectionsOnly) {
- this.measureBefore = 0;
- this.measureAfter = 0;
- this.clone && this.clone.draw(selectionsOnly);
- };
- WalkontableCornerScrollbarNative.prototype.getScrollPosition = function () {
- };
- WalkontableCornerScrollbarNative.prototype.getLastCell = function () {
- };
- WalkontableCornerScrollbarNative.prototype.getTableSize = function () {
- };
- WalkontableCornerScrollbarNative.prototype.applyToDOM = function () {
- };
- WalkontableCornerScrollbarNative.prototype.scrollTo = function () {
- };
- WalkontableCornerScrollbarNative.prototype.readWindowSize = function () {
- };
- WalkontableCornerScrollbarNative.prototype.readSettings = function () {
- };
- function WalkontableHorizontalScrollbarNative(instance) {
- this.instance = instance;
- this.type = 'horizontal';
- this.cellSize = 50;
- this.init();
- this.clone = this.makeClone('left');
- }
- WalkontableHorizontalScrollbarNative.prototype = new WalkontableScrollbarNative();
- WalkontableHorizontalScrollbarNative.prototype.makeClone = function (direction) {
- if (this.instance.cloneFrom) {
- return;
- }
- var that = this;
- var clone = $('<div id="cln_' + direction + '" class="handsontable"></div>');
- this.instance.wtTable.holder.parentNode.appendChild(clone[0]);
- clone.css({
- position: 'fixed',
- overflow: 'hidden'
- });
- // clone[0].style.height = '184px';
- // clone[0].style.width = '55px';
- var table2 = $('<table class="htCore"></table>');
- table2.className = this.instance.wtTable.TABLE.className;
- clone.append(table2);
- var walkontableConfig = {};
- walkontableConfig.cloneFrom = this.instance;
- walkontableConfig.cloneDirection = direction;
- walkontableConfig.table = table2[0];
- var wt = new Walkontable(walkontableConfig);
- return wt;
- };
- WalkontableHorizontalScrollbarNative.prototype.resetFixedPosition = function () {
- if (!this.instance.wtTable.holder.parentNode) {
- return; //removed from DOM
- }
- var elem = this.clone.wtTable.holder.parentNode;
- var box;
- if (this.scrollHandler === window) {
- box = this.instance.wtTable.holder.getBoundingClientRect();
- var left = Math.ceil(box.left, 10);
- var right = Math.ceil(box.right, 10);
- if (left < 0 && right > 0) {
- elem.style.left = '0';
- }
- else {
- elem.style.left = left + 'px';
- }
- }
- else {
- box = this.scrollHandler.getBoundingClientRect();
- elem.style.top = Math.ceil(box.top, 10) + 'px';
- elem.style.left = Math.ceil(box.left, 10) + 'px';
- }
- elem.style.width = WalkontableDom.prototype.outerWidth(this.clone.wtTable.TABLE) + 4 + 'px';
- elem.style.height = this.instance.wtViewport.getWorkspaceHeight() - this.instance.getSetting('scrollbarHeight') + 'px';
- };
- WalkontableHorizontalScrollbarNative.prototype.prepare = function () {
- };
- WalkontableHorizontalScrollbarNative.prototype.refresh = function (selectionsOnly) {
- this.measureBefore = 0;
- this.measureAfter = 0;
- this.clone && this.clone.draw(selectionsOnly);
- };
- WalkontableHorizontalScrollbarNative.prototype.getScrollPosition = function () {
- if (this.scrollHandler === window) {
- return this.scrollHandler.scrollX;
- }
- else {
- return this.scrollHandler.scrollLeft;
- }
- };
- WalkontableHorizontalScrollbarNative.prototype.onScroll = function () {
- if (this.instance.cloneFrom) {
- return;
- }
- this.windowScrollPosition = this.getScrollPosition();
- };
- WalkontableHorizontalScrollbarNative.prototype.getLastCell = function () {
- return this.instance.wtTable.getLastVisibleColumn();
- };
- WalkontableHorizontalScrollbarNative.prototype.getTableSize = function () {
- return this.instance.wtDom.outerWidth(this.TABLE);
- };
- WalkontableHorizontalScrollbarNative.prototype.applyToDOM = function () {
- this.fixedContainer.style.paddingLeft = this.measureBefore + 'px';
- this.fixedContainer.style.paddingRight = this.measureAfter + 'px';
- };
- WalkontableHorizontalScrollbarNative.prototype.scrollTo = function (cell) {
- this.$scrollHandler.scrollLeft(this.tableParentOffset + cell * this.cellSize);
- };
- WalkontableHorizontalScrollbarNative.prototype.readWindowSize = function () {
- if (this.scrollHandler === window) {
- this.windowSize = document.documentElement.clientWidth;
- this.tableParentOffset = this.instance.wtTable.holderOffset.left;
- }
- else {
- this.windowSize = WalkontableDom.prototype.outerWidth(this.scrollHandler);
- this.tableParentOffset = 0;
- }
- this.windowScrollPosition = this.getScrollPosition();
- };
- WalkontableHorizontalScrollbarNative.prototype.readSettings = function () {
- this.offset = this.instance.getSetting('offsetColumn');
- this.total = this.instance.getSetting('totalColumns');
- };
- function WalkontableVerticalScrollbarNative(instance) {
- this.instance = instance;
- this.type = 'vertical';
- this.cellSize = 23;
- this.init();
- var that = this;
- WalkontableCellStrategy.prototype.isLastIncomplete = function () { //monkey patch needed. In future get rid of it to improve performance
- /*
- * this.remainingSize = window viewport reduced by sum of all rendered cells (also those before the visible part)
- * that.sumCellSizes(...) = sum of the sizes of cells that are before the visible part + 1 cell that is partially visible on top of the screen
- */
- return this.remainingSize > that.sumCellSizes(that.offset, that.offset + that.curOuts + 1);
- };
- this.clone = this.makeClone('top');
- }
- WalkontableVerticalScrollbarNative.prototype = new WalkontableScrollbarNative();
- WalkontableVerticalScrollbarNative.prototype.makeClone = function (direction) {
- if (this.instance.cloneFrom) {
- return;
- }
- var that = this;
- var clone = $('<div id="cln_' + direction + '" class="handsontable"></div>');
- this.instance.wtTable.holder.parentNode.appendChild(clone[0]);
- clone.css({
- position: 'fixed',
- overflow: 'hidden'
- });
- var table2 = $('<table class="htCore"></table>');
- table2.className = this.instance.wtTable.TABLE.className;
- clone.append(table2);
- var walkontableConfig = {};
- walkontableConfig.cloneFrom = this.instance;
- walkontableConfig.cloneDirection = direction;
- walkontableConfig.table = table2[0];
- var wt = new Walkontable(walkontableConfig);
- var cloneTable = clone.find('table')[0];
- this.$scrollHandler.on('scroll.' + this.instance.guid, function () {
- if (!that.instance.wtTable.holder.parentNode) {
- //Walkontable was detached from DOM, but this handler was not removed
- that.destroy();
- return;
- }
- that.onScroll();
- that.instance.wtScrollbars.horizontal.onScroll(); //it's done here to make sure that all onScroll's are executed before changing styles
- cloneTable.style.left = that.instance.wtScrollbars.horizontal.measureBefore - that.instance.wtScrollbars.horizontal.windowScrollPosition + 'px';
- that.instance.wtScrollbars.horizontal.clone.wtTable.TABLE.style.top = that.instance.wtScrollbars.vertical.measureBefore - that.instance.wtScrollbars.vertical.windowScrollPosition + 'px';
- });
- $(window).on('load.' + this.instance.guid, function () {
- that.resetFixedPosition();
- that.instance.wtScrollbars.horizontal.resetFixedPosition();
- that.instance.wtScrollbars.corner.resetFixedPosition();
- });
- $(window).on('scroll.' + this.instance.guid, function () {
- that.resetFixedPosition();
- that.instance.wtScrollbars.horizontal.resetFixedPosition();
- that.instance.wtScrollbars.corner.resetFixedPosition();
- });
- $(window).on('resize.' + this.instance.guid, function () {
- that.resetFixedPosition();
- that.instance.wtScrollbars.horizontal.resetFixedPosition();
- that.instance.wtScrollbars.corner.resetFixedPosition();
- });
- $(document).on('ready.' + this.instance.guid, function () {
- that.resetFixedPosition();
- that.instance.wtScrollbars.horizontal.resetFixedPosition();
- that.instance.wtScrollbars.corner.resetFixedPosition();
- });
- return wt;
- };
- WalkontableVerticalScrollbarNative.prototype.resetFixedPosition = function () {
- if (!this.instance.wtTable.holder.parentNode) {
- return; //removed from DOM
- }
- var elem = this.clone.wtTable.holder.parentNode;
- var box;
- if (this.scrollHandler === window) {
- box = this.instance.wtTable.holder.getBoundingClientRect();
- var top = Math.ceil(box.top, 10);
- var bottom = Math.ceil(box.bottom, 10);
- if (top < 0 && bottom > 0) {
- elem.style.top = '0';
- }
- else {
- elem.style.top = top + 'px';
- }
- }
- else {
- box = this.scrollHandler.getBoundingClientRect();
- elem.style.top = Math.ceil(box.top, 10) + 'px';
- elem.style.left = Math.ceil(box.left, 10) + 'px';
- }
- elem.style.width = WalkontableDom.prototype.outerWidth(this.instance.wtTable.holder.parentNode) + 'px';
- elem.style.height = WalkontableDom.prototype.outerHeight(this.clone.wtTable.TABLE) + 4 + 'px';
- };
- WalkontableVerticalScrollbarNative.prototype.getScrollPosition = function () {
- if (this.scrollHandler === window) {
- return this.scrollHandler.scrollY;
- }
- else {
- return this.scrollHandler.scrollTop;
- }
- };
- WalkontableVerticalScrollbarNative.prototype.setScrollPosition = function (pos) {
- this.scrollHandler.scrollTop = pos;
- };
- WalkontableVerticalScrollbarNative.prototype.onScroll = function (forcePosition) {
- if (this.instance.cloneFrom) {
- return;
- }
- this.windowScrollPosition = this.getScrollPosition();
- this.readSettings(); //read window scroll position
- if (forcePosition) {
- this.windowScrollPosition = forcePosition;
- }
- if (this.windowScrollPosition === this.lastWindowScrollPosition) {
- return;
- }
- if (this.windowScrollPosition > this.lastBegin && this.windowScrollPosition + this.windowSize < this.lastEnd) {
- return;
- }
- this.lastWindowScrollPosition = this.windowScrollPosition;
- var scrollDelta;
- var newOffset = 0;
- if (1 == 1 || this.windowScrollPosition > this.tableParentOffset) {
- scrollDelta = this.windowScrollPosition - this.tableParentOffset;
- partialOffset = 0;
- if (scrollDelta > 0) {
- var sum = 0;
- var last;
- for (var i = 0; i < this.total; i++) {
- last = this.instance.getSetting('rowHeight', i);
- sum += last;
- if (sum > scrollDelta) {
- break;
- }
- }
- if (this.offset > 0) {
- partialOffset = (sum - scrollDelta);
- }
- newOffset = i;
- newOffset = Math.min(newOffset, this.total);
- }
- }
- this.curOuts = newOffset > this.maxOuts ? this.maxOuts : newOffset;
- newOffset -= this.curOuts;
- this.instance.update('offsetRow', newOffset);
- this.readSettings(); //read new offset
- this.instance.draw();
- };
- WalkontableVerticalScrollbarNative.prototype.getLastCell = function () {
- return this.instance.getSetting('offsetRow') + this.instance.wtTable.tbodyChildrenLength - 1;
- };
- WalkontableVerticalScrollbarNative.prototype.getTableSize = function () {
- return this.instance.wtDom.outerHeight(this.TABLE);
- };
- var partialOffset = 0;
- WalkontableVerticalScrollbarNative.prototype.sumCellSizes = function (from, length) {
- var sum = 0;
- while (from < length) {
- sum += this.instance.getSetting('rowHeight', from);
- from++;
- }
- return sum;
- };
- WalkontableVerticalScrollbarNative.prototype.applyToDOM = function () {
- if (this.instance.cloneDirection === 'top') {
- return;
- }
- var headerSize = this.instance.wtViewport.getColumnHeaderHeight();
- this.fixedContainer.style.height = headerSize + this.sumCellSizes(0, this.total) + 'px';
- this.fixed.style.top = this.measureBefore + 'px';
- this.fixed.style.bottom = '';
- this.lastBegin = this.tableParentOffset + this.measureBefore;
- this.lastEnd = this.lastBegin + headerSize + this.instance.wtTable.rowStrategy.cellSizesSum;
- };
- WalkontableVerticalScrollbarNative.prototype.scrollTo = function (cell) {
- var newY = this.tableParentOffset + cell * this.cellSize;
- this.$scrollHandler.scrollTop(newY);
- this.onScroll(newY);
- };
- WalkontableVerticalScrollbarNative.prototype.readWindowSize = function () {
- if (this.scrollHandler === window) {
- this.windowSize = document.documentElement.clientHeight;
- this.tableParentOffset = this.instance.wtTable.holderOffset.left;
- }
- else {
- this.windowSize = WalkontableDom.prototype.outerHeight(this.scrollHandler);
- this.tableParentOffset = 0;
- }
- this.windowScrollPosition = this.getScrollPosition();
- };
- WalkontableVerticalScrollbarNative.prototype.readSettings = function () {
- this.offset = this.instance.getSetting('offsetRow');
- this.total = this.instance.getSetting('totalRows');
- };
- function WalkontableScrollbars(instance) {
- if (instance.getSetting('nativeScrollbars')) {
- instance.update('scrollbarWidth', walkontableGetScrollbarWidth());
- instance.update('scrollbarHeight', walkontableGetScrollbarWidth());
- this.vertical = new WalkontableVerticalScrollbarNative(instance);
- this.horizontal = new WalkontableHorizontalScrollbarNative(instance);
- this.corner = new WalkontableCornerScrollbarNative(instance);
- }
- else {
- this.vertical = new WalkontableVerticalScrollbar(instance);
- this.horizontal = new WalkontableHorizontalScrollbar(instance);
- }
- }
- WalkontableScrollbars.prototype.destroy = function () {
- this.vertical && this.vertical.destroy();
- this.horizontal && this.horizontal.destroy();
- };
- WalkontableScrollbars.prototype.refresh = function (selectionsOnly) {
- this.horizontal && this.horizontal.readSettings();
- this.vertical && this.vertical.readSettings();
- this.horizontal && this.horizontal.prepare();
- this.vertical && this.vertical.prepare();
- this.horizontal && this.horizontal.refresh(selectionsOnly);
- this.vertical && this.vertical.refresh(selectionsOnly);
- this.corner && this.corner.refresh(selectionsOnly);
- };
- function WalkontableSelection(instance, settings) {
- this.instance = instance;
- this.settings = settings;
- this.selected = [];
- if (settings.border) {
- this.border = new WalkontableBorder(instance, settings);
- }
- }
- WalkontableSelection.prototype.add = function (coords) {
- this.selected.push(coords);
- };
- WalkontableSelection.prototype.clear = function () {
- this.selected.length = 0; //http://jsperf.com/clear-arrayxxx
- };
- /**
- * Returns the top left (TL) and bottom right (BR) selection coordinates
- * @returns {Object}
- */
- WalkontableSelection.prototype.getCorners = function () {
- var minRow
- , minColumn
- , maxRow
- , maxColumn
- , i
- , ilen = this.selected.length;
- if (ilen > 0) {
- minRow = maxRow = this.selected[0][0];
- minColumn = maxColumn = this.selected[0][1];
- if (ilen > 1) {
- for (i = 1; i < ilen; i++) {
- if (this.selected[i][0] < minRow) {
- minRow = this.selected[i][0];
- }
- else if (this.selected[i][0] > maxRow) {
- maxRow = this.selected[i][0];
- }
- if (this.selected[i][1] < minColumn) {
- minColumn = this.selected[i][1];
- }
- else if (this.selected[i][1] > maxColumn) {
- maxColumn = this.selected[i][1];
- }
- }
- }
- }
- return [minRow, minColumn, maxRow, maxColumn];
- };
- WalkontableSelection.prototype.draw = function () {
- var corners, r, c, source_r, source_c;
- var visibleRows = this.instance.wtTable.rowStrategy.countVisible()
- , visibleColumns = this.instance.wtTable.columnStrategy.countVisible();
- if (this.selected.length) {
- corners = this.getCorners();
- for (r = 0; r < visibleRows; r++) {
- for (c = 0; c < visibleColumns; c++) {
- source_r = this.instance.wtTable.rowFilter.visibleToSource(r);
- source_c = this.instance.wtTable.columnFilter.visibleToSource(c);
- if (source_r >= corners[0] && source_r <= corners[2] && source_c >= corners[1] && source_c <= corners[3]) {
- //selected cell
- this.instance.wtTable.currentCellCache.add(r, c, this.settings.className);
- }
- else if (source_r >= corners[0] && source_r <= corners[2]) {
- //selection is in this row
- this.instance.wtTable.currentCellCache.add(r, c, this.settings.highlightRowClassName);
- }
- else if (source_c >= corners[1] && source_c <= corners[3]) {
- //selection is in this column
- this.instance.wtTable.currentCellCache.add(r, c, this.settings.highlightColumnClassName);
- }
- }
- }
- this.border && this.border.appear(corners); //warning! border.appear modifies corners!
- }
- else {
- this.border && this.border.disappear();
- }
- };
- function WalkontableSettings(instance, settings) {
- var that = this;
- this.instance = instance;
- //default settings. void 0 means it is required, null means it can be empty
- this.defaults = {
- table: void 0,
- //presentation mode
- scrollH: 'auto', //values: scroll (always show scrollbar), auto (show scrollbar if table does not fit in the container), none (never show scrollbar)
- scrollV: 'auto', //values: see above
- nativeScrollbars: false, //values: false (dragdealer), true (native)
- stretchH: 'hybrid', //values: hybrid, all, last, none
- currentRowClassName: null,
- currentColumnClassName: null,
- //data source
- data: void 0,
- offsetRow: 0,
- offsetColumn: 0,
- fixedColumnsLeft: 0,
- fixedRowsTop: 0,
- rowHeaders: function () {
- return []
- }, //this must be array of functions: [function (row, TH) {}]
- columnHeaders: function () {
- return []
- }, //this must be array of functions: [function (column, TH) {}]
- totalRows: void 0,
- totalColumns: void 0,
- width: null,
- height: null,
- cellRenderer: function (row, column, TD) {
- var cellData = that.getSetting('data', row, column);
- that.instance.wtDom.fastInnerText(TD, cellData === void 0 || cellData === null ? '' : cellData);
- },
- columnWidth: 50,
- selections: null,
- hideBorderOnMouseDownOver: false,
- //callbacks
- onCellMouseDown: null,
- onCellMouseOver: null,
- // onCellMouseOut: null,
- onCellDblClick: null,
- onCellCornerMouseDown: null,
- onCellCornerDblClick: null,
- beforeDraw: null,
- onDraw: null,
- //constants
- scrollbarWidth: 10,
- scrollbarHeight: 10
- };
- //reference to settings
- this.settings = {};
- for (var i in this.defaults) {
- if (this.defaults.hasOwnProperty(i)) {
- if (settings[i] !== void 0) {
- this.settings[i] = settings[i];
- }
- else if (this.defaults[i] === void 0) {
- throw new Error('A required setting "' + i + '" was not provided');
- }
- else {
- this.settings[i] = this.defaults[i];
- }
- }
- }
- }
- /**
- * generic methods
- */
- WalkontableSettings.prototype.update = function (settings, value) {
- if (value === void 0) { //settings is object
- for (var i in settings) {
- if (settings.hasOwnProperty(i)) {
- this.settings[i] = settings[i];
- }
- }
- }
- else { //if value is defined then settings is the key
- this.settings[settings] = value;
- }
- return this.instance;
- };
- WalkontableSettings.prototype.getSetting = function (key, param1, param2, param3) {
- if (this[key]) {
- return this[key](param1, param2, param3);
- }
- else {
- return this._getSetting(key, param1, param2, param3);
- }
- };
- WalkontableSettings.prototype._getSetting = function (key, param1, param2, param3) {
- if (typeof this.settings[key] === 'function') {
- return this.settings[key](param1, param2, param3);
- }
- else if (param1 !== void 0 && Object.prototype.toString.call(this.settings[key]) === '[object Array]') {
- return this.settings[key][param1];
- }
- else {
- return this.settings[key];
- }
- };
- WalkontableSettings.prototype.has = function (key) {
- return !!this.settings[key]
- };
- /**
- * specific methods
- */
- WalkontableSettings.prototype.rowHeight = function (row, TD) {
- if (!this.instance.rowHeightCache) {
- this.instance.rowHeightCache = []; //hack. This cache is being invalidated in WOT core.js
- }
- if (this.instance.rowHeightCache[row] === void 0) {
- var size = 23; //guess
- if (TD) {
- size = this.instance.wtDom.outerHeight(TD); //measure
- this.instance.rowHeightCache[row] = size; //cache only something we measured
- }
- return size;
- }
- else {
- return this.instance.rowHeightCache[row];
- }
- };
- function WalkontableTable(instance, table) {
- //reference to instance
- this.instance = instance;
- this.TABLE = table;
- this.wtDom = this.instance.wtDom;
- this.wtDom.removeTextNodes(this.TABLE);
- //wtSpreader
- var parent = this.TABLE.parentNode;
- if (!parent || parent.nodeType !== 1 || !this.wtDom.hasClass(parent, 'wtHolder')) {
- var spreader = document.createElement('DIV');
- spreader.className = 'wtSpreader';
- if (parent) {
- parent.insertBefore(spreader, this.TABLE); //if TABLE is detached (e.g. in Jasmine test), it has no parentNode so we cannot attach holder to it
- }
- spreader.appendChild(this.TABLE);
- }
- this.spreader = this.TABLE.parentNode;
- //wtHider
- parent = this.spreader.parentNode;
- if (!parent || parent.nodeType !== 1 || !this.wtDom.hasClass(parent, 'wtHolder')) {
- var hider = document.createElement('DIV');
- hider.className = 'wtHider';
- if (parent) {
- parent.insertBefore(hider, this.spreader); //if TABLE is detached (e.g. in Jasmine test), it has no parentNode so we cannot attach holder to it
- }
- hider.appendChild(this.spreader);
- }
- this.hider = this.spreader.parentNode;
- this.hiderStyle = this.hider.style;
- this.hiderStyle.position = 'relative';
- //wtHolder
- parent = this.hider.parentNode;
- if (!parent || parent.nodeType !== 1 || !this.wtDom.hasClass(parent, 'wtHolder')) {
- var holder = document.createElement('DIV');
- holder.style.position = 'relative';
- holder.className = 'wtHolder';
- if (parent) {
- parent.insertBefore(holder, this.hider); //if TABLE is detached (e.g. in Jasmine test), it has no parentNode so we cannot attach holder to it
- }
- holder.appendChild(this.hider);
- }
- this.holder = this.hider.parentNode;
- //bootstrap from settings
- this.TBODY = this.TABLE.getElementsByTagName('TBODY')[0];
- if (!this.TBODY) {
- this.TBODY = document.createElement('TBODY');
- this.TABLE.appendChild(this.TBODY);
- }
- this.THEAD = this.TABLE.getElementsByTagName('THEAD')[0];
- if (!this.THEAD) {
- this.THEAD = document.createElement('THEAD');
- this.TABLE.insertBefore(this.THEAD, this.TBODY);
- }
- this.COLGROUP = this.TABLE.getElementsByTagName('COLGROUP')[0];
- if (!this.COLGROUP) {
- this.COLGROUP = document.createElement('COLGROUP');
- this.TABLE.insertBefore(this.COLGROUP, this.THEAD);
- }
- if (this.instance.getSetting('columnHeaders').length) {
- if (!this.THEAD.childNodes.length) {
- var TR = document.createElement('TR');
- this.THEAD.appendChild(TR);
- }
- }
- this.colgroupChildrenLength = this.COLGROUP.childNodes.length;
- this.theadChildrenLength = this.THEAD.firstChild ? this.THEAD.firstChild.childNodes.length : 0;
- this.tbodyChildrenLength = this.TBODY.childNodes.length;
- this.oldCellCache = new WalkontableClassNameCache();
- this.currentCellCache = new WalkontableClassNameCache();
- this.rowFilter = new WalkontableRowFilter();
- this.columnFilter = new WalkontableColumnFilter();
- this.verticalRenderReverse = false;
- }
- WalkontableTable.prototype.refreshHiderDimensions = function () {
- var height = this.instance.wtViewport.getWorkspaceHeight();
- var width = this.instance.wtViewport.getWorkspaceWidth();
- var spreaderStyle = this.spreader.style;
- if ((height !== Infinity || width !== Infinity) && !this.instance.getSetting('nativeScrollbars')) {
- if (height === Infinity) {
- height = this.instance.wtViewport.getWorkspaceActualHeight();
- }
- if (width === Infinity) {
- width = this.instance.wtViewport.getWorkspaceActualWidth();
- }
- this.hiderStyle.overflow = 'hidden';
- spreaderStyle.position = 'absolute';
- spreaderStyle.top = '0';
- spreaderStyle.left = '0';
- if (!this.instance.getSetting('nativeScrollbars')) {
- spreaderStyle.height = '4000px';
- spreaderStyle.width = '4000px';
- }
- if (height < 0) { //this happens with WalkontableScrollbarNative and causes "Invalid argument" error in IE8
- height = 0;
- }
- this.hiderStyle.height = height + 'px';
- this.hiderStyle.width = width + 'px';
- }
- else {
- spreaderStyle.position = 'relative';
- spreaderStyle.width = 'auto';
- spreaderStyle.height = 'auto';
- }
- };
- WalkontableTable.prototype.refreshStretching = function () {
- var instance = this.instance
- , stretchH = instance.getSetting('stretchH')
- , totalRows = instance.getSetting('totalRows')
- , totalColumns = instance.getSetting('totalColumns')
- , offsetColumn = instance.getSetting('offsetColumn');
- var containerWidthFn = function (cacheWidth) {
- var viewportWidth = that.instance.wtViewport.getViewportWidth(cacheWidth);
- if (viewportWidth < cacheWidth && that.instance.getSetting('nativeScrollbars')) {
- return Infinity; //disable stretching when viewport is bigger than sum of the cell widths
- }
- return viewportWidth;
- };
- var that = this;
- var columnWidthFn = function (i) {
- var source_c = that.columnFilter.visibleToSource(i);
- if (source_c < totalColumns) {
- return instance.getSetting('columnWidth', source_c);
- }
- };
- if (stretchH === 'hybrid') {
- if (offsetColumn > 0) {
- stretchH = 'last';
- }
- else {
- stretchH = 'none';
- }
- }
- var containerHeightFn = function (cacheHeight) {
- if (that.instance.getSetting('nativeScrollbars')) {
- return 2 * that.instance.wtViewport.getViewportHeight(cacheHeight);
- }
- return that.instance.wtViewport.getViewportHeight(cacheHeight);
- };
- var rowHeightFn = function (i, TD) {
- if (that.instance.getSetting('nativeScrollbars')) {
- return 20;
- }
- var source_r = that.rowFilter.visibleToSource(i);
- if (source_r < totalRows) {
- if (that.verticalRenderReverse && i === 0) {
- return that.instance.getSetting('rowHeight', source_r, TD) - 1;
- }
- else {
- return that.instance.getSetting('rowHeight', source_r, TD);
- }
- }
- };
- this.columnStrategy = new WalkontableColumnStrategy(containerWidthFn, columnWidthFn, stretchH);
- this.rowStrategy = new WalkontableRowStrategy(containerHeightFn, rowHeightFn);
- };
- WalkontableTable.prototype.adjustAvailableNodes = function () {
- var displayTds
- , rowHeaders = this.instance.getSetting('rowHeaders')
- , displayThs = rowHeaders.length
- , columnHeaders = this.instance.getSetting('columnHeaders')
- , TR
- , TD
- , c;
- //adjust COLGROUP
- while (this.colgroupChildrenLength < displayThs) {
- this.COLGROUP.appendChild(document.createElement('COL'));
- this.colgroupChildrenLength++;
- }
- this.refreshStretching();
- if (this.instance.cloneFrom && (this.instance.cloneDirection === 'left' || this.instance.cloneDirection === 'corner')) {
- this.columnStrategy.cellCount = 0;
- }
- displayTds = this.columnStrategy.cellCount;
- //adjust COLGROUP
- while (this.colgroupChildrenLength < displayTds + displayThs) {
- this.COLGROUP.appendChild(document.createElement('COL'));
- this.colgroupChildrenLength++;
- }
- while (this.colgroupChildrenLength > displayTds + displayThs) {
- this.COLGROUP.removeChild(this.COLGROUP.lastChild);
- this.colgroupChildrenLength--;
- }
- //adjust THEAD
- TR = this.THEAD.firstChild;
- if (columnHeaders.length) {
- if (!TR) {
- TR = document.createElement('TR');
- this.THEAD.appendChild(TR);
- }
- this.theadChildrenLength = TR.childNodes.length;
- while (this.theadChildrenLength < displayTds + displayThs) {
- TR.appendChild(document.createElement('TH'));
- this.theadChildrenLength++;
- }
- while (this.theadChildrenLength > displayTds + displayThs) {
- TR.removeChild(TR.lastChild);
- this.theadChildrenLength--;
- }
- }
- else if (TR) {
- this.wtDom.empty(TR);
- }
- //draw COLGROUP
- for (c = 0; c < this.colgroupChildrenLength; c++) {
- if (c < displayThs) {
- this.wtDom.addClass(this.COLGROUP.childNodes[c], 'rowHeader');
- }
- else {
- this.wtDom.removeClass(this.COLGROUP.childNodes[c], 'rowHeader');
- }
- }
- //draw THEAD
- if (columnHeaders.length) {
- TR = this.THEAD.firstChild;
- if (displayThs) {
- TD = TR.firstChild; //actually it is TH but let's reuse single variable
- for (c = 0; c < displayThs; c++) {
- rowHeaders[c](-displayThs + c, TD);
- TD = TD.nextSibling;
- }
- }
- }
- for (c = 0; c < displayTds; c++) {
- if (columnHeaders.length) {
- columnHeaders[0](this.columnFilter.visibleToSource(c), TR.childNodes[displayThs + c]);
- }
- }
- };
- WalkontableTable.prototype.adjustColumns = function (TR, desiredCount) {
- var count = TR.childNodes.length;
- while (count < desiredCount) {
- var TD = document.createElement('TD');
- TR.appendChild(TD);
- count++;
- }
- while (count > desiredCount) {
- TR.removeChild(TR.lastChild);
- count--;
- }
- };
- WalkontableTable.prototype.draw = function (selectionsOnly) {
- if (this.instance.getSetting('nativeScrollbars')) {
- this.verticalRenderReverse = false; //this is only supported in dragdealer mode, not in native
- }
- this.rowFilter.readSettings(this.instance);
- this.columnFilter.readSettings(this.instance);
- if (!selectionsOnly) {
- if (this.instance.getSetting('nativeScrollbars')) {
- if (this.instance.cloneFrom) {
- this.tableOffset = this.instance.cloneFrom.wtTable.tableOffset;
- }
- else {
- this.holderOffset = this.wtDom.offset(this.holder);
- this.tableOffset = this.wtDom.offset(this.TABLE);
- this.instance.wtScrollbars.vertical.readWindowSize();
- this.instance.wtScrollbars.horizontal.readWindowSize();
- this.instance.wtViewport.resetSettings();
- }
- }
- else {
- this.tableOffset = this.wtDom.offset(this.TABLE);
- this.instance.wtViewport.resetSettings();
- }
- this._doDraw();
- }
- else {
- this.instance.wtScrollbars && this.instance.wtScrollbars.refresh(true);
- }
- this.refreshPositions(selectionsOnly);
- this.instance.drawn = true;
- return this;
- };
- WalkontableTable.prototype._doDraw = function () {
- var r = 0
- , source_r
- , c
- , source_c
- , offsetRow = this.instance.getSetting('offsetRow')
- , totalRows = this.instance.getSetting('totalRows')
- , totalColumns = this.instance.getSetting('totalColumns')
- , displayTds
- , rowHeaders = this.instance.getSetting('rowHeaders')
- , displayThs = rowHeaders.length
- , TR
- , TD
- , TH
- , adjusted = false
- , workspaceWidth
- , mustBeInViewport
- , res;
- if (this.verticalRenderReverse) {
- mustBeInViewport = offsetRow;
- }
- var noPartial = false;
- if (this.verticalRenderReverse) {
- if (offsetRow === totalRows - this.rowFilter.fixedCount - 1) {
- noPartial = true;
- }
- else {
- this.instance.update('offsetRow', offsetRow + 1); //if we are scrolling reverse
- this.rowFilter.readSettings(this.instance);
- }
- }
- //draw TBODY
- if (totalColumns > 0) {
- source_r = this.rowFilter.visibleToSource(r);
- var first = true;
- var fixedRowsTop = this.instance.getSetting('fixedRowsTop');
- while (source_r < totalRows && source_r >= 0) {
- if (r > 1000) {
- throw new Error('Security brake: Too much TRs. Please define height for your table, which will enforce scrollbars.');
- }
- if (this.instance.cloneFrom && (this.instance.cloneDirection === 'top' || this.instance.cloneDirection === 'corner') && r === fixedRowsTop) {
- break;
- }
- if (r >= this.tbodyChildrenLength || (this.verticalRenderReverse && r >= this.rowFilter.fixedCount)) {
- TR = document.createElement('TR');
- for (c = 0; c < displayThs; c++) {
- TR.appendChild(document.createElement('TH'));
- }
- if (this.verticalRenderReverse && r >= this.rowFilter.fixedCount) {
- this.TBODY.insertBefore(TR, this.TBODY.childNodes[this.rowFilter.fixedCount] || this.TBODY.firstChild);
- }
- else {
- this.TBODY.appendChild(TR);
- }
- this.tbodyChildrenLength++;
- }
- else if (r === 0) {
- TR = this.TBODY.firstChild;
- }
- else {
- TR = TR.nextSibling; //http://jsperf.com/nextsibling-vs-indexed-childnodes
- }
- //TH
- TH = TR.firstChild;
- for (c = 0; c < displayThs; c++) {
- //If the number of row headers increased we need to replace TD with TH
- if (TH.nodeName == 'TD') {
- TD = TH;
- TH = document.createElement('TH');
- TR.insertBefore(TH, TD);
- TR.removeChild(TD);
- }
- rowHeaders[c](source_r, TH); //actually TH
- TH = TH.nextSibling; //http://jsperf.com/nextsibling-vs-indexed-childnodes
- }
- if (first) {
- // if (r === 0) {
- first = false;
- this.adjustAvailableNodes();
- adjusted = true;
- displayTds = this.columnStrategy.cellCount;
- //TD
- this.adjustColumns(TR, displayTds + displayThs);
- workspaceWidth = this.instance.wtViewport.getWorkspaceWidth();
- this.columnStrategy.stretch();
- for (c = 0; c < displayTds; c++) {
- this.COLGROUP.childNodes[c + displayThs].style.width = this.columnStrategy.getSize(c) + 'px';
- }
- }
- else {
- //TD
- this.adjustColumns(TR, displayTds + displayThs);
- }
- for (c = 0; c < displayTds; c++) {
- source_c = this.columnFilter.visibleToSource(c);
- if (c === 0) {
- TD = TR.childNodes[this.columnFilter.sourceColumnToVisibleRowHeadedColumn(source_c)];
- }
- else {
- TD = TD.nextSibling; //http://jsperf.com/nextsibling-vs-indexed-childnodes
- }
- //If the number of headers has been reduced, we need to replace excess TH with TD
- if (TD.nodeName == 'TH') {
- TH = TD;
- TD = document.createElement('TD');
- TR.insertBefore(TD, TH);
- TR.removeChild(TH);
- }
- TD.className = '';
- TD.removeAttribute('style');
- this.instance.getSetting('cellRenderer', source_r, source_c, TD);
- }
- offsetRow = this.instance.getSetting('offsetRow'); //refresh the value
- //after last column is rendered, check if last cell is fully displayed
- if (this.verticalRenderReverse && noPartial) {
- if (-this.wtDom.outerHeight(TR.firstChild) < this.rowStrategy.remainingSize) {
- this.TBODY.removeChild(TR);
- this.instance.update('offsetRow', offsetRow + 1);
- this.tbodyChildrenLength--;
- this.rowFilter.readSettings(this.instance);
- break;
- }
- else {
- res = this.rowStrategy.add(r, TD, this.verticalRenderReverse);
- if (res === false) {
- this.rowStrategy.removeOutstanding();
- }
- }
- }
- else {
- res = this.rowStrategy.add(r, TD, this.verticalRenderReverse);
- if (res === false) {
- if (!this.instance.getSetting('nativeScrollbars')) {
- this.rowStrategy.removeOutstanding();
- }
- }
- if (this.rowStrategy.isLastIncomplete()) {
- if (this.verticalRenderReverse && !this.isRowInViewport(mustBeInViewport)) {
- //we failed because one of the cells was by far too large. Recover by rendering from top
- this.verticalRenderReverse = false;
- this.instance.update('offsetRow', mustBeInViewport);
- this.draw();
- return;
- }
- break;
- }
- }
- if (this.verticalRenderReverse && r >= this.rowFilter.fixedCount) {
- if (offsetRow === 0) {
- break;
- }
- this.instance.update('offsetRow', offsetRow - 1);
- this.rowFilter.readSettings(this.instance);
- }
- else {
- r++;
- }
- source_r = this.rowFilter.visibleToSource(r);
- }
- }
- if (!adjusted) {
- this.adjustAvailableNodes();
- }
- r = this.rowStrategy.countVisible();
- while (this.tbodyChildrenLength > r) {
- this.TBODY.removeChild(this.TBODY.lastChild);
- this.tbodyChildrenLength--;
- }
- this.instance.wtScrollbars && this.instance.wtScrollbars.refresh(false);
- if (workspaceWidth !== this.instance.wtViewport.getWorkspaceWidth()) {
- //workspace width changed though to shown/hidden vertical scrollbar. Let's reapply stretching
- this.columnStrategy.stretch();
- for (c = 0; c < this.columnStrategy.cellCount; c++) {
- this.COLGROUP.childNodes[c + displayThs].style.width = this.columnStrategy.getSize(c) + 'px';
- }
- }
- this.verticalRenderReverse = false;
- };
- WalkontableTable.prototype.refreshPositions = function (selectionsOnly) {
- this.refreshHiderDimensions();
- this.refreshSelections(selectionsOnly);
- };
- WalkontableTable.prototype.refreshSelections = function (selectionsOnly) {
- var vr
- , r
- , vc
- , c
- , s
- , slen
- , classNames = []
- , visibleRows = this.rowStrategy.countVisible()
- , visibleColumns = this.columnStrategy.countVisible();
- this.oldCellCache = this.currentCellCache;
- this.currentCellCache = new WalkontableClassNameCache();
- if (this.instance.selections) {
- for (r in this.instance.selections) {
- if (this.instance.selections.hasOwnProperty(r)) {
- this.instance.selections[r].draw();
- if (this.instance.selections[r].settings.className) {
- classNames.push(this.instance.selections[r].settings.className);
- }
- if (this.instance.selections[r].settings.highlightRowClassName) {
- classNames.push(this.instance.selections[r].settings.highlightRowClassName);
- }
- if (this.instance.selections[r].settings.highlightColumnClassName) {
- classNames.push(this.instance.selections[r].settings.highlightColumnClassName);
- }
- }
- }
- }
- slen = classNames.length;
- for (vr = 0; vr < visibleRows; vr++) {
- for (vc = 0; vc < visibleColumns; vc++) {
- r = this.rowFilter.visibleToSource(vr);
- c = this.columnFilter.visibleToSource(vc);
- for (s = 0; s < slen; s++) {
- if (this.currentCellCache.test(vr, vc, classNames[s])) {
- this.wtDom.addClass(this.getCell([r, c]), classNames[s]);
- }
- else if (selectionsOnly && this.oldCellCache.test(vr, vc, classNames[s])) {
- this.wtDom.removeClass(this.getCell([r, c]), classNames[s]);
- }
- }
- }
- }
- };
- /**
- * getCell
- * @param {Array} coords
- * @return {Object} HTMLElement on success or {Number} one of the exit codes on error:
- * -1 row before viewport
- * -2 row after viewport
- * -3 column before viewport
- * -4 column after viewport
- *
- */
- WalkontableTable.prototype.getCell = function (coords) {
- if (this.isRowBeforeViewport(coords[0])) {
- return -1; //row before viewport
- }
- else if (this.isRowAfterViewport(coords[0])) {
- return -2; //row after viewport
- }
- else {
- if (this.isColumnBeforeViewport(coords[1])) {
- return -3; //column before viewport
- }
- else if (this.isColumnAfterViewport(coords[1])) {
- return -4; //column after viewport
- }
- else {
- return this.TBODY.childNodes[this.rowFilter.sourceToVisible(coords[0])].childNodes[this.columnFilter.sourceColumnToVisibleRowHeadedColumn(coords[1])];
- }
- }
- };
- WalkontableTable.prototype.getCoords = function (TD) {
- return [
- this.rowFilter.visibleToSource(this.wtDom.index(TD.parentNode)),
- this.columnFilter.visibleRowHeadedColumnToSourceColumn(TD.cellIndex)
- ];
- };
- //returns -1 if no row is visible
- WalkontableTable.prototype.getLastVisibleRow = function () {
- return this.rowFilter.visibleToSource(this.rowStrategy.cellCount - 1);
- };
- //returns -1 if no column is visible
- WalkontableTable.prototype.getLastVisibleColumn = function () {
- return this.columnFilter.visibleToSource(this.columnStrategy.cellCount - 1);
- };
- WalkontableTable.prototype.isRowBeforeViewport = function (r) {
- return (this.rowFilter.sourceToVisible(r) < this.rowFilter.fixedCount && r >= this.rowFilter.fixedCount);
- };
- WalkontableTable.prototype.isRowAfterViewport = function (r) {
- return (r > this.getLastVisibleRow());
- };
- WalkontableTable.prototype.isColumnBeforeViewport = function (c) {
- return (this.columnFilter.sourceToVisible(c) < this.columnFilter.fixedCount && c >= this.columnFilter.fixedCount);
- };
- WalkontableTable.prototype.isColumnAfterViewport = function (c) {
- return (c > this.getLastVisibleColumn());
- };
- WalkontableTable.prototype.isRowInViewport = function (r) {
- return (!this.isRowBeforeViewport(r) && !this.isRowAfterViewport(r));
- };
- WalkontableTable.prototype.isColumnInViewport = function (c) {
- return (!this.isColumnBeforeViewport(c) && !this.isColumnAfterViewport(c));
- };
- WalkontableTable.prototype.isLastRowFullyVisible = function () {
- return (this.getLastVisibleRow() === this.instance.getSetting('totalRows') - 1 && !this.rowStrategy.isLastIncomplete());
- };
- WalkontableTable.prototype.isLastColumnFullyVisible = function () {
- return (this.getLastVisibleColumn() === this.instance.getSetting('totalColumns') - 1 && !this.columnStrategy.isLastIncomplete());
- };
- function WalkontableViewport(instance) {
- this.instance = instance;
- this.resetSettings();
- if (this.instance.getSetting('nativeScrollbars')) {
- var that = this;
- $(window).on('resize', function () {
- that.clientHeight = that.getWorkspaceHeight();
- });
- }
- }
- /*WalkontableViewport.prototype.isInSightVertical = function () {
- //is table outside viewport bottom edge
- if (tableTop > windowHeight + scrollTop) {
- return -1;
- }
- //is table outside viewport top edge
- else if (scrollTop > tableTop + tableFakeHeight) {
- return -2;
- }
- //table is in viewport but how much exactly?
- else {
- }
- };*/
- //used by scrollbar
- WalkontableViewport.prototype.getWorkspaceHeight = function (proposedHeight) {
- if (this.instance.getSetting('nativeScrollbars')) {
- return this.instance.wtScrollbars.vertical.windowSize;
- }
- var height = this.instance.getSetting('height');
- if (height === Infinity || height === void 0 || height === null || height < 1) {
- if (this.instance.wtScrollbars.vertical instanceof WalkontableScrollbarNative) {
- height = this.instance.wtScrollbars.vertical.availableSize();
- }
- else {
- height = Infinity;
- }
- }
- if (height !== Infinity) {
- if (proposedHeight >= height) {
- height -= this.instance.getSetting('scrollbarHeight');
- }
- else if (this.instance.wtScrollbars.horizontal.visible) {
- height -= this.instance.getSetting('scrollbarHeight');
- }
- }
- return height;
- };
- WalkontableViewport.prototype.getWorkspaceWidth = function (proposedWidth) {
- var width = this.instance.getSetting('width');
- if (width === Infinity || width === void 0 || width === null || width < 1) {
- if (this.instance.wtScrollbars.horizontal instanceof WalkontableScrollbarNative) {
- width = this.instance.wtScrollbars.horizontal.availableSize();
- }
- else {
- width = Infinity;
- }
- }
- if (width !== Infinity) {
- if (proposedWidth >= width) {
- width -= this.instance.getSetting('scrollbarWidth');
- }
- else if (this.instance.wtScrollbars.vertical.visible) {
- width -= this.instance.getSetting('scrollbarWidth');
- }
- }
- return width;
- };
- WalkontableViewport.prototype.getWorkspaceActualHeight = function () {
- return this.instance.wtDom.outerHeight(this.instance.wtTable.TABLE);
- };
- WalkontableViewport.prototype.getWorkspaceActualWidth = function () {
- return this.instance.wtDom.outerWidth(this.instance.wtTable.TABLE) || this.instance.wtDom.outerWidth(this.instance.wtTable.TBODY) || this.instance.wtDom.outerWidth(this.instance.wtTable.THEAD); //IE8 reports 0 as <table> offsetWidth;
- };
- WalkontableViewport.prototype.getColumnHeaderHeight = function () {
- if (isNaN(this.columnHeaderHeight)) {
- var cellOffset = this.instance.wtDom.offset(this.instance.wtTable.TBODY)
- , tableOffset = this.instance.wtTable.tableOffset;
- this.columnHeaderHeight = cellOffset.top - tableOffset.top;
- }
- return this.columnHeaderHeight;
- };
- WalkontableViewport.prototype.getViewportHeight = function (proposedHeight) {
- var containerHeight = this.getWorkspaceHeight(proposedHeight);
- if (containerHeight === Infinity) {
- return containerHeight;
- }
- var columnHeaderHeight = this.getColumnHeaderHeight();
- if (columnHeaderHeight > 0) {
- return containerHeight - columnHeaderHeight;
- }
- else {
- return containerHeight;
- }
- };
- WalkontableViewport.prototype.getRowHeaderHeight = function () {
- if (this.instance.cloneFrom) {
- return this.instance.cloneFrom.wtViewport.getRowHeaderHeight();
- }
- if (isNaN(this.rowHeaderWidth)) {
- var TR = this.instance.wtTable.TBODY ? this.instance.wtTable.TBODY.firstChild : null;
- if (TR) {
- var TD = TR.firstChild;
- this.rowHeaderWidth = 0;
- while (TD && TD.nodeName === 'TH') {
- this.rowHeaderWidth += this.instance.wtDom.outerWidth(TD);
- TD = TD.nextSibling;
- }
- }
- }
- return this.rowHeaderWidth;
- };
- WalkontableViewport.prototype.getViewportWidth = function (proposedWidth) {
- var containerWidth = this.getWorkspaceWidth(proposedWidth);
- if (containerWidth === Infinity) {
- return containerWidth;
- }
- var rowHeaderWidth = this.getRowHeaderHeight();
- if (rowHeaderWidth > 0) {
- return containerWidth - rowHeaderWidth;
- }
- else {
- return containerWidth;
- }
- };
- WalkontableViewport.prototype.resetSettings = function () {
- this.rowHeaderWidth = NaN;
- this.columnHeaderHeight = NaN;
- };
- function WalkontableWheel(instance) {
- if (instance.getSetting('nativeScrollbars')) {
- return;
- }
- //spreader === instance.wtTable.TABLE.parentNode
- $(instance.wtTable.spreader).on('mousewheel', function (event, delta, deltaX, deltaY) {
- if (!deltaX && !deltaY && delta) { //we are in IE8, see https://github.com/brandonaaron/jquery-mousewheel/issues/53
- deltaY = delta;
- }
- if (!deltaX && !deltaY) { //this happens in IE8 test case
- return;
- }
- if (deltaY > 0 && instance.getSetting('offsetRow') === 0) {
- return; //attempt to scroll up when it's already showing first row
- }
- else if (deltaY < 0 && instance.wtTable.isLastRowFullyVisible()) {
- return; //attempt to scroll down when it's already showing last row
- }
- else if (deltaX < 0 && instance.getSetting('offsetColumn') === 0) {
- return; //attempt to scroll left when it's already showing first column
- }
- else if (deltaX > 0 && instance.wtTable.isLastColumnFullyVisible()) {
- return; //attempt to scroll right when it's already showing last column
- }
- //now we are sure we really want to scroll
- clearTimeout(instance.wheelTimeout);
- instance.wheelTimeout = setTimeout(function () { //timeout is needed because with fast-wheel scrolling mousewheel event comes dozen times per second
- if (deltaY) {
- //ceil is needed because jquery-mousewheel reports fractional mousewheel deltas on touchpad scroll
- //see http://stackoverflow.com/questions/5527601/normalizing-mousewheel-speed-across-browsers
- if (instance.wtScrollbars.vertical.visible) { // if we see scrollbar
- instance.scrollVertical(-Math.ceil(deltaY)).draw();
- }
- }
- else if (deltaX) {
- if (instance.wtScrollbars.horizontal.visible) { // if we see scrollbar
- instance.scrollHorizontal(Math.ceil(deltaX)).draw();
- }
- }
- }, 0);
- event.preventDefault();
- });
- }
- /**
- * Dragdealer JS v0.9.5 - patched by Walkontable at lines 66, 309-310, 339-340
- * http://code.ovidiu.ch/dragdealer-js
- *
- * Copyright (c) 2010, Ovidiu Chereches
- * MIT License
- * http://legal.ovidiu.ch/licenses/MIT
- */
- /* Cursor */
- var Cursor =
- {
- x: 0, y: 0,
- init: function()
- {
- this.setEvent('mouse');
- this.setEvent('touch');
- },
- setEvent: function(type)
- {
- var moveHandler = document['on' + type + 'move'] || function(){};
- document['on' + type + 'move'] = function(e)
- {
- moveHandler(e);
- Cursor.refresh(e);
- }
- },
- refresh: function(e)
- {
- if(!e)
- {
- e = window.event;
- }
- if(e.type == 'mousemove')
- {
- this.set(e);
- }
- else if(e.touches)
- {
- this.set(e.touches[0]);
- }
- },
- set: function(e)
- {
- if(e.pageX || e.pageY)
- {
- this.x = e.pageX;
- this.y = e.pageY;
- }
- else if(e.clientX || e.clientY)
- {
- this.x = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
- this.y = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
- }
- }
- };
- Cursor.init();
- /* Position */
- var Position =
- {
- get: function(obj)
- {
- var curtop = 0, curleft = 0; //Walkontable patch. Original (var curleft = curtop = 0;) created curtop in global scope
- if(obj.offsetParent)
- {
- do
- {
- curleft += obj.offsetLeft;
- curtop += obj.offsetTop;
- }
- while((obj = obj.offsetParent));
- }
- return [curleft, curtop];
- }
- };
- /* Dragdealer */
- var Dragdealer = function(wrapper, options)
- {
- if(typeof(wrapper) == 'string')
- {
- wrapper = document.getElementById(wrapper);
- }
- if(!wrapper)
- {
- return;
- }
- var handle = wrapper.getElementsByTagName('div')[0];
- if(!handle || handle.className.search(/(^|\s)handle(\s|$)/) == -1)
- {
- return;
- }
- this.init(wrapper, handle, options || {});
- this.setup();
- };
- Dragdealer.prototype =
- {
- init: function(wrapper, handle, options)
- {
- this.wrapper = wrapper;
- this.handle = handle;
- this.options = options;
-
- this.disabled = this.getOption('disabled', false);
- this.horizontal = this.getOption('horizontal', true);
- this.vertical = this.getOption('vertical', false);
- this.slide = this.getOption('slide', true);
- this.steps = this.getOption('steps', 0);
- this.snap = this.getOption('snap', false);
- this.loose = this.getOption('loose', false);
- this.speed = this.getOption('speed', 10) / 100;
- this.xPrecision = this.getOption('xPrecision', 0);
- this.yPrecision = this.getOption('yPrecision', 0);
-
- this.callback = options.callback || null;
- this.animationCallback = options.animationCallback || null;
-
- this.bounds = {
- left: options.left || 0, right: -(options.right || 0),
- top: options.top || 0, bottom: -(options.bottom || 0),
- x0: 0, x1: 0, xRange: 0,
- y0: 0, y1: 0, yRange: 0
- };
- this.value = {
- prev: [-1, -1],
- current: [options.x || 0, options.y || 0],
- target: [options.x || 0, options.y || 0]
- };
- this.offset = {
- wrapper: [0, 0],
- mouse: [0, 0],
- prev: [-999999, -999999],
- current: [0, 0],
- target: [0, 0]
- };
- this.change = [0, 0];
-
- this.activity = false;
- this.dragging = false;
- this.tapping = false;
- },
- getOption: function(name, defaultValue)
- {
- return this.options[name] !== undefined ? this.options[name] : defaultValue;
- },
- setup: function()
- {
- this.setWrapperOffset();
- this.setBoundsPadding();
- this.setBounds();
- this.setSteps();
-
- this.addListeners();
- },
- setWrapperOffset: function()
- {
- this.offset.wrapper = Position.get(this.wrapper);
- },
- setBoundsPadding: function()
- {
- if(!this.bounds.left && !this.bounds.right)
- {
- this.bounds.left = Position.get(this.handle)[0] - this.offset.wrapper[0];
- this.bounds.right = -this.bounds.left;
- }
- if(!this.bounds.top && !this.bounds.bottom)
- {
- this.bounds.top = Position.get(this.handle)[1] - this.offset.wrapper[1];
- this.bounds.bottom = -this.bounds.top;
- }
- },
- setBounds: function()
- {
- this.bounds.x0 = this.bounds.left;
- this.bounds.x1 = this.wrapper.offsetWidth + this.bounds.right;
- this.bounds.xRange = (this.bounds.x1 - this.bounds.x0) - this.handle.offsetWidth;
-
- this.bounds.y0 = this.bounds.top;
- this.bounds.y1 = this.wrapper.offsetHeight + this.bounds.bottom;
- this.bounds.yRange = (this.bounds.y1 - this.bounds.y0) - this.handle.offsetHeight;
-
- this.bounds.xStep = 1 / (this.xPrecision || Math.max(this.wrapper.offsetWidth, this.handle.offsetWidth));
- this.bounds.yStep = 1 / (this.yPrecision || Math.max(this.wrapper.offsetHeight, this.handle.offsetHeight));
- },
- setSteps: function()
- {
- if(this.steps > 1)
- {
- this.stepRatios = [];
- for(var i = 0; i <= this.steps - 1; i++)
- {
- this.stepRatios[i] = i / (this.steps - 1);
- }
- }
- },
- addListeners: function()
- {
- var self = this;
-
- this.wrapper.onselectstart = function()
- {
- return false;
- }
- this.handle.onmousedown = this.handle.ontouchstart = function(e)
- {
- self.handleDownHandler(e);
- };
- this.wrapper.onmousedown = this.wrapper.ontouchstart = function(e)
- {
- self.wrapperDownHandler(e);
- };
- var mouseUpHandler = document.onmouseup || function(){};
- document.onmouseup = function(e)
- {
- mouseUpHandler(e);
- self.documentUpHandler(e);
- };
- var touchEndHandler = document.ontouchend || function(){};
- document.ontouchend = function(e)
- {
- touchEndHandler(e);
- self.documentUpHandler(e);
- };
- var resizeHandler = window.onresize || function(){};
- window.onresize = function(e)
- {
- resizeHandler(e);
- self.documentResizeHandler(e);
- };
- this.wrapper.onmousemove = function(e)
- {
- self.activity = true;
- }
- this.wrapper.onclick = function(e)
- {
- return !self.activity;
- }
-
- this.interval = setInterval(function(){ self.animate() }, 25);
- self.animate(false, true);
- },
- handleDownHandler: function(e)
- {
- this.activity = false;
- Cursor.refresh(e);
-
- this.preventDefaults(e, true);
- this.startDrag();
- },
- wrapperDownHandler: function(e)
- {
- Cursor.refresh(e);
-
- this.preventDefaults(e, true);
- this.startTap();
- },
- documentUpHandler: function(e)
- {
- this.stopDrag();
- this.stopTap();
- },
- documentResizeHandler: function(e)
- {
- this.setWrapperOffset();
- this.setBounds();
-
- this.update();
- },
- enable: function()
- {
- this.disabled = false;
- this.handle.className = this.handle.className.replace(/\s?disabled/g, '');
- },
- disable: function()
- {
- this.disabled = true;
- this.handle.className += ' disabled';
- },
- setStep: function(x, y, snap)
- {
- this.setValue(
- this.steps && x > 1 ? (x - 1) / (this.steps - 1) : 0,
- this.steps && y > 1 ? (y - 1) / (this.steps - 1) : 0,
- snap
- );
- },
- setValue: function(x, y, snap)
- {
- this.setTargetValue([x, y || 0]);
- if(snap)
- {
- this.groupCopy(this.value.current, this.value.target);
- }
- },
- startTap: function(target)
- {
- if(this.disabled)
- {
- return;
- }
- this.tapping = true;
- this.setWrapperOffset();
- this.setBounds();
- if(target === undefined)
- {
- target = [
- Cursor.x - this.offset.wrapper[0] - (this.handle.offsetWidth / 2),
- Cursor.y - this.offset.wrapper[1] - (this.handle.offsetHeight / 2)
- ];
- }
- this.setTargetOffset(target);
- },
- stopTap: function()
- {
- if(this.disabled || !this.tapping)
- {
- return;
- }
- this.tapping = false;
-
- this.setTargetValue(this.value.current);
- this.result();
- },
- startDrag: function()
- {
- if(this.disabled)
- {
- return;
- }
- this.setWrapperOffset();
- this.setBounds();
- this.offset.mouse = [
- Cursor.x - Position.get(this.handle)[0],
- Cursor.y - Position.get(this.handle)[1]
- ];
-
- this.dragging = true;
- },
- stopDrag: function()
- {
- if(this.disabled || !this.dragging)
- {
- return;
- }
- this.dragging = false;
-
- var target = this.groupClone(this.value.current);
- if(this.slide)
- {
- var ratioChange = this.change;
- target[0] += ratioChange[0] * 4;
- target[1] += ratioChange[1] * 4;
- }
- this.setTargetValue(target);
- this.result();
- },
- feedback: function()
- {
- var value = this.value.current;
- if(this.snap && this.steps > 1)
- {
- value = this.getClosestSteps(value);
- }
- if(!this.groupCompare(value, this.value.prev))
- {
- if(typeof(this.animationCallback) == 'function')
- {
- this.animationCallback(value[0], value[1]);
- }
- this.groupCopy(this.value.prev, value);
- }
- },
- result: function()
- {
- if(typeof(this.callback) == 'function')
- {
- this.callback(this.value.target[0], this.value.target[1]);
- }
- },
- animate: function(direct, first)
- {
- if(direct && !this.dragging)
- {
- return;
- }
- if(this.dragging)
- {
- var prevTarget = this.groupClone(this.value.target);
-
- var offset = [
- Cursor.x - this.offset.wrapper[0] - this.offset.mouse[0],
- Cursor.y - this.offset.wrapper[1] - this.offset.mouse[1]
- ];
- this.setTargetOffset(offset, this.loose);
-
- this.change = [
- this.value.target[0] - prevTarget[0],
- this.value.target[1] - prevTarget[1]
- ];
- }
- if(this.dragging || first)
- {
- this.groupCopy(this.value.current, this.value.target);
- }
- if(this.dragging || this.glide() || first)
- {
- this.update();
- this.feedback();
- }
- },
- glide: function()
- {
- var diff = [
- this.value.target[0] - this.value.current[0],
- this.value.target[1] - this.value.current[1]
- ];
- if(!diff[0] && !diff[1])
- {
- return false;
- }
- if(Math.abs(diff[0]) > this.bounds.xStep || Math.abs(diff[1]) > this.bounds.yStep)
- {
- this.value.current[0] += diff[0] * this.speed;
- this.value.current[1] += diff[1] * this.speed;
- }
- else
- {
- this.groupCopy(this.value.current, this.value.target);
- }
- return true;
- },
- update: function()
- {
- if(!this.snap)
- {
- this.offset.current = this.getOffsetsByRatios(this.value.current);
- }
- else
- {
- this.offset.current = this.getOffsetsByRatios(
- this.getClosestSteps(this.value.current)
- );
- }
- this.show();
- },
- show: function()
- {
- if(!this.groupCompare(this.offset.current, this.offset.prev))
- {
- if(this.horizontal)
- {
- this.handle.style.left = String(this.offset.current[0]) + 'px';
- }
- if(this.vertical)
- {
- this.handle.style.top = String(this.offset.current[1]) + 'px';
- }
- this.groupCopy(this.offset.prev, this.offset.current);
- }
- },
- setTargetValue: function(value, loose)
- {
- var target = loose ? this.getLooseValue(value) : this.getProperValue(value);
-
- this.groupCopy(this.value.target, target);
- this.offset.target = this.getOffsetsByRatios(target);
- },
- setTargetOffset: function(offset, loose)
- {
- var value = this.getRatiosByOffsets(offset);
- var target = loose ? this.getLooseValue(value) : this.getProperValue(value);
-
- this.groupCopy(this.value.target, target);
- this.offset.target = this.getOffsetsByRatios(target);
- },
- getLooseValue: function(value)
- {
- var proper = this.getProperValue(value);
- return [
- proper[0] + ((value[0] - proper[0]) / 4),
- proper[1] + ((value[1] - proper[1]) / 4)
- ];
- },
- getProperValue: function(value)
- {
- var proper = this.groupClone(value);
- proper[0] = Math.max(proper[0], 0);
- proper[1] = Math.max(proper[1], 0);
- proper[0] = Math.min(proper[0], 1);
- proper[1] = Math.min(proper[1], 1);
- if((!this.dragging && !this.tapping) || this.snap)
- {
- if(this.steps > 1)
- {
- proper = this.getClosestSteps(proper);
- }
- }
- return proper;
- },
- getRatiosByOffsets: function(group)
- {
- return [
- this.getRatioByOffset(group[0], this.bounds.xRange, this.bounds.x0),
- this.getRatioByOffset(group[1], this.bounds.yRange, this.bounds.y0)
- ];
- },
- getRatioByOffset: function(offset, range, padding)
- {
- return range ? (offset - padding) / range : 0;
- },
- getOffsetsByRatios: function(group)
- {
- return [
- this.getOffsetByRatio(group[0], this.bounds.xRange, this.bounds.x0),
- this.getOffsetByRatio(group[1], this.bounds.yRange, this.bounds.y0)
- ];
- },
- getOffsetByRatio: function(ratio, range, padding)
- {
- return Math.round(ratio * range) + padding;
- },
- getClosestSteps: function(group)
- {
- return [
- this.getClosestStep(group[0]),
- this.getClosestStep(group[1])
- ];
- },
- getClosestStep: function(value)
- {
- var k = 0;
- var min = 1;
- for(var i = 0; i <= this.steps - 1; i++)
- {
- if(Math.abs(this.stepRatios[i] - value) < min)
- {
- min = Math.abs(this.stepRatios[i] - value);
- k = i;
- }
- }
- return this.stepRatios[k];
- },
- groupCompare: function(a, b)
- {
- return a[0] == b[0] && a[1] == b[1];
- },
- groupCopy: function(a, b)
- {
- a[0] = b[0];
- a[1] = b[1];
- },
- groupClone: function(a)
- {
- return [a[0], a[1]];
- },
- preventDefaults: function(e, selection)
- {
- if(!e)
- {
- e = window.event;
- }
- if(e.preventDefault)
- {
- e.preventDefault();
- }
- e.returnValue = false;
-
- if(selection && document.selection)
- {
- document.selection.empty();
- }
- },
- cancelEvent: function(e)
- {
- if(!e)
- {
- e = window.event;
- }
- if(e.stopPropagation)
- {
- e.stopPropagation();
- }
- e.cancelBubble = true;
- }
- };
- /*! Copyright (c) 2013 Brandon Aaron (http://brandonaaron.net)
- * Licensed under the MIT License (LICENSE.txt).
- *
- * Thanks to: http://adomas.org/javascript-mouse-wheel/ for some pointers.
- * Thanks to: Mathias Bank(http://www.mathias-bank.de) for a scope bug fix.
- * Thanks to: Seamus Leahy for adding deltaX and deltaY
- *
- * Version: 3.1.3
- *
- * Requires: 1.2.2+
- */
- (function (factory) {
- if ( typeof define === 'function' && define.amd ) {
- // AMD. Register as an anonymous module.
- define(['jquery'], factory);
- } else if (typeof exports === 'object') {
- // Node/CommonJS style for Browserify
- module.exports = factory;
- } else {
- // Browser globals
- factory(jQuery);
- }
- }(function ($) {
- var toFix = ['wheel', 'mousewheel', 'DOMMouseScroll', 'MozMousePixelScroll'];
- var toBind = 'onwheel' in document || document.documentMode >= 9 ? ['wheel'] : ['mousewheel', 'DomMouseScroll', 'MozMousePixelScroll'];
- var lowestDelta, lowestDeltaXY;
- if ( $.event.fixHooks ) {
- for ( var i = toFix.length; i; ) {
- $.event.fixHooks[ toFix[--i] ] = $.event.mouseHooks;
- }
- }
- $.event.special.mousewheel = {
- setup: function() {
- if ( this.addEventListener ) {
- for ( var i = toBind.length; i; ) {
- this.addEventListener( toBind[--i], handler, false );
- }
- } else {
- this.onmousewheel = handler;
- }
- },
- teardown: function() {
- if ( this.removeEventListener ) {
- for ( var i = toBind.length; i; ) {
- this.removeEventListener( toBind[--i], handler, false );
- }
- } else {
- this.onmousewheel = null;
- }
- }
- };
- $.fn.extend({
- mousewheel: function(fn) {
- return fn ? this.bind("mousewheel", fn) : this.trigger("mousewheel");
- },
- unmousewheel: function(fn) {
- return this.unbind("mousewheel", fn);
- }
- });
- function handler(event) {
- var orgEvent = event || window.event,
- args = [].slice.call(arguments, 1),
- delta = 0,
- deltaX = 0,
- deltaY = 0,
- absDelta = 0,
- absDeltaXY = 0,
- fn;
- event = $.event.fix(orgEvent);
- event.type = "mousewheel";
- // Old school scrollwheel delta
- if ( orgEvent.wheelDelta ) { delta = orgEvent.wheelDelta; }
- if ( orgEvent.detail ) { delta = orgEvent.detail * -1; }
- // New school wheel delta (wheel event)
- if ( orgEvent.deltaY ) {
- deltaY = orgEvent.deltaY * -1;
- delta = deltaY;
- }
- if ( orgEvent.deltaX ) {
- deltaX = orgEvent.deltaX;
- delta = deltaX * -1;
- }
- // Webkit
- if ( orgEvent.wheelDeltaY !== undefined ) { deltaY = orgEvent.wheelDeltaY; }
- if ( orgEvent.wheelDeltaX !== undefined ) { deltaX = orgEvent.wheelDeltaX * -1; }
- // Look for lowest delta to normalize the delta values
- absDelta = Math.abs(delta);
- if ( !lowestDelta || absDelta < lowestDelta ) { lowestDelta = absDelta; }
- absDeltaXY = Math.max(Math.abs(deltaY), Math.abs(deltaX));
- if ( !lowestDeltaXY || absDeltaXY < lowestDeltaXY ) { lowestDeltaXY = absDeltaXY; }
- // Get a whole value for the deltas
- fn = delta > 0 ? 'floor' : 'ceil';
- delta = Math[fn](delta / lowestDelta);
- deltaX = Math[fn](deltaX / lowestDeltaXY);
- deltaY = Math[fn](deltaY / lowestDeltaXY);
- // Add event and delta to the front of the arguments
- args.unshift(event, delta, deltaX, deltaY);
- return ($.event.dispatch || $.event.handle).apply(this, args);
- }
- }));
- /**
- * Array.filter() shim by Trevor Menagh (https://github.com/trevmex) with some modifications
- */
- if (!Array.prototype.filter) {
- Array.prototype.filter = function (fun, thisp) {
- "use strict";
- if (typeof this === "undefined" || this === null) {
- throw new TypeError();
- }
- if (typeof fun !== "function") {
- throw new TypeError();
- }
- thisp = thisp || this;
- if (isNodeList(thisp)) {
- thisp = convertNodeListToArray(thisp);
- }
- var len = thisp.length,
- res = [],
- i,
- val;
- for (i = 0; i < len; i += 1) {
- if (thisp.hasOwnProperty(i)) {
- val = thisp[i]; // in case fun mutates this
- if (fun.call(thisp, val, i, thisp)) {
- res.push(val);
- }
- }
- }
- return res;
- function isNodeList(object) {
- return /NodeList/i.test(object.item);
- }
- function convertNodeListToArray(nodeList) {
- var array = [];
- for (var i = 0, len = nodeList.length; i < len; i++){
- array[i] = nodeList[i]
- }
- return array;
- }
- };
- }
- })(jQuery, window, Handsontable);
- // numeral.js
- // version : 1.4.7
- // author : Adam Draper
- // license : MIT
- // http://adamwdraper.github.com/Numeral-js/
- (function () {
- /************************************
- Constants
- ************************************/
- var numeral,
- VERSION = '1.4.7',
- // internal storage for language config files
- languages = {},
- currentLanguage = 'en',
- zeroFormat = null,
- // check for nodeJS
- hasModule = (typeof module !== 'undefined' && module.exports);
- /************************************
- Constructors
- ************************************/
- // Numeral prototype object
- function Numeral (number) {
- this._n = number;
- }
- /**
- * Implementation of toFixed() that treats floats more like decimals
- *
- * Fixes binary rounding issues (eg. (0.615).toFixed(2) === '0.61') that present
- * problems for accounting- and finance-related software.
- */
- function toFixed (value, precision, optionals) {
- var power = Math.pow(10, precision),
- output;
- // Multiply up by precision, round accurately, then divide and use native toFixed():
- output = (Math.round(value * power) / power).toFixed(precision);
- if (optionals) {
- var optionalsRegExp = new RegExp('0{1,' + optionals + '}$');
- output = output.replace(optionalsRegExp, '');
- }
- return output;
- }
- /************************************
- Formatting
- ************************************/
- // determine what type of formatting we need to do
- function formatNumeral (n, format) {
- var output;
- // figure out what kind of format we are dealing with
- if (format.indexOf('$') > -1) { // currency!!!!!
- output = formatCurrency(n, format);
- } else if (format.indexOf('%') > -1) { // percentage
- output = formatPercentage(n, format);
- } else if (format.indexOf(':') > -1) { // time
- output = formatTime(n, format);
- } else { // plain ol' numbers or bytes
- output = formatNumber(n, format);
- }
- // return string
- return output;
- }
- // revert to number
- function unformatNumeral (n, string) {
- if (string.indexOf(':') > -1) {
- n._n = unformatTime(string);
- } else {
- if (string === zeroFormat) {
- n._n = 0;
- } else {
- var stringOriginal = string;
- if (languages[currentLanguage].delimiters.decimal !== '.') {
- string = string.replace(/\./g,'').replace(languages[currentLanguage].delimiters.decimal, '.');
- }
- // see if abbreviations are there so that we can multiply to the correct number
- var thousandRegExp = new RegExp(languages[currentLanguage].abbreviations.thousand + '(?:\\)|(\\' + languages[currentLanguage].currency.symbol + ')?(?:\\))?)?$'),
- millionRegExp = new RegExp(languages[currentLanguage].abbreviations.million + '(?:\\)|(\\' + languages[currentLanguage].currency.symbol + ')?(?:\\))?)?$'),
- billionRegExp = new RegExp(languages[currentLanguage].abbreviations.billion + '(?:\\)|(\\' + languages[currentLanguage].currency.symbol + ')?(?:\\))?)?$'),
- trillionRegExp = new RegExp(languages[currentLanguage].abbreviations.trillion + '(?:\\)|(\\' + languages[currentLanguage].currency.symbol + ')?(?:\\))?)?$');
- // see if bytes are there so that we can multiply to the correct number
- var prefixes = ['KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'],
- bytesMultiplier = false;
- for (var power = 0; power <= prefixes.length; power++) {
- bytesMultiplier = (string.indexOf(prefixes[power]) > -1) ? Math.pow(1024, power + 1) : false;
- if (bytesMultiplier) {
- break;
- }
- }
- // do some math to create our number
- n._n = ((bytesMultiplier) ? bytesMultiplier : 1) * ((stringOriginal.match(thousandRegExp)) ? Math.pow(10, 3) : 1) * ((stringOriginal.match(millionRegExp)) ? Math.pow(10, 6) : 1) * ((stringOriginal.match(billionRegExp)) ? Math.pow(10, 9) : 1) * ((stringOriginal.match(trillionRegExp)) ? Math.pow(10, 12) : 1) * ((string.indexOf('%') > -1) ? 0.01 : 1) * Number(((string.indexOf('(') > -1) ? '-' : '') + string.replace(/[^0-9\.-]+/g, ''));
- // round if we are talking about bytes
- n._n = (bytesMultiplier) ? Math.ceil(n._n) : n._n;
- }
- }
- return n._n;
- }
- function formatCurrency (n, format) {
- var prependSymbol = (format.indexOf('$') <= 1) ? true : false;
- // remove $ for the moment
- var space = '';
- // check for space before or after currency
- if (format.indexOf(' $') > -1) {
- space = ' ';
- format = format.replace(' $', '');
- } else if (format.indexOf('$ ') > -1) {
- space = ' ';
- format = format.replace('$ ', '');
- } else {
- format = format.replace('$', '');
- }
- // format the number
- var output = formatNumeral(n, format);
- // position the symbol
- if (prependSymbol) {
- if (output.indexOf('(') > -1 || output.indexOf('-') > -1) {
- output = output.split('');
- output.splice(1, 0, languages[currentLanguage].currency.symbol + space);
- output = output.join('');
- } else {
- output = languages[currentLanguage].currency.symbol + space + output;
- }
- } else {
- if (output.indexOf(')') > -1) {
- output = output.split('');
- output.splice(-1, 0, space + languages[currentLanguage].currency.symbol);
- output = output.join('');
- } else {
- output = output + space + languages[currentLanguage].currency.symbol;
- }
- }
- return output;
- }
- function formatPercentage (n, format) {
- var space = '';
- // check for space before %
- if (format.indexOf(' %') > -1) {
- space = ' ';
- format = format.replace(' %', '');
- } else {
- format = format.replace('%', '');
- }
- n._n = n._n * 100;
- var output = formatNumeral(n, format);
- if (output.indexOf(')') > -1 ) {
- output = output.split('');
- output.splice(-1, 0, space + '%');
- output = output.join('');
- } else {
- output = output + space + '%';
- }
- return output;
- }
- function formatTime (n, format) {
- var hours = Math.floor(n._n/60/60),
- minutes = Math.floor((n._n - (hours * 60 * 60))/60),
- seconds = Math.round(n._n - (hours * 60 * 60) - (minutes * 60));
- return hours + ':' + ((minutes < 10) ? '0' + minutes : minutes) + ':' + ((seconds < 10) ? '0' + seconds : seconds);
- }
- function unformatTime (string) {
- var timeArray = string.split(':'),
- seconds = 0;
- // turn hours and minutes into seconds and add them all up
- if (timeArray.length === 3) {
- // hours
- seconds = seconds + (Number(timeArray[0]) * 60 * 60);
- // minutes
- seconds = seconds + (Number(timeArray[1]) * 60);
- // seconds
- seconds = seconds + Number(timeArray[2]);
- } else if (timeArray.lenght === 2) {
- // minutes
- seconds = seconds + (Number(timeArray[0]) * 60);
- // seconds
- seconds = seconds + Number(timeArray[1]);
- }
- return Number(seconds);
- }
- function formatNumber (n, format) {
- var negP = false,
- optDec = false,
- abbr = '',
- bytes = '',
- ord = '',
- abs = Math.abs(n._n);
- // check if number is zero and a custom zero format has been set
- if (n._n === 0 && zeroFormat !== null) {
- return zeroFormat;
- } else {
- // see if we should use parentheses for negative number
- if (format.indexOf('(') > -1) {
- negP = true;
- format = format.slice(1, -1);
- }
- // see if abbreviation is wanted
- if (format.indexOf('a') > -1) {
- // check for space before abbreviation
- if (format.indexOf(' a') > -1) {
- abbr = ' ';
- format = format.replace(' a', '');
- } else {
- format = format.replace('a', '');
- }
- if (abs >= Math.pow(10, 12)) {
- // trillion
- abbr = abbr + languages[currentLanguage].abbreviations.trillion;
- n._n = n._n / Math.pow(10, 12);
- } else if (abs < Math.pow(10, 12) && abs >= Math.pow(10, 9)) {
- // billion
- abbr = abbr + languages[currentLanguage].abbreviations.billion;
- n._n = n._n / Math.pow(10, 9);
- } else if (abs < Math.pow(10, 9) && abs >= Math.pow(10, 6)) {
- // million
- abbr = abbr + languages[currentLanguage].abbreviations.million;
- n._n = n._n / Math.pow(10, 6);
- } else if (abs < Math.pow(10, 6) && abs >= Math.pow(10, 3)) {
- // thousand
- abbr = abbr + languages[currentLanguage].abbreviations.thousand;
- n._n = n._n / Math.pow(10, 3);
- }
- }
- // see if we are formatting bytes
- if (format.indexOf('b') > -1) {
- // check for space before
- if (format.indexOf(' b') > -1) {
- bytes = ' ';
- format = format.replace(' b', '');
- } else {
- format = format.replace('b', '');
- }
- var prefixes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'],
- min,
- max;
- for (var power = 0; power <= prefixes.length; power++) {
- min = Math.pow(1024, power);
- max = Math.pow(1024, power+1);
- if (n._n >= min && n._n < max) {
- bytes = bytes + prefixes[power];
- if (min > 0) {
- n._n = n._n / min;
- }
- break;
- }
- }
- }
- // see if ordinal is wanted
- if (format.indexOf('o') > -1) {
- // check for space before
- if (format.indexOf(' o') > -1) {
- ord = ' ';
- format = format.replace(' o', '');
- } else {
- format = format.replace('o', '');
- }
- ord = ord + languages[currentLanguage].ordinal(n._n);
- }
- if (format.indexOf('[.]') > -1) {
- optDec = true;
- format = format.replace('[.]', '.');
- }
- var w = n._n.toString().split('.')[0],
- precision = format.split('.')[1],
- thousands = format.indexOf(','),
- d = '',
- neg = false;
- if (precision) {
- if (precision.indexOf('[') > -1) {
- precision = precision.replace(']', '');
- precision = precision.split('[');
- d = toFixed(n._n, (precision[0].length + precision[1].length), precision[1].length);
- } else {
- d = toFixed(n._n, precision.length);
- }
- w = d.split('.')[0];
- if (d.split('.')[1].length) {
- d = languages[currentLanguage].delimiters.decimal + d.split('.')[1];
- } else {
- d = '';
- }
- if (optDec && Number(d) === 0) {
- d = '';
- }
- } else {
- w = toFixed(n._n, null);
- }
- // format number
- if (w.indexOf('-') > -1) {
- w = w.slice(1);
- neg = true;
- }
- if (thousands > -1) {
- w = w.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1' + languages[currentLanguage].delimiters.thousands);
- }
- if (format.indexOf('.') === 0) {
- w = '';
- }
- return ((negP && neg) ? '(' : '') + ((!negP && neg) ? '-' : '') + w + d + ((ord) ? ord : '') + ((abbr) ? abbr : '') + ((bytes) ? bytes : '') + ((negP && neg) ? ')' : '');
- }
- }
- /************************************
- Top Level Functions
- ************************************/
- numeral = function (input) {
- if (numeral.isNumeral(input)) {
- input = input.value();
- } else if (!Number(input)) {
- input = 0;
- }
- return new Numeral(Number(input));
- };
- // version number
- numeral.version = VERSION;
- // compare numeral object
- numeral.isNumeral = function (obj) {
- return obj instanceof Numeral;
- };
- // This function will load languages and then set the global language. If
- // no arguments are passed in, it will simply return the current global
- // language key.
- numeral.language = function (key, values) {
- if (!key) {
- return currentLanguage;
- }
- if (key && !values) {
- currentLanguage = key;
- }
- if (values || !languages[key]) {
- loadLanguage(key, values);
- }
- return numeral;
- };
- numeral.language('en', {
- delimiters: {
- thousands: ',',
- decimal: '.'
- },
- abbreviations: {
- thousand: 'k',
- million: 'm',
- billion: 'b',
- trillion: 't'
- },
- ordinal: function (number) {
- var b = number % 10;
- return (~~ (number % 100 / 10) === 1) ? 'th' :
- (b === 1) ? 'st' :
- (b === 2) ? 'nd' :
- (b === 3) ? 'rd' : 'th';
- },
- currency: {
- symbol: '$'
- }
- });
- numeral.zeroFormat = function (format) {
- if (typeof(format) === 'string') {
- zeroFormat = format;
- } else {
- zeroFormat = null;
- }
- };
- /************************************
- Helpers
- ************************************/
- function loadLanguage(key, values) {
- languages[key] = values;
- }
- /************************************
- Numeral Prototype
- ************************************/
- numeral.fn = Numeral.prototype = {
- clone : function () {
- return numeral(this);
- },
- format : function (inputString) {
- return formatNumeral(this, inputString ? inputString : numeral.defaultFormat);
- },
- unformat : function (inputString) {
- return unformatNumeral(this, inputString ? inputString : numeral.defaultFormat);
- },
- value : function () {
- return this._n;
- },
- valueOf : function () {
- return this._n;
- },
- set : function (value) {
- this._n = Number(value);
- return this;
- },
- add : function (value) {
- this._n = this._n + Number(value);
- return this;
- },
- subtract : function (value) {
- this._n = this._n - Number(value);
- return this;
- },
- multiply : function (value) {
- this._n = this._n * Number(value);
- return this;
- },
- divide : function (value) {
- this._n = this._n / Number(value);
- return this;
- },
- difference : function (value) {
- var difference = this._n - Number(value);
- if (difference < 0) {
- difference = -difference;
- }
- return difference;
- }
- };
- /************************************
- Exposing Numeral
- ************************************/
- // CommonJS module is defined
- if (hasModule) {
- module.exports = numeral;
- }
- /*global ender:false */
- if (typeof ender === 'undefined') {
- // here, `this` means `window` in the browser, or `global` on the server
- // add `numeral` as a global object via a string identifier,
- // for Closure Compiler 'advanced' mode
- this['numeral'] = numeral;
- }
- /*global define:false */
- if (typeof define === 'function' && define.amd) {
- define([], function () {
- return numeral;
- });
- }
- }).call(this);
|