view.js 105 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473
  1. /* jshint esversion: 6 */
  2. var view = view || {};
  3. var base = base || require('./base');
  4. var zip = zip || require('./zip');
  5. var gzip = gzip || require('./gzip');
  6. var tar = tar || require('./tar');
  7. var json = json || require('./json');
  8. var protobuf = protobuf || require('./protobuf');
  9. var flatbuffers = flatbuffers || require('./flatbuffers');
  10. var python = python || require('./python');
  11. var sidebar = sidebar || require('./view-sidebar');
  12. var grapher = grapher || require('./view-grapher');
  13. view.View = class {
  14. constructor(host, id) {
  15. this._host = host;
  16. this._id = id ? ('-' + id) : '';
  17. this._host.initialize(this).then(() => {
  18. this._model = null;
  19. this._graphs = [];
  20. this._selection = [];
  21. this._sidebar = new sidebar.Sidebar(this._host, id);
  22. this._showAttributes = false;
  23. this._showInitializers = true;
  24. this._showNames = false;
  25. this._showHorizontal = false;
  26. this._searchText = '';
  27. this._modelFactoryService = new view.ModelFactoryService(this._host);
  28. this._getElementById('zoom-in-button').addEventListener('click', () => {
  29. this.zoomIn();
  30. });
  31. this._getElementById('zoom-out-button').addEventListener('click', () => {
  32. this.zoomOut();
  33. });
  34. this._getElementById('sidebar').addEventListener('mousewheel', (e) => {
  35. this._preventZoom(e);
  36. }, { passive: true });
  37. this._host.document.addEventListener('keydown', () => {
  38. this.clearSelection();
  39. });
  40. this._host.start();
  41. switch (this._host.environment('zoom')) {
  42. case 'scroll': {
  43. const userAgent = navigator.userAgent.toLowerCase();
  44. const safari = userAgent.indexOf('safari') !== -1 && userAgent.indexOf('chrome') === -1;
  45. const elements = [ 'graph', 'toolbar' ];
  46. for (const id of elements) {
  47. const element = this._getElementById(id);
  48. element.addEventListener('mousewheel', (e) => {
  49. this._mouseWheelHandler(e);
  50. });
  51. element.addEventListener('scroll', (e) => {
  52. this._scrollHandler(e);
  53. });
  54. element.addEventListener('wheel', (e) => {
  55. this._mouseWheelHandler(e);
  56. });
  57. if (safari) {
  58. element.addEventListener('gesturestart', (e) => {
  59. e.preventDefault();
  60. this._gestureZoom = this._zoom;
  61. }, false);
  62. element.addEventListener('gesturechange', (e) => {
  63. e.preventDefault();
  64. this._updateZoom(this._gestureZoom * e.scale, e);
  65. }, false);
  66. element.addEventListener('gestureend', (e) => {
  67. e.preventDefault();
  68. this._updateZoom(this._gestureZoom * e.scale, e);
  69. }, false);
  70. }
  71. else {
  72. element.addEventListener('touchstart', (e) => {
  73. if (e.touches.length === 2) {
  74. this._touchPoints = Array.from(e.touches);
  75. this._touchZoom = this._zoom;
  76. }
  77. }, { passive: true });
  78. element.addEventListener('touchmove', (e) => {
  79. if (Array.isArray(this._touchPoints) && this._touchPoints.length === 2 && e.touches.length === 2) {
  80. const distance = (points) => {
  81. const dx =(points[1].clientX - points[0].clientX);
  82. const dy =(points[1].clientY - points[0].clientY);
  83. return Math.sqrt(dx * dx + dy * dy);
  84. };
  85. const d1 = distance(Array.from(e.touches));
  86. const d2 = distance(this._touchPoints);
  87. if (d2 !== 0) {
  88. const points = this._touchPoints;
  89. const e = {
  90. pageX: (points[1].pageX + points[0].pageX) / 2,
  91. pageY: (points[1].pageY + points[0].pageY) / 2
  92. };
  93. const zoom = d2 === 0 ? d1 : d1 / d2;
  94. this._updateZoom(this._touchZoom * zoom, e);
  95. }
  96. }
  97. }, { passive: true });
  98. element.addEventListener('touchcancel', () => {
  99. delete this._touchPoints;
  100. delete this._touchZoom;
  101. }, { passive: true });
  102. element.addEventListener('touchend', () => {
  103. delete this._touchPoints;
  104. delete this._touchZoom;
  105. }, { passive: true });
  106. }
  107. }
  108. break;
  109. }
  110. case 'drag': {
  111. this._getElementById('toolbar').addEventListener('mousewheel', (e) => {
  112. this._preventZoom(e);
  113. }, { passive: true });
  114. break;
  115. }
  116. }
  117. }).catch((err) => {
  118. this.error(err, null, null);
  119. });
  120. }
  121. show(page) {
  122. if (!page) {
  123. page = (!this._model && !this._graphs[0]) ? 'welcome' : 'default';
  124. }
  125. this._host.screen(page);
  126. if (this._sidebar) {
  127. this._sidebar.close();
  128. }
  129. this._host.document.body.setAttribute('class', page);
  130. }
  131. cut() {
  132. this._host.document.execCommand('cut');
  133. }
  134. copy() {
  135. this._host.document.execCommand('copy');
  136. }
  137. paste() {
  138. this._host.document.execCommand('paste');
  139. }
  140. selectAll() {
  141. this._host.document.execCommand('selectall');
  142. }
  143. find() {
  144. if (this._graphs[0]) {
  145. this.clearSelection();
  146. const graphElement = this._getElementById('canvas');
  147. const view = new sidebar.FindSidebar(this._host, graphElement, this._graphs[0]);
  148. view.on('search-text-changed', (sender, text) => {
  149. this._searchText = text;
  150. });
  151. view.on('select', (sender, selection) => {
  152. this._sidebar.close();
  153. this.select(selection);
  154. });
  155. this._sidebar.open(view.content, 'Find');
  156. view.focus(this._searchText);
  157. }
  158. }
  159. get model() {
  160. return this._model;
  161. }
  162. toggleAttributes() {
  163. this._showAttributes = !this._showAttributes;
  164. this._reload();
  165. }
  166. get showAttributes() {
  167. return this._showAttributes;
  168. }
  169. toggleInitializers() {
  170. this._showInitializers = !this._showInitializers;
  171. this._reload();
  172. }
  173. get showInitializers() {
  174. return this._showInitializers;
  175. }
  176. toggleNames() {
  177. this._showNames = !this._showNames;
  178. this._reload();
  179. }
  180. get showNames() {
  181. return this._showNames;
  182. }
  183. toggleDirection() {
  184. this._showHorizontal = !this._showHorizontal;
  185. this._reload();
  186. }
  187. get showHorizontal() {
  188. return this._showHorizontal;
  189. }
  190. _reload() {
  191. this.show('welcome spinner');
  192. if (this._model && this._graphs[0]) {
  193. this._updateGraph(this._model, this._graphs).catch((error) => {
  194. if (error) {
  195. this.error(error, 'Graph update failed.', 'welcome');
  196. }
  197. });
  198. }
  199. }
  200. _timeout(time) {
  201. return new Promise((resolve) => {
  202. setTimeout(() => { resolve(); }, time);
  203. });
  204. }
  205. _getElementById(id) {
  206. return this._host.document.getElementById(id + this._id);
  207. }
  208. zoomIn() {
  209. switch (this._host.environment('zoom')) {
  210. case 'scroll':
  211. this._updateZoom(this._zoom * 1.1);
  212. break;
  213. case 'drag':
  214. if (this._zoom) {
  215. this._zoom.scaleBy(1.2);
  216. }
  217. break;
  218. }
  219. }
  220. zoomOut() {
  221. switch (this._host.environment('zoom')) {
  222. case 'scroll':
  223. this._updateZoom(this._zoom * 0.9);
  224. break;
  225. case 'drag':
  226. if (this._zoom) {
  227. this._zoom.scaleBy(0.8);
  228. }
  229. break;
  230. }
  231. }
  232. resetZoom() {
  233. switch (this._host.environment('zoom')) {
  234. case 'scroll':
  235. this._updateZoom(1);
  236. break;
  237. case 'drag':
  238. if (this._zoom) {
  239. this._zoom.scaleTo(1);
  240. }
  241. break;
  242. }
  243. }
  244. _preventZoom(e) {
  245. if (e.shiftKey || e.ctrlKey) {
  246. e.preventDefault();
  247. }
  248. }
  249. _updateZoom(zoom, e) {
  250. const graphElement = this._getElementById('graph');
  251. const min = Math.min(Math.max(graphElement.clientHeight / this._height, 0.2), 1);
  252. zoom = Math.min(zoom, 1.4);
  253. zoom = Math.max(min, zoom);
  254. const scrollLeft = this._scrollLeft || graphElement.scrollLeft;
  255. const scrollTop = this._scrollTop || graphElement.scrollTop;
  256. const x = (e ? e.pageX : (graphElement.clientWidth / 2)) + scrollLeft;
  257. const y = (e ? e.pageY : (graphElement.clientHeight / 2)) + scrollTop;
  258. const canvasElement = this._getElementById('canvas');
  259. canvasElement.style.width = zoom * this._width;
  260. canvasElement.style.height = zoom * this._height;
  261. this._scrollLeft = ((x * zoom) / this._zoom) - (x - scrollLeft);
  262. this._scrollTop = ((y * zoom) / this._zoom) - (y - scrollTop);
  263. this._scrollLeft = Math.max(0, this._scrollLeft);
  264. this._scrollTop = Math.max(0, this._scrollTop);
  265. graphElement.scrollLeft = this._scrollLeft;
  266. graphElement.scrollTop = this._scrollTop;
  267. this._zoom = zoom;
  268. }
  269. _mouseWheelHandler(e) {
  270. if (e.shiftKey || e.ctrlKey) {
  271. this._updateZoom(this._zoom + (e.wheelDelta * 1.0 / 4000.0), e);
  272. e.preventDefault();
  273. }
  274. }
  275. _scrollHandler(e) {
  276. if (this._scrollLeft && e.target.scrollLeft !== Math.floor(this._scrollLeft)) {
  277. delete this._scrollLeft;
  278. }
  279. if (this._scrollTop && e.target.scrollTop !== Math.floor(this._scrollTop)) {
  280. delete this._scrollTop;
  281. }
  282. }
  283. select(selection) {
  284. this.clearSelection();
  285. if (selection && selection.length > 0) {
  286. const graphElement = this._getElementById('graph');
  287. switch (this._host.environment('zoom')) {
  288. case 'drag': {
  289. let x = 0;
  290. let y = 0;
  291. for (const element of selection) {
  292. element.classList.add('select');
  293. this._selection.push(element);
  294. const transform = element.transform.baseVal.consolidate();
  295. const box = element.getBBox();
  296. const ex = transform ? transform.matrix.e : box.x + (box.width / 2);
  297. const ey = transform ? transform.matrix.f : box.y + (box.height / 2);
  298. x += ex;
  299. y += ey;
  300. }
  301. x = x / selection.length;
  302. y = y / selection.length;
  303. const canvasElement = this._getElementById('canvas');
  304. const canvasRect = canvasElement.getBoundingClientRect();
  305. this._zoom.transform(view.Zoom.identity().translate((canvasRect.width / 2) - x, (canvasRect.height / 2) - y));
  306. break;
  307. }
  308. case 'scroll': {
  309. let x = 0;
  310. let y = 0;
  311. for (const element of selection) {
  312. element.classList.add('select');
  313. this._selection.push(element);
  314. const rect = element.getBoundingClientRect();
  315. x += rect.left + (rect.width / 2);
  316. y += rect.top + (rect.height / 2);
  317. }
  318. x = x / selection.length;
  319. y = y / selection.length;
  320. const rect = graphElement.getBoundingClientRect();
  321. const left = (graphElement.scrollLeft + x - rect.left) - (rect.width / 2);
  322. const top = (graphElement.scrollTop + y - rect.top) - (rect.height / 2);
  323. graphElement.scrollTo({ left: left, top: top, behavior: 'smooth' });
  324. break;
  325. }
  326. }
  327. }
  328. }
  329. clearSelection() {
  330. while (this._selection.length > 0) {
  331. const element = this._selection.pop();
  332. element.classList.remove('select');
  333. }
  334. }
  335. error(err, name, screen) {
  336. if (this._sidebar) {
  337. this._sidebar.close();
  338. }
  339. this._host.exception(err, false);
  340. const knowns = [
  341. { name: '', message: /^Invalid argument identifier/, url: 'https://github.com/lutzroeder/netron/issues/540' },
  342. { name: '', message: /^Cannot read property/, url: 'https://github.com/lutzroeder/netron/issues/647' },
  343. { name: '', message: /^Failed to render tensor/, url: 'https://github.com/lutzroeder/netron/issues/681' },
  344. { name: 'Error', message: /^EPERM: operation not permitted/, url: 'https://github.com/lutzroeder/netron/issues/551' },
  345. { name: 'Error', message: /^EACCES: permission denied/, url: 'https://github.com/lutzroeder/netron/issues/504' },
  346. { name: 'RangeError', message: /^Offset is outside the bounds of the DataView/, url: 'https://github.com/lutzroeder/netron/issues/563' },
  347. { name: 'RangeError', message: /^start offset of Int32Array/, url: 'https://github.com/lutzroeder/netron/issues/565' },
  348. { name: 'RangeError', message: /^Maximum call stack size exceeded/, url: 'https://github.com/lutzroeder/netron/issues/589' },
  349. { name: 'RangeError', message: /^Invalid string length/, url: 'https://github.com/lutzroeder/netron/issues/648' },
  350. { name: 'Error loading model.', message: /^Unsupported file content \(/, url: 'https://github.com/lutzroeder/netron/issues/550' },
  351. { name: 'Error loading model.', message: /^Unsupported Protocol Buffers content/, url: 'https://github.com/lutzroeder/netron/issues/593' },
  352. { name: 'Error loading model.', message: /^Unsupported Protocol Buffers text content/, url: 'https://github.com/lutzroeder/netron/issues/594' },
  353. { name: 'Error loading model.', message: /^Unsupported JSON content/, url: 'https://github.com/lutzroeder/netron/issues/595' },
  354. { name: 'Error loading Caffe model.', message: /^File format is not caffe\.NetParameter/, url: 'https://github.com/lutzroeder/netron/issues/563' },
  355. { name: 'Error loading Darknet model.', message: /^Invalid tensor shape/, url: 'https://github.com/lutzroeder/netron/issues/541' },
  356. { name: 'Error loading Keras model.', message: /^Unsupported data object header version/, url: 'https://github.com/lutzroeder/netron/issues/548' },
  357. { name: 'Error loading MNN model.', message: /^File format is not mnn\.Net/, url: 'https://github.com/lutzroeder/netron/issues/746' },
  358. { name: 'Error loading PyTorch model.', message: /^File does not contain root module or state dictionary/, url: 'https://github.com/lutzroeder/netron/issues/543' },
  359. { name: 'Error loading PyTorch model.', message: /^Module does not contain modules/, url: 'https://github.com/lutzroeder/netron/issues/544' },
  360. { name: 'Error loading PyTorch model.', message: /^Failed to resolve module/, url: 'https://github.com/lutzroeder/netron/issues/545' },
  361. { name: 'Error loading PyTorch model.', message: /^Unsupported function/, url: 'https://github.com/lutzroeder/netron/issues/546' },
  362. { name: 'Error loading PyTorch model.', message: /^Unsupported uninitialized argument/, url: 'https://github.com/lutzroeder/netron/issues/547' },
  363. { name: 'Error loading ONNX model.', message: /^File format is not onnx\.ModelProto/, url: 'https://github.com/lutzroeder/netron/issues/549' },
  364. { name: 'Error loading TensorFlow model.', message: /^File text format is not TensorFlow\.js graph-model/, url: 'https://github.com/lutzroeder/netron/issues/764' },
  365. { name: 'Error loading TensorFlow Lite model.', message: /^Offset is outside the bounds of the DataView/, url: 'https://github.com/lutzroeder/netron/issues/563' },
  366. { name: 'Error loading UFF model.', message: /^Unknown attribute/, url: 'https://github.com/lutzroeder/netron/issues/649' }
  367. ];
  368. const known = knowns.find((known) => (known.name.length === 0 || known.name === err.name) && err.message.match(known.message));
  369. const message = err.message + (known ? '\n\nPlease provide information about this issue at ' + known.url + '.' : '');
  370. name = name || err.name;
  371. this._host.error(name, message);
  372. this.show(screen !== undefined ? screen : 'welcome');
  373. if (known) {
  374. this._host.openURL(known.url);
  375. }
  376. }
  377. accept(file) {
  378. return this._modelFactoryService.accept(file);
  379. }
  380. open(context) {
  381. this._host.event('Model', 'Open', 'Size', context.stream ? context.stream.length : 0);
  382. this._sidebar.close();
  383. return this._timeout(2).then(() => {
  384. return this._modelFactoryService.open(context).then((model) => {
  385. const format = [];
  386. if (model.format) {
  387. format.push(model.format);
  388. }
  389. if (model.producer) {
  390. format.push('(' + model.producer + ')');
  391. }
  392. if (format.length > 0) {
  393. this._host.event('Model', 'Format', format.join(' '));
  394. }
  395. return this._timeout(20).then(() => {
  396. const graphs = Array.isArray(model.graphs) && model.graphs.length > 0 ? [ model.graphs[0] ] : [];
  397. return this._updateGraph(model, graphs);
  398. });
  399. });
  400. });
  401. }
  402. _updateActiveGraph(name) {
  403. this._sidebar.close();
  404. if (this._model) {
  405. const model = this._model;
  406. const graph = model.graphs.filter(graph => name === graph.name).shift();
  407. if (graph) {
  408. this.show('welcome spinner');
  409. this._timeout(200).then(() => {
  410. return this._updateGraph(model, [ graph ]).catch((error) => {
  411. if (error) {
  412. this.error(error, 'Graph update failed.', 'welcome');
  413. }
  414. });
  415. });
  416. }
  417. }
  418. }
  419. _updateGraph(model, graphs) {
  420. const active = (graphs) => {
  421. return Array.isArray(graphs) && graphs.length > 0 ? graphs[0] : null;
  422. };
  423. const graph = active(graphs);
  424. return this._timeout(100).then(() => {
  425. if (graph && graph != this._graphs[0]) {
  426. const nodes = graph.nodes;
  427. if (nodes.length > 1400) {
  428. 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?')) {
  429. this._host.event('Graph', 'Render', 'Skip', nodes.length);
  430. this.show(null);
  431. return null;
  432. }
  433. }
  434. }
  435. return this.renderGraph(model, graph).then(() => {
  436. this._model = model;
  437. this._graphs = graphs;
  438. if (!graphs || graphs.length <= 1) {
  439. this.show('default');
  440. }
  441. return this._model;
  442. }).catch((error) => {
  443. return this.renderGraph(this._model, active(this._graphs)).then(() => {
  444. if (!graphs || graphs.length <= 1) {
  445. this.show('default');
  446. }
  447. throw error;
  448. }).catch(() => {
  449. throw error;
  450. });
  451. });
  452. });
  453. }
  454. pushGraph(graph) {
  455. if (graph !== this._graphs[0]) {
  456. return this._updateGraph(this._model, [ graph ].concat(this._graphs));
  457. }
  458. return Promise.resolve();
  459. }
  460. popGraph() {
  461. if (this._graphs.length > 1) {
  462. return this._updateGraph(this._model, this._graphs.subarray(1));
  463. }
  464. }
  465. renderGraph(model, graph) {
  466. try {
  467. const graphElement = this._getElementById('graph');
  468. const canvasElement = this._getElementById('canvas');
  469. while (canvasElement.lastChild) {
  470. canvasElement.removeChild(canvasElement.lastChild);
  471. }
  472. if (!graph) {
  473. return Promise.resolve();
  474. }
  475. else {
  476. switch (this._host.environment('zoom')) {
  477. case 'scroll':
  478. this._zoom = 1;
  479. canvasElement.style.position = 'static';
  480. canvasElement.style.margin = 'auto';
  481. break;
  482. case 'drag':
  483. this._zoom = null;
  484. canvasElement.style.position = 'absolute';
  485. canvasElement.style.margin = '0';
  486. break;
  487. }
  488. const groups = graph.groups;
  489. const nodes = graph.nodes;
  490. this._host.event('Graph', 'Render', 'Size', nodes.length);
  491. const options = {};
  492. options.nodesep = 25;
  493. options.ranksep = 20;
  494. const rotate = graph.nodes.every((node) => node.inputs.filter((input) => input.arguments.every((argument) => !argument.initializer)).length === 0 && node.outputs.length === 0);
  495. const showHorizontal = rotate ? !this._showHorizontal : this._showHorizontal;
  496. if (showHorizontal) {
  497. options.rankdir = "LR";
  498. }
  499. if (nodes.length > 1500) {
  500. options.ranker = 'longest-path';
  501. }
  502. const viewGraph = new view.Graph(this, model, groups, options);
  503. const clusters = new Set();
  504. const clusterParentMap = new Map();
  505. if (groups) {
  506. for (const node of nodes) {
  507. if (node.group) {
  508. const path = node.group.split('/');
  509. while (path.length > 0) {
  510. const name = path.join('/');
  511. path.pop();
  512. clusterParentMap.set(name, path.join('/'));
  513. }
  514. }
  515. }
  516. }
  517. for (const node of nodes) {
  518. const viewNode = viewGraph.createNode(node);
  519. const inputs = node.inputs;
  520. for (const input of inputs) {
  521. for (const argument of input.arguments) {
  522. if (argument.name != '' && !argument.initializer) {
  523. viewGraph.createArgument(argument).to(viewNode);
  524. }
  525. }
  526. }
  527. let outputs = node.outputs;
  528. if (node.chain && node.chain.length > 0) {
  529. const chainOutputs = node.chain[node.chain.length - 1].outputs;
  530. if (chainOutputs.length > 0) {
  531. outputs = chainOutputs;
  532. }
  533. }
  534. for (const output of outputs) {
  535. for (const argument of output.arguments) {
  536. if (!argument) {
  537. throw new view.Error("Invalid null argument in '" + model.format + "'.");
  538. }
  539. if (argument.name != '') {
  540. viewGraph.createArgument(argument).from(viewNode);
  541. }
  542. }
  543. }
  544. if (node.controlDependencies && node.controlDependencies.length > 0) {
  545. for (const name of node.controlDependencies) {
  546. viewGraph.createArgument({ name: name, controlDependency: true }).to(viewNode);
  547. }
  548. }
  549. const createCluster = function(name) {
  550. if (!clusters.has(name)) {
  551. viewGraph.setNode({ name: name, rx: 5, ry: 5});
  552. clusters.add(name);
  553. const parent = clusterParentMap.get(name);
  554. if (parent) {
  555. createCluster(parent);
  556. viewGraph.setParent(name, parent);
  557. }
  558. }
  559. };
  560. if (groups) {
  561. let groupName = node.group;
  562. if (groupName && groupName.length > 0) {
  563. if (!clusterParentMap.has(groupName)) {
  564. const lastIndex = groupName.lastIndexOf('/');
  565. if (lastIndex != -1) {
  566. groupName = groupName.substring(0, lastIndex);
  567. if (!clusterParentMap.has(groupName)) {
  568. groupName = null;
  569. }
  570. }
  571. else {
  572. groupName = null;
  573. }
  574. }
  575. if (groupName) {
  576. createCluster(groupName);
  577. viewGraph.setParent(viewNode.name, groupName);
  578. }
  579. }
  580. }
  581. }
  582. for (const input of graph.inputs) {
  583. const viewInput = viewGraph.createInput(input);
  584. for (const argument of input.arguments) {
  585. viewGraph.createArgument(argument).from(viewInput);
  586. }
  587. }
  588. for (const output of graph.outputs) {
  589. const viewOutput = viewGraph.createOutput(output);
  590. for (const argument of output.arguments) {
  591. viewGraph.createArgument(argument).to(viewOutput);
  592. }
  593. }
  594. // Workaround for Safari background drag/zoom issue:
  595. // https://stackoverflow.com/questions/40887193/d3-js-zoom-is-not-working-with-mousewheel-in-safari
  596. const backgroundElement = this._host.document.createElementNS('http://www.w3.org/2000/svg', 'rect');
  597. backgroundElement.setAttribute('id', 'background');
  598. if (this._host.environment('zoom') === 'drag') {
  599. backgroundElement.setAttribute('width', '100%');
  600. backgroundElement.setAttribute('height', '100%');
  601. }
  602. backgroundElement.setAttribute('fill', 'none');
  603. backgroundElement.setAttribute('pointer-events', 'all');
  604. canvasElement.appendChild(backgroundElement);
  605. const originElement = this._host.document.createElementNS('http://www.w3.org/2000/svg', 'g');
  606. originElement.setAttribute('id', 'origin');
  607. canvasElement.appendChild(originElement);
  608. viewGraph.build(this._host.document, originElement);
  609. switch (this._host.environment('zoom')) {
  610. case 'drag': {
  611. this._zoom = new view.Zoom(canvasElement, 0.1, 1.4);
  612. this._zoom.on('zoom', (sender, data) => {
  613. originElement.setAttribute('transform', data.transform.toString());
  614. });
  615. this._zoom.transform(view.Zoom.identity());
  616. break;
  617. }
  618. case 'scroll': {
  619. this._zoom = 1;
  620. break;
  621. }
  622. }
  623. return this._timeout(20).then(() => {
  624. viewGraph.layout();
  625. const elements = Array.from(canvasElement.getElementsByClassName('graph-input') || []);
  626. if (elements.length === 0) {
  627. const nodeElements = Array.from(canvasElement.getElementsByClassName('graph-node') || []);
  628. if (nodeElements.length > 0) {
  629. elements.push(nodeElements[0]);
  630. }
  631. }
  632. switch (this._host.environment('zoom')) {
  633. case 'drag': {
  634. const svgSize = canvasElement.getBoundingClientRect();
  635. if (elements && elements.length > 0) {
  636. // Center view based on input elements
  637. const xs = [];
  638. const ys = [];
  639. for (let i = 0; i < elements.length; i++) {
  640. const transform = elements[i].transform.baseVal.consolidate();
  641. if (transform) {
  642. xs.push(transform.matrix.e);
  643. ys.push(transform.matrix.f);
  644. }
  645. }
  646. let x = xs[0];
  647. const y = ys[0];
  648. if (ys.every(y => y === ys[0])) {
  649. x = xs.reduce((a, b) => a + b, 0) / xs.length;
  650. }
  651. const sx = (svgSize.width / (this._showHorizontal ? 4 : 2)) - x;
  652. const sy = (svgSize.height / (this._showHorizontal ? 2 : 4)) - y;
  653. this._zoom.transform(view.Zoom.identity().translate(sx, sy));
  654. }
  655. else {
  656. this._zoom.transform(view.Zoom.identity().translate((svgSize.width - viewGraph.graph().width) / 2, (svgSize.height - viewGraph.graph().height) / 2));
  657. }
  658. break;
  659. }
  660. case 'scroll': {
  661. const size = canvasElement.getBBox();
  662. const margin = 100;
  663. const width = Math.ceil(margin + size.width + margin);
  664. const height = Math.ceil(margin + size.height + margin);
  665. originElement.setAttribute('transform', 'translate(' + margin.toString() + ', ' + margin.toString() + ') scale(1)');
  666. backgroundElement.setAttribute('width', width);
  667. backgroundElement.setAttribute('height', height);
  668. this._width = width;
  669. this._height = height;
  670. this._zoom = 1;
  671. delete this._scrollLeft;
  672. delete this._scrollRight;
  673. canvasElement.setAttribute('viewBox', '0 0 ' + width + ' ' + height);
  674. canvasElement.setAttribute('width', width);
  675. canvasElement.setAttribute('height', height);
  676. this._updateZoom(this._zoom);
  677. if (elements && elements.length > 0) {
  678. // Center view based on input elements
  679. const xs = [];
  680. const ys = [];
  681. for (let i = 0; i < elements.length; i++) {
  682. const element = elements[i];
  683. const rect = element.getBoundingClientRect();
  684. xs.push(rect.left + (rect.width / 2));
  685. ys.push(rect.top + (rect.height / 2));
  686. }
  687. let x = xs[0];
  688. const y = ys[0];
  689. if (ys.every(y => y === ys[0])) {
  690. x = xs.reduce((a, b) => a + b, 0) / xs.length;
  691. }
  692. // const canvasRect = graphElement.getBoundingClientRect();
  693. const graphRect = graphElement.getBoundingClientRect();
  694. // const sx = (canvasRect.width / (this._showHorizontal ? 4 : 2)) - x;
  695. // const sy = (canvasRect.height / (this._showHorizontal ? 2 : 4)) - y;
  696. const left = (graphElement.scrollLeft + x - graphRect.left) - (graphRect.width / 2);
  697. const top = (graphElement.scrollTop + y - graphRect.top) - (graphRect.height / 2);
  698. graphElement.scrollTo({ left: left, top: top, behavior: 'auto' });
  699. }
  700. else {
  701. const canvasRect = graphElement.getBoundingClientRect();
  702. const graphRect = graphElement.getBoundingClientRect();
  703. const left = (graphElement.scrollLeft + (canvasRect.width / 2) - graphRect.left) - (graphRect.width / 2);
  704. const top = (graphElement.scrollTop + (canvasRect.height / 2) - graphRect.top) - (graphRect.height / 2);
  705. graphElement.scrollTo({ left: left, top: top, behavior: 'auto' });
  706. }
  707. break;
  708. }
  709. }
  710. return;
  711. });
  712. }
  713. }
  714. catch (error) {
  715. return Promise.reject(error);
  716. }
  717. }
  718. applyStyleSheet(element, name) {
  719. let rules = [];
  720. for (const styleSheet of this._host.document.styleSheets) {
  721. if (styleSheet && styleSheet.href && styleSheet.href.endsWith('/' + name)) {
  722. rules = styleSheet.cssRules;
  723. break;
  724. }
  725. }
  726. const nodes = element.getElementsByTagName('*');
  727. for (const node of nodes) {
  728. for (const rule of rules) {
  729. if (node.matches(rule.selectorText)) {
  730. for (const item of rule.style) {
  731. node.style[item] = rule.style[item];
  732. }
  733. }
  734. }
  735. }
  736. }
  737. export(file) {
  738. const lastIndex = file.lastIndexOf('.');
  739. const extension = (lastIndex != -1) ? file.substring(lastIndex + 1) : '';
  740. if (this._graphs[0] && (extension === 'png' || extension === 'svg')) {
  741. const graphElement = this._getElementById('canvas');
  742. const exportElement = graphElement.cloneNode(true);
  743. this.applyStyleSheet(exportElement, 'view-grapher.css');
  744. exportElement.setAttribute('id', 'export');
  745. exportElement.removeAttribute('width');
  746. exportElement.removeAttribute('height');
  747. exportElement.style.removeProperty('opacity');
  748. exportElement.style.removeProperty('display');
  749. const backgroundElement = exportElement.querySelector('#background');
  750. const originElement = exportElement.querySelector('#origin');
  751. originElement.setAttribute('transform', 'translate(0,0) scale(1)');
  752. backgroundElement.removeAttribute('width');
  753. backgroundElement.removeAttribute('height');
  754. const parentElement = graphElement.parentElement;
  755. parentElement.insertBefore(exportElement, graphElement);
  756. const size = exportElement.getBBox();
  757. parentElement.removeChild(exportElement);
  758. parentElement.removeChild(graphElement);
  759. parentElement.appendChild(graphElement);
  760. const delta = (Math.min(size.width, size.height) / 2.0) * 0.1;
  761. const width = Math.ceil(delta + size.width + delta);
  762. const height = Math.ceil(delta + size.height + delta);
  763. originElement.setAttribute('transform', 'translate(' + delta.toString() + ', ' + delta.toString() + ') scale(1)');
  764. exportElement.setAttribute('width', width);
  765. exportElement.setAttribute('height', height);
  766. backgroundElement.setAttribute('width', width);
  767. backgroundElement.setAttribute('height', height);
  768. backgroundElement.setAttribute('fill', '#fff');
  769. const data = new XMLSerializer().serializeToString(exportElement);
  770. if (extension === 'svg') {
  771. const blob = new Blob([ data ], { type: 'image/svg' });
  772. this._host.export(file, blob);
  773. }
  774. if (extension === 'png') {
  775. const imageElement = new Image();
  776. imageElement.onload = () => {
  777. const max = Math.max(width, height);
  778. const scale = Math.min(24000.0 / max, 2.0);
  779. const canvas = this._host.document.createElement('canvas');
  780. canvas.width = Math.ceil(width * scale);
  781. canvas.height = Math.ceil(height * scale);
  782. const context = canvas.getContext('2d');
  783. context.scale(scale, scale);
  784. context.drawImage(imageElement, 0, 0);
  785. canvas.toBlob((blob) => {
  786. if (blob) {
  787. this._host.export(file, blob);
  788. }
  789. else {
  790. const err = new Error();
  791. err.name = 'Error exporting image.';
  792. err.message = 'Image may be too large to render as PNG.';
  793. this._host.exception(err, false);
  794. this._host.error(err.name, err.message);
  795. }
  796. }, 'image/png');
  797. };
  798. imageElement.src = 'data:image/svg+xml;base64,' + this._host.window.btoa(unescape(encodeURIComponent(data)));
  799. }
  800. }
  801. }
  802. showModelProperties() {
  803. if (this._model) {
  804. const modelSidebar = new sidebar.ModelSidebar(this._host, this._model, this._graphs[0]);
  805. modelSidebar.on('update-active-graph', (sender, name) => {
  806. this._updateActiveGraph(name);
  807. });
  808. this._sidebar.open(modelSidebar.render(), 'Model Properties');
  809. }
  810. }
  811. showNodeProperties(node, input) {
  812. if (node) {
  813. const nodeSidebar = new sidebar.NodeSidebar(this._host, node);
  814. nodeSidebar.on('show-documentation', (/* sender, e */) => {
  815. this.showNodeDocumentation(node);
  816. });
  817. nodeSidebar.on('show-graph', (sender, graph) => {
  818. this.pushGraph(graph);
  819. });
  820. nodeSidebar.on('export-tensor', (sender, tensor) => {
  821. this._host.require('./numpy').then((numpy) => {
  822. const defaultPath = tensor.name ? tensor.name.split('/').join('_').split(':').join('_').split('.').join('_') : 'tensor';
  823. this._host.save('NumPy Array', 'npy', defaultPath, (file) => {
  824. try {
  825. const dataTypeMap = new Map([
  826. [ 'float16', 'f2' ], [ 'float32', 'f4' ], [ 'float64', 'f8' ],
  827. [ 'int8', 'i1' ], [ 'int16', 'i2'], [ 'int32', 'i4' ], [ 'int64', 'i8' ],
  828. [ 'uint8', 'u1' ], [ 'uint16', 'u2' ], [ 'uint32', 'u4' ], [ 'uint64', 'u8' ],
  829. [ 'qint8', 'i1' ], [ 'qint16', 'i2' ],
  830. [ 'quint8', 'u1' ], [ 'quint16', 'u2' ]
  831. ]);
  832. const array = new numpy.Array();
  833. array.shape = tensor.type.shape.dimensions;
  834. array.data = tensor.value;
  835. array.dataType = dataTypeMap.has(tensor.type.dataType) ? dataTypeMap.get(tensor.type.dataType) : tensor.type.dataType;
  836. const blob = new Blob([ array.toBuffer() ], { type: 'application/octet-stream' });
  837. this._host.export(file, blob);
  838. }
  839. catch (error) {
  840. this.error(error, 'Error saving NumPy tensor.', null);
  841. }
  842. });
  843. }).catch(() => {
  844. });
  845. });
  846. nodeSidebar.on('error', (sender, error) => {
  847. if (this._model) {
  848. error.message = error.message.replace(/\.$/, '') + " in format '" + this._model.format + "'.";
  849. }
  850. this.error(error, null, null);
  851. });
  852. if (input) {
  853. nodeSidebar.toggleInput(input.name);
  854. }
  855. this._sidebar.open(nodeSidebar.render(), 'Node Properties');
  856. }
  857. }
  858. showNodeDocumentation(node) {
  859. const type = node.type;
  860. if (type && (type.description || type.inputs || type.outputs || type.attributes)) {
  861. if (type.nodes) {
  862. this.pushGraph(type);
  863. }
  864. const documentationSidebar = new sidebar.DocumentationSidebar(this._host, type);
  865. documentationSidebar.on('navigate', (sender, e) => {
  866. this._host.openURL(e.link);
  867. });
  868. this._sidebar.push(documentationSidebar.render(), 'Documentation');
  869. }
  870. }
  871. };
  872. view.Graph = class extends grapher.Graph {
  873. constructor(view, model, compound, options) {
  874. super(compound, options);
  875. this.view = view;
  876. this.model = model;
  877. this._arguments = new Map();
  878. this._nodeKey = 0;
  879. }
  880. createNode(node) {
  881. const value = new view.Node(this, node);
  882. value.name = this._nodeKey++;
  883. this.setNode(value);
  884. return value;
  885. }
  886. createInput(input) {
  887. const value = new view.Input(this, input);
  888. value.name = this._nodeKey++;
  889. this.setNode(value);
  890. return value;
  891. }
  892. createOutput(output) {
  893. const value = new view.Output(this, output);
  894. value.name = this._nodeKey++;
  895. this.setNode(value);
  896. return value;
  897. }
  898. createArgument(argument) {
  899. const name = argument.name;
  900. if (!this._arguments.has(name)) {
  901. this._arguments.set(name, new view.Argument(this, argument));
  902. }
  903. return this._arguments.get(name);
  904. }
  905. createEdge(from, to) {
  906. const value = new view.Edge(from, to);
  907. return value;
  908. }
  909. build(document, originElement) {
  910. for (const argument of this._arguments.values()) {
  911. argument.build();
  912. }
  913. super.build(document, originElement);
  914. }
  915. };
  916. view.Node = class extends grapher.Node {
  917. constructor(context, value) {
  918. super();
  919. this.context = context;
  920. this.value = value;
  921. view.Node.counter = view.Node.counter || 0;
  922. this.id = 'node-' + (value.name ? 'name-' + value.name : 'id-' + (view.Node.counter++).toString());
  923. this._add(this.value);
  924. }
  925. get class() {
  926. return 'graph-node';
  927. }
  928. _add(node) {
  929. const header = this.header();
  930. const styles = [ 'node-item-type' ];
  931. const type = node.type;
  932. const category = type && type.category ? type.category : '';
  933. if (category) {
  934. styles.push('node-item-type-' + category.toLowerCase());
  935. }
  936. if (typeof type.name !== 'string' || !type.name.split) { // #416
  937. const format = this.context.model && this.context.model.format ? this.context.model.format : '?';
  938. throw new view.Error("Unknown node type '" + JSON.stringify(type.name) + "' in format '" + format + "'.");
  939. }
  940. const content = this.context.view.showNames && (node.name || node.location) ? (node.name || node.location) : type.name.split('.').pop();
  941. const tooltip = this.context.view.showNames && (node.name || node.location) ? type.name : (node.name || node.location);
  942. header.add(null, styles, content, tooltip, () => {
  943. this.context.view.showNodeProperties(node, null);
  944. });
  945. if (node.type.nodes) {
  946. header.add(null, styles, '\u238B', 'Show Function Definition', () => {
  947. this.context.view.pushGraph(node.type);
  948. });
  949. }
  950. if (node.nodes) {
  951. header.add(null, styles, '+', null, () => {
  952. // debugger;
  953. });
  954. }
  955. const initializers = [];
  956. let hiddenInitializers = false;
  957. if (this.context.view.showInitializers) {
  958. for (const input of node.inputs) {
  959. if (input.visible && input.arguments.length === 1 && input.arguments[0].initializer != null) {
  960. initializers.push(input);
  961. }
  962. if ((!input.visible || input.arguments.length > 1) &&
  963. input.arguments.some((argument) => argument.initializer != null)) {
  964. hiddenInitializers = true;
  965. }
  966. }
  967. }
  968. let sortedAttributes = [];
  969. const attributes = node.attributes || [];
  970. if (this.context.view.showAttributes) {
  971. sortedAttributes = attributes.filter((attribute) => attribute.visible).slice();
  972. }
  973. sortedAttributes.sort((a, b) => {
  974. const au = a.name.toUpperCase();
  975. const bu = b.name.toUpperCase();
  976. return (au < bu) ? -1 : (au > bu) ? 1 : 0;
  977. });
  978. if (initializers.length > 0 || hiddenInitializers || sortedAttributes.length > 0) {
  979. const block = this.list();
  980. block.handler = () => {
  981. this.context.view.showNodeProperties(node);
  982. };
  983. for (const initializer of initializers) {
  984. const argument = initializer.arguments[0];
  985. const type = argument.type;
  986. let shape = '';
  987. let separator = '';
  988. if (type && type.shape && type.shape.dimensions && Array.isArray(type.shape.dimensions)) {
  989. shape = '\u3008' + type.shape.dimensions.map((d) => d ? d : '?').join('\u00D7') + '\u3009';
  990. if (type.shape.dimensions.length === 0 && argument.initializer && !argument.initializer.state) {
  991. try {
  992. shape = argument.initializer.toString();
  993. if (shape && shape.length > 10) {
  994. shape = shape.substring(0, 10) + '\u2026';
  995. }
  996. separator = ' = ';
  997. }
  998. catch (err) {
  999. let type = '?';
  1000. try {
  1001. type = argument.initializer.type.toString();
  1002. }
  1003. catch (error) {
  1004. // continue regardless of error
  1005. }
  1006. const format = this.context.view.model && this.context.view.model.format ? this.context.view.model.format : '?';
  1007. throw new view.Error("Failed to render tensor of type '" + type + "' in format '" + format + "' (" + err.message + ").");
  1008. }
  1009. }
  1010. }
  1011. block.add(argument.name ? 'initializer-' + argument.name : '', initializer.name, shape, type ? type.toString() : '', separator);
  1012. }
  1013. if (hiddenInitializers) {
  1014. block.add(null, '\u3008' + '\u2026' + '\u3009', '', null, '');
  1015. }
  1016. for (const attribute of sortedAttributes) {
  1017. if (attribute.visible) {
  1018. let value = sidebar.NodeSidebar.formatAttributeValue(attribute.value, attribute.type);
  1019. if (value && value.length > 25) {
  1020. value = value.substring(0, 25) + '\u2026';
  1021. }
  1022. block.add(null, attribute.name, value, attribute.type, ' = ');
  1023. }
  1024. }
  1025. }
  1026. if (Array.isArray(node.chain) && node.chain.length > 0) {
  1027. for (const innerNode of node.chain) {
  1028. this._add(innerNode);
  1029. }
  1030. }
  1031. if (node.inner) {
  1032. this._add(node.inner);
  1033. }
  1034. }
  1035. };
  1036. view.Input = class extends grapher.Node {
  1037. constructor(context, value) {
  1038. super();
  1039. this.context = context;
  1040. this.value = value;
  1041. view.Input.counter = view.Input.counter || 0;
  1042. const types = value.arguments.map((argument) => argument.type || '').join('\n');
  1043. let name = value.name || '';
  1044. if (name.length > 16) {
  1045. name = name.split('/').pop();
  1046. }
  1047. const header = this.header();
  1048. header.add(null, [ 'graph-item-input' ], name, types, () => this.context.view.showModelProperties());
  1049. this.id = 'input-' + (name ? 'name-' + name : 'id-' + (view.Input.counter++).toString());
  1050. }
  1051. get class() {
  1052. return 'graph-input';
  1053. }
  1054. };
  1055. view.Output = class extends grapher.Node {
  1056. constructor(context, value) {
  1057. super();
  1058. this.context = context;
  1059. this.value = value;
  1060. const types = value.arguments.map((argument) => argument.type || '').join('\n');
  1061. let name = value.name || '';
  1062. if (name.length > 16) {
  1063. name = name.split('/').pop();
  1064. }
  1065. const header = this.header();
  1066. header.add(null, [ 'graph-item-output' ], name, types, () => this.context.view.showModelProperties());
  1067. }
  1068. };
  1069. view.Argument = class {
  1070. constructor(context, argument) {
  1071. this.context = context;
  1072. this._argument = argument;
  1073. }
  1074. from(node) {
  1075. this._from = node;
  1076. }
  1077. to(node) {
  1078. this._to = this._to || [];
  1079. this._to.push(node);
  1080. }
  1081. build() {
  1082. this._edges = this._edges || [];
  1083. if (this._from && this._to) {
  1084. for (const to of this._to) {
  1085. let text = '';
  1086. const type = this._argument.type;
  1087. if (type && type.shape && type.shape.dimensions && type.shape.dimensions.length > 0) {
  1088. text = type.shape.dimensions.map((dimension) => dimension || '?').join('\u00D7');
  1089. }
  1090. if (this.context.view.showNames) {
  1091. text = this._argument.name.split('\n').shift(); // custom argument id
  1092. }
  1093. const edge = this.context.createEdge(this._from, to);
  1094. edge.v = this._from.name;
  1095. edge.w = to.name;
  1096. edge.label = text;
  1097. edge.id = 'edge-' + this._argument.name;
  1098. if (this._argument.controlDependency) {
  1099. edge.class = 'edge-path-control-dependency';
  1100. }
  1101. this.context.setEdge(edge);
  1102. this._edges.push(edge);
  1103. }
  1104. }
  1105. }
  1106. };
  1107. view.Edge = class extends grapher.Edge {
  1108. constructor(from, to) {
  1109. super(from, to);
  1110. }
  1111. };
  1112. view.ModelContext = class {
  1113. constructor(context, formats) {
  1114. this._context = context;
  1115. this._tags = new Map();
  1116. this._content = new Map();
  1117. this._formats = formats || new Map();
  1118. }
  1119. get identifier() {
  1120. return this._context.identifier;
  1121. }
  1122. get stream() {
  1123. return this._context.stream;
  1124. }
  1125. request(file, encoding, base) {
  1126. return this._context.request(file, encoding, base);
  1127. }
  1128. require(id) {
  1129. return this._context.require(id);
  1130. }
  1131. exception(error, fatal) {
  1132. this._context.exception(error, fatal);
  1133. }
  1134. entries(format) {
  1135. return this._formats.get(format) || new Map();
  1136. }
  1137. open(type) {
  1138. if (!this._content.has(type)) {
  1139. this._content.set(type, undefined);
  1140. const stream = this.stream;
  1141. const position = stream.position;
  1142. const skip =
  1143. Array.from(this._tags).some((pair) => pair[0] !== 'flatbuffers' && pair[1].size > 0) ||
  1144. Array.from(this._content.values()).some((obj) => obj !== undefined);
  1145. if (!skip) {
  1146. switch (type) {
  1147. case 'json': {
  1148. try {
  1149. const reader = json.TextReader.open(stream);
  1150. if (reader) {
  1151. const obj = reader.read();
  1152. this._content.set(type, obj);
  1153. }
  1154. }
  1155. catch (err) {
  1156. // continue regardless of error
  1157. }
  1158. break;
  1159. }
  1160. case 'pkl': {
  1161. let unpickler = null;
  1162. try {
  1163. if (stream.length > 2) {
  1164. const zlib = (stream) => {
  1165. const buffer = stream.peek(2);
  1166. if (buffer[0] === 0x78) {
  1167. const check = (buffer[0] << 8) + buffer[1];
  1168. if (check % 31 === 0) {
  1169. const archive = zip.Archive.open(stream);
  1170. return archive.entries.get('');
  1171. }
  1172. }
  1173. return stream;
  1174. };
  1175. unpickler = python.Unpickler.open(zlib(stream));
  1176. }
  1177. }
  1178. catch (err) {
  1179. // continue regardless of error
  1180. }
  1181. if (unpickler) {
  1182. const execution = new python.Execution(null, (error, fatal) => {
  1183. const message = error && error.message ? error.message : error.toString();
  1184. this.exception(new view.Error(message.replace(/\.$/, '') + " in '" + this.identifier + "'."), fatal);
  1185. });
  1186. const obj = unpickler.load((name, args) => execution.invoke(name, args));
  1187. this._content.set(type, obj);
  1188. }
  1189. break;
  1190. }
  1191. }
  1192. }
  1193. if (stream.position !== position) {
  1194. stream.seek(0);
  1195. }
  1196. }
  1197. return this._content.get(type);
  1198. }
  1199. tags(type) {
  1200. if (!this._tags.has(type)) {
  1201. let tags = new Map();
  1202. const stream = this.stream;
  1203. const position = stream.position;
  1204. if (stream) {
  1205. const signatures = [
  1206. [ 0x80, undefined, 0x8a, 0x0a, 0x6c, 0xfc, 0x9c, 0x46, 0xf9, 0x20, 0x6a, 0xa8, 0x50, 0x19 ], // PyTorch
  1207. [ 0x50, 0x4b ], // Zip
  1208. [ 0x1f, 0x8b ] // Gzip
  1209. ];
  1210. const skip =
  1211. signatures.some((signature) => signature.length <= stream.length && stream.peek(signature.length).every((value, index) => signature[index] === undefined || signature[index] === value)) ||
  1212. (Array.from(this._tags).some((pair) => pair[0] !== 'flatbuffers' && pair[1].size > 0) && type !== 'pb+') ||
  1213. Array.from(this._content.values()).some((obj) => obj !== undefined);
  1214. if (!skip) {
  1215. try {
  1216. switch (type) {
  1217. case 'pbtxt': {
  1218. const reader = protobuf.TextReader.open(stream);
  1219. tags = reader ? reader.signature() : tags;
  1220. break;
  1221. }
  1222. case 'pb': {
  1223. const reader = protobuf.BinaryReader.open(stream);
  1224. tags = reader.signature();
  1225. break;
  1226. }
  1227. case 'pb+': {
  1228. const reader = protobuf.BinaryReader.open(stream);
  1229. tags = reader.decode();
  1230. break;
  1231. }
  1232. case 'flatbuffers': {
  1233. if (stream.length >= 8) {
  1234. const buffer = stream.peek(Math.min(32, stream.length));
  1235. const reader = flatbuffers.BinaryReader.open(buffer);
  1236. const identifier = reader.identifier;
  1237. if (identifier.length > 0) {
  1238. tags.set('file_identifier', identifier);
  1239. }
  1240. }
  1241. break;
  1242. }
  1243. }
  1244. }
  1245. catch (error) {
  1246. tags.clear();
  1247. }
  1248. }
  1249. }
  1250. if (stream.position !== position) {
  1251. stream.seek(position);
  1252. }
  1253. this._tags.set(type, tags);
  1254. }
  1255. return this._tags.get(type);
  1256. }
  1257. };
  1258. view.ArchiveContext = class {
  1259. constructor(host, entries, rootFolder, identifier, stream) {
  1260. this._host = host;
  1261. this._entries = new Map();
  1262. if (entries) {
  1263. for (const entry of entries) {
  1264. if (entry[0].startsWith(rootFolder)) {
  1265. const name = entry[0].substring(rootFolder.length);
  1266. this._entries.set(name, entry[1]);
  1267. }
  1268. }
  1269. }
  1270. this._identifier = identifier.substring(rootFolder.length);
  1271. this._stream = stream;
  1272. }
  1273. get identifier() {
  1274. return this._identifier;
  1275. }
  1276. get stream() {
  1277. return this._stream;
  1278. }
  1279. request(file, encoding, base) {
  1280. if (base === undefined) {
  1281. const stream = this._entries.get(file);
  1282. if (!stream) {
  1283. return Promise.reject(new Error('File not found.'));
  1284. }
  1285. if (encoding) {
  1286. const decoder = new TextDecoder(encoding);
  1287. const buffer = stream.peek();
  1288. const value = decoder.decode(buffer);
  1289. return Promise.resolve(value);
  1290. }
  1291. return Promise.resolve(stream);
  1292. }
  1293. return this._host.request(file, encoding, base);
  1294. }
  1295. require(id) {
  1296. return this._host.require(id);
  1297. }
  1298. exception(error, fatal) {
  1299. this._host.exception(error, fatal);
  1300. }
  1301. };
  1302. view.ArchiveError = class extends Error {
  1303. constructor(message) {
  1304. super(message);
  1305. this.name = 'Error loading archive.';
  1306. }
  1307. };
  1308. view.ModelFactoryService = class {
  1309. constructor(host) {
  1310. this._host = host;
  1311. this._extensions = [];
  1312. this.register('./pytorch', [ '.pt', '.pth', '.pt1', '.pyt', '.pkl', '.pickle', '.h5', '.t7', '.model', '.dms', '.tar', '.ckpt', '.chkpt', '.tckpt', '.bin', '.pb', '.zip', '.nn', '.torchmodel' ]);
  1313. this.register('./onnx', [ '.onnx', '.onn', '.pb', '.pbtxt', '.prototxt', '.model', '.pt', '.pth', '.pkl', '.ort', '.ort.onnx' ]);
  1314. this.register('./mxnet', [ '.json', '.params' ]);
  1315. this.register('./coreml', [ '.mlmodel', '.bin', 'manifest.json', 'metadata.json', 'featuredescriptions.json' ]);
  1316. this.register('./caffe', [ '.caffemodel', '.pbtxt', '.prototxt', '.pt', '.txt' ]);
  1317. this.register('./caffe2', [ '.pb', '.pbtxt', '.prototxt' ]);
  1318. this.register('./torch', [ '.t7' ]);
  1319. this.register('./tflite', [ '.tflite', '.lite', '.tfl', '.bin', '.pb', '.tmfile', '.h5', '.model', '.json', '.txt' ]);
  1320. this.register('./tf', [ '.pb', '.meta', '.pbtxt', '.prototxt', '.pt', '.json', '.index', '.ckpt', '.graphdef', /.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./ ]);
  1321. this.register('./mediapipe', [ '.pbtxt' ]);
  1322. this.register('./uff', [ '.uff', '.pb', '.pbtxt', '.uff.txt', '.trt', '.engine' ]);
  1323. this.register('./tensorrt', [ '.trt', '.engine', '.model', '.txt', '.uff', '.pb', '.tmfile', '.onnx', '.pth' ]);
  1324. this.register('./npz', [ '.npz', '.npy', '.pkl' ]);
  1325. this.register('./lasagne', [ '.pkl', '.pickle', '.joblib', '.model', '.pkl.z', '.joblib.z' ]);
  1326. this.register('./lightgbm', [ '.txt', '.pkl', '.model' ]);
  1327. this.register('./sklearn', [ '.pkl', '.pickle', '.joblib', '.model', '.meta', '.pb', '.pt', '.h5', '.pkl.z', '.joblib.z' ]);
  1328. this.register('./pickle', [ '.pkl', '.pickle', '.joblib', '.model', '.meta', '.pb', '.pt', '.h5', '.pkl.z', '.joblib.z' ]);
  1329. this.register('./cntk', [ '.model', '.cntk', '.cmf', '.dnn' ]);
  1330. this.register('./paddle', [ '.pdmodel', '.pdparams', '.pdiparams', '.paddle', '__model__', '.__model__', '.pbtxt', '.txt', '.tar', '.tar.gz' ]);
  1331. this.register('./bigdl', [ '.model', '.bigdl' ]);
  1332. this.register('./darknet', [ '.cfg', '.model', '.txt', '.weights' ]);
  1333. this.register('./weka', [ '.model' ]);
  1334. this.register('./rknn', [ '.rknn', '.onnx' ]);
  1335. this.register('./dlc', [ '.dlc' ]);
  1336. this.register('./keras', [ '.h5', '.hd5', '.hdf5', '.keras', '.json', '.cfg', '.model', '.pb', '.pth', '.weights', '.pkl', '.lite', '.tflite', '.ckpt' ]);
  1337. this.register('./armnn', [ '.armnn', '.json' ]);
  1338. this.register('./mnn', ['.mnn']);
  1339. this.register('./ncnn', [ '.param', '.bin', '.cfg.ncnn', '.weights.ncnn' ]);
  1340. this.register('./tnn', [ '.tnnproto', '.tnnmodel' ]);
  1341. this.register('./tengine', ['.tmfile']);
  1342. this.register('./mslite', [ '.ms']);
  1343. this.register('./barracuda', [ '.nn' ]);
  1344. this.register('./dnn', [ '.dnn' ]);
  1345. this.register('./xmodel', [ '.xmodel' ]);
  1346. this.register('./openvino', [ '.xml', '.bin' ]);
  1347. this.register('./flux', [ '.bson' ]);
  1348. this.register('./dl4j', [ '.zip' ]);
  1349. this.register('./mlnet', [ '.zip' ]);
  1350. this.register('./acuity', [ '.json' ]);
  1351. this.register('./imgdnn', [ '.dnn', 'params', '.json' ]);
  1352. }
  1353. register(id, extensions) {
  1354. for (const extension of extensions) {
  1355. this._extensions.push({ extension: extension, id: id });
  1356. }
  1357. }
  1358. open(context) {
  1359. return this._openSignature(context).then((context) => {
  1360. const containers = new Map();
  1361. let stream = context.stream;
  1362. const entries = context.entries;
  1363. if (!stream && entries && entries.size > 0) {
  1364. containers.set('', entries);
  1365. }
  1366. else {
  1367. const identifier = context.identifier;
  1368. try {
  1369. const archive = gzip.Archive.open(stream);
  1370. if (archive) {
  1371. const entries = archive.entries;
  1372. containers.set('gzip', entries);
  1373. if (archive.entries.size === 1) {
  1374. stream = archive.entries.values().next().value;
  1375. }
  1376. }
  1377. }
  1378. catch (error) {
  1379. const message = error && error.message ? error.message : error.toString();
  1380. throw new view.ArchiveError(message.replace(/\.$/, '') + " in '" + identifier + "'.");
  1381. }
  1382. try {
  1383. const formats = new Map([ [ 'zip', zip ], [ 'tar', tar ] ]);
  1384. for (const pair of formats) {
  1385. const format = pair[0];
  1386. const module = pair[1];
  1387. const archive = module.Archive.open(stream);
  1388. if (archive) {
  1389. containers.set(format, archive.entries);
  1390. containers.delete('gzip');
  1391. break;
  1392. }
  1393. }
  1394. }
  1395. catch (error) {
  1396. const message = error && error.message ? error.message : error.toString();
  1397. throw new view.ArchiveError(message.replace(/\.$/, '') + " in '" + identifier + "'.");
  1398. }
  1399. }
  1400. const modelContext = new view.ModelContext(context, containers);
  1401. return this._openContext(modelContext).then((model) => {
  1402. if (model) {
  1403. return model;
  1404. }
  1405. if (containers.size > 0) {
  1406. return this._openEntries(containers.values().next().value).then((context) => {
  1407. if (context) {
  1408. return this._openContext(context);
  1409. }
  1410. this._unsupported(modelContext);
  1411. });
  1412. }
  1413. this._unsupported(modelContext);
  1414. });
  1415. });
  1416. }
  1417. _unsupported(context) {
  1418. const identifier = context.identifier;
  1419. const extension = identifier.split('.').pop().toLowerCase();
  1420. const stream = context.stream;
  1421. for (const module of [ zip, tar, gzip ]) {
  1422. let archive = null;
  1423. try {
  1424. archive = module.Archive.open(stream);
  1425. }
  1426. catch (error) {
  1427. // continue regardless of error
  1428. }
  1429. if (archive) {
  1430. throw new view.Error("Archive contains no model files in '" + identifier + "'.", true);
  1431. }
  1432. }
  1433. const skip = () => {
  1434. const knownUnsupportedIdentifiers = new Set([
  1435. 'natives_blob.bin',
  1436. 'v8_context_snapshot.bin',
  1437. 'snapshot_blob.bin',
  1438. 'image_net_labels.json',
  1439. 'package.json',
  1440. 'models.json',
  1441. 'LICENSE.meta',
  1442. 'input_0.pb',
  1443. 'output_0.pb'
  1444. ]);
  1445. return knownUnsupportedIdentifiers.has(context.identifier);
  1446. };
  1447. const json = () => {
  1448. const obj = context.open('json');
  1449. if (obj) {
  1450. const formats = [
  1451. { name: 'Netron metadata', tags: [ '[].name', '[].schema' ] },
  1452. { name: 'Netron metadata', tags: [ '[].name', '[].attributes' ] },
  1453. { name: 'Darkflow metadata', tags: [ 'net', 'type', 'model' ] },
  1454. { name: 'keras-yolo2 configuration', tags: [ 'model', 'train', 'valid' ] },
  1455. { name: 'Vulkan SwiftShader ICD manifest', tags: [ 'file_format_version', 'ICD' ] },
  1456. { name: 'DeepLearningExamples configuration', tags: [ 'attention_probs_dropout_prob', 'hidden_act', 'hidden_dropout_prob', 'hidden_size', ] },
  1457. { name: 'NuGet assets', tags: [ 'version', 'targets', 'packageFolders' ] },
  1458. { name: 'NuGet data', tags: [ 'format', 'restore', 'projects' ] },
  1459. { name: 'NPM package', tags: [ 'name', 'version', 'dependencies' ] },
  1460. { name: 'NetworkX adjacency_data', tags: [ 'directed', 'graph', 'nodes' ] }
  1461. ];
  1462. const match = (obj, tag) => {
  1463. if (tag.startsWith('[].')) {
  1464. tag = tag.substring(3);
  1465. return (Array.isArray(obj) && obj.some((item) => Object.prototype.hasOwnProperty.call(item, tag)));
  1466. }
  1467. return Object.prototype.hasOwnProperty.call(obj, tag);
  1468. };
  1469. for (const format of formats) {
  1470. if (format.tags.every((tag) => match(obj, tag))) {
  1471. throw new view.Error('Invalid file content. File contains ' + format.name + '.', true);
  1472. }
  1473. }
  1474. const content = JSON.stringify(obj).substring(0, 100).replace(/\s/, '').substr(0, 48) + '...';
  1475. throw new view.Error("Unsupported JSON content '" + (content.length > 64 ? content.substring(0, 100) + '...' : content) + "' for extension '." + extension + "' in '" + identifier + "'.", !skip());
  1476. }
  1477. };
  1478. const pbtxt = () => {
  1479. const formats = [
  1480. { name: 'ImageNet LabelMap data', tags: [ 'entry', 'entry.target_class' ] },
  1481. { name: 'StringIntLabelMapProto data', tags: [ 'item', 'item.id', 'item.name' ] },
  1482. { name: 'caffe.LabelMap data', tags: [ 'item', 'item.name', 'item.label' ] },
  1483. { name: 'Triton Inference Server configuration', tags: [ 'name', 'platform', 'input', 'output' ] },
  1484. { name: 'TensorFlow OpList data', tags: [ 'op', 'op.name', 'op.input_arg' ] },
  1485. { name: 'vitis.ai.proto.DpuModelParamList data', tags: [ 'model', 'model.name', 'model.kernel' ] },
  1486. { name: 'object_detection.protos.DetectionModel data', tags: [ 'model', 'model.ssd' ] },
  1487. { name: 'object_detection.protos.DetectionModel data', tags: [ 'model', 'model.faster_rcnn' ] },
  1488. { name: 'tensorflow.CheckpointState data', tags: [ 'model_checkpoint_path', 'all_model_checkpoint_paths' ] }
  1489. ];
  1490. const tags = context.tags('pbtxt');
  1491. if (tags.size > 0) {
  1492. for (const format of formats) {
  1493. if (format.tags.every((tag) => tags.has(tag))) {
  1494. throw new view.Error('Invalid file content. File contains ' + format.name + '.', true);
  1495. }
  1496. }
  1497. const entries = [];
  1498. entries.push(...Array.from(tags).filter((pair) => pair[0].toString().indexOf('.') === -1));
  1499. entries.push(...Array.from(tags).filter((pair) => pair[0].toString().indexOf('.') !== -1));
  1500. const content = entries.map((pair) => pair[1] === true ? pair[0] : pair[0] + ':' + JSON.stringify(pair[1])).join(',');
  1501. throw new view.Error("Unsupported Protocol Buffers text content '" + (content.length > 64 ? content.substring(0, 100) + '...' : content) + "' for extension '." + extension + "' in '" + identifier + "'.", !skip());
  1502. }
  1503. };
  1504. const pb = () => {
  1505. const tags = context.tags('pb+');
  1506. if (tags.size > 0) {
  1507. const formats = [
  1508. { 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]] },
  1509. { 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,[]]] },
  1510. { name: 'third_party.tensorflow.python.keras.protobuf.SavedMetadata data', tags: [[1,[[1,[[1,0],[2,0]]],[2,0],[3,2],[4,2],[5,2]]]] }
  1511. ];
  1512. const match = (tags, schema) => {
  1513. for (const pair of schema) {
  1514. const key = pair[0];
  1515. const inner = pair[1];
  1516. if (!tags.has(key)) {
  1517. continue;
  1518. }
  1519. else if (inner === false) {
  1520. return false;
  1521. }
  1522. if (Array.isArray(inner)) {
  1523. const value = tags.get(key);
  1524. if (!(value instanceof Map) || !match(value, inner)) {
  1525. return false;
  1526. }
  1527. }
  1528. else if (inner !== tags.get(key)) {
  1529. return false;
  1530. }
  1531. }
  1532. return true;
  1533. };
  1534. const tags = context.tags('pb+');
  1535. for (const format of formats) {
  1536. if (match(tags, format.tags)) {
  1537. throw new view.Error('Invalid file content. File contains ' + format.name + '.', true);
  1538. }
  1539. }
  1540. const format = (tags) => {
  1541. const text = Array.from(tags).map((pair) => {
  1542. const key = pair[0];
  1543. const value = pair[1];
  1544. return key.toString() + ':' + (value instanceof Map ? '{' + format(value) + '}' : value.toString());
  1545. });
  1546. return text.join(',');
  1547. };
  1548. const content = format(tags);
  1549. throw new view.Error("Unsupported Protocol Buffers content '" + (content.length > 64 ? content.substring(0, 100) + '...' : content) + "' for extension '." + extension + "' in '" + identifier + "'.", !skip());
  1550. }
  1551. };
  1552. const flatbuffers = () => {
  1553. const tags = context.tags('flatbuffers');
  1554. if (tags.has('file_identifier')) {
  1555. const file_identifier = tags.get('file_identifier');
  1556. const formats = [
  1557. { name: 'onnxruntime.experimental.fbs.InferenceSession data', identifier: 'ORTM' },
  1558. { name: 'tflite.Model data', identifier: 'TFL3' }
  1559. ];
  1560. for (const format of formats) {
  1561. if (file_identifier === format.identifier) {
  1562. throw new view.Error('Invalid file content. File contains ' + format.name + '.', true);
  1563. }
  1564. }
  1565. }
  1566. };
  1567. const unknown = () => {
  1568. stream.seek(0);
  1569. const buffer = stream.peek(Math.min(16, stream.length));
  1570. const bytes = Array.from(buffer).map((c) => (c < 16 ? '0' : '') + c.toString(16)).join('');
  1571. const content = stream.length > 268435456 ? '(' + bytes + ') [' + stream.length.toString() + ']': '(' + bytes + ')';
  1572. throw new view.Error("Unsupported file content " + content + " for extension '." + extension + "' in '" + identifier + "'.", !skip());
  1573. };
  1574. json();
  1575. pbtxt();
  1576. pb();
  1577. flatbuffers();
  1578. unknown();
  1579. }
  1580. _openContext(context) {
  1581. const modules = this._filter(context).filter((module) => module && module.length > 0);
  1582. const errors = [];
  1583. let match = false;
  1584. const nextModule = () => {
  1585. if (modules.length > 0) {
  1586. const id = modules.shift();
  1587. return this._host.require(id).then((module) => {
  1588. const updateErrorContext = (error, context) => {
  1589. const text = " in '" + context.identifier + "'.";
  1590. if (error && !error.message.endsWith(text) && (error.context === undefined || error.context === true)) {
  1591. error.message = error.message.replace(/\.$/, '') + text;
  1592. }
  1593. };
  1594. if (!module.ModelFactory) {
  1595. throw new view.Error("Failed to load module '" + id + "'.");
  1596. }
  1597. const modelFactory = new module.ModelFactory();
  1598. try {
  1599. if (!modelFactory.match(context)) {
  1600. return nextModule();
  1601. }
  1602. }
  1603. catch (error) {
  1604. updateErrorContext(error, context);
  1605. return Promise.reject(error);
  1606. }
  1607. match = true;
  1608. return modelFactory.open(context).then((model) => {
  1609. return model;
  1610. }).catch((error) => {
  1611. updateErrorContext(error, context);
  1612. errors.push(error);
  1613. return nextModule();
  1614. });
  1615. });
  1616. }
  1617. else {
  1618. if (match) {
  1619. if (errors.length === 1) {
  1620. const error = errors[0];
  1621. return Promise.reject(error);
  1622. }
  1623. return Promise.reject(new view.Error(errors.map((err) => err.message).join('\n')));
  1624. }
  1625. return Promise.resolve(null);
  1626. }
  1627. };
  1628. return nextModule();
  1629. }
  1630. _openEntries(entries) {
  1631. try {
  1632. const rootFolder = (files) => {
  1633. const map = files.map((file) => file.split('/').slice(0, -1));
  1634. const at = index => list => list[index];
  1635. const rotate = list => list.length === 0 ? [] : list[0].map((item, index) => list.map(at(index)));
  1636. const equals = list => list.every((item) => item === list[0]);
  1637. const folder = rotate(map).filter(equals).map(at(0)).join('/');
  1638. return folder.length === 0 ? folder : folder + '/';
  1639. };
  1640. const filter = (queue) => {
  1641. let matches = [];
  1642. const nextEntry = () => {
  1643. if (queue.length > 0) {
  1644. const entry = queue.shift();
  1645. const context = new view.ModelContext(new view.ArchiveContext(this._host, null, folder, entry.name, entry.stream));
  1646. let modules = this._filter(context);
  1647. const nextModule = () => {
  1648. if (modules.length > 0) {
  1649. const id = modules.shift();
  1650. return this._host.require(id).then((module) => {
  1651. if (!module.ModelFactory) {
  1652. throw new view.ArchiveError("Failed to load module '" + id + "'.", null);
  1653. }
  1654. const factory = new module.ModelFactory();
  1655. if (factory.match(context)) {
  1656. matches.push(entry);
  1657. modules = [];
  1658. }
  1659. return nextModule();
  1660. });
  1661. }
  1662. else {
  1663. return nextEntry();
  1664. }
  1665. };
  1666. return nextModule();
  1667. }
  1668. else {
  1669. if (matches.length === 0) {
  1670. return Promise.resolve(null);
  1671. }
  1672. // MXNet
  1673. if (matches.length === 2 &&
  1674. matches.some((e) => e.name.toLowerCase().endsWith('.params')) &&
  1675. matches.some((e) => e.name.toLowerCase().endsWith('-symbol.json'))) {
  1676. matches = matches.filter((e) => e.name.toLowerCase().endsWith('.params'));
  1677. }
  1678. // TensorFlow.js
  1679. if (matches.length > 0 &&
  1680. matches.some((e) => e.name.toLowerCase().endsWith('.bin')) &&
  1681. matches.some((e) => e.name.toLowerCase().endsWith('.json'))) {
  1682. matches = matches.filter((e) => e.name.toLowerCase().endsWith('.json'));
  1683. }
  1684. // ncnn
  1685. if (matches.length > 0 &&
  1686. matches.some((e) => e.name.toLowerCase().endsWith('.bin')) &&
  1687. matches.some((e) => e.name.toLowerCase().endsWith('.param'))) {
  1688. matches = matches.filter((e) => e.name.toLowerCase().endsWith('.param'));
  1689. }
  1690. // ncnn
  1691. if (matches.length > 0 &&
  1692. matches.some((e) => e.name.toLowerCase().endsWith('.bin')) &&
  1693. matches.some((e) => e.name.toLowerCase().endsWith('.param.bin'))) {
  1694. matches = matches.filter((e) => e.name.toLowerCase().endsWith('.param.bin'));
  1695. }
  1696. // Paddle
  1697. if (matches.length > 0 &&
  1698. matches.some((e) => e.name.toLowerCase().endsWith('.pdmodel')) &&
  1699. matches.some((e) => e.name.toLowerCase().endsWith('.pdiparams'))) {
  1700. matches = matches.filter((e) => e.name.toLowerCase().endsWith('.pdmodel'));
  1701. }
  1702. // TensorFlow Bundle
  1703. if (matches.length > 1 &&
  1704. matches.some((e) => e.name.toLowerCase().endsWith('.data-00000-of-00001'))) {
  1705. matches = matches.filter((e) => !e.name.toLowerCase().endsWith('.data-00000-of-00001'));
  1706. }
  1707. // TensorFlow SavedModel
  1708. if (matches.length === 2 &&
  1709. matches.some((e) => e.name.toLowerCase().split('/').pop() === 'keras_metadata.pb')) {
  1710. matches = matches.filter((e) => e.name.toLowerCase().split('/').pop() !== 'keras_metadata.pb');
  1711. }
  1712. if (matches.length > 1) {
  1713. return Promise.reject(new view.ArchiveError('Archive contains multiple model files.'));
  1714. }
  1715. const match = matches.shift();
  1716. return Promise.resolve(new view.ModelContext(new view.ArchiveContext(this._host, entries, folder, match.name, match.stream)));
  1717. }
  1718. };
  1719. return nextEntry();
  1720. };
  1721. const list = Array.from(entries).map((entry) => {
  1722. return { name: entry[0], stream: entry[1] };
  1723. });
  1724. const files = list.filter((entry) => {
  1725. if (entry.name.endsWith('/')) {
  1726. return false;
  1727. }
  1728. if (entry.name.split('/').pop().startsWith('.')) {
  1729. return false;
  1730. }
  1731. if (!entry.name.startsWith('./') && entry.name.startsWith('.')) {
  1732. return false;
  1733. }
  1734. return true;
  1735. });
  1736. const folder = rootFolder(files.map((entry) => entry.name));
  1737. const queue = files.slice(0).filter((entry) => entry.name.substring(folder.length).indexOf('/') < 0);
  1738. return filter(queue).then((context) => {
  1739. if (context) {
  1740. return Promise.resolve(context);
  1741. }
  1742. const queue = files.slice(0).filter((entry) => entry.name.substring(folder.length).indexOf('/') >= 0);
  1743. return filter(queue);
  1744. });
  1745. }
  1746. catch (error) {
  1747. return Promise.reject(new view.ArchiveError(error.message));
  1748. }
  1749. }
  1750. accept(identifier) {
  1751. const extension = identifier.indexOf('.') === -1 ? '' : identifier.split('.').pop().toLowerCase();
  1752. identifier = identifier.toLowerCase().split('/').pop();
  1753. for (const entry of this._extensions) {
  1754. if ((typeof entry.extension === 'string' && identifier.endsWith(entry.extension)) ||
  1755. (entry.extension instanceof RegExp && entry.extension.exec(identifier))) {
  1756. this._host.event('File', 'Accept', extension, 1);
  1757. return true;
  1758. }
  1759. }
  1760. if (identifier.endsWith('.zip') ||
  1761. identifier.endsWith('.tar') ||
  1762. identifier.endsWith('.tar.gz') ||
  1763. identifier.endsWith('.tgz') ||
  1764. identifier.endsWith('.gz') ||
  1765. identifier.endsWith('.mar') ||
  1766. identifier.endsWith('.model') ||
  1767. identifier.endsWith('.mlpackage')) {
  1768. this._host.event('File', 'Accept', extension, 1);
  1769. return true;
  1770. }
  1771. this._host.event('File', 'Reject', extension, 1);
  1772. return false;
  1773. }
  1774. _filter(context) {
  1775. const identifier = context.identifier.toLowerCase().split('/').pop();
  1776. const list = this._extensions.filter((entry) =>
  1777. (typeof entry.extension === 'string' && identifier.endsWith(entry.extension)) ||
  1778. (entry.extension instanceof RegExp && entry.extension.exec(identifier)));
  1779. return Array.from(new Set(list.map((entry) => entry.id)));
  1780. }
  1781. _openSignature(context) {
  1782. const stream = context.stream;
  1783. if (stream) {
  1784. let empty = true;
  1785. let position = 0;
  1786. while (empty && position < stream.length) {
  1787. const buffer = stream.read(Math.min(4096, stream.length - position));
  1788. position += buffer.length;
  1789. if (!buffer.every((value) => value === 0x00)) {
  1790. empty = false;
  1791. break;
  1792. }
  1793. }
  1794. stream.seek(0);
  1795. if (empty) {
  1796. return Promise.reject(new view.Error('File has no content.', true));
  1797. }
  1798. /* eslint-disable no-control-regex */
  1799. const entries = [
  1800. { name: 'ELF executable', value: /^\x7FELF/ },
  1801. { name: 'PNG image', value: /^\x89PNG/ },
  1802. { name: 'Git LFS header', value: /^version https:\/\/git-lfs.github.com/ },
  1803. { name: 'Git LFS header', value: /^\s*oid sha256:/ },
  1804. { name: 'HTML markup', value: /^\s*<html>/ },
  1805. { name: 'HTML markup', value: /^\s*<!doctype\s*html>/ },
  1806. { name: 'HTML markup', value: /^\s*<!DOCTYPE\s*html>/ },
  1807. { name: 'HTML markup', value: /^\s*<!DOCTYPE\s*HTML>/ },
  1808. { name: 'HTML markup', value: /^\s*<!DOCTYPE\s*HTML\s+(PUBLIC|SYSTEM)?/ },
  1809. { name: 'Unity metadata', value: /^fileFormatVersion:/ },
  1810. { name: 'Python source code', value: /^\s*import[ ]+(os|sys|types|torch|argparse|onnx|numpy|tensorflow)(,|;|\s)/ },
  1811. { name: 'Python source code', value: /^\s*import[ ]+([a-z])+[ ]+as[ ]+/ },
  1812. { name: 'Python source code', value: /^\s*from[ ]+(torch)[ ]+import[ ]+/ },
  1813. { name: 'TSD header', value: /^%TSD-Header-###%/ },
  1814. { name: 'AppleDouble data', value: /^\x00\x05\x16\x07/ },
  1815. { name: 'TensorFlow Hub module', value: /^\x08\x03$/, identifier: 'tfhub_module.pb' }
  1816. ];
  1817. /* eslint-enable no-control-regex */
  1818. const buffer = stream.peek(Math.min(4096, stream.length));
  1819. const text = String.fromCharCode.apply(null, buffer);
  1820. for (const entry of entries) {
  1821. if (text.match(entry.value) && (!entry.identifier || entry.identifier === context.identifier)) {
  1822. return Promise.reject(new view.Error('Invalid file content. File contains ' + entry.name + '.', true));
  1823. }
  1824. }
  1825. }
  1826. return Promise.resolve(context);
  1827. }
  1828. };
  1829. view.Error = class extends Error {
  1830. constructor(message, telemetry) {
  1831. super(message);
  1832. this.name = 'Error loading model.';
  1833. this.telemetry = telemetry;
  1834. this.stack = undefined;
  1835. }
  1836. };
  1837. view.Zoom = class {
  1838. constructor(node, min, max) {
  1839. this._scaleExtent = [ min, max ];
  1840. this._translateExtent = [ [-Infinity, -Infinity], [Infinity, Infinity] ],
  1841. this._touchStarting = false;
  1842. this._touchFirst = false;
  1843. this._touchEnding = false;
  1844. this._touchDelay = 500;
  1845. this._wheelDelay = 150;
  1846. this._events = new Map([ [ 'start', [] ], [ 'zoom', [] ], [ 'end', [] ] ]);
  1847. this._selection = new view.Zoom.Selection(node);
  1848. this._selection.node.__zoom = view.Zoom.identity();
  1849. this._selection.on('wheel.zoom', (event) => this._wheel(event), {passive: false});
  1850. this._selection.on('mousedown.zoom', (event) => this._mouseDown(event));
  1851. if (navigator.maxTouchPoints || node.ontouchstart) {
  1852. this._selection.on('touchstart.zoom', (event) => this._touchStarted(event));
  1853. this._selection.on('touchmove.zoom', (event) => this._touchMoved(event));
  1854. this._selection.on('touchend.zoom', (event) => this._touchEnded(event));
  1855. this._selection.on('touchcancel.zoom', (event) => this._touchEnded(event));
  1856. node.style.setProperty('-webkit-tap-highlight-color', 'rgba(0,0,0,0)', '');
  1857. }
  1858. }
  1859. static identity() {
  1860. view.Zoom._identity = view.Zoom._identity || new view.Zoom.Transform(1, 0, 0);
  1861. return view.Zoom._identity;
  1862. }
  1863. on(event, callback) {
  1864. if (this._events.has(event)) {
  1865. if (callback) {
  1866. this._events.get(event).push(callback);
  1867. }
  1868. else {
  1869. this._events.set([]);
  1870. }
  1871. }
  1872. }
  1873. raise(event, data) {
  1874. if (this._events.has(event)) {
  1875. const callbacks = this._events.get(event);
  1876. for (const callback of callbacks) {
  1877. callback(this, data);
  1878. }
  1879. }
  1880. }
  1881. transform(transform) {
  1882. const node = this._selection.node;
  1883. if (node) {
  1884. this._gesture(node, arguments)
  1885. .start()
  1886. .zoom(null, typeof transform === 'function' ? transform() : transform)
  1887. .end();
  1888. }
  1889. }
  1890. scaleTo(k) {
  1891. const node = this._selection.node;
  1892. if (node) {
  1893. this.transform(() => {
  1894. const e = this.extent(node);
  1895. const t0 = node.__zoom;
  1896. const p0 = this._centroid(e);
  1897. const p1 = t0.invert(p0);
  1898. const k1 = typeof k === 'function' ? k() : k;
  1899. const transform = this.translate(this.scale(t0, k1), p0, p1);
  1900. return this._constrain(transform, e, this._translateExtent);
  1901. });
  1902. }
  1903. }
  1904. scaleBy(k) {
  1905. const node = this._selection.node;
  1906. if (node) {
  1907. this.scaleTo(() => {
  1908. const k0 = node.__zoom.k;
  1909. const k1 = k;
  1910. return k0 * k1;
  1911. });
  1912. }
  1913. }
  1914. scale(transform, k) {
  1915. k = Math.max(this._scaleExtent[0], Math.min(this._scaleExtent[1], k));
  1916. return k === transform.k ? transform : new view.Zoom.Transform(k, transform.x, transform.y);
  1917. }
  1918. translate(transform, p0, p1) {
  1919. const x = p0[0] - p1[0] * transform.k, y = p0[1] - p1[1] * transform.k;
  1920. return x === transform.x && y === transform.y ? transform : new view.Zoom.Transform(transform.k, x, y);
  1921. }
  1922. pointer(event, node) {
  1923. while (event.sourceEvent) {
  1924. event = event.sourceEvent;
  1925. }
  1926. if (node === undefined) {
  1927. node = event.currentTarget;
  1928. }
  1929. if (node) {
  1930. const svg = node.ownerSVGElement || node;
  1931. if (svg.createSVGPoint) {
  1932. let point = svg.createSVGPoint();
  1933. point.x = event.clientX, point.y = event.clientY;
  1934. point = point.matrixTransform(node.getScreenCTM().inverse());
  1935. return [point.x, point.y];
  1936. }
  1937. if (node.getBoundingClientRect) {
  1938. const rect = node.getBoundingClientRect();
  1939. return [event.clientX - rect.left - node.clientLeft, event.clientY - rect.top - node.clientTop];
  1940. }
  1941. }
  1942. return [event.pageX, event.pageY];
  1943. }
  1944. _filter(event) {
  1945. return (!event.ctrlKey || event.type === 'wheel') && !event.button;
  1946. }
  1947. extent(node) {
  1948. let e = node;
  1949. if (e instanceof SVGElement) {
  1950. e = e.ownerSVGElement || e;
  1951. if (e.hasAttribute('viewBox')) {
  1952. e = e.viewBox.baseVal;
  1953. return [[e.x, e.y], [e.x + e.width, e.y + e.height]];
  1954. }
  1955. return [[0, 0], [e.width.baseVal.value, e.height.baseVal.value]];
  1956. }
  1957. return [[0, 0], [e.clientWidth, e.clientHeight]];
  1958. }
  1959. _wheelDelta(event) {
  1960. return -event.deltaY * (event.deltaMode === 1 ? 0.05 : event.deltaMode ? 1 : 0.002) * (event.ctrlKey ? 10 : 1);
  1961. }
  1962. _constrain(transform, extent, translateExtent) {
  1963. const dx0 = transform.invertX(extent[0][0]) - translateExtent[0][0];
  1964. const dx1 = transform.invertX(extent[1][0]) - translateExtent[1][0];
  1965. const dy0 = transform.invertY(extent[0][1]) - translateExtent[0][1];
  1966. const dy1 = transform.invertY(extent[1][1]) - translateExtent[1][1];
  1967. return transform.translate(
  1968. dx1 > dx0 ? (dx0 + dx1) / 2 : Math.min(0, dx0) || Math.max(0, dx1),
  1969. dy1 > dy0 ? (dy0 + dy1) / 2 : Math.min(0, dy0) || Math.max(0, dy1)
  1970. );
  1971. }
  1972. _centroid(extent) {
  1973. return [ (+extent[0][0] + +extent[1][0]) / 2, (+extent[0][1] + +extent[1][1]) / 2 ];
  1974. }
  1975. _gesture(node, clean) {
  1976. return (!clean && node.__zooming) || new view.Zoom.Gesture(node, this);
  1977. }
  1978. _stopEvent(event) {
  1979. event.preventDefault();
  1980. event.stopImmediatePropagation();
  1981. }
  1982. _wheel(event) {
  1983. const currentTarget = event.currentTarget;
  1984. const wheelidled = (gesture) => {
  1985. gesture.wheel = null;
  1986. gesture.end();
  1987. };
  1988. if (this._filter(event)) {
  1989. const gesture = this._gesture(currentTarget);
  1990. const t = currentTarget.__zoom;
  1991. const k = Math.max(this._scaleExtent[0], Math.min(this._scaleExtent[1], t.k * Math.pow(2, this._wheelDelta(event))));
  1992. const p = this.pointer(event);
  1993. if (gesture.wheel) {
  1994. if (gesture.mouse[0][0] !== p[0] || gesture.mouse[0][1] !== p[1]) {
  1995. gesture.mouse[1] = t.invert(gesture.mouse[0] = p);
  1996. }
  1997. clearTimeout(gesture.wheel);
  1998. }
  1999. else if (t.k === k) {
  2000. return;
  2001. }
  2002. else {
  2003. gesture.mouse = [p, t.invert(p)];
  2004. gesture.start();
  2005. }
  2006. event.preventDefault();
  2007. event.stopImmediatePropagation();
  2008. gesture.wheel = setTimeout(() => wheelidled(gesture), this._wheelDelay);
  2009. const transform = this.translate(this.scale(t, k), gesture.mouse[0], gesture.mouse[1]);
  2010. gesture.zoom('mouse', this._constrain(transform, gesture.extent, this._translateExtent));
  2011. }
  2012. }
  2013. _mouseDown(event) {
  2014. const currentTarget = event.currentTarget;
  2015. if (this._touchEnding || !this._filter(event)) return;
  2016. const gesture = this._gesture(currentTarget, true);
  2017. const selection = new view.Zoom.Selection(event.view)
  2018. .on('mousemove.zoom', (event) => mousemoved(event), true)
  2019. .on('mouseup.zoom', (event) => mouseupped(event), true);
  2020. const p = this.pointer(event, currentTarget);
  2021. const x0 = event.clientX;
  2022. const y0 = event.clientY;
  2023. const root = event.view.document.documentElement;
  2024. selection.on('dragstart.drag', (event) => this._stopEvent(event), { capture: true, passive: false });
  2025. if ('onselectstart' in root) {
  2026. selection.on('selectstart.drag', (event) => this._stopEvent(event), { capture: true, passive: false });
  2027. }
  2028. else {
  2029. root.__noselect = root.style.MozUserSelect;
  2030. root.style.MozUserSelect = 'none';
  2031. }
  2032. event.stopImmediatePropagation();
  2033. gesture.mouse = [ p, currentTarget.__zoom.invert(p) ];
  2034. gesture.start();
  2035. const mousemoved = (event) => {
  2036. event.preventDefault();
  2037. event.stopImmediatePropagation();
  2038. if (!gesture.moved) {
  2039. const dx = event.clientX - x0, dy = event.clientY - y0;
  2040. gesture.moved = dx * dx + dy * dy > 0;
  2041. }
  2042. const transform = this.translate(gesture.node.__zoom, gesture.mouse[0] = this.pointer(event, currentTarget), gesture.mouse[1]);
  2043. gesture.zoom('mouse', this._constrain(transform, gesture.extent, this._translateExtent));
  2044. };
  2045. const mouseupped = (event) => {
  2046. selection.on('mousemove.zoom', null);
  2047. selection.on('mouseup.zoom', null);
  2048. const root = event.view.document.documentElement;
  2049. selection.on('dragstart.drag', null);
  2050. if (gesture.moved) {
  2051. selection.on('click.drag', (event) => this._stopEvent(event), { capture: true, passive: false });
  2052. setTimeout(function() { selection.on('click.drag', null); }, 0);
  2053. }
  2054. if ('onselectstart' in root) {
  2055. selection.on('selectstart.drag', null);
  2056. }
  2057. else {
  2058. root.style.MozUserSelect = root.__noselect;
  2059. delete root.__noselect;
  2060. }
  2061. event.preventDefault();
  2062. event.stopImmediatePropagation();
  2063. gesture.end();
  2064. };
  2065. }
  2066. _touchStarted(event) {
  2067. const currentTarget = event.currentTarget;
  2068. if (this._filter(event)) {
  2069. const touches = event.touches;
  2070. const gesture = this._gesture(currentTarget, event.changedTouches.length === touches.length);
  2071. let started;
  2072. let p;
  2073. event.stopImmediatePropagation();
  2074. for (let i = 0; i < touches.length; ++i) {
  2075. const t = touches[i];
  2076. p = this.pointer(t, currentTarget);
  2077. p = [p, currentTarget.__zoom.invert(p), t.identifier];
  2078. if (!gesture.touch0) {
  2079. gesture.touch0 = p;
  2080. started = true;
  2081. gesture.taps = 1 + !!this._touchStarting;
  2082. }
  2083. else if (!gesture.touch1 && gesture.touch0[2] !== p[2]) {
  2084. gesture.touch1 = p;
  2085. gesture.taps = 0;
  2086. }
  2087. }
  2088. if (this._touchStarting) {
  2089. this._touchStarting = clearTimeout(this._touchStarting);
  2090. }
  2091. if (started) {
  2092. if (gesture.taps < 2) {
  2093. this._touchFirst = p[0];
  2094. this._touchStarting = setTimeout(function() { this._touchStarting = null; }, this._touchDelay);
  2095. }
  2096. gesture.start();
  2097. }
  2098. }
  2099. }
  2100. _touchMoved(event) {
  2101. const currentTarget = event.currentTarget;
  2102. if (currentTarget.__zooming) {
  2103. const gesture = this._gesture(currentTarget);
  2104. const touches = event.changedTouches;
  2105. let t, p, l;
  2106. event.preventDefault();
  2107. event.stopImmediatePropagation();
  2108. for (let i = 0; i < touches.length; i++) {
  2109. t = touches[i], p = this.pointer(t, currentTarget);
  2110. if (gesture.touch0 && gesture.touch0[2] === t.identifier) {
  2111. gesture.touch0[0] = p;
  2112. }
  2113. else if (gesture.touch1 && gesture.touch1[2] === t.identifier) {
  2114. gesture.touch1[0] = p;
  2115. }
  2116. }
  2117. t = gesture.node.__zoom;
  2118. if (gesture.touch1) {
  2119. const p0 = gesture.touch0[0];
  2120. const l0 = gesture.touch0[1];
  2121. const p1 = gesture.touch1[0];
  2122. const l1 = gesture.touch1[1];
  2123. let dp, dl;
  2124. dp = (dp = p1[0] - p0[0]) * dp + (dp = p1[1] - p0[1]) * dp;
  2125. dl = (dl = l1[0] - l0[0]) * dl + (dl = l1[1] - l0[1]) * dl;
  2126. t = this.scale(t, Math.sqrt(dp / dl));
  2127. p = [(p0[0] + p1[0]) / 2, (p0[1] + p1[1]) / 2];
  2128. l = [(l0[0] + l1[0]) / 2, (l0[1] + l1[1]) / 2];
  2129. }
  2130. else if (gesture.touch0) {
  2131. p = gesture.touch0[0], l = gesture.touch0[1];
  2132. }
  2133. else {
  2134. return;
  2135. }
  2136. const transform = this.translate(t, p, l);
  2137. gesture.zoom('touch', this._constrain(transform, gesture.extent, this._translateExtent));
  2138. }
  2139. }
  2140. _touchEnded(event) {
  2141. const currentTarget = event.currentTarget;
  2142. if (currentTarget.__zooming) {
  2143. const gesture = this._gesture(currentTarget);
  2144. const touches = event.changedTouches;
  2145. event.stopImmediatePropagation();
  2146. if (this._touchEnding) {
  2147. clearTimeout(this._touchEnding);
  2148. }
  2149. this._touchEnding = setTimeout(function() { this._touchEnding = null; }, this._touchDelay);
  2150. for (let i = 0; i < touches.length; i++) {
  2151. const touch = touches[i];
  2152. if (gesture.touch0 && gesture.touch0[2] === touch.identifier) {
  2153. delete gesture.touch0;
  2154. }
  2155. else if (gesture.touch1 && gesture.touch1[2] === touch.identifier) {
  2156. delete gesture.touch1;
  2157. }
  2158. }
  2159. if (gesture.touch1 && !gesture.touch0) {
  2160. gesture.touch0 = gesture.touch1;
  2161. delete gesture.touch1;
  2162. }
  2163. if (gesture.touch0) {
  2164. gesture.touch0[1] = currentTarget.__zoom.invert(gesture.touch0[0]);
  2165. }
  2166. else {
  2167. gesture.end();
  2168. }
  2169. }
  2170. }
  2171. };
  2172. view.Zoom.Selection = class {
  2173. constructor(node) {
  2174. this._node = node;
  2175. }
  2176. get node() {
  2177. return this._node;
  2178. }
  2179. each(callback) {
  2180. if (this._node) {
  2181. callback(this._node);
  2182. }
  2183. return this;
  2184. }
  2185. on(name, value, options) {
  2186. const node = this._node;
  2187. if (node) {
  2188. const key = name.split('.');
  2189. if (value) {
  2190. node.__on = node.__on || [];
  2191. const listener = (event) => value.call(node, event);
  2192. let match = false;
  2193. for (const handler of node.__on) {
  2194. if (handler.type === key[0] && handler.name === key[1]) {
  2195. node.removeEventListener(handler.type, handler.listener, handler.options);
  2196. node.addEventListener(handler.type, handler.listener = listener, handler.options = options);
  2197. handler.value = value;
  2198. handler.options = options;
  2199. match = true;
  2200. break;
  2201. }
  2202. }
  2203. if (!match) {
  2204. node.addEventListener(key[0], listener, options);
  2205. node.__on.push({ type: key[0], name: key[1], value: value, listener: listener, options: options });
  2206. }
  2207. }
  2208. else if (node.__on) {
  2209. node.__on = node.__on.filter((handler) => {
  2210. if (handler.type === key[0] && handler.name === key[1]) {
  2211. node.removeEventListener(handler.type, handler.listener, handler.options);
  2212. return false;
  2213. }
  2214. return true;
  2215. });
  2216. if (node.__on.length === 0) {
  2217. delete node.__on;
  2218. }
  2219. }
  2220. }
  2221. return this;
  2222. }
  2223. };
  2224. view.Zoom.Transform = class {
  2225. constructor(k, x, y) {
  2226. this.k = k;
  2227. this.x = x;
  2228. this.y = y;
  2229. }
  2230. translate(x, y) {
  2231. return x === 0 & y === 0 ? this : new view.Zoom.Transform(this.k, this.x + this.k * x, this.y + this.k * y);
  2232. }
  2233. invert(location) {
  2234. return [(location[0] - this.x) / this.k, (location[1] - this.y) / this.k];
  2235. }
  2236. invertX(x) {
  2237. return (x - this.x) / this.k;
  2238. }
  2239. invertY(y) {
  2240. return (y - this.y) / this.k;
  2241. }
  2242. toString() {
  2243. return 'translate(' + this.x + ',' + this.y + ') scale(' + this.k + ')';
  2244. }
  2245. };
  2246. view.Zoom.Gesture = class {
  2247. constructor(node, target) {
  2248. this.node = node;
  2249. this.active = 0;
  2250. this.extent = target.extent(node);
  2251. this.taps = 0;
  2252. this.target = target;
  2253. }
  2254. start() {
  2255. if (++this.active === 1) {
  2256. this.node.__zooming = this;
  2257. this.raise('start');
  2258. }
  2259. return this;
  2260. }
  2261. zoom(name, transform) {
  2262. if (this.mouse && name !== 'mouse') {
  2263. this.mouse[1] = transform.invert(this.mouse[0]);
  2264. }
  2265. if (this.touch0 && name !== 'touch') {
  2266. this.touch0[1] = transform.invert(this.touch0[0]);
  2267. }
  2268. if (this.touch1 && name !== 'touch') {
  2269. this.touch1[1] = transform.invert(this.touch1[0]);
  2270. }
  2271. this.node.__zoom = transform;
  2272. this.raise('zoom');
  2273. return this;
  2274. }
  2275. end() {
  2276. if (--this.active === 0) {
  2277. delete this.node.__zooming;
  2278. this.raise('end');
  2279. }
  2280. return this;
  2281. }
  2282. raise(event) {
  2283. this.target.raise(event, { transform: this.node.__zoom });
  2284. }
  2285. };
  2286. if (typeof module !== 'undefined' && typeof module.exports === 'object') {
  2287. module.exports.View = view.View;
  2288. module.exports.ModelFactoryService = view.ModelFactoryService;
  2289. }