linkswidget.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. // JavaScript Document
  2. function LinksWidget(id, sClass, sAttCode, iInputId, sSuffix, bDuplicates)
  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. var me = this;
  11. this.Init = function()
  12. {
  13. // make sure that the form is clean
  14. $('#linkedset_'+this.id+' .selection').each( function() { this.checked = false; });
  15. $('#'+this.id+'_btnRemove').attr('disabled','disabled');
  16. $('#'+this.id+'_linksToRemove').val('');
  17. }
  18. this.RemoveSelected = function()
  19. {
  20. var my_id = '#'+me.id;
  21. $('#linkedset_'+me.id+' .selection:checked').each(
  22. function()
  23. {
  24. $linksToRemove = $(my_id+'_linksToRemove');
  25. prevValue = $linksToRemove.val();
  26. if (prevValue != '')
  27. {
  28. $linksToRemove.val(prevValue + ',' + this.value);
  29. }
  30. else
  31. {
  32. $linksToRemove.val(this.value);
  33. }
  34. $(my_id+'_row_'+this.value).remove();
  35. }
  36. );
  37. // Disable the button since all the selected items have been removed
  38. $(my_id+'_btnRemove').attr('disabled','disabled');
  39. // Re-run the zebra plugin to properly highlight the remaining lines & and take into account the removed ones
  40. $('#linkedset_'+this.id+' .listResults').trigger('update').trigger("applyWidgets");
  41. }
  42. this.OnSelectChange = function()
  43. {
  44. var nbChecked = $('#linkedset_'+me.id+' .selection:checked').length;
  45. if (nbChecked > 0)
  46. {
  47. $('#'+me.id+'_btnRemove').attr('disabled','');
  48. }
  49. else
  50. {
  51. $('#'+me.id+'_btnRemove').attr('disabled','disabled');
  52. }
  53. }
  54. this.AddObjects = function()
  55. {
  56. $('#dlg_'+me.id).dialog('open');
  57. this.UpdateSizes(null, null);
  58. }
  59. this.SearchObjectsToAdd = function()
  60. {
  61. var theMap = { sAttCode: me.sAttCode,
  62. iInputId: me.iInputId,
  63. sSuffix: me.sSuffix,
  64. bDuplicates: me.bDuplicates
  65. }
  66. // Gather the parameters from the search form
  67. $('#SearchFormToAdd_'+me.id+' :input').each(
  68. function(i)
  69. {
  70. if (this.name != '')
  71. {
  72. theMap[this.name] = this.value;
  73. }
  74. }
  75. );
  76. // Gather the already linked target objects
  77. theMap.aAlreadyLinked = new Array();
  78. $('#linkedset_'+me.id+' .selection:input').each(
  79. function(i)
  80. {
  81. theMap.aAlreadyLinked.push(this.value);
  82. }
  83. );
  84. theMap['sRemoteClass'] = theMap['class']; // swap 'class' (defined in the form) and 'remoteClass'
  85. theMap['class'] = me.sClass;
  86. theMap.operation = 'searchObjectsToAdd'; // Override what is defined in the form itself
  87. sSearchAreaId = '#SearchResultsToAdd_'+me.id;
  88. $(sSearchAreaId).block();
  89. // Run the query and display the results
  90. $.post( 'ajax.render.php', theMap,
  91. function(data)
  92. {
  93. $(sSearchAreaId).html(data);
  94. $(sSearchAreaId+' .listResults').tableHover();
  95. $(sSearchAreaId+' .listResults').tablesorter( { headers: {0: {sorter: false}}, widgets: ['myZebra', 'truncatedList']} ); // sortable and zebra tables
  96. $(sSearchAreaId).unblock();
  97. },
  98. 'html'
  99. );
  100. return false; // Don't submit the form, stay in the current page !
  101. }
  102. this.DoAddObjects = function()
  103. {
  104. var theMap = { sAttCode: me.sAttCode,
  105. iInputId: me.iInputId,
  106. sSuffix: me.sSuffix,
  107. bDuplicates: me.bDuplicates,
  108. 'class': me.sClass
  109. }
  110. // Gather the parameters from the search form
  111. $('#SearchResultsToAdd_'+me.id+' :checked').each(
  112. function(i)
  113. {
  114. if ( (this.name != '') && ((this.type != 'checkbox') || (this.checked)) )
  115. {
  116. //console.log(this.type);
  117. arrayExpr = /\[\]$/;
  118. if (arrayExpr.test(this.name))
  119. {
  120. // Array
  121. if (theMap[this.name] == undefined)
  122. {
  123. theMap[this.name] = new Array();
  124. }
  125. theMap[this.name].push(this.value);
  126. }
  127. else
  128. {
  129. theMap[this.name] = this.value;
  130. }
  131. $(this).parents('tr:first').remove(); // Remove the whole line, so that, next time the dialog gets displayed it's no longer there
  132. }
  133. }
  134. );
  135. theMap['operation'] = 'doAddObjects';
  136. $('#busy_'+me.iInputId).html('&nbsp;<img src="../images/indicator.gif"/>');
  137. // Run the query and display the results
  138. $.post( 'ajax.render.php', theMap,
  139. function(data)
  140. {
  141. //console.log('Data: ' + data);
  142. if (data != '')
  143. {
  144. $('#'+me.id+'_empty_row').remove();
  145. $('#linkedset_'+me.id+' .listResults tbody').append(data);
  146. $('#linkedset_'+me.id+' .listResults').trigger('update');
  147. $('#linkedset_'+me.id+' .listResults').tableHover();
  148. $('#linkedset_'+me.id+' .listResults').trigger('update').trigger("applyWidgets"); // table is already sortable, just refresh it
  149. $('#linkedset_'+me.id+' :input').each( function() { $(this).trigger('validate', ''); }); // Validate newly added form fields...
  150. $('#busy_'+me.iInputId).html('');
  151. }
  152. },
  153. 'html'
  154. );
  155. $('#dlg_'+me.id).dialog('close');
  156. return false;
  157. }
  158. this.UpdateSizes = function(event, ui)
  159. {
  160. var dlg = $('#dlg_'+me.id);
  161. var searchForm = $('#SearchFormToAdd_'+me.id);
  162. var results = $('#SearchResultsToAdd_'+me.id);
  163. padding_right = parseInt(dlg.css('padding-right').replace('px', ''));
  164. padding_left = parseInt(dlg.css('padding-left').replace('px', ''));
  165. padding_top = parseInt(dlg.css('padding-top').replace('px', ''));
  166. padding_bottom = parseInt(dlg.css('padding-bottom').replace('px', ''));
  167. width = dlg.innerWidth() - padding_right - padding_left - 22; // 5 (margin-left) + 5 (padding-left) + 5 (padding-right) + 5 (margin-right) + 2 for rounding !
  168. height = dlg.innerHeight() - padding_top - padding_bottom -22;
  169. wizard = dlg.find('.wizContainer:first');
  170. wizard.width(width);
  171. wizard.height(height);
  172. form_height = searchForm.outerHeight();
  173. results.height(height - form_height - 40); // Leave some space for the buttons
  174. }
  175. }