GraphPageWidget.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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/sniff",
  19. "dojo/_base/array",
  20. "dojo/dom",
  21. "dojo/dom-construct",
  22. "dojo/on",
  23. "dojo/store/Memory",
  24. "dojo/data/ObjectStore",
  25. "dijit/layout/_LayoutWidget",
  26. "dijit/_TemplatedMixin",
  27. "dijit/_WidgetsInTemplateMixin",
  28. "dijit/layout/BorderContainer",
  29. "dijit/layout/TabContainer",
  30. "dijit/layout/ContentPane",
  31. "dijit/registry",
  32. "dijit/Dialog",
  33. "dojox/grid/DataGrid",
  34. "hpcc/GraphWidget",
  35. "hpcc/ESPWorkunit",
  36. "hpcc/TimingGridWidget",
  37. "hpcc/TimingTreeMapWidget",
  38. "dojo/text!../templates/GraphPageWidget.html",
  39. "dijit/form/Select",
  40. "dijit/form/TextBox"
  41. ], function (declare, sniff, array, dom, domConstruct, on, Memory, ObjectStore,
  42. _LayoutWidget, _TemplatedMixin, _WidgetsInTemplateMixin, BorderContainer, TabContainer, ContentPane, registry, Dialog,
  43. DataGrid, GraphWidget, ESPWorkunit, TimingGridWidget, TimingTreeMapWidget,
  44. template) {
  45. return declare("GraphPageWidget", [_LayoutWidget, _TemplatedMixin, _WidgetsInTemplateMixin], {
  46. templateString: template,
  47. baseClass: "GraphPageWidget",
  48. borderContainer: null,
  49. rightBorderContainer: null,
  50. graphName: "",
  51. wu: null,
  52. editorControl: null,
  53. main: null,
  54. overview: null,
  55. local: null,
  56. graphSelect: null,
  57. timingGrid: null,
  58. timingTreeMap: null,
  59. verticesGrid: null,
  60. edgesGrid: null,
  61. findField: null,
  62. findText: "",
  63. found: [],
  64. foundIndex: 0,
  65. buildRendering: function (args) {
  66. this.inherited(arguments);
  67. },
  68. postCreate: function (args) {
  69. this.inherited(arguments);
  70. this._initControls();
  71. },
  72. startup: function (args) {
  73. this.inherited(arguments);
  74. var splitter = this.borderContainer.getSplitter("right");
  75. this.main.watchSplitter(splitter);
  76. this.overview.watchSplitter(splitter);
  77. this.local.watchSplitter(splitter);
  78. splitter = this.rightBorderContainer.getSplitter("bottom");
  79. this.main.watchSplitter(splitter);
  80. this.overview.watchSplitter(splitter);
  81. this.local.watchSplitter(splitter);
  82. this.main.watchSelect(this.graphSelect);
  83. this.overview.watchSelect(this.graphSelect);
  84. this.local.watchSelect(this.graphSelect);
  85. this.overview.watchStyleChange();
  86. this.local.watchStyleChange();
  87. },
  88. resize: function (args) {
  89. this.inherited(arguments);
  90. this.borderContainer.resize();
  91. },
  92. layout: function (args) {
  93. this.inherited(arguments);
  94. },
  95. // Implementation ---
  96. _initControls: function () {
  97. this.borderContainer = registry.byId(this.id + "BorderContainer");
  98. this.rightBorderContainer = registry.byId(this.id + "RightBorderContainer");
  99. this.findField = registry.byId(this.id + "FindField");
  100. this._initGraphControls();
  101. this._initGraphSelect()
  102. this._initTimings();
  103. this._initVertices();
  104. this._initEdges();
  105. },
  106. _initGraphControls: function () {
  107. var context = this;
  108. this.main = registry.byId(this.id + "MainGraphWidget");
  109. this.main.onSelectionChanged = function (items) {
  110. context.syncSelectionFrom(context.main);
  111. };
  112. this.main.onLayoutFinished = function () {
  113. context.wu.setGraphSvg(context.graphName, context.main.svg);
  114. };
  115. this.overview = registry.byId(this.id + "MiniGraphWidget");
  116. this.overview.onSelectionChanged = function (items) {
  117. context.syncSelectionFrom(context.overview);
  118. };
  119. this.overview.onDoubleClick = function (globalID) {
  120. var mainItem = context.main.getItem(globalID);
  121. context.main.centerOnItem(mainItem, true);
  122. };
  123. this.local = registry.byId(this.id + "LocalGraphWidget");
  124. this.local.onSelectionChanged = function (items) {
  125. context.syncSelectionFrom(context.local);
  126. };
  127. this.local.onDoubleClick = function (globalID) {
  128. var mainItem = context.main.getItem(globalID);
  129. context.main.centerOnItem(mainItem, true);
  130. };
  131. },
  132. _initGraphSelect: function () {
  133. this.graphSelect = registry.byId(this.id + "GraphSelect");
  134. var context = this;
  135. this.graphSelect.onChange = function () {
  136. context.graphName = this.getValue();
  137. context.loadGraph(context.wu, context.graphName);
  138. context.timingGrid.setQuery(context.graphName);
  139. context.timingTreeMap.setQuery(context.graphName);
  140. }
  141. },
  142. _initTimings: function () {
  143. this.timingGrid = registry.byId(this.id + "TimingsGrid");
  144. var context = this;
  145. this.timingGrid.onClick = function (items) {
  146. context.syncSelectionFrom(context.timingGrid);
  147. };
  148. this.timingGrid.onDblClick = function (item) {
  149. var subgraphID = item.SubGraphId;
  150. var mainItem = context.main.getItem(subgraphID);
  151. context.main.centerOnItem(mainItem, true);
  152. };
  153. this.timingTreeMap = registry.byId(this.id + "TimingsTreeMap");
  154. this.timingTreeMap.onClick = function (value) {
  155. context.syncSelectionFrom(context.timingTreeMap);
  156. }
  157. this.timingTreeMap.onDblClick = function (value) {
  158. var mainItem = context.main.getItem(value.subGraphId);
  159. context.main.centerOnItem(mainItem, true);
  160. }
  161. },
  162. _initItemGrid: function (grid) {
  163. var context = this;
  164. grid.on("RowClick", function (evt) {
  165. context.syncSelectionFrom(grid);
  166. }, true);
  167. var context = this;
  168. grid.on("RowDblClick", function (evt) {
  169. var idx = evt.rowIndex;
  170. var item = this.getItem(idx);
  171. if (this.store.getValue(item, "_globalID")) {
  172. var globalID = this.store.getValue(item, "_globalID");
  173. var mainItem = context.main.getItem(globalID);
  174. context.main.centerOnItem(mainItem, true);
  175. }
  176. }, true);
  177. },
  178. _initVertices: function () {
  179. this.verticesGrid = registry.byId(this.id + "VerticesGrid");
  180. this._initItemGrid(this.verticesGrid);
  181. },
  182. _initEdges: function () {
  183. this.edgesGrid = registry.byId(this.id + "EdgesGrid");
  184. this._initItemGrid(this.edgesGrid);
  185. },
  186. _initProperties: function () {
  187. this.propertyGrid = new DataGrid({
  188. //store: objStore,
  189. query: { id: "*" },
  190. structure: [
  191. { name: "Property", field: "key", width: "auto" },
  192. { name: "Value", field: "value", width: "auto" }
  193. ]
  194. });
  195. this.propertyGrid.placeAt(this.id + "Properties");
  196. this.propertyGrid.startup();
  197. },
  198. _onLayout: function () {
  199. this.main.setMessage("Performing Layout...");
  200. this.main.startLayout("dot");
  201. },
  202. _onLocalSync: function () {
  203. this.syncSelectionFrom(this.main);
  204. },
  205. _doFind: function (prev) {
  206. if (this.findText != this.findField.value) {
  207. this.findText = this.findField.value;
  208. this.found = this.main.find(this.findText);
  209. this.main.setSelected(this.found);
  210. this.syncSelectionFrom(this.main);
  211. this.foundIndex = -1;
  212. }
  213. this.foundIndex += prev ? -1 : +1;
  214. if (this.foundIndex < 0) {
  215. this.foundIndex = this.found.length - 1;
  216. } else if (this.foundIndex >= this.found.length) {
  217. this.foundIndex = 0;
  218. }
  219. if (this.found.length) {
  220. this.main.centerOnItem(this.found[this.foundIndex], true);
  221. }
  222. },
  223. _onFind: function (prev) {
  224. this.findText = "";
  225. this._doFind(false);
  226. },
  227. _onFindNext: function () {
  228. this._doFind(false);
  229. },
  230. _onFindPrevious: function () {
  231. this._doFind(true);
  232. },
  233. init: function (params) {
  234. this.graphName = params.GraphName;
  235. this.wu = new ESPWorkunit({
  236. wuid: params.Wuid
  237. });
  238. var firstLoad = true;
  239. var context = this;
  240. this.wu.monitor(function () {
  241. context.wu.getInfo({
  242. onGetApplicationValues: function (applicationValues) {
  243. },
  244. onGetGraphs: function (graphs) {
  245. if (firstLoad == true) {
  246. firstLoad = false;
  247. context.loadGraphSelect(graphs);
  248. } else {
  249. context.refreshGraph(context.wu, context.graphName);
  250. }
  251. }
  252. });
  253. });
  254. this.timingGrid.init(params);
  255. this.timingGrid.setQuery(this.graphName);
  256. this.timingTreeMap.init(params);
  257. },
  258. loadGraphSelect: function (graphs) {
  259. this.graphSelect.options = [];
  260. for (var i = 0; i < graphs.length; ++i) {
  261. if (!this.graphName) {
  262. this.graphName = graphs[i].Name;
  263. }
  264. this.graphSelect.options.push({
  265. label: graphs[i].Name + (graphs[i].Time ? " (" + graphs[i].Time + ")" : ""),
  266. value: graphs[i].Name
  267. });
  268. }
  269. this.graphSelect.setValue(this.graphName);
  270. },
  271. loadGraph: function (wu, graphName) {
  272. var context = this;
  273. wu.fetchGraphXgmmlByName(graphName, function (xgmml, svg) {
  274. context.main.setMessage("Loading Data...");
  275. context.main.loadXGMML(xgmml);
  276. context.overview.loadXGMML(context.main.getLocalisedXGMML([0]));
  277. context.loadVertices();
  278. context.loadEdges();
  279. if (svg) {
  280. context.main.setMessage("Loading Layout...");
  281. if (context.main.mergeSVG(svg)) {
  282. context.main.centerOnItem(0, true);
  283. context.main.setMessage("");
  284. return;
  285. }
  286. }
  287. context.main.setMessage("Performing Layout...");
  288. context.main.startLayout("dot");
  289. });
  290. },
  291. refreshGraph: function (wu, graphName) {
  292. var context = this;
  293. wu.fetchGraphXgmmlByName(graphName, function (xgmml) {
  294. context.main.mergeXGMML(xgmml);
  295. context.loadVertices();
  296. context.loadEdges();
  297. });
  298. },
  299. loadVertices: function () {
  300. var vertices = this.main.plugin.getVerticesWithProperties();
  301. var layoutMap = [];
  302. for (var i = 0; i < vertices.length; ++i) {
  303. for (var key in vertices[i]) {
  304. if (key != "id" && key != "ecl" && key != "label" && key.substring(0, 1) != "_") {
  305. layoutMap[key] = true;
  306. }
  307. }
  308. }
  309. var layout = [[
  310. { 'name': 'ID', 'field': 'id', 'width': '50px' },
  311. { 'name': 'Label', 'field': 'label', 'width': '150px' }
  312. ]];
  313. for (var key in layoutMap) {
  314. layout[0].push({ 'name': key, 'field': key, 'width': '200px' });
  315. }
  316. layout[0].push({ 'name': "ECL", 'field': "ecl", 'width': '1024px' });
  317. var store = new Memory({ data: vertices });
  318. var dataStore = new ObjectStore({ objectStore: store });
  319. this.verticesGrid.setStructure(layout);
  320. this.verticesGrid.setStore(dataStore);
  321. this.verticesGrid.setQuery({
  322. id: "*"
  323. });
  324. },
  325. loadEdges: function () {
  326. var edges = this.main.plugin.getEdgesWithProperties();
  327. var layoutMap = [];
  328. for (var i = 0; i < edges.length; ++i) {
  329. for (var key in edges[i]) {
  330. if (key != "id" && key.substring(0, 1) != "_") {
  331. layoutMap[key] = true;
  332. }
  333. }
  334. }
  335. var layout = [[
  336. { 'name': 'ID', 'field': 'id', 'width': '50px' }
  337. ]];
  338. for (var key in layoutMap) {
  339. layout[0].push({ 'name': key, 'field': key, 'width': '100px' });
  340. }
  341. var store = new Memory({ data: edges });
  342. var dataStore = new ObjectStore({ objectStore: store });
  343. this.edgesGrid.setStructure(layout);
  344. this.edgesGrid.setStore(dataStore);
  345. this.edgesGrid.setQuery({
  346. id: "*"
  347. });
  348. },
  349. syncSelectionFrom: function (sourceControl) {
  350. var selItems = [];
  351. // Get Selected Items ---
  352. if (sourceControl == this.timingGrid || sourceControl == this.timingTreeMap) {
  353. var items = sourceControl.getSelected();
  354. for (var i = 0; i < items.length; ++i) {
  355. if (items[i].SubGraphId) {
  356. selItems.push(items[i].SubGraphId);
  357. }
  358. }
  359. } else if (sourceControl == this.verticesGrid || sourceControl == this.edgesGrid) {
  360. var items = sourceControl.selection.getSelected();
  361. for (var i = 0; i < items.length; ++i) {
  362. if (items[i]._globalID) {
  363. selItems.push(items[i]._globalID);
  364. }
  365. }
  366. } else {
  367. selItems = sourceControl.getSelectionAsGlobalID();
  368. }
  369. // Set Selected Items ---
  370. if (sourceControl != this.timingGrid) {
  371. this.timingGrid.setSelected(selItems);
  372. }
  373. if (sourceControl != this.timingTreeMap) {
  374. this.timingTreeMap.setSelected(selItems);
  375. }
  376. if (sourceControl != this.verticesGrid && this.verticesGrid.store) {
  377. for (var i = 0; i < this.verticesGrid.rowCount; ++i) {
  378. var row = this.verticesGrid.getItem(i);
  379. this.verticesGrid.selection.setSelected(i, (row._globalID && array.indexOf(selItems, row._globalID) != -1));
  380. }
  381. }
  382. if (sourceControl != this.edgesGrid && this.edgesGrid.store) {
  383. for (var i = 0; i < this.edgesGrid.rowCount; ++i) {
  384. var row = this.edgesGrid.getItem(i);
  385. this.edgesGrid.selection.setSelected(i, (row._globalID && array.indexOf(selItems, row._globalID) != -1));
  386. }
  387. }
  388. if (sourceControl != this.main) {
  389. this.main.setSelectedAsGlobalID(selItems);
  390. }
  391. if (sourceControl != this.overview) {
  392. this.overview.setSelectedAsGlobalID(selItems);
  393. }
  394. var mainItems = [];
  395. for (var i = 0; i < selItems.length; ++i) {
  396. mainItems.push(this.main.getItem(selItems[i]));
  397. }
  398. if (sourceControl != this.local) {
  399. var xgmml = this.main.getLocalisedXGMML(mainItems, 2);
  400. this.local.loadXGMML(xgmml);
  401. this.local.setSelectedAsGlobalID(selItems);
  402. }
  403. var propertiesDom = dom.byId(this.id + "Properties");
  404. propertiesDom.innerHTML = "";
  405. for (var i = 0; i < mainItems.length; ++i) {
  406. this.main.displayProperties(mainItems[i], propertiesDom);
  407. }
  408. },
  409. showHelpAbout: function () {
  410. myDialog = new Dialog({
  411. title: "About Graph sourceControl",
  412. content: "Version: " + main.getVersion(),
  413. style: "width: 300px"
  414. });
  415. myDialog.show();
  416. },
  417. resetPage: function () {
  418. this.main.clear();
  419. },
  420. displayGraphs: function (graphs) {
  421. for (var i = 0; i < graphs.length; ++i) {
  422. this.wu.fetchGraphXgmml(i, function (xgmml) {
  423. this.main.loadXGMML(xgmml, true);
  424. });
  425. }
  426. }
  427. });
  428. });