GraphPageWidget.js 18 KB

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