linkswidget.js 5.5 KB

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