utils.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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').tablesorter( { widgets: ['zebra', 'truncatedList']} ); // sortable and zebra tables
  17. //$('#'+divId).unblockUI();
  18. }
  19. );
  20. }
  21. /**
  22. * Truncate a previously expanded list !
  23. */
  24. function TruncateList(divId, iLimit, sNewLabel, sLinkLabel)
  25. {
  26. var iCount = 0;
  27. $('#'+divId+' table.listResults tr:gt('+iLimit+')').each( function(){
  28. $(this).remove();
  29. });
  30. $('#lbl_'+divId).html(sNewLabel);
  31. $('#'+divId+' table.listResults tr:last td').addClass('truncated');
  32. $('#'+divId+' table.listResults').addClass('truncated');
  33. $('#trc_'+divId).html(sLinkLabel);
  34. $('#'+divId+' .listResults').trigger("update"); // Reset the cache
  35. }
  36. /**
  37. * Reload any block -- used for periodic auto-reload
  38. */
  39. function ReloadBlock(divId, sStyle, sSerializedFilter, sExtraParams)
  40. {
  41. $('#'+divId).addClass('loading');
  42. //$('#'+divId).blockUI();
  43. $.post('ajax.render.php?style='+sStyle,
  44. { operation: 'ajax', filter: sSerializedFilter, extra_params: sExtraParams },
  45. function(data){
  46. $('#'+divId).empty();
  47. $('#'+divId).append(data);
  48. $('#'+divId).removeClass('loading');
  49. $('#'+divId+' .listResults').tableHover(); // hover tables
  50. $('#'+divId+' .listResults').tablesorter( { widgets: ['myZebra', 'truncatedList']} ); // sortable and zebra tables
  51. //$('#'+divId).unblockUI();
  52. }
  53. );
  54. }
  55. /**
  56. * Update the display and value of a file input widget when the user picks a new file
  57. */
  58. function UpdateFileName(id, sNewFileName)
  59. {
  60. var aPath = sNewFileName.split('\\');
  61. var sNewFileName = aPath[aPath.length-1];
  62. $('#'+id).val(sNewFileName);
  63. $('#'+id).trigger('validate');
  64. $('#name_'+id).text(sNewFileName);
  65. return true;
  66. }
  67. /**
  68. * Reload a search form for the specified class
  69. */
  70. function ReloadSearchForm(divId, sClassName, sBaseClass)
  71. {
  72. var oDiv = $('#'+divId);
  73. oDiv.block();
  74. var oFormEvents = $('#'+divId+' form').data('events');
  75. // Save the submit handlers
  76. aSubmit = new Array();
  77. if ( (oFormEvents != null) && (oFormEvents.submit != undefined))
  78. {
  79. aSubmit = oFormEvents.submit;
  80. }
  81. $.post('ajax.render.php',
  82. { operation: 'search_form', className: sClassName, baseClass: sBaseClass, currentId: divId },
  83. function(data) {
  84. oDiv.empty();
  85. oDiv.append(data);
  86. if (aSubmit.length > 0)
  87. {
  88. var oForm = $('#'+divId+' form'); // Form was reloaded, recompute it
  89. for(index = 0; index < aSubmit.length; index++)
  90. {
  91. // Restore the previously bound submit handlers
  92. if (aSubmit[index].data != undefined)
  93. {
  94. oForm.bind('submit.'+aSubmit[index].namespace, aSubmit[index].data, aSubmit[index].handler)
  95. }
  96. else
  97. {
  98. oForm.bind('submit.'+aSubmit[index].namespace, aSubmit[index].handler)
  99. }
  100. }
  101. }
  102. oDiv.unblock();
  103. oDiv.parent().resize(); // Inform the parent that the form has just been (potentially) resized
  104. }
  105. );
  106. }
  107. /**
  108. * Stores - in a persistent way - user specific preferences
  109. * depends on a global variable oUserPreferences created/filled by the iTopWebPage
  110. * that acts as a local -write through- cache
  111. */
  112. function SetUserPreference(sPreferenceCode, sPrefValue, bPersistent)
  113. {
  114. sPreviousValue = undefined;
  115. try
  116. {
  117. sPreviousValue = oUserPreferences[sPreferenceCode];
  118. }
  119. catch(err)
  120. {
  121. sPreviousValue = undefined;
  122. }
  123. oUserPreferences[sPreferenceCode] = sPrefValue;
  124. if (bPersistent && (sPrefValue != sPreviousValue))
  125. {
  126. ajax_request = $.post('ajax.render.php',
  127. { operation: 'set_pref', code: sPreferenceCode, value: sPrefValue} ); // Make it persistent
  128. }
  129. }
  130. /**
  131. * Get user specific preferences
  132. * depends on a global variable oUserPreferences created/filled by the iTopWebPage
  133. * that acts as a local -write through- cache
  134. */
  135. function GetUserPreference(sPreferenceCode, sDefaultValue)
  136. {
  137. var value = sDefaultValue;
  138. if ( oUserPreferences[sPreferenceCode] != undefined)
  139. {
  140. value = oUserPreferences[sPreferenceCode];
  141. }
  142. return value;
  143. }