simple_graph.js 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949
  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. sSelected = (this.options.additional_contexts[k]['default']) ? 'selected' : '';
  424. sHtml += '<option value="'+k+'" '+sSelected+'>'+this.options.additional_contexts[k].label+'</option>';
  425. }
  426. sHtml += '</select>'
  427. }
  428. sHtml += '&nbsp;<button type="button" id="'+sId+'_refresh_btn">'+this.options.labels.refresh+'</button>';
  429. sHtml += '<div class="itop_popup toolkit_menu graph" style="font-size: 12px;" id="'+sPopupMenuId+'"><ul><li><img src="../images/toolkit_menu.png"><ul>';
  430. if (this.options.export_as_pdf != null)
  431. {
  432. sHtml += '<li><a href="#" id="'+sPopupMenuId+'_pdf">'+this.options.export_as_pdf.label+'</a></li>';
  433. }
  434. if (this.options.export_as_attachment != null)
  435. {
  436. sHtml += '<li><a href="#" id="'+sPopupMenuId+'_attachment">'+this.options.export_as_attachment.label+'</a></li>';
  437. }
  438. //sHtml += '<li><a href="#" id="'+sPopupMenuId+'_reload">Refresh</a></li>';
  439. sHtml += '</ul></li></ul></div>';
  440. sHtml += '<span class="graph_zoom"><span>'+this.options.labels.zoom+'</span>';
  441. sHtml += '<div id="'+sId+'_zoom_minus" class="graph_zoom_minus ui-icon ui-icon-circle-minus"></div>';
  442. sHtml += '<div id="'+sId+'_zoom" class="graph_zoom_slider"></div>';
  443. sHtml += '<div id="'+sId+'_zoom_plus" class="graph_zoom_plus ui-icon ui-icon-circle-plus"></div>';
  444. sHtml += '</span>';
  445. sHtml += '</div>';
  446. this.element.before(sHtml);
  447. $('#'+sPopupMenuId+'>ul').popupmenu();
  448. var me = this;
  449. $('#'+sPopupMenuId+'_pdf').click(function() { me.export_as_pdf(); });
  450. $('#'+sPopupMenuId+'_attachment').click(function() { me.export_as_attachment(); });
  451. $('#'+sId+'_grouping_threshold').spinner({ min: 2});
  452. $('#'+sId+'_zoom').slider({ min: 0, max: 5, value: 1, step: 0.25, change: function() { me._on_zoom_change( $(this).slider('value')); } });
  453. $('#'+sId+'_zoom_plus').click(function() { $('#'+sId+'_zoom').slider('value', 0.25 + $('#'+sId+'_zoom').slider('value')); return false; });
  454. $('#'+sId+'_zoom_minus').click(function() { $('#'+sId+'_zoom').slider('value', $('#'+sId+'_zoom').slider('value') - 0.25); return false; });
  455. $('#'+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});
  456. $('#'+sId+'_refresh_btn').button().click(function() { me.reload(); });
  457. },
  458. _build_context_menus: function()
  459. {
  460. var sId = this.element.attr('id');
  461. var me = this;
  462. $.contextMenu({
  463. selector: '#'+sId+' .popupMenuTarget',
  464. build: function(trigger, e) {
  465. // this callback is executed every time the menu is to be shown
  466. // its results are destroyed every time the menu is hidden
  467. // e is the original contextmenu event, containing e.pageX and e.pageY (amongst other data)
  468. var sType = trigger.attr('data-type');
  469. var sNodeId = trigger.attr('data-id');
  470. var oNode = me._find_node(sNodeId);
  471. clearTimeout(trigger.data('openTimeoutId'));
  472. /*
  473. var sObjName = trigger.attr('data-class');
  474. var sIndex = trigger.attr('data-index');
  475. var originalEvent = e;
  476. var bHasItems = false;
  477. */
  478. var oResult = {callback: null, items: {}};
  479. switch(sType)
  480. {
  481. case 'group':
  482. var sGroupIndex = oNode.group_index;
  483. if( $('#relation_group_'+sGroupIndex).length > 0)
  484. {
  485. oResult = {
  486. callback: function(key, options) {
  487. var me = $('.itop-simple-graph').data('itopSimple_graph'); // need a live value
  488. me.show_group('relation_group_'+sGroupIndex);
  489. },
  490. items: { 'show': {name: me.options.drill_down.label } }
  491. };
  492. }
  493. break;
  494. case 'icon':
  495. var sObjClass = oNode.obj_class;
  496. var sObjKey = oNode.obj_key;
  497. oResult = {
  498. callback: function(key, options) {
  499. var me = $('.itop-simple-graph').data('itopSimple_graph'); // need a live value
  500. var sURL = me.options.drill_down.url.replace('%1$s', sObjClass).replace('%2$s', sObjKey);
  501. window.location.href = sURL;
  502. },
  503. items: { 'details': {name: me.options.drill_down.label } }
  504. };
  505. break;
  506. default:
  507. oResult = false; // No context menu
  508. }
  509. return oResult;
  510. }
  511. });
  512. },
  513. export_as_pdf: function()
  514. {
  515. this._export_dlg(this.options.labels.export_pdf_title, this.options.export_as_pdf.url, 'download_pdf');
  516. },
  517. _export_dlg: function(sTitle, sSubmitUrl, sOperation)
  518. {
  519. var sId = this.element.attr('id');
  520. var me = this;
  521. var oPositions = {};
  522. for(k in this.aNodes)
  523. {
  524. oPositions[this.aNodes[k].id] = {x: this.aNodes[k].x, y: this.aNodes[k].y };
  525. }
  526. var sHtmlForm = '<div id="GraphExportDlg'+this.element.attr('id')+'"><form id="graph_'+this.element.attr('id')+'_export_dlg" target="_blank" action="'+sSubmitUrl+'" method="post">';
  527. sHtmlForm += '<input type="hidden" name="g" value="'+this.options.grouping_threshold+'">';
  528. sHtmlForm += '<input type="hidden" name="context_key" value="'+this.options.context_key+'">';
  529. $('#'+sId+'_contexts').multiselect('getChecked').each(function() {
  530. sHtmlForm += '<input type="hidden" name="contexts['+$(this).val()+']" value="'+me.options.additional_contexts[$(this).val()].oql+'">';
  531. });
  532. sHtmlForm += '<input type="hidden" name="positions" value="">';
  533. for(k in this.options.excluded_classes)
  534. {
  535. sHtmlForm += '<input type="hidden" name="excluded_classes[]" value="'+this.options.excluded_classes[k]+'">';
  536. }
  537. for(var k1 in this.options.sources)
  538. {
  539. for(var k2 in this.options.sources[k1])
  540. {
  541. sHtmlForm += '<input type="hidden" name="sources['+k1+'][]" value="'+this.options.sources[k1][k2]+'">';
  542. }
  543. }
  544. for(var k1 in this.options.excluded)
  545. {
  546. for(var k2 in this.options.excluded[k1])
  547. {
  548. sHtmlForm += '<input type="hidden" name="excluded['+k1+'][]" value="'+this.options.excluded[k1][k2]+'">';
  549. }
  550. }
  551. if (sOperation == 'attachment')
  552. {
  553. sHtmlForm += '<input type="hidden" name="obj_class" value="'+this.options.export_as_attachment.obj_class+'">';
  554. sHtmlForm += '<input type="hidden" name="obj_key" value="'+this.options.export_as_attachment.obj_key+'">';
  555. }
  556. sHtmlForm += '<table>';
  557. sHtmlForm += '<tr><td>'+this.options.page_format.label+'</td><td><select name="p">';
  558. for(k in this.options.page_format.values)
  559. {
  560. var sSelected = (k == this.options.page_format['default']) ? ' selected' : '';
  561. sHtmlForm += '<option value="'+k+'"'+sSelected+'>'+this.options.page_format.values[k]+'</option>';
  562. }
  563. sHtmlForm += '</select></td></tr>';
  564. sHtmlForm += '<tr><td>'+this.options.page_orientation.label+'</td><td><select name="o">';
  565. for(k in this.options.page_orientation.values)
  566. {
  567. var sSelected = (k == this.options.page_orientation['default']) ? ' selected' : '';
  568. sHtmlForm += '<option value="'+k+'"'+sSelected+'>'+this.options.page_orientation.values[k]+'</option>';
  569. }
  570. sHtmlForm += '</select></td></tr>';
  571. sHtmlForm += '<tr><td>'+this.options.labels.title+'</td><td><input name="title" value="'+this.options.labels.untitled+'" style="width: 20em;"/></td></tr>';
  572. sHtmlForm += '<tr><td>'+this.options.labels.comments+'</td><td><textarea style="width: 20em; height:5em;" name="comments"/></textarea></td></tr>';
  573. 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>';
  574. sHtmlForm += '<table>';
  575. sHtmlForm += '</form></div>';
  576. $('body').append(sHtmlForm);
  577. $('#graph_'+this.element.attr('id')+'_export_dlg input[name="positions"]').val(JSON.stringify(oPositions));
  578. var me = this;
  579. if (sOperation == 'attachment')
  580. {
  581. $('#GraphExportDlg'+this.element.attr('id')+' form').submit(function() { return me._on_export_as_attachment(); });
  582. }
  583. $('#GraphExportDlg'+this.element.attr('id')).dialog({
  584. width: 'auto',
  585. modal: true,
  586. title: sTitle,
  587. close: function() { $(this).remove(); },
  588. buttons: [
  589. {text: this.options.labels['cancel'], click: function() { $(this).dialog('close');} },
  590. {text: this.options.labels['export'], click: function() { $('#graph_'+me.element.attr('id')+'_export_dlg').submit(); $(this).dialog('close');} },
  591. ]
  592. });
  593. },
  594. _on_zoom_change: function(sliderValue)
  595. {
  596. if(!this.bInUpdateSliderZoom)
  597. {
  598. var Z0 = this.fSliderZoom;
  599. var X = this.xOffset - this.element.width()/2;
  600. var Y = this.yOffset - this.element.height()/2;
  601. this.fSliderZoom = Math.pow(2 , (sliderValue - 1));
  602. var Z1 = this.fSliderZoom = Math.pow(2 , (sliderValue - 1));
  603. var dx = X * (1 - Z1/Z0);
  604. var dy = Y * (1 - Z1/Z0);
  605. this.xPan += dx;
  606. this.yPan += dy;
  607. this._close_all_tooltips();
  608. this.oPaper.setViewBox(this.xPan, this.yPan, this.element.width(), this.element.height(), false);
  609. this.draw();
  610. }
  611. },
  612. _on_mousewheel: function(event, delta, deltaX, deltaY)
  613. {
  614. var fStep = 0.25*delta;
  615. var sId = this.element.attr('id');
  616. $('#'+sId+'_zoom').slider('value', fStep + $('#'+sId+'_zoom').slider('value'));
  617. },
  618. _on_resize: function()
  619. {
  620. this.element.closest('.ui-tabs').tabs({ heightStyle: "fill" });
  621. this.auto_scale();
  622. this._close_all_tooltips();
  623. this.draw();
  624. },
  625. _on_tabs_activate: function(ui)
  626. {
  627. if (ui.newPanel.selector == ('#'+this.sTabId))
  628. {
  629. if (this.bRedrawNeeded)
  630. {
  631. this._updateBBox();
  632. this.auto_scale();
  633. this.oPaper.setSize(this.element.width(), this.element.height());
  634. this._reset_pan_and_zoom();
  635. this.draw();
  636. bRedrawNeeded = false;
  637. }
  638. }
  639. },
  640. load: function(oData)
  641. {
  642. var me = this;
  643. var sId = this.element.attr('id');
  644. this.aNodes = [];
  645. this.aEdges = [];
  646. for(k in oData.nodes)
  647. {
  648. this.add_node(oData.nodes[k]);
  649. }
  650. for(k in oData.edges)
  651. {
  652. this.add_edge(oData.edges[k]);
  653. }
  654. if (oData.groups)
  655. {
  656. this.refresh_groups(oData.groups);
  657. }
  658. if (this.element.is(':visible'))
  659. {
  660. this._updateBBox();
  661. this.auto_scale();
  662. this._reset_pan_and_zoom();
  663. this.draw();
  664. }
  665. else
  666. {
  667. this.bRedrawNeeded = true;
  668. }
  669. },
  670. refresh_groups: function(aGroups)
  671. {
  672. if ($('#impacted_groups').length > 0)
  673. {
  674. // The "Groups" tab is present, refresh it
  675. if (aGroups.length == 0)
  676. {
  677. this.element.closest('.ui-tabs').tabs("disable", 2);
  678. $('#impacted_groups').html('');
  679. }
  680. else
  681. {
  682. this.element.closest('.ui-tabs').tabs("enable", 2);
  683. $('#impacted_groups').html('<img src="../images/indicator.gif">');
  684. var sUrl = GetAbsoluteUrlAppRoot()+'pages/ajax.render.php';
  685. $.post(sUrl, { operation: 'relation_groups', groups: aGroups }, function(data) {
  686. $('#impacted_groups').html(data);
  687. });
  688. }
  689. }
  690. },
  691. _reset_pan_and_zoom: function()
  692. {
  693. this.xPan = 0;
  694. this.yPan = 0;
  695. var sId = this.element.attr('id');
  696. this.bInUpdateSliderZoom = true;
  697. $('#'+sId+'_zoom').slider('value', 1);
  698. this.fSliderZoom = 1.0;
  699. this.bInUpdateSliderZoom = false;
  700. this.oPaper.setViewBox(this.xPan, this.yPan, this.element.width(), this.element.height(), false);
  701. },
  702. load_from_url: function(sUrl)
  703. {
  704. this.options.load_from_url = sUrl;
  705. var me = this;
  706. var sId = this.element.attr('id');
  707. this.options.grouping_threshold = $('#'+sId+'_grouping_threshold').val();
  708. if (this.options.grouping_threshold < 2)
  709. {
  710. this.options.grouping_threshold = 2;
  711. $('#'+sId+'_grouping_threshold').val(this.options.grouping_threshold);
  712. }
  713. var aContexts = [];
  714. $('#'+sId+'_contexts').multiselect('getChecked').each(function() { aContexts[$(this).val()] = me.options.additional_contexts[$(this).val()].oql; });
  715. this.element.closest('.ui-tabs').tabs({ heightStyle: "fill" });
  716. this.adjust_height();
  717. this._close_all_tooltips();
  718. this.oPaper.rect(this.xPan, this.yPan, this.element.width(), this.element.height()).attr({fill: '#000', opacity: 0.4, 'stroke-width': 0});
  719. this.oPaper.rect(this.xPan + this.element.width()/2 - 100, this.yPan + this.element.height()/2 - 10, 200, 20)
  720. .attr({fill: 'url(../setup/orange-progress.gif)', stroke: '#000', 'stroke-width': 1});
  721. this.oPaper.text(this.xPan + this.element.width()/2, this.yPan + this.element.height()/2 - 20, this.options.labels.loading);
  722. $('#'+sId+'_refresh_btn').button('disable');
  723. $.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) {
  724. me.load(data);
  725. $('#'+sId+'_refresh_btn').button('enable');
  726. }, 'json');
  727. },
  728. export_as_attachment: function()
  729. {
  730. this._export_dlg(this.options.labels.export_as_attachment_title, this.options.export_as_attachment.url, 'attachment');
  731. },
  732. _on_export_as_attachment: function()
  733. {
  734. var oParams = {};
  735. var oPositions = {};
  736. var jForm = $('#GraphExportDlg'+this.element.attr('id')+' form');
  737. for(k in this.aNodes)
  738. {
  739. oPositions[this.aNodes[k].id] = {x: this.aNodes[k].x, y: this.aNodes[k].y };
  740. }
  741. oParams.positions = JSON.stringify(oPositions);
  742. oParams.sources = this.options.sources;
  743. oParams.excluded_classes = this.options.excluded_classes;
  744. oParams.title = jForm.find(':input[name="title"]').val();
  745. oParams.comments = jForm.find(':input[name="comments"]').val();
  746. oParams.include_list = jForm.find(':input[name="include_list"]:checked').length;
  747. oParams.o = jForm.find(':input[name="o"]').val();
  748. oParams.p = jForm.find(':input[name="p"]').val();
  749. oParams.obj_class = this.options.export_as_attachment.obj_class;
  750. oParams.obj_key = this.options.export_as_attachment.obj_key;
  751. oParams.contexts = [];
  752. var me = this;
  753. $('#'+this.element.attr('id')+'_contexts').multiselect('getChecked').each(function() {
  754. oParams.contexts[$(this).val()] = me.options.additional_contexts[$(this).val()].oql;
  755. });
  756. oParams.context_key = this.options.context_key;
  757. var sUrl = jForm.attr('action');
  758. var sTitle = oParams.title;
  759. var jPanel = $('#attachments').closest('.ui-tabs-panel');
  760. var jTab = null;
  761. var sTabText = null;
  762. if (jPanel.length > 0)
  763. {
  764. var sTabId = jPanel.attr('id');
  765. jTab = $('li[aria-controls='+sTabId+']');
  766. sTabText = jTab.find('span').html();
  767. jTab.find('span').html(sTabText+' <img style="vertical-align:bottom" src="../images/indicator.gif">');
  768. }
  769. $.post(sUrl, oParams, function(data) {
  770. var sDownloadLink = GetAbsoluteUrlAppRoot()+'pages/ajax.render.php?operation=download_document&class=Attachment&id='+data.att_id+'&field=contents';
  771. var sIcon = GetAbsoluteUrlModulesRoot()+'itop-attachments/icons/pdf.png';
  772. if (jTab != null)
  773. {
  774. var re = /^([^(]+)\(([0-9]+)\)(.*)$/;
  775. var aParts = re.exec(sTabText);
  776. if (aParts == null)
  777. {
  778. // First attachment
  779. $('#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>');
  780. jTab.find('span').html(sTabText +' (1)');
  781. }
  782. else
  783. {
  784. $('#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>');
  785. var iPrevCount = parseInt(aParts[2], 10);
  786. jTab.find('span').html(aParts[1]+'('+(1 + iPrevCount)+')'+aParts[3]);
  787. }
  788. }
  789. }, 'json');
  790. return false;
  791. },
  792. reload: function()
  793. {
  794. this.load_from_url(this.options.load_from_url);
  795. },
  796. _make_tooltips: function()
  797. {
  798. var me = this;
  799. $( ".popupMenuTarget" ).tooltip({
  800. content: function() {
  801. var sDataId = $(this).attr('data-id');
  802. var sTooltipContent = '<div class="tooltip-close-button" data-id="'+sDataId+'" style="display:inline-block; float:right; cursor:pointer; padding-left:0.25em;">×</div>';
  803. sTooltipContent += me._get_tooltip_content(sDataId);
  804. return sTooltipContent;
  805. },
  806. items: '.popupMenuTarget',
  807. tooltipClass: 'tooltip-simple-graph',
  808. position: {
  809. my: "center bottom-10",
  810. at: "center top",
  811. using: function( position, feedback ) {
  812. $(this).css( position );
  813. $( "<div>" )
  814. .addClass( "arrow" )
  815. .addClass( feedback.vertical )
  816. .appendTo( this );
  817. }
  818. }
  819. })
  820. .off( "mouseover mouseout" )
  821. .on( "mouseover", function(event){
  822. event.stopImmediatePropagation();
  823. var jMe = $(this);
  824. jMe.data('openTimeoutId', setTimeout(function() {
  825. var sDataId = jMe.attr('data-id');
  826. if ($('.tooltip-close-button[data-id="'+sDataId+'"]').length == 0)
  827. {
  828. jMe.data('openTimeoutId', 0);
  829. jMe.tooltip('open');
  830. }
  831. }, 1000));
  832. })
  833. .on( "mouseout", function(event){
  834. event.stopImmediatePropagation();
  835. clearTimeout($(this).data('openTimeoutId'));
  836. });
  837. /* Happens at every on_drag_end !!!
  838. .on( "click", function(){
  839. var sDataId = $(this).attr('data-id');
  840. if ($('.tooltip-close-button[data-id="'+sDataId+'"]').length == 0)
  841. {
  842. $(this).tooltip( 'open' );
  843. }
  844. else
  845. {
  846. $(this).tooltip( 'close' );
  847. }
  848. $( this ).unbind( "mouseleave" );
  849. return false;
  850. });
  851. */
  852. $('body').on('click', '.tooltip-close-button', function() {
  853. var sDataId = $(this).attr('data-id');
  854. $('.popupMenuTarget[data-id="'+sDataId+'"]').tooltip('close');
  855. });
  856. this.element.on("click", ":not(.tooltip-simple-graph *,.tooltip-simple-graph)", function(){
  857. $('.popupMenuTarget').each(function (i) {
  858. clearTimeout($(this).data('openTimeoutId'));
  859. $(this).data('openTimeoutId', 0);
  860. $(this).tooltip("close");
  861. });
  862. });
  863. },
  864. _get_tooltip_content: function(sNodeId)
  865. {
  866. var oNode = this._find_node(sNodeId);
  867. if (oNode !== null)
  868. {
  869. return oNode.tooltip;
  870. }
  871. return '<p>Node Id:'+sNodeId+'</p>';
  872. },
  873. _close_all_tooltips: function()
  874. {
  875. this.element.find('.popupMenuTarget').each(function() {
  876. clearTimeout($(this).data('openTimeoutId'));
  877. $(this).data('openTimeoutId', 0);
  878. $(this).tooltip('close');
  879. });
  880. },
  881. _on_background_drag_start: function(x, y, event)
  882. {
  883. this.bDragging = true;
  884. this.xDrag = 0;
  885. this.yDrag = 0;
  886. //this._close_all_tooltips();
  887. },
  888. _on_background_move: function(dx, dy, x, y, event)
  889. {
  890. if (this.bDragging)
  891. {
  892. this.xDrag = dx;
  893. this.yDrag = dy;
  894. this.oPaper.setViewBox(this.xPan - this.xDrag, this.yPan - this.yDrag, this.element.width(), this.element.height(), false);
  895. }
  896. },
  897. _on_background_drag_end: function(event)
  898. {
  899. if (this.bDragging)
  900. {
  901. this.xPan -= this.xDrag;
  902. this.yPan -= this.yDrag;
  903. this.xDrag = 0;
  904. this.yDrag = 0;
  905. this.bDragging = false;
  906. }
  907. },
  908. });
  909. });