linkswidget.js 9.8 KB

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