extkeywidget.js 15 KB

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