linkswidget.js 8.3 KB

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