linkswidget.js 6.9 KB

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