linkswidget.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. // JavaScript Document
  2. function LinksWidget(id, sClass, sAttCode, iInputId, sSuffix, bDuplicates, oWizHelper)
  3. {
  4. this.id = id;
  5. this.iInputId = iInputId;
  6. this.sClass = sClass;
  7. this.sAttCode = sAttCode;
  8. this.sSuffix = sSuffix;
  9. this.bDuplicates = bDuplicates;
  10. this.oWizardHelper = oWizHelper;
  11. var me = this;
  12. this.Init = function()
  13. {
  14. // make sure that the form is clean
  15. $('#linkedset_'+this.id+' .selection').each( function() { this.checked = false; });
  16. $('#'+this.id+'_btnRemove').attr('disabled','disabled');
  17. $('#'+this.id+'_linksToRemove').val('');
  18. }
  19. this.RemoveSelected = function()
  20. {
  21. var my_id = '#'+me.id;
  22. $('#linkedset_'+me.id+' .selection:checked').each(
  23. function()
  24. {
  25. $linksToRemove = $(my_id+'_linksToRemove');
  26. prevValue = $linksToRemove.val();
  27. if (prevValue != '')
  28. {
  29. $linksToRemove.val(prevValue + ',' + this.value);
  30. }
  31. else
  32. {
  33. $linksToRemove.val(this.value);
  34. }
  35. $(my_id+'_row_'+this.value).remove();
  36. }
  37. );
  38. // Disable the button since all the selected items have been removed
  39. $(my_id+'_btnRemove').attr('disabled','disabled');
  40. // Re-run the zebra plugin to properly highlight the remaining lines & and take into account the removed ones
  41. $('#linkedset_'+this.id+' .listResults').trigger('update').trigger("applyWidgets");
  42. if ($('$linkedset_'+this.id+' .selection').length == 0)
  43. {
  44. // All items were removed: add a dummy hidden input to make sure that the linkset will be updated (emptied) when posted
  45. $('#'+me.id+'_empty_row').show();
  46. }
  47. }
  48. this.OnSelectChange = function()
  49. {
  50. var nbChecked = $('#linkedset_'+me.id+' .selection:checked').length;
  51. if (nbChecked > 0)
  52. {
  53. $('#'+me.id+'_btnRemove').attr('disabled','');
  54. }
  55. else
  56. {
  57. $('#'+me.id+'_btnRemove').attr('disabled','disabled');
  58. }
  59. }
  60. this.AddObjects = function()
  61. {
  62. $('#dlg_'+me.id).dialog('open');
  63. this.UpdateSizes(null, null);
  64. this.SearchObjectsToAdd();
  65. }
  66. this.SearchObjectsToAdd = function()
  67. {
  68. var theMap = { sAttCode: me.sAttCode,
  69. iInputId: me.iInputId,
  70. sSuffix: me.sSuffix,
  71. bDuplicates: me.bDuplicates
  72. }
  73. me.UpdateButtons(0);
  74. // Gather the parameters from the search form
  75. $('#SearchFormToAdd_'+me.id+' :input').each(
  76. function(i)
  77. {
  78. if (this.name != '')
  79. {
  80. theMap[this.name] = this.value;
  81. }
  82. }
  83. );
  84. // Gather the already linked target objects
  85. theMap.aAlreadyLinked = new Array();
  86. $('#linkedset_'+me.id+' .selection:input').each(
  87. function(i)
  88. {
  89. theMap.aAlreadyLinked.push(this.value);
  90. }
  91. );
  92. theMap['sRemoteClass'] = theMap['class']; // swap 'class' (defined in the form) and 'remoteClass'
  93. theMap['class'] = me.sClass;
  94. theMap.operation = 'searchObjectsToAdd'; // Override what is defined in the form itself
  95. sSearchAreaId = '#SearchResultsToAdd_'+me.id;
  96. $(sSearchAreaId).block();
  97. // Run the query and display the results
  98. $.post( GetAbsoluteUrlAppRoot()+'pages/ajax.render.php', theMap,
  99. function(data)
  100. {
  101. $(sSearchAreaId).html(data);
  102. $(sSearchAreaId+' .listResults').tableHover();
  103. $('#count_'+me.id).change(function(){
  104. var c = this.value;
  105. me.UpdateButtons(c);
  106. });
  107. $(sSearchAreaId).unblock();
  108. },
  109. 'html'
  110. );
  111. return false; // Don't submit the form, stay in the current page !
  112. }
  113. this.UpdateButtons = function(iCount)
  114. {
  115. var okBtn = $('#btn_ok_'+me.id);
  116. if (iCount > 0)
  117. {
  118. okBtn.attr('disabled', '');
  119. }
  120. else
  121. {
  122. okBtn.attr('disabled', 'disabled');
  123. }
  124. }
  125. this.DoAddObjects = function()
  126. {
  127. var theMap = { sAttCode: me.sAttCode,
  128. iInputId: me.iInputId,
  129. sSuffix: me.sSuffix,
  130. bDuplicates: me.bDuplicates,
  131. 'class': me.sClass
  132. }
  133. // Gather the parameters from the search form
  134. var context = $('#SearchResultsToAdd_'+me.id);
  135. var selectionMode = $(':input[name=selectionMode]', context);
  136. if (selectionMode.length > 0)
  137. {
  138. // Paginated table retrieve the mode and the exceptions
  139. var sMode = selectionMode.val();
  140. theMap['selectionMode'] = sMode;
  141. $('#fs_SearchFormToAdd_'+me.id+' :input').each(
  142. function(i)
  143. {
  144. theMap[this.name] = this.value;
  145. }
  146. );
  147. theMap['sRemoteClass'] = theMap['class']; // swap 'class' (defined in the form) and 'remoteClass'
  148. theMap['class'] = me.sClass;
  149. $(' :input[name^=storedSelection]', context).each(function() {
  150. if (theMap[this.name] == undefined)
  151. {
  152. theMap[this.name] = new Array();
  153. }
  154. theMap[this.name].push(this.value);
  155. $(this).remove(); // Remove the selection for the next time the dialog re-opens
  156. });
  157. // Retrieve the 'filter' definition
  158. var table = $('#ResultsToAdd_'+me.id).find('table.listResults')[0];
  159. theMap['filter'] = table.config.filter;
  160. theMap['extra_params'] = table.config.extra_params;
  161. }
  162. // else
  163. // {
  164. // Normal table, retrieve all the checked check-boxes
  165. $(':checked[name^=selectObject]', context).each(
  166. function(i)
  167. {
  168. if ( (this.name != '') && ((this.type != 'checkbox') || (this.checked)) )
  169. {
  170. //console.log(this.type);
  171. arrayExpr = /\[\]$/;
  172. if (arrayExpr.test(this.name))
  173. {
  174. // Array
  175. if (theMap[this.name] == undefined)
  176. {
  177. theMap[this.name] = new Array();
  178. }
  179. theMap[this.name].push(this.value);
  180. }
  181. else
  182. {
  183. theMap[this.name] = this.value;
  184. }
  185. }
  186. $(this).parents('tr:first').remove(); // Remove the whole line, so that, next time the dialog gets displayed it's no longer there
  187. }
  188. );
  189. // }
  190. theMap['operation'] = 'doAddObjects';
  191. if (me.oWizardHelper == null)
  192. {
  193. theMap['json'] = '';
  194. }
  195. else
  196. {
  197. // Not inside a "search form", updating a real object
  198. me.oWizardHelper.UpdateWizard();
  199. theMap['json'] = me.oWizardHelper.ToJSON();
  200. }
  201. $('#busy_'+me.iInputId).html('&nbsp;<img src="../images/indicator.gif"/>');
  202. // Run the query and display the results
  203. $.post( GetAbsoluteUrlAppRoot()+'pages/ajax.render.php', theMap,
  204. function(data)
  205. {
  206. //console.log('Data: ' + data);
  207. if (data != '')
  208. {
  209. $('#'+me.id+'_empty_row').hide();
  210. $('#linkedset_'+me.id+' .listResults tbody').append(data);
  211. $('#linkedset_'+me.id+' .listResults').trigger('update');
  212. $('#linkedset_'+me.id+' .listResults').tableHover();
  213. $('#linkedset_'+me.id+' .listResults').trigger('update').trigger("applyWidgets"); // table is already sortable, just refresh it
  214. $('#linkedset_'+me.id+' :input').each( function() { $(this).trigger('validate', ''); }); // Validate newly added form fields...
  215. $('#busy_'+me.iInputId).html('');
  216. }
  217. },
  218. 'html'
  219. );
  220. $('#dlg_'+me.id).dialog('close');
  221. return false;
  222. }
  223. this.UpdateSizes = function(event, ui)
  224. {
  225. var dlg = $('#dlg_'+me.id);
  226. var searchForm = $('#SearchFormToAdd_'+me.id);
  227. var results = $('#SearchResultsToAdd_'+me.id);
  228. padding_right = parseInt(dlg.css('padding-right').replace('px', ''));
  229. padding_left = parseInt(dlg.css('padding-left').replace('px', ''));
  230. padding_top = parseInt(dlg.css('padding-top').replace('px', ''));
  231. padding_bottom = parseInt(dlg.css('padding-bottom').replace('px', ''));
  232. width = dlg.innerWidth() - padding_right - padding_left - 22; // 5 (margin-left) + 5 (padding-left) + 5 (padding-right) + 5 (margin-right) + 2 for rounding !
  233. height = dlg.innerHeight() - padding_top - padding_bottom -22;
  234. wizard = dlg.find('.wizContainer:first');
  235. wizard.width(width);
  236. wizard.height(height);
  237. form_height = searchForm.outerHeight();
  238. results.height(height - form_height - 40); // Leave some space for the buttons
  239. }
  240. }