extkeywidget.js 16 KB

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