GraphWidget.js 19 KB

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