extkeywidget.js 15 KB

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