utils.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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)
  101. {
  102. var oDiv = $('#'+divId);
  103. oDiv.block();
  104. var oFormEvents = $('#'+divId+' form').data('events');
  105. // Save the submit handlers
  106. aSubmit = new Array();
  107. if ( (oFormEvents != null) && (oFormEvents.submit != undefined))
  108. {
  109. aSubmit = oFormEvents.submit;
  110. }
  111. $.post('ajax.render.php',
  112. { operation: 'search_form', className: sClassName, baseClass: sBaseClass, currentId: divId },
  113. function(data) {
  114. oDiv.empty();
  115. oDiv.append(data);
  116. if (aSubmit.length > 0)
  117. {
  118. var oForm = $('#'+divId+' form'); // Form was reloaded, recompute it
  119. for(index = 0; index < aSubmit.length; index++)
  120. {
  121. // Restore the previously bound submit handlers
  122. if (aSubmit[index].data != undefined)
  123. {
  124. oForm.bind('submit.'+aSubmit[index].namespace, aSubmit[index].data, aSubmit[index].handler)
  125. }
  126. else
  127. {
  128. oForm.bind('submit.'+aSubmit[index].namespace, aSubmit[index].handler)
  129. }
  130. }
  131. }
  132. oDiv.unblock();
  133. oDiv.parent().resize(); // Inform the parent that the form has just been (potentially) resized
  134. }
  135. );
  136. }
  137. /**
  138. * Stores - in a persistent way - user specific preferences
  139. * depends on a global variable oUserPreferences created/filled by the iTopWebPage
  140. * that acts as a local -write through- cache
  141. */
  142. function SetUserPreference(sPreferenceCode, sPrefValue, bPersistent)
  143. {
  144. sPreviousValue = undefined;
  145. try
  146. {
  147. sPreviousValue = oUserPreferences[sPreferenceCode];
  148. }
  149. catch(err)
  150. {
  151. sPreviousValue = undefined;
  152. }
  153. oUserPreferences[sPreferenceCode] = sPrefValue;
  154. if (bPersistent && (sPrefValue != sPreviousValue))
  155. {
  156. ajax_request = $.post('ajax.render.php',
  157. { operation: 'set_pref', code: sPreferenceCode, value: sPrefValue} ); // Make it persistent
  158. }
  159. }
  160. /**
  161. * Get user specific preferences
  162. * depends on a global variable oUserPreferences created/filled by the iTopWebPage
  163. * that acts as a local -write through- cache
  164. */
  165. function GetUserPreference(sPreferenceCode, sDefaultValue)
  166. {
  167. var value = sDefaultValue;
  168. if ( oUserPreferences[sPreferenceCode] != undefined)
  169. {
  170. value = oUserPreferences[sPreferenceCode];
  171. }
  172. return value;
  173. }