GraphWidget.js 24 KB

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