extkeywidget.js 10 KB

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