linkswidget.js 7.4 KB

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