GraphPageWidget.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  1. /*##############################################################################
  2. # HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. ############################################################################## */
  16. define([
  17. "dojo/_base/declare",
  18. "dojo/_base/lang",
  19. "dojo/_base/array",
  20. "dojo/dom",
  21. "dojo/dom-construct",
  22. "dojo/on",
  23. "dojo/html",
  24. "dojo/store/Memory",
  25. "dojo/store/Observable",
  26. "dijit/layout/_LayoutWidget",
  27. "dijit/_TemplatedMixin",
  28. "dijit/_WidgetsInTemplateMixin",
  29. "dijit/layout/BorderContainer",
  30. "dijit/layout/TabContainer",
  31. "dijit/layout/ContentPane",
  32. "dijit/registry",
  33. "dijit/Dialog",
  34. "dojox/html/entities",
  35. "dgrid/OnDemandGrid",
  36. "dgrid/Keyboard",
  37. "dgrid/Selection",
  38. "dgrid/selector",
  39. "dgrid/extensions/ColumnResizer",
  40. "dgrid/extensions/DijitRegistry",
  41. "hpcc/GraphWidget",
  42. "hpcc/ESPUtil",
  43. "hpcc/ESPWorkunit",
  44. "hpcc/TimingGridWidget",
  45. "hpcc/TimingTreeMapWidget",
  46. "dojo/text!../templates/GraphPageWidget.html",
  47. "dijit/PopupMenuItem",
  48. "dijit/Menu",
  49. "dijit/MenuItem",
  50. "dijit/MenuSeparator",
  51. "dijit/Dialog",
  52. "dijit/form/TextBox",
  53. "dijit/form/SimpleTextarea",
  54. "dijit/form/NumberSpinner",
  55. "dijit/form/DropDownButton"
  56. ], function (declare, lang, arrayUtil, dom, domConstruct, on, html, Memory, Observable,
  57. _LayoutWidget, _TemplatedMixin, _WidgetsInTemplateMixin, BorderContainer, TabContainer, ContentPane, registry, Dialog,
  58. entities,
  59. OnDemandGrid, Keyboard, Selection, selector, ColumnResizer, DijitRegistry,
  60. GraphWidget, ESPUtil, ESPWorkunit, TimingGridWidget, TimingTreeMapWidget,
  61. template) {
  62. return declare("GraphPageWidget", [_LayoutWidget, _TemplatedMixin, _WidgetsInTemplateMixin], {
  63. templateString: template,
  64. baseClass: "GraphPageWidget",
  65. borderContainer: null,
  66. rightBorderContainer: null,
  67. graphName: "",
  68. wu: null,
  69. editorControl: null,
  70. global: null,
  71. main: null,
  72. overview: null,
  73. local: null,
  74. timingGrid: null,
  75. timingTreeMap: null,
  76. subgraphsGrid: null,
  77. verticesGrid: null,
  78. edgesGrid: null,
  79. xgmmlDialog: null,
  80. infoDialog: null,
  81. findField: null,
  82. findText: "",
  83. found: [],
  84. foundIndex: 0,
  85. overviewDepth: null,
  86. localDepth: null,
  87. localDistance: null,
  88. initalized: false,
  89. buildRendering: function (args) {
  90. this.inherited(arguments);
  91. },
  92. postCreate: function (args) {
  93. this.inherited(arguments);
  94. this.borderContainer = registry.byId(this.id + "BorderContainer");
  95. this.rightBorderContainer = registry.byId(this.id + "RightBorderContainer");
  96. this.findField = registry.byId(this.id + "FindField");
  97. this.mainDepth = registry.byId(this.id + "MainDepth");
  98. this.overviewDepth = registry.byId(this.id + "OverviewDepth");
  99. this.localDepth = registry.byId(this.id + "LocalDepth");
  100. this.localDistance = registry.byId(this.id + "LocalDistance");
  101. this._initGraphControls();
  102. this._initTimings();
  103. this._initDialogs();
  104. },
  105. startup: function (args) {
  106. this.inherited(arguments);
  107. this._initSubgraphs();
  108. this._initVertices();
  109. this._initEdges();
  110. var splitter = this.borderContainer.getSplitter("right");
  111. this.main.watchSplitter(splitter);
  112. this.overview.watchSplitter(splitter);
  113. this.local.watchSplitter(splitter);
  114. splitter = this.rightBorderContainer.getSplitter("bottom");
  115. this.main.watchSplitter(splitter);
  116. this.overview.watchSplitter(splitter);
  117. this.local.watchSplitter(splitter);
  118. this.main.watchSelect(registry.byId(this.id + "AdvancedMenu"));
  119. },
  120. resize: function (args) {
  121. this.inherited(arguments);
  122. this.borderContainer.resize();
  123. },
  124. layout: function (args) {
  125. this.inherited(arguments);
  126. },
  127. // Implementation ---
  128. _initGraphControls: function () {
  129. var context = this;
  130. this.global = registry.byId(this.id + "GlobalGraphWidget");
  131. this.main = registry.byId(this.id + "MainGraphWidget");
  132. this.main.onSelectionChanged = function (items) {
  133. context.syncSelectionFrom(context.main);
  134. };
  135. this.main.onLayoutFinished = function () {
  136. // TODO: Could be too expensive ---
  137. //context.wu.setGraphSvg(context.graphName, context.main.svg);
  138. };
  139. this.overview = registry.byId(this.id + "MiniGraphWidget");
  140. this.overview.onSelectionChanged = function (items) {
  141. context.syncSelectionFrom(context.overview);
  142. };
  143. this.overview.onDoubleClick = function (globalID) {
  144. var mainItem = context.main.getItem(globalID);
  145. context.main.centerOnItem(mainItem, true);
  146. };
  147. this.local = registry.byId(this.id + "LocalGraphWidget");
  148. this.local.onSelectionChanged = function (items) {
  149. context.syncSelectionFrom(context.local);
  150. };
  151. this.local.onDoubleClick = function (globalID) {
  152. var mainItem = context.main.getItem(globalID);
  153. context.main.centerOnItem(mainItem, true);
  154. };
  155. },
  156. _initTimings: function () {
  157. this.timingGrid = registry.byId(this.id + "TimingsGrid");
  158. var context = this;
  159. this.timingGrid.onClick = function (items) {
  160. context.syncSelectionFrom(context.timingGrid);
  161. };
  162. this.timingGrid.onDblClick = function (item) {
  163. var subgraphID = item.SubGraphId;
  164. var mainItem = context.main.getItem(subgraphID);
  165. context.main.centerOnItem(mainItem, true);
  166. };
  167. this.timingTreeMap = registry.byId(this.id + "TimingsTreeMap");
  168. this.timingTreeMap.onClick = function (value) {
  169. context.syncSelectionFrom(context.timingTreeMap);
  170. }
  171. this.timingTreeMap.onDblClick = function (value) {
  172. var mainItem = context.main.getItem(value.SubGraphId);
  173. context.main.centerOnItem(mainItem, true);
  174. }
  175. },
  176. _initDialogs: function () {
  177. var context = this;
  178. this.infoDialog = registry.byId(this.id + "InfoDialog");
  179. on(dom.byId(this.id + "InfoDialogCancel"), "click", function (event) {
  180. context.infoDialog.hide();
  181. });
  182. this.xgmmlDialog = registry.byId(this.id + "XGMMLDialog");
  183. this.xgmmlTextArea = registry.byId(this.id + "XGMMLTextArea");
  184. on(dom.byId(this.id + "XGMMLDialogApply"), "click", function (event) {
  185. context.xgmmlDialog.hide();
  186. if (context.xgmmlDialog.get("hpccMode") === "XGMML") {
  187. var xgmml = context.xgmmlTextArea.get("value");
  188. context.loadGraphFromXGMML(xgmml);
  189. } else if (context.xgmmlDialog.get("hpccMode") === "DOT") {
  190. var dot = context.xgmmlTextArea.get("value");
  191. context.loadGraphFromDOT(dot);
  192. } else if (context.xgmmlDialog.get("hpccMode") === "DOTATTRS") {
  193. var dotAttrs = context.xgmmlTextArea.get("value");
  194. context.global.setDotMetaAttributes(dotAttrs);
  195. context.main.setDotMetaAttributes(dotAttrs);
  196. context.overview.setDotMetaAttributes(dotAttrs);
  197. context.local.setDotMetaAttributes(dotAttrs);
  198. context._onMainRefresh();
  199. }
  200. });
  201. on(dom.byId(this.id + "XGMMLDialogCancel"), "click", function (event) {
  202. context.xgmmlDialog.hide();
  203. });
  204. },
  205. _initItemGrid: function (grid) {
  206. var context = this;
  207. grid.on(".dgrid-row:click", function (evt) {
  208. context.syncSelectionFrom(grid);
  209. });
  210. grid.on(".dgrid-row:dblclick", function (evt) {
  211. var item = grid.row(evt).data;
  212. if (item._globalID) {
  213. var mainItem = context.main.getItem(item._globalID);
  214. context.main.centerOnItem(mainItem, true);
  215. }
  216. });
  217. },
  218. _initSubgraphs: function () {
  219. var store = new Memory({
  220. idProperty: "id",
  221. data: []
  222. });
  223. this.subgraphsStore = Observable(store);
  224. this.subgraphsGrid = new declare([OnDemandGrid, Keyboard, Selection, ColumnResizer, DijitRegistry, ESPUtil.GridHelper])({
  225. store: this.subgraphsStore
  226. }, this.id + "SubgraphsGrid");
  227. this._initItemGrid(this.subgraphsGrid);
  228. },
  229. _initVertices: function () {
  230. var store = new Memory({
  231. idProperty: "id",
  232. data: []
  233. });
  234. this.verticesStore = Observable(store);
  235. this.verticesGrid = new declare([OnDemandGrid, Keyboard, Selection, ColumnResizer, DijitRegistry, ESPUtil.GridHelper])({
  236. store: this.verticesStore
  237. }, this.id + "VerticesGrid");
  238. this._initItemGrid(this.verticesGrid);
  239. },
  240. _initEdges: function () {
  241. var store = new Memory({
  242. idProperty: "id",
  243. data: []
  244. });
  245. this.edgesStore = Observable(store);
  246. this.edgesGrid = new declare([OnDemandGrid, Keyboard, Selection, ColumnResizer, DijitRegistry, ESPUtil.GridHelper])({
  247. store: this.edgesStore
  248. }, this.id + "EdgesGrid");
  249. this._initItemGrid(this.edgesGrid);
  250. },
  251. _onMainRefresh: function () {
  252. this.main.setMessage("Performing Layout...");
  253. this.main.startLayout("dot");
  254. },
  255. _onLocalRefresh: function () {
  256. this.refreshLocal(this.local.getSelectionAsGlobalID());
  257. },
  258. _doFind: function (prev) {
  259. if (this.findText != this.findField.value) {
  260. this.findText = this.findField.value;
  261. this.found = this.global.findAsGlobalID(this.findText);
  262. this.global.setSelectedAsGlobalID(this.found);
  263. this.syncSelectionFrom(this.global);
  264. this.foundIndex = -1;
  265. }
  266. this.foundIndex += prev ? -1 : +1;
  267. if (this.foundIndex < 0) {
  268. this.foundIndex = this.found.length - 1;
  269. } else if (this.foundIndex >= this.found.length) {
  270. this.foundIndex = 0;
  271. }
  272. if (this.found.length) {
  273. this.main.centerOnGlobalID(this.found[this.foundIndex], true);
  274. this.local.centerOnGlobalID(this.found[this.foundIndex], true);
  275. }
  276. },
  277. _onFind: function (prev) {
  278. this.findText = "";
  279. this._doFind(false);
  280. },
  281. _onFindNext: function () {
  282. this._doFind(false);
  283. },
  284. _onFindPrevious: function () {
  285. this._doFind(true);
  286. },
  287. _onAbout: function () {
  288. html.set(dom.byId(this.id + "InfoDialogContent"), "<div style='width: 320px; height: 120px; text-align: center;'><p>Version: " + this.main.getVersion() + "</p><p>" + this.main.getResourceLinks() + "</p>");
  289. this.infoDialog.set("title", "About HPCC Systems Graph Control");
  290. this.infoDialog.show();
  291. },
  292. _onGetSVG: function () {
  293. html.set(dom.byId(this.id + "InfoDialogContent"), "<textarea rows='25' cols='80'>" + entities.encode(this.main.getSVG()) + "</textarea>");
  294. this.infoDialog.set("title", "SVG Source");
  295. this.infoDialog.show();
  296. },
  297. _onRenderSVG: function () {
  298. var context = this
  299. this.main.localLayout(function (svg) {
  300. html.set(dom.byId(context.id + "InfoDialogContent"), "<div style='border: 1px inset grey; width: 640px; height: 480px; overflow : auto; '>" + svg + "</div>");
  301. context.infoDialog.set("title", "Rendered SVG");
  302. context.infoDialog.show();
  303. });
  304. },
  305. _onGetXGMML: function () {
  306. this.xgmmlDialog.set("title", "XGMML");
  307. this.xgmmlDialog.set("hpccMode", "XGMML");
  308. this.xgmmlTextArea.set("value", this.main.getXGMML());
  309. this.xgmmlDialog.show();
  310. },
  311. _onEditDOT: function () {
  312. this.xgmmlDialog.set("title", "DOT");
  313. this.xgmmlDialog.set("hpccMode", "DOT");
  314. this.xgmmlTextArea.set("value", this.main.getDOT());
  315. this.xgmmlDialog.show();
  316. },
  317. _onGetGraphAttributes: function () {
  318. this.xgmmlDialog.set("title", "DOT Attributes");
  319. this.xgmmlDialog.set("hpccMode", "DOTATTRS");
  320. this.xgmmlTextArea.set("value", this.global.getDotMetaAttributes());
  321. this.xgmmlDialog.show();
  322. },
  323. _onMainDepthChange: function (value) {
  324. this.refreshMain();
  325. },
  326. _onOverviewDepthChange: function (value) {
  327. this.refreshOverview();
  328. },
  329. _onLocalDepthChange: function (value) {
  330. this._onLocalRefresh();
  331. },
  332. _onLocalDistanceChange: function (value) {
  333. this._onLocalRefresh();
  334. },
  335. init: function (params) {
  336. if (this.initalized) {
  337. return;
  338. }
  339. this.initalized = true;
  340. if (params.Wuid) {
  341. this.graphName = params.GraphName;
  342. this.wu = ESPWorkunit.Get(params.Wuid);
  343. var firstLoad = true;
  344. var context = this;
  345. this.wu.monitor(function () {
  346. context.wu.getInfo({
  347. onGetApplicationValues: function (applicationValues) {
  348. },
  349. onGetGraphs: function (graphs) {
  350. if (firstLoad == true) {
  351. firstLoad = false;
  352. context.loadGraph(context.wu, context.graphName);
  353. } else {
  354. context.refreshGraph(context.wu, context.graphName);
  355. }
  356. }
  357. });
  358. });
  359. }
  360. this.timingGrid.init(lang.mixin({
  361. query: this.graphName
  362. }, params));
  363. this.timingTreeMap.init(lang.mixin({
  364. query: this.graphName
  365. }, params));
  366. },
  367. loadGraphFromXGMML: function (xgmml) {
  368. this.global.loadXGMML(xgmml, false);
  369. this.refreshMain();
  370. this.refreshOverview();
  371. this.loadSubgraphs();
  372. this.loadVertices();
  373. this.loadEdges();
  374. },
  375. loadGraphFromDOT: function (dot) {
  376. this.global.loadDOT(dot);
  377. this.refreshMain();
  378. this.refreshOverview();
  379. this.loadSubgraphs();
  380. this.loadVertices();
  381. this.loadEdges();
  382. },
  383. loadGraph: function (wu, graphName) {
  384. var context = this;
  385. wu.fetchGraphXgmmlByName(graphName, function (xgmml, svg) {
  386. context.loadGraphFromXGMML(xgmml, svg);
  387. });
  388. },
  389. refreshGraph: function (wu, graphName) {
  390. var context = this;
  391. wu.fetchGraphXgmmlByName(graphName, function (xgmml) {
  392. context.main.mergeXGMML(xgmml);
  393. context.loadSubgraphs();
  394. context.loadVertices();
  395. context.loadEdges();
  396. });
  397. },
  398. loadSubgraphs: function () {
  399. var subgraphs = this.main.getSubgraphsWithProperties();
  400. var layoutMap = [];
  401. for (var i = 0; i < subgraphs.length; ++i) {
  402. for (var key in subgraphs[i]) {
  403. if (key != "id" && key.substring(0, 1) != "_") {
  404. layoutMap[key] = true;
  405. }
  406. }
  407. }
  408. var layout = [
  409. { label: "ID", field: "id", width: 50 }
  410. ];
  411. for (var key in layoutMap) {
  412. layout.push({ label: key, field: key, width: 100 });
  413. }
  414. this.subgraphsStore.setData(subgraphs);
  415. this.subgraphsGrid.set("columns", layout);
  416. this.subgraphsGrid.refresh();
  417. },
  418. loadVertices: function () {
  419. var vertices = this.main.getVerticesWithProperties();
  420. var layoutMap = [];
  421. for (var i = 0; i < vertices.length; ++i) {
  422. for (var key in vertices[i]) {
  423. if (key != "id" && key != "ecl" && key != "label" && key.substring(0, 1) != "_") {
  424. layoutMap[key] = true;
  425. }
  426. }
  427. }
  428. var layout = [
  429. { label: "ID", field: "id", width: 50 },
  430. { label: "Label", field: "label", width: 150 }
  431. ];
  432. for (var key in layoutMap) {
  433. layout.push({ label: key, field: key, width: 200 });
  434. }
  435. layout.push({ label: "ECL", field: "ecl", width: 1024 });
  436. this.verticesStore.setData(vertices);
  437. this.verticesGrid.set("columns", layout);
  438. this.verticesGrid.refresh();
  439. },
  440. loadEdges: function () {
  441. var edges = this.main.getEdgesWithProperties();
  442. var layoutMap = [];
  443. for (var i = 0; i < edges.length; ++i) {
  444. for (var key in edges[i]) {
  445. if (key != "id" && key.substring(0, 1) != "_") {
  446. layoutMap[key] = true;
  447. }
  448. }
  449. }
  450. var layout = [
  451. { label: "ID", field: "id", width: 50 }
  452. ];
  453. for (var key in layoutMap) {
  454. layout.push({ label: key, field: key, width: 100 });
  455. }
  456. this.edgesStore.setData(edges);
  457. this.edgesGrid.set("columns", layout);
  458. this.edgesGrid.refresh();
  459. },
  460. syncSelectionFrom: function (sourceControl) {
  461. var selectedGlobalIDs = [];
  462. // Get Selected Items ---
  463. if (sourceControl == this.timingGrid || sourceControl == this.timingTreeMap) {
  464. var items = sourceControl.getSelected();
  465. for (var i = 0; i < items.length; ++i) {
  466. if (items[i].SubGraphId) {
  467. selectedGlobalIDs.push(items[i].SubGraphId);
  468. }
  469. }
  470. } else if (sourceControl == this.verticesGrid || sourceControl == this.edgesGrid || sourceControl == this.subgraphsGrid) {
  471. var items = sourceControl.getSelected();
  472. for (var i = 0; i < items.length; ++i) {
  473. if (items[i]._globalID) {
  474. selectedGlobalIDs.push(items[i]._globalID);
  475. }
  476. }
  477. } else {
  478. selectedGlobalIDs = sourceControl.getSelectionAsGlobalID();
  479. }
  480. // Set Selected Items ---
  481. if (sourceControl != this.timingGrid) {
  482. this.timingGrid.setSelectedAsGlobalID(selectedGlobalIDs);
  483. }
  484. if (sourceControl != this.timingTreeMap) {
  485. this.timingTreeMap.setSelectedAsGlobalID(selectedGlobalIDs);
  486. }
  487. if (sourceControl != this.subgraphsGrid && this.subgraphsGrid.store) {
  488. this.subgraphsGrid.setSelection(selectedGlobalIDs);
  489. }
  490. if (sourceControl != this.verticesGrid && this.verticesGrid.store) {
  491. this.verticesGrid.setSelection(selectedGlobalIDs);
  492. }
  493. if (sourceControl != this.edgesGrid && this.edgesGrid.store) {
  494. this.edgesGrid.setSelection(selectedGlobalIDs);
  495. }
  496. if (sourceControl != this.main) {
  497. this.main.setSelectedAsGlobalID(selectedGlobalIDs);
  498. }
  499. if (sourceControl != this.overview) {
  500. this.overview.setSelectedAsGlobalID(selectedGlobalIDs);
  501. }
  502. if (sourceControl != this.local) {
  503. this.refreshLocal(selectedGlobalIDs);
  504. }
  505. var propertiesDom = dom.byId(this.id + "Properties");
  506. propertiesDom.innerHTML = "";
  507. for (var i = 0; i < selectedGlobalIDs.length; ++i) {
  508. this.global.displayProperties(selectedGlobalIDs[i], propertiesDom);
  509. }
  510. },
  511. resetPage: function () {
  512. this.main.clear();
  513. },
  514. refreshMain: function () {
  515. var xgmml = this.global.getLocalisedXGMML([0], this.mainDepth.get("value"));
  516. this.main.loadXGMML(xgmml);
  517. },
  518. refreshOverview: function () {
  519. var xgmml = this.global.getLocalisedXGMML([0], this.overviewDepth.get("value"));
  520. this.overview.loadXGMML(xgmml);
  521. },
  522. refreshLocal: function (selectedGlobalIDs) {
  523. var globalItems = [];
  524. for (var i = 0; i < selectedGlobalIDs.length; ++i) {
  525. globalItems.push(this.global.getItem(selectedGlobalIDs[i]));
  526. }
  527. var xgmml = this.global.getLocalisedXGMML(globalItems, this.localDepth.get("value"), this.localDistance.get("value"));
  528. this.local.loadXGMML(xgmml);
  529. this.local.setSelectedAsGlobalID(selectedGlobalIDs);
  530. },
  531. displayGraphs: function (graphs) {
  532. for (var i = 0; i < graphs.length; ++i) {
  533. this.wu.fetchGraphXgmml(i, function (xgmml) {
  534. this.main.loadXGMML(xgmml, true);
  535. });
  536. }
  537. }
  538. });
  539. });