extkeywidget.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  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. me.DoSearchObjects();
  91. },
  92. 'html'
  93. );
  94. };
  95. this.UpdateSizes = function()
  96. {
  97. var dlg = $('#ac_dlg_'+me.id);
  98. // Adjust the dialog's size to fit into the screen
  99. if (dlg.width() > ($(window).width()-40))
  100. {
  101. dlg.width($(window).width()-40);
  102. }
  103. if (dlg.height() > ($(window).height()-70))
  104. {
  105. dlg.height($(window).height()-70);
  106. }
  107. var searchForm = dlg.find('div.display_block:first'); // Top search form, enclosing display_block
  108. var results = $('#dr_'+me.id);
  109. var oPadding = {};
  110. for(k in ['top', 'right', 'bottom', 'left'])
  111. {
  112. oPadding[k] = 0;
  113. if (dlg.css('padding-'+k))
  114. {
  115. oPadding[k] = parseInt(dlg.css('padding-'+k).replace('px', ''));
  116. }
  117. }
  118. width = dlg.innerWidth() - oPadding.right - oPadding.left - 22; // 5 (margin-left) + 5 (padding-left) + 5 (padding-right) + 5 (margin-right) + 2 for rounding !
  119. height = dlg.innerHeight() - oPadding.top - oPadding.bottom -22;
  120. form_height = searchForm.outerHeight();
  121. results.height(height - form_height - 40); // Leave some space for the buttons
  122. };
  123. this.UpdateButtons = function()
  124. {
  125. var okBtn = $('#btn_ok_'+me.id);
  126. if ($('#count_'+me.id).val() > 0)
  127. {
  128. okBtn.removeAttr('disabled');
  129. }
  130. else
  131. {
  132. okBtn.attr('disabled', 'disabled');
  133. }
  134. };
  135. this.DoSearchObjects = function(id)
  136. {
  137. var theMap = { sTargetClass: me.sTargetClass,
  138. iInputId: me.id,
  139. sFilter: me.sFilter,
  140. bSearchMode: me.bSearchMode
  141. };
  142. // Gather the parameters from the search form
  143. $('#fs_'+me.id+' :input').each( function() {
  144. if (this.name != '')
  145. {
  146. var val = $(this).val(); // supports multiselect as well
  147. if (val !== null)
  148. {
  149. theMap[this.name] = val;
  150. }
  151. }
  152. });
  153. if (me.oWizardHelper == null)
  154. {
  155. theMap['json'] = '';
  156. }
  157. else
  158. {
  159. // Not inside a "search form", updating a real object
  160. me.oWizardHelper.UpdateWizard();
  161. theMap['json'] = me.oWizardHelper.ToJSON();
  162. }
  163. theMap['sRemoteClass'] = theMap['class']; // swap 'class' (defined in the form) and 'remoteClass'
  164. theMap.operation = 'searchObjectsToSelect'; // Override what is defined in the form itself
  165. theMap.sAttCode = me.sAttCode,
  166. sSearchAreaId = '#dr_'+me.id;
  167. //$(sSearchAreaId).html('<div style="text-align:center;width:100%;height:24px;vertical-align:middle;"><img src="../images/indicator.gif" /></div>');
  168. $(sSearchAreaId).block();
  169. me.UpdateButtons();
  170. // Make sure that we cancel any pending request before issuing another
  171. // since responses may arrive in arbitrary order
  172. me.StopPendingRequest();
  173. // Run the query and display the results
  174. me.ajax_request = $.post( AddAppContext(GetAbsoluteUrlAppRoot()+'pages/ajax.render.php'), theMap,
  175. function(data)
  176. {
  177. $(sSearchAreaId).html(data);
  178. $(sSearchAreaId+' .listResults').tableHover();
  179. $('#fr_'+me.id+' input:radio').click(function() { me.UpdateButtons(); });
  180. me.UpdateButtons();
  181. me.ajax_request = null;
  182. $('#count_'+me.id).change(function(){
  183. me.UpdateButtons();
  184. });
  185. },
  186. 'html'
  187. );
  188. return false; // Don't submit the form, stay in the current page !
  189. };
  190. this.DoOk = function()
  191. {
  192. var s = $('#'+me.id+'_results').find(':input[name^=storedSelection]');
  193. var iObjectId = 0;
  194. if (s.length > 0)
  195. {
  196. iObjectId = s.val();
  197. }
  198. else
  199. {
  200. iObjectId = $('#fr_'+me.id+' input[name=selectObject]:checked').val();
  201. }
  202. $('#ac_dlg_'+this.id).dialog('close');
  203. $('#label_'+this.id).addClass('ac_dlg_loading');
  204. // Query the server again to get the display name of the selected object
  205. var theMap = { sTargetClass: me.sTargetClass,
  206. iInputId: me.id,
  207. iObjectId: iObjectId,
  208. sAttCode: me.sAttCode,
  209. bSearchMode: me.bSearchMode,
  210. operation: 'getObjectName'
  211. };
  212. // Make sure that we cancel any pending request before issuing another
  213. // since responses may arrive in arbitrary order
  214. me.StopPendingRequest();
  215. // Run the query and get the result back directly in JSON
  216. me.ajax_request = $.post( AddAppContext(GetAbsoluteUrlAppRoot()+'pages/ajax.render.php'), theMap,
  217. function(data)
  218. {
  219. var oTemp = $('<div id="ac_temp" style="display:none">'+data.name+'</div>');
  220. var txt = oTemp.html(); // this causes HTML entities to be interpreted
  221. $('#label_'+me.id).val(txt);
  222. $('#label_'+me.id).removeClass('ac_dlg_loading');
  223. var prevValue = $('#'+me.id).val();
  224. $('#'+me.id).val(iObjectId);
  225. if (prevValue != iObjectId)
  226. {
  227. $('#'+me.id).trigger('validate');
  228. $('#'+me.id).trigger('extkeychange');
  229. $('#'+me.id).trigger('change');
  230. }
  231. $('#label_'+me.id).focus();
  232. me.ajax_request = null;
  233. },
  234. 'json'
  235. );
  236. return false; // Do NOT submit the form in case we are called by OnSubmit...
  237. };
  238. // Workaround for a ui.jquery limitation: if the content of
  239. // the dialog contains many INPUTs, closing and opening the
  240. // dialog is very slow. So empty it each time.
  241. this.OnClose = function()
  242. {
  243. me.StopPendingRequest();
  244. // called by the dialog, so in the context 'this' points to the jQueryObject
  245. if (me.emptyOnClose)
  246. {
  247. $('#dr_'+me.id).html(me.emptyHtml);
  248. }
  249. $('#label_'+me.id).removeClass('ac_dlg_loading');
  250. $('#label_'+me.id).focus();
  251. me.ajax_request = null;
  252. };
  253. this.CreateObject = function(oWizHelper)
  254. {
  255. if($('#'+me.id).attr('disabled')) return; // Disabled, do nothing
  256. // Query the server to get the form to create a target object
  257. if (me.bSelectMode)
  258. {
  259. me.v_html = $('#v_'+me.id).html();
  260. $('#v_'+me.id).html('<img src="../images/indicator.gif" />');
  261. }
  262. else
  263. {
  264. $('#label_'+me.id).addClass('ac_dlg_loading');
  265. }
  266. me.oWizardHelper.UpdateWizard();
  267. var theMap = { sTargetClass: me.sTargetClass,
  268. iInputId: me.id,
  269. sAttCode: me.sAttCode,
  270. 'json': me.oWizardHelper.ToJSON(),
  271. operation: 'objectCreationForm'
  272. };
  273. // Make sure that we cancel any pending request before issuing another
  274. // since responses may arrive in arbitrary order
  275. me.StopPendingRequest();
  276. // Run the query and get the result back directly in HTML
  277. me.ajax_request = $.post( AddAppContext(GetAbsoluteUrlAppRoot()+'pages/ajax.render.php'), theMap,
  278. function(data)
  279. {
  280. $('#ajax_'+me.id).html(data);
  281. $('#ac_create_'+me.id).dialog('open');
  282. $('#ac_create_'+me.id).dialog( "option", "close", me.OnCloseCreateObject );
  283. // Modify the action of the cancel button
  284. $('#ac_create_'+me.id+' button.cancel').unbind('click').click( me.CloseCreateObject );
  285. me.ajax_request = null;
  286. // Adjust the dialog's size to fit into the screen
  287. if ($('#ac_create_'+me.id).width() > ($(window).width()-40))
  288. {
  289. $('#ac_create_'+me.id).width($(window).width()-40);
  290. }
  291. if ($('#ac_create_'+me.id).height() > ($(window).height()-70))
  292. {
  293. $('#ac_create_'+me.id).height($(window).height()-70);
  294. }
  295. },
  296. 'html'
  297. );
  298. };
  299. this.CloseCreateObject = function()
  300. {
  301. $('#ac_create_'+me.id).dialog( "close" );
  302. };
  303. this.OnCloseCreateObject = function()
  304. {
  305. if (me.bSelectMode)
  306. {
  307. $('#v_'+me.id).html(me.v_html);
  308. }
  309. else
  310. {
  311. $('#label_'+me.id).removeClass('ac_dlg_loading');
  312. }
  313. $('#label_'+me.id).focus();
  314. $('#ac_create_'+me.id).dialog("destroy");
  315. $('#ac_create_'+me.id).remove();
  316. $('#ajax_'+me.id).html('');
  317. };
  318. this.DoCreateObject = function()
  319. {
  320. var sFormId = $('#dcr_'+me.id+' form').attr('id');
  321. if (CheckFields(sFormId, true))
  322. {
  323. $('#'+sFormId).block();
  324. var theMap = { sTargetClass: me.sTargetClass,
  325. iInputId: me.id,
  326. sAttCode: me.sAttCode,
  327. 'json': me.oWizardHelper.ToJSON()
  328. };
  329. // Gather the values from the form
  330. // Gather the parameters from the search form
  331. $('#'+sFormId+' :input').each(
  332. function(i)
  333. {
  334. if (this.name != '')
  335. {
  336. theMap[this.name] = this.value;
  337. }
  338. }
  339. );
  340. // Override the 'operation' code
  341. theMap['operation'] = 'doCreateObject';
  342. theMap['class'] = me.sClass;
  343. $('#ac_create_'+me.id).dialog('close');
  344. // Make sure that we cancel any pending request before issuing another
  345. // since responses may arrive in arbitrary order
  346. me.StopPendingRequest();
  347. // Run the query and get the result back directly in JSON
  348. me.ajax_request = $.post( AddAppContext(GetAbsoluteUrlAppRoot()+'pages/ajax.render.php'), theMap,
  349. function(data)
  350. {
  351. if (me.bSelectMode)
  352. {
  353. // Add the newly created object to the drop-down list and select it
  354. $('<option/>', { value : data.id }).html(data.name).appendTo('#'+me.id);
  355. $('#'+me.id+' option[value="'+data.id+'"]').attr('selected', 'selected');
  356. $('#'+me.id).focus();
  357. }
  358. else
  359. {
  360. // Put the value corresponding to the newly created object in the autocomplete
  361. var oTemp = $('<div id="ac_temp" style="display:none">'+data.name+'</div>');
  362. var txt = oTemp.html(); // this causes HTML entities to be interpreted
  363. $('#label_'+me.id).val(txt);
  364. $('#'+me.id).val(data.id);
  365. $('#label_'+me.id).removeClass('ac_dlg_loading');
  366. $('#label_'+me.id).focus();
  367. }
  368. $('#'+me.id).trigger('validate');
  369. $('#'+me.id).trigger('extkeychange');
  370. $('#'+me.id).trigger('change');
  371. me.ajax_request = null;
  372. },
  373. 'json'
  374. );
  375. }
  376. return false; // do NOT submit the form
  377. };
  378. this.Update = function()
  379. {
  380. if ($('#'+me.id).attr('disabled'))
  381. {
  382. $('#v_'+me.id).html('');
  383. $('#label_'+me.id).attr('disabled', 'disabled');
  384. $('#label_'+me.id).css({'background': 'transparent'});
  385. $('#mini_add_'+me.id).hide();
  386. $('#mini_tree_'+me.id).hide();
  387. $('#mini_search_'+me.id).hide();
  388. }
  389. else
  390. {
  391. $('#label_'+me.id).removeAttr('disabled');
  392. $('#label_'+me.id).css({'background': '#fff url(../images/ac-background.gif) no-repeat right'});
  393. $('#mini_add_'+me.id).show();
  394. $('#mini_tree_'+me.id).show();
  395. $('#mini_search_'+me.id).show();
  396. }
  397. };
  398. this.HKDisplay = function()
  399. {
  400. var theMap = { sTargetClass: me.sTargetClass,
  401. sInputId: me.id,
  402. sFilter: me.sFilter,
  403. bSearchMode: me.bSearchMode,
  404. sAttCode: me.sAttCode,
  405. value: $('#'+me.id).val()
  406. };
  407. if (me.bSelectMode)
  408. {
  409. me.v_html = $('#v_'+me.id).html();
  410. $('#v_'+me.id).html('<img src="../images/indicator.gif" />');
  411. }
  412. else
  413. {
  414. $('#label_'+me.id).addClass('ac_dlg_loading');
  415. }
  416. if (me.oWizardHelper == null)
  417. {
  418. theMap['json'] = '';
  419. }
  420. else
  421. {
  422. // Not inside a "search form", updating a real object
  423. me.oWizardHelper.UpdateWizard();
  424. theMap['json'] = me.oWizardHelper.ToJSON();
  425. }
  426. theMap['sRemoteClass'] = me.sTargetClass;
  427. theMap.operation = 'displayHierarchy';
  428. // Make sure that we cancel any pending request before issuing another
  429. // since responses may arrive in arbitrary order
  430. me.StopPendingRequest();
  431. // Run the query and display the results
  432. me.ajax_request = $.post( AddAppContext(GetAbsoluteUrlAppRoot()+'pages/ajax.render.php'), theMap,
  433. function(data)
  434. {
  435. $('#ac_tree_'+me.id).html(data);
  436. var maxHeight = $(window).height()-110;
  437. $('#tree_'+me.id).css({maxHeight: maxHeight});
  438. },
  439. 'html'
  440. );
  441. };
  442. this.OnHKResize = function(event, ui)
  443. {
  444. var dh = ui.size.height - ui.originalSize.height;
  445. if (dh != 0)
  446. {
  447. var dlg_content = $('#dlg_tree_'+me.id+' .wizContainer');
  448. var h = dlg_content.height();
  449. dlg_content.height(h + dh);
  450. var tree = $('#tree_'+me.id);
  451. var h = tree.height();
  452. tree.height(h + dh - 1);
  453. }
  454. };
  455. this.OnHKClose = function()
  456. {
  457. if (me.bSelectMode)
  458. {
  459. $('#v_'+me.id).html(me.v_html);
  460. }
  461. else
  462. {
  463. $('#label_'+me.id).removeClass('ac_dlg_loading');
  464. }
  465. $('#label_'+me.id).focus();
  466. $('#dlg_tree_'+me.id).dialog("destroy");
  467. $('#dlg_tree_'+me.id).remove();
  468. };
  469. this.DoHKOk = function()
  470. {
  471. iObjectId = $('#tree_'+me.id+' input[name=selectObject]:checked').val();
  472. $('#dlg_tree_'+me.id).dialog('close');
  473. // Query the server again to get the display name of the selected object
  474. var theMap = { sTargetClass: me.sTargetClass,
  475. iInputId: me.id,
  476. iObjectId: iObjectId,
  477. sAttCode: me.sAttCode,
  478. bSearchMode: me.bSearchMode,
  479. operation: 'getObjectName'
  480. };
  481. // Make sure that we cancel any pending request before issuing another
  482. // since responses may arrive in arbitrary order
  483. me.StopPendingRequest();
  484. // Run the query and get the result back directly in JSON
  485. me.ajax_request = $.post( AddAppContext(GetAbsoluteUrlAppRoot()+'pages/ajax.render.php'), theMap,
  486. function(data)
  487. {
  488. var oTemp = $('<div id="ac_temp" style="display:none">'+data.name+'</div>');
  489. var txt = oTemp.html(); // this causes HTML entities to be interpreted
  490. $('#label_'+me.id).val(txt);
  491. $('#label_'+me.id).removeClass('ac_dlg_loading');
  492. var prevValue = $('#'+me.id).val();
  493. $('#'+me.id).val(iObjectId);
  494. if (prevValue != iObjectId)
  495. {
  496. $('#'+me.id).trigger('validate');
  497. $('#'+me.id).trigger('extkeychange');
  498. $('#'+me.id).trigger('change');
  499. }
  500. $('#label_'+me.id).focus();
  501. me.ajax_request = null;
  502. },
  503. 'json'
  504. );
  505. return false; // Do NOT submit the form in case we are called by OnSubmit...
  506. };
  507. }