simple_graph.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750
  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. /*
  423. var sObjName = trigger.attr('data-class');
  424. var sIndex = trigger.attr('data-index');
  425. var originalEvent = e;
  426. var bHasItems = false;
  427. */
  428. var oResult = {callback: null, items: {}};
  429. switch(sType)
  430. {
  431. case 'group':
  432. var sGroupIndex = oNode.group_index;
  433. if( $('#relation_group_'+sGroupIndex).length > 0)
  434. {
  435. oResult = {
  436. callback: function(key, options) {
  437. var me = $('.itop-simple-graph').data('itopSimple_graph'); // need a live value
  438. me.show_group('relation_group_'+sGroupIndex);
  439. },
  440. items: { 'show': {name: me.options.drill_down.label } }
  441. };
  442. }
  443. break;
  444. case 'icon':
  445. var sObjClass = oNode.obj_class;
  446. var sObjKey = oNode.obj_key;
  447. oResult = {
  448. callback: function(key, options) {
  449. var me = $('.itop-simple-graph').data('itopSimple_graph'); // need a live value
  450. var sURL = me.options.drill_down.url.replace('%1$s', sObjClass).replace('%2$s', sObjKey);
  451. window.location.href = sURL;
  452. },
  453. items: { 'details': {name: me.options.drill_down.label } }
  454. };
  455. break;
  456. default:
  457. oResult = false; // No context menu
  458. }
  459. return oResult;
  460. }
  461. });
  462. },
  463. export_as_pdf: function()
  464. {
  465. this._export_dlg(this.options.labels.export_pdf_title, this.options.export_as_pdf.url, 'download_pdf');
  466. },
  467. _export_dlg: function(sTitle, sSubmitUrl, sOperation)
  468. {
  469. var sId = this.element.attr('id');
  470. var me = this;
  471. var oPositions = {};
  472. for(k in this.aNodes)
  473. {
  474. oPositions[this.aNodes[k].id] = {x: this.aNodes[k].x, y: this.aNodes[k].y };
  475. }
  476. var sHtmlForm = '<div id="GraphExportDlg'+this.element.attr('id')+'"><form id="graph_'+this.element.attr('id')+'_export_dlg" target="_blank" action="'+sSubmitUrl+'" method="post">';
  477. sHtmlForm += '<input type="hidden" name="g" value="'+this.options.grouping_threshold+'">';
  478. sHtmlForm += '<input type="hidden" name="context_key" value="'+this.options.context_key+'">';
  479. $('#'+sId+'_contexts').multiselect('getChecked').each(function() {
  480. sHtmlForm += '<input type="hidden" name="contexts['+$(this).val()+']" value="'+me.options.additional_contexts[$(this).val()].oql+'">';
  481. });
  482. sHtmlForm += '<input type="hidden" name="positions" value="">';
  483. for(k in this.options.excluded_classes)
  484. {
  485. sHtmlForm += '<input type="hidden" name="excluded_classes[]" value="'+this.options.excluded_classes[k]+'">';
  486. }
  487. for(var k1 in this.options.sources)
  488. {
  489. for(var k2 in this.options.sources[k1])
  490. {
  491. sHtmlForm += '<input type="hidden" name="sources['+k1+'][]" value="'+this.options.sources[k1][k2]+'">';
  492. }
  493. }
  494. for(var k1 in this.options.excluded)
  495. {
  496. for(var k2 in this.options.excluded[k1])
  497. {
  498. sHtmlForm += '<input type="hidden" name="excluded['+k1+'][]" value="'+this.options.excluded[k1][k2]+'">';
  499. }
  500. }
  501. if (sOperation == 'attachment')
  502. {
  503. sHtmlForm += '<input type="hidden" name="obj_class" value="'+this.options.export_as_attachment.obj_class+'">';
  504. sHtmlForm += '<input type="hidden" name="obj_key" value="'+this.options.export_as_attachment.obj_key+'">';
  505. }
  506. sHtmlForm += '<table>';
  507. sHtmlForm += '<tr><td>'+this.options.page_format.label+'</td><td><select name="p">';
  508. for(k in this.options.page_format.values)
  509. {
  510. var sSelected = (k == this.options.page_format['default']) ? ' selected' : '';
  511. sHtmlForm += '<option value="'+k+'"'+sSelected+'>'+this.options.page_format.values[k]+'</option>';
  512. }
  513. sHtmlForm += '</select></td></tr>';
  514. sHtmlForm += '<tr><td>'+this.options.page_orientation.label+'</td><td><select name="o">';
  515. for(k in this.options.page_orientation.values)
  516. {
  517. var sSelected = (k == this.options.page_orientation['default']) ? ' selected' : '';
  518. sHtmlForm += '<option value="'+k+'"'+sSelected+'>'+this.options.page_orientation.values[k]+'</option>';
  519. }
  520. sHtmlForm += '</select></td></tr>';
  521. sHtmlForm += '<tr><td>'+this.options.labels.title+'</td><td><input name="title" value="'+this.options.labels.untitled+'" style="width: 20em;"/></td></tr>';
  522. sHtmlForm += '<tr><td>'+this.options.labels.comments+'</td><td><textarea style="width: 20em; height:5em;" name="comments"/></textarea></td></tr>';
  523. 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>';
  524. sHtmlForm += '<table>';
  525. sHtmlForm += '</form></div>';
  526. $('body').append(sHtmlForm);
  527. $('#graph_'+this.element.attr('id')+'_export_dlg input[name="positions"]').val(JSON.stringify(oPositions));
  528. var me = this;
  529. if (sOperation == 'attachment')
  530. {
  531. $('#GraphExportDlg'+this.element.attr('id')+' form').submit(function() { return me._on_export_as_attachment(); });
  532. }
  533. $('#GraphExportDlg'+this.element.attr('id')).dialog({
  534. width: 'auto',
  535. modal: true,
  536. title: sTitle,
  537. close: function() { $(this).remove(); },
  538. buttons: [
  539. {text: this.options.labels['cancel'], click: function() { $(this).dialog('close');} },
  540. {text: this.options.labels['export'], click: function() { $('#graph_'+me.element.attr('id')+'_export_dlg').submit(); $(this).dialog('close');} },
  541. ]
  542. });
  543. },
  544. _on_resize: function()
  545. {
  546. this.element.closest('.ui-tabs').tabs({ heightStyle: "fill" });
  547. this.auto_scale();
  548. this.oPaper.setSize(this.element.width(), this.element.height());
  549. this.draw();
  550. },
  551. load: function(oData)
  552. {
  553. this.aNodes = [];
  554. this.aEdges = [];
  555. for(k in oData.nodes)
  556. {
  557. this.add_node(oData.nodes[k]);
  558. }
  559. for(k in oData.edges)
  560. {
  561. this.add_edge(oData.edges[k]);
  562. }
  563. this._updateBBox();
  564. this.auto_scale();
  565. this.oPaper.setSize(this.element.width(), this.element.height());
  566. this.draw();
  567. },
  568. load_from_url: function(sUrl)
  569. {
  570. this.options.load_from_url = sUrl;
  571. var me = this;
  572. var sId = this.element.attr('id');
  573. this.options.grouping_threshold = $('#'+sId+'_grouping_threshold').val();
  574. if (this.options.grouping_threshold < 2)
  575. {
  576. this.options.grouping_threshold = 2;
  577. $('#'+sId+'_grouping_threshold').val(this.options.grouping_threshold);
  578. }
  579. var aContexts = [];
  580. $('#'+sId+'_contexts').multiselect('getChecked').each(function() { aContexts[$(this).val()] = me.options.additional_contexts[$(this).val()].oql; });
  581. this.element.closest('.ui-tabs').tabs({ heightStyle: "fill" });
  582. this._close_all_tooltips();
  583. this.oPaper.rect(0, 0, this.element.width(), this.element.height()).attr({fill: '#000', opacity: 0.4, 'stroke-width': 0});
  584. $.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) {
  585. me.load(data);
  586. }, 'json');
  587. },
  588. export_as_attachment: function()
  589. {
  590. this._export_dlg(this.options.labels.export_as_attachment_title, this.options.export_as_attachment.url, 'attachment');
  591. },
  592. _on_export_as_attachment: function()
  593. {
  594. var oParams = {};
  595. var oPositions = {};
  596. var jForm = $('#GraphExportDlg'+this.element.attr('id')+' form');
  597. for(k in this.aNodes)
  598. {
  599. oPositions[this.aNodes[k].id] = {x: this.aNodes[k].x, y: this.aNodes[k].y };
  600. }
  601. oParams.positions = JSON.stringify(oPositions);
  602. oParams.sources = this.options.sources;
  603. oParams.excluded_classes = this.options.excluded_classes;
  604. oParams.title = jForm.find(':input[name="title"]').val();
  605. oParams.comments = jForm.find(':input[name="comments"]').val();
  606. oParams.include_list = jForm.find(':input[name="include_list"]:checked').length;
  607. oParams.o = jForm.find(':input[name="o"]').val();
  608. oParams.p = jForm.find(':input[name="p"]').val();
  609. oParams.obj_class = this.options.export_as_attachment.obj_class;
  610. oParams.obj_key = this.options.export_as_attachment.obj_key;
  611. var sUrl = jForm.attr('action');
  612. var sTitle = oParams.title;
  613. var jPanel = $('#attachments').closest('.ui-tabs-panel');
  614. var jTab = null;
  615. var sTabText = null;
  616. if (jPanel.length > 0)
  617. {
  618. var sTabId = jPanel.attr('id');
  619. jTab = $('li[aria-controls='+sTabId+']');
  620. sTabText = jTab.find('span').html();
  621. jTab.find('span').html(sTabText+' <img style="vertical-align:bottom" src="../images/indicator.gif">');
  622. }
  623. $.post(sUrl, oParams, function(data) {
  624. var sDownloadLink = GetAbsoluteUrlAppRoot()+'pages/ajax.render.php?operation=download_document&class=Attachment&id='+data.att_id+'&field=contents';
  625. var sIcon = GetAbsoluteUrlModulesRoot()+'itop-attachments/icons/pdf.png';
  626. $('#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>');
  627. if (jTab != null)
  628. {
  629. var re = /^([^(]+)\(([0-9]+)\)(.*)$/;
  630. var aParts = re.exec(sTabText);
  631. var iPrevCount = parseInt(aParts[2], 10);
  632. jTab.find('span').html(aParts[1]+'('+(1 + iPrevCount)+')'+aParts[3]);
  633. }
  634. }, 'json');
  635. return false;
  636. },
  637. reload: function()
  638. {
  639. this.load_from_url(this.options.load_from_url);
  640. },
  641. _make_tooltips: function()
  642. {
  643. var me = this;
  644. $( ".popupMenuTarget" ).tooltip({
  645. content: function() {
  646. var sDataId = $(this).attr('data-id');
  647. var sTooltipContent = '<div class="tooltip-close-button" data-id="'+sDataId+'" style="display:inline-block; float:right; cursor:pointer; padding-left:0.25em;">×</div>';
  648. sTooltipContent += me._get_tooltip_content(sDataId);
  649. return sTooltipContent;
  650. },
  651. items: '.popupMenuTarget',
  652. position: {
  653. my: "center bottom-10",
  654. at: "center top",
  655. using: function( position, feedback ) {
  656. $(this).css( position );
  657. $( "<div>" )
  658. .addClass( "arrow" )
  659. .addClass( feedback.vertical )
  660. .addClass( feedback.horizontal )
  661. .appendTo( this );
  662. }
  663. }
  664. })
  665. .off( "mouseover mouseout" )
  666. .on( "mouseover", function(event){
  667. event.stopImmediatePropagation();
  668. var jMe = $(this);
  669. $(this).data('openTimeoutId', setTimeout(function() {
  670. var sDataId = jMe.attr('data-id');
  671. if ($('.tooltip-close-button[data-id="'+sDataId+'"]').length == 0)
  672. {
  673. jMe.tooltip('open');
  674. }
  675. }, 500));
  676. })
  677. .on( "mouseout", function(event){
  678. event.stopImmediatePropagation();
  679. clearTimeout($(this).data('openTimeoutId'));
  680. });
  681. /* Happens at every on_drag_end !!!
  682. .on( "click", function(){
  683. var sDataId = $(this).attr('data-id');
  684. if ($('.tooltip-close-button[data-id="'+sDataId+'"]').length == 0)
  685. {
  686. $(this).tooltip( 'open' );
  687. }
  688. else
  689. {
  690. $(this).tooltip( 'close' );
  691. }
  692. $( this ).unbind( "mouseleave" );
  693. return false;
  694. });
  695. */
  696. $('body').on('click', '.tooltip-close-button', function() {
  697. var sDataId = $(this).attr('data-id');
  698. $('.popupMenuTarget[data-id="'+sDataId+'"]').tooltip('close');
  699. });
  700. },
  701. _get_tooltip_content: function(sNodeId)
  702. {
  703. var oNode = this._find_node(sNodeId);
  704. if (oNode !== null)
  705. {
  706. return oNode.tooltip;
  707. }
  708. return '<p>Node Id:'+sNodeId+'</p>';
  709. },
  710. _close_all_tooltips: function()
  711. {
  712. this.element.find('.popupMenuTarget').tooltip('close');
  713. }
  714. });
  715. });