simple_graph.js 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948
  1. // jQuery UI style "widget" for displaying a graph
  2. ////////////////////////////////////////////////////////////////////////////////
  3. //
  4. // graph
  5. //
  6. $(function()
  7. {
  8. // the widget definition, where "itop" is the namespace,
  9. // "dashboard" the widget name
  10. $.widget( "itop.simple_graph",
  11. {
  12. // default options
  13. options:
  14. {
  15. align: 'center',
  16. 'vertical-align': 'middle',
  17. source_url: null,
  18. sources: {},
  19. excluded: {},
  20. export_as_pdf: null,
  21. page_format: { label: 'Page Format:', values: { A3: 'A3', A4: 'A4', Letter: 'Letter' }, 'default': 'A4'},
  22. page_orientation: { label: 'Page Orientation:', values: { P: 'Portait', L: 'Landscape' }, 'default': 'L' },
  23. labels: {
  24. export_pdf_title: 'PDF Export Options',
  25. cancel: 'Cancel', 'export': 'Export',
  26. title: 'Document Title',
  27. include_list: 'Include the list of objects',
  28. comments: 'Comments',
  29. grouping_threshold: 'Grouping Threshold',
  30. additional_context_info: 'Additional Context Info',
  31. refresh: 'Refresh',
  32. check_all: 'Check All',
  33. uncheck_all: 'Uncheck All',
  34. none_selected: 'None',
  35. nb_selected: '# selected',
  36. zoom: 'Zoom',
  37. loading: 'Loading...'
  38. },
  39. export_as_document: null,
  40. drill_down: null,
  41. grouping_threshold: 10,
  42. excluded_classes: [],
  43. attachment_obj_class: null,
  44. attachment_obj_key: null,
  45. additional_contexts: [],
  46. context_key: ''
  47. },
  48. // the constructor
  49. _create: function()
  50. {
  51. var me = this;
  52. this.aNodes = [];
  53. this.aEdges = [];
  54. this.fZoom = 1.0;
  55. this.xOffset = 0;
  56. this.yOffset = 0;
  57. this.xPan = 0;
  58. this.yPan = 0;
  59. this.iTextHeight = 12;
  60. this.fSliderZoom = 1.0;
  61. this.bInUpdateSliderZoom = false;
  62. this.bRedrawNeeded = false;
  63. this.oPaper = Raphael(this.element.get(0), 16*this.element.width(), 16*this.element.height());
  64. this.element
  65. .addClass('panel-resized')
  66. .addClass('itop-simple-graph')
  67. .addClass('graph');
  68. this._create_toolkit_menu();
  69. this._build_context_menus();
  70. this.sTabId = null;
  71. var jTabPanel = this.element.closest('.ui-tabs-panel');
  72. if (jTabPanel.length > 0)
  73. {
  74. // We are inside a tab, find out which one and hook its activation
  75. this.sTabId = jTabPanel.attr('id');
  76. var jTabs = this.element.closest('.ui-tabs');
  77. jTabs.on( "tabsactivate", function( event, ui ) {
  78. me._on_tabs_activate(ui);
  79. });
  80. }
  81. $(window).bind('resized', function() { var that = me; window.setTimeout(function() { that._on_resize(); }, 50); } );
  82. this.element.bind('mousewheel', function(event, delta, deltaX, deltaY) {
  83. return me._on_mousewheel(event, delta, deltaX, deltaY);
  84. });
  85. if (this.options.source_url != null)
  86. {
  87. this.load_from_url(this.options.source_url);
  88. }
  89. },
  90. // called when created, and later when changing options
  91. _refresh: function()
  92. {
  93. this.draw();
  94. },
  95. // events bound via _bind are removed automatically
  96. // revert other modifications here
  97. _destroy: function()
  98. {
  99. var sId = this.element.attr('id');
  100. this.element
  101. .removeClass('itop-simple-graph')
  102. .removeClass('graph');
  103. $('#tk_graph'+sId).remove();
  104. $('#graph_'+sId+'_export_as_pdf').remove();
  105. },
  106. // _setOptions is called with a hash of all options that are changing
  107. _setOptions: function()
  108. {
  109. this._superApply(arguments);
  110. },
  111. // _setOption is called for each individual option that is changing
  112. _setOption: function( key, value )
  113. {
  114. this._superApply(arguments);
  115. },
  116. draw: function()
  117. {
  118. this._updateBBox();
  119. this.auto_scale();
  120. this.oPaper.clear();
  121. this._reset
  122. this.oPaper.setViewBox(this.xPan, this.yPan, this.element.width(), this.element.height(), false);
  123. for(var k in this.aNodes)
  124. {
  125. this.aNodes[k].aElements = [];
  126. this._draw_node(this.aNodes[k]);
  127. }
  128. for(var k in this.aEdges)
  129. {
  130. this.aEdges[k].aElements = [];
  131. this._draw_edge(this.aEdges[k]);
  132. }
  133. var me = this;
  134. this.oBackground = this.oPaper
  135. .rect(-10000, -10000, 20000, 20000)
  136. .attr({fill: '#fff', opacity: 0, cursor: 'move'})
  137. .toBack()
  138. .drag(function(dx, dy, x, y, event) { me._on_background_move(dx, dy, x, y, event); }, function(x, y, event) { me._on_background_drag_start(x, y, event); }, function (event) { me._on_background_drag_end(event); });
  139. this._make_tooltips();
  140. },
  141. _draw_node: function(oNode)
  142. {
  143. var iWidth = oNode.width;
  144. var iHeight = 32;
  145. var iFontSize = 10;
  146. var fTotalZoom = this.fZoom * this.fSliderZoom;
  147. var xPos = Math.round(oNode.x * fTotalZoom + this.xOffset);
  148. var yPos = Math.round(oNode.y * fTotalZoom + this.yOffset);
  149. oNode.tx = 0;
  150. oNode.ty = 0;
  151. switch(oNode.shape)
  152. {
  153. case 'disc':
  154. oNode.aElements.push(this.oPaper.circle(xPos, yPos, iWidth*fTotalZoom / 2).attr(oNode.disc_attr));
  155. var oText = this.oPaper.text(xPos, yPos, oNode.label);
  156. oNode.text_attr['font-size'] = iFontSize * fTotalZoom;
  157. oText.attr(oNode.text_attr);
  158. //oText.transform('s'+this.fZoom);
  159. oNode.aElements.push(oText);
  160. break;
  161. case 'group':
  162. oNode.aElements.push(this.oPaper.circle(xPos, yPos, iWidth*fTotalZoom / 2).attr({fill: '#fff', 'stroke-width':0}));
  163. oNode.aElements.push(this.oPaper.circle(xPos, yPos, iWidth*fTotalZoom / 2).attr(oNode.disc_attr));
  164. var xIcon = xPos - 18 * fTotalZoom;
  165. var yIcon = yPos - 18 * fTotalZoom;
  166. oNode.aElements.push(this.oPaper.image(oNode.icon_url, xIcon, yIcon, 16*fTotalZoom, 16*fTotalZoom).attr(oNode.icon_attr));
  167. oNode.aElements.push(this.oPaper.image(oNode.icon_url, xIcon + 18*fTotalZoom, yIcon, 16*fTotalZoom, 16*fTotalZoom).attr(oNode.icon_attr));
  168. oNode.aElements.push(this.oPaper.image(oNode.icon_url, xIcon + 9*fTotalZoom, yIcon + 18*fTotalZoom, 16*fTotalZoom, 16*fTotalZoom).attr(oNode.icon_attr));
  169. var oText = this.oPaper.text(xPos, yPos +2, oNode.label);
  170. oNode.text_attr['font-size'] = iFontSize * fTotalZoom;
  171. oText.attr(oNode.text_attr);
  172. //oText.transform('s'+this.fZoom);
  173. var oBB = oText.getBBox();
  174. var dy = iHeight/2*fTotalZoom + oBB.height/2;
  175. oText.remove();
  176. oText = this.oPaper.text(xPos, yPos +dy +2, oNode.label);
  177. oText.attr(oNode.text_attr);
  178. //oText.transform('s'+this.fZoom);
  179. oNode.aElements.push(oText);
  180. oNode.aElements.push(this.oPaper.rect( xPos - oBB.width/2 -2, yPos - oBB.height/2 + dy, oBB.width +4, oBB.height).attr({fill: '#fff', stroke: '#fff', opacity: 0.9}));
  181. oText.toFront();
  182. break;
  183. case 'icon':
  184. if(Raphael.svg)
  185. {
  186. // the colorShift plugin works only in SVG
  187. oNode.aElements.push(this.oPaper.image(oNode.icon_url, xPos - iWidth * fTotalZoom/2, yPos - iHeight * fTotalZoom/2, iWidth*fTotalZoom, iHeight*fTotalZoom).colorShift('#fff', 1));
  188. }
  189. oNode.aElements.push(this.oPaper.image(oNode.icon_url, xPos - iWidth * fTotalZoom/2, yPos - iHeight * fTotalZoom/2, iWidth*fTotalZoom, iHeight*fTotalZoom).attr(oNode.icon_attr));
  190. var idx = 0;
  191. for(var i in oNode.context_icons)
  192. {
  193. var sgn = 2*(idx % 2) -1; // Suite: -1, 1, -1, 1, -1, 1, -1, etc.
  194. var coef = Math.floor((1+idx)/2) * sgn; // Suite: 0, 1, -1, 2, -2, 3, -3, etc.
  195. var alpha = coef*Math.PI/4 - Math.PI/2;
  196. var x = xPos + Math.cos(alpha) * 1.25*iWidth * fTotalZoom / 2;
  197. var y = yPos + Math.sin(alpha) * 1.25*iWidth * fTotalZoom / 2;
  198. var l = iWidth/3 * fTotalZoom;
  199. oNode.aElements.push(this.oPaper.image(oNode.context_icons[i], x - l/2, y - l/2, l , l).attr(oNode.icon_attr));
  200. idx++;
  201. }
  202. var oText = this.oPaper.text( xPos, yPos, oNode.label);
  203. oNode.text_attr['font-size'] = iFontSize * fTotalZoom;
  204. oText.attr(oNode.text_attr);
  205. //oText.transform('S'+fTotalZoom);
  206. var oBB = oText.getBBox();
  207. var dy = iHeight/2*fTotalZoom + oBB.height/2;
  208. oText.remove();
  209. oText = this.oPaper.text( xPos, yPos + dy, oNode.label);
  210. oText.attr(oNode.text_attr);
  211. //oText.transform('S'+fTotalZoom);
  212. oNode.aElements.push(oText);
  213. oNode.aElements.push(this.oPaper.rect( xPos - oBB.width/2 -2, yPos - oBB.height/2 + dy, oBB.width +4, oBB.height).attr({fill: '#fff', stroke: '#fff', opacity: 0.9}).toBack());
  214. break;
  215. }
  216. if (oNode.source)
  217. {
  218. oNode.aElements.push(this.oPaper.circle(xPos, yPos, 1.25*iWidth*fTotalZoom / 2).attr({stroke: '#c33', 'stroke-width': 3*fTotalZoom }).toBack());
  219. }
  220. if (oNode.sink)
  221. {
  222. oNode.aElements.push(this.oPaper.circle(xPos, yPos, 1.25*iWidth*fTotalZoom / 2).attr({stroke: '#33c', 'stroke-width': 3*fTotalZoom }).toBack());
  223. }
  224. var me = this;
  225. for(k in oNode.aElements)
  226. {
  227. var sNodeId = oNode.id;
  228. $(oNode.aElements[k].node).attr({'data-type': oNode.shape, 'data-id': oNode.id} ).attr('class', 'popupMenuTarget');
  229. oNode.aElements[k].drag(
  230. function(dx, dy, x, y, event) {
  231. clearTimeout($(this.node).data('openTimeoutId'));
  232. me._move(sNodeId, dx, dy, x, y, event);
  233. },
  234. function(x, y, event) {
  235. me._drag_start(sNodeId, x, y, event);
  236. },
  237. function (event) {
  238. me._drag_end(sNodeId, event);
  239. }
  240. );
  241. }
  242. },
  243. _move: function(sNodeId, dx, dy, x, y, event)
  244. {
  245. var fTotalZoom = this.fZoom * this.fSliderZoom;
  246. var origDx = dx / fTotalZoom;
  247. var origDy = dy / fTotalZoom;
  248. var oNode = this._find_node(sNodeId);
  249. oNode.x = oNode.xOrig + origDx;
  250. oNode.y = oNode.yOrig + origDy;
  251. for(k in oNode.aElements)
  252. {
  253. oNode.aElements[k].transform('t'+(oNode.tx + dx)+', '+(oNode.ty + dy));
  254. for(j in this.aEdges)
  255. {
  256. var oEdge = this.aEdges[j];
  257. if ((oEdge.source_node_id == sNodeId) || (oEdge.sink_node_id == sNodeId))
  258. {
  259. var sPath = this._get_edge_path(oEdge);
  260. oEdge.aElements[0].attr({path: sPath});
  261. }
  262. }
  263. }
  264. },
  265. _drag_start: function(sNodeId, x, y, event)
  266. {
  267. var oNode = this._find_node(sNodeId);
  268. oNode.xOrig = oNode.x;
  269. oNode.yOrig = oNode.y;
  270. },
  271. _drag_end: function(sNodeId, event)
  272. {
  273. var fTotalZoom = this.fZoom * this.fSliderZoom;
  274. var oNode = this._find_node(sNodeId);
  275. oNode.tx += (oNode.x - oNode.xOrig) * fTotalZoom;
  276. oNode.ty += (oNode.y - oNode.yOrig) * fTotalZoom;
  277. oNode.xOrig = oNode.x;
  278. oNode.yOrig = oNode.y;
  279. this._updateBBox();
  280. },
  281. _updateBBox: function()
  282. {
  283. this.options.xmin = 9999;
  284. this.options.xmax = -9999;
  285. this.options.ymin = 9999;
  286. this.options.ymax = -9999;
  287. for(var k in this.aNodes)
  288. {
  289. this.options.xmin = Math.min(this.aNodes[k].x + this.aNodes[k].tx - this.aNodes[k].width/2, this.options.xmin);
  290. this.options.xmax = Math.max(this.aNodes[k].x + this.aNodes[k].tx + this.aNodes[k].width/2, this.options.xmax);
  291. this.options.ymin = Math.min(this.aNodes[k].y + this.aNodes[k].ty - this.aNodes[k].width/2, this.options.ymin);
  292. this.options.ymax = Math.max(this.aNodes[k].y + this.aNodes[k].ty + this.aNodes[k].width/2, this.options.ymax);
  293. }
  294. },
  295. _get_edge_path: function(oEdge)
  296. {
  297. var fTotalZoom = this.fZoom * this.fSliderZoom;
  298. var oStart = this._find_node(oEdge.source_node_id);
  299. var oEnd = this._find_node(oEdge.sink_node_id);
  300. var iArrowSize = 5;
  301. if ((oStart == null) || (oEnd == null)) return '';
  302. var xStart = Math.round(oStart.x * fTotalZoom + this.xOffset);
  303. var yStart = Math.round(oStart.y * fTotalZoom + this.yOffset);
  304. var xEnd = Math.round(oEnd.x * fTotalZoom + this.xOffset);
  305. var yEnd = Math.round(oEnd.y * fTotalZoom + this.yOffset);
  306. var sPath = Raphael.format('M{0},{1}L{2},{3}', xStart, yStart, xEnd, yEnd);
  307. var vx = (xEnd - xStart);
  308. var vy = (yEnd - yStart);
  309. var l = Math.sqrt(vx*vx+vy*vy);
  310. vx = vx / l;
  311. vy = vy / l;
  312. var ux = -vy;
  313. var uy = vx;
  314. var lPos = Math.max(l/2, l - 40*fTotalZoom);
  315. var xArrow = xStart + vx * lPos;
  316. var yArrow = yStart + vy * lPos;
  317. sPath += Raphael.format('M{0},{1}l{2},{3}M{4},{5}l{6},{7}', xArrow, yArrow, fTotalZoom * iArrowSize *(-vx + ux), fTotalZoom * iArrowSize *(-vy + uy), xArrow, yArrow, fTotalZoom * iArrowSize *(-vx - ux), fTotalZoom * iArrowSize *(-vy - uy));
  318. return sPath;
  319. },
  320. _draw_edge: function(oEdge)
  321. {
  322. var fTotalZoom = this.fZoom * this.fSliderZoom;
  323. var fStrokeSize = Math.max(1, 2 * fTotalZoom);
  324. var sPath = this._get_edge_path(oEdge);
  325. var oAttr = $.extend(oEdge.attr);
  326. oAttr['stroke-linecap'] = 'round';
  327. oAttr['stroke-width'] = fStrokeSize;
  328. oEdge.aElements.push(this.oPaper.path(sPath).attr(oAttr).toBack());
  329. },
  330. _find_node: function(sId)
  331. {
  332. for(var k in this.aNodes)
  333. {
  334. if (this.aNodes[k].id == sId) return this.aNodes[k];
  335. }
  336. return null;
  337. },
  338. adjust_height: function()
  339. {
  340. var maxHeight = this.element.parent().height();
  341. // Compute the available height
  342. var element = this.element;
  343. this.element.parent().children().each(function() {
  344. if($(this).is(':visible') && !$(this).hasClass('graph') && ($(this).attr('id') != element.attr('id')))
  345. {
  346. maxHeight = maxHeight - $(this).height();
  347. }
  348. });
  349. this.element.height(maxHeight - 20);
  350. this.oPaper.setSize(this.element.width(), this.element.height());
  351. },
  352. auto_scale: function()
  353. {
  354. var fMaxZoom = 1.5;
  355. this.adjust_height();
  356. iMargin = 10;
  357. xmin = this.options.xmin - iMargin;
  358. xmax = this.options.xmax + iMargin;
  359. ymin = this.options.ymin - iMargin;
  360. ymax = this.options.ymax + iMargin;
  361. var xScale = this.element.width() / (xmax - xmin);
  362. var yScale = this.element.height() / (ymax - ymin + this.iTextHeight);
  363. this.fZoom = Math.min(xScale, yScale, fMaxZoom);
  364. switch(this.options.align)
  365. {
  366. case 'left':
  367. this.xOffset = -xmin * this.fZoom;
  368. break;
  369. case 'right':
  370. this.xOffset = (this.element.width() - (xmax - xmin) * this.fZoom);
  371. break;
  372. case 'center':
  373. this.xOffset = -xmin * this.fZoom + (this.element.width() - (xmax - xmin) * this.fZoom) / 2;
  374. break;
  375. }
  376. switch(this.options['vertical-align'])
  377. {
  378. case 'top':
  379. this.yOffset = -ymin * this.fZoom;
  380. break;
  381. case 'bottom':
  382. this.yOffset = this.element.height() - (ymax + this.iTextHeight) * this.fZoom;
  383. break;
  384. case 'middle':
  385. this.yOffset = -ymin * this.fZoom + (this.element.height() - (ymax - ymin + this.iTextHeight) * this.fZoom) / 2;
  386. break;
  387. }
  388. },
  389. add_node: function(oNode)
  390. {
  391. oNode.aElements = [];
  392. oNode.tx = 0;
  393. oNode.ty = 0;
  394. this.aNodes.push(oNode);
  395. },
  396. add_edge: function(oEdge)
  397. {
  398. oEdge.aElements = [];
  399. this.aEdges.push(oEdge);
  400. },
  401. show_group: function(sGroupId)
  402. {
  403. this._close_all_tooltips();
  404. // Activate the 3rd tab
  405. this.element.closest('.ui-tabs').tabs("option", "active", 2);
  406. // Scroll into view the group
  407. if ($('#'+sGroupId).length > 0)
  408. {
  409. $('#'+sGroupId)[0].scrollIntoView();
  410. }
  411. },
  412. _create_toolkit_menu: function()
  413. {
  414. var sPopupMenuId = 'tk_graph'+this.element.attr('id');
  415. var sHtml = '<div class="graph_config">';
  416. var sId = this.element.attr('id');
  417. sHtml += this.options.labels.grouping_threshold+'&nbsp;<input type="text" name="g" value="'+this.options.grouping_threshold+'" id="'+sId+'_grouping_threshold" size="2">';
  418. if (this.options.additional_contexts.length > 0)
  419. {
  420. sHtml += '&nbsp;'+this.options.labels.additional_context_info+' <select id="'+sId+'_contexts" name="contexts" class="multiselect" multiple size="1">';
  421. for(var k in this.options.additional_contexts)
  422. {
  423. sHtml += '<option value="'+k+'" selected>'+this.options.additional_contexts[k].label+'</option>';
  424. }
  425. sHtml += '</select>'
  426. }
  427. sHtml += '&nbsp;<button type="button" id="'+sId+'_refresh_btn">'+this.options.labels.refresh+'</button>';
  428. sHtml += '<div class="itop_popup toolkit_menu graph" style="font-size: 12px;" id="'+sPopupMenuId+'"><ul><li><img src="../images/toolkit_menu.png"><ul>';
  429. if (this.options.export_as_pdf != null)
  430. {
  431. sHtml += '<li><a href="#" id="'+sPopupMenuId+'_pdf">'+this.options.export_as_pdf.label+'</a></li>';
  432. }
  433. if (this.options.export_as_attachment != null)
  434. {
  435. sHtml += '<li><a href="#" id="'+sPopupMenuId+'_attachment">'+this.options.export_as_attachment.label+'</a></li>';
  436. }
  437. //sHtml += '<li><a href="#" id="'+sPopupMenuId+'_reload">Refresh</a></li>';
  438. sHtml += '</ul></li></ul></div>';
  439. sHtml += '<span class="graph_zoom"><span>'+this.options.labels.zoom+'</span>';
  440. sHtml += '<div id="'+sId+'_zoom_minus" class="graph_zoom_minus ui-icon ui-icon-circle-minus"></div>';
  441. sHtml += '<div id="'+sId+'_zoom" class="graph_zoom_slider"></div>';
  442. sHtml += '<div id="'+sId+'_zoom_plus" class="graph_zoom_plus ui-icon ui-icon-circle-plus"></div>';
  443. sHtml += '</span>';
  444. sHtml += '</div>';
  445. this.element.before(sHtml);
  446. $('#'+sPopupMenuId+'>ul').popupmenu();
  447. var me = this;
  448. $('#'+sPopupMenuId+'_pdf').click(function() { me.export_as_pdf(); });
  449. $('#'+sPopupMenuId+'_attachment').click(function() { me.export_as_attachment(); });
  450. $('#'+sId+'_grouping_threshold').spinner({ min: 2});
  451. $('#'+sId+'_zoom').slider({ min: 0, max: 5, value: 1, step: 0.25, change: function() { me._on_zoom_change( $(this).slider('value')); } });
  452. $('#'+sId+'_zoom_plus').click(function() { $('#'+sId+'_zoom').slider('value', 0.25 + $('#'+sId+'_zoom').slider('value')); return false; });
  453. $('#'+sId+'_zoom_minus').click(function() { $('#'+sId+'_zoom').slider('value', $('#'+sId+'_zoom').slider('value') - 0.25); return false; });
  454. $('#'+sId+'_contexts').multiselect({header: true, checkAllText: this.options.labels.check_all, uncheckAllText: this.options.labels.uncheck_all, noneSelectedText: this.options.labels.none_selected, selectedText: this.options.labels.nb_selected, selectedList: 1});
  455. $('#'+sId+'_refresh_btn').button().click(function() { me.reload(); });
  456. },
  457. _build_context_menus: function()
  458. {
  459. var sId = this.element.attr('id');
  460. var me = this;
  461. $.contextMenu({
  462. selector: '#'+sId+' .popupMenuTarget',
  463. build: function(trigger, e) {
  464. // this callback is executed every time the menu is to be shown
  465. // its results are destroyed every time the menu is hidden
  466. // e is the original contextmenu event, containing e.pageX and e.pageY (amongst other data)
  467. var sType = trigger.attr('data-type');
  468. var sNodeId = trigger.attr('data-id');
  469. var oNode = me._find_node(sNodeId);
  470. clearTimeout(trigger.data('openTimeoutId'));
  471. /*
  472. var sObjName = trigger.attr('data-class');
  473. var sIndex = trigger.attr('data-index');
  474. var originalEvent = e;
  475. var bHasItems = false;
  476. */
  477. var oResult = {callback: null, items: {}};
  478. switch(sType)
  479. {
  480. case 'group':
  481. var sGroupIndex = oNode.group_index;
  482. if( $('#relation_group_'+sGroupIndex).length > 0)
  483. {
  484. oResult = {
  485. callback: function(key, options) {
  486. var me = $('.itop-simple-graph').data('itopSimple_graph'); // need a live value
  487. me.show_group('relation_group_'+sGroupIndex);
  488. },
  489. items: { 'show': {name: me.options.drill_down.label } }
  490. };
  491. }
  492. break;
  493. case 'icon':
  494. var sObjClass = oNode.obj_class;
  495. var sObjKey = oNode.obj_key;
  496. oResult = {
  497. callback: function(key, options) {
  498. var me = $('.itop-simple-graph').data('itopSimple_graph'); // need a live value
  499. var sURL = me.options.drill_down.url.replace('%1$s', sObjClass).replace('%2$s', sObjKey);
  500. window.location.href = sURL;
  501. },
  502. items: { 'details': {name: me.options.drill_down.label } }
  503. };
  504. break;
  505. default:
  506. oResult = false; // No context menu
  507. }
  508. return oResult;
  509. }
  510. });
  511. },
  512. export_as_pdf: function()
  513. {
  514. this._export_dlg(this.options.labels.export_pdf_title, this.options.export_as_pdf.url, 'download_pdf');
  515. },
  516. _export_dlg: function(sTitle, sSubmitUrl, sOperation)
  517. {
  518. var sId = this.element.attr('id');
  519. var me = this;
  520. var oPositions = {};
  521. for(k in this.aNodes)
  522. {
  523. oPositions[this.aNodes[k].id] = {x: this.aNodes[k].x, y: this.aNodes[k].y };
  524. }
  525. var sHtmlForm = '<div id="GraphExportDlg'+this.element.attr('id')+'"><form id="graph_'+this.element.attr('id')+'_export_dlg" target="_blank" action="'+sSubmitUrl+'" method="post">';
  526. sHtmlForm += '<input type="hidden" name="g" value="'+this.options.grouping_threshold+'">';
  527. sHtmlForm += '<input type="hidden" name="context_key" value="'+this.options.context_key+'">';
  528. $('#'+sId+'_contexts').multiselect('getChecked').each(function() {
  529. sHtmlForm += '<input type="hidden" name="contexts['+$(this).val()+']" value="'+me.options.additional_contexts[$(this).val()].oql+'">';
  530. });
  531. sHtmlForm += '<input type="hidden" name="positions" value="">';
  532. for(k in this.options.excluded_classes)
  533. {
  534. sHtmlForm += '<input type="hidden" name="excluded_classes[]" value="'+this.options.excluded_classes[k]+'">';
  535. }
  536. for(var k1 in this.options.sources)
  537. {
  538. for(var k2 in this.options.sources[k1])
  539. {
  540. sHtmlForm += '<input type="hidden" name="sources['+k1+'][]" value="'+this.options.sources[k1][k2]+'">';
  541. }
  542. }
  543. for(var k1 in this.options.excluded)
  544. {
  545. for(var k2 in this.options.excluded[k1])
  546. {
  547. sHtmlForm += '<input type="hidden" name="excluded['+k1+'][]" value="'+this.options.excluded[k1][k2]+'">';
  548. }
  549. }
  550. if (sOperation == 'attachment')
  551. {
  552. sHtmlForm += '<input type="hidden" name="obj_class" value="'+this.options.export_as_attachment.obj_class+'">';
  553. sHtmlForm += '<input type="hidden" name="obj_key" value="'+this.options.export_as_attachment.obj_key+'">';
  554. }
  555. sHtmlForm += '<table>';
  556. sHtmlForm += '<tr><td>'+this.options.page_format.label+'</td><td><select name="p">';
  557. for(k in this.options.page_format.values)
  558. {
  559. var sSelected = (k == this.options.page_format['default']) ? ' selected' : '';
  560. sHtmlForm += '<option value="'+k+'"'+sSelected+'>'+this.options.page_format.values[k]+'</option>';
  561. }
  562. sHtmlForm += '</select></td></tr>';
  563. sHtmlForm += '<tr><td>'+this.options.page_orientation.label+'</td><td><select name="o">';
  564. for(k in this.options.page_orientation.values)
  565. {
  566. var sSelected = (k == this.options.page_orientation['default']) ? ' selected' : '';
  567. sHtmlForm += '<option value="'+k+'"'+sSelected+'>'+this.options.page_orientation.values[k]+'</option>';
  568. }
  569. sHtmlForm += '</select></td></tr>';
  570. sHtmlForm += '<tr><td>'+this.options.labels.title+'</td><td><input name="title" value="'+this.options.labels.untitled+'" style="width: 20em;"/></td></tr>';
  571. sHtmlForm += '<tr><td>'+this.options.labels.comments+'</td><td><textarea style="width: 20em; height:5em;" name="comments"/></textarea></td></tr>';
  572. sHtmlForm += '<tr><td colspan=2><input type="checkbox" checked id="include_list_checkbox" name="include_list" value="1"><label for="include_list_checkbox">&nbsp;'+this.options.labels.include_list+'</label></td></tr>';
  573. sHtmlForm += '<table>';
  574. sHtmlForm += '</form></div>';
  575. $('body').append(sHtmlForm);
  576. $('#graph_'+this.element.attr('id')+'_export_dlg input[name="positions"]').val(JSON.stringify(oPositions));
  577. var me = this;
  578. if (sOperation == 'attachment')
  579. {
  580. $('#GraphExportDlg'+this.element.attr('id')+' form').submit(function() { return me._on_export_as_attachment(); });
  581. }
  582. $('#GraphExportDlg'+this.element.attr('id')).dialog({
  583. width: 'auto',
  584. modal: true,
  585. title: sTitle,
  586. close: function() { $(this).remove(); },
  587. buttons: [
  588. {text: this.options.labels['cancel'], click: function() { $(this).dialog('close');} },
  589. {text: this.options.labels['export'], click: function() { $('#graph_'+me.element.attr('id')+'_export_dlg').submit(); $(this).dialog('close');} },
  590. ]
  591. });
  592. },
  593. _on_zoom_change: function(sliderValue)
  594. {
  595. if(!this.bInUpdateSliderZoom)
  596. {
  597. var Z0 = this.fSliderZoom;
  598. var X = this.xOffset - this.element.width()/2;
  599. var Y = this.yOffset - this.element.height()/2;
  600. this.fSliderZoom = Math.pow(2 , (sliderValue - 1));
  601. var Z1 = this.fSliderZoom = Math.pow(2 , (sliderValue - 1));
  602. var dx = X * (1 - Z1/Z0);
  603. var dy = Y * (1 - Z1/Z0);
  604. this.xPan += dx;
  605. this.yPan += dy;
  606. this._close_all_tooltips();
  607. this.oPaper.setViewBox(this.xPan, this.yPan, this.element.width(), this.element.height(), false);
  608. this.draw();
  609. }
  610. },
  611. _on_mousewheel: function(event, delta, deltaX, deltaY)
  612. {
  613. var fStep = 0.25*delta;
  614. var sId = this.element.attr('id');
  615. $('#'+sId+'_zoom').slider('value', fStep + $('#'+sId+'_zoom').slider('value'));
  616. },
  617. _on_resize: function()
  618. {
  619. this.element.closest('.ui-tabs').tabs({ heightStyle: "fill" });
  620. this.auto_scale();
  621. this._close_all_tooltips();
  622. this.draw();
  623. },
  624. _on_tabs_activate: function(ui)
  625. {
  626. if (ui.newPanel.selector == ('#'+this.sTabId))
  627. {
  628. if (this.bRedrawNeeded)
  629. {
  630. this._updateBBox();
  631. this.auto_scale();
  632. this.oPaper.setSize(this.element.width(), this.element.height());
  633. this._reset_pan_and_zoom();
  634. this.draw();
  635. bRedrawNeeded = false;
  636. }
  637. }
  638. },
  639. load: function(oData)
  640. {
  641. var me = this;
  642. var sId = this.element.attr('id');
  643. this.aNodes = [];
  644. this.aEdges = [];
  645. for(k in oData.nodes)
  646. {
  647. this.add_node(oData.nodes[k]);
  648. }
  649. for(k in oData.edges)
  650. {
  651. this.add_edge(oData.edges[k]);
  652. }
  653. if (oData.groups)
  654. {
  655. this.refresh_groups(oData.groups);
  656. }
  657. if (this.element.is(':visible'))
  658. {
  659. this._updateBBox();
  660. this.auto_scale();
  661. this._reset_pan_and_zoom();
  662. this.draw();
  663. }
  664. else
  665. {
  666. this.bRedrawNeeded = true;
  667. }
  668. },
  669. refresh_groups: function(aGroups)
  670. {
  671. if ($('#impacted_groups').length > 0)
  672. {
  673. // The "Groups" tab is present, refresh it
  674. if (aGroups.length == 0)
  675. {
  676. this.element.closest('.ui-tabs').tabs("disable", 2);
  677. $('#impacted_groups').html('');
  678. }
  679. else
  680. {
  681. this.element.closest('.ui-tabs').tabs("enable", 2);
  682. $('#impacted_groups').html('<img src="../images/indicator.gif">');
  683. var sUrl = GetAbsoluteUrlAppRoot()+'pages/ajax.render.php';
  684. $.post(sUrl, { operation: 'relation_groups', groups: aGroups }, function(data) {
  685. $('#impacted_groups').html(data);
  686. });
  687. }
  688. }
  689. },
  690. _reset_pan_and_zoom: function()
  691. {
  692. this.xPan = 0;
  693. this.yPan = 0;
  694. var sId = this.element.attr('id');
  695. this.bInUpdateSliderZoom = true;
  696. $('#'+sId+'_zoom').slider('value', 1);
  697. this.fSliderZoom = 1.0;
  698. this.bInUpdateSliderZoom = false;
  699. this.oPaper.setViewBox(this.xPan, this.yPan, this.element.width(), this.element.height(), false);
  700. },
  701. load_from_url: function(sUrl)
  702. {
  703. this.options.load_from_url = sUrl;
  704. var me = this;
  705. var sId = this.element.attr('id');
  706. this.options.grouping_threshold = $('#'+sId+'_grouping_threshold').val();
  707. if (this.options.grouping_threshold < 2)
  708. {
  709. this.options.grouping_threshold = 2;
  710. $('#'+sId+'_grouping_threshold').val(this.options.grouping_threshold);
  711. }
  712. var aContexts = [];
  713. $('#'+sId+'_contexts').multiselect('getChecked').each(function() { aContexts[$(this).val()] = me.options.additional_contexts[$(this).val()].oql; });
  714. this.element.closest('.ui-tabs').tabs({ heightStyle: "fill" });
  715. this.adjust_height();
  716. this._close_all_tooltips();
  717. this.oPaper.rect(this.xPan, this.yPan, this.element.width(), this.element.height()).attr({fill: '#000', opacity: 0.4, 'stroke-width': 0});
  718. this.oPaper.rect(this.xPan + this.element.width()/2 - 100, this.yPan + this.element.height()/2 - 10, 200, 20)
  719. .attr({fill: 'url(../setup/orange-progress.gif)', stroke: '#000', 'stroke-width': 1});
  720. this.oPaper.text(this.xPan + this.element.width()/2, this.yPan + this.element.height()/2 - 20, this.options.labels.loading);
  721. $('#'+sId+'_refresh_btn').button('disable');
  722. $.post(sUrl, {excluded_classes: this.options.excluded_classes, g: this.options.grouping_threshold, sources: this.options.sources, excluded: this.options.excluded, contexts: aContexts, context_key: this.options.context_key }, function(data) {
  723. me.load(data);
  724. $('#'+sId+'_refresh_btn').button('enable');
  725. }, 'json');
  726. },
  727. export_as_attachment: function()
  728. {
  729. this._export_dlg(this.options.labels.export_as_attachment_title, this.options.export_as_attachment.url, 'attachment');
  730. },
  731. _on_export_as_attachment: function()
  732. {
  733. var oParams = {};
  734. var oPositions = {};
  735. var jForm = $('#GraphExportDlg'+this.element.attr('id')+' form');
  736. for(k in this.aNodes)
  737. {
  738. oPositions[this.aNodes[k].id] = {x: this.aNodes[k].x, y: this.aNodes[k].y };
  739. }
  740. oParams.positions = JSON.stringify(oPositions);
  741. oParams.sources = this.options.sources;
  742. oParams.excluded_classes = this.options.excluded_classes;
  743. oParams.title = jForm.find(':input[name="title"]').val();
  744. oParams.comments = jForm.find(':input[name="comments"]').val();
  745. oParams.include_list = jForm.find(':input[name="include_list"]:checked').length;
  746. oParams.o = jForm.find(':input[name="o"]').val();
  747. oParams.p = jForm.find(':input[name="p"]').val();
  748. oParams.obj_class = this.options.export_as_attachment.obj_class;
  749. oParams.obj_key = this.options.export_as_attachment.obj_key;
  750. oParams.contexts = [];
  751. var me = this;
  752. $('#'+this.element.attr('id')+'_contexts').multiselect('getChecked').each(function() {
  753. oParams.contexts[$(this).val()] = me.options.additional_contexts[$(this).val()].oql;
  754. });
  755. oParams.context_key = this.options.context_key;
  756. var sUrl = jForm.attr('action');
  757. var sTitle = oParams.title;
  758. var jPanel = $('#attachments').closest('.ui-tabs-panel');
  759. var jTab = null;
  760. var sTabText = null;
  761. if (jPanel.length > 0)
  762. {
  763. var sTabId = jPanel.attr('id');
  764. jTab = $('li[aria-controls='+sTabId+']');
  765. sTabText = jTab.find('span').html();
  766. jTab.find('span').html(sTabText+' <img style="vertical-align:bottom" src="../images/indicator.gif">');
  767. }
  768. $.post(sUrl, oParams, function(data) {
  769. var sDownloadLink = GetAbsoluteUrlAppRoot()+'pages/ajax.render.php?operation=download_document&class=Attachment&id='+data.att_id+'&field=contents';
  770. var sIcon = GetAbsoluteUrlModulesRoot()+'itop-attachments/icons/pdf.png';
  771. if (jTab != null)
  772. {
  773. var re = /^([^(]+)\(([0-9]+)\)(.*)$/;
  774. var aParts = re.exec(sTabText);
  775. if (aParts == null)
  776. {
  777. // First attachment
  778. $('#attachments').html('<div class="attachment" id="display_attachment_'+data.att_id+'"><a data-preview="false" href="'+sDownloadLink+'"><img src="'+sIcon+'"><br/>'+sTitle+'.pdf<input id="attachment_'+data.att_id+'" type="hidden" name="attachments[]" value="'+data.att_id+'"/></a><br/><input type="button" class="btn_hidden" value="{$sDeleteBtn}" onClick="RemoveAttachment('+data.att_id+');"/></div>');
  779. jTab.find('span').html(sTabText +' (1)');
  780. }
  781. else
  782. {
  783. $('#attachments').append('<div class="attachment" id="display_attachment_'+data.att_id+'"><a data-preview="false" href="'+sDownloadLink+'"><img src="'+sIcon+'"><br/>'+sTitle+'.pdf<input id="attachment_'+data.att_id+'" type="hidden" name="attachments[]" value="'+data.att_id+'"/></a><br/><input type="button" class="btn_hidden" value="{$sDeleteBtn}" onClick="RemoveAttachment('+data.att_id+');"/></div>');
  784. var iPrevCount = parseInt(aParts[2], 10);
  785. jTab.find('span').html(aParts[1]+'('+(1 + iPrevCount)+')'+aParts[3]);
  786. }
  787. }
  788. }, 'json');
  789. return false;
  790. },
  791. reload: function()
  792. {
  793. this.load_from_url(this.options.load_from_url);
  794. },
  795. _make_tooltips: function()
  796. {
  797. var me = this;
  798. $( ".popupMenuTarget" ).tooltip({
  799. content: function() {
  800. var sDataId = $(this).attr('data-id');
  801. var sTooltipContent = '<div class="tooltip-close-button" data-id="'+sDataId+'" style="display:inline-block; float:right; cursor:pointer; padding-left:0.25em;">×</div>';
  802. sTooltipContent += me._get_tooltip_content(sDataId);
  803. return sTooltipContent;
  804. },
  805. items: '.popupMenuTarget',
  806. tooltipClass: 'tooltip-simple-graph',
  807. position: {
  808. my: "center bottom-10",
  809. at: "center top",
  810. using: function( position, feedback ) {
  811. $(this).css( position );
  812. $( "<div>" )
  813. .addClass( "arrow" )
  814. .addClass( feedback.vertical )
  815. .appendTo( this );
  816. }
  817. }
  818. })
  819. .off( "mouseover mouseout" )
  820. .on( "mouseover", function(event){
  821. event.stopImmediatePropagation();
  822. var jMe = $(this);
  823. jMe.data('openTimeoutId', setTimeout(function() {
  824. var sDataId = jMe.attr('data-id');
  825. if ($('.tooltip-close-button[data-id="'+sDataId+'"]').length == 0)
  826. {
  827. jMe.data('openTimeoutId', 0);
  828. jMe.tooltip('open');
  829. }
  830. }, 1000));
  831. })
  832. .on( "mouseout", function(event){
  833. event.stopImmediatePropagation();
  834. clearTimeout($(this).data('openTimeoutId'));
  835. });
  836. /* Happens at every on_drag_end !!!
  837. .on( "click", function(){
  838. var sDataId = $(this).attr('data-id');
  839. if ($('.tooltip-close-button[data-id="'+sDataId+'"]').length == 0)
  840. {
  841. $(this).tooltip( 'open' );
  842. }
  843. else
  844. {
  845. $(this).tooltip( 'close' );
  846. }
  847. $( this ).unbind( "mouseleave" );
  848. return false;
  849. });
  850. */
  851. $('body').on('click', '.tooltip-close-button', function() {
  852. var sDataId = $(this).attr('data-id');
  853. $('.popupMenuTarget[data-id="'+sDataId+'"]').tooltip('close');
  854. });
  855. this.element.on("click", ":not(.tooltip-simple-graph *,.tooltip-simple-graph)", function(){
  856. $('.popupMenuTarget').each(function (i) {
  857. clearTimeout($(this).data('openTimeoutId'));
  858. $(this).data('openTimeoutId', 0);
  859. $(this).tooltip("close");
  860. });
  861. });
  862. },
  863. _get_tooltip_content: function(sNodeId)
  864. {
  865. var oNode = this._find_node(sNodeId);
  866. if (oNode !== null)
  867. {
  868. return oNode.tooltip;
  869. }
  870. return '<p>Node Id:'+sNodeId+'</p>';
  871. },
  872. _close_all_tooltips: function()
  873. {
  874. this.element.find('.popupMenuTarget').each(function() {
  875. clearTimeout($(this).data('openTimeoutId'));
  876. $(this).data('openTimeoutId', 0);
  877. $(this).tooltip('close');
  878. });
  879. },
  880. _on_background_drag_start: function(x, y, event)
  881. {
  882. this.bDragging = true;
  883. this.xDrag = 0;
  884. this.yDrag = 0;
  885. //this._close_all_tooltips();
  886. },
  887. _on_background_move: function(dx, dy, x, y, event)
  888. {
  889. if (this.bDragging)
  890. {
  891. this.xDrag = dx;
  892. this.yDrag = dy;
  893. this.oPaper.setViewBox(this.xPan - this.xDrag, this.yPan - this.yDrag, this.element.width(), this.element.height(), false);
  894. }
  895. },
  896. _on_background_drag_end: function(event)
  897. {
  898. if (this.bDragging)
  899. {
  900. this.xPan -= this.xDrag;
  901. this.yPan -= this.yDrag;
  902. this.xDrag = 0;
  903. this.yDrag = 0;
  904. this.bDragging = false;
  905. }
  906. },
  907. });
  908. });