simple_graph.js 33 KB

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