linkswidget.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. $(sSearchAreaId).unblock();
  130. },
  131. 'html'
  132. );
  133. return false; // Don't submit the form, stay in the current page !
  134. };
  135. this.UpdateButtons = function(iCount)
  136. {
  137. var okBtn = $('#btn_ok_'+me.id);
  138. if (iCount > 0)
  139. {
  140. okBtn.removeAttr('disabled');
  141. }
  142. else
  143. {
  144. okBtn.attr('disabled', 'disabled');
  145. }
  146. };
  147. this.DoAddObjects = function()
  148. {
  149. var theMap = { sAttCode: me.sAttCode,
  150. iInputId: me.iInputId,
  151. sSuffix: me.sSuffix,
  152. bDuplicates: me.bDuplicates,
  153. 'class': me.sClass
  154. };
  155. // Gather the parameters from the search form
  156. var context = $('#SearchResultsToAdd_'+me.id);
  157. var selectionMode = $(':input[name=selectionMode]', context);
  158. if (selectionMode.length > 0)
  159. {
  160. // Paginated table retrieve the mode and the exceptions
  161. var sMode = selectionMode.val();
  162. theMap['selectionMode'] = sMode;
  163. $('#fs_SearchFormToAdd_'+me.id+' :input').each(
  164. function(i)
  165. {
  166. theMap[this.name] = this.value;
  167. }
  168. );
  169. theMap['sRemoteClass'] = theMap['class']; // swap 'class' (defined in the form) and 'remoteClass'
  170. theMap['class'] = me.sClass;
  171. $(' :input[name^=storedSelection]', context).each(function() {
  172. if (theMap[this.name] == undefined)
  173. {
  174. theMap[this.name] = new Array();
  175. }
  176. theMap[this.name].push(this.value);
  177. $(this).remove(); // Remove the selection for the next time the dialog re-opens
  178. });
  179. // Retrieve the 'filter' definition
  180. var table = $('#ResultsToAdd_'+me.id).find('table.listResults')[0];
  181. theMap['filter'] = table.config.filter;
  182. theMap['extra_params'] = table.config.extra_params;
  183. }
  184. // else
  185. // {
  186. // Normal table, retrieve all the checked check-boxes
  187. $(':checked[name^=selectObject]', context).each(
  188. function(i)
  189. {
  190. if ( (this.name != '') && ((this.type != 'checkbox') || (this.checked)) )
  191. {
  192. //console.log(this.type);
  193. arrayExpr = /\[\]$/;
  194. if (arrayExpr.test(this.name))
  195. {
  196. // Array
  197. if (theMap[this.name] == undefined)
  198. {
  199. theMap[this.name] = new Array();
  200. }
  201. theMap[this.name].push(this.value);
  202. }
  203. else
  204. {
  205. theMap[this.name] = this.value;
  206. }
  207. }
  208. $(this).parents('tr:first').remove(); // Remove the whole line, so that, next time the dialog gets displayed it's no longer there
  209. }
  210. );
  211. // }
  212. theMap['operation'] = 'doAddObjects';
  213. if (me.oWizardHelper == null)
  214. {
  215. theMap['json'] = '';
  216. }
  217. else
  218. {
  219. // Not inside a "search form", updating a real object
  220. me.oWizardHelper.UpdateWizard();
  221. theMap['json'] = me.oWizardHelper.ToJSON();
  222. }
  223. $('#busy_'+me.iInputId).html('&nbsp;<img src="../images/indicator.gif"/>');
  224. // Run the query and display the results
  225. $.post( GetAbsoluteUrlAppRoot()+'pages/ajax.render.php', theMap,
  226. function(data)
  227. {
  228. //console.log('Data: ' + data);
  229. if (data != '')
  230. {
  231. $('#'+me.id+'_empty_row').hide();
  232. $('#linkedset_'+me.id+' .listResults tbody').prepend(data);
  233. $('#linkedset_'+me.id+' .listResults').trigger('update');
  234. $('#linkedset_'+me.id+' .listResults').tableHover();
  235. $('#linkedset_'+me.id+' .listResults').trigger('update').trigger("applyWidgets"); // table is already sortable, just refresh it
  236. $('#linkedset_'+me.id+' :input').each( function() { $(this).trigger('validate', ''); }); // Validate newly added form fields...
  237. $('#busy_'+me.iInputId).html('');
  238. }
  239. },
  240. 'html'
  241. );
  242. $('#dlg_'+me.id).dialog('close');
  243. return false;
  244. };
  245. this.UpdateSizes = function(event, ui)
  246. {
  247. var dlg = $('#dlg_'+me.id);
  248. var searchForm = $('#SearchFormToAdd_'+me.id);
  249. var results = $('#SearchResultsToAdd_'+me.id);
  250. var padding_right = 0;
  251. if (dlg.css('padding-right'))
  252. {
  253. padding_right = parseInt(dlg.css('padding-right').replace('px', ''));
  254. }
  255. var padding_left = 0;
  256. if (dlg.css('padding-left'))
  257. {
  258. padding_left = parseInt(dlg.css('padding-left').replace('px', ''));
  259. }
  260. var padding_top = 0;
  261. if (dlg.css('padding-top'))
  262. {
  263. padding_top = parseInt(dlg.css('padding-top').replace('px', ''));
  264. }
  265. var padding_bottom = 0;
  266. if (dlg.css('padding-bottom'))
  267. {
  268. padding_bottom = parseInt(dlg.css('padding-bottom').replace('px', ''));
  269. }
  270. width = dlg.innerWidth() - padding_right - padding_left - 22; // 5 (margin-left) + 5 (padding-left) + 5 (padding-right) + 5 (margin-right) + 2 for rounding !
  271. height = dlg.innerHeight() - padding_top - padding_bottom -22;
  272. wizard = dlg.find('.wizContainer:first');
  273. wizard.width(width);
  274. wizard.height(height);
  275. form_height = searchForm.outerHeight();
  276. results.height(height - form_height - 40); // Leave some space for the buttons
  277. };
  278. this.GetUpdatedValue = function()
  279. {
  280. var sSelector = '#linkedset_'+me.id+' input[name^=attr_'+me.id+']';
  281. var aIndexes = [];
  282. var aValues = [];
  283. $(sSelector).each(function() {
  284. var re = /\[([^\[]+)\]\[(.+)\]/;
  285. var aMatches = [];
  286. if (aMatches = this.name.match(re))
  287. {
  288. var idx = aMatches[1];
  289. var index = jQuery.inArray(idx, aIndexes);
  290. if (index == -1)
  291. {
  292. aIndexes.push(idx);
  293. index = jQuery.inArray(idx, aIndexes);
  294. aValues[index] = {};
  295. }
  296. var value = $(this).val();
  297. if (aMatches[2] == "id")
  298. {
  299. var iId = parseInt(aMatches[1], 10);
  300. if (iId < 0)
  301. {
  302. aValues[index][me.sExtKeyToRemote] = -iId;
  303. }
  304. else
  305. {
  306. aValues[index]['id'] = value;
  307. }
  308. }
  309. else
  310. {
  311. aValues[index][aMatches[2]] = value;
  312. }
  313. }
  314. });
  315. return JSON.stringify(aValues);
  316. };
  317. }