simple_graph.js 33 KB

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