autocompletewidget.js 8.8 KB

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