utils.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // Some general purpose JS functions for the iTop application
  2. /**
  3. * Reload a truncated list
  4. */
  5. function ReloadTruncatedList(divId, sSerializedFilter, sExtraParams)
  6. {
  7. $('#'+divId).addClass('loading');
  8. //$('#'+divId).blockUI();
  9. $.post('ajax.render.php?style=list',
  10. { operation: 'ajax', filter: sSerializedFilter, extra_params: sExtraParams },
  11. function(data){
  12. $('#'+divId).empty();
  13. $('#'+divId).append(data);
  14. $('#'+divId).removeClass('loading');
  15. $('#'+divId+' .listResults').tableHover(); // hover tables
  16. $('#'+divId+' .listResults').each( function()
  17. {
  18. var table = $(this);
  19. var id = $(this).parent();
  20. var checkbox = (table.find('th:first :checkbox').length > 0);
  21. if (checkbox)
  22. {
  23. // There is a checkbox in the first column, don't make it sortable
  24. table.tablesorter( { headers: { 0: {sorter: false}}, widgets: ['myZebra', 'truncatedList']} ); // sortable and zebra tables
  25. }
  26. else
  27. {
  28. // There is NO checkbox in the first column, all columns are considered sortable
  29. table.tablesorter( { widgets: ['myZebra', 'truncatedList']} ); // sortable and zebra tables
  30. }
  31. });
  32. //$('#'+divId).unblockUI();
  33. }
  34. );
  35. }
  36. /**
  37. * Truncate a previously expanded list !
  38. */
  39. function TruncateList(divId, iLimit, sNewLabel, sLinkLabel)
  40. {
  41. var iCount = 0;
  42. $('#'+divId+' table.listResults tr:gt('+iLimit+')').each( function(){
  43. $(this).remove();
  44. });
  45. $('#lbl_'+divId).html(sNewLabel);
  46. $('#'+divId+' table.listResults tr:last td').addClass('truncated');
  47. $('#'+divId+' table.listResults').addClass('truncated');
  48. $('#trc_'+divId).html(sLinkLabel);
  49. $('#'+divId+' .listResults').trigger("update"); // Reset the cache
  50. }
  51. /**
  52. * Reload any block -- used for periodic auto-reload
  53. */
  54. function ReloadBlock(divId, sStyle, sSerializedFilter, sExtraParams)
  55. {
  56. $('#'+divId).addClass('loading');
  57. //$('#'+divId).blockUI();
  58. $.post('ajax.render.php?style='+sStyle,
  59. { operation: 'ajax', filter: sSerializedFilter, extra_params: sExtraParams },
  60. function(data){
  61. $('#'+divId).empty();
  62. $('#'+divId).append(data);
  63. $('#'+divId).removeClass('loading');
  64. $('#'+divId+' .listResults').tableHover(); // hover tables
  65. $('#'+divId+' .listResults').each( function()
  66. {
  67. var table = $(this);
  68. var id = $(this).parent();
  69. var checkbox = (table.find('th:first :checkbox').length > 0);
  70. if (checkbox)
  71. {
  72. // There is a checkbox in the first column, don't make it sortable
  73. table.tablesorter( { headers: { 0: {sorter: false}}, widgets: ['myZebra', 'truncatedList']} ); // sortable and zebra tables
  74. }
  75. else
  76. {
  77. // There is NO checkbox in the first column, all columns are considered sortable
  78. table.tablesorter( { widgets: ['myZebra', 'truncatedList']} ); // sortable and zebra tables
  79. }
  80. });
  81. //$('#'+divId).unblockUI();
  82. }
  83. );
  84. }
  85. /**
  86. * Update the display and value of a file input widget when the user picks a new file
  87. */
  88. function UpdateFileName(id, sNewFileName)
  89. {
  90. var aPath = sNewFileName.split('\\');
  91. var sNewFileName = aPath[aPath.length-1];
  92. $('#'+id).val(sNewFileName);
  93. $('#'+id).trigger('validate');
  94. $('#name_'+id).text(sNewFileName);
  95. return true;
  96. }
  97. /**
  98. * Reload a search form for the specified class
  99. */
  100. function ReloadSearchForm(divId, sClassName, sBaseClass, sContext)
  101. {
  102. var oDiv = $('#'+divId);
  103. oDiv.block();
  104. var oFormEvents = $('#'+divId+' form').data('events');
  105. var aSubmit = new Array();
  106. // Save the submit handlers
  107. aSubmit = new Array();
  108. if ( (oFormEvents != null) && (oFormEvents.submit != undefined))
  109. {
  110. for(index = 0; index < oFormEvents.submit.length; index++)
  111. {
  112. aSubmit [index ] = { data:oFormEvents.submit[index].data, namespace:oFormEvents.submit[index].namespace, handler: oFormEvents.submit[index].handler};
  113. }
  114. }
  115. $.post('ajax.render.php?'+sContext,
  116. { operation: 'search_form', className: sClassName, baseClass: sBaseClass, currentId: divId },
  117. function(data) {
  118. oDiv.empty();
  119. oDiv.append(data);
  120. if (aSubmit.length > 0)
  121. {
  122. var oForm = $('#'+divId+' form'); // Form was reloaded, recompute it
  123. for(index = 0; index < aSubmit.length; index++)
  124. {
  125. // Restore the previously bound submit handlers
  126. if (aSubmit[index].data != undefined)
  127. {
  128. oForm.bind('submit.'+aSubmit[index].namespace, aSubmit[index].data, aSubmit[index].handler)
  129. }
  130. else
  131. {
  132. oForm.bind('submit.'+aSubmit[index].namespace, aSubmit[index].handler)
  133. }
  134. }
  135. }
  136. oDiv.unblock();
  137. oDiv.parent().resize(); // Inform the parent that the form has just been (potentially) resized
  138. }
  139. );
  140. }
  141. /**
  142. * Stores - in a persistent way - user specific preferences
  143. * depends on a global variable oUserPreferences created/filled by the iTopWebPage
  144. * that acts as a local -write through- cache
  145. */
  146. function SetUserPreference(sPreferenceCode, sPrefValue, bPersistent)
  147. {
  148. sPreviousValue = undefined;
  149. try
  150. {
  151. sPreviousValue = oUserPreferences[sPreferenceCode];
  152. }
  153. catch(err)
  154. {
  155. sPreviousValue = undefined;
  156. }
  157. oUserPreferences[sPreferenceCode] = sPrefValue;
  158. if (bPersistent && (sPrefValue != sPreviousValue))
  159. {
  160. ajax_request = $.post('ajax.render.php',
  161. { operation: 'set_pref', code: sPreferenceCode, value: sPrefValue} ); // Make it persistent
  162. }
  163. }
  164. /**
  165. * Get user specific preferences
  166. * depends on a global variable oUserPreferences created/filled by the iTopWebPage
  167. * that acts as a local -write through- cache
  168. */
  169. function GetUserPreference(sPreferenceCode, sDefaultValue)
  170. {
  171. var value = sDefaultValue;
  172. if ( oUserPreferences[sPreferenceCode] != undefined)
  173. {
  174. value = oUserPreferences[sPreferenceCode];
  175. }
  176. return value;
  177. }
  178. /**
  179. * Check/uncheck a whole list of checkboxes
  180. */
  181. function CheckAll(sSelector, bValue)
  182. {
  183. var value = bValue;
  184. $(sSelector).each( function() {
  185. this.checked = value;
  186. });
  187. }