GraphPageWidget.js 17 KB

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