extkeywidget.js 9.8 KB

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