GraphPageWidget.js 18 KB

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