extkeywidget.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  1. // Copyright (C) 2010-2012 Combodo SARL
  2. //
  3. // This file is part of iTop.
  4. //
  5. // iTop is free software; you can redistribute it and/or modify
  6. // it under the terms of the GNU Affero General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // iTop is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU Affero General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Affero General Public License
  16. // along with iTop. If not, see <http://www.gnu.org/licenses/>
  17. function ExtKeyWidget(id, sTargetClass, sFilter, sTitle, bSelectMode, oWizHelper, sAttCode, bSearchMode)
  18. {
  19. this.id = id;
  20. this.sTargetClass = sTargetClass;
  21. this.sFilter = sFilter;
  22. this.sTitle = sTitle;
  23. this.sAttCode = sAttCode;
  24. this.emptyHtml = ''; // content to be displayed when the search results are empty (when opening the dialog)
  25. this.emptyOnClose = true; // Workaround for the JQuery dialog being very slow when opening and closing if the content contains many INPUT tags
  26. this.oWizardHelper = oWizHelper;
  27. this.ajax_request = null;
  28. this.bSelectMode = bSelectMode; // true if the edited field is a SELECT, false if it's an autocomplete
  29. this.bSearchMode = bSearchMode; // true if selecting a value in the context of a search form
  30. this.v_html = '';
  31. var me = this;
  32. this.Init = function()
  33. {
  34. // make sure that the form is clean
  35. $('#'+this.id+'_btnRemove').attr('disabled','disabled');
  36. $('#'+this.id+'_linksToRemove').val('');
  37. };
  38. this.StopPendingRequest = function()
  39. {
  40. if (me.ajax_request)
  41. {
  42. me.ajax_request.abort();
  43. me.ajax_request = null;
  44. }
  45. };
  46. this.Search = function()
  47. {
  48. if($('#'+me.id).attr('disabled')) return; // Disabled, do nothing
  49. var value = $('#'+me.id).val(); // Current value
  50. // Query the server to get the form to search for target objects
  51. if (me.bSelectMode)
  52. {
  53. me.v_html = $('#v_'+me.id).html();
  54. $('#v_'+me.id).html('<img src="../images/indicator.gif" />');
  55. }
  56. else
  57. {
  58. $('#label_'+me.id).addClass('ac_dlg_loading');
  59. }
  60. var theMap = { sAttCode: me.sAttCode,
  61. iInputId: me.id,
  62. sTitle: me.sTitle,
  63. sAttCode: me.sAttCode,
  64. sTargetClass: me.sTargetClass,
  65. bSearchMode: me.bSearchMode,
  66. operation: 'objectSearchForm'
  67. };
  68. if (me.oWizardHelper == null)
  69. {
  70. theMap['json'] = '';
  71. }
  72. else
  73. {
  74. // Not inside a "search form", updating a real object
  75. me.oWizardHelper.UpdateWizard();
  76. theMap['json'] = me.oWizardHelper.ToJSON();
  77. }
  78. // Make sure that we cancel any pending request before issuing another
  79. // since responses may arrive in arbitrary order
  80. me.StopPendingRequest();
  81. // Run the query and get the result back directly in HTML
  82. me.ajax_request = $.post( AddAppContext(GetAbsoluteUrlAppRoot()+'pages/ajax.render.php'), theMap,
  83. function(data)
  84. {
  85. $('#ac_dlg_'+me.id).html(data);
  86. $('#ac_dlg_'+me.id).dialog('open');
  87. me.UpdateSizes();
  88. me.UpdateButtons();
  89. me.ajax_request = null;
  90. FixSearchFormsDisposition();
  91. me.DoSearchObjects();
  92. },
  93. 'html'
  94. );
  95. };
  96. this.UpdateSizes = function()
  97. {
  98. var dlg = $('#ac_dlg_'+me.id);
  99. // Adjust the dialog's size to fit into the screen
  100. if (dlg.width() > ($(window).width()-40))
  101. {
  102. dlg.width($(window).width()-40);
  103. }
  104. if (dlg.height() > ($(window).height()-70))
  105. {
  106. dlg.height($(window).height()-70);
  107. }
  108. var searchForm = dlg.find('div.display_block:first'); // Top search form, enclosing display_block
  109. var results = $('#dr_'+me.id);
  110. var oPadding = {};
  111. var aKeys = ['top', 'right', 'bottom', 'left'];
  112. for(k in aKeys)
  113. {
  114. oPadding[aKeys[k]] = 0;
  115. if (dlg.css('padding-'+aKeys[k]))
  116. {
  117. oPadding[aKeys[k]] = parseInt(dlg.css('padding-'+aKeys[k]).replace('px', ''));
  118. }
  119. }
  120. width = dlg.innerWidth() - oPadding['right'] - oPadding['left'] - 22; // 5 (margin-left) + 5 (padding-left) + 5 (padding-right) + 5 (margin-right) + 2 for rounding !
  121. height = dlg.innerHeight() - oPadding['top'] - oPadding['bottom'] -22;
  122. form_height = searchForm.outerHeight();
  123. results.height(height - form_height - 40); // Leave some space for the buttons
  124. };
  125. this.UpdateButtons = function()
  126. {
  127. var okBtn = $('#btn_ok_'+me.id);
  128. if ($('#count_'+me.id).val() > 0)
  129. {
  130. okBtn.removeAttr('disabled');
  131. }
  132. else
  133. {
  134. okBtn.attr('disabled', 'disabled');
  135. }
  136. };
  137. this.DoSearchObjects = function(id)
  138. {
  139. var theMap = { sTargetClass: me.sTargetClass,
  140. iInputId: me.id,
  141. sFilter: me.sFilter,
  142. bSearchMode: me.bSearchMode
  143. };
  144. // Gather the parameters from the search form
  145. $('#fs_'+me.id+' :input').each( function() {
  146. if (this.name != '')
  147. {
  148. var val = $(this).val(); // supports multiselect as well
  149. if (val !== null)
  150. {
  151. theMap[this.name] = val;
  152. }
  153. }
  154. });
  155. if (me.oWizardHelper == null)
  156. {
  157. theMap['json'] = '';
  158. }
  159. else
  160. {
  161. // Not inside a "search form", updating a real object
  162. me.oWizardHelper.UpdateWizard();
  163. theMap['json'] = me.oWizardHelper.ToJSON();
  164. }
  165. theMap['sRemoteClass'] = theMap['class']; // swap 'class' (defined in the form) and 'remoteClass'
  166. theMap.operation = 'searchObjectsToSelect'; // Override what is defined in the form itself
  167. theMap.sAttCode = me.sAttCode,
  168. sSearchAreaId = '#dr_'+me.id;
  169. //$(sSearchAreaId).html('<div style="text-align:center;width:100%;height:24px;vertical-align:middle;"><img src="../images/indicator.gif" /></div>');
  170. $(sSearchAreaId).block();
  171. me.UpdateButtons();
  172. // Make sure that we cancel any pending request before issuing another
  173. // since responses may arrive in arbitrary order
  174. me.StopPendingRequest();
  175. // Run the query and display the results
  176. me.ajax_request = $.post( AddAppContext(GetAbsoluteUrlAppRoot()+'pages/ajax.render.php'), theMap,
  177. function(data)
  178. {
  179. $(sSearchAreaId).html(data);
  180. $(sSearchAreaId+' .listResults').tableHover();
  181. $('#fr_'+me.id+' input:radio').click(function() { me.UpdateButtons(); });
  182. me.UpdateButtons();
  183. me.ajax_request = null;
  184. $('#count_'+me.id).change(function(){
  185. me.UpdateButtons();
  186. });
  187. me.UpdateSizes();
  188. },
  189. 'html'
  190. );
  191. return false; // Don't submit the form, stay in the current page !
  192. };
  193. this.DoOk = function()
  194. {
  195. var s = $('#'+me.id+'_results').find(':input[name^=storedSelection]');
  196. var iObjectId = 0;
  197. if (s.length > 0)
  198. {
  199. iObjectId = s.val();
  200. }
  201. else
  202. {
  203. iObjectId = $('#fr_'+me.id+' input[name=selectObject]:checked').val();
  204. }
  205. $('#ac_dlg_'+this.id).dialog('close');
  206. $('#label_'+this.id).addClass('ac_dlg_loading');
  207. // Query the server again to get the display name of the selected object
  208. var theMap = { sTargetClass: me.sTargetClass,
  209. iInputId: me.id,
  210. iObjectId: iObjectId,
  211. sAttCode: me.sAttCode,
  212. bSearchMode: me.bSearchMode,
  213. operation: 'getObjectName'
  214. };
  215. // Make sure that we cancel any pending request before issuing another
  216. // since responses may arrive in arbitrary order
  217. me.StopPendingRequest();
  218. // Run the query and get the result back directly in JSON
  219. me.ajax_request = $.post( AddAppContext(GetAbsoluteUrlAppRoot()+'pages/ajax.render.php'), theMap,
  220. function(data)
  221. {
  222. var oTemp = $('<div id="ac_temp" style="display:none">'+data.name+'</div>');
  223. var txt = oTemp.html(); // this causes HTML entities to be interpreted
  224. $('#label_'+me.id).val(txt);
  225. $('#label_'+me.id).removeClass('ac_dlg_loading');
  226. var prevValue = $('#'+me.id).val();
  227. $('#'+me.id).val(iObjectId);
  228. if (prevValue != iObjectId)
  229. {
  230. $('#'+me.id).trigger('validate');
  231. $('#'+me.id).trigger('extkeychange');
  232. $('#'+me.id).trigger('change');
  233. }
  234. $('#label_'+me.id).focus();
  235. me.ajax_request = null;
  236. },
  237. 'json'
  238. );
  239. return false; // Do NOT submit the form in case we are called by OnSubmit...
  240. };
  241. // Workaround for a ui.jquery limitation: if the content of
  242. // the dialog contains many INPUTs, closing and opening the
  243. // dialog is very slow. So empty it each time.
  244. this.OnClose = function()
  245. {
  246. me.StopPendingRequest();
  247. // called by the dialog, so in the context 'this' points to the jQueryObject
  248. if (me.emptyOnClose)
  249. {
  250. $('#dr_'+me.id).html(me.emptyHtml);
  251. }
  252. $('#label_'+me.id).removeClass('ac_dlg_loading');
  253. $('#label_'+me.id).focus();
  254. me.ajax_request = null;
  255. };
  256. this.CreateObject = function(oWizHelper)
  257. {
  258. if($('#'+me.id).attr('disabled')) return; // Disabled, do nothing
  259. // Query the server to get the form to create a target object
  260. if (me.bSelectMode)
  261. {
  262. me.v_html = $('#v_'+me.id).html();
  263. $('#v_'+me.id).html('<img src="../images/indicator.gif" />');
  264. }
  265. else
  266. {
  267. $('#label_'+me.id).addClass('ac_dlg_loading');
  268. }
  269. me.oWizardHelper.UpdateWizard();
  270. var theMap = { sTargetClass: me.sTargetClass,
  271. iInputId: me.id,
  272. sAttCode: me.sAttCode,
  273. 'json': me.oWizardHelper.ToJSON(),
  274. operation: 'objectCreationForm'
  275. };
  276. // Make sure that we cancel any pending request before issuing another
  277. // since responses may arrive in arbitrary order
  278. me.StopPendingRequest();
  279. // Run the query and get the result back directly in HTML
  280. me.ajax_request = $.post( AddAppContext(GetAbsoluteUrlAppRoot()+'pages/ajax.render.php'), theMap,
  281. function(data)
  282. {
  283. $('#ajax_'+me.id).html(data);
  284. $('#ac_create_'+me.id).dialog('open');
  285. $('#ac_create_'+me.id).dialog( "option", "close", me.OnCloseCreateObject );
  286. // Modify the action of the cancel button
  287. $('#ac_create_'+me.id+' button.cancel').unbind('click').click( me.CloseCreateObject );
  288. me.ajax_request = null;
  289. // Adjust the dialog's size to fit into the screen
  290. if ($('#ac_create_'+me.id).width() > ($(window).width()-40))
  291. {
  292. $('#ac_create_'+me.id).width($(window).width()-40);
  293. }
  294. if ($('#ac_create_'+me.id).height() > ($(window).height()-70))
  295. {
  296. $('#ac_create_'+me.id).height($(window).height()-70);
  297. }
  298. },
  299. 'html'
  300. );
  301. };
  302. this.CloseCreateObject = function()
  303. {
  304. $('#ac_create_'+me.id).dialog( "close" );
  305. };
  306. this.OnCloseCreateObject = function()
  307. {
  308. if (me.bSelectMode)
  309. {
  310. $('#v_'+me.id).html(me.v_html);
  311. }
  312. else
  313. {
  314. $('#label_'+me.id).removeClass('ac_dlg_loading');
  315. }
  316. $('#label_'+me.id).focus();
  317. $('#ac_create_'+me.id).dialog("destroy");
  318. $('#ac_create_'+me.id).remove();
  319. $('#ajax_'+me.id).html('');
  320. };
  321. this.DoCreateObject = function()
  322. {
  323. var sFormId = $('#dcr_'+me.id+' form').attr('id');
  324. if (CheckFields(sFormId, true))
  325. {
  326. $('#'+sFormId).block();
  327. var theMap = { sTargetClass: me.sTargetClass,
  328. iInputId: me.id,
  329. sAttCode: me.sAttCode,
  330. 'json': me.oWizardHelper.ToJSON()
  331. };
  332. // Gather the values from the form
  333. // Gather the parameters from the search form
  334. $('#'+sFormId+' :input').each(
  335. function(i)
  336. {
  337. if (this.name != '')
  338. {
  339. theMap[this.name] = this.value;
  340. }
  341. }
  342. );
  343. // Override the 'operation' code
  344. theMap['operation'] = 'doCreateObject';
  345. theMap['class'] = me.sClass;
  346. $('#ac_create_'+me.id).dialog('close');
  347. // Make sure that we cancel any pending request before issuing another
  348. // since responses may arrive in arbitrary order
  349. me.StopPendingRequest();
  350. // Run the query and get the result back directly in JSON
  351. me.ajax_request = $.post( AddAppContext(GetAbsoluteUrlAppRoot()+'pages/ajax.render.php'), theMap,
  352. function(data)
  353. {
  354. if (me.bSelectMode)
  355. {
  356. // Add the newly created object to the drop-down list and select it
  357. $('<option/>', { value : data.id }).html(data.name).appendTo('#'+me.id);
  358. $('#'+me.id+' option[value="'+data.id+'"]').attr('selected', 'selected');
  359. $('#'+me.id).focus();
  360. }
  361. else
  362. {
  363. // Put the value corresponding to the newly created object in the autocomplete
  364. var oTemp = $('<div id="ac_temp" style="display:none">'+data.name+'</div>');
  365. var txt = oTemp.html(); // this causes HTML entities to be interpreted
  366. $('#label_'+me.id).val(txt);
  367. $('#'+me.id).val(data.id);
  368. $('#label_'+me.id).removeClass('ac_dlg_loading');
  369. $('#label_'+me.id).focus();
  370. }
  371. $('#'+me.id).trigger('validate');
  372. $('#'+me.id).trigger('extkeychange');
  373. $('#'+me.id).trigger('change');
  374. me.ajax_request = null;
  375. },
  376. 'json'
  377. );
  378. }
  379. return false; // do NOT submit the form
  380. };
  381. this.Update = function()
  382. {
  383. if ($('#'+me.id).attr('disabled'))
  384. {
  385. $('#v_'+me.id).html('');
  386. $('#label_'+me.id).attr('disabled', 'disabled');
  387. $('#label_'+me.id).css({'background': 'transparent'});
  388. $('#mini_add_'+me.id).hide();
  389. $('#mini_tree_'+me.id).hide();
  390. $('#mini_search_'+me.id).hide();
  391. }
  392. else
  393. {
  394. $('#label_'+me.id).removeAttr('disabled');
  395. $('#label_'+me.id).css({'background': '#fff url(../images/ac-background.gif) no-repeat right'});
  396. $('#mini_add_'+me.id).show();
  397. $('#mini_tree_'+me.id).show();
  398. $('#mini_search_'+me.id).show();
  399. }
  400. };
  401. this.HKDisplay = function()
  402. {
  403. var theMap = { sTargetClass: me.sTargetClass,
  404. sInputId: me.id,
  405. sFilter: me.sFilter,
  406. bSearchMode: me.bSearchMode,
  407. sAttCode: me.sAttCode,
  408. value: $('#'+me.id).val()
  409. };
  410. if (me.bSelectMode)
  411. {
  412. me.v_html = $('#v_'+me.id).html();
  413. $('#v_'+me.id).html('<img src="../images/indicator.gif" />');
  414. }
  415. else
  416. {
  417. $('#label_'+me.id).addClass('ac_dlg_loading');
  418. }
  419. if (me.oWizardHelper == null)
  420. {
  421. theMap['json'] = '';
  422. }
  423. else
  424. {
  425. // Not inside a "search form", updating a real object
  426. me.oWizardHelper.UpdateWizard();
  427. theMap['json'] = me.oWizardHelper.ToJSON();
  428. }
  429. theMap['sRemoteClass'] = me.sTargetClass;
  430. theMap.operation = 'displayHierarchy';
  431. // Make sure that we cancel any pending request before issuing another
  432. // since responses may arrive in arbitrary order
  433. me.StopPendingRequest();
  434. // Run the query and display the results
  435. me.ajax_request = $.post( AddAppContext(GetAbsoluteUrlAppRoot()+'pages/ajax.render.php'), theMap,
  436. function(data)
  437. {
  438. $('#ac_tree_'+me.id).html(data);
  439. var maxHeight = $(window).height()-110;
  440. $('#tree_'+me.id).css({maxHeight: maxHeight});
  441. },
  442. 'html'
  443. );
  444. };
  445. this.OnHKResize = function(event, ui)
  446. {
  447. var dh = ui.size.height - ui.originalSize.height;
  448. if (dh != 0)
  449. {
  450. var dlg_content = $('#dlg_tree_'+me.id+' .wizContainer');
  451. var h = dlg_content.height();
  452. dlg_content.height(h + dh);
  453. var tree = $('#tree_'+me.id);
  454. var h = tree.height();
  455. tree.height(h + dh - 1);
  456. }
  457. };
  458. this.OnHKClose = function()
  459. {
  460. if (me.bSelectMode)
  461. {
  462. $('#v_'+me.id).html(me.v_html);
  463. }
  464. else
  465. {
  466. $('#label_'+me.id).removeClass('ac_dlg_loading');
  467. }
  468. $('#label_'+me.id).focus();
  469. $('#dlg_tree_'+me.id).dialog("destroy");
  470. $('#dlg_tree_'+me.id).remove();
  471. };
  472. this.DoHKOk = function()
  473. {
  474. iObjectId = $('#tree_'+me.id+' input[name=selectObject]:checked').val();
  475. $('#dlg_tree_'+me.id).dialog('close');
  476. // Query the server again to get the display name of the selected object
  477. var theMap = { sTargetClass: me.sTargetClass,
  478. iInputId: me.id,
  479. iObjectId: iObjectId,
  480. sAttCode: me.sAttCode,
  481. bSearchMode: me.bSearchMode,
  482. operation: 'getObjectName'
  483. };
  484. // Make sure that we cancel any pending request before issuing another
  485. // since responses may arrive in arbitrary order
  486. me.StopPendingRequest();
  487. // Run the query and get the result back directly in JSON
  488. me.ajax_request = $.post( AddAppContext(GetAbsoluteUrlAppRoot()+'pages/ajax.render.php'), theMap,
  489. function(data)
  490. {
  491. var oTemp = $('<div id="ac_temp" style="display:none">'+data.name+'</div>');
  492. var txt = oTemp.html(); // this causes HTML entities to be interpreted
  493. $('#label_'+me.id).val(txt);
  494. $('#label_'+me.id).removeClass('ac_dlg_loading');
  495. var prevValue = $('#'+me.id).val();
  496. $('#'+me.id).val(iObjectId);
  497. if (prevValue != iObjectId)
  498. {
  499. $('#'+me.id).trigger('validate');
  500. $('#'+me.id).trigger('extkeychange');
  501. $('#'+me.id).trigger('change');
  502. }
  503. if ( $('#'+me.id).hasClass('multiselect'))
  504. {
  505. $('#'+me.id+' option').each(function() { this.selected = ($(this).attr('value') == iObjectId); });
  506. $('#'+me.id).multiselect('refresh');
  507. }
  508. $('#label_'+me.id).focus();
  509. me.ajax_request = null;
  510. },
  511. 'json'
  512. );
  513. return false; // Do NOT submit the form in case we are called by OnSubmit...
  514. };
  515. }