simple_graph.js 35 KB

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