GraphWidget.js 21 KB

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