GraphPageWidget.js 21 KB

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