extkeywidget.js 15 KB

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