GraphPageWidget.js 19 KB

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