linkswidget.js 9.7 KB

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