linkswidget.js 9.6 KB

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