GraphWidget.js 25 KB

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