autocompletewidget.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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)
  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. var me = this;
  24. this.Init = function()
  25. {
  26. // make sure that the form is clean
  27. $('#linkedset_'+this.id+' .selection').each( function() { this.checked = false; });
  28. $('#'+this.id+'_btnRemove').attr('disabled','disabled');
  29. $('#'+this.id+'_linksToRemove').val('');
  30. }
  31. this.Search = function()
  32. {
  33. $('#ac_dlg_'+me.id).dialog('open');
  34. this.UpdateSizes();
  35. this.UpdateButtons();
  36. }
  37. this.UpdateSizes = function()
  38. {
  39. var dlg = $('#ac_dlg_'+me.id);
  40. var searchForm = dlg.find('div.display_block:first'); // Top search form, enclosing display_block
  41. var results = $('#dr_'+me.id);
  42. padding_right = parseInt(dlg.css('padding-right').replace('px', ''));
  43. padding_left = parseInt(dlg.css('padding-left').replace('px', ''));
  44. padding_top = parseInt(dlg.css('padding-top').replace('px', ''));
  45. padding_bottom = parseInt(dlg.css('padding-bottom').replace('px', ''));
  46. width = dlg.innerWidth() - padding_right - padding_left - 22; // 5 (margin-left) + 5 (padding-left) + 5 (padding-right) + 5 (margin-right) + 2 for rounding !
  47. height = dlg.innerHeight() - padding_top - padding_bottom -22;
  48. form_height = searchForm.outerHeight();
  49. results.height(height - form_height - 40); // Leave some space for the buttons
  50. }
  51. this.UpdateButtons = function()
  52. {
  53. var okBtn = $('#btn_ok_'+me.id);
  54. if ($('#fr_'+me.id+' input[name=selectObject]:checked').length > 0)
  55. {
  56. okBtn.attr('disabled', '');
  57. }
  58. else
  59. {
  60. okBtn.attr('disabled', 'disabled');
  61. }
  62. }
  63. var ajax_request = null;
  64. this.DoSearchObjects = function(id)
  65. {
  66. var theMap = { sAttCode: me.sAttCode,
  67. iInputId: me.id,
  68. sSuffix: me.sSuffix,
  69. }
  70. // Gather the parameters from the search form
  71. $('#fs_'+me.id+' :input').each(
  72. function(i)
  73. {
  74. if (this.name != '')
  75. {
  76. theMap[this.name] = this.value;
  77. }
  78. }
  79. );
  80. oWizardHelper.UpdateWizard();
  81. theMap['json'] = oWizardHelper.ToJSON();
  82. theMap['sRemoteClass'] = theMap['class']; // swap 'class' (defined in the form) and 'remoteClass'
  83. theMap['class'] = me.sClass;
  84. theMap.operation = 'searchObjectsToSelect'; // Override what is defined in the form itself
  85. sSearchAreaId = '#dr_'+me.id;
  86. //$(sSearchAreaId).html('<div style="text-align:center;width:100%;height:24px;vertical-align:middle;"><img src="../images/indicator.gif" /></div>');
  87. $(sSearchAreaId).block();
  88. me.UpdateButtons();
  89. // Make sure that we cancel any pending request before issuing another
  90. // since responses may arrive in arbitrary order
  91. if (ajax_request != null)
  92. {
  93. ajax_request.abort();
  94. ajax_request = null;
  95. }
  96. // Run the query and display the results
  97. ajax_request = $.post( 'ajax.render.php', theMap,
  98. function(data)
  99. {
  100. $(sSearchAreaId).html(data);
  101. $(sSearchAreaId+' .listResults').tableHover();
  102. $(sSearchAreaId+' .listResults').tablesorter( { headers: {0: {sorter: false}}, widgets: ['myZebra', 'truncatedList']} ); // sortable and zebra tables
  103. $('#fr_'+me.id+' input:radio').click(function() { me.UpdateButtons(); });
  104. me.UpdateButtons();
  105. ajax_request = null;
  106. },
  107. 'html'
  108. );
  109. return false; // Don't submit the form, stay in the current page !
  110. }
  111. this.DoOk = function()
  112. {
  113. var iObjectId = $('#fr_'+me.id+' input[name=selectObject]:checked').val();
  114. $('#ac_dlg_'+this.id).dialog('close');
  115. $('#label_'+this.id).addClass('ac_loading');
  116. // Query the server again to get the display name of the selected object
  117. var theMap = { sAttCode: me.sAttCode,
  118. iInputId: me.id,
  119. iObjectId: iObjectId,
  120. sSuffix: me.sSuffix,
  121. 'class': me.sClass,
  122. operation: 'getObjectName'
  123. }
  124. // Make sure that we cancel any pending request before issuing another
  125. // since responses may arrive in arbitrary order
  126. if (ajax_request != null)
  127. {
  128. ajax_request.abort();
  129. ajax_request = null;
  130. }
  131. // Run the query and get the result back directly in JSON
  132. 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. ajax_request = null;
  140. },
  141. 'json'
  142. );
  143. return false; // Do NOT submit the form in case we are called by OnSubmit...
  144. }
  145. // Workaround for a ui.jquery limitation: if the content of
  146. // the dialog contains many INPUTs, closing and opening the
  147. // dialog is very slow. So empty it each time.
  148. this.OnClose = function()
  149. {
  150. // called by the dialog, so in the context 'this' points to the jQueryObject
  151. if (me.emptyOnClose)
  152. {
  153. $('#dr_'+me.id).html(me.emptyHtml);
  154. }
  155. $('#label_'+me.id).focus();
  156. }
  157. }