GraphWidget.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  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. require([
  17. "dojo/_base/declare",
  18. "dojo/aspect",
  19. "dojo/has",
  20. "dojo/dom",
  21. "dojo/dom-construct",
  22. "dojo/dom-class",
  23. "dijit/layout/_LayoutWidget",
  24. "dijit/_TemplatedMixin",
  25. "dijit/_WidgetsInTemplateMixin",
  26. "dijit/layout/BorderContainer",
  27. "dijit/layout/ContentPane",
  28. "dojo/text!./templates/GraphWidget.html",
  29. "dijit/Toolbar", "dijit/ToolbarSeparator", "dijit/form/Button"
  30. ],
  31. function (declare, aspect, has, dom, domConstruct, domClass,
  32. _LayoutWidget, _TemplatedMixin, _WidgetsInTemplateMixin, BorderContainer, ContentPane,
  33. template) {
  34. return declare("GraphWidget", [_LayoutWidget, _TemplatedMixin, _WidgetsInTemplateMixin], {
  35. templateString: template,
  36. baseClass: "GraphWidget",
  37. borderContainer: null,
  38. graphContentPane: null,
  39. _isPluginInstalled: false,
  40. _plugin: null,
  41. eventsRegistered: false,
  42. xgmml: "",
  43. dot: "",
  44. svg: "",
  45. // Known control properties ---
  46. DOT_META_ATTR: "DOT_META_ATTR",
  47. _onClickRefresh: function () {
  48. this.setMessage("Performing Layout...");
  49. this.startLayout("dot");
  50. },
  51. _onClickZoomOrig: function (args) {
  52. this.setScale(100);
  53. this.centerOnItem(0);
  54. },
  55. _onClickZoomAll: function (args) {
  56. this.centerOnItem(0, true);
  57. },
  58. _onClickZoomWidth: function (args) {
  59. this.centerOnItem(0, true, true);
  60. },
  61. onSelectionChanged: function (items) {
  62. },
  63. onDoubleClick: function (globalID) {
  64. },
  65. onLayoutFinished: function () {
  66. },
  67. buildRendering: function (args) {
  68. this.inherited(arguments);
  69. },
  70. postCreate: function (args) {
  71. this.inherited(arguments);
  72. this.borderContainer = dijit.byId(this.id + "BorderContainer");
  73. this.graphContentPane = dijit.byId(this.id + "GraphContentPane");
  74. },
  75. startup: function (args) {
  76. this.inherited(arguments);
  77. this._isPluginInstalled = this.isPluginInstalled();
  78. this.createPlugin();
  79. this.watchStyleChange();
  80. },
  81. resize: function (args) {
  82. this.inherited(arguments);
  83. this.borderContainer.resize();
  84. },
  85. layout: function (args) {
  86. this.inherited(arguments);
  87. },
  88. // Plugin wrapper ---
  89. clear: function () {
  90. if (this._plugin) {
  91. this.xgmml = "";
  92. this.svg = "";
  93. this.dot = "";
  94. this._plugin.clear();
  95. }
  96. },
  97. loadXGMML: function (xgmml, merge) {
  98. this.registerEvents();
  99. if (this._plugin && this.xgmml != xgmml) {
  100. this.setMessage("Loading Data...");
  101. if (merge)
  102. this._plugin.mergeXGMML(xgmml);
  103. else
  104. this._plugin.loadXGMML(xgmml);
  105. this.setMessage("Performing Layout...");
  106. this._plugin.startLayout("dot");
  107. this.xgmml = xgmml;
  108. }
  109. },
  110. mergeXGMML: function (xgmml) {
  111. this.registerEvents();
  112. if (this._plugin && this.xgmml != xgmml) {
  113. this._plugin.mergeXGMML(xgmml);
  114. this.xgmml = xgmml;
  115. }
  116. },
  117. loadDOT: function (dot) {
  118. this.registerEvents();
  119. this.load(dot, "dot");
  120. },
  121. load: function (dot, layout) {
  122. this.registerEvents();
  123. if (this._plugin && this.xgmml != xgmml) {
  124. this.setMessage("Loading Data...");
  125. this._plugin.loadDOT(dot);
  126. this.setMessage("Performing Layout...");
  127. this._plugin.startLayout(layout);
  128. this.xgmml = xgmml;
  129. }
  130. },
  131. setLayout: function (layout) {
  132. if (this._plugin) {
  133. this.setMessage("Performing Layout...");
  134. this._plugin.startLayout(layout);
  135. }
  136. },
  137. centerOn: function (globalID) {
  138. if (this._plugin) {
  139. var item = this._plugin.getItem(globalID);
  140. this._plugin.centerOnItem(item, true);
  141. var items = [item];
  142. this._plugin.setSelected(items, true);
  143. }
  144. },
  145. getVersion: function () {
  146. if (this._plugin) {
  147. return this._plugin.version;
  148. }
  149. return "";
  150. },
  151. getSVG: function () {
  152. return this._plugin.getSVG();
  153. },
  154. getXGMML: function () {
  155. return this.xgmml;
  156. },
  157. localLayout: function(callback) {
  158. var context = this;
  159. require(
  160. ["hpcc/viz"],
  161. function (viz) {
  162. callback(Viz(context.dot, "svg"));
  163. }
  164. );
  165. },
  166. displayProperties: function (globalID, place) {
  167. if (this._plugin) {
  168. var item = this._plugin.getItem(globalID);
  169. var props = this._plugin.getProperties(item);
  170. if (props.id) {
  171. var table = domConstruct.create("h3", {
  172. innerHTML: props.id,
  173. align: "center"
  174. }, place);
  175. delete props.id;
  176. }
  177. if (props.count) {
  178. var table = domConstruct.create("table", { border: 1, cellspacing: 0, width: "100%" }, place);
  179. var tr = domConstruct.create("tr", null, table);
  180. var td = domConstruct.create("td", { innerHTML: "Count" }, tr);
  181. var td = domConstruct.create("td", {
  182. align: "right",
  183. innerHTML: props.count
  184. }, tr);
  185. delete props.count;
  186. domConstruct.create("br", null, place);
  187. }
  188. if (props.max) {
  189. var table = domConstruct.create("table", { border: 1, cellspacing: 0, width: "100%" }, place);
  190. var tr = domConstruct.create("tr", null, table);
  191. domConstruct.create("th", { innerHTML: " " }, tr);
  192. domConstruct.create("th", { innerHTML: "Skew" }, tr);
  193. domConstruct.create("th", { innerHTML: "Node" }, tr);
  194. domConstruct.create("th", { innerHTML: "Rows" }, tr);
  195. tr = domConstruct.create("tr", null, table);
  196. domConstruct.create("td", { innerHTML: "Max" }, tr);
  197. domConstruct.create("td", { innerHTML: props.maxskew }, tr);
  198. domConstruct.create("td", { innerHTML: props.maxEndpoint }, tr);
  199. domConstruct.create("td", { innerHTML: props.max }, tr);
  200. tr = domConstruct.create("tr", null, table);
  201. domConstruct.create("td", { innerHTML: "Min" }, tr);
  202. domConstruct.create("td", { innerHTML: props.minskew }, tr);
  203. domConstruct.create("td", { innerHTML: props.minEndpoint }, tr);
  204. domConstruct.create("td", { innerHTML: props.min }, tr);
  205. delete props.maxskew;
  206. delete props.maxEndpoint;
  207. delete props.max;
  208. delete props.minskew;
  209. delete props.minEndpoint;
  210. delete props.min;
  211. domConstruct.create("br", null, place);
  212. }
  213. if (props.slaves) {
  214. var table = domConstruct.create("table", { border: 1, cellspacing: 0, width: "100%" }, place);
  215. var tr = domConstruct.create("tr", null, table);
  216. domConstruct.create("th", { innerHTML: "Slaves" }, tr);
  217. domConstruct.create("th", { innerHTML: "Started" }, tr);
  218. domConstruct.create("th", { innerHTML: "Stopped" }, tr);
  219. tr = domConstruct.create("tr", null, table);
  220. domConstruct.create("td", { innerHTML: props.slaves }, tr);
  221. domConstruct.create("td", { innerHTML: props.started }, tr);
  222. domConstruct.create("td", { innerHTML: props.stopped }, tr);
  223. delete props.slaves;
  224. delete props.started;
  225. delete props.stopped;
  226. domConstruct.create("br", null, place);
  227. }
  228. var first = true;
  229. var table = {};
  230. var tr = {};
  231. for (key in props) {
  232. if (key[0] == "_")
  233. continue;
  234. if (first) {
  235. first = false;
  236. table = domConstruct.create("table", { border: 1, cellspacing: 0, width: "100%" }, place);
  237. tr = domConstruct.create("tr", null, table);
  238. domConstruct.create("th", { innerHTML: "Property" }, tr);
  239. domConstruct.create("th", { innerHTML: "Value" }, tr);
  240. }
  241. tr = domConstruct.create("tr", null, table);
  242. domConstruct.create("td", { innerHTML: key }, tr);
  243. domConstruct.create("td", { innerHTML: props[key] }, tr);
  244. }
  245. if (first == false) {
  246. domConstruct.create("br", null, place);
  247. }
  248. }
  249. },
  250. isPluginInstalled: function () {
  251. if (has("ie")) {
  252. try {
  253. var o = new ActiveXObject("HPCCSystems.HPCCSystemsGraphViewControl.1");
  254. o = null;
  255. return true;
  256. } catch (e) { }
  257. return false;
  258. } else {
  259. for (var i = 0, p = navigator.plugins, l = p.length; i < l; i++) {
  260. if (p[i].name.indexOf("HPCCSystemsGraphViewControl") > -1) {
  261. return true;
  262. }
  263. }
  264. return false;
  265. }
  266. },
  267. createPlugin: function () {
  268. if (this._plugin == null) {
  269. if (this._isPluginInstalled) {
  270. var pluginID = this.id + "Plugin";
  271. if (has("ie")) {
  272. this.graphContentPane.domNode.innerHTML = '<object type="application/x-hpccsystemsgraphviewcontrol" '
  273. + 'id="' + pluginID + '" '
  274. + 'name="' + pluginID + '" '
  275. + 'width="100%" '
  276. + 'height="100%">'
  277. + '</object>';
  278. } else {
  279. this.graphContentPane.domNode.innerHTML = '<embed type="application/x-hpccsystemsgraphviewcontrol" '
  280. + 'id="' + pluginID + '" '
  281. + 'name="' + pluginID + '" '
  282. + 'width="100%" '
  283. + 'height="100%">'
  284. + '</embed>';
  285. }
  286. this._plugin = dom.byId(pluginID);
  287. var context = this;
  288. } else {
  289. domConstruct.create("div", {
  290. innerHTML: "<h4>Graph View</h4>" +
  291. "<p>To enable graph views, please install the Graph View Control plugin:</p>" +
  292. this.getResourceLinks()
  293. }, this.graphContentPane.domNode);
  294. }
  295. }
  296. },
  297. getResourceLinks: function () {
  298. return "<a href=\"http://hpccsystems.com/download/free-community-edition/graph-control\" target=\"_blank\">Binary Installs</a><br/>" +
  299. "<a href=\"https://github.com/hpcc-systems/GraphControl\" target=\"_blank\">Source Code</a><br/><br/>" +
  300. "<a href=\"http://hpccsystems.com\" target=\"_blank\">HPCC Systems</a>"
  301. },
  302. setMessage: function (message) {
  303. if (this._plugin) {
  304. return this._plugin.setMessage(message);
  305. }
  306. return null;
  307. },
  308. getLocalisedXGMML: function (items, depth, distance) {
  309. if (this._plugin) {
  310. return this._plugin.getLocalisedXGMML(items, depth, distance);
  311. }
  312. return null;
  313. },
  314. mergeSVG: function (svg) {
  315. if (this._plugin) {
  316. return this._plugin.mergeSVG(svg);
  317. }
  318. return null;
  319. },
  320. startLayout: function (layout) {
  321. if (this._plugin) {
  322. return this._plugin.startLayout(layout);
  323. }
  324. return null;
  325. },
  326. find: function (findText) {
  327. if (this._plugin) {
  328. return this._plugin.find(findText);
  329. }
  330. return [];
  331. },
  332. findAsGlobalID: function (findText) {
  333. if (this._plugin) {
  334. var items = this.find(findText);
  335. var globalIDs = [];
  336. for (var i = 0; i < items.length; ++i) {
  337. globalIDs.push(this._plugin.getGlobalID(items[i]));
  338. }
  339. return globalIDs;
  340. }
  341. return [];
  342. },
  343. setScale: function(percent) {
  344. if (this._plugin) {
  345. return this._plugin.setScale(percent);
  346. }
  347. return 100;
  348. },
  349. centerOnItem: function (item, scaleToFit, widthOnly) {
  350. if (this._plugin) {
  351. return this._plugin.centerOnItem(item, scaleToFit, widthOnly);
  352. }
  353. return null;
  354. },
  355. centerOnGlobalID: function (globalID, scaleToFit, widthOnly) {
  356. if (this._plugin) {
  357. var item = this._plugin.getItem(globalID);
  358. if (item) {
  359. return this.centerOnItem(item, scaleToFit, widthOnly);
  360. }
  361. }
  362. return null;
  363. },
  364. setSelected: function (items) {
  365. if (this._plugin) {
  366. return this._plugin.setSelected(items);
  367. }
  368. return null;
  369. },
  370. setSelectedAsGlobalID: function (items) {
  371. if (this._plugin) {
  372. return this._plugin.setSelectedAsGlobalID(items);
  373. }
  374. return null;
  375. },
  376. getSelection: function () {
  377. if (this._plugin) {
  378. return this._plugin.getSelection();
  379. }
  380. return [];
  381. },
  382. getSelectionAsGlobalID: function () {
  383. if (this._plugin) {
  384. return this._plugin.getSelectionAsGlobalID();
  385. }
  386. return [];
  387. },
  388. getItem: function (globalID) {
  389. if (this._plugin) {
  390. return this._plugin.getItem(globalID);
  391. }
  392. return null;
  393. },
  394. hide: function () {
  395. if (this._plugin) {
  396. dojo.style(this._plugin, "width", "1px");
  397. dojo.style(this._plugin, "height", "1px");
  398. }
  399. },
  400. show: function () {
  401. if (this._plugin) {
  402. dojo.style(this._plugin, "width", "100%");
  403. dojo.style(this._plugin, "height", "100%");
  404. }
  405. },
  406. watchSplitter: function (splitter) {
  407. if (has("chrome")) {
  408. // Chrome can ignore splitter events
  409. return;
  410. }
  411. var context = this;
  412. dojo.connect(splitter, "_startDrag", function () {
  413. context.hide();
  414. });
  415. dojo.connect(splitter, "_stopDrag", function (evt) {
  416. context.show();
  417. });
  418. },
  419. watchSelect: function (select) {
  420. if (select) {
  421. // Only chrome needs to monitor select drop downs.
  422. var context = this;
  423. select.watch("_opened", function () {
  424. if (select._opened) {
  425. context.hide();
  426. } else {
  427. context.show();
  428. }
  429. });
  430. }
  431. },
  432. watchStyleChange: function () {
  433. // When chrome hides the plugin it destroys it. To prevent this it is just made very small.
  434. if (has("chrome")) {
  435. var watchList = [];
  436. var context = this;
  437. var domNode = this.domNode;
  438. // There are many places that may cause the plugin to be hidden, the possible places are calculated by walking the hierarchy upwards.
  439. while (domNode) {
  440. if (domNode.id) {
  441. watchList[domNode.id] = false;
  442. }
  443. domNode = domNode.parentElement;
  444. }
  445. // Hijack the dojo style class replacement call and monitor for elements in our watchList.
  446. aspect.around(domClass, "replace", function (origFunc) {
  447. return function (node, addStyle, removeStyle) {
  448. if (node.firstChild && (node.firstChild.id in watchList)) {
  449. if (addStyle == "dijitHidden") {
  450. watchList[node.firstChild.id] = true;
  451. dojo.style(node, "width", "1px");
  452. dojo.style(node, "height", "1px");
  453. dojo.style(node.firstChild, "width", "1px");
  454. dojo.style(node.firstChild, "height", "1px");
  455. return;
  456. } else if (addStyle == "dijitVisible" && watchList[node.firstChild.id] == true) {
  457. watchList[node.firstChild.id] = false;
  458. dojo.style(node, "width", "100%");
  459. dojo.style(node, "height", "100%");
  460. dojo.style(node.firstChild, "width", "100%");
  461. dojo.style(node.firstChild, "height", "100%");
  462. return;
  463. }
  464. }
  465. return origFunc(node, addStyle, removeStyle);
  466. }
  467. });
  468. }
  469. },
  470. getDotMetaAttributes: function () {
  471. if (this._plugin && this._plugin.getControlProperty) {
  472. return this._plugin.getControlProperty(this.DOT_META_ATTR);
  473. }
  474. return "";
  475. },
  476. setDotMetaAttributes: function (dotMetaAttr) {
  477. if (this._plugin && this._plugin.setControlProperty) {
  478. this._plugin.setControlProperty(this.DOT_META_ATTR, dotMetaAttr);
  479. }
  480. },
  481. getProperty: function (item, key) {
  482. if (this._plugin && this._plugin.getProperty) {
  483. return this._plugin.getProperty(item, key);
  484. }
  485. return "";
  486. },
  487. setProperty: function (item, key, value) {
  488. if (this._plugin && this._plugin.setProperty) {
  489. this._plugin.setProperty(item, key, value);
  490. }
  491. },
  492. getProperties: function (item) {
  493. if (this._plugin) {
  494. return this._plugin.getProperties(item);
  495. }
  496. return [];
  497. },
  498. getSubgraphsWithProperties: function () {
  499. if (this._plugin) {
  500. return this._plugin.getSubgraphsWithProperties();
  501. }
  502. return [];
  503. },
  504. getVerticesWithProperties: function () {
  505. if (this._plugin) {
  506. return this._plugin.getVerticesWithProperties();
  507. }
  508. return [];
  509. },
  510. getEdgesWithProperties: function () {
  511. if (this._plugin) {
  512. return this._plugin.getEdgesWithProperties();
  513. }
  514. return [];
  515. },
  516. registerEvents: function () {
  517. if (!this.eventsRegistered) {
  518. this.eventsRegistered = true;
  519. var context = this;
  520. this.registerEvent("MouseDoubleClick", function (item) {
  521. context._plugin.centerOnItem(item, true);
  522. context.onDoubleClick(context._plugin.getGlobalID(item));
  523. });
  524. this.registerEvent("LayoutFinished", function () {
  525. context._plugin.centerOnItem(0, true);
  526. context.setMessage('');
  527. context.dot = context._plugin.getDOT();
  528. context.svg = context._plugin.getSVG();
  529. context.onLayoutFinished();
  530. });
  531. this.registerEvent("SelectionChanged", function (items) {
  532. context.onSelectionChanged(items);
  533. });
  534. }
  535. },
  536. registerEvent: function (evt, func) {
  537. if (this._plugin) {
  538. if (this._plugin.attachEvent) {
  539. return this._plugin.attachEvent("on" + evt, func);
  540. } else {
  541. return this._plugin.addEventListener(evt, func, false);
  542. }
  543. }
  544. return false;
  545. }
  546. });
  547. });