| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483 |
- var view = {};
- var base = require('./base');
- var zip = require('./zip');
- var tar = require('./tar');
- var json = require('./json');
- var xml = require('./xml');
- var protobuf = require('./protobuf');
- var flatbuffers = require('./flatbuffers');
- var hdf5 = require('./hdf5');
- var python = require('./python');
- var grapher = require('./grapher');
- view.View = class {
- constructor(host) {
- this._host = host;
- this._options = {
- weights: true,
- attributes: false,
- names: false,
- direction: 'vertical',
- mousewheel: 'scroll'
- };
- this._host.view(this).then(() => {
- this._model = null;
- this._graphs = [];
- this._selection = [];
- this._sidebar = new view.Sidebar(this._host);
- this._searchText = '';
- this._modelFactoryService = new view.ModelFactoryService(this._host);
- this._element('sidebar-button').addEventListener('click', () => {
- this.showModelProperties();
- });
- this._element('zoom-in-button').addEventListener('click', () => {
- this.zoomIn();
- });
- this._element('zoom-out-button').addEventListener('click', () => {
- this.zoomOut();
- });
- this._element('back-button').addEventListener('click', () => {
- this.popGraph();
- });
- this._element('name-button').addEventListener('click', () => {
- this.showDocumentation(this.activeGraph);
- });
- this._element('sidebar').addEventListener('mousewheel', (e) => {
- if (e.shiftKey || e.ctrlKey) {
- e.preventDefault();
- }
- }, { passive: true });
- this._host.document.addEventListener('keydown', () => {
- if (this._graph) {
- this._graph.select(null);
- }
- });
- const platform = this._host.environment('platform');
- this._menu = new view.Menu(this._host);
- this._menu.add({
- accelerator: platform === 'darwin' ? 'Ctrl+Cmd+F' : 'F11',
- execute: () => this._host.execute('fullscreen')
- });
- if (this._host.environment('menu')) {
- this._menu.attach(this._element('menu'), this._element('menu-button'));
- const file = this._menu.group('&File');
- file.add({
- label: '&Open...',
- accelerator: 'CmdOrCtrl+O',
- execute: () => this._host.execute('open')
- });
- if (this._host.type === 'Electron') {
- this._recents = file.group('Open &Recent');
- file.add({
- label: '&Export...',
- accelerator: 'CmdOrCtrl+Shift+E',
- execute: () => this._host.execute('export'),
- enabled: () => this.activeGraph
- });
- file.add({
- label: platform === 'darwin' ? '&Close Window' : '&Close',
- accelerator: 'CmdOrCtrl+W',
- execute: () => this._host.execute('close'),
- });
- file.add({
- label: platform === 'win32' ? 'E&xit' : '&Quit',
- accelerator: platform === 'win32' ? '' : 'CmdOrCtrl+Q',
- execute: () => this._host.execute('quit'),
- });
- } else {
- file.add({
- label: 'Export as &PNG',
- accelerator: 'CmdOrCtrl+Shift+E',
- execute: () => this.export(this._host.document.title + '.png'),
- enabled: () => this.activeGraph
- });
- file.add({
- label: 'Export as &SVG',
- accelerator: 'CmdOrCtrl+Alt+E',
- execute: () => this.export(this._host.document.title + '.svg'),
- enabled: () => this.activeGraph
- });
- }
- const edit = this._menu.group('&Edit');
- edit.add({
- label: '&Find...',
- accelerator: 'CmdOrCtrl+F',
- execute: () => this.find(),
- enabled: () => this.activeGraph
- });
- const view = this._menu.group('&View');
- view.add({
- label: () => this.options.attributes ? 'Hide &Attributes' : 'Show &Attributes',
- accelerator: 'CmdOrCtrl+D',
- execute: () => this.toggle('attributes'),
- enabled: () => this.activeGraph
- });
- view.add({
- label: () => this.options.weights ? 'Hide &Weights' : 'Show &Weights',
- accelerator: 'CmdOrCtrl+I',
- execute: () => this.toggle('weights'),
- enabled: () => this.activeGraph
- });
- view.add({
- label: () => this.options.names ? 'Hide &Names' : 'Show &Names',
- accelerator: 'CmdOrCtrl+U',
- execute: () => this.toggle('names'),
- enabled: () => this.activeGraph
- });
- view.add({
- label: () => this.options.direction === 'vertical' ? 'Show &Horizontal' : 'Show &Vertical',
- accelerator: 'CmdOrCtrl+K',
- execute: () => this.toggle('direction'),
- enabled: () => this.activeGraph
- });
- view.add({
- label: () => this.options.mousewheel === 'scroll' ? '&Mouse Wheel: Zoom' : '&Mouse Wheel: Scroll',
- accelerator: 'CmdOrCtrl+M',
- execute: () => this.toggle('mousewheel'),
- enabled: () => this.activeGraph
- });
- view.add({});
- if (this._host.type === 'Electron') {
- view.add({
- label: '&Reload',
- accelerator: platform === 'darwin' ? 'CmdOrCtrl+R' : 'F5',
- execute: () => this._host.execute('reload'),
- enabled: () => this.activeGraph
- });
- view.add({});
- }
- view.add({
- label: 'Zoom &In',
- accelerator: 'Shift+Up',
- execute: () => this.zoomIn(),
- enabled: () => this.activeGraph
- });
- view.add({
- label: 'Zoom &Out',
- accelerator: 'Shift+Down',
- execute: () => this.zoomOut(),
- enabled: () => this.activeGraph
- });
- view.add({
- label: 'Actual &Size',
- accelerator: 'Shift+Backspace',
- execute: () => this.resetZoom(),
- enabled: () => this.activeGraph
- });
- view.add({});
- view.add({
- label: '&Properties...',
- accelerator: 'CmdOrCtrl+Enter',
- execute: () => this.showModelProperties(),
- enabled: () => this.activeGraph
- });
- if (this._host.type === 'Electron' && !this._host.environment('packaged')) {
- view.add({});
- view.add({
- label: '&Developer Tools...',
- accelerator: 'CmdOrCtrl+Alt+I',
- execute: () => this._host.execute('toggle-developer-tools')
- });
- }
- const help = this._menu.group('&Help');
- help.add({
- label: 'Report &Issue',
- execute: () => this._host.execute('report-issue')
- });
- help.add({
- label: '&About ' + this._host.environment('name'),
- execute: () => this._host.execute('about')
- });
- }
- this._host.start();
- }).catch((err) => {
- this.error(err, null, null);
- });
- }
- show(page) {
- if (!page) {
- page = (!this._model && !this.activeGraph) ? 'welcome' : 'default';
- }
- this._host.event('screen_view', {
- screen_name: page,
- });
- if (this._sidebar) {
- this._sidebar.close();
- }
- if (this._menu) {
- this._menu.close();
- }
- this._host.document.body.classList.remove(...Array.from(this._host.document.body.classList).filter((_) => _ !== 'active'));
- this._host.document.body.classList.add(...page.split(' '));
- if (page === 'default') {
- this._activate();
- } else {
- this._deactivate();
- }
- if (page === 'welcome') {
- const element = this._element('open-file-button');
- if (element) {
- element.focus();
- }
- }
- this._page = page;
- }
- progress(percent) {
- const bar = this._element('progress-bar');
- if (bar) {
- bar.style.width = percent.toString() + '%';
- }
- }
- cut() {
- this._host.document.execCommand('cut');
- }
- copy() {
- this._host.document.execCommand('copy');
- }
- paste() {
- this._host.document.execCommand('paste');
- }
- selectAll() {
- this._host.document.execCommand('selectall');
- }
- find() {
- if (this._graph) {
- this._graph.select(null);
- const content = new view.FindSidebar(this._host, this.activeGraph);
- content.on('search-text-changed', (sender, text) => {
- this._searchText = text;
- });
- content.on('select', (sender, selection) => {
- this.scrollTo(this._graph.select([ selection ]));
- });
- content.on('focus', (sender, selection) => {
- this._graph.focus([ selection ]);
- });
- content.on('blur', (sender, selection) => {
- this._graph.blur([ selection ]);
- });
- this._sidebar.open(content.render(), 'Find');
- content.focus(this._searchText);
- }
- }
- get model() {
- return this._model;
- }
- get options() {
- return this._options;
- }
- toggle(name) {
- switch (name) {
- case 'names':
- case 'attributes':
- case 'weights':
- this._options[name] = !this._options[name];
- this._reload();
- break;
- case 'direction':
- this._options.direction = this._options.direction === 'vertical' ? 'horizontal' : 'vertical';
- this._reload();
- break;
- case 'mousewheel':
- this._options.mousewheel = this._options.mousewheel === 'scroll' ? 'zoom' : 'scroll';
- break;
- default:
- throw new view.Error("Unsupported toogle '" + name + "'.");
- }
- }
- recents(recents) {
- if (this._recents) {
- this._recents.clear();
- for (let i = 0; i < recents.length; i++) {
- const recent = recents[i];
- this._recents.add({
- label: recent.label,
- accelerator: 'CmdOrCtrl+' + (i + 1).toString(),
- execute: () => this._host.execute('open', recent.path)
- });
- }
- }
- }
- _reload() {
- this.show('welcome spinner');
- if (this._model && this._graphs.length > 0) {
- this._updateGraph(this._model, this._graphs).catch((error) => {
- if (error) {
- this.error(error, 'Graph update failed.', 'welcome');
- }
- });
- }
- }
- _timeout(delay) {
- return new Promise((resolve) => {
- setTimeout(resolve, delay);
- });
- }
- _element(id) {
- return this._host.document.getElementById(id);
- }
- zoomIn() {
- this._updateZoom(this._zoom * 1.1);
- }
- zoomOut() {
- this._updateZoom(this._zoom * 0.9);
- }
- resetZoom() {
- this._updateZoom(1);
- }
- _activate() {
- if (!this._events) {
- this._events = {};
- this._events.scroll = (e) => this._scrollHandler(e);
- this._events.wheel = (e) => this._wheelHandler(e);
- this._events.gesturestart = (e) => this._gestureStartHandler(e);
- this._events.pointerdown = (e) => this._pointerDownHandler(e);
- this._events.touchstart = (e) => this._touchStartHandler(e);
- }
- const graph = this._element('graph');
- graph.focus();
- graph.addEventListener('scroll', this._events.scroll);
- graph.addEventListener('wheel', this._events.wheel, { passive: false });
- graph.addEventListener('pointerdown', this._events.pointerdown);
- if (this._host.environment('agent') === 'safari') {
- graph.addEventListener('gesturestart', this._events.gesturestart, false);
- } else {
- graph.addEventListener('touchstart', this._events.touchstart, { passive: true });
- }
- }
- _deactivate() {
- if (this._events) {
- const graph = this._element('graph');
- graph.removeEventListener('scroll', this._events.scroll);
- graph.removeEventListener('wheel', this._events.wheel);
- graph.removeEventListener('pointerdown', this._events.pointerdown);
- graph.removeEventListener('gesturestart', this._events.gesturestart);
- graph.removeEventListener('touchstart', this._events.touchstart);
- }
- }
- _updateZoom(zoom, e) {
- const container = this._element('graph');
- const canvas = this._element('canvas');
- const limit = this._options.direction === 'vertical' ?
- container.clientHeight / this._height :
- container.clientWidth / this._width;
- const min = Math.min(Math.max(limit, 0.15), 1);
- zoom = Math.max(min, Math.min(zoom, 1.4));
- const scrollLeft = this._scrollLeft || container.scrollLeft;
- const scrollTop = this._scrollTop || container.scrollTop;
- const x = (e ? e.pageX : (container.clientWidth / 2)) + scrollLeft;
- const y = (e ? e.pageY : (container.clientHeight / 2)) + scrollTop;
- const width = zoom * this._width;
- const height = zoom * this._height;
- canvas.style.width = width + 'px';
- canvas.style.height = height + 'px';
- this._scrollLeft = Math.max(0, ((x * zoom) / this._zoom) - (x - scrollLeft));
- this._scrollTop = Math.max(0, ((y * zoom) / this._zoom) - (y - scrollTop));
- container.scrollLeft = this._scrollLeft;
- container.scrollTop = this._scrollTop;
- this._zoom = zoom;
- }
- _pointerDownHandler(e) {
- if (e.pointerType !== 'touch' && e.buttons === 1) {
- e.target.setPointerCapture(e.pointerId);
- const document = this._host.document.documentElement;
- const container = this._element('graph');
- this._mousePosition = {
- left: container.scrollLeft,
- top: container.scrollTop,
- x: e.clientX,
- y: e.clientY
- };
- container.style.cursor = 'grabbing';
- e.stopImmediatePropagation();
- const pointerMoveHandler = (e) => {
- e.preventDefault();
- e.stopImmediatePropagation();
- if (this._mousePosition) {
- const dx = e.clientX - this._mousePosition.x;
- const dy = e.clientY - this._mousePosition.y;
- this._mousePosition.moved = dx * dx + dy * dy > 0;
- if (this._mousePosition.moved) {
- const container = this._element('graph');
- container.scrollTop = this._mousePosition.top - dy;
- container.scrollLeft = this._mousePosition.left - dx;
- }
- }
- };
- const pointerUpHandler = (e) => {
- e.target.releasePointerCapture(e.pointerId);
- container.style.removeProperty('cursor');
- container.removeEventListener('pointerup', pointerUpHandler);
- container.removeEventListener('pointerleave', pointerUpHandler);
- container.removeEventListener('pointermove', pointerMoveHandler);
- if (this._mousePosition && this._mousePosition.moved) {
- e.preventDefault();
- e.stopImmediatePropagation();
- delete this._mousePosition;
- document.addEventListener('click', clickHandler, true);
- }
- };
- const clickHandler = (e) => {
- e.stopPropagation();
- document.removeEventListener('click', clickHandler, true);
- };
- container.addEventListener('pointermove', pointerMoveHandler);
- container.addEventListener('pointerup', pointerUpHandler);
- container.addEventListener('pointerleave', pointerUpHandler);
- }
- }
- _touchStartHandler(e) {
- if (e.touches.length === 2) {
- this._touchPoints = Array.from(e.touches);
- this._touchZoom = this._zoom;
- }
- const touchMoveHandler = (e) => {
- if (Array.isArray(this._touchPoints) && this._touchPoints.length === 2 && e.touches.length === 2) {
- const distance = (points) => {
- const dx =(points[1].clientX - points[0].clientX);
- const dy =(points[1].clientY - points[0].clientY);
- return Math.sqrt(dx * dx + dy * dy);
- };
- const d1 = distance(Array.from(e.touches));
- const d2 = distance(this._touchPoints);
- if (d2 !== 0) {
- const points = this._touchPoints;
- const e = {
- pageX: (points[1].pageX + points[0].pageX) / 2,
- pageY: (points[1].pageY + points[0].pageY) / 2
- };
- const zoom = d2 === 0 ? d1 : d1 / d2;
- this._updateZoom(this._touchZoom * zoom, e);
- }
- }
- };
- const touchEndHandler = () => {
- container.removeEventListener('touchmove', touchMoveHandler, { passive: true });
- container.removeEventListener('touchcancel', touchEndHandler, { passive: true });
- container.removeEventListener('touchend', touchEndHandler, { passive: true });
- delete this._touchPoints;
- delete this._touchZoom;
- };
- const container = this._element('graph');
- container.addEventListener('touchmove', touchMoveHandler, { passive: true });
- container.addEventListener('touchcancel', touchEndHandler, { passive: true });
- container.addEventListener('touchend', touchEndHandler, { passive: true });
- }
- _gestureStartHandler(e) {
- e.preventDefault();
- this._gestureZoom = this._zoom;
- const container = this._element('graph');
- const gestureChangeHandler = (e) => {
- e.preventDefault();
- this._updateZoom(this._gestureZoom * e.scale, e);
- };
- const gestureEndHandler = (e) => {
- container.removeEventListener('gesturechange', gestureChangeHandler, false);
- container.removeEventListener('gestureend', gestureEndHandler, false);
- e.preventDefault();
- if (this._gestureZoom) {
- this._updateZoom(this._gestureZoom * e.scale, e);
- delete this._gestureZoom;
- }
- };
- container.addEventListener('gesturechange', gestureChangeHandler, false);
- container.addEventListener('gestureend', gestureEndHandler, false);
- }
- _scrollHandler(e) {
- if (this._scrollLeft && e.target.scrollLeft !== Math.floor(this._scrollLeft)) {
- delete this._scrollLeft;
- }
- if (this._scrollTop && e.target.scrollTop !== Math.floor(this._scrollTop)) {
- delete this._scrollTop;
- }
- }
- _wheelHandler(e) {
- if (e.shiftKey || e.ctrlKey || this._options.mousewheel === 'zoom') {
- const delta = -e.deltaY * (e.deltaMode === 1 ? 0.05 : e.deltaMode ? 1 : 0.002) * (e.ctrlKey ? 10 : 1);
- this._updateZoom(this._zoom * Math.pow(2, delta), e);
- e.preventDefault();
- }
- }
- scrollTo(selection) {
- if (selection && selection.length > 0) {
- const container = this._element('graph');
- let x = 0;
- let y = 0;
- for (const element of selection) {
- const rect = element.getBoundingClientRect();
- x += rect.left + (rect.width / 2);
- y += rect.top + (rect.height / 2);
- }
- x = x / selection.length;
- y = y / selection.length;
- const rect = container.getBoundingClientRect();
- const left = (container.scrollLeft + x - rect.left) - (rect.width / 2);
- const top = (container.scrollTop + y - rect.top) - (rect.height / 2);
- container.scrollTo({ left: left, top: top, behavior: 'smooth' });
- }
- }
- async error(err, name, screen) {
- if (this._sidebar) {
- this._sidebar.close();
- }
- this._host.exception(err, false);
- const knowns = [
- { name: '', message: /^Invalid value identifier/, url: 'https://github.com/lutzroeder/netron/issues/540' },
- { name: '', message: /^Cannot read property/, url: 'https://github.com/lutzroeder/netron/issues/647' },
- { name: '', message: /^Failed to render tensor/, url: 'https://github.com/lutzroeder/netron/issues/681' },
- { name: 'Error', message: /^EPERM: operation not permitted/, url: 'https://github.com/lutzroeder/netron/issues/551' },
- { name: 'Error', message: /^EACCES: permission denied/, url: 'https://github.com/lutzroeder/netron/issues/504' },
- { name: 'RangeError', message: /^Offset is outside the bounds of the DataView/, url: 'https://github.com/lutzroeder/netron/issues/563' },
- { name: 'RangeError', message: /^Maximum call stack size exceeded/, url: 'https://github.com/lutzroeder/netron/issues/589' },
- { name: 'RangeError', message: /^Invalid string length/, url: 'https://github.com/lutzroeder/netron/issues/648' },
- { name: 'Python Error', message: /^Unknown function/, url: 'https://github.com/lutzroeder/netron/issues/546' },
- { name: 'Error loading model.', message: /^Unsupported file content \(/, url: 'https://github.com/lutzroeder/netron/issues/550' },
- { name: 'Error loading model.', message: /^Unsupported Protocol Buffers content/, url: 'https://github.com/lutzroeder/netron/issues/593' },
- { name: 'Error loading model.', message: /^Unsupported Protocol Buffers text content/, url: 'https://github.com/lutzroeder/netron/issues/594' },
- { name: 'Error loading model.', message: /^Unsupported JSON content/, url: 'https://github.com/lutzroeder/netron/issues/595' },
- { name: 'Error loading Caffe model.', message: /^File format is not caffe\.NetParameter/, url: 'https://github.com/lutzroeder/netron/issues/563' },
- { name: 'Error loading Darknet model.', message: /^Invalid tensor shape/, url: 'https://github.com/lutzroeder/netron/issues/541' },
- { name: 'Error loading DaVinci model.', message: /^Unsupported attribute type/, url: 'https://github.com/lutzroeder/netron/issues/926' },
- { name: 'Error loading Keras model.', message: /^Unsupported data object header version/, url: 'https://github.com/lutzroeder/netron/issues/548' },
- { name: 'Error loading MNN model.', message: /^File format is not mnn\.Net/, url: 'https://github.com/lutzroeder/netron/issues/746' },
- { name: 'Error loading NNEF model.', message: /^.*/, url: 'https://github.com/lutzroeder/netron/issues/992' },
- { name: 'Error loading PyTorch model.', message: /^File does not contain root module or state dictionary/, url: 'https://github.com/lutzroeder/netron/issues/543' },
- { name: 'Error loading PyTorch model.', message: /^Module does not contain modules/, url: 'https://github.com/lutzroeder/netron/issues/544' },
- { name: 'Error loading PyTorch model.', message: /^Unknown type name/, url: 'https://github.com/lutzroeder/netron/issues/969' },
- { name: 'Error loading ONNX model.', message: /^File format is not onnx\.ModelProto/, url: 'https://github.com/lutzroeder/netron/issues/549' },
- { name: 'Error loading TensorFlow Lite model.', message: /^Offset is outside the bounds of the DataView/, url: 'https://github.com/lutzroeder/netron/issues/563' },
- ];
- const known = knowns.find((known) => (known.name.length === 0 || known.name === err.name) && err.message.match(known.message));
- const message = err.message;
- name = name || err.name;
- const button = await this._host.error(name, message);
- const url = known && known.url ? known.url : null;
- if (button === 0 && (url || this._host.type == 'Electron')) {
- this._host.openURL(url || this._host.environment('repository') + '/issues');
- }
- this.show(screen !== undefined ? screen : 'welcome');
- }
- accept(file, size) {
- return this._modelFactoryService.accept(file, size);
- }
- async open(context) {
- this._sidebar.close();
- await this._timeout(2);
- try {
- const model = await this._modelFactoryService.open(context);
- const format = [];
- if (model.format) {
- format.push(model.format);
- }
- if (model.producer) {
- format.push('(' + model.producer + ')');
- }
- if (format.length > 0) {
- this._host.event_ua('Model', 'Format', format.join(' '));
- this._host.event('model_open', {
- model_format: model.format || '',
- model_producer: model.producer || ''
- });
- }
- await this._timeout(20);
- const graphs = Array.isArray(model.graphs) && model.graphs.length > 0 ? [ model.graphs[0] ] : [];
- return await this._updateGraph(model, graphs);
- } catch (error) {
- if (error && context.identifier) {
- error.context = context.identifier;
- }
- throw error;
- }
- }
- async _updateActiveGraph(graph) {
- this._sidebar.close();
- if (this._model) {
- const model = this._model;
- this.show('welcome spinner');
- await this._timeout(200);
- try {
- await this._updateGraph(model, [ graph ]);
- } catch (error) {
- if (error) {
- this.error(error, 'Graph update failed.', 'welcome');
- }
- }
- }
- }
- get activeGraph() {
- return Array.isArray(this._graphs) && this._graphs.length > 0 ? this._graphs[0] : null;
- }
- async _updateGraph(model, graphs) {
- await this._timeout(100);
- const graph = Array.isArray(graphs) && graphs.length > 0 ? graphs[0] : null;
- if (graph && graph != this._graphs[0]) {
- const nodes = graph.nodes;
- if (nodes.length > 2048) {
- if (!this._host.confirm('Large model detected.', 'This graph contains a large number of nodes and might take a long time to render. Do you want to continue?')) {
- this._host.event('graph_view', {
- graph_node_count: nodes.length,
- graph_skip: 1 }
- );
- this.show(null);
- return null;
- }
- }
- }
- const update = () => {
- const nameButton = this._element('name-button');
- const backButton = this._element('back-button');
- if (this._graphs.length > 1) {
- const graph = this.activeGraph;
- nameButton.innerHTML = graph ? graph.name : '';
- backButton.style.opacity = 1;
- nameButton.style.opacity = 1;
- } else {
- backButton.style.opacity = 0;
- nameButton.style.opacity = 0;
- }
- };
- const lastModel = this._model;
- const lastGraphs = this._graphs;
- this._model = model;
- this._graphs = graphs;
- try {
- await this.renderGraph(this._model, this.activeGraph);
- if (this._page !== 'default') {
- this.show('default');
- }
- update();
- return this._model;
- } catch (error) {
- this._model = lastModel;
- this._graphs = lastGraphs;
- await this.renderGraph(this._model, this.activeGraph);
- if (this._page !== 'default') {
- this.show('default');
- }
- update();
- throw error;
- }
- }
- pushGraph(graph) {
- if (graph !== this.activeGraph) {
- this._sidebar.close();
- this._updateGraph(this._model, [ graph ].concat(this._graphs));
- }
- }
- popGraph() {
- if (this._graphs.length > 1) {
- this._sidebar.close();
- return this._updateGraph(this._model, this._graphs.slice(1));
- }
- return null;
- }
- async renderGraph(model, graph) {
- this._graph = null;
- const canvas = this._element('canvas');
- while (canvas.lastChild) {
- canvas.removeChild(canvas.lastChild);
- }
- if (!graph) {
- return;
- }
- this._zoom = 1;
- const groups = graph.groups;
- const nodes = graph.nodes;
- this._host.event('graph_view', {
- graph_node_count: nodes.length,
- graph_skip: 0
- });
- const options = {};
- options.nodesep = 20;
- options.ranksep = 20;
- const rotate = graph.nodes.every((node) => node.inputs.filter((input) => input.value.every((argument) => !argument.initializer)).length === 0 && node.outputs.length === 0);
- const horizontal = rotate ? this._options.direction === 'vertical' : this._options.direction !== 'vertical';
- if (horizontal) {
- options.rankdir = "LR";
- }
- if (nodes.length > 3000) {
- options.ranker = 'longest-path';
- }
- const viewGraph = new view.Graph(this, model, groups, options);
- viewGraph.add(graph);
- // Workaround for Safari background drag/zoom issue:
- // https://stackoverflow.com/questions/40887193/d3-js-zoom-is-not-working-with-mousewheel-in-safari
- const background = this._host.document.createElementNS('http://www.w3.org/2000/svg', 'rect');
- background.setAttribute('id', 'background');
- background.setAttribute('fill', 'none');
- background.setAttribute('pointer-events', 'all');
- canvas.appendChild(background);
- const origin = this._host.document.createElementNS('http://www.w3.org/2000/svg', 'g');
- origin.setAttribute('id', 'origin');
- canvas.appendChild(origin);
- viewGraph.build(this._host.document, origin);
- this._zoom = 1;
- await this._timeout(20);
- viewGraph.update();
- const elements = Array.from(canvas.getElementsByClassName('graph-input') || []);
- if (elements.length === 0) {
- const nodeElements = Array.from(canvas.getElementsByClassName('graph-node') || []);
- if (nodeElements.length > 0) {
- elements.push(nodeElements[0]);
- }
- }
- const size = canvas.getBBox();
- const margin = 100;
- const width = Math.ceil(margin + size.width + margin);
- const height = Math.ceil(margin + size.height + margin);
- origin.setAttribute('transform', 'translate(' + margin.toString() + ', ' + margin.toString() + ') scale(1)');
- background.setAttribute('width', width);
- background.setAttribute('height', height);
- this._width = width;
- this._height = height;
- delete this._scrollLeft;
- delete this._scrollRight;
- canvas.setAttribute('viewBox', '0 0 ' + width + ' ' + height);
- canvas.setAttribute('width', width);
- canvas.setAttribute('height', height);
- this._zoom = 1;
- this._updateZoom(this._zoom);
- const container = this._element('graph');
- if (elements && elements.length > 0) {
- // Center view based on input elements
- const xs = [];
- const ys = [];
- for (let i = 0; i < elements.length; i++) {
- const element = elements[i];
- const rect = element.getBoundingClientRect();
- xs.push(rect.left + (rect.width / 2));
- ys.push(rect.top + (rect.height / 2));
- }
- let x = xs[0];
- const y = ys[0];
- if (ys.every(y => y === ys[0])) {
- x = xs.reduce((a, b) => a + b, 0) / xs.length;
- }
- const graphRect = container.getBoundingClientRect();
- const left = (container.scrollLeft + x - graphRect.left) - (graphRect.width / 2);
- const top = (container.scrollTop + y - graphRect.top) - (graphRect.height / 2);
- container.scrollTo({ left: left, top: top, behavior: 'auto' });
- } else {
- const canvasRect = canvas.getBoundingClientRect();
- const graphRect = container.getBoundingClientRect();
- const left = (container.scrollLeft + (canvasRect.width / 2) - graphRect.left) - (graphRect.width / 2);
- const top = (container.scrollTop + (canvasRect.height / 2) - graphRect.top) - (graphRect.height / 2);
- container.scrollTo({ left: left, top: top, behavior: 'auto' });
- }
- this._graph = viewGraph;
- }
- applyStyleSheet(element, name) {
- let rules = [];
- for (const styleSheet of this._host.document.styleSheets) {
- if (styleSheet && styleSheet.href && styleSheet.href.endsWith('/' + name)) {
- rules = styleSheet.cssRules;
- break;
- }
- }
- const nodes = element.getElementsByTagName('*');
- for (const node of nodes) {
- for (const rule of rules) {
- if (node.matches(rule.selectorText)) {
- for (const item of rule.style) {
- node.style[item] = rule.style[item];
- }
- }
- }
- }
- }
- export(file) {
- const lastIndex = file.lastIndexOf('.');
- const extension = (lastIndex != -1) ? file.substring(lastIndex + 1).toLowerCase() : 'png';
- if (this.activeGraph && (extension === 'png' || extension === 'svg')) {
- const canvas = this._element('canvas');
- const clone = canvas.cloneNode(true);
- this.applyStyleSheet(clone, 'grapher.css');
- clone.setAttribute('id', 'export');
- clone.removeAttribute('viewBox');
- clone.removeAttribute('width');
- clone.removeAttribute('height');
- clone.style.removeProperty('opacity');
- clone.style.removeProperty('display');
- clone.style.removeProperty('width');
- clone.style.removeProperty('height');
- const background = clone.querySelector('#background');
- const origin = clone.querySelector('#origin');
- origin.setAttribute('transform', 'translate(0,0) scale(1)');
- background.removeAttribute('width');
- background.removeAttribute('height');
- const parent = canvas.parentElement;
- parent.insertBefore(clone, canvas);
- const size = clone.getBBox();
- parent.removeChild(clone);
- parent.removeChild(canvas);
- parent.appendChild(canvas);
- const delta = (Math.min(size.width, size.height) / 2.0) * 0.1;
- const width = Math.ceil(delta + size.width + delta);
- const height = Math.ceil(delta + size.height + delta);
- origin.setAttribute('transform', 'translate(' + (delta - size.x).toString() + ', ' + (delta - size.y).toString() + ') scale(1)');
- clone.setAttribute('width', width);
- clone.setAttribute('height', height);
- background.setAttribute('width', width);
- background.setAttribute('height', height);
- background.setAttribute('fill', '#fff');
- const data = new XMLSerializer().serializeToString(clone);
- if (extension === 'svg') {
- const blob = new Blob([ data ], { type: 'image/svg' });
- this._host.export(file, blob);
- }
- if (extension === 'png') {
- const image = new Image();
- image.onload = () => {
- const max = Math.max(width, height);
- const scale = Math.min(24000.0 / max, 2.0);
- const canvas = this._host.document.createElement('canvas');
- canvas.width = Math.ceil(width * scale);
- canvas.height = Math.ceil(height * scale);
- const context = canvas.getContext('2d');
- context.scale(scale, scale);
- context.drawImage(image, 0, 0);
- canvas.toBlob((blob) => {
- if (blob) {
- this._host.export(file, blob);
- } else {
- const error = new Error('Image may be too large to render as PNG.');
- error.name = 'Error exporting image.';
- this._host.exception(error, false);
- this._host.error(error.name, error.message);
- }
- }, 'image/png');
- };
- image.src = 'data:image/svg+xml;base64,' + this._host.window.btoa(unescape(encodeURIComponent(data)));
- }
- }
- }
- showModelProperties() {
- if (this._model) {
- try {
- const modelSidebar = new view.ModelSidebar(this._host, this._model, this.activeGraph);
- modelSidebar.on('update-active-graph', (sender, graph) => {
- this._updateActiveGraph(graph);
- });
- const content = modelSidebar.render();
- this._sidebar.open(content, 'Model Properties');
- } catch (error) {
- if (error) {
- error.context = this._model.identifier;
- }
- this.error(error, 'Error showing model properties.', null);
- }
- }
- }
- showNodeProperties(node, input) {
- if (node) {
- try {
- if (this._menu) {
- this._menu.close();
- }
- const nodeSidebar = new view.NodeSidebar(this._host, node);
- nodeSidebar.on('show-documentation', (/* sender, e */) => {
- this.showDocumentation(node.type);
- });
- nodeSidebar.on('show-graph', (sender, graph) => {
- this.pushGraph(graph);
- });
- nodeSidebar.on('export-tensor', (sender, tensor) => {
- const defaultPath = tensor.name ? tensor.name.split('/').join('_').split(':').join('_').split('.').join('_') : 'tensor';
- this._host.save('NumPy Array', 'npy', defaultPath, (file) => {
- try {
- let data_type = tensor.type.dataType;
- if (data_type === 'boolean') {
- data_type = 'bool';
- }
- const execution = new python.Execution();
- const bytes = execution.invoke('io.BytesIO', []);
- const dtype = execution.invoke('numpy.dtype', [ data_type ]);
- const array = execution.invoke('numpy.asarray', [ tensor.value, dtype ]);
- execution.invoke('numpy.save', [ bytes, array ]);
- bytes.seek(0);
- const blob = new Blob([ bytes.read() ], { type: 'application/octet-stream' });
- this._host.export(file, blob);
- } catch (error) {
- this.error(error, 'Error saving NumPy tensor.', null);
- }
- });
- });
- nodeSidebar.on('error', (sender, error) => {
- if (this._model) {
- error.context = this._model.identifier;
- }
- this.error(error, null, null);
- });
- nodeSidebar.on('activate', (sender, argument) => {
- this._graph.select([ argument ]);
- });
- nodeSidebar.on('deactivate', () => {
- this._graph.select(null);
- });
- if (input) {
- nodeSidebar.toggleInput(input.name);
- }
- this._sidebar.open(nodeSidebar.render(), 'Node Properties');
- } catch (error) {
- if (error) {
- error.context = this._model.identifier;
- }
- this.error(error, 'Error showing node properties.', null);
- }
- }
- }
- showDocumentation(type) {
- if (type && (type.description || type.inputs || type.outputs || type.attributes)) {
- if (type.nodes && type.nodes.length > 0) {
- this.pushGraph(type);
- }
- const documentationSidebar = new view.DocumentationSidebar(this._host, type);
- documentationSidebar.on('navigate', (sender, e) => {
- this._host.openURL(e.link);
- });
- const title = type.type === 'function' ? 'Function' : 'Documentation';
- this._sidebar.push(documentationSidebar.render(), title);
- }
- }
- about() {
- this._host.document.getElementById('version').innerText = this._host.version;
- const handler = () => {
- this._host.window.removeEventListener('keydown', handler);
- this._host.document.body.removeEventListener('click', handler);
- this._host.document.body.classList.remove('about');
- };
- this._host.window.addEventListener('keydown', handler);
- this._host.document.body.addEventListener('click', handler);
- this._host.document.body.classList.add('about');
- }
- };
- view.Menu = class {
- constructor(host) {
- this.items = [];
- this._darwin = host.environment('platform') === 'darwin';
- this._document = host.document;
- this._stack = [];
- this._root = [];
- this._buttons = [];
- this._accelerators = new Map();
- this._keyCodes = new Map([
- [ 'Backspace', 0x08 ], [ 'Enter', 0x0D ], [ 'Escape', 0x1B ],
- [ 'Left', 0x25 ], [ 'Up', 0x26 ], [ 'Right', 0x27 ], [ 'Down', 0x28 ],
- [ 'F5', 0x74 ], [ 'F11', 0x7a ]
- ]);
- this._symbols = new Map([
- [ 'Backspace', '⌫' ], [ 'Enter', '⏎' ],
- [ 'Up', '↑' ], [ 'Down', '↓' ],
- ]);
- this._keydown = (e) => {
- this._alt = false;
- const code = e.keyCode | (e.altKey ? 0x0200 : 0) | (e.shiftKey ? 0x0100 : 0);
- const modifier = (e.ctrlKey ? 0x0400 : 0) | (e.metaKey ? 0x0800 : 0);
- if ((code | modifier) === 0x0212) { // Alt
- this._alt = true;
- } else {
- const action =
- this._accelerators.get(code | modifier) ||
- this._accelerators.get(code | ((e.ctrlKey && !this._darwin) || (e.metaKey && this._darwin) ? 0x1000 : 0));
- if (action && this._execute(action)) {
- e.preventDefault();
- } else {
- const item = this._mnemonic(code | modifier);
- if (item && this._activate(item)) {
- e.preventDefault();
- }
- }
- }
- };
- this._keyup = (e) => {
- const code = e.keyCode;
- if (code === 0x0012 && this._alt) { // Alt
- switch (this._stack.length) {
- case 0: {
- if (this.open()) {
- e.preventDefault();
- }
- break;
- }
- case 1: {
- if (this.close()) {
- e.preventDefault();
- }
- break;
- }
- default: {
- this._stack = [ this ];
- if (this._root.length > 1) {
- this._root = [ this ];
- this._rebuild();
- }
- this._update();
- e.preventDefault();
- break;
- }
- }
- }
- this._alt = false;
- };
- this._next = () => {
- const button = this._element.ownerDocument.activeElement;
- const index = this._buttons.indexOf(button);
- if (index !== -1 && index < this._buttons.length - 1) {
- const next = this._buttons[index + 1];
- next.focus();
- }
- };
- this._previous = () => {
- const button = this._element.ownerDocument.activeElement;
- const index = this._buttons.indexOf(button);
- if (index > 0) {
- const next = this._buttons[index - 1];
- next.focus();
- }
- };
- this._push = () => {
- const button = this._element.ownerDocument.activeElement;
- if (button && button.getAttribute('data-type') === 'group') {
- button.click();
- }
- };
- this._pop = () => {
- if (this._stack.length > 1) {
- this._deactivate();
- }
- };
- this._exit = () => {
- this._deactivate();
- if (this._stack.length === 0) {
- this.close();
- }
- };
- host.window.addEventListener('keydown', this._keydown);
- host.window.addEventListener('keyup', this._keyup);
- }
- attach(element, button) {
- this._element = element;
- button.addEventListener('click', (e) => {
- this.toggle();
- e.preventDefault();
- });
- }
- add(value) {
- const item = new view.Menu.Command(value);
- this.register(item, item.accelerator);
- }
- group(label) {
- const item = new view.Menu.Group(this, label);
- item.identifier = 'menu-item-' + this.items.length.toString();
- this.items.push(item);
- item.shortcut = this.register(item.accelerator);
- return item;
- }
- toggle() {
- if (this._element.style.opacity >= 1) {
- this.close();
- } else {
- this._root = [ this ];
- this._stack = [ this ];
- this.open();
- }
- }
- open() {
- if (this._element) {
- if (this._stack.length === 0) {
- this.toggle();
- this._stack = [ this ];
- }
- this._rebuild();
- this._update();
- this.register(this._exit, 'Escape');
- this.register(this._previous, 'Up');
- this.register(this._next, 'Down');
- this.register(this._pop, 'Left');
- this.register(this._push, 'Right');
- }
- }
- close() {
- if (this._element) {
- this.unregister(this._exit);
- this.unregister(this._previous);
- this.unregister(this._next);
- this.unregister(this._pop);
- this.unregister(this._push);
- this._element.style.opacity = 0;
- this._element.style.left = '-16em';
- const button = this._element.ownerDocument.activeElement;
- if (this._buttons.indexOf(button) > 0) {
- button.blur();
- }
- while (this._root.length > 1) {
- this._deactivate();
- }
- this._stack = [];
- }
- }
- register(action, accelerator) {
- let shortcut = '';
- if (accelerator) {
- let shift = false;
- let alt = false;
- let ctrl = false;
- let cmd = false;
- let cmdOrCtrl = false;
- let key = '';
- for (const part of accelerator.split('+')) {
- switch (part) {
- case 'CmdOrCtrl': cmdOrCtrl = true; break;
- case 'Cmd': cmd = true; break;
- case 'Ctrl': ctrl = true; break;
- case 'Alt': alt = true; break;
- case 'Shift': shift = true; break;
- default: key = part; break;
- }
- }
- if (key !== '') {
- if (this._darwin) {
- shortcut += ctrl ? '⌃' : '';
- shortcut += alt ? '⌥' : '';
- shortcut += shift ? '⇧' : '';
- shortcut += cmdOrCtrl || cmd ? '⌘' : '';
- shortcut += this._symbols.has(key) ? this._symbols.get(key) : key;
- } else {
- shortcut += cmdOrCtrl || ctrl ? 'Ctrl+' : '';
- shortcut += alt ? 'Alt+' : '';
- shortcut += shift ? 'Shift+' : '';
- shortcut += key;
- }
- let code = (cmdOrCtrl ? 0x1000 : 0) | (cmd ? 0x0800 : 0) | (ctrl ? 0x0400 : 0) | (alt ? 0x0200 : 0 | shift ? 0x0100 : 0);
- code |= this._keyCodes.has(key) ? this._keyCodes.get(key) : key.charCodeAt(0);
- this._accelerators.set(code, action);
- }
- }
- return shortcut;
- }
- unregister(action) {
- this._accelerators = new Map(Array.from(this._accelerators.entries()).filter((entry) => entry[1] !== action));
- }
- _execute(action) {
- if (typeof action === 'function') {
- action();
- return true;
- }
- switch (action ? action.type : null) {
- case 'group': {
- while (this._stack.length > this._root.length) {
- this._stack.pop();
- }
- this._root.push({ items: [ action ] });
- this._stack.push(action);
- this._rebuild();
- this._update();
- return true;
- }
- case 'command': {
- this.close();
- setTimeout(() => action.execute(), 10);
- return true;
- }
- default: {
- return false;
- }
- }
- }
- _mnemonic(code) {
- const key = /[a-zA-Z0-9]/.test(String.fromCharCode(code & 0x00FF));
- const modifier = (code & 0xFF00) !== 0;
- const alt = (code & 0xFF00) === 0x0200;
- if (alt && key) {
- this.open();
- }
- if (this._stack.length > 0 && key && (alt || !modifier)) {
- const key = String.fromCharCode(code & 0x00FF);
- const group = this._stack.length > 0 ? this._stack[this._stack.length - 1] : this;
- const item = group.items.find((item) => key === item.mnemonic && (item.type === 'group' || item.type === 'command') && item.enabled);
- if (item) {
- return item;
- }
- }
- return null;
- }
- _activate(item) {
- switch (item ? item.type : null) {
- case 'group': {
- this._stack.push(item);
- this._rebuild();
- this._update();
- return true;
- }
- case 'command': {
- return this._execute(item);
- }
- default: {
- return false;
- }
- }
- }
- _deactivate() {
- if (this._root.length > 1) {
- this._root.pop();
- const group = this._stack.pop();
- this._rebuild();
- this._update();
- if (group) {
- const button = this._buttons.find((button) => button.getAttribute('id') === group.identifier);
- if (button) {
- button.focus();
- }
- }
- } else if (this._stack.length > 0) {
- this._stack.pop();
- this._update();
- }
- }
- _label(item, mnemonic) {
- delete item.mnemonic;
- const value = item.label;
- if (value) {
- const index = value.indexOf('&');
- if (index !== -1) {
- if (mnemonic) {
- item.mnemonic = value[index + 1].toUpperCase();
- return value.substring(0, index) + '<u>' + value[index + 1] + '</u>' + value.substring(index + 2);
- }
- return value.substring(0, index) + value.substring(index + 1);
- }
- }
- return value || '';
- }
- _rebuild() {
- this._element.innerHTML = '';
- const root = this._root[this._root.length - 1];
- for (const group of root.items) {
- const container = this._document.createElement('div');
- container.setAttribute('id', group.identifier);
- container.setAttribute('class', 'menu-group');
- container.innerHTML = "<div class='menu-group-header'></div>";
- for (const item of group.items) {
- switch (item.type) {
- case 'group':
- case 'command': {
- const button = this._document.createElement('button');
- button.setAttribute('class', 'menu-command');
- button.setAttribute('id', item.identifier);
- button.setAttribute('data-type', item.type);
- button.addEventListener('mouseenter', () => button.focus());
- button.addEventListener('click', () => this._execute(item));
- const accelerator = this._document.createElement('span');
- accelerator.setAttribute('class', 'menu-shortcut');
- if (item.type === 'group') {
- accelerator.innerHTML = '❯';
- } else if (item.shortcut) {
- accelerator.innerHTML = item.shortcut;
- }
- button.appendChild(accelerator);
- const content = this._document.createElement('span');
- content.setAttribute('class', 'menu-label');
- button.appendChild(content);
- container.appendChild(button);
- break;
- }
- case 'separator': {
- const element = this._document.createElement('div');
- element.setAttribute('class', 'menu-separator');
- element.setAttribute('id', item.identifier);
- container.appendChild(element);
- break;
- }
- default: {
- break;
- }
- }
- }
- this._element.appendChild(container);
- }
- this._element.style.opacity = 1.0;
- this._element.style.left = '0px';
- if (this._root.length > 1) {
- this._element.style.width = 'auto';
- this._element.style.maxWidth = '60%';
- } else {
- this._element.style.removeProperty('width');
- this._element.style.maxWidth = 'auto';
- }
- }
- _update() {
- this._buttons = [];
- const selected = this._stack.length > 0 ? this._stack[this._stack.length - 1] : null;
- const root = this._root[this._root.length - 1];
- for (const group of root.items) {
- let visible = false;
- let block = false;
- const active = this._stack.length <= 1 || this._stack[1] === group;
- const container = this._document.getElementById(group.identifier);
- container.childNodes[0].innerHTML = this._label(group, this === selected);
- for (const item of group.items) {
- switch (item.type) {
- case 'group':
- case 'command': {
- const label = this._label(item, group === selected);
- const button = this._document.getElementById(item.identifier);
- button.childNodes[1].innerHTML = label;
- if (item.enabled) {
- button.removeAttribute('disabled');
- button.style.display = 'block';
- visible = true;
- block = true;
- if (active) {
- this._buttons.push(button);
- }
- } else {
- button.setAttribute('disabled', '');
- button.style.display = 'none';
- }
- break;
- }
- case 'separator': {
- const element = this._document.getElementById(item.identifier);
- element.style.display = block ? 'block' : 'none';
- block = false;
- break;
- }
- default: {
- break;
- }
- }
- }
- for (let i = group.items.length - 1; i >= 0; i--) {
- const item = group.items[i];
- if ((item.type === 'group' || item.type === 'command') && item.enabled) {
- break;
- } else if (item.type === 'separator') {
- const element = this._document.getElementById(item.identifier);
- element.style.display = 'none';
- }
- }
- if (!visible) {
- container.style.display = 'none';
- }
- container.style.opacity = active ? 1 : 0;
- }
- const button = this._element.ownerDocument.activeElement;
- const index = this._buttons.indexOf(button);
- if (index === -1 && this._buttons.length > 0) {
- this._buttons[0].focus();
- }
- }
- };
- view.Menu.Group = class {
- constructor(parent, label) {
- this.type = 'group';
- this.parent = parent;
- this.label = label;
- this.items = [];
- }
- get enabled() {
- return this.items.some((item) => item.enabled);
- }
- add(value) {
- const item = Object.keys(value).length > 0 ? new view.Menu.Command(value) : new view.Menu.Separator();
- item.identifier = this.identifier + '-' + this.items.length.toString();
- this.items.push(item);
- item.shortcut = this.parent.register(item, item.accelerator);
- }
- group(label) {
- const item = new view.Menu.Group(this, label);
- item.identifier = this.identifier + '-' + this.items.length.toString();
- this.items.push(item);
- item.shortcut = this.parent.register(item, item.accelerator);
- return item;
- }
- clear() {
- for (const item of this.items) {
- if (item.clear) {
- item.clear();
- }
- this.parent.unregister(item);
- }
- this.items = [];
- }
- register(item, accelerator) {
- return this.parent.register(item, accelerator);
- }
- unregister(item) {
- this.parent.unregister(item);
- }
- };
- view.Menu.Command = class {
- constructor(item) {
- this.type = 'command';
- this.accelerator = item.accelerator;
- this._label = item.label;
- this._enabled = item.enabled;
- this._execute = item.execute;
- }
- get label() {
- return typeof this._label === 'function' ? this._label() : this._label;
- }
- get enabled() {
- return this._enabled ? this._enabled() : true;
- }
- execute() {
- if (this._execute && this.enabled) {
- this._execute();
- }
- }
- };
- view.Menu.Separator = class {
- constructor() {
- this.type = 'separator';
- this.enabled = false;
- }
- };
- view.Graph = class extends grapher.Graph {
- constructor(view, model, compound, options) {
- super(compound, options);
- this.view = view;
- this.model = model;
- this._nodeKey = 0;
- this._values = new Map();
- this._table = new Map();
- this._selection = new Set();
- }
- createNode(node) {
- const value = new view.Node(this, node);
- value.name = (this._nodeKey++).toString();
- this.setNode(value);
- this._table.set(node, value);
- return value;
- }
- createInput(input) {
- const value = new view.Input(this, input);
- value.name = (this._nodeKey++).toString();
- this.setNode(value);
- this._table.set(input, value);
- return value;
- }
- createOutput(output) {
- const value = new view.Output(this, output);
- value.name = (this._nodeKey++).toString();
- this.setNode(value);
- this._table.set(output, value);
- return value;
- }
- createValue(argument) {
- const name = argument.name;
- if (!this._values.has(name)) {
- const value = new view.Value(this, argument);
- this._values.set(name, value);
- this._table.set(argument, value);
- } else {
- // TODO #1109 duplicate argument name
- const value = this._values.get(name);
- this._table.set(argument, value);
- }
- return this._values.get(name);
- }
- add(graph) {
- const clusters = new Set();
- const clusterParentMap = new Map();
- const groups = graph.groups;
- if (groups) {
- for (const node of graph.nodes) {
- if (node.group) {
- const path = node.group.split('/');
- while (path.length > 0) {
- const name = path.join('/');
- path.pop();
- clusterParentMap.set(name, path.join('/'));
- }
- }
- }
- }
- for (const input of graph.inputs) {
- const viewInput = this.createInput(input);
- for (const value of input.value) {
- this.createValue(value).from(viewInput);
- }
- }
- for (const node of graph.nodes) {
- const viewNode = this.createNode(node);
- const inputs = node.inputs;
- for (const input of inputs) {
- for (const value of input.value) {
- if (value.name != '' && !value.initializer) {
- this.createValue(value).to(viewNode);
- }
- }
- }
- let outputs = node.outputs;
- if (node.chain && node.chain.length > 0) {
- const chainOutputs = node.chain[node.chain.length - 1].outputs;
- if (chainOutputs.length > 0) {
- outputs = chainOutputs;
- }
- }
- for (const output of outputs) {
- for (const value of output.value) {
- if (!value) {
- const error = new view.Error('Invalid null argument.');
- error.context = this.model.identifier;
- throw error;
- }
- if (value.name != '') {
- this.createValue(value).from(viewNode);
- }
- }
- }
- if (node.controlDependencies && node.controlDependencies.length > 0) {
- for (const value of node.controlDependencies) {
- this.createValue(value).to(viewNode, true);
- }
- }
- const createCluster = (name) => {
- if (!clusters.has(name)) {
- this.setNode({ name: name, rx: 5, ry: 5 });
- clusters.add(name);
- const parent = clusterParentMap.get(name);
- if (parent) {
- createCluster(parent);
- this.setParent(name, parent);
- }
- }
- };
- if (groups) {
- let groupName = node.group;
- if (groupName && groupName.length > 0) {
- if (!clusterParentMap.has(groupName)) {
- const lastIndex = groupName.lastIndexOf('/');
- if (lastIndex != -1) {
- groupName = groupName.substring(0, lastIndex);
- if (!clusterParentMap.has(groupName)) {
- groupName = null;
- }
- } else {
- groupName = null;
- }
- }
- if (groupName) {
- createCluster(groupName + '\ngroup');
- this.setParent(viewNode.name, groupName + '\ngroup');
- }
- }
- }
- }
- for (const output of graph.outputs) {
- const viewOutput = this.createOutput(output);
- for (const value of output.value) {
- this.createValue(value).to(viewOutput);
- }
- }
- }
- build(document, origin) {
- for (const value of this._values.values()) {
- value.build();
- }
- super.build(document, origin);
- }
- select(selection) {
- for (const element of this._selection) {
- element.deselect();
- }
- this._selection.clear();
- let array = [];
- if (selection) {
- for (const value of selection) {
- if (this._table.has(value)) {
- const element = this._table.get(value);
- array = array.concat(element.select());
- this._selection.add(element);
- }
- }
- }
- return array;
- }
- focus(selection) {
- for (const value of selection) {
- const element = this._table.get(value);
- if (element && !this._selection.has(element)) {
- element.select();
- }
- }
- }
- blur(selection) {
- for (const value of selection) {
- const element = this._table.get(value);
- if (element && !this._selection.has(element)) {
- element.deselect();
- }
- }
- }
- };
- view.Node = class extends grapher.Node {
- constructor(context, value) {
- super();
- this.context = context;
- this.value = value;
- view.Node.counter = view.Node.counter || 0;
- this.id = 'node-' + (value.name ? 'name-' + value.name : 'id-' + (view.Node.counter++).toString());
- this._add(this.value);
- }
- get class() {
- return 'graph-node';
- }
- get inputs() {
- return this.value.inputs;
- }
- get outputs() {
- return this.value.outputs;
- }
- _add(node) {
- const header = this.header();
- const styles = [ 'node-item-type' ];
- const type = node.type;
- const category = type && type.category ? type.category : '';
- if (category) {
- styles.push('node-item-type-' + category.toLowerCase());
- }
- if (typeof type.name !== 'string' || !type.name.split) { // #416
- const error = new view.Error("Unsupported node type '" + JSON.stringify(type.name) + "'.");
- if (this.context.model && this.context.model.identifier) {
- error.context = this.context.model.identifier;
- }
- throw error;
- }
- const content = this.context.view.options.names && (node.name || node.location) ? (node.name || node.location) : type.name.split('.').pop();
- const tooltip = this.context.view.options.names && (node.name || node.location) ? type.name : (node.name || node.location);
- const title = header.add(null, styles, content, tooltip);
- title.on('click', () => this.context.view.showNodeProperties(node, null));
- if (node.type.nodes && node.type.nodes.length > 0) {
- const definition = header.add(null, styles, '\u0192', 'Show Function Definition');
- definition.on('click', () => this.context.view.pushGraph(node.type));
- }
- if (node.nodes) {
- // this._expand = header.add(null, styles, '+', null);
- // this._expand.on('click', () => this.toggle());
- }
- const initializers = [];
- let hiddenInitializers = false;
- if (this.context.view.options.weights) {
- for (const input of node.inputs) {
- if (input.visible !== false && input.value.length === 1 && input.value[0].initializer != null) {
- initializers.push(input);
- }
- if ((input.visible === false || input.value.length > 1) &&
- input.value.some((argument) => argument.initializer != null)) {
- hiddenInitializers = true;
- }
- }
- }
- let sortedAttributes = [];
- const attributes = node.attributes || [];
- if (this.context.view.options.attributes) {
- sortedAttributes = attributes.filter((attribute) => attribute.visible).slice();
- }
- sortedAttributes.sort((a, b) => {
- const au = a.name.toUpperCase();
- const bu = b.name.toUpperCase();
- return (au < bu) ? -1 : (au > bu) ? 1 : 0;
- });
- if (initializers.length > 0 || hiddenInitializers || sortedAttributes.length > 0) {
- const list = this.list();
- list.on('click', () => this.context.view.showNodeProperties(node));
- for (const initializer of initializers) {
- const value = initializer.value[0];
- const type = value.type;
- let shape = '';
- let separator = '';
- if (type && type.shape && type.shape.dimensions && Array.isArray(type.shape.dimensions)) {
- shape = '\u3008' + type.shape.dimensions.map((d) => (d !== null && d !== undefined) ? d : '?').join('\u00D7') + '\u3009';
- if (type.shape.dimensions.length === 0 && value.initializer) {
- try {
- const initializer = value.initializer;
- const tensor = new view.Tensor(initializer);
- if ((tensor.layout === '<' || tensor.layout === '>' || tensor.layout === '|') && !tensor.empty && tensor.type.dataType !== '?') {
- shape = tensor.toString();
- if (shape && shape.length > 10) {
- shape = shape.substring(0, 10) + '\u2026';
- }
- separator = ' = ';
- }
- } catch (err) {
- let type = '?';
- try {
- type = value.initializer.type.toString();
- } catch (error) {
- // continue regardless of error
- }
- const error = new view.Error("Failed to render tensor of type '" + type + "' (" + err.message + ").");
- if (this.context.view.model && this.context.view.model.identifier) {
- error.context = this.context.view.model.identifier;
- }
- throw error;
- }
- }
- }
- list.add(value.name ? 'initializer-' + value.name : '', initializer.name, shape, type ? type.toString() : '', separator);
- }
- if (hiddenInitializers) {
- list.add(null, '\u3008' + '\u2026' + '\u3009', '', null, '');
- }
- for (const attribute of sortedAttributes) {
- if (attribute.visible) {
- let value = new view.Formatter(attribute.value, attribute.type).toString();
- if (value && value.length > 25) {
- value = value.substring(0, 25) + '\u2026';
- }
- list.add(null, attribute.name, value, attribute.type, ' = ');
- }
- }
- }
- if (Array.isArray(node.chain) && node.chain.length > 0) {
- for (const innerNode of node.chain) {
- this._add(innerNode);
- }
- }
- if (node.inner) {
- this._add(node.inner);
- }
- if (node.nodes) {
- // this.canvas = this.canvas();
- }
- }
- toggle() {
- this._expand.content = '-';
- this._graph = new view.Graph(this.context.view, this.context.model, false, {});
- this._graph.add(this.value);
- // const document = this.element.ownerDocument;
- // const parent = this.element.parentElement;
- // this._graph.build(document, parent);
- // this._graph.update();
- this.canvas.width = 300;
- this.canvas.height = 300;
- this.layout();
- this.context.update();
- }
- };
- view.Input = class extends grapher.Node {
- constructor(context, value) {
- super();
- this.context = context;
- this.value = value;
- view.Input.counter = view.Input.counter || 0;
- const types = value.value.map((argument) => argument.type || '').join('\n');
- let name = value.name || '';
- if (name.length > 16) {
- name = name.split('/').pop();
- }
- const header = this.header();
- const title = header.add(null, [ 'graph-item-input' ], name, types);
- title.on('click', () => this.context.view.showModelProperties());
- this.id = 'input-' + (name ? 'name-' + name : 'id-' + (view.Input.counter++).toString());
- }
- get class() {
- return 'graph-input';
- }
- get inputs() {
- return [];
- }
- get outputs() {
- return [ this.value ];
- }
- };
- view.Output = class extends grapher.Node {
- constructor(context, value) {
- super();
- this.context = context;
- this.value = value;
- const types = value.value.map((argument) => argument.type || '').join('\n');
- let name = value.name || '';
- if (name.length > 16) {
- name = name.split('/').pop();
- }
- const header = this.header();
- const title = header.add(null, [ 'graph-item-output' ], name, types);
- title.on('click', () => this.context.view.showModelProperties());
- }
- get inputs() {
- return [ this.value ];
- }
- get outputs() {
- return [];
- }
- };
- view.Value = class {
- constructor(context, argument) {
- this.context = context;
- this.value = argument;
- }
- from(node) {
- this._from = node;
- }
- to(node, controlDependency) {
- this._to = this._to || [];
- if (controlDependency) {
- this._controlDependencies = this._controlDependencies || new Set();
- this._controlDependencies.add(this._to.length);
- }
- this._to.push(node);
- }
- build() {
- this._edges = this._edges || [];
- if (this._from && this._to) {
- for (let i = 0; i < this._to.length; i++) {
- const to = this._to[i];
- let content = '';
- const type = this.value.type;
- if (type &&
- type.shape &&
- type.shape.dimensions &&
- type.shape.dimensions.length > 0 &&
- type.shape.dimensions.every((dim) => !dim || Number.isInteger(dim) || dim instanceof base.Int64 || (typeof dim === 'string'))) {
- content = type.shape.dimensions.map((dim) => (dim !== null && dim !== undefined) ? dim : '?').join('\u00D7');
- content = content.length > 16 ? '' : content;
- }
- if (this.context.view.options.names) {
- content = this.value.name.split('\n').shift(); // custom argument id
- }
- const edge = new view.Edge(this, this._from, to);
- edge.v = this._from.name;
- edge.w = to.name;
- if (content) {
- edge.label = content;
- }
- edge.id = 'edge-' + this.value.name;
- if (this._controlDependencies && this._controlDependencies.has(i)) {
- edge.class = 'edge-path-control-dependency';
- }
- this.context.setEdge(edge);
- this._edges.push(edge);
- }
- }
- }
- select() {
- let array = [];
- for (const edge of this._edges) {
- array = array.concat(edge.select());
- }
- return array;
- }
- deselect() {
- for (const edge of this._edges) {
- edge.deselect();
- }
- }
- };
- view.Edge = class extends grapher.Edge {
- constructor(argument, from, to) {
- super(from, to);
- this.argument = argument;
- }
- get minlen() {
- if (this.from.inputs.every((parameter) => parameter.value.every((argument) => argument.initializer))) {
- return 2;
- }
- return 1;
- }
- emit(event) {
- switch (event) {
- case 'pointerover':
- this.argument.context.focus([ this.argument.value ]);
- break;
- case 'pointerleave':
- this.argument.context.blur([ this.argument.value ]);
- break;
- default:
- break;
- }
- }
- };
- view.Sidebar = class {
- constructor(host) {
- this._host = host;
- this._stack = [];
- const pop = () => this._update(this._stack.slice(0, -1));
- this._closeSidebarHandler = () => pop();
- this._closeSidebarKeyDownHandler = (e) => {
- if (e.keyCode == 27) {
- e.preventDefault();
- pop();
- }
- };
- const sidebar = this._element('sidebar');
- sidebar.addEventListener('transitionend', (event) => {
- if (event.propertyName === 'opacity' && sidebar.style.opacity === '0') {
- const content = this._element('sidebar-content');
- content.innerHTML = '';
- }
- });
- }
- _element(id) {
- return this._host.document.getElementById(id);
- }
- open(content, title) {
- this._update([ { title: title, content: content } ]);
- }
- close() {
- this._update([]);
- }
- push(content, title) {
- this._update(this._stack.concat({ title: title, content: content }));
- }
- _update(stack) {
- const sidebar = this._element('sidebar');
- const container = this._element('graph');
- const closeButton = this._element('sidebar-closebutton');
- closeButton.removeEventListener('click', this._closeSidebarHandler);
- this._host.document.removeEventListener('keydown', this._closeSidebarKeyDownHandler);
- if (stack) {
- this._stack = stack;
- } else if (this._stack.length > 0) {
- this._stack.pop();
- }
- if (this._stack.length > 0) {
- const item = this._stack[this._stack.length - 1];
- this._element('sidebar-title').innerHTML = item.title || '';
- closeButton.addEventListener('click', this._closeSidebarHandler);
- const content = this._element('sidebar-content');
- if (typeof item.content == 'string') {
- content.innerHTML = item.content;
- } else if (item.content instanceof Array) {
- content.innerHTML = '';
- for (const element of item.content) {
- content.appendChild(element);
- }
- } else {
- content.innerHTML = '';
- content.appendChild(item.content);
- }
- sidebar.style.width = 'min(calc(100% * 0.6), 42em)';
- sidebar.style.right = 0;
- sidebar.style.opacity = 1;
- this._host.document.addEventListener('keydown', this._closeSidebarKeyDownHandler);
- container.style.width = 'max(40vw, calc(100vw - 42em))';
- } else {
- sidebar.style.right = 'calc(0px - min(calc(100% * 0.6), 42em))';
- sidebar.style.opacity = 0;
- container.style.width = '100%';
- container.focus();
- }
- }
- };
- view.Control = class {
- on(event, callback) {
- this._events = this._events || {};
- this._events[event] = this._events[event] || [];
- this._events[event].push(callback);
- }
- emit(event, data) {
- if (this._events && this._events[event]) {
- for (const callback of this._events[event]) {
- callback(this, data);
- }
- }
- }
- };
- view.NodeSidebar = class extends view.Control {
- constructor(host, node) {
- super();
- this._host = host;
- this._node = node;
- this._elements = [];
- this._attributes = [];
- this._inputs = [];
- this._outputs = [];
- const container = this._host.document.createElement('div');
- container.className = 'sidebar-node';
- this._elements.push(container);
- if (node.type) {
- let showDocumentation = null;
- const type = node.type;
- if (type && (type.description || type.inputs || type.outputs || type.attributes)) {
- showDocumentation = {};
- showDocumentation.text = type.nodes ? '\u0192': '?';
- showDocumentation.callback = () => {
- this.emit('show-documentation', null);
- };
- }
- this._addProperty('type', new view.ValueTextView(this._host, node.type.identifier || node.type.name, showDocumentation));
- if (node.type.module) {
- this._addProperty('module', new view.ValueTextView(this._host, node.type.module));
- }
- }
- if (node.name) {
- this._addProperty('name', new view.ValueTextView(this._host, node.name));
- }
- if (node.location) {
- this._addProperty('location', new view.ValueTextView(this._host, node.location));
- }
- if (node.description) {
- this._addProperty('description', new view.ValueTextView(this._host, node.description));
- }
- if (node.device) {
- this._addProperty('device', new view.ValueTextView(this._host, node.device));
- }
- const attributes = node.attributes;
- if (attributes && attributes.length > 0) {
- const sortedAttributes = node.attributes.slice();
- sortedAttributes.sort((a, b) => {
- const au = a.name.toUpperCase();
- const bu = b.name.toUpperCase();
- return (au < bu) ? -1 : (au > bu) ? 1 : 0;
- });
- this._addHeader('Attributes');
- for (const attribute of sortedAttributes) {
- this._addAttribute(attribute.name, attribute);
- }
- }
- const inputs = node.inputs;
- if (inputs && inputs.length > 0) {
- this._addHeader('Inputs');
- for (const input of inputs) {
- this._addInput(input.name, input);
- }
- }
- const outputs = node.outputs;
- if (outputs && outputs.length > 0) {
- this._addHeader('Outputs');
- for (const output of outputs) {
- this._addOutput(output.name, output);
- }
- }
- }
- render() {
- return this._elements;
- }
- _addHeader(title) {
- const element = this._host.document.createElement('div');
- element.className = 'sidebar-header';
- element.innerText = title;
- this._elements[0].appendChild(element);
- }
- _addProperty(name, value) {
- const item = new view.NameValueView(this._host, name, value);
- this._elements[0].appendChild(item.render());
- }
- _addAttribute(name, attribute) {
- const value = new view.AttributeView(this._host, attribute);
- value.on('show-graph', (sender, graph) => {
- this.emit('show-graph', graph);
- });
- const item = new view.NameValueView(this._host, name, value);
- this._attributes.push(item);
- this._elements[0].appendChild(item.render());
- }
- _addInput(name, input) {
- if (input.value.length > 0) {
- const value = new view.ParameterView(this._host, input);
- value.on('export-tensor', (sender, value) => this.emit('export-tensor', value));
- value.on('error', (sender, value) => this.emit('error', value));
- value.on('activate', (sender, value) => this.emit('activate', value));
- value.on('deactivate', (sender, value) => this.emit('deactivate', value));
- const item = new view.NameValueView(this._host, name, value);
- this._inputs.push(item);
- this._elements[0].appendChild(item.render());
- }
- }
- _addOutput(name, output) {
- if (output.value.length > 0) {
- const value = new view.ParameterView(this._host, output);
- value.on('activate', (sender, value) => this.emit('activate', value));
- value.on('deactivate', (sender, value) => this.emit('deactivate', value));
- const item = new view.NameValueView(this._host, name, value);
- this._outputs.push(item);
- this._elements[0].appendChild(item.render());
- }
- }
- toggleInput(name) {
- for (const input of this._inputs) {
- if (name == input.name) {
- input.toggle();
- }
- }
- }
- };
- view.NameValueView = class {
- constructor(host, name, value) {
- this._host = host;
- this._name = name;
- this._value = value;
- const nameElement = this._host.document.createElement('div');
- nameElement.className = 'sidebar-item-name';
- const nameInputElement = this._host.document.createElement('input');
- nameInputElement.setAttribute('type', 'text');
- nameInputElement.setAttribute('value', name);
- nameInputElement.setAttribute('title', name);
- nameInputElement.setAttribute('readonly', 'true');
- nameElement.appendChild(nameInputElement);
- const valueElement = this._host.document.createElement('div');
- valueElement.className = 'sidebar-item-value-list';
- for (const element of value.render()) {
- valueElement.appendChild(element);
- }
- this._element = this._host.document.createElement('div');
- this._element.className = 'sidebar-item';
- this._element.appendChild(nameElement);
- this._element.appendChild(valueElement);
- }
- get name() {
- return this._name;
- }
- render() {
- return this._element;
- }
- toggle() {
- this._value.toggle();
- }
- };
- view.SelectView = class extends view.Control {
- constructor(host, values, selected) {
- super();
- this._host = host;
- this._elements = [];
- this._values = values;
- const selectElement = this._host.document.createElement('select');
- selectElement.setAttribute('class', 'sidebar-item-select');
- selectElement.addEventListener('change', (e) => {
- this.emit('change', this._values[e.target.selectedIndex]);
- });
- this._elements.push(selectElement);
- for (const value of values) {
- const optionElement = this._host.document.createElement('option');
- optionElement.innerText = value.name || '';
- if (value == selected) {
- optionElement.setAttribute('selected', 'selected');
- }
- selectElement.appendChild(optionElement);
- }
- }
- render() {
- return this._elements;
- }
- };
- view.ValueTextView = class {
- constructor(host, value, action) {
- this._host = host;
- this._elements = [];
- const element = this._host.document.createElement('div');
- element.className = 'sidebar-item-value';
- this._elements.push(element);
- if (action) {
- this._action = this._host.document.createElement('div');
- this._action.className = 'sidebar-item-value-expander';
- this._action.innerHTML = action.text;
- this._action.addEventListener('click', () => {
- action.callback();
- });
- element.appendChild(this._action);
- }
- const list = Array.isArray(value) ? value : [ value ];
- let className = 'sidebar-item-value-line';
- for (const item of list) {
- const line = this._host.document.createElement('div');
- line.className = className;
- line.innerText = item;
- element.appendChild(line);
- className = 'sidebar-item-value-line-border';
- }
- }
- render() {
- return this._elements;
- }
- toggle() {
- }
- };
- view.ValueView = class extends view.Control {
- _bold(name, value) {
- const line = this._host.document.createElement('div');
- line.innerHTML = name + ': ' + '<b>' + value + '</b>';
- this._add(line);
- }
- _code(name, value) {
- const line = this._host.document.createElement('div');
- line.innerHTML = name + ': ' + '<code><b>' + value + '</b></code>';
- this._add(line);
- }
- _add(child) {
- child.className = this._element.childNodes.length < 2 ? 'sidebar-item-value-line' : 'sidebar-item-value-line-border';
- this._element.appendChild(child);
- }
- _tensor(value) {
- const contentLine = this._host.document.createElement('pre');
- try {
- const tensor = new view.Tensor(value);
- const layout = tensor.layout;
- if (layout) {
- const layouts = new Map([
- [ 'sparse', 'Sparse' ],
- [ 'sparse.coo', 'Sparse COO' ],
- [ 'sparse.csr', 'Sparse CSR' ],
- [ 'sparse.csc', 'Sparse CSC' ],
- [ 'sparse.bsr', 'Sparse BSR' ],
- [ 'sparse.bsc', 'Sparse BSC' ]
- ]);
- if (layouts.has(layout)) {
- this._bold('layout', layouts.get(layout));
- }
- }
- if (Array.isArray(tensor.stride) && tensor.stride.length > 0) {
- this._code('stride', tensor.stride.join(','));
- }
- if (tensor.layout !== '<' && tensor.layout !== '>' && tensor.layout !== '|' && tensor.layout !== 'sparse' && tensor.layout !== 'sparse.coo') {
- contentLine.innerHTML = "Tensor layout '" + tensor.layout + "' is not implemented.";
- } else if (tensor.empty) {
- contentLine.innerHTML = 'Tensor data is empty.';
- } else if (tensor.type && tensor.type.dataType === '?') {
- contentLine.innerHTML = 'Tensor data type is not defined.';
- } else if (tensor.type && !tensor.type.shape) {
- contentLine.innerHTML = 'Tensor shape is not defined.';
- } else {
- contentLine.innerHTML = tensor.toString();
- if (this._host.save &&
- value.type.shape && value.type.shape.dimensions &&
- value.type.shape.dimensions.length > 0) {
- this._saveButton = this._host.document.createElement('div');
- this._saveButton.className = 'sidebar-item-value-expander';
- this._saveButton.innerHTML = '💾';
- this._saveButton.addEventListener('click', () => {
- this.emit('export-tensor', tensor);
- });
- this._element.appendChild(this._saveButton);
- }
- }
- } catch (err) {
- contentLine.innerHTML = err.toString();
- this.emit('error', err);
- }
- const valueLine = this._host.document.createElement('div');
- valueLine.className = 'sidebar-item-value-line-border';
- valueLine.appendChild(contentLine);
- this._element.appendChild(valueLine);
- }
- };
- view.AttributeView = class extends view.ValueView {
- constructor(host, attribute) {
- super();
- this._host = host;
- this._attribute = attribute;
- this._element = this._host.document.createElement('div');
- this._element.className = 'sidebar-item-value';
- const type = this._attribute.type;
- if (type) {
- this._expander = this._host.document.createElement('div');
- this._expander.className = 'sidebar-item-value-expander';
- this._expander.innerText = '+';
- this._expander.addEventListener('click', () => {
- this.toggle();
- });
- this._element.appendChild(this._expander);
- }
- const value = this._attribute.value;
- switch (type) {
- case 'graph': {
- const line = this._host.document.createElement('div');
- line.className = 'sidebar-item-value-line-link';
- line.innerHTML = value.name;
- line.addEventListener('click', () => {
- this.emit('show-graph', value);
- });
- this._element.appendChild(line);
- break;
- }
- case 'function': {
- const line = this._host.document.createElement('div');
- line.className = 'sidebar-item-value-line-link';
- line.innerHTML = value.type.name;
- line.addEventListener('click', () => {
- this.emit('show-graph', value.type);
- });
- this._element.appendChild(line);
- break;
- }
- default: {
- let content = new view.Formatter(value, type).toString();
- if (content && content.length > 1000) {
- content = content.substring(0, 1000) + '\u2026';
- }
- if (content && typeof content === 'string') {
- content = content.split('<').join('<').split('>').join('>');
- }
- const line = this._host.document.createElement('div');
- line.className = 'sidebar-item-value-line';
- line.innerHTML = content ? content : ' ';
- this._element.appendChild(line);
- }
- }
- }
- render() {
- return [ this._element ];
- }
- toggle() {
- if (this._expander.innerText == '+') {
- this._expander.innerText = '-';
- const type = this._attribute.type;
- const value = this._attribute.value;
- const content = type == 'tensor' && value && value.type ? value.type.toString() : this._attribute.type;
- const typeLine = this._host.document.createElement('div');
- typeLine.className = 'sidebar-item-value-line-border';
- typeLine.innerHTML = 'type: ' + '<code><b>' + content + '</b></code>';
- this._element.appendChild(typeLine);
- const description = this._attribute.description;
- if (description) {
- const descriptionLine = this._host.document.createElement('div');
- descriptionLine.className = 'sidebar-item-value-line-border';
- descriptionLine.innerHTML = description;
- this._element.appendChild(descriptionLine);
- }
- if (this._attribute.type == 'tensor' && value) {
- this._tensor(value);
- }
- } else {
- this._expander.innerText = '+';
- while (this._element.childElementCount > 2) {
- this._element.removeChild(this._element.lastChild);
- }
- }
- }
- };
- view.ParameterView = class extends view.Control {
- constructor(host, list) {
- super();
- this._list = list;
- this._elements = [];
- this._items = [];
- for (const value of list.value) {
- const item = new view.ArgumentView(host, value);
- item.on('export-tensor', (sender, value) => this.emit('export-tensor', value));
- item.on('error', (sender, value) => this.emit('error', value));
- item.on('activate', (sender, value) => this.emit('activate', value));
- item.on('deactivate', (sender, value) => this.emit('deactivate', value));
- this._items.push(item);
- this._elements.push(item.render());
- }
- }
- render() {
- return this._elements;
- }
- toggle() {
- for (const item of this._items) {
- item.toggle();
- }
- }
- };
- view.ArgumentView = class extends view.ValueView {
- constructor(host, argument) {
- super();
- this._host = host;
- this._argument = argument;
- this._element = this._host.document.createElement('div');
- this._element.className = 'sidebar-item-value';
- const type = this._argument.type;
- const initializer = this._argument.initializer;
- const quantization = this._argument.quantization;
- const location = this._argument.location !== undefined;
- if (initializer) {
- this._element.classList.add('sidebar-item-value-dark');
- }
- if (type || initializer || quantization || location) {
- this._expander = this._host.document.createElement('div');
- this._expander.className = 'sidebar-item-value-expander';
- this._expander.innerText = '+';
- this._expander.addEventListener('click', () => {
- this.toggle();
- });
- this._element.appendChild(this._expander);
- }
- const name = this._argument.name ? this._argument.name.split('\n').shift() : ''; // custom argument id
- this._hasId = name ? true : false;
- this._hasCategory = initializer && initializer.category ? true : false;
- if (this._hasId || (!this._hasCategory && !type)) {
- this._hasId = true;
- const nameLine = this._host.document.createElement('div');
- nameLine.className = 'sidebar-item-value-line';
- if (typeof name !== 'string') {
- throw new Error("Invalid value identifier '" + JSON.stringify(name) + "'.");
- }
- nameLine.innerHTML = '<span class=\'sidebar-item-value-line-content\'>name: <b>' + (name || ' ') + '</b></span>';
- nameLine.addEventListener('pointerenter', () => this.emit('activate', this._argument));
- nameLine.addEventListener('pointerleave', () => this.emit('deactivate', this._argument));
- this._element.appendChild(nameLine);
- } else if (this._hasCategory) {
- this._bold('category', initializer.category);
- } else if (type) {
- this._code('type', type.toString().split('<').join('<').split('>').join('>'));
- }
- }
- render() {
- return this._element;
- }
- toggle() {
- if (this._expander) {
- if (this._expander.innerText == '+') {
- this._expander.innerText = '-';
- const initializer = this._argument.initializer;
- if (this._hasId && this._hasCategory) {
- this._bold('category', initializer.category);
- }
- let type = null;
- let denotation = null;
- if (this._argument.type) {
- type = this._argument.type.toString();
- denotation = this._argument.type.denotation || null;
- }
- if (type && (this._hasId || this._hasCategory)) {
- this._code('type', type.split('<').join('<').split('>').join('>'));
- }
- if (denotation) {
- this._code('denotation', denotation);
- }
- const description = this._argument.description;
- if (description) {
- const descriptionLine = this._host.document.createElement('div');
- descriptionLine.className = 'sidebar-item-value-line-border';
- descriptionLine.innerHTML = description;
- this._element.appendChild(descriptionLine);
- }
- const quantization = this._argument.quantization;
- if (quantization) {
- const quantizationLine = this._host.document.createElement('div');
- quantizationLine.className = 'sidebar-item-value-line-border';
- const content = !Array.isArray(quantization) ? quantization : '<br><br>' + quantization.map((value) => ' ' + value).join('<br>');
- quantizationLine.innerHTML = '<span class=\'sidebar-item-value-line-content\'>quantization: ' + '<b>' + content + '</b></span>';
- this._element.appendChild(quantizationLine);
- }
- const location = this._argument.location;
- if (location !== undefined) {
- this._bold('location', location);
- }
- if (initializer) {
- this._tensor(initializer);
- }
- } else {
- this._expander.innerText = '+';
- while (this._element.childElementCount > 2) {
- this._element.removeChild(this._element.lastChild);
- }
- }
- }
- }
- };
- view.ModelSidebar = class extends view.Control {
- constructor(host, model, graph) {
- super();
- this._host = host;
- this._model = model;
- this._container = this._host.document.createElement('div');
- this._container.className = 'sidebar-node';
- if (model.format) {
- this._addProperty('format', new view.ValueTextView(this._host, model.format));
- }
- if (model.producer) {
- this._addProperty('producer', new view.ValueTextView(this._host, model.producer));
- }
- if (model.name) {
- this._addProperty('name', new view.ValueTextView(this._host, model.name));
- }
- if (model.version) {
- this._addProperty('version', new view.ValueTextView(this._host, model.version));
- }
- if (model.description) {
- this._addProperty('description', new view.ValueTextView(this._host, model.description));
- }
- if (model.domain) {
- this._addProperty('domain', new view.ValueTextView(this._host, model.domain));
- }
- if (model.imports) {
- this._addProperty('imports', new view.ValueTextView(this._host, model.imports));
- }
- if (model.runtime) {
- this._addProperty('runtime', new view.ValueTextView(this._host, model.runtime));
- }
- if (model.metadata) {
- for (const entry of model.metadata) {
- this._addProperty(entry.name, new view.ValueTextView(this._host, entry.value));
- }
- }
- const graphs = Array.isArray(model.graphs) ? model.graphs : [];
- if (graphs.length > 1) {
- const graphSelector = new view.SelectView(this._host, model.graphs, graph);
- graphSelector.on('change', (sender, data) => {
- this.emit('update-active-graph', data);
- });
- this._addProperty('subgraph', graphSelector);
- }
- if (graph) {
- if (graph.version) {
- this._addProperty('version', new view.ValueTextView(this._host, graph.version));
- }
- if (graph.type) {
- this._addProperty('type', new view.ValueTextView(this._host, graph.type));
- }
- if (graph.tags) {
- this._addProperty('tags', new view.ValueTextView(this._host, graph.tags));
- }
- if (graph.description) {
- this._addProperty('description', new view.ValueTextView(this._host, graph.description));
- }
- if (Array.isArray(graph.inputs) && graph.inputs.length > 0) {
- this._addHeader('Inputs');
- for (const input of graph.inputs) {
- this.addArgument(input.name, input);
- }
- }
- if (Array.isArray(graph.outputs) && graph.outputs.length > 0) {
- this._addHeader('Outputs');
- for (const output of graph.outputs) {
- this.addArgument(output.name, output);
- }
- }
- }
- }
- render() {
- return [ this._container ];
- }
- _addHeader(title) {
- const element = this._host.document.createElement('div');
- element.className = 'sidebar-header';
- element.innerText = title;
- this._container.appendChild(element);
- }
- _addProperty(name, value) {
- const item = new view.NameValueView(this._host, name, value);
- this._container.appendChild(item.render());
- }
- addArgument(name, argument) {
- const value = new view.ParameterView(this._host, argument);
- value.toggle();
- const item = new view.NameValueView(this._host, name, value);
- this._container.appendChild(item.render());
- }
- };
- view.DocumentationSidebar = class extends view.Control {
- constructor(host, type) {
- super();
- this._host = host;
- this._type = type;
- }
- render() {
- if (!this._elements) {
- this._elements = [];
- const type = view.Documentation.format(this._type);
- const element = this._host.document.createElement('div');
- element.setAttribute('class', 'sidebar-documentation');
- this._append(element, 'h1', type.name);
- if (type.summary) {
- this._append(element, 'p', type.summary);
- }
- if (type.description) {
- this._append(element, 'p', type.description);
- }
- if (Array.isArray(type.attributes) && type.attributes.length > 0) {
- this._append(element, 'h2', 'Attributes');
- const attributes = this._append(element, 'dl');
- for (const attribute of type.attributes) {
- this._append(attributes, 'dt', attribute.name + (attribute.type ? ': <tt>' + attribute.type + '</tt>' : ''));
- this._append(attributes, 'dd', attribute.description);
- }
- element.appendChild(attributes);
- }
- if (Array.isArray(type.inputs) && type.inputs.length > 0) {
- this._append(element, 'h2', 'Inputs' + (type.inputs_range ? ' (' + type.inputs_range + ')' : ''));
- const inputs = this._append(element, 'dl');
- for (const input of type.inputs) {
- this._append(inputs, 'dt', input.name + (input.type ? ': <tt>' + input.type + '</tt>' : '') + (input.option ? ' (' + input.option + ')' : ''));
- this._append(inputs, 'dd', input.description);
- }
- }
- if (Array.isArray(type.outputs) && type.outputs.length > 0) {
- this._append(element, 'h2', 'Outputs' + (type.outputs_range ? ' (' + type.outputs_range + ')' : ''));
- const outputs = this._append(element, 'dl');
- for (const output of type.outputs) {
- this._append(outputs, 'dt', output.name + (output.type ? ': <tt>' + output.type + '</tt>' : '') + (output.option ? ' (' + output.option + ')' : ''));
- this._append(outputs, 'dd', output.description);
- }
- }
- if (Array.isArray(type.type_constraints) && type.type_constraints.length > 0) {
- this._append(element, 'h2', 'Type Constraints');
- const type_constraints = this._append(element, 'dl');
- for (const type_constraint of type.type_constraints) {
- this._append(type_constraints, 'dt', type_constraint.type_param_str + ': ' + type_constraint.allowed_type_strs.map((item) => '<tt>' + item + '</tt>').join(', '));
- this._append(type_constraints, 'dd', type_constraint.description);
- }
- }
- if (Array.isArray(type.examples) && type.examples.length > 0) {
- this._append(element, 'h2', 'Examples');
- for (const example of type.examples) {
- this._append(element, 'h3', example.summary);
- this._append(element, 'pre', example.code);
- }
- }
- if (Array.isArray(type.references) && type.references.length > 0) {
- this._append(element, 'h2', 'References');
- const references = this._append(element, 'ul');
- for (const reference of type.references) {
- this._append(references, 'li', reference.description);
- }
- }
- if (type.domain && type.version && type.support_level) {
- this._append(element, 'h2', 'Support');
- this._append(element, 'dl', 'In domain <tt>' + type.domain + '</tt> since version <tt>' + type.version + '</tt> at support level <tt>' + type.support_level + '</tt>.');
- }
- if (this._host.type === 'Electron') {
- element.addEventListener('click', (e) => {
- if (e.target && e.target.href) {
- const url = e.target.href;
- if (url.startsWith('http://') || url.startsWith('https://')) {
- e.preventDefault();
- this.emit('navigate', { link: url });
- }
- }
- });
- }
- this._elements = [ element ];
- }
- return this._elements;
- }
- _append(parent, type, content) {
- const element = this._host.document.createElement(type);
- if (content) {
- element.innerHTML = content;
- }
- parent.appendChild(element);
- return element;
- }
- };
- view.FindSidebar = class extends view.Control {
- constructor(host, graph) {
- super();
- this._host = host;
- this._graph = graph;
- this._table = new Map();
- this._searchElement = this._host.document.createElement('input');
- this._searchElement.setAttribute('class', 'sidebar-find-search');
- this._searchElement.setAttribute('id', 'search');
- this._searchElement.setAttribute('type', 'text');
- this._searchElement.setAttribute('spellcheck', 'false');
- this._searchElement.setAttribute('placeholder', 'Search');
- this._searchElement.addEventListener('input', (e) => {
- this.update(e.target.value);
- this.emit('search-text-changed', e.target.value);
- });
- this._contentElement = this._host.document.createElement('ol');
- this._contentElement.setAttribute('class', 'sidebar-find-content');
- this._contentElement.addEventListener('click', (e) => {
- const identifier = e.target.getAttribute('data');
- if (this._table.has(identifier)) {
- this.emit('select', this._table.get(identifier));
- }
- });
- }
- on(event, callback) {
- this._events = this._events || {};
- this._events[event] = this._events[event] || [];
- this._events[event].push(callback);
- }
- emit(event, data) {
- if (this._events && this._events[event]) {
- for (const callback of this._events[event]) {
- callback(this, data);
- }
- }
- }
- focus(searchText) {
- this._searchElement.focus();
- this._searchElement.value = '';
- this._searchElement.value = searchText;
- this.update(searchText);
- }
- update(searchText) {
- while (this._contentElement.lastChild) {
- this._contentElement.removeChild(this._contentElement.lastChild);
- }
- this._table.clear();
- let index = 0;
- const add = (value, content) => {
- const key = index.toString();
- index++;
- this._table.set(key, value);
- const element = this._host.document.createElement('li');
- element.innerText = content;
- element.setAttribute('data', key);
- element.addEventListener('pointerover', (e) => {
- const identifier = e.target.getAttribute('data');
- if (this._table.has(identifier)) {
- this.emit('focus', this._table.get(identifier));
- }
- });
- element.addEventListener('pointerleave', (e) => {
- const identifier = e.target.getAttribute('data');
- if (this._table.has(identifier)) {
- this.emit('blur', this._table.get(identifier));
- }
- });
- this._contentElement.appendChild(element);
- };
- let terms = null;
- let match = null;
- const unquote = searchText.match(new RegExp(/^'(.*)'|"(.*)"$/));
- if (unquote) {
- const term = unquote[1] || unquote[2];
- terms = [ term ];
- match = (name) => {
- return term == name;
- };
- } else {
- terms = searchText.trim().toLowerCase().split(' ').map((term) => term.trim()).filter((term) => term.length > 0);
- match = (name) => {
- return terms.every((term) => name.toLowerCase().indexOf(term) !== -1);
- };
- }
- const edges = new Set();
- const arg = (argument) => {
- if (terms.length === 0) {
- return true;
- }
- let match = false;
- for (const term of terms) {
- if (argument.name && argument.name.toLowerCase().indexOf(term) !== -1) {
- match = true;
- break;
- }
- if (argument.type) {
- if (argument.type.dataType && term === argument.type.dataType.toLowerCase()) {
- match = true;
- break;
- }
- if (argument.type.shape) {
- if (term === argument.type.shape.toString().toLowerCase()) {
- match = true;
- break;
- }
- if (argument.type.shape && Array.isArray(argument.type.shape.dimensions)) {
- const dimensions = argument.type.shape.dimensions.map((dimension) => dimension ? dimension.toString().toLowerCase() : '');
- if (term === dimensions.join(',')) {
- match = true;
- break;
- }
- if (dimensions.some((dimension) => term === dimension)) {
- match = true;
- break;
- }
- }
- }
- }
- }
- return match;
- };
- const edge = (argument) => {
- if (argument.name && !edges.has(argument.name) && arg(argument)) {
- add(argument, '\u2192 ' + argument.name.split('\n').shift()); // split custom argument id
- edges.add(argument.name);
- }
- };
- for (const input of this._graph.inputs) {
- for (const value of input.value) {
- edge(value);
- }
- }
- for (const node of this._graph.nodes) {
- const initializers = [];
- for (const input of node.inputs) {
- for (const value of input.value) {
- if (value.initializer) {
- initializers.push(value);
- } else {
- edge(value);
- }
- }
- }
- const name = node.name;
- const type = node.type.name;
- if ((name && match(name)) || (type && match(type))) {
- add(node, '\u25A2 ' + (name || '[' + type + ']'));
- }
- for (const value of initializers) {
- if (value.name && !edges.has(value.name) && arg(value)) {
- add(node, '\u25A0 ' + value.name.split('\n').shift()); // split custom argument id
- }
- }
- }
- for (const output of this._graph.outputs) {
- for (const value of output.value) {
- edge(value);
- }
- }
- this._contentElement.style.display = this._contentElement.childNodes.length != 0 ? 'block' : 'none';
- }
- render() {
- return [ this._searchElement, this._contentElement ];
- }
- };
- view.Tensor = class {
- constructor(tensor) {
- this._tensor = tensor;
- this._type = tensor.type;
- this._stride = tensor.stride;
- switch (tensor.layout) {
- case undefined:
- case '':
- case '<': {
- this._data = this._tensor.values;
- this._layout = '<';
- this._littleEndian = true;
- break;
- }
- case '>': {
- this._data = this._tensor.values;
- this._layout = '>';
- this._littleEndian = false;
- break;
- }
- case '|': {
- this._values = this._tensor.values;
- this._layout = '|';
- break;
- }
- case 'sparse': {
- this._indices = this._tensor.indices;
- this._values = this._tensor.values;
- this._layout = 'sparse';
- break;
- }
- case 'sparse.coo': {
- this._indices = this._tensor.indices;
- this._values = this._tensor.values;
- this._layout = 'sparse.coo';
- break;
- }
- default: {
- this._layout = tensor.layout;
- break;
- }
- }
- view.Tensor.dataTypes = view.Tensor.dataTypeSizes || new Map([
- [ 'boolean', 1 ],
- [ 'qint8', 1 ], [ 'qint16', 2 ], [ 'qint32', 4 ],
- [ 'quint8', 1 ], [ 'quint16', 2 ], [ 'quint32', 4 ],
- [ 'xint8', 1 ],
- [ 'int8', 1 ], [ 'int16', 2 ], [ 'int32', 4 ], [ 'int64', 8 ],
- [ 'uint8', 1 ], [ 'uint16', 2 ], [ 'uint32', 4, ], [ 'uint64', 8 ],
- [ 'float16', 2 ], [ 'float32', 4 ], [ 'float64', 8 ], [ 'bfloat16', 2 ],
- [ 'complex64', 8 ], [ 'complex128', 16 ],
- [ 'float8e4m3fn', 1 ], [ 'float8e4m3fnuz', 1 ], [ 'float8e5m2', 1 ], [ 'float8e5m2fnuz', 1 ]
- ]);
- }
- get type() {
- return this._type;
- }
- get layout() {
- return this._layout;
- }
- get stride() {
- return this._stride;
- }
- get empty() {
- switch (this._layout) {
- case '<':
- case '>': {
- return !(Array.isArray(this._data) || this._data instanceof Uint8Array || this._data instanceof Int8Array) || this._data.length === 0;
- }
- case '|': {
- return !(Array.isArray(this._values) || ArrayBuffer.isView(this._values)) || this._values.length === 0;
- }
- case 'sparse':
- case 'sparse.coo': {
- return !this._values || this.indices || this._values.values.length === 0;
- }
- default: {
- throw new Error("Unsupported tensor format '" + this._format + "'.");
- }
- }
- }
- get value() {
- const context = this._context();
- context.limit = Number.MAX_SAFE_INTEGER;
- switch (context.layout) {
- case '<':
- case '>': {
- return this._decodeData(context, 0);
- }
- case '|': {
- return this._decodeValues(context, 0);
- }
- default: {
- throw new Error("Unsupported tensor format '" + this._format + "'.");
- }
- }
- }
- toString() {
- const context = this._context();
- context.limit = 10000;
- switch (context.layout) {
- case '<':
- case '>': {
- const value = this._decodeData(context, 0);
- return view.Tensor._stringify(value, '', ' ');
- }
- case '|': {
- const value = this._decodeValues(context, 0);
- return view.Tensor._stringify(value, '', ' ');
- }
- default: {
- throw new Error("Unsupported tensor format '" + this._format + "'.");
- }
- }
- }
- _context() {
- if (this._layout !== '<' && this._layout !== '>' && this._layout !== '|' && this._layout !== 'sparse' && this._layout !== 'sparse.coo') {
- throw new Error("Tensor layout '" + this._layout + "' is not supported.");
- }
- const dataType = this._type.dataType;
- const context = {};
- context.layout = this._layout;
- context.dimensions = this._type.shape.dimensions.map((value) => !Number.isInteger(value) && value.toNumber ? value.toNumber() : value);
- context.dataType = dataType;
- const size = context.dimensions.reduce((a, b) => a * b, 1);
- switch (this._layout) {
- case '<':
- case '>': {
- context.data = (this._data instanceof Uint8Array || this._data instanceof Int8Array) ? this._data : this._data.peek();
- context.view = new DataView(context.data.buffer, context.data.byteOffset, context.data.byteLength);
- if (view.Tensor.dataTypes.has(dataType)) {
- context.itemsize = view.Tensor.dataTypes.get(dataType);
- if (context.data.length < (context.itemsize * size)) {
- throw new Error('Invalid tensor data size.');
- }
- } else if (dataType.startsWith('uint') && !isNaN(parseInt(dataType.substring(4), 10))) {
- context.dataType = 'uint';
- context.bits = parseInt(dataType.substring(4), 10);
- context.itemsize = 1;
- } else if (dataType.startsWith('int') && !isNaN(parseInt(dataType.substring(3), 10))) {
- context.dataType = 'int';
- context.bits = parseInt(dataType.substring(3), 10);
- context.itemsize = 1;
- } else {
- throw new Error("Tensor data type '" + dataType + "' is not implemented.");
- }
- break;
- }
- case '|': {
- context.data = this._values;
- if (!view.Tensor.dataTypes.has(dataType) && dataType !== 'string' && dataType !== 'object') {
- throw new Error("Tensor data type '" + dataType + "' is not implemented.");
- }
- if (size !== this._values.length) {
- throw new Error('Invalid tensor data length.');
- }
- break;
- }
- case 'sparse': {
- const indices = new view.Tensor(this._indices).value;
- const values = new view.Tensor(this._values).value;
- context.data = this._decodeSparse(dataType, context.dimensions, indices, values);
- context.layout = '|';
- break;
- }
- case 'sparse.coo': {
- const values = new view.Tensor(this._values).value;
- const data = new view.Tensor(this._indices).value;
- const dimensions = context.dimensions.length;
- let stride = 1;
- const strides = context.dimensions.slice().reverse().map((dim) => {
- const value = stride;
- stride *= dim;
- return value;
- }).reverse();
- const indices = new Uint32Array(values.length);
- for (let i = 0; i < dimensions; i++) {
- const stride = strides[i];
- const dimension = data[i];
- for (let i = 0; i < indices.length; i++) {
- indices[i] += dimension[i] * stride;
- }
- }
- context.data = this._decodeSparse(dataType, context.dimensions, indices, values);
- context.layout = '|';
- break;
- }
- default: {
- throw new view.Tensor("Unsupported tensor layout '" + this._layout + "'.");
- }
- }
- context.index = 0;
- context.count = 0;
- return context;
- }
- _decodeSparse(dataType, dimensions, indices, values) {
- const size = dimensions.reduce((a, b) => a * b, 1);
- const array = new Array(size);
- switch (dataType) {
- case 'boolean':
- array.fill(false);
- break;
- default:
- array.fill(0);
- break;
- }
- if (indices.length > 0) {
- if (Object.prototype.hasOwnProperty.call(indices[0], 'low')) {
- for (let i = 0; i < indices.length; i++) {
- const index = indices[i];
- array[index.high === 0 ? index.low : index.toNumber()] = values[i];
- }
- } else {
- for (let i = 0; i < indices.length; i++) {
- array[indices[i]] = values[i];
- }
- }
- }
- return array;
- }
- _decodeData(context, dimension) {
- const results = [];
- const dimensions = (context.dimensions.length == 0) ? [ 1 ] : context.dimensions;
- const size = dimensions[dimension];
- const dataType = context.dataType;
- const view = context.view;
- if (dimension == dimensions.length - 1) {
- const ellipsis = (context.count + size) > context.limit;
- const length = ellipsis ? context.limit - context.count : size;
- let i = context.index;
- const max = i + (length * context.itemsize);
- switch (dataType) {
- case 'boolean':
- for (; i < max; i += 1) {
- results.push(view.getUint8(i) === 0 ? false : true);
- }
- break;
- case 'qint8':
- case 'xint8':
- case 'int8':
- for (; i < max; i++) {
- results.push(view.getInt8(i));
- }
- break;
- case 'qint16':
- case 'int16':
- for (; i < max; i += 2) {
- results.push(view.getInt16(i, this._littleEndian));
- }
- break;
- case 'qint32':
- case 'int32':
- for (; i < max; i += 4) {
- results.push(view.getInt32(i, this._littleEndian));
- }
- break;
- case 'int64':
- for (; i < max; i += 8) {
- results.push(view.getInt64(i, this._littleEndian));
- }
- break;
- case 'int':
- for (; i < size; i++) {
- results.push(view.getIntBits(i, context.bits));
- }
- break;
- case 'quint8':
- case 'uint8':
- for (; i < max; i++) {
- results.push(view.getUint8(i));
- }
- break;
- case 'quint16':
- case 'uint16':
- for (; i < max; i += 2) {
- results.push(view.getUint16(i, true));
- }
- break;
- case 'quint32':
- case 'uint32':
- for (; i < max; i += 4) {
- results.push(view.getUint32(i, true));
- }
- break;
- case 'uint64':
- for (; i < max; i += 8) {
- results.push(view.getUint64(i, true));
- }
- break;
- case 'uint':
- for (; i < max; i++) {
- results.push(view.getUintBits(i, context.bits));
- }
- break;
- case 'float16':
- for (; i < max; i += 2) {
- results.push(view.getFloat16(i, this._littleEndian));
- }
- break;
- case 'float32':
- for (; i < max; i += 4) {
- results.push(view.getFloat32(i, this._littleEndian));
- }
- break;
- case 'float64':
- for (; i < max; i += 8) {
- results.push(view.getFloat64(i, this._littleEndian));
- }
- break;
- case 'bfloat16':
- for (; i < max; i += 2) {
- results.push(view.getBfloat16(i, this._littleEndian));
- }
- break;
- case 'complex64':
- for (; i < max; i += 8) {
- results.push(view.getComplex64(i, this._littleEndian));
- context.index += 8;
- }
- break;
- case 'complex128':
- for (; i < size; i += 16) {
- results.push(view.getComplex128(i, this._littleEndian));
- }
- break;
- case 'float8e4m3fn':
- for (; i < size; i++) {
- results.push(view.getFloat8e4m3(i, true, false));
- }
- break;
- case 'float8e4m3fnuz':
- for (; i < size; i++) {
- results.push(view.getFloat8e4m3(i, true, true));
- }
- break;
- case 'float8e5m2':
- for (; i < size; i++) {
- results.push(view.getFloat8e5m2(i, false, false));
- }
- break;
- case 'float8e5m2fnuz':
- for (; i < size; i++) {
- results.push(view.getFloat8e5m2(i, true, true));
- }
- break;
- default:
- throw new Error("Unsupported tensor data type '" + dataType + "'.");
- }
- context.index = i;
- context.count += length;
- if (ellipsis) {
- results.push('...');
- }
- } else {
- for (let j = 0; j < size; j++) {
- if (context.count >= context.limit) {
- results.push('...');
- return results;
- }
- results.push(this._decodeData(context, dimension + 1));
- }
- }
- if (context.dimensions.length == 0) {
- return results[0];
- }
- return results;
- }
- _decodeValues(context, dimension) {
- const results = [];
- const dimensions = (context.dimensions.length == 0) ? [ 1 ] : context.dimensions;
- const size = dimensions[dimension];
- const dataType = context.dataType;
- if (dimension == dimensions.length - 1) {
- for (let i = 0; i < size; i++) {
- if (context.count > context.limit) {
- results.push('...');
- return results;
- }
- switch (dataType) {
- case 'boolean':
- results.push(context.data[context.index] === 0 ? false : true);
- break;
- default:
- results.push(context.data[context.index]);
- break;
- }
- context.index++;
- context.count++;
- }
- } else {
- for (let j = 0; j < size; j++) {
- if (context.count > context.limit) {
- results.push('...');
- return results;
- }
- results.push(this._decodeValues(context, dimension + 1));
- }
- }
- if (context.dimensions.length == 0) {
- return results[0];
- }
- return results;
- }
- static _stringify(value, indentation, indent) {
- if (Array.isArray(value)) {
- const result = [];
- result.push(indentation + '[');
- const items = value.map((item) => view.Tensor._stringify(item, indentation + indent, indent));
- if (items.length > 0) {
- result.push(items.join(',\n'));
- }
- result.push(indentation + ']');
- return result.join('\n');
- }
- if (value === null) {
- return indentation + 'null';
- }
- switch (typeof value) {
- case 'boolean':
- return indentation + value.toString();
- case 'string':
- return indentation + '"' + value + '"';
- case 'number':
- if (value == Infinity) {
- return indentation + 'Infinity';
- }
- if (value == -Infinity) {
- return indentation + '-Infinity';
- }
- if (isNaN(value)) {
- return indentation + 'NaN';
- }
- return indentation + value.toString();
- default:
- if (value && value.toString) {
- return indentation + value.toString();
- }
- return indentation + '(undefined)';
- }
- }
- };
- view.Documentation = class {
- static format(source) {
- if (source) {
- const generator = new markdown.Generator();
- const target = {};
- if (source.name !== undefined) {
- target.name = source.name;
- }
- if (source.module !== undefined) {
- target.module = source.module;
- }
- if (source.category !== undefined) {
- target.category = source.category;
- }
- if (source.summary !== undefined) {
- target.summary = generator.html(source.summary);
- }
- if (source.description !== undefined) {
- target.description = generator.html(source.description);
- }
- if (Array.isArray(source.attributes)) {
- target.attributes = source.attributes.map((source) => {
- const target = {};
- target.name = source.name;
- if (source.type !== undefined) {
- target.type = source.type;
- }
- if (source.option !== undefined) {
- target.option = source.option;
- }
- if (source.optional !== undefined) {
- target.optional = source.optional;
- }
- if (source.required !== undefined) {
- target.required = source.required;
- }
- if (source.minimum !== undefined) {
- target.minimum = source.minimum;
- }
- if (source.src !== undefined) {
- target.src = source.src;
- }
- if (source.src_type !== undefined) {
- target.src_type = source.src_type;
- }
- if (source.description !== undefined) {
- target.description = generator.html(source.description);
- }
- if (source.default !== undefined) {
- target.default = source.default;
- }
- if (source.visible !== undefined) {
- target.visible = source.visible;
- }
- return target;
- });
- }
- if (Array.isArray(source.inputs)) {
- target.inputs = source.inputs.map((source) => {
- const target = {};
- target.name = source.name;
- if (source.type !== undefined) {
- target.type = source.type;
- }
- if (source.description !== undefined) {
- target.description = generator.html(source.description);
- }
- if (source.default !== undefined) {
- target.default = source.default;
- }
- if (source.src !== undefined) {
- target.src = source.src;
- }
- if (source.list !== undefined) {
- target.list = source.list;
- }
- if (source.isRef !== undefined) {
- target.isRef = source.isRef;
- }
- if (source.typeAttr !== undefined) {
- target.typeAttr = source.typeAttr;
- }
- if (source.numberAttr !== undefined) {
- target.numberAttr = source.numberAttr;
- }
- if (source.typeListAttr !== undefined) {
- target.typeListAttr = source.typeListAttr;
- }
- if (source.option !== undefined) {
- target.option = source.option;
- }
- if (source.optional !== undefined) {
- target.optional = source.optional;
- }
- if (source.visible !== undefined) {
- target.visible = source.visible;
- }
- return target;
- });
- }
- if (Array.isArray(source.outputs)) {
- target.outputs = source.outputs.map((source) => {
- const target = {};
- target.name = source.name;
- if (source.type) {
- target.type = source.type;
- }
- if (source.description !== undefined) {
- target.description = generator.html(source.description);
- }
- if (source.list !== undefined) {
- target.list = source.list;
- }
- if (source.typeAttr !== undefined) {
- target.typeAttr = source.typeAttr;
- }
- if (source.typeListAttr !== undefined) {
- target.typeListAttr = source.typeAttr;
- }
- if (source.numberAttr !== undefined) {
- target.numberAttr = source.numberAttr;
- }
- if (source.isRef !== undefined) {
- target.isRef = source.isRef;
- }
- if (source.option !== undefined) {
- target.option = source.option;
- }
- return target;
- });
- }
- if (Array.isArray(source.references)) {
- target.references = source.references.map((source) => {
- if (source) {
- target.description = generator.html(source.description);
- }
- return target;
- });
- }
- if (source.version !== undefined) {
- target.version = source.version;
- }
- if (source.operator !== undefined) {
- target.operator = source.operator;
- }
- if (source.identifier !== undefined) {
- target.identifier = source.identifier;
- }
- if (source.package !== undefined) {
- target.package = source.package;
- }
- if (source.support_level !== undefined) {
- target.support_level = source.support_level;
- }
- if (source.min_input !== undefined) {
- target.min_input = source.min_input;
- }
- if (source.max_input !== undefined) {
- target.max_input = source.max_input;
- }
- if (source.min_output !== undefined) {
- target.min_output = source.min_output;
- }
- if (source.max_input !== undefined) {
- target.max_output = source.max_output;
- }
- if (source.inputs_range !== undefined) {
- target.inputs_range = source.inputs_range;
- }
- if (source.outputs_range !== undefined) {
- target.outputs_range = source.outputs_range;
- }
- if (source.examples !== undefined) {
- target.examples = source.examples;
- }
- if (source.constants !== undefined) {
- target.constants = source.constants;
- }
- if (source.type_constraints !== undefined) {
- target.type_constraints = source.type_constraints;
- }
- return target;
- }
- return '';
- }
- };
- view.Formatter = class {
- constructor(value, type, quote) {
- this._value = value;
- this._type = type;
- this._quote = quote;
- this._values = new Set();
- }
- toString() {
- return this._format(this._value, this._type, this._quote);
- }
- _format(value, type, quote) {
- if (value && value.__class__ && value.__class__.__module__ === 'builtins' && value.__class__.__name__ === 'type') {
- return value.__module__ + '.' + value.__name__;
- }
- if (value && value.__class__ && value.__class__.__module__ === 'builtins' && value.__class__.__name__ === 'function') {
- return value.__module__ + '.' + value.__name__;
- }
- if (typeof value === 'function') {
- return value();
- }
- if (value && (value instanceof base.Int64 || value instanceof base.Uint64)) {
- return value.toString();
- }
- if (Number.isNaN(value)) {
- return 'NaN';
- }
- switch (type) {
- case 'shape':
- return value ? value.toString() : '(null)';
- case 'shape[]':
- if (value && !Array.isArray(value)) {
- throw new Error("Invalid shape '" + JSON.stringify(value) + "'.");
- }
- return value ? value.map((item) => item.toString()).join(', ') : '(null)';
- case 'graph':
- return value ? value.name : '(null)';
- case 'graph[]':
- return value ? value.map((graph) => graph.name).join(', ') : '(null)';
- case 'tensor':
- if (value && value.type && value.type.shape && value.type.shape.dimensions && value.type.shape.dimensions.length == 0) {
- return value.toString();
- }
- return '[...]';
- case 'function':
- return value.type.name;
- case 'function[]':
- return value ? value.map((item) => item.type.name).join(', ') : '(null)';
- case 'type':
- return value ? value.toString() : '(null)';
- case 'type[]':
- return value ? value.map((item) => item.toString()).join(', ') : '(null)';
- default:
- break;
- }
- if (typeof value === 'string' && (!type || type != 'string')) {
- return quote ? '"' + value + '"' : value;
- }
- if (Array.isArray(value)) {
- if (value.length == 0) {
- return quote ? '[]' : '';
- }
- let ellipsis = false;
- if (value.length > 1000) {
- value = value.slice(0, 1000);
- ellipsis = true;
- }
- const itemType = (type && type.endsWith('[]')) ? type.substring(0, type.length - 2) : null;
- const array = value.map((item) => {
- if (item && (item instanceof base.Int64 || item instanceof base.Uint64)) {
- return item.toString();
- }
- if (Number.isNaN(item)) {
- return 'NaN';
- }
- const quote = !itemType || itemType === 'string';
- return this._format(item, itemType, quote);
- });
- if (ellipsis) {
- array.push('\u2026');
- }
- return quote ? [ '[', array.join(', '), ']' ].join(' ') : array.join(', ');
- }
- if (value === null) {
- return quote ? 'null' : '';
- }
- if (value === undefined) {
- return 'undefined';
- }
- if (value !== Object(value)) {
- return value.toString();
- }
- if (this._values.has(value)) {
- return '\u2026';
- }
- this._values.add(value);
- let list = null;
- const entries = Object.entries(value).filter((entry) => !entry[0].startsWith('__') && !entry[0].endsWith('__'));
- if (entries.length == 1) {
- list = [ this._format(entries[0][1], null, true) ];
- } else {
- list = new Array(entries.length);
- for (let i = 0; i < entries.length; i++) {
- const entry = entries[i];
- list[i] = entry[0] + ': ' + this._format(entry[1], null, true);
- }
- }
- let objectType = value.__type__;
- if (!objectType && value.constructor.name && value.constructor.name !== 'Object') {
- objectType = value.constructor.name;
- }
- if (objectType) {
- return objectType + (list.length == 0 ? '()' : [ '(', list.join(', '), ')' ].join(''));
- }
- switch (list.length) {
- case 0:
- return quote ? '()' : '';
- case 1:
- return list[0];
- default:
- return quote ? [ '(', list.join(', '), ')' ].join(' ') : list.join(', ');
- }
- }
- };
- const markdown = {};
- markdown.Generator = class {
- constructor() {
- this._newlineRegExp = /^\n+/;
- this._codeRegExp = /^( {4}[^\n]+\n*)+/;
- this._fencesRegExp = /^ {0,3}(`{3,}(?=[^`\n]*\n)|~{3,})([^\n]*)\n(?:|([\s\S]*?)\n)(?: {0,3}\1[~`]* *(?:\n+|$)|$)/;
- this._hrRegExp = /^ {0,3}((?:- *){3,}|(?:_ *){3,}|(?:\* *){3,})(?:\n+|$)/;
- this._headingRegExp = /^ {0,3}(#{1,6}) +([^\n]*?)(?: +#+)? *(?:\n+|$)/;
- this._blockquoteRegExp = /^( {0,3}> ?(([^\n]+(?:\n(?! {0,3}((?:- *){3,}|(?:_ *){3,}|(?:\* *){3,})(?:\n+|$)| {0,3}#{1,6} | {0,3}>| {0,3}(?:`{3,}(?=[^`\n]*\n)|~{3,})[^\n]*\n| {0,3}(?:[*+-]|1[.)]) |<\/?(?:address|article|aside|base|basefont|blockquote|body|caption|center|col|colgroup|dd|details|dialog|dir|div|dl|dt|fieldset|figcaption|figure|footer|form|frame|frameset|h[1-6]|head|header|hr|html|iframe|legend|li|link|main|menu|menuitem|meta|nav|noframes|ol|optgroup|option|p|param|section|source|summary|table|tbody|td|tfoot|th|thead|title|tr|track|ul)(?: +|\n|\/?>)|<(?:script|pre|style|!--))[^\n]+)*)|[^\n]*)(?:\n|$))+/;
- this._listRegExp = /^( {0,3})((?:[*+-]|\d{1,9}[.)])) [\s\S]+?(?:\n+(?=\1?(?:(?:- *){3,}|(?:_ *){3,}|(?:\* *){3,})(?:\n+|$))|\n+(?= {0,3}\[((?!\s*\])(?:\\[[\]]|[^[\]])+)\]: *\n? *<?([^\s>]+)>?(?:(?: +\n? *| *\n *)((?:"(?:\\"?|[^"\\])*"|'[^'\n]*(?:\n[^'\n]+)*\n?'|\([^()]*\))))? *(?:\n+|$))|\n{2,}(?! )(?!\1(?:[*+-]|\d{1,9}[.)]) )\n*|\s*$)/;
- this._htmlRegExp = /^ {0,3}(?:<(script|pre|style)[\s>][\s\S]*?(?:<\/\1>[^\n]*\n+|$)|<!--(?!-?>)[\s\S]*?(?:-->|$)[^\n]*(\n+|$)|<\?[\s\S]*?(?:\?>\n*|$)|<![A-Z][\s\S]*?(?:>\n*|$)|<!\[CDATA\[[\s\S]*?(?:\]\]>\n*|$)|<\/?(address|article|aside|base|basefont|blockquote|body|caption|center|col|colgroup|dd|details|dialog|dir|div|dl|dt|fieldset|figcaption|figure|footer|form|frame|frameset|h[1-6]|head|header|hr|html|iframe|legend|li|link|main|menu|menuitem|meta|nav|noframes|ol|optgroup|option|p|param|section|source|summary|table|tbody|td|tfoot|th|thead|title|tr|track|ul)(?: +|\n|\/?>)[\s\S]*?(?:\n{2,}|$)|<(?!script|pre|style)([a-z][\w-]*)(?: +[a-zA-Z:_][\w.:-]*(?: *= *"[^"\n]*"| *= *'[^'\n]*'| *= *[^\s"'=<>`]+)?)*? *\/?>(?=[ \t]*(?:\n|$))[\s\S]*?(?:\n{2,}|$)|<\/(?!script|pre|style)[a-z][\w-]*\s*>(?=[ \t]*(?:\n|$))[\s\S]*?(?:\n{2,}|$))/i;
- this._defRegExp = /^ {0,3}\[((?!\s*\])(?:\\[[\]]|[^[\]])+)\]: *\n? *<?([^\s>]+)>?(?:(?: +\n? *| *\n *)((?:"(?:\\"?|[^"\\])*"|'[^'\n]*(?:\n[^'\n]+)*\n?'|\([^()]*\))))? *(?:\n+|$)/;
- this._nptableRegExp = /^ *([^|\n ].*\|.*)\n {0,3}([-:]+ *\|[-| :]*)(?:\n((?:(?!\n| {0,3}((?:- *){3,}|(?:_ *){3,}|(?:\* *){3,})(?:\n+|$)| {0,3}#{1,6} | {0,3}>| {4}[^\n]| {0,3}(?:`{3,}(?=[^`\n]*\n)|~{3,})[^\n]*\n| {0,3}(?:[*+-]|1[.)]) |<\/?(?:address|article|aside|base|basefont|blockquote|body|caption|center|col|colgroup|dd|details|dialog|dir|div|dl|dt|fieldset|figcaption|figure|footer|form|frame|frameset|h[1-6]|head|header|hr|html|iframe|legend|li|link|main|menu|menuitem|meta|nav|noframes|ol|optgroup|option|p|param|section|source|summary|table|tbody|td|tfoot|th|thead|title|tr|track|ul)(?: +|\n|\/?>)|<(?:script|pre|style|!--)).*(?:\n|$))*)\n*|$)/;
- this._tableRegExp = /^ *\|(.+)\n {0,3}\|?( *[-:]+[-| :]*)(?:\n *((?:(?!\n| {0,3}((?:- *){3,}|(?:_ *){3,}|(?:\* *){3,})(?:\n+|$)| {0,3}#{1,6} | {0,3}>| {4}[^\n]| {0,3}(?:`{3,}(?=[^`\n]*\n)|~{3,})[^\n]*\n| {0,3}(?:[*+-]|1[.)]) |<\/?(?:address|article|aside|base|basefont|blockquote|body|caption|center|col|colgroup|dd|details|dialog|dir|div|dl|dt|fieldset|figcaption|figure|footer|form|frame|frameset|h[1-6]|head|header|hr|html|iframe|legend|li|link|main|menu|menuitem|meta|nav|noframes|ol|optgroup|option|p|param|section|source|summary|table|tbody|td|tfoot|th|thead|title|tr|track|ul)(?: +|\n|\/?>)|<(?:script|pre|style|!--)).*(?:\n|$))*)\n*|$)/;
- this._lheadingRegExp = /^([^\n]+)\n {0,3}(=+|-+) *(?:\n+|$)/;
- this._textRegExp = /^[^\n]+/;
- this._bulletRegExp = /(?:[*+-]|\d{1,9}[.)])/;
- this._itemRegExp = /^( *)((?:[*+-]|\d{1,9}[.)])) ?[^\n]*(?:\n(?!\1(?:[*+-]|\d{1,9}[.)]) ?)[^\n]*)*/gm;
- this._paragraphRegExp = /^([^\n]+(?:\n(?! {0,3}((?:- *){3,}|(?:_ *){3,}|(?:\* *){3,})(?:\n+|$)| {0,3}#{1,6} | {0,3}>| {0,3}(?:`{3,}(?=[^`\n]*\n)|~{3,})[^\n]*\n| {0,3}(?:[*+-]|1[.)]) |<\/?(?:address|article|aside|base|basefont|blockquote|body|caption|center|col|colgroup|dd|details|dialog|dir|div|dl|dt|fieldset|figcaption|figure|footer|form|frame|frameset|h[1-6]|head|header|hr|html|iframe|legend|li|link|main|menu|menuitem|meta|nav|noframes|ol|optgroup|option|p|param|section|source|summary|table|tbody|td|tfoot|th|thead|title|tr|track|ul)(?: +|\n|\/?>)|<(?:script|pre|style|!--))[^\n]+)*)/;
- this._backpedalRegExp = /(?:[^?!.,:;*_~()&]+|\([^)]*\)|&(?![a-zA-Z0-9]+;$)|[?!.,:;*_~)]+(?!$))+/;
- this._escapeRegExp = /^\\([!"#$%&'()*+,\-./:;<=>?@[\]\\^_`{|}~~|])/;
- this._escapesRegExp = /\\([!"#$%&'()*+,\-./:;<=>?@[\]\\^_`{|}~])/g;
- /* eslint-disable no-control-regex */
- this._autolinkRegExp = /^<([a-zA-Z][a-zA-Z0-9+.-]{1,31}:[^\s\x00-\x1f<>]*|[a-zA-Z0-9.!#$%&'*+/=?_`{|}~-]+(@)[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)+(?![-_]))>/;
- this._linkRegExp = /^!?\[((?:\[(?:\\.|[^[\]\\])*\]|\\.|`[^`]*`|[^[\]\\`])*?)\]\(\s*(<(?:\\[<>]?|[^\s<>\\])*>|[^\s\x00-\x1f]*)(?:\s+("(?:\\"?|[^"\\])*"|'(?:\\'?|[^'\\])*'|\((?:\\\)?|[^)\\])*\)))?\s*\)/;
- /* eslint-enable no-control-regex */
- this._urlRegExp = /^((?:ftp|https?):\/\/|www\.)(?:[a-zA-Z0-9-]+\.?)+[^\s<]*|^[A-Za-z0-9._+-]+(@)[a-zA-Z0-9-_]+(?:\.[a-zA-Z0-9-_]*[a-zA-Z0-9])+(?![-_])/i;
- this._tagRegExp = /^<!--(?!-?>)[\s\S]*?-->|^<\/[a-zA-Z][\w:-]*\s*>|^<[a-zA-Z][\w-]*(?:\s+[a-zA-Z:_][\w.:-]*(?:\s*=\s*"[^"]*"|\s*=\s*'[^']*'|\s*=\s*[^\s"'=<>`]+)?)*?\s*\/?>|^<\?[\s\S]*?\?>|^<![a-zA-Z]+\s[\s\S]*?>|^<!\[CDATA\[[\s\S]*?\]\]>/;
- this._reflinkRegExp = /^!?\[((?:\[(?:\\.|[^[\]\\])*\]|\\.|`[^`]*`|[^[\]\\`])*?)\]\[(?!\s*\])((?:\\[[\]]?|[^[\]\\])+)\]/;
- this._nolinkRegExp = /^!?\[(?!\s*\])((?:\[[^[\]]*\]|\\[[\]]|[^[\]])*)\](?:\[\])?/;
- this._reflinkSearchRegExp = /!?\[((?:\[(?:\\.|[^[\]\\])*\]|\\.|`[^`]*`|[^[\]\\`])*?)\]\[(?!\s*\])((?:\\[[\]]?|[^[\]\\])+)\]|!?\[(?!\s*\])((?:\[[^[\]]*\]|\\[[\]]|[^[\]])*)\](?:\[\])?(?!\()/g;
- this._strongStartRegExp = /^(?:(\*\*(?=[*!"#$%&'()+\-.,/:;<=>?@[\]`{|}~]))|\*\*)(?![\s])|__/;
- this._strongMiddleRegExp = /^\*\*(?:(?:(?!__[^_]*?__|\*\*\[^\*\]*?\*\*)(?:[^*]|\\\*)|__[^_]*?__|\*\*\[^\*\]*?\*\*)|\*(?:(?!__[^_]*?__|\*\*\[^\*\]*?\*\*)(?:[^*]|\\\*)|__[^_]*?__|\*\*\[^\*\]*?\*\*)*?\*)+?\*\*$|^__(?![\s])((?:(?:(?!__[^_]*?__|\*\*\[^\*\]*?\*\*)(?:[^_]|\\_)|__[^_]*?__|\*\*\[^\*\]*?\*\*)|_(?:(?!__[^_]*?__|\*\*\[^\*\]*?\*\*)(?:[^_]|\\_)|__[^_]*?__|\*\*\[^\*\]*?\*\*)*?_)+?)__$/;
- this._strongEndAstRegExp = /[^!"#$%&'()+\-.,/:;<=>?@[\]`{|}~\s]\*\*(?!\*)|[!"#$%&'()+\-.,/:;<=>?@[\]`{|}~]\*\*(?!\*)(?:(?=[!"#$%&'()+\-.,/:;<=>?@[\]`{|}~_\s]|$))/g;
- this._strongEndUndRegExp = /[^\s]__(?!_)(?:(?=[!"#$%&'()+\-.,/:;<=>?@[\]`{|}~*\s])|$)/g;
- this._emStartRegExp = /^(?:(\*(?=[!"#$%&'()+\-.,/:;<=>?@[\]`{|}~]))|\*)(?![*\s])|_/;
- this._emMiddleRegExp = /^\*(?:(?:(?!__[^_]*?__|\*\*\[^\*\]*?\*\*)(?:[^*]|\\\*)|__[^_]*?__|\*\*\[^\*\]*?\*\*)|\*(?:(?!__[^_]*?__|\*\*\[^\*\]*?\*\*)(?:[^*]|\\\*)|__[^_]*?__|\*\*\[^\*\]*?\*\*)*?\*)+?\*$|^_(?![_\s])(?:(?:(?!__[^_]*?__|\*\*\[^\*\]*?\*\*)(?:[^_]|\\_)|__[^_]*?__|\*\*\[^\*\]*?\*\*)|_(?:(?!__[^_]*?__|\*\*\[^\*\]*?\*\*)(?:[^_]|\\_)|__[^_]*?__|\*\*\[^\*\]*?\*\*)*?_)+?_$/;
- this._emEndAstRegExp = /[^!"#$%&'()+\-.,/:;<=>?@[\]`{|}~\s]\*(?!\*)|[!"#$%&'()+\-.,/:;<=>?@[\]`{|}~]\*(?!\*)(?:(?=[!"#$%&'()+\-.,/:;<=>?@[\]`{|}~_\s]|$))/g;
- this._emEndUndRegExp = /[^\s]_(?!_)(?:(?=[!"#$%&'()+\-.,/:;<=>?@[\]`{|}~*\s])|$)/g;
- this._codespanRegExp = /^(`+)([^`]|[^`][\s\S]*?[^`])\1(?!`)/;
- this._brRegExp = /^( {2,}|\\)\n(?!\s*$)/;
- this._delRegExp = /^~+(?=\S)([\s\S]*?\S)~+/;
- this._textspanRegExp = /^(`+|[^`])(?:(?= {2,}\n)|[\s\S]*?(?:(?=[\\<![`*~]|\b_|https?:\/\/|ftp:\/\/|www\.|$)|[^ ](?= {2,}\n)|[^a-zA-Z0-9.!#$%&'*+/=?_`{|}~-](?=[a-zA-Z0-9.!#$%&'*+/=?_`{|}~-]+@))|(?=[a-zA-Z0-9.!#$%&'*+/=?_`{|}~-]+@))/;
- this._punctuationRegExp = /^([\s*!"#$%&'()+\-.,/:;<=>?@[\]`{|}~])/;
- this._blockSkipRegExp = /\[[^\]]*?\]\([^)]*?\)|`[^`]*?`|<[^>]*?>/g;
- this._escapeTestRegExp = /[&<>"']/;
- this._escapeReplaceRegExp = /[&<>"']/g;
- this._escapeTestNoEncodeRegExp = /[<>"']|&(?!#?\w+;)/;
- this._escapeReplaceNoEncodeRegExp = /[<>"']|&(?!#?\w+;)/g;
- this._escapeReplacementsMap = { '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' };
- }
- html(source) {
- const tokens = [];
- const links = new Map();
- source = source.replace(/\r\n|\r/g, '\n').replace(/\t/g, ' ');
- this._tokenize(source, tokens, links, true);
- this._tokenizeBlock(tokens, links);
- const result = this._render(tokens, true);
- return result;
- }
- _tokenize(source, tokens, links, top) {
- source = source.replace(/^ +$/gm, '');
- while (source) {
- let match = this._newlineRegExp.exec(source);
- if (match) {
- source = source.substring(match[0].length);
- if (match[0].length > 1) {
- tokens.push({ type: 'space' });
- }
- continue;
- }
- match = this._codeRegExp.exec(source);
- if (match) {
- source = source.substring(match[0].length);
- const lastToken = tokens[tokens.length - 1];
- if (lastToken && lastToken.type === 'paragraph') {
- lastToken.text += '\n' + match[0].trimRight();
- } else {
- const text = match[0].replace(/^ {4}/gm, '').replace(/\n*$/, '');
- tokens.push({ type: 'code', text: text });
- }
- continue;
- }
- match = this._fencesRegExp.exec(source);
- if (match) {
- source = source.substring(match[0].length);
- const language = match[2] ? match[2].trim() : match[2];
- let content = match[3] || '';
- const matchIndent = match[0].match(/^(\s+)(?:```)/);
- if (matchIndent !== null) {
- const indent = matchIndent[1];
- content = content.split('\n').map(node => {
- const match = node.match(/^\s+/);
- return (match !== null && match[0].length >= indent.length) ? node.slice(indent.length) : node;
- }).join('\n');
- }
- tokens.push({ type: 'code', language: language, text: content });
- continue;
- }
- match = this._headingRegExp.exec(source);
- if (match) {
- source = source.substring(match[0].length);
- tokens.push({ type: 'heading', depth: match[1].length, text: match[2] });
- continue;
- }
- match = this._nptableRegExp.exec(source);
- if (match) {
- const header = this._splitCells(match[1].replace(/^ *| *\| *$/g, ''));
- const align = match[2].replace(/^ *|\| *$/g, '').split(/ *\| */);
- if (header.length === align.length) {
- const cells = match[3] ? match[3].replace(/\n$/, '').split('\n') : [];
- const token = { type: 'table', header: header, align: align, cells: cells, raw: match[0] };
- for (let i = 0; i < token.align.length; i++) {
- if (/^ *-+: *$/.test(token.align[i])) {
- token.align[i] = 'right';
- } else if (/^ *:-+: *$/.test(token.align[i])) {
- token.align[i] = 'center';
- } else if (/^ *:-+ *$/.test(token.align[i])) {
- token.align[i] = 'left';
- } else {
- token.align[i] = null;
- }
- }
- token.cells = token.cells.map((cell) => this._splitCells(cell, token.header.length));
- source = source.substring(token.raw.length);
- tokens.push(token);
- continue;
- }
- }
- match = this._hrRegExp.exec(source);
- if (match) {
- source = source.substring(match[0].length);
- tokens.push({ type: 'hr' });
- continue;
- }
- match = this._blockquoteRegExp.exec(source);
- if (match) {
- source = source.substring(match[0].length);
- const text = match[0].replace(/^ *> ?/gm, '');
- tokens.push({ type: 'blockquote', text: text, tokens: this._tokenize(text, [], links, top) });
- continue;
- }
- match = this._listRegExp.exec(source);
- if (match) {
- let raw = match[0];
- const bull = match[2];
- const ordered = bull.length > 1;
- const parent = bull[bull.length - 1] === ')';
- const list = { type: 'list', raw: raw, ordered: ordered, start: ordered ? +bull.slice(0, -1) : '', loose: false, items: [] };
- const itemMatch = match[0].match(this._itemRegExp);
- let next = false;
- const length = itemMatch.length;
- for (let i = 0; i < length; i++) {
- let item = itemMatch[i];
- raw = item;
- let space = item.length;
- item = item.replace(/^ *([*+-]|\d+[.)]) ?/, '');
- if (~item.indexOf('\n ')) {
- space -= item.length;
- item = item.replace(new RegExp('^ {1,' + space + '}', 'gm'), '');
- }
- if (i !== length - 1) {
- const bullet = this._bulletRegExp.exec(itemMatch[i + 1])[0];
- if (ordered ? bullet.length === 1 || (!parent && bullet[bullet.length - 1] === ')') : (bullet.length > 1)) {
- const addBack = itemMatch.slice(i + 1).join('\n');
- list.raw = list.raw.substring(0, list.raw.length - addBack.length);
- i = length - 1;
- }
- }
- let loose = next || /\n\n(?!\s*$)/.test(item);
- if (i !== length - 1) {
- next = item.charAt(item.length - 1) === '\n';
- if (!loose) {
- loose = next;
- }
- }
- if (loose) {
- list.loose = true;
- }
- const task = /^\[[ xX]\] /.test(item);
- let checked = undefined;
- if (task) {
- checked = item[1] !== ' ';
- item = item.replace(/^\[[ xX]\] +/, '');
- }
- list.items.push({ type: 'list_item', raw, task: task, checked: checked, loose: loose, text: item });
- }
- source = source.substring(list.raw.length);
- for (const item of list.items) {
- item.tokens = this._tokenize(item.text, [], links, false);
- }
- tokens.push(list);
- continue;
- }
- match = this._htmlRegExp.exec(source);
- if (match) {
- source = source.substring(match[0].length);
- tokens.push({ type: 'html', pre: (match[1] === 'pre' || match[1] === 'script' || match[1] === 'style'), text: match[0] });
- continue;
- }
- if (top) {
- match = this._defRegExp.exec(source);
- if (match) {
- source = source.substring(match[0].length);
- match[3] = match[3] ? match[3].substring(1, match[3].length - 1) : match[3];
- const tag = match[1].toLowerCase().replace(/\s+/g, ' ');
- if (!links.has(tag)) {
- links.set(tag, { href: match[2], title: match[3] });
- }
- continue;
- }
- }
- match = this._tableRegExp.exec(source);
- if (match) {
- const header = this._splitCells(match[1].replace(/^ *| *\| *$/g, ''));
- const align = match[2].replace(/^ *|\| *$/g, '').split(/ *\| */);
- if (header.length === align.length) {
- const cells = match[3] ? match[3].replace(/\n$/, '').split('\n') : [];
- const token = { type: 'table', header: header, align: align, cells: cells, raw: match[0] };
- for (let i = 0; i < token.align.length; i++) {
- if (/^ *-+: *$/.test(token.align[i])) {
- token.align[i] = 'right';
- } else if (/^ *:-+: *$/.test(token.align[i])) {
- token.align[i] = 'center';
- } else if (/^ *:-+ *$/.test(token.align[i])) {
- token.align[i] = 'left';
- } else {
- token.align[i] = null;
- }
- }
- token.cells = token.cells.map((cell) => this._splitCells(cell.replace(/^ *\| *| *\| *$/g, ''), token.header.length));
- source = source.substring(token.raw.length);
- tokens.push(token);
- continue;
- }
- }
- match = this._lheadingRegExp.exec(source);
- if (match) {
- source = source.substring(match[0].length);
- tokens.push({ type: 'heading', depth: match[2].charAt(0) === '=' ? 1 : 2, text: match[1] });
- continue;
- }
- if (top) {
- match = this._paragraphRegExp.exec(source);
- if (match) {
- source = source.substring(match[0].length);
- tokens.push({ type: 'paragraph', text: match[1].charAt(match[1].length - 1) === '\n' ? match[1].slice(0, -1) : match[1] });
- continue;
- }
- }
- match = this._textRegExp.exec(source);
- if (match) {
- source = source.substring(match[0].length);
- const lastToken = tokens[tokens.length - 1];
- if (lastToken && lastToken.type === 'text') {
- lastToken.text += '\n' + match[0];
- } else {
- tokens.push({ type: 'text', text: match[0] });
- }
- continue;
- }
- throw new Error("Unexpected '" + source.charCodeAt(0) + "'.");
- }
- return tokens;
- }
- _tokenizeInline(source, links, inLink, inRawBlock, prevChar) {
- const tokens = [];
- let maskedSource = source;
- if (links.size > 0) {
- while (maskedSource) {
- const match = this._reflinkSearchRegExp.exec(maskedSource);
- if (match) {
- if (links.has(match[0].slice(match[0].lastIndexOf('[') + 1, -1))) {
- maskedSource = maskedSource.slice(0, match.index) + '[' + 'a'.repeat(match[0].length - 2) + ']' + maskedSource.slice(this._reflinkSearchRegExp.lastIndex);
- }
- continue;
- }
- break;
- }
- }
- while (maskedSource) {
- const match = this._blockSkipRegExp.exec(maskedSource);
- if (match) {
- maskedSource = maskedSource.slice(0, match.index) + '[' + 'a'.repeat(match[0].length - 2) + ']' + maskedSource.slice(this._blockSkipRegExp.lastIndex);
- continue;
- }
- break;
- }
- while (source) {
- let match = this._escapeRegExp.exec(source);
- if (match) {
- source = source.substring(match[0].length);
- tokens.push({ type: 'escape', text: this._escape(match[1]) });
- continue;
- }
- match = this._tagRegExp.exec(source);
- if (match) {
- source = source.substring(match[0].length);
- if (!inLink && /^<a /i.test(match[0])) {
- inLink = true;
- } else if (inLink && /^<\/a>/i.test(match[0])) {
- inLink = false;
- }
- if (!inRawBlock && /^<(pre|code|kbd|script)(\s|>)/i.test(match[0])) {
- inRawBlock = true;
- } else if (inRawBlock && /^<\/(pre|code|kbd|script)(\s|>)/i.test(match[0])) {
- inRawBlock = false;
- }
- tokens.push({ type: 'html', raw: match[0], text: match[0] });
- continue;
- }
- match = this._linkRegExp.exec(source);
- if (match) {
- let index = -1;
- const ref = match[2];
- if (ref.indexOf(')') !== -1) {
- let level = 0;
- for (let i = 0; i < ref.length; i++) {
- switch (ref[i]) {
- case '\\':
- i++;
- break;
- case '(':
- level++;
- break;
- case ')':
- level--;
- if (level < 0) {
- index = i;
- i = ref.length;
- }
- break;
- default:
- break;
- }
- }
- }
- if (index > -1) {
- const length = (match[0].indexOf('!') === 0 ? 5 : 4) + match[1].length + index;
- match[2] = match[2].substring(0, index);
- match[0] = match[0].substring(0, length).trim();
- match[3] = '';
- }
- const title = (match[3] ? match[3].slice(1, -1) : '').replace(this._escapesRegExp, '$1');
- const href = match[2].trim().replace(/^<([\s\S]*)>$/, '$1').replace(this._escapesRegExp, '$1');
- const token = this._outputLink(match, href, title);
- source = source.substring(match[0].length);
- if (token.type === 'link') {
- token.tokens = this._tokenizeInline(token.text, links, true, inRawBlock, '');
- }
- tokens.push(token);
- continue;
- }
- match = this._reflinkRegExp.exec(source) || this._nolinkRegExp.exec(source);
- if (match) {
- let link = (match[2] || match[1]).replace(/\s+/g, ' ');
- link = links.get(link.toLowerCase());
- if (!link || !link.href) {
- const text = match[0].charAt(0);
- source = source.substring(text.length);
- tokens.push({ type: 'text', text: text });
- } else {
- source = source.substring(match[0].length);
- const token = this._outputLink(match, link);
- if (token.type === 'link') {
- token.tokens = this._tokenizeInline(token.text, links, true, inRawBlock, '');
- }
- tokens.push(token);
- }
- continue;
- }
- match = this._strongStartRegExp.exec(source);
- if (match && (!match[1] || (match[1] && (prevChar === '' || this._punctuationRegExp.exec(prevChar))))) {
- const masked = maskedSource.slice(-1 * source.length);
- const endReg = match[0] === '**' ? this._strongEndAstRegExp : this._strongEndUndRegExp;
- endReg.lastIndex = 0;
- let cap;
- while ((match = endReg.exec(masked)) != null) {
- cap = this._strongMiddleRegExp.exec(masked.slice(0, match.index + 3));
- if (cap) {
- break;
- }
- }
- if (cap) {
- const text = source.substring(2, cap[0].length - 2);
- source = source.substring(cap[0].length);
- tokens.push({ type: 'strong', text: text, tokens: this._tokenizeInline(text, links, inLink, inRawBlock, '') });
- continue;
- }
- }
- match = this._emStartRegExp.exec(source);
- if (match && (!match[1] || (match[1] && (prevChar === '' || this._punctuationRegExp.exec(prevChar))))) {
- const masked = maskedSource.slice(-1 * source.length);
- const endReg = match[0] === '*' ? this._emEndAstRegExp : this._emEndUndRegExp;
- endReg.lastIndex = 0;
- let cap;
- while ((match = endReg.exec(masked)) != null) {
- cap = this._emMiddleRegExp.exec(masked.slice(0, match.index + 2));
- if (cap) {
- break;
- }
- }
- if (cap) {
- const text = source.slice(1, cap[0].length - 1);
- source = source.substring(cap[0].length);
- tokens.push({ type: 'em', text: text, tokens: this._tokenizeInline(text, links, inLink, inRawBlock, '') });
- continue;
- }
- }
- match = this._codespanRegExp.exec(source);
- if (match) {
- source = source.substring(match[0].length);
- let content = match[2].replace(/\n/g, ' ');
- if (/[^ ]/.test(content) && content.startsWith(' ') && content.endsWith(' ')) {
- content = content.substring(1, content.length - 1);
- }
- tokens.push({ type: 'codespan', text: this._encode(content) });
- continue;
- }
- match = this._brRegExp.exec(source);
- if (match) {
- source = source.substring(match[0].length);
- tokens.push({ type: 'br' });
- continue;
- }
- match = this._delRegExp.exec(source);
- if (match) {
- source = source.substring(match[0].length);
- const text = match[1];
- tokens.push({ type: 'del', text: text, tokens: this._tokenizeInline(text, links, inLink, inRawBlock, '') });
- continue;
- }
- match = this._autolinkRegExp.exec(source);
- if (match) {
- source = source.substring(match[0].length);
- const text = this._escape(match[1]);
- const href = match[2] === '@' ? 'mailto:' + text : text;
- tokens.push({ type: 'link', text: text, href: href, tokens: [ { type: 'text', raw: text, text } ] });
- continue;
- }
- if (!inLink) {
- match = this._urlRegExp.exec(source);
- if (match) {
- const email = match[2] === '@';
- if (!email) {
- let prevCapZero;
- do {
- prevCapZero = match[0];
- match[0] = this._backpedalRegExp.exec(match[0])[0];
- } while (prevCapZero !== match[0]);
- }
- const text = this._escape(match[0]);
- const href = email ? ('mailto:' + text) : (match[1] === 'www.' ? 'http://' + text : text);
- source = source.substring(match[0].length);
- tokens.push({ type: 'link', text: text, href: href, tokens: [ { type: 'text', text: text } ] });
- continue;
- }
- }
- match = this._textspanRegExp.exec(source);
- if (match) {
- source = source.substring(match[0].length);
- prevChar = match[0].slice(-1);
- tokens.push({ type: 'text' , text: inRawBlock ? match[0] : this._escape(match[0]) });
- continue;
- }
- throw new Error("Unexpected '" + source.charCodeAt(0) + "'.");
- }
- return tokens;
- }
- _tokenizeBlock(tokens, links) {
- for (const token of tokens) {
- switch (token.type) {
- case 'paragraph':
- case 'text':
- case 'heading': {
- token.tokens = this._tokenizeInline(token.text, links, false, false, '');
- break;
- }
- case 'table': {
- token.tokens = {};
- token.tokens.header = token.header.map((header) => this._tokenizeInline(header, links, false, false, ''));
- token.tokens.cells = token.cells.map((cell) => cell.map((row) => this._tokenizeInline(row, links, false, false, '')));
- break;
- }
- case 'blockquote': {
- this._tokenizeBlock(token.tokens, links);
- break;
- }
- case 'list': {
- for (const item of token.items) {
- this._tokenizeBlock(item.tokens, links);
- }
- break;
- }
- default: {
- break;
- }
- }
- }
- }
- _render(tokens, top) {
- let html = '';
- while (tokens.length > 0) {
- const token = tokens.shift();
- switch (token.type) {
- case 'space': {
- continue;
- }
- case 'hr': {
- html += '<hr>\n';
- continue;
- }
- case 'heading': {
- const level = token.depth;
- html += '<h' + level + '">' + this._renderInline(token.tokens) + '</h' + level + '>\n';
- continue;
- }
- case 'code': {
- const code = token.text;
- const language = (token.language || '').match(/\S*/)[0];
- html += '<pre><code' + (language ? ' class="' + 'language-' + this._encode(language) + '"' : '') + '>' + (token.escaped ? code : this._encode(code)) + '</code></pre>\n';
- continue;
- }
- case 'table': {
- let header = '';
- let cell = '';
- for (let j = 0; j < token.header.length; j++) {
- const content = this._renderInline(token.tokens.header[j]);
- const align = token.align[j];
- cell += '<th' + (align ? ' align="' + align + '"' : '') + '>' + content + '</th>\n';
- }
- header += '<tr>\n' + cell + '</tr>\n';
- let body = '';
- for (let j = 0; j < token.cells.length; j++) {
- const row = token.tokens.cells[j];
- cell = '';
- for (let k = 0; k < row.length; k++) {
- const content = this._renderInline(row[k]);
- const align = token.align[k];
- cell += '<td' + (align ? ' align="' + align + '"' : '') + '>' + content + '</td>\n';
- }
- body += '<tr>\n' + cell + '</tr>\n';
- }
- html += '<table>\n<thead>\n' + header + '</thead>\n' + (body ? '<tbody>' + body + '</tbody>' : body) + '</table>\n';
- continue;
- }
- case 'blockquote': {
- html += '<blockquote>\n' + this._render(token.tokens, true) + '</blockquote>\n';
- continue;
- }
- case 'list': {
- const ordered = token.ordered;
- const start = token.start;
- const loose = token.loose;
- let body = '';
- for (const item of token.items) {
- let itemBody = '';
- if (item.task) {
- const checkbox = '<input ' + (item.checked ? 'checked="" ' : '') + 'disabled="" type="checkbox"' + '> ';
- if (loose) {
- if (item.tokens.length > 0 && item.tokens[0].type === 'text') {
- item.tokens[0].text = checkbox + ' ' + item.tokens[0].text;
- if (item.tokens[0].tokens && item.tokens[0].tokens.length > 0 && item.tokens[0].tokens[0].type === 'text') {
- item.tokens[0].tokens[0].text = checkbox + ' ' + item.tokens[0].tokens[0].text;
- }
- } else {
- item.tokens.unshift({ type: 'text', text: checkbox });
- }
- } else {
- itemBody += checkbox;
- }
- }
- itemBody += this._render(item.tokens, loose);
- body += '<li>' + itemBody + '</li>\n';
- }
- const type = (ordered ? 'ol' : 'ul');
- html += '<' + type + (ordered && start !== 1 ? (' start="' + start + '"') : '') + '>\n' + body + '</' + type + '>\n';
- continue;
- }
- case 'html': {
- html += token.text;
- continue;
- }
- case 'paragraph': {
- html += '<p>' + this._renderInline(token.tokens) + '</p>\n';
- continue;
- }
- case 'text': {
- html += top ? '<p>' : '';
- html += token.tokens ? this._renderInline(token.tokens) : token.text;
- while (tokens.length > 0 && tokens[0].type === 'text') {
- const token = tokens.shift();
- html += '\n' + (token.tokens ? this._renderInline(token.tokens) : token.text);
- }
- html += top ? '</p>\n' : '';
- continue;
- }
- default: {
- throw new Error("Unexpected token type '" + token.type + "'.");
- }
- }
- }
- return html;
- }
- _renderInline(tokens) {
- let html = '';
- for (const token of tokens) {
- switch (token.type) {
- case 'escape':
- case 'html':
- case 'text': {
- html += token.text;
- break;
- }
- case 'link': {
- const text = this._renderInline(token.tokens);
- html += '<a href="' + token.href + '"' + (token.title ? ' title="' + token.title + '"' : '') + ' target="_blank">' + text + '</a>';
- break;
- }
- case 'image': {
- html += '<img src="' + token.href + '" alt="' + token.text + '"' + (token.title ? ' title="' + token.title + '"' : '') + '>';
- break;
- }
- case 'strong': {
- const text = this._renderInline(token.tokens);
- html += '<strong>' + text + '</strong>';
- break;
- }
- case 'em': {
- const text = this._renderInline(token.tokens);
- html += '<em>' + text + '</em>';
- break;
- }
- case 'codespan': {
- html += '<code>' + token.text + '</code>';
- break;
- }
- case 'br': {
- html += '<br>';
- break;
- }
- case 'del': {
- const text = this._renderInline(token.tokens);
- html += '<del>' + text + '</del>';
- break;
- }
- default: {
- throw new Error("Unexpected token type '" + token.type + "'.");
- }
- }
- }
- return html;
- }
- _outputLink(match, href, title) {
- title = title ? this._escape(title) : null;
- const text = match[1].replace(/\\([[\]])/g, '$1');
- return match[0].charAt(0) !== '!' ?
- { type: 'link', href: href, title: title, text: text } :
- { type: 'image', href: href, title: title, text: this._escape(text) };
- }
- _splitCells(tableRow, count) {
- const row = tableRow.replace(/\|/g, (match, offset, str) => {
- let escaped = false;
- let position = offset;
- while (--position >= 0 && str[position] === '\\') {
- escaped = !escaped;
- }
- return escaped ? '|' : ' |';
- });
- const cells = row.split(/ \|/);
- if (cells.length > count) {
- cells.splice(count);
- } else {
- while (cells.length < count) {
- cells.push('');
- }
- }
- return cells.map((cell) => cell.trim().replace(/\\\|/g, '|'));
- }
- _encode(content) {
- if (this._escapeTestRegExp.test(content)) {
- return content.replace(this._escapeReplaceRegExp, (ch) => this._escapeReplacementsMap[ch]);
- }
- return content;
- }
- _escape(content) {
- if (this._escapeTestNoEncodeRegExp.test(content)) {
- return content.replace(this._escapeReplaceNoEncodeRegExp, (ch) => this._escapeReplacementsMap[ch]);
- }
- return content;
- }
- };
- view.ModelContext = class {
- constructor(context) {
- this._context = context;
- this._tags = new Map();
- this._content = new Map();
- let stream = context.stream;
- const entries = context.entries;
- if (!stream && entries && entries.size > 0) {
- this._entries = entries;
- this._format = '';
- } else {
- this._entries = new Map();
- const entry = context instanceof view.EntryContext;
- try {
- const archive = zip.Archive.open(stream, 'gzip');
- if (archive) {
- this._entries = archive.entries;
- this._format = 'gzip';
- if (this._entries.size === 1) {
- stream = this._entries.values().next().value;
- }
- }
- } catch (error) {
- if (!entry) {
- throw error;
- }
- }
- try {
- const formats = new Map([ [ 'zip', zip ], [ 'tar', tar ] ]);
- for (const pair of formats) {
- const format = pair[0];
- const module = pair[1];
- const archive = module.Archive.open(stream);
- if (archive) {
- this._entries = archive.entries;
- this._format = format;
- break;
- }
- }
- } catch (error) {
- if (!entry) {
- throw error;
- }
- }
- }
- }
- get identifier() {
- return this._context.identifier;
- }
- get stream() {
- return this._context.stream;
- }
- request(file, encoding, base) {
- return this._context.request(file, encoding, base);
- }
- require(id) {
- return this._context.require(id);
- }
- exception(error, fatal) {
- if (error && this.identifier) {
- error.context = this.identifier;
- }
- this._context.exception(error, fatal);
- }
- entries(format) {
- if (format !== undefined && format !== this._format) {
- return new Map();
- }
- return this._entries;
- }
- open(type) {
- if (!this._content.has(type)) {
- this._content.set(type, undefined);
- const stream = this.stream;
- if (stream) {
- const position = stream.position;
- const signatures = [
- [ 0x80, undefined, 0x8a, 0x0a, 0x6c, 0xfc, 0x9c, 0x46, 0xf9, 0x20, 0x6a, 0xa8, 0x50, 0x19 ] // PyTorch
- ];
- const skip =
- signatures.some((signature) => signature.length <= stream.length && stream.peek(signature.length).every((value, index) => signature[index] === undefined || signature[index] === value)) ||
- Array.from(this._tags).some((entry) => entry[0] !== 'flatbuffers' && entry[1].size > 0) ||
- Array.from(this._content.values()).some((obj) => obj !== undefined);
- if (!skip) {
- switch (type) {
- case 'json': {
- try {
- const buffer = this.stream.peek(Math.min(this.stream.length, 0x1000));
- if ((buffer.length < 8 || String.fromCharCode.apply(null, buffer.slice(0, 8)) !== '\x89HDF\r\n\x1A\n') &&
- (buffer.some((v) => v === 0x22 || v === 0x5b || v === 0x5d || v === 0x7b || v === 0x7d))) {
- const reader = json.TextReader.open(this.stream);
- if (reader) {
- const obj = reader.read();
- this._content.set(type, obj);
- }
- }
- } catch (error) {
- // continue regardless of error
- }
- break;
- }
- case 'json.gz': {
- try {
- const archive = zip.Archive.open(this.stream, 'gzip');
- if (archive && archive.entries.size === 1) {
- const stream = archive.entries.values().next().value;
- const reader = json.TextReader.open(stream);
- if (reader) {
- const obj = reader.read();
- this._content.set(type, obj);
- }
- }
- } catch (error) {
- // continue regardless of error
- }
- break;
- }
- case 'pkl': {
- let unpickler = null;
- try {
- const archive = zip.Archive.open(stream, 'zlib');
- const data = archive ? archive.entries.get('') : stream;
- let condition = false;
- if (data.length > 2) {
- const head = data.peek(2);
- condition = head[0] === 0x80 && head[1] < 7;
- if (!condition) {
- data.seek(-1);
- const tail = data.peek(1);
- data.seek(0);
- condition = tail[0] === 0x2e;
- }
- }
- if (condition) {
- const signature = [ 0x80, undefined, 0x63, 0x5F, 0x5F, 0x74, 0x6F, 0x72, 0x63, 0x68, 0x5F, 0x5F, 0x2E]; // __torch__.
- const torch = signature.length <= data.length && data.peek(signature.length).every((value, index) => signature[index] === undefined || signature[index] === value);
- const execution = new python.Execution();
- execution.on('resolve', (_, name) => {
- if (!torch || !name.startsWith('__torch__.')) {
- this.exception(new view.Error("Unknown type name '" + name + "'."));
- }
- });
- const pickle = execution.__import__('pickle');
- unpickler = new pickle.Unpickler(data);
- }
- } catch (error) {
- // continue regardless of error
- }
- if (unpickler) {
- unpickler.persistent_load = (saved_id) => saved_id;
- const obj = unpickler.load();
- this._content.set(type, obj);
- }
- break;
- }
- case 'hdf5': {
- const file = hdf5.File.open(stream);
- if (file && file.rootGroup && file.rootGroup.attributes) {
- this._content.set(type, file.rootGroup);
- }
- break;
- }
- default: {
- throw new view.Error("Unsupported open format type '" + type + "'.");
- }
- }
- }
- if (stream.position !== position) {
- stream.seek(0);
- }
- }
- }
- return this._content.get(type);
- }
- tags(type) {
- if (!this._tags.has(type)) {
- let tags = new Map();
- const stream = this.stream;
- if (stream) {
- const position = stream.position;
- const signatures = [
- [ 0x89, 0x48, 0x44, 0x46, 0x0D, 0x0A, 0x1A, 0x0A ], // HDF5
- [ 0x80, undefined, 0x8a, 0x0a, 0x6c, 0xfc, 0x9c, 0x46, 0xf9, 0x20, 0x6a, 0xa8, 0x50, 0x19 ], // PyTorch
- [ 0x50, 0x4b ], // Zip
- [ 0x1f, 0x8b ] // Gzip
- ];
- const skip =
- signatures.some((signature) => signature.length <= stream.length && stream.peek(signature.length).every((value, index) => signature[index] === undefined || signature[index] === value)) ||
- (Array.from(this._tags).some((pair) => pair[0] !== 'flatbuffers' && pair[1].size > 0) && type !== 'pb+') ||
- Array.from(this._content.values()).some((obj) => obj !== undefined);
- if (!skip) {
- try {
- switch (type) {
- case 'pbtxt': {
- const reader = protobuf.TextReader.open(stream);
- tags = reader ? reader.signature() : tags;
- break;
- }
- case 'pb': {
- const reader = protobuf.BinaryReader.open(stream);
- tags = reader.signature();
- break;
- }
- case 'pb+': {
- const reader = protobuf.BinaryReader.open(stream);
- tags = reader.decode();
- break;
- }
- case 'flatbuffers': {
- if (stream.length >= 8) {
- const buffer = stream.peek(Math.min(32, stream.length));
- const reader = flatbuffers.BinaryReader.open(buffer);
- const identifier = reader.identifier;
- if (identifier.length > 0) {
- tags.set('file_identifier', identifier);
- }
- }
- break;
- }
- case 'xml': {
- const reader = xml.TextReader.open(stream);
- if (reader) {
- const document = reader.peek();
- const element = document.documentElement;
- const namespaceURI = element.namespaceURI;
- const localName = element.localName;
- const name = namespaceURI ? namespaceURI + ':' + localName : localName;
- tags.set(name, element);
- }
- break;
- }
- default: {
- throw new view.Error("Unsupported tags format type '" + type + "'.");
- }
- }
- } catch (error) {
- tags.clear();
- }
- }
- if (stream.position !== position) {
- stream.seek(position);
- }
- }
- this._tags.set(type, tags);
- }
- return this._tags.get(type);
- }
- metadata(name) {
- return view.Metadata.open(this, name);
- }
- };
- view.EntryContext = class {
- constructor(host, entries, rootFolder, identifier, stream) {
- this._host = host;
- this._entries = new Map();
- if (entries) {
- for (const entry of entries) {
- if (entry[0].startsWith(rootFolder)) {
- const name = entry[0].substring(rootFolder.length);
- this._entries.set(name, entry[1]);
- }
- }
- }
- this._identifier = identifier.substring(rootFolder.length);
- this._stream = stream;
- }
- get identifier() {
- return this._identifier;
- }
- get stream() {
- return this._stream;
- }
- async request(file, encoding, base) {
- if (base === undefined) {
- const stream = this._entries.get(file);
- if (!stream) {
- throw new view.Error('File not found.');
- }
- if (encoding) {
- const decoder = new TextDecoder(encoding);
- const buffer = stream.peek();
- const value = decoder.decode(buffer);
- return value;
- }
- return stream;
- }
- return this._host.request(file, encoding, base);
- }
- require(id) {
- return this._host.require(id);
- }
- exception(error, fatal) {
- this._host.exception(error, fatal);
- }
- };
- view.ArchiveError = class extends Error {
- constructor(message) {
- super(message);
- this.name = 'Error loading archive.';
- }
- };
- view.ModelFactoryService = class {
- constructor(host) {
- this._host = host;
- this._extensions = new Set([ '.zip', '.tar', '.tar.gz', '.tgz', '.gz' ]);
- this._factories = [];
- this.register('./server', [ '.netron']);
- this.register('./pytorch', [ '.pt', '.pth', '.ptl', '.pt1', '.pyt', '.pyth', '.pkl', '.pickle', '.h5', '.t7', '.model', '.dms', '.tar', '.ckpt', '.chkpt', '.tckpt', '.bin', '.pb', '.zip', '.nn', '.torchmodel', '.torchscript', '.pytorch', '.ot', '.params', '.trt', '.ff', '.ptmf' ], [ '.model' ]);
- this.register('./onnx', [ '.onnx', '.onn', '.pb', '.onnxtxt', '.pbtxt', '.prototxt', '.txt', '.model', '.pt', '.pth', '.pkl', '.ort', '.ort.onnx', 'onnxmodel', 'ngf' ]);
- this.register('./mxnet', [ '.json', '.params' ], [ '.mar']);
- this.register('./coreml', [ '.mlmodel', '.bin', 'manifest.json', 'metadata.json', 'featuredescriptions.json', '.pb' ], [ '.mlpackage' ]);
- this.register('./caffe', [ '.caffemodel', '.pbtxt', '.prototxt', '.pt', '.txt' ]);
- this.register('./caffe2', [ '.pb', '.pbtxt', '.prototxt' ]);
- this.register('./torch', [ '.t7', '.net' ]);
- this.register('./tflite', [ '.tflite', '.lite', '.tfl', '.bin', '.pb', '.tmfile', '.h5', '.model', '.json', '.txt' ]);
- this.register('./circle', [ '.circle' ]);
- this.register('./tf', [ '.pb', '.meta', '.pbtxt', '.prototxt', '.txt', '.pt', '.json', '.index', '.ckpt', '.graphdef', '.pbmm', /.data-[0-9][0-9][0-9][0-9][0-9]-of-[0-9][0-9][0-9][0-9][0-9]$/, /^events.out.tfevents./ ], [ '.zip' ]);
- this.register('./mediapipe', [ '.pbtxt' ]);
- this.register('./uff', [ '.uff', '.pb', '.pbtxt', '.uff.txt', '.trt', '.engine' ]);
- this.register('./tensorrt', [ '.trt', '.trtmodel', '.engine', '.model', '.txt', '.uff', '.pb', '.tmfile', '.onnx', '.pth', '.dnn', '.plan' ]);
- this.register('./numpy', [ '.npz', '.npy', '.pkl', '.pickle', '.model', '.model2', '.mge' ]);
- this.register('./lasagne', [ '.pkl', '.pickle', '.joblib', '.model', '.pkl.z', '.joblib.z' ]);
- this.register('./lightgbm', [ '.txt', '.pkl', '.model' ]);
- this.register('./keras', [ '.h5', '.hd5', '.hdf5', '.keras', '.json', '.cfg', '.model', '.pb', '.pth', '.weights', '.pkl', '.lite', '.tflite', '.ckpt' ], [ '.zip' ]);
- this.register('./sklearn', [ '.pkl', '.pickle', '.joblib', '.model', '.meta', '.pb', '.pt', '.h5', '.pkl.z', '.joblib.z' ]);
- this.register('./megengine', [ '.tm', '.mge' ]);
- this.register('./pickle', [ '.pkl', '.pickle', '.joblib', '.model', '.meta', '.pb', '.pt', '.h5', '.pkl.z', '.joblib.z', '.pdstates', '.mge' ]);
- this.register('./cntk', [ '.model', '.cntk', '.cmf', '.dnn' ]);
- this.register('./paddle', [ '.pdmodel', '.pdiparams', '.pdparams', '.pdopt', '.paddle', '__model__', '.__model__', '.pbtxt', '.txt', '.tar', '.tar.gz', '.nb' ]);
- this.register('./bigdl', [ '.model', '.bigdl' ]);
- this.register('./darknet', [ '.cfg', '.model', '.txt', '.weights' ]);
- this.register('./weka', [ '.model' ]);
- this.register('./rknn', [ '.rknn', '.nb', '.onnx' ]);
- this.register('./dlc', [ '.dlc', 'model', '.params' ]);
- this.register('./armnn', [ '.armnn', '.json' ]);
- this.register('./mnn', ['.mnn']);
- this.register('./ncnn', [ '.param', '.bin', '.cfg.ncnn', '.weights.ncnn', '.ncnnmodel' ]);
- this.register('./tnn', [ '.tnnproto', '.tnnmodel' ]);
- this.register('./tengine', ['.tmfile']);
- this.register('./mslite', [ '.ms']);
- this.register('./barracuda', [ '.nn' ]);
- this.register('./dnn', [ '.dnn' ]);
- this.register('./xmodel', [ '.xmodel' ]);
- this.register('./kmodel', [ '.kmodel' ]);
- this.register('./flux', [ '.bson' ]);
- this.register('./dl4j', [ '.json', '.bin' ]);
- this.register('./openvino', [ '.xml', '.bin' ]);
- this.register('./mlnet', [ '.zip', '.mlnet' ]);
- this.register('./acuity', [ '.json' ]);
- this.register('./imgdnn', [ '.dnn', 'params', '.json' ]);
- this.register('./flax', [ '.msgpack' ]);
- this.register('./om', [ '.om', '.onnx', '.pb', '.engine' ]);
- this.register('./nnabla', [ '.nntxt' ], [ '.nnp' ]);
- this.register('./hickle', [ '.h5', '.hkl' ]);
- this.register('./nnef', [ '.nnef', '.dat' ]);
- this.register('./cambricon', [ '.cambricon' ]);
- this.register('./onednn', [ '.json']);
- this.register('./mlir', [ '.mlir']);
- this.register('./hailo', [ '.hn', '.har' ]);
- this.register('./safetensors', [ '.safetensors' ]);
- }
- register(id, factories, containers) {
- for (const extension of factories) {
- this._factories.push({ extension: extension, id: id });
- this._extensions.add(extension);
- }
- for (const extension of containers || []) {
- this._extensions.add(extension);
- }
- }
- async open(context) {
- try {
- await this._openSignature(context);
- const modelContext = new view.ModelContext(context);
- const model = await this._openContext(modelContext);
- if (!model) {
- const entries = modelContext.entries();
- if (!entries || entries.size === 0) {
- this._unsupported(modelContext);
- }
- const context = await this._openEntries(entries);
- if (!context) {
- this._unsupported(modelContext);
- }
- return this._openContext(context);
- }
- return model;
- } catch (error) {
- if (error && context.identifier) {
- error.context = context.identifier;
- }
- throw error;
- }
- }
- _unsupported(context) {
- const identifier = context.identifier;
- const extension = identifier.split('.').pop().toLowerCase();
- const stream = context.stream;
- const callbacks = [
- (stream) => zip.Archive.open(stream, 'zip'),
- (stream) => tar.Archive.open(stream),
- (stream) => zip.Archive.open(stream, 'gzip')
- ];
- for (const callback of callbacks) {
- let archive = null;
- try {
- archive = callback(stream);
- } catch (error) {
- // continue regardless of error
- }
- if (archive) {
- throw new view.Error("Archive contains no model files.");
- }
- }
- const json = () => {
- const obj = context.open('json');
- if (obj) {
- const formats = [
- { name: 'Netron metadata', tags: [ '[].name', '[].schema' ] },
- { name: 'Netron metadata', tags: [ '[].name', '[].attributes' ] },
- { name: 'Netron metadata', tags: [ '[].name', '[].category' ] },
- { name: 'Netron test data', tags: [ '[].type', '[].target', '[].source', '[].format', '[].link' ] },
- { name: 'Darkflow metadata', tags: [ 'net', 'type', 'model' ] },
- { name: 'keras-yolo2 configuration', tags: [ 'model', 'train', 'valid' ] },
- { name: 'Vulkan SwiftShader ICD manifest', tags: [ 'file_format_version', 'ICD' ] },
- { name: 'DeepLearningExamples configuration', tags: [ 'attention_probs_dropout_prob', 'hidden_act', 'hidden_dropout_prob', 'hidden_size', ] },
- { name: 'NuGet assets', tags: [ 'version', 'targets', 'packageFolders' ] },
- { name: 'NuGet data', tags: [ 'format', 'restore', 'projects' ] },
- { name: 'NPM package', tags: [ 'name', 'version', 'dependencies' ] },
- { name: 'NetworkX adjacency_data', tags: [ 'directed', 'graph', 'nodes' ] },
- { name: 'Waifu2x data', tags: [ 'name', 'arch_name', 'channels' ] },
- { name: 'Waifu2x data', tags: [ '[].nInputPlane', '[].nOutputPlane', '[].weight', '[].bias' ] },
- { name: 'Brain.js data', tags: [ 'type', 'sizes', 'layers' ] },
- { name: 'Custom Vision metadata', tags: [ 'CustomVision.Metadata.Version' ] },
- { name: 'W&B metadata', tags: [ 'program', 'host', 'executable' ] }
- ];
- const match = (obj, tag) => {
- if (tag.startsWith('[].')) {
- tag = tag.substring(3);
- return (Array.isArray(obj) && obj.some((item) => Object.prototype.hasOwnProperty.call(item, tag)));
- }
- return Object.prototype.hasOwnProperty.call(obj, tag);
- };
- for (const format of formats) {
- if (format.tags.every((tag) => match(obj, tag))) {
- throw new view.Error('Invalid file content. File contains ' + format.name + '.');
- }
- }
- const content = JSON.stringify(obj).substring(0, 100).replace(/\s/, '').substring(0, 48) + '...';
- throw new view.Error("Unsupported JSON content '" + (content.length > 64 ? content.substring(0, 100) + '...' : content) + "' for extension '." + extension + "'.");
- }
- };
- const pbtxt = () => {
- const formats = [
- { name: 'ImageNet LabelMap data', tags: [ 'entry', 'entry.target_class' ] },
- { name: 'StringIntLabelMapProto data', tags: [ 'item', 'item.id', 'item.name' ] },
- { name: 'caffe.LabelMap data', tags: [ 'item', 'item.name', 'item.label' ] },
- { name: 'Triton Inference Server configuration', tags: [ 'name', 'platform', 'input', 'output' ] },
- { name: 'TensorFlow OpList data', tags: [ 'op', 'op.name', 'op.input_arg' ] },
- { name: 'vitis.ai.proto.DpuModelParamList data', tags: [ 'model', 'model.name', 'model.kernel' ] },
- { name: 'object_detection.protos.DetectionModel data', tags: [ 'model', 'model.ssd' ] },
- { name: 'object_detection.protos.DetectionModel data', tags: [ 'model', 'model.faster_rcnn' ] },
- { name: 'tensorflow.CheckpointState data', tags: [ 'model_checkpoint_path', 'all_model_checkpoint_paths' ] },
- { name: 'apollo.perception.camera.traffic_light.detection.DetectionParam data', tags: [ 'min_crop_size', 'crop_method' ] },
- { name: 'tidl_meta_arch.TIDLMetaArch data', tags: [ 'caffe_ssd' ] }, // https://github.com/TexasInstruments/edgeai-mmdetection/blob/master/mmdet/utils/proto/mmdet_meta_arch.proto
- { name: 'tidl_meta_arch.TIDLMetaArch data', tags: [ 'tf_od_api_ssd' ] },
- { name: 'tidl_meta_arch.TIDLMetaArch data', tags: [ 'tidl_ssd' ] },
- { name: 'tidl_meta_arch.TIDLMetaArch data', tags: [ 'tidl_faster_rcnn' ] },
- { name: 'tidl_meta_arch.TIDLMetaArch data', tags: [ 'tidl_yolo' ] },
- { name: 'tidl_meta_arch.TIDLMetaArch data', tags: [ 'tidl_retinanet' ] },
- { name: 'domi.InsertNewOps data', tags: [ 'aipp_op' ] } // https://github.com/Ascend/parser/blob/development/parser/proto/insert_op.proto
- ];
- const tags = context.tags('pbtxt');
- if (tags.size > 0) {
- for (const format of formats) {
- if (format.tags.every((tag) => tags.has(tag))) {
- const error = new view.Error('Invalid file content. File contains ' + format.name + '.');
- error.context = context.identifier;
- throw error;
- }
- }
- const entries = [];
- entries.push(...Array.from(tags).filter((pair) => pair[0].toString().indexOf('.') === -1));
- entries.push(...Array.from(tags).filter((pair) => pair[0].toString().indexOf('.') !== -1));
- const content = entries.map((pair) => pair[1] === true ? pair[0] : pair[0] + ':' + JSON.stringify(pair[1])).join(',');
- throw new view.Error("Unsupported Protocol Buffers text content '" + (content.length > 64 ? content.substring(0, 100) + '...' : content) + "' for extension '." + extension + "'.");
- }
- };
- const pb = () => {
- const tags = context.tags('pb+');
- if (Object.keys(tags).length > 0) {
- const formats = [
- { name: 'sentencepiece.ModelProto data', tags: [[1,[[1,2],[2,5],[3,0]]],[2,[[1,2],[2,2],[3,0],[4,0],[5,2],[6,0],[7,2],[10,5],[16,0],[40,0],[41,0],[42,0],[43,0]]],[3,[]],[4,[]],[5,[]]] },
- { name: 'mediapipe.BoxDetectorIndex data', tags: [[1,[[1,[[1,[[1,5],[2,5],[3,5],[4,5],[6,0],[7,5],[8,5],[10,5],[11,0],[12,0]]],[2,5],[3,[]]]],[2,false],[3,false],[4,false],[5,false]]],[2,false],[3,false]] },
- { name: 'third_party.tensorflow.python.keras.protobuf.SavedMetadata data', tags: [[1,[[1,[[1,0],[2,0]]],[2,0],[3,2],[4,2],[5,2]]]] },
- { name: 'pblczero.Net data', tags: [[1,5],[2,2],[3,[[1,0],[2,0],[3,0]],[10,[[1,[]],[2,[]],[3,[]],[4,[]],[5,[]],[6,[]]]],[11,[]]]] } // https://github.com/LeelaChessZero/lczero-common/blob/master/proto/net.proto
- ];
- const match = (tags, schema) => {
- for (const pair of schema) {
- const key = pair[0];
- const inner = pair[1];
- const value = tags[key];
- if (value === undefined) {
- continue;
- }
- if (inner === false) {
- return false;
- }
- if (Array.isArray(inner)) {
- if (typeof value !== 'object' || !match(value, inner)) {
- return false;
- }
- } else if (inner !== value) {
- if (inner === 2 && !Array.isArray(value) && Object(value) === (value) && Object.keys(value).length === 0) {
- return true;
- }
- return false;
- }
- }
- return true;
- };
- const tags = context.tags('pb+');
- for (const format of formats) {
- if (match(tags, format.tags)) {
- const error = new view.Error('Invalid file content. File contains ' + format.name + '.');
- error.context = context.identifier;
- throw error;
- }
- }
- const format = (tags) => {
- const content = Object.entries(tags).map((pair) => {
- const key = pair[0];
- const value = pair[1];
- return key.toString() + ':' + (Object(value) === value ? '{' + format(value) + '}' : value.toString());
- });
- return content.join(',');
- };
- const content = format(tags);
- throw new view.Error("Unsupported Protocol Buffers content '" + (content.length > 64 ? content.substring(0, 100) + '...' : content) + "' for extension '." + extension + "'.");
- }
- };
- const flatbuffers = () => {
- const tags = context.tags('flatbuffers');
- if (tags.has('file_identifier')) {
- const file_identifier = tags.get('file_identifier');
- const formats = [
- { name: 'onnxruntime.experimental.fbs.InferenceSession data', identifier: 'ORTM' },
- { name: 'tflite.Model data', identifier: 'TFL3' },
- { name: 'FlatBuffers ENNC data', identifier: 'ENNC' },
- ];
- for (const format of formats) {
- if (file_identifier === format.identifier) {
- throw new view.Error('Invalid file content. File contains ' + format.name + '.');
- }
- }
- }
- };
- const xml = () => {
- const tags = context.tags('xml');
- if (tags.size > 0) {
- const formats = [
- { name: 'OpenCV storage data', tags: [ 'opencv_storage' ] },
- { name: 'XHTML markup', tags: [ 'http://www.w3.org/1999/xhtml:html' ] }
- ];
- for (const format of formats) {
- if (format.tags.some((tag) => tags.has(tag))) {
- const error = new view.Error('Invalid file content. File contains ' + format.name + '.');
- error.content = context.identifier;
- throw error;
- }
- }
- throw new view.Error("Unsupported XML content '" + tags.keys().next().value + "'.");
- }
- };
- const unknown = () => {
- if (stream) {
- stream.seek(0);
- const buffer = stream.peek(Math.min(16, stream.length));
- const bytes = Array.from(buffer).map((c) => (c < 16 ? '0' : '') + c.toString(16)).join('');
- const content = stream.length > 268435456 ? '(' + bytes + ') [' + stream.length.toString() + ']': '(' + bytes + ')';
- throw new view.Error("Unsupported file content " + content + " for extension '." + extension + "'.");
- }
- throw new view.Error("Unsupported file directory.");
- };
- json();
- pbtxt();
- pb();
- flatbuffers();
- xml();
- unknown();
- }
- async _openContext(context) {
- const modules = this._filter(context).filter((module) => module && module.length > 0);
- const errors = [];
- let success = false;
- const next = async () => {
- if (modules.length > 0) {
- try {
- const id = modules.shift();
- const module = await this._host.require(id);
- if (!module.ModelFactory) {
- throw new view.Error("Failed to load module '" + id + "'.");
- }
- const modelFactory = new module.ModelFactory();
- const target = modelFactory.match(context);
- if (target) {
- success = true;
- const model = await modelFactory.open(context, target);
- if (!model.identifier) {
- model.identifier = context.identifier;
- }
- return model;
- }
- } catch (error) {
- if (context.stream && context.stream.position !== 0) {
- context.stream.seek(0);
- }
- errors.push(error);
- }
- return await next();
- }
- if (success) {
- if (errors.length === 1) {
- throw errors[0];
- }
- throw new view.Error(errors.map((err) => err.message).join('\n'));
- }
- return null;
- };
- return await next();
- }
- async _openEntries(entries) {
- try {
- const rootFolder = (files) => {
- const map = files.map((file) => file.split('/').slice(0, -1));
- const at = index => list => list[index];
- const rotate = list => list.length === 0 ? [] : list[0].map((item, index) => list.map(at(index)));
- const equals = list => list.every((item) => item === list[0]);
- const folder = rotate(map).filter(equals).map(at(0)).join('/');
- return folder.length === 0 ? folder : folder + '/';
- };
- const filter = async (queue) => {
- let matches = [];
- const nextEntry = async () => {
- if (queue.length > 0) {
- const entry = queue.shift();
- const context = new view.ModelContext(new view.EntryContext(this._host, entries, folder, entry.name, entry.stream));
- const modules = this._filter(context);
- const nextModule = async () => {
- if (modules.length > 0) {
- const id = modules.shift();
- const module = await this._host.require(id);
- if (!module.ModelFactory) {
- throw new view.ArchiveError("Failed to load module '" + id + "'.", null);
- }
- const modelFactory = new module.ModelFactory();
- if (modelFactory.match(context)) {
- matches.push(context);
- modules.splice(0, modules.length);
- }
- return await nextModule();
- }
- return await nextEntry();
- };
- return await nextModule();
- }
- if (matches.length === 0) {
- return null;
- }
- // MXNet
- if (matches.length === 2 &&
- matches.some((context) => context.identifier.toLowerCase().endsWith('.params')) &&
- matches.some((context) => context.identifier.toLowerCase().endsWith('-symbol.json'))) {
- matches = matches.filter((context) => context.identifier.toLowerCase().endsWith('.params'));
- }
- // TensorFlow.js
- if (matches.length > 0 &&
- matches.some((context) => context.identifier.toLowerCase().endsWith('.bin')) &&
- matches.some((context) => context.identifier.toLowerCase().endsWith('.json'))) {
- matches = matches.filter((context) => context.identifier.toLowerCase().endsWith('.json'));
- }
- // ncnn
- if (matches.length > 0 &&
- matches.some((context) => context.identifier.toLowerCase().endsWith('.bin')) &&
- matches.some((context) => context.identifier.toLowerCase().endsWith('.param'))) {
- matches = matches.filter((context) => context.identifier.toLowerCase().endsWith('.param'));
- }
- // ncnn
- if (matches.length > 0 &&
- matches.some((context) => context.identifier.toLowerCase().endsWith('.bin')) &&
- matches.some((context) => context.identifier.toLowerCase().endsWith('.param.bin'))) {
- matches = matches.filter((context) => context.identifier.toLowerCase().endsWith('.param.bin'));
- }
- // NNEF
- if (matches.length > 0 &&
- matches.some((context) => context.identifier.toLowerCase().endsWith('.nnef')) &&
- matches.some((context) => context.identifier.toLowerCase().endsWith('.dat'))) {
- matches = matches.filter((context) => context.identifier.toLowerCase().endsWith('.nnef'));
- }
- // Paddle
- if (matches.length > 0 &&
- matches.some((context) => context.identifier.toLowerCase().endsWith('.pdmodel')) &&
- (matches.some((context) => context.identifier.toLowerCase().endsWith('.pdparams')) ||
- matches.some((context) => context.identifier.toLowerCase().endsWith('.pdopt')) ||
- matches.some((context) => context.identifier.toLowerCase().endsWith('.pdiparams')))) {
- matches = matches.filter((context) => context.identifier.toLowerCase().endsWith('.pdmodel'));
- }
- // Paddle Lite
- if (matches.length > 0 &&
- matches.some((context) => context.identifier.toLowerCase().split('/').pop() === '__model__.nb') &&
- matches.some((context) => context.identifier.toLowerCase().split('/').pop() === 'param.nb')) {
- matches = matches.filter((context) => context.identifier.toLowerCase().split('/').pop() == '__model__.nb');
- }
- // TensorFlow Bundle
- if (matches.length > 1 &&
- matches.some((context) => context.identifier.toLowerCase().endsWith('.data-00000-of-00001'))) {
- matches = matches.filter((context) => !context.identifier.toLowerCase().endsWith('.data-00000-of-00001'));
- }
- // TensorFlow SavedModel
- if (matches.length === 2 &&
- matches.some((context) => context.identifier.toLowerCase().split('/').pop() === 'keras_metadata.pb')) {
- matches = matches.filter((context) => context.identifier.toLowerCase().split('/').pop() !== 'keras_metadata.pb');
- }
- if (matches.length > 1) {
- throw new view.ArchiveError('Archive contains multiple model files.');
- }
- const match = matches.shift();
- return match;
- };
- return await nextEntry();
- };
- const list = Array.from(entries).map((entry) => {
- return { name: entry[0], stream: entry[1] };
- });
- const files = list.filter((entry) => {
- if (entry.name.endsWith('/')) {
- return false;
- }
- if (entry.name.split('/').pop().startsWith('.')) {
- return false;
- }
- if (!entry.name.startsWith('./') && entry.name.startsWith('.')) {
- return false;
- }
- return true;
- });
- const folder = rootFolder(files.map((entry) => entry.name));
- const queue = files.slice(0).filter((entry) => entry.name.substring(folder.length).indexOf('/') < 0);
- const context = await filter(queue);
- if (!context) {
- const queue = files.slice(0).filter((entry) => entry.name.substring(folder.length).indexOf('/') >= 0);
- return await filter(queue);
- }
- return context;
- } catch (error) {
- throw new view.ArchiveError(error.message);
- }
- }
- accept(identifier, size) {
- const extension = identifier.indexOf('.') === -1 ? '' : identifier.split('.').pop().toLowerCase();
- identifier = identifier.toLowerCase().split('/').pop();
- let accept = false;
- for (const extension of this._extensions) {
- if ((typeof extension === 'string' && identifier.endsWith(extension)) || (extension instanceof RegExp && extension.exec(identifier))) {
- accept = true;
- break;
- }
- }
- this._host.event('model_file', {
- file_extension: extension,
- file_size: size || 0,
- file_accept: accept ? 1 : 0
- });
- return accept;
- }
- _filter(context) {
- const identifier = context.identifier.toLowerCase().split('/').pop();
- const list = this._factories.filter((entry) =>
- (typeof entry.extension === 'string' && identifier.endsWith(entry.extension)) ||
- (entry.extension instanceof RegExp && entry.extension.exec(identifier)));
- return Array.from(new Set(list.map((entry) => entry.id)));
- }
- async _openSignature(context) {
- const stream = context.stream;
- if (stream) {
- let empty = true;
- let position = 0;
- while (position < stream.length) {
- const buffer = stream.read(Math.min(4096, stream.length - position));
- position += buffer.length;
- if (!buffer.every((value) => value === 0x00)) {
- empty = false;
- break;
- }
- }
- stream.seek(0);
- if (empty) {
- throw new view.Error('File has no content.');
- }
- /* eslint-disable no-control-regex */
- const entries = [
- { name: 'ELF executable', value: /^\x7FELF/ },
- { name: 'PNG image', value: /^\x89PNG/ },
- { name: 'Git LFS header', value: /^version https:\/\/git-lfs.github.com/ },
- { name: 'Git LFS header', value: /^\s*oid sha256:/ },
- { name: 'HTML markup', value: /^\s*<html>/ },
- { name: 'HTML markup', value: /^\s*<!doctype\s*html>/ },
- { name: 'HTML markup', value: /^\s*<!DOCTYPE\s*html>/ },
- { name: 'HTML markup', value: /^\s*<!DOCTYPE\s*HTML>/ },
- { name: 'HTML markup', value: /^\s*<!DOCTYPE\s*HTML\s+(PUBLIC|SYSTEM)?/ },
- { name: 'Unity metadata', value: /^fileFormatVersion:/ },
- { name: 'Python source code', value: /^\s*('''.*''')?\s*import[ ]+[a-zA-Z_]\w*(\.[a-zA-Z_]\w*)*([ ]+as[ ]+[a-zA-Z]\w*)?[ ]*(,|;|\n|\r\n)/ },
- { name: 'Python source code', value: /^\s*('''.*''')?\s*from[ ]+([a-zA-Z_]\w*(\.[a-zA-Z_]\w*)*)[ ]+import[ ]+[a-zA-Z]\w*[ ]+/ },
- { name: 'Bash script', value: /^#!\/usr\/bin\/env\s/ },
- { name: 'Bash script', value: /^#!\/bin\/bash\s/ },
- { name: 'TSD header', value: /^%TSD-Header-###%/ },
- { name: 'AppleDouble data', value: /^\x00\x05\x16\x07/ },
- { name: 'TensorFlow Hub module', value: /^\x08\x03$/, identifier: 'tfhub_module.pb' },
- { name: 'ViSQOL model', value: /^svm_type\s/ },
- { name: 'SenseTime model', value: /^STEF/ }
- ];
- /* eslint-enable no-control-regex */
- const buffer = stream.peek(Math.min(4096, stream.length));
- const content = String.fromCharCode.apply(null, buffer);
- for (const entry of entries) {
- if (content.match(entry.value) && (!entry.identifier || entry.identifier === context.identifier)) {
- throw new view.Error('Invalid file content. File contains ' + entry.name + '.');
- }
- }
- }
- }
- };
- view.Metadata = class {
- static async open(context, name) {
- view.Metadata._metadata = view.Metadata._metadata || new Map();
- if (view.Metadata._metadata.has(name)) {
- return view.Metadata._metadata.get(name);
- }
- try {
- const json = await context.request(name, 'utf-8', null);
- const data = JSON.parse(json);
- const library = new view.Metadata(data);
- view.Metadata._metadata.set(name, library);
- return library;
- } catch (error) {
- const library = new view.Metadata(null);
- view.Metadata._metadata.set(name, library);
- return library;
- }
- }
- constructor(data) {
- this._types = new Map();
- this._attributes = new Map();
- this._inputs = new Map();
- for (const entry of data || []) {
- this._types.set(entry.name, entry);
- if (entry.identifier !== undefined) {
- this._types.set(entry.identifier, entry);
- }
- }
- }
- type(name) {
- if (!this._types.has(name)) {
- this._types.set(name, { name: name.toString() });
- }
- return this._types.get(name);
- }
- attribute(type, name) {
- const key = type + ':' + name;
- if (!this._attributes.has(key)) {
- this._attributes.set(key, null);
- const metadata = this.type(type);
- if (metadata && Array.isArray(metadata.attributes)) {
- for (const attribute of metadata.attributes) {
- this._attributes.set(type + ':' + attribute.name, attribute);
- }
- }
- }
- return this._attributes.get(key);
- }
- input(type, name) {
- const key = type + ':' + name;
- if (!this._inputs.has(key)) {
- this._inputs.set(key, null);
- const metadata = this.type(type);
- if (metadata && Array.isArray(metadata.inputs)) {
- for (const input of metadata.inputs) {
- this._inputs.set(type + ':' + input.name, input);
- }
- }
- }
- return this._inputs.get(key);
- }
- };
- view.Error = class extends Error {
- constructor(message) {
- super(message);
- this.name = 'Error loading model.';
- }
- };
- if (typeof module !== 'undefined' && typeof module.exports === 'object') {
- module.exports.View = view.View;
- module.exports.ModelFactoryService = view.ModelFactoryService;
- module.exports.Documentation = view.Documentation;
- module.exports.Formatter = view.Formatter;
- module.exports.Tensor = view.Tensor;
- }
|