linkswidget.js 8.6 KB

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