GraphWidget.js 24 KB

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