simple_graph.js 27 KB

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