utils.js 4.0 KB

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