autocompletewidget.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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 AutocompleteWidget(id, sClass, sAttCode, sSuffix, oWizHelper)
  16. {
  17. this.id = id;
  18. this.sClass = sClass;
  19. this.sAttCode = sAttCode;
  20. this.sSuffix = sSuffix;
  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. var me = this;
  26. this.Init = function()
  27. {
  28. // make sure that the form is clean
  29. $('#'+this.id+'_btnRemove').attr('disabled','disabled');
  30. $('#'+this.id+'_linksToRemove').val('');
  31. }
  32. this.StopPendingRequest = function()
  33. {
  34. if (me.ajax_request)
  35. {
  36. me.ajax_request.Abort();
  37. me.ajax_request = null;
  38. }
  39. }
  40. this.Search = function()
  41. {
  42. $('#ac_dlg_'+me.id).dialog('open');
  43. this.UpdateSizes();
  44. this.UpdateButtons();
  45. }
  46. this.UpdateSizes = function()
  47. {
  48. var dlg = $('#ac_dlg_'+me.id);
  49. var searchForm = dlg.find('div.display_block:first'); // Top search form, enclosing display_block
  50. var results = $('#dr_'+me.id);
  51. padding_right = parseInt(dlg.css('padding-right').replace('px', ''));
  52. padding_left = parseInt(dlg.css('padding-left').replace('px', ''));
  53. padding_top = parseInt(dlg.css('padding-top').replace('px', ''));
  54. padding_bottom = parseInt(dlg.css('padding-bottom').replace('px', ''));
  55. width = dlg.innerWidth() - padding_right - padding_left - 22; // 5 (margin-left) + 5 (padding-left) + 5 (padding-right) + 5 (margin-right) + 2 for rounding !
  56. height = dlg.innerHeight() - padding_top - padding_bottom -22;
  57. form_height = searchForm.outerHeight();
  58. results.height(height - form_height - 40); // Leave some space for the buttons
  59. }
  60. this.UpdateButtons = function()
  61. {
  62. var okBtn = $('#btn_ok_'+me.id);
  63. if ($('#fr_'+me.id+' input[name=selectObject]:checked').length > 0)
  64. {
  65. okBtn.attr('disabled', '');
  66. }
  67. else
  68. {
  69. okBtn.attr('disabled', 'disabled');
  70. }
  71. }
  72. this.DoSearchObjects = function(id)
  73. {
  74. var theMap = { sAttCode: me.sAttCode,
  75. iInputId: me.id,
  76. sSuffix: me.sSuffix,
  77. }
  78. // Gather the parameters from the search form
  79. $('#fs_'+me.id+' :input').each(
  80. function(i)
  81. {
  82. if (this.name != '')
  83. {
  84. theMap[this.name] = this.value;
  85. }
  86. }
  87. );
  88. me.oWizardHelper.UpdateWizard();
  89. theMap['json'] = me.oWizardHelper.ToJSON();
  90. theMap['sRemoteClass'] = theMap['class']; // swap 'class' (defined in the form) and 'remoteClass'
  91. theMap['class'] = me.sClass;
  92. theMap.operation = 'searchObjectsToSelect'; // Override what is defined in the form itself
  93. sSearchAreaId = '#dr_'+me.id;
  94. //$(sSearchAreaId).html('<div style="text-align:center;width:100%;height:24px;vertical-align:middle;"><img src="../images/indicator.gif" /></div>');
  95. $(sSearchAreaId).block();
  96. me.UpdateButtons();
  97. // Make sure that we cancel any pending request before issuing another
  98. // since responses may arrive in arbitrary order
  99. me.StopPendingRequest();
  100. // Run the query and display the results
  101. me.ajax_request = $.post( 'ajax.render.php', theMap,
  102. function(data)
  103. {
  104. $(sSearchAreaId).html(data);
  105. $(sSearchAreaId+' .listResults').tableHover();
  106. $(sSearchAreaId+' .listResults').tablesorter( { headers: {0: {sorter: false}}, widgets: ['myZebra', 'truncatedList']} ); // sortable and zebra tables
  107. $('#fr_'+me.id+' input:radio').click(function() { me.UpdateButtons(); });
  108. me.UpdateButtons();
  109. me.ajax_request = null;
  110. },
  111. 'html'
  112. );
  113. return false; // Don't submit the form, stay in the current page !
  114. }
  115. this.DoOk = function()
  116. {
  117. var iObjectId = $('#fr_'+me.id+' input[name=selectObject]:checked').val();
  118. $('#ac_dlg_'+this.id).dialog('close');
  119. $('#label_'+this.id).addClass('ac_loading');
  120. // Query the server again to get the display name of the selected object
  121. var theMap = { sAttCode: me.sAttCode,
  122. iInputId: me.id,
  123. iObjectId: iObjectId,
  124. sSuffix: me.sSuffix,
  125. 'class': me.sClass,
  126. operation: 'getObjectName'
  127. }
  128. // Make sure that we cancel any pending request before issuing another
  129. // since responses may arrive in arbitrary order
  130. me.StopPendingRequest();
  131. // Run the query and get the result back directly in JSON
  132. me.ajax_request = $.post( 'ajax.render.php', theMap,
  133. function(data)
  134. {
  135. $('#label_'+me.id).val(data.name);
  136. $('#label_'+me.id).removeClass('ac_loading');
  137. $('#'+me.id).val(iObjectId);
  138. $('#'+me.id).trigger('validate');
  139. $('#label_'+me.id).focus();
  140. me.ajax_request = null;
  141. },
  142. 'json'
  143. );
  144. return false; // Do NOT submit the form in case we are called by OnSubmit...
  145. }
  146. // Workaround for a ui.jquery limitation: if the content of
  147. // the dialog contains many INPUTs, closing and opening the
  148. // dialog is very slow. So empty it each time.
  149. this.OnClose = function()
  150. {
  151. // called by the dialog, so in the context 'this' points to the jQueryObject
  152. if (me.emptyOnClose)
  153. {
  154. $('#dr_'+me.id).html(me.emptyHtml);
  155. }
  156. $('#label_'+me.id).focus();
  157. }
  158. this.CreateObject = function(oWizHelper)
  159. {
  160. // Query the server to get the form to create a target object
  161. $('#label_'+me.id).addClass('ac_loading');
  162. me.oWizardHelper.UpdateWizard();
  163. var theMap = { sAttCode: me.sAttCode,
  164. iInputId: me.id,
  165. sSuffix: me.sSuffix,
  166. 'class': me.sClass,
  167. 'json': me.oWizardHelper.ToJSON(),
  168. operation: 'objectCreationForm'
  169. }
  170. // Make sure that we cancel any pending request before issuing another
  171. // since responses may arrive in arbitrary order
  172. me.StopPendingRequest();
  173. // Run the query and get the result back directly in JSON
  174. me.ajax_request = $.post( 'ajax.render.php', theMap,
  175. function(data)
  176. {
  177. $('#dcr_'+me.id).html(data);
  178. // Adjust the height of the dialog
  179. $('#ac_create_'+me.id).dialog('open');
  180. var h = $('#ac_create_'+me.id+' .wizContainer').outerHeight();
  181. $('#ac_create_'+me.id).dialog( "option", "height", h+55 ); // space for dialog title and padding...
  182. $('#ac_create_'+me.id).dialog( "option", "close", function() { $('#label_'+me.id).removeClass('ac_loading'); $('#label_'+me.id).focus(); } );
  183. // Modify the action of the cancel button
  184. $('#ac_create_'+me.id+' button.cancel').unbind('click').click( me.CloseCreateObject );
  185. me.ajax_request = null;
  186. },
  187. 'html'
  188. );
  189. }
  190. this.CloseCreateObject = function()
  191. {
  192. $('#ac_create_'+me.id).dialog( "close" );
  193. $('#dcr_'+me.id).html('');
  194. }
  195. this.DoCreateObject = function()
  196. {
  197. var sFormId = $('#dcr_'+me.id+' form').attr('id');
  198. if (CheckFields(sFormId, true))
  199. {
  200. $('#'+sFormId).block();
  201. var theMap = { sAttCode: me.sAttCode,
  202. iInputId: me.id,
  203. sSuffix: me.sSuffix,
  204. 'class': me.sClass,
  205. 'json': me.oWizardHelper.ToJSON()
  206. }
  207. // Gather the values from the form
  208. // Gather the parameters from the search form
  209. $('#'+sFormId+' :input').each(
  210. function(i)
  211. {
  212. if (this.name != '')
  213. {
  214. theMap[this.name] = this.value;
  215. }
  216. }
  217. );
  218. // Override the 'operation' code
  219. theMap['operation'] = 'doCreateObject';
  220. theMap['class'] = me.sClass;
  221. $('#ac_create_'+me.id).dialog('close');
  222. // Make sure that we cancel any pending request before issuing another
  223. // since responses may arrive in arbitrary order
  224. me.StopPendingRequest();
  225. // Run the query and get the result back directly in JSON
  226. me.ajax_request = $.post( 'ajax.render.php', theMap,
  227. function(data)
  228. {
  229. $('#label_'+me.id).val(data.name);
  230. $('#'+me.id).val(data.id);
  231. $('#'+me.id).trigger('validate');
  232. $('#label_'+me.id).removeClass('ac_loading');
  233. $('#label_'+me.id).focus();
  234. me.ajax_request = null;
  235. },
  236. 'json'
  237. );
  238. }
  239. return false; // do NOT submit the form
  240. }
  241. }