GraphWidget.js 22 KB

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