GraphPageWidget.js 23 KB

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