extkeywidget.js 11 KB

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