GraphWidget.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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. },
  70. resize: function (args) {
  71. this.inherited(arguments);
  72. this.borderContainer.resize();
  73. },
  74. layout: function (args) {
  75. this.inherited(arguments);
  76. },
  77. // Plugin wrapper ---
  78. clear: function () {
  79. if (this.plugin) {
  80. this.plugin.clear();
  81. }
  82. },
  83. loadXGMML: function (xgmml, merge) {
  84. if (this.plugin && this.xgmml != xgmml) {
  85. this.setMessage("Loading Data...");
  86. if (merge)
  87. this.plugin.mergeXGMML(xgmml);
  88. else
  89. this.plugin.loadXGMML(xgmml);
  90. this.setMessage("Performing Layout...");
  91. this.plugin.startLayout("dot");
  92. this.xgmml = xgmml;
  93. }
  94. },
  95. mergeXGMML: function (xgmml) {
  96. if (this.plugin && this.xgmml != xgmml) {
  97. this.plugin.mergeXGMML(xgmml);
  98. this.xgmml = xgmml;
  99. }
  100. },
  101. loadDOT: function (dot) {
  102. this.load(dot, "dot");
  103. },
  104. load: function (dot, layout) {
  105. if (this.plugin && this.xgmml != xgmml) {
  106. this.setMessage("Loading Data...");
  107. this.plugin.loadDOT(dot);
  108. this.setMessage("Performing Layout...");
  109. this.plugin.startLayout(layout);
  110. this.xgmml = xgmml;
  111. }
  112. },
  113. setLayout: function (layout) {
  114. if (this.plugin) {
  115. this.setMessage("Performing Layout...");
  116. this.plugin.startLayout(layout);
  117. }
  118. },
  119. centerOn: function (globalID) {
  120. var item = this.plugin.getItem(globalID);
  121. this.plugin.centerOnItem(item, true);
  122. var items = [item];
  123. this.plugin.setSelected(items, true);
  124. },
  125. getVersion: function () {
  126. return this.plugin.version;
  127. },
  128. displayProperties: function (item, place) {
  129. var props = this.plugin.getProperties(item);
  130. if (props.id) {
  131. var table = domConstruct.create("h3", {
  132. innerHTML: props.id,
  133. align: "center"
  134. }, place);
  135. delete props.id;
  136. }
  137. if (props.count) {
  138. var table = domConstruct.create("table", { border: 1, cellspacing: 0, width: "100%" }, place);
  139. var tr = domConstruct.create("tr", null, table);
  140. var td = domConstruct.create("td", { innerHTML: "Count" }, tr);
  141. var td = domConstruct.create("td", {
  142. align: "right",
  143. innerHTML: props.count
  144. }, tr);
  145. delete props.count;
  146. domConstruct.create("br", null, place);
  147. }
  148. if (props.max) {
  149. var table = domConstruct.create("table", { border: 1, cellspacing: 0, width: "100%" }, place);
  150. var tr = domConstruct.create("tr", null, table);
  151. domConstruct.create("th", { innerHTML: " " }, tr);
  152. domConstruct.create("th", { innerHTML: "Skew" }, tr);
  153. domConstruct.create("th", { innerHTML: "Node" }, tr);
  154. domConstruct.create("th", { innerHTML: "Rows" }, tr);
  155. tr = domConstruct.create("tr", null, table);
  156. domConstruct.create("td", { innerHTML: "Max" }, tr);
  157. domConstruct.create("td", { innerHTML: props.maxskew }, tr);
  158. domConstruct.create("td", { innerHTML: props.maxEndpoint }, tr);
  159. domConstruct.create("td", { innerHTML: props.max }, tr);
  160. tr = domConstruct.create("tr", null, table);
  161. domConstruct.create("td", { innerHTML: "Min" }, tr);
  162. domConstruct.create("td", { innerHTML: props.minskew }, tr);
  163. domConstruct.create("td", { innerHTML: props.minEndpoint }, tr);
  164. domConstruct.create("td", { innerHTML: props.min }, tr);
  165. delete props.maxskew;
  166. delete props.maxEndpoint;
  167. delete props.max;
  168. delete props.minskew;
  169. delete props.minEndpoint;
  170. delete props.min;
  171. domConstruct.create("br", null, place);
  172. }
  173. if (props.slaves) {
  174. var table = domConstruct.create("table", { border: 1, cellspacing: 0, width: "100%" }, place);
  175. var tr = domConstruct.create("tr", null, table);
  176. domConstruct.create("th", { innerHTML: "Slaves" }, tr);
  177. domConstruct.create("th", { innerHTML: "Started" }, tr);
  178. domConstruct.create("th", { innerHTML: "Stopped" }, tr);
  179. tr = domConstruct.create("tr", null, table);
  180. domConstruct.create("td", { innerHTML: props.slaves }, tr);
  181. domConstruct.create("td", { innerHTML: props.started }, tr);
  182. domConstruct.create("td", { innerHTML: props.stopped }, tr);
  183. delete props.slaves;
  184. delete props.started;
  185. delete props.stopped;
  186. domConstruct.create("br", null, place);
  187. }
  188. var first = true;
  189. var table = {};
  190. var tr = {};
  191. for (key in props) {
  192. if (key[0] == "_")
  193. continue;
  194. if (first) {
  195. first = false;
  196. table = domConstruct.create("table", { border: 1, cellspacing: 0, width: "100%" }, place);
  197. tr = domConstruct.create("tr", null, table);
  198. domConstruct.create("th", { innerHTML: "Property" }, tr);
  199. domConstruct.create("th", { innerHTML: "Value" }, tr);
  200. }
  201. tr = domConstruct.create("tr", null, table);
  202. domConstruct.create("td", { innerHTML: key }, tr);
  203. domConstruct.create("td", { innerHTML: props[key] }, tr);
  204. }
  205. if (first == false) {
  206. domConstruct.create("br", null, place);
  207. }
  208. },
  209. isPluginInstalled: function () {
  210. if (has("ie")) {
  211. try {
  212. var o = new ActiveXObject("HPCCSystems.HPCCSystemsGraphViewControl.1");
  213. o = null;
  214. return true;
  215. } catch (e) { }
  216. return false;
  217. } else {
  218. for (var i = 0, p = navigator.plugins, l = p.length; i < l; i++) {
  219. if (p[i].name.indexOf("HPCCSystemsGraphViewControl") > -1) {
  220. return true;
  221. }
  222. }
  223. return false;
  224. }
  225. },
  226. createPlugin: function () {
  227. var pluginID = this.id + "Plugin";
  228. if (this.plugin == null) {
  229. if (this._isPluginInstalled) {
  230. if (has("ie")) {
  231. this.graphContentPane.domNode.innerHTML = '<object '
  232. + 'type="application/x-hpccsystemsgraphviewcontrol" '
  233. + 'id="' + pluginID + '" '
  234. + 'name="' + pluginID + '" '
  235. + 'width="100%" '
  236. + 'height="100%">'
  237. + '</object>';
  238. } else {
  239. this.graphContentPane.domNode.innerHTML = '<embed type="application/x-hpccsystemsgraphviewcontrol" '
  240. + 'id="' + pluginID + '" '
  241. + 'name="' + pluginID + '" '
  242. + 'width="100%" '
  243. + 'height="100%">'
  244. + '</embed>';
  245. }
  246. this.plugin = dom.byId(pluginID);
  247. var context = this;
  248. setTimeout(function () {
  249. context.registerEvents();
  250. }, 20);
  251. } else {
  252. domConstruct.create("div", {
  253. innerHTML: "<h4>Graph View</h4>" +
  254. "<p>To enable graph views, please install the Graph View Control plugin:</p>" +
  255. "<a href=\"http://graphcontrol.hpccsystems.com/stable/SetupGraphControl.msi\">Internet Explorer + Firefox (32bit)</a><br>" +
  256. "<a href=\"http://graphcontrol.hpccsystems.com/stable/SetupGraphControl64.msi\">Internet Explorer + Firefox (64bit)</a><br>" +
  257. "<a href=\"https://github.com/hpcc-systems/GraphControl\">Linux/Other (sources)</a>"
  258. }, this.graphContentPane.domNode);
  259. }
  260. }
  261. },
  262. setMessage: function (message) {
  263. if (this.plugin) {
  264. return this.plugin.setMessage(message);
  265. }
  266. return null;
  267. },
  268. getLocalisedXGMML: function (items) {
  269. if (this.plugin) {
  270. return this.plugin.getLocalisedXGMML(items);
  271. }
  272. return null;
  273. },
  274. mergeSVG: function (svg) {
  275. if (this.plugin) {
  276. return this.plugin.mergeSVG(svg);
  277. }
  278. return null;
  279. },
  280. startLayout: function (layout) {
  281. if (this.plugin) {
  282. return this.plugin.startLayout(layout);
  283. }
  284. return null;
  285. },
  286. find: function (findText) {
  287. if (this.plugin) {
  288. return this.plugin.find(findText);
  289. }
  290. return [];
  291. },
  292. centerOnItem: function (item, scaleToFit, widthOnly) {
  293. if (this.plugin) {
  294. return this.plugin.centerOnItem(item, scaleToFit, widthOnly);
  295. }
  296. return null;
  297. },
  298. setSelected: function (items) {
  299. if (this.plugin) {
  300. return this.plugin.setSelected(items);
  301. }
  302. return null;
  303. },
  304. setSelectedAsGlobalID: function (items) {
  305. if (this.plugin) {
  306. return this.plugin.setSelectedAsGlobalID(items);
  307. }
  308. return null;
  309. },
  310. getSelectionAsGlobalID: function () {
  311. if (this.plugin) {
  312. return this.plugin.getSelectionAsGlobalID();
  313. }
  314. return [];
  315. },
  316. getItem: function (globalID) {
  317. if (this.plugin) {
  318. return this.plugin.getItem(globalID);
  319. }
  320. return null;
  321. },
  322. watchSplitter: function (splitter) {
  323. if (has("chrome")) {
  324. // Chrome can ignore splitter events
  325. return;
  326. }
  327. var context = this;
  328. dojo.connect(splitter, "_startDrag", function () {
  329. if (context.plugin) {
  330. dojo.style(context.plugin, "width", "1px");
  331. dojo.style(context.plugin, "height", "1px");
  332. }
  333. });
  334. dojo.connect(splitter, "_stopDrag", function (evt) {
  335. if (context.plugin) {
  336. dojo.style(context.plugin, "width", "100%");
  337. dojo.style(context.plugin, "height", "100%");
  338. }
  339. });
  340. },
  341. watchSelect: function (select) {
  342. if (has("chrome") && select) {
  343. // Only chrome needs to monitor select drop downs.
  344. var context = this;
  345. select.watch("_opened", function () {
  346. if (context.plugin) {
  347. if (select._opened) {
  348. dojo.style(context.plugin, "width", "1px");
  349. dojo.style(context.plugin, "height", "1px");
  350. } else {
  351. dojo.style(context.plugin, "width", "100%");
  352. dojo.style(context.plugin, "height", "100%");
  353. }
  354. }
  355. });
  356. }
  357. },
  358. watchStyleChange: function () {
  359. if (has("chrome")) {
  360. var context = this;
  361. aspect.around(domClass, "replace", function (origFunc) {
  362. return function (node, addStyle, removeStyle) {
  363. if (node.id == context.id) {
  364. if (addStyle == "dijitHidden") {
  365. context.hiddenBySelf = true;
  366. dojo.style(node, "width", "1px");
  367. dojo.style(node, "height", "1px");
  368. } else if (addStyle == "dijitVisible" && context.hiddenBySelf) {
  369. context.hiddenBySelf = false;
  370. dojo.style(node, "width", "100%");
  371. dojo.style(node, "height", "100%");
  372. } else {
  373. var deferred = origFunc(node, addStyle, removeStyle);
  374. // alternative: return origFunc.apply(this, arguments);
  375. return deferred;
  376. }
  377. } else {
  378. var deferred = origFunc(node, addStyle, removeStyle);
  379. // alternative: return origFunc.apply(this, arguments);
  380. return deferred;
  381. }
  382. }
  383. });
  384. }
  385. },
  386. registerEvents: function () {
  387. if (!this.eventsRegistered) {
  388. this.eventsRegistered = true;
  389. var context = this;
  390. this.registerEvent("MouseDoubleClick", function (item) {
  391. context.plugin.centerOnItem(item, true);
  392. context.onDoubleClick(context.plugin.getGlobalID(item));
  393. });
  394. this.registerEvent("LayoutFinished", function () {
  395. context.plugin.centerOnItem(0, true);
  396. context.setMessage('');
  397. context.dot = context.plugin.getDOT();
  398. context.svg = context.plugin.getSVG();
  399. context.onLayoutFinished();
  400. });
  401. this.registerEvent("SelectionChanged", function (items) {
  402. context.onSelectionChanged(items);
  403. });
  404. }
  405. },
  406. registerEvent: function (evt, func) {
  407. if (this.plugin) {
  408. if (this.plugin.attachEvent) {
  409. return this.plugin.attachEvent("on" + evt, func);
  410. } else {
  411. return this.plugin.addEventListener(evt, func, false);
  412. }
  413. }
  414. return false;
  415. }
  416. });
  417. });