extkeywidget.js 16 KB

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